<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
* XE.com Currency Converter Plugin
*
* @version $Id: convertXE.php 2009-03-12 14:10 martin
*
* MGS Creativa - Web Intelligent Solutions - Argentina -
http://www.mgscreativa.com* Martin Sebastian Brilla Ghia - martin@mgscreativa.com
*
* @package VirtueMart
* @subpackage classes
* @copyright Copyright (C) 2009 Martin Sebastian Brilla Ghia - All rights reserved.
* @license
http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
*
http://virtuemart.net*/
/**
* This class uses the currency rates provided by xe.com
*/
class convertXE {
/********** OPTIONS START **********/
// Set to true to roundup prices IE: $XX.99, set to false to leave
// the exact value of convert server
var $roundUp = false;
// Set the extra charge to add to converted price, sometimes the
// currency server differs from the local bank currency
// It has no effect if $roundUp is set to true
var $extraCharge = 0.00;
/********** OPTIONS END **********/
var $archive = true;
var $suffix = '_';
var $service = 'XE';
/**
* Converts an amount from one currency into another using
* the xe.com webpage
*
* @param float $amountA
* @param string $currA defaults to $vendor_currency
* @param string $currB defaults to $GLOBALS['product_currency'] (and that defaults to $vendor_currency)
* @return the converted amount or the original amount when failure
*/
function convert( $amountA, $currA='', $currB='' ) {
global $vendor_currency, $vmLogger;
$vmLogger->debug('Using convertXE.php currency plugin');
// global $vendor_currency is DEFAULT!
if( !$currA ) {
$currA = $vendor_currency;
}
if( !$currB ) {
$currB = $GLOBALS['product_currency'];
}
// If both currency codes match, do nothing
if( $currA == $currB ) {
return $amountA;
}
if ($this->roundUp)
$val = ceil($amountA * $this->xeCurrency($currB,$currA)) - 0.01;
else
$val = ($amountA * $this->xeCurrency($currB,$currA)) + $this->extraCharge;
$vmLogger->debug('Converted '.$amountA.' '.$currA.' to '.$val.' '.$currB);
return $val;
} // end function convert
// Function to encapsulate XE communication and cache handling
function xeCurrency($code, $base='') {
global $mosConfig_cachepath, $mosConfig_absolute_path,
$vendor_currency, $vmLogger;
// Build the URL String to fetch currencyes
$url = '
http://www.xe.com/ucc/convert.cgi?Amount=1&From=' . $base . '&To=' . $code;
// Start cache code
setlocale(LC_TIME, "en-GB");
$now = time();
if (date("I")) {
$now += 3600; // Adjust for daylight saving time
}
$weekday_now_local = gmdate('w', $now); // week day, important: week starts with sunday (= 0) !!
$date_now_local = gmdate('Ymd', $now);
$time_now_local = gmdate('Hi', $now);
$time_xe_update = '1415';
if( is_writable($mosConfig_cachepath) ) {
$store_path = $mosConfig_cachepath;
}
else {
$store_path = $mosConfig_absolute_path."/tmp";
}
$archivefile_name = $store_path.'/'.$code.$this->suffix.$this->service.'.txt';
$xe_filename = $url;
if(file_exists($archivefile_name) && filesize( $archivefile_name ) > 0 ) {
// timestamp for the Filename
$file_datestamp = date('Ymd', filemtime($archivefile_name));
// check if today is a weekday - no updates on weekends
if( date( 'w' ) > 0 && date( 'w' ) < 6
// compare filedate and actual date
&& $file_datestamp != $date_now_local
// if localtime is greater then update-time go on to update and write files
&& $time_now_local > $time_xe_update) {
$curr_filename = $xe_filename;
$vmLogger->debug( "File exists but is too old!..." );
} else {
$curr_filename = $archivefile_name;
$this->archive = false;
$vmLogger->debug( "Bingo! cached file..." );
}
} else {
$curr_filename = $xe_filename;
$vmLogger->debug( "Must visit $this->service...very slow this time!..." );
}
if( !is_writable( $store_path )) {
$this->archive = false;
$vmLogger->debug( "The file $archivefile_name can't be created. The directory $store_path is not writable" );
}
if( $curr_filename == $xe_filename ) {
// Fetch the file from the internet
require_once( CLASSPATH.'connectionTools.class.php');
$contents = vmConnector::handleCommunication( $curr_filename );
$contents = explode('\n', $contents);
} else {
$vmLogger->debug( "Exchange Ratio $base-$code: " . @file_get_contents( $curr_filename ));
return @file_get_contents( $curr_filename );
}
if( $contents ) {
// Search for currency data
if (is_object($contents) || $contents !='') {
$match = array();
preg_match('/[0-9.]+\s*' . $base . '\s*=\s*([0-9.]+)\s*' . $code . '/', implode('', $contents), $match);
if (sizeof($match) > 0 && $this->archive ) {
// now write new file
file_put_contents( $archivefile_name, $match[1] );
$vmLogger->debug( "Exchange Ratio $base-$code: $match[1]" );
return $match[1];
} else {
return 1;
}
}
} else {
$vmLogger->err( 'Failed to retrieve the currencies.');
$GLOBALS['product_currency'] = $vendor_currency;
return 1;
}
// End cache code
} // end function xeCurrency
}
?>
Hi copy the above code save it as xy.php file and place the file in
administrator\components\com_virtuemart\classes\currency
Then go to admin panel
and change the Global --> Select a currency converter module -->xy.php
Try this it convert the currency well