'; print currency_rate('EUR'); print '
'; print convert_currency(20, 'USD', 3); print '
'; */ } //———————————————————————————————————————————————————-+ // Script functions: + //———————————————————————————————————————————————————-+ // list currencies in various formats // list_currencies($format) // convert a given amount into the currency code // convert_currency($amount, $code, $dp) // returns the rate for a code or 0 // currency_rate($code) // go through data to fill an array // get_currencies_array() // retrieve currency data from cache or from live data // get_currencies_cache() // retrieve the response from the info service // get_currencies() //———————————————————————————————————————————————————-+ // interface function to list_currencies function html_currencies($format) { // return html string $html = ''; // what format is specified? // list format if ($format == 'list') { $html = list_currencies(3, '', ' (', ')
'); } // table format if ($format == 'table') { $html = ""; $html .= list_currencies(3, ""); $html .= "
", " ", "
"; } // return html string return $html; } // list currencies function list_currencies($dp, $sep1, $sep2, $sep3) { global $currencies; // check its an array if (!is_array($currencies)) return; // go thru array to construct list $list = ''; foreach ($currencies as $fld=>$value) { // check its numeric if (!is_numeric($value)) $value = 0; // format dp $value = number_format($value, $dp); // add to list with separators $list .= $sep1 . $fld . $sep2 . $value . $sep3; } return $list; } //———————————————————————————————————————————————————-+ // output JavaScript code function writeJavaScript($ts,$ch,$dt){ // format the data to enable it to be passed to JavaScript $dta = str_replace("\n", "\\n' + \n'", Trim($dt)); // output intro print ' // The <pre> tag enables the code to be viewed in a browser... //
// worldpay_currency.php
// By: Nick Sumner, dev@nicksumner.com 
// ECommerce Integration - http://www.tele-pro.co.uk/
// Running in JavaScript mode
// Rates Date: ' . $ts . '
// Data Cached: ' . boolToStr($ch) . '

// variable indicates if data cached
var fromCache = ' . boolToStr($ch) . ';

// variable for date of cache
var ratesDate = \'' . $ts . '\';

// variable contains raw data 
var rawCurrencyData = \'' . $dta . '\';

// variable for text list
var htmlCurrencyList = \'' . html_currencies('list') . '\';

// variable for table list
var htmlCurrencyTable = \'' . html_currencies('table') . '\';

// define Array for Currencies
var WorldpayCurrencies = new Array();
';
  
  // output JavaScript code to build an array 
  print list_currencies(8, "WorldpayCurrencies['", "']='", "';\n");
  
  // output JavaScript WorldpayConvert function
   print '
// Convert amount with Worldpay currency rate
function WorldpayConvert(amount, currency, dp){
  var result=0;
  // get the rate
  var rate = WorldpayCurrencies[currency];
  // does the currency exist?
  if (rate == 0) return result;
  // convert the amount to the currency
  result = (amount * rate);
  // number format dp
  result = numberFormat(result, dp);
  // return
  return result;
}

// format number n to p decimal places
function numberFormat(n, p){
  var d = Math.pow(10,p);
  var r = (Math.round(d * n) /d);
  return r;
}
';

}

//———————————————————————————————————————————————————-+

// convert a given amount into the currency code

function convert_currency($amount, $code, $dp) {
  // default return value
  $result = 0;
  
  // get the rate
  $rate = currency_rate($code);
  
  // does the currency exist?
  if ($rate == 0) return $result;
  
  // convert the amount to the currency
  $result = ($amount * $rate);
  
  // decimal places
  $result = number_format($result, $dp);
  
  // return
  return $result;
}

//———————————————————————————————————————————————————-+

// returns the rate for a code, or 0 if not in array

function currency_rate($code) {
  global $currencies;
  
  // default return value
  $rate = 0;
  
  if (is_array($currencies))
    if (isset($currencies[$code]))
      $rate = $currencies[$code];
    
  return $rate;
}

//———————————————————————————————————————————————————-+

// go through data to fill an array

