#!/usr/bin/php
<?php

/**
 * Automatic Currency Update v4.0
 * Originally by Richard Fink (masterblaster)  based on Zen Cart manual currency update
 * updated by Kuroi to include Zen Cart's currency uplift ratio
 * further updated by Kuroi to use European Central Bank reference rates  (adapted from ECB-supplied code)
 *
 * Rework for PHP 5.3 compatibility (ereg and mysql_db_query functions deprecated in PHP 5.3.0)
 * switched to using SimpleXML and mysqli instead.
 * JeRo www.jero.co.nz 18/06/2010
 *
 * license: GNU Public License V2.0
 *
 **/

# Set this to the physical path of your installation.
# Make sure to include the trailing "/"
define('PATH_TO_STORE_ROOT','/home/livingnd/public_html/products/');

// Get Zen Cart configuration data
require_once(PATH_TO_STORE_ROOT . 'includes/configure.php');
require_once(PATH_TO_STORE_ROOT . 'includes/database_tables.php');

if (! $mysqli=new mysqli(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE))
    die("Can't connect to DB");
else
	echo "connected";

# Get default currency.
$row=$mysqli->query("select configuration_value from " . TABLE_CONFIGURATION .
                    " WHERE configuration_key='DEFAULT_CURRENCY' LIMIT 1 ")->fetch_row();
define('DEFAULT_CURRENCY', $row[0]);

#Get currency conversion ratio.
$row=$mysqli->query("select configuration_value from " . TABLE_CONFIGURATION .
                    " WHERE configuration_key='CURRENCY_UPLIFT_RATIO' LIMIT 1 ")->fetch_row();
define('CURRENCY_UPLIFT_RATIO', $row[0]);

# Create new SimpleXML element from the Euro zone file
# If your PHP configuration does not have allow_url_fopen, you may need to
# write a script to pick it up using wget:
# wget -O eurofxref-daily.xml www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
# and then uncomment/comment these next two lines:
#$xml = new SimpleXMLElement(file_get_contents("eurofxref-daily.xml"));
$xml= new SimpleXMLElement(file_get_contents("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"));

# extract your preferred currency's exchange rate.
$nzd = $xml->xpath("//*[@currency='".DEFAULT_CURRENCY."']");
$base = (float)$nzd[0]['rate'];

# If our default currency is the Euro, it won't be in the XML,
# so set the base exchange rate to 1
if (DEFAULT_CURRENCY == 'EUR')
    $base=(float)1;

# Get array of currency data from the XML file.
$currencies=$xml->xpath("//*[@currency]");

# Use a prepared query for slightly faster execution.
# Note this is an update, not and insert or replace.
# This statement assumes that your currency codes are the same
# as the international standard ones.
$stmt=$mysqli->prepare("update ".TABLE_CURRENCIES. " set value=?,last_updated=now() where code=?");


# Iterate over array, pulling out currency code and rate.
# Convert into your preferred currency.
# Pump it into the database.
foreach ($currencies as $curr) {
    $cc  = $curr[0]['currency'];
    $rr  = (float)$curr[0]['rate'] * CURRENCY_UPLIFT_RATIO;
    $new = $rr/$base;
# But don't uplift your default currency!
    if ( $cc != DEFAULT_CURRENCY ) {
        $stmt->bind_param("ds",$new,$cc);

        $stmt->execute();
    }
}

# Feed is based on the Euro, which is not included in the feed,
# therefore we need to update it manually,
# unless of course our preferred currency is the Euro
$cc='EUR';
$rr=1/$base*CURRENCY_UPLIFT_RATIO;
if (DEFAULT_CURRENCY != $cc) {
    $stmt->bind_param("ds",$rr,$cc);
    $stmt->execute();
}

$mysqli->close();
echo " all done";
?>