function get_currencies_array($str_data) {
  global $base_currency;
  
  // split the data into lines
  $result = array();
  $result = explode( "\n", $str_data);
  
  // go through the lines looking for currency data
  // and adding it to the clean array
  $clean = array();
  $currency_id = $base_currency . '_' ;
  
  foreach ($result as $fld=>$value) {  
    if (Trim($value) != '')
      if ($value[0] != '#') 
        if (strpos($value, '.') > 0) 
          if (strpos($value, '_') > 0) 
            if (strpos($value, $currency_id ) == 0) {
              // this is a currency line
              
              // extract the currency code
              $currency_code = substr(Trim($value), 
                strlen($currency_id),
                strpos($value, '=') - strlen($currency_id));
               
              // extract the currency rate
              $clean[$currency_code] = substr(Trim($value),
                (1 + strlen($currency_code) + strlen($currency_id)),
                strlen(Trim($value)));
            }
          
  } // for
  
  // return array
  return $clean;
}

//———————————————————————————————————————————————————-+

// retrieve currency data from cache or from live data

function get_currencies_cache(){ 
  global $from_cache;
  global $DateToday;
  
  $raw = '';
  
  // compare with cache data
  $rateDateCached = get_php_app('rateDate');
  $from_cache = ($DateToday == $rateDateCached);
  
  // are they the same?
  if ($from_cache) {
     // we can use the cache data
  	$raw = get_php_app('rateData');
  } 
  if ($raw == '') {
    // get data and cache it
  	$from_cache = false;
  	
  	// get the raw response  
  	$raw = get_currencies();
  	
  	// extract the "rateDateString" 
  	$rateDateString = getIniValue($raw, 'rateDateString');
  	
  	// store the date of this data
  	set_php_app('rateDate', $rateDateString);
  	
  	// store this data
  	set_php_app('rateData', $raw);
  }
  
  // return string
  return $raw; 
} //function


//———————————————————————————————————————————————————-+

// retrieve the response from the info service and 
// return it as a raw string

function get_currencies(){
  // use the global variables 
  global $worldpay_inst_id;
  global $worldpay_host; 

  // get the response
  $result = get_http( $worldpay_host . $worldpay_inst_id, '' );
 
  // return it
  return $result;
}

//———————————————————————————————————————————————————-+
// Generic functions:                                 +
//———————————————————————————————————————————————————-+

// option_explicit($errno,$errstr,$errfile,$errline)
// server($var)
// get_query($var)
// get_http( $host)
// write2file($doc, $outputstring)
// write2fileMode($doc,$outputstring,$for_mode)
// fexists($f) 
// file_get_contents1($f)
// add_trailing_slash($str_dir)
// clr_all_php_app()
// list_all_php_app()
// do_all_php_app($perform_action)
// get_php_app($key_name, $str_value)
// set_php_app($key_name, $str_value)
// getIniValue($str, $ky) 
// StringPart($strContains, $strStart, $strEnd, $incl) 

//———————————————————————————————————————————————————-+

// function used as an error handler 
// an error is raised if a variable is not declared

function option_explicit($errno, $errstr, $errfile, $errline) {
  print "
Fatal error: $errstr in $errfile"; print " on line $errline
"; exit(); } //———————————————————————————————————————————————————-+ // functions to avoid undefined index errors // return a possible $_SERVER variable function server($var){ $val = ''; if (isset($_SERVER[$var])) { $val = $_SERVER[$var]; } return $val; } // return a possible $_GET variable function get_query($var){ $val = ''; if (isset($_GET[$var])) { $val = $_GET[$var]; } return $val; } //——————————————————————————————————————————————————// // performs a HTTP GET and returns the whole response function get_http($url){ $fp = fopen($url, 'r'); $output = ''; // while content exists, keep retrieving in chunks while( !feof( $fp ) ) { $output .= fgets( $fp, 1024); } // close the socket connection fclose( $fp); return $output; } //——————————————————————————————————————————————————// // write string to file function write2file($doc, $outputstring ){ write2fileMode($doc, $outputstring, "w"); } //——————————————————————————————————————————————————// // write string to file with Mode function write2fileMode($doc, $outputstring, $for_mode) { if ($doc == '') return; // Set file for opening $fp = fopen($doc, $for_mode); // Check if can write to file if (!$fp) { print "Could not open desired file for writing"; } else { fwrite($fp, $outputstring); } // Close the written file fclose($fp); } //——————————————————————————————————————————————————-+ // file_exists replacement function fexists($f) { if (is_dir($f)) { return false; } else { return (file_exists($f)); } } //——————————————————————————————————————————————————-+ // file_get_contents replacement // file_get_contents is 4.0.0 - 4.3.0 and 5.0.0 + function file_get_contents1($f) { if (fexists($f)) { return implode('', file($f)); } else { // ("File does not exist!"); } } //——————————————————————————————————————————————————-+ // add trailing slash to a string function add_trailing_slash($str_dir){ // work with temp str $str = Trim($str_dir); // if a slash is not the last char then add it if ( (1 + strrpos($str, '\\')) != (strlen($str)) ) $str .= '\\'; // return string return $str; } //——————————————————————————————————————————————————-+ // delete all files function clr_all_php_app(){ // call do_all_php_app with parameter 'delete' return do_all_php_app('delete'); } //——————————————————————————————————————————————————-+ // list all files function list_all_php_app(){ // call do_all_php_app with parameter 'list' return do_all_php_app('list'); } //——————————————————————————————————————————————————-+ // operate on all files function do_all_php_app($perform_action){ global $app_var_dir; global $app_var_file; // settings $max_loop = 1000; // file count variable // -1 means could not open the dir $c = -1; $d = $app_var_dir; // loop thru files in dir $mdir = opendir($d); while(($entry = readdir($mdir)) && ($c < $max_loop)){ // reset file count variable if ($c == -1) $c = 0; // ignore sys folders if (($entry!= ".") && ($entry!= "..")) { // does the file contain the stem of app var files? $gentry = $app_var_file; if (($gentry=='') || (strpos(strtolower($entry), strtolower($gentry))>-1)) { // delete file $filename = Trim($d.$entry); // do what? $perform_action = Trim(strtolower($perform_action)); if ($perform_action == 'delete') unlink($filename); else if ($perform_action == 'list') print ('
'. $filename); // inc count $c = $c + 1; } //if } //if } //while closedir($mdir); // return count return $c; } //function //——————————————————————————————————————————————————-+ // get value of an application variable function get_php_app($key_name){ global $app_var_dir; global $app_var_file; // checks if (Trim($key_name) == '') exit('No key_name was specified in get_php_app'); // $str_value can be blank // construct path of temp file for this variable $app_var = $app_var_dir . $app_var_file . $key_name . '.txt'; // read the value from the file $app_value = file_get_contents1($app_var); // return string return $app_value; } //——————————————————————————————————————————————————-+ // set value of an application variable function set_php_app($key_name, $str_value){ // application variables file global $app_var_dir; global $app_var_file; // checks if (Trim($key_name) == '') exit('No key_name was specified in set_php_app'); // $str_value can be blank // construct path of temp file for this variable $app_var = $app_var_dir . $app_var_file . $key_name . '.txt'; // delete the file if (fexists($app_var)) unlink($app_var); // store string value in file write2file( $app_var, $str_value); // return string value return $str_value; } //——————————————————————————————————————————————————// // return boolean as string function boolToStr($b) { $boolStr = 'false'; if ($b != 0) $boolStr = 'true'; if ($b === true) $boolStr = 'true'; return $boolStr; } //——————————————————————————————————————————————————// // extract key/value function getIniValue($str, $ky){ $tmp = Trim($str); $tmp = StringPart($tmp, $ky . '=', '', 0); $tmp = StringPart($tmp, '', "\n", 0); return $tmp; } //——————————————————————————————————————————————————// // extract part of a string function StringPart($strContains, $strStart, $strEnd, $incl) { $intCopyStart = 0; if ($strStart != ''){ $intCopyStart = strpos($strContains, $strStart); if ($incl != 1) $intCopyStart = $intCopyStart+ strlen($strStart); } $intCopyEnd = strlen($strContains); if ($strEnd != ''){ $intCopyEnd = strpos($strContains, $strEnd, $intCopyStart); if ($incl == 1) $intCopyEnd = $intCopyEnd+ strlen($strEnd); } return substr($strContains,$intCopyStart,$intCopyEnd-$intCopyStart); } //———————————————————————————————————————————————————-+ // end ?>