* Copyright 2001-2009 Strangecode Internet Consultancy
*
* This file is part of The Strangecode Codebase.
*
* The Strangecode Codebase is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* The Strangecode Codebase is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* The Strangecode Codebase. If not, see .
*/
/**
* Currency.inc.php
*
* Class to convert currency values.
*
* @author Quinn Comendant
* @version 1.0
*
* Example of use:
---------------------------------------------------------------------
$currency = new Currency();
echo $currency->getValue(1, 'eur', 'usd');
---------------------------------------------------------------------
*/
require_once 'SOAP/Client.php';
class Currency {
// Configuration parameters for this object.
var $_params = array(
'cache_result' => true,
'cache_dir' => '',
'cache_age' => 28800, // 8 hours.
'soap_api_url' => 'http://xurrency.com/servidor_soap.php',
);
// Object to hold SOAP_Client instance.
var $_soap_client;
/**
* Cart constructor.
*/
function Currency($params=array())
{
$app =& App::getInstance();
// Initialize.
if (!isset($_SESSION['_currency'])) {
$_SESSION['_currency'] = array();
}
// Set custom parameters.
$this->setParam($params);
// Setup cache directory.
if ($this->getParam('cache_result')) {
if ('' == $this->getParam('cache_dir')) {
// Use a sane default cache directory.
$this->setParam(array('cache_dir' => '/tmp/xcache_' . md5(COMMON_BASE)));
}
if (!is_dir($this->getParam('cache_dir'))) {
$app->logMsg(sprintf('Creating cache_dir: %s', $this->getParam('cache_dir')), LOG_INFO, __FILE__, __LINE__);
if (!mkdir($this->getParam('cache_dir'))) {
$app->logMsg(sprintf('Could not create cache_dir: %s', $this->getParam('cache_dir')), LOG_WARNING, __FILE__, __LINE__);
}
}
}
// Setup SOAP object.
$this->_soap_client = new SOAP_Client($this->getParam('soap_api_url'));
}
/**
* Set the params of this object.
*
* @param array $params Array of param keys and values to set.
*/
function setParam($params=null)
{
if (isset($params) && is_array($params)) {
// Merge new parameters with old overriding only those passed.
$this->_params = array_merge($this->_params, $params);
}
}
/**
* Return the value of a parameter, if it exists.
*
* @access public
* @param string $param Which parameter to return.
* @return mixed Configured parameter value.
*/
function getParam($param)
{
$app =& App::getInstance();
if (isset($this->_params[$param])) {
return $this->_params[$param];
} else {
$app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
return null;
}
}
/*
* Return the exchange value between the two given currencies.
*
* @access public
* @param float Base amount to convert from.
* @param string 3-letter currency code to convert from.
* @param string 3-letter currency code to convert to.
* @return mixed Float exchange rate value, or false on error.
* @author Quinn Comendant
* @version 1.0
* @since 05 May 2008 23:50:59
*/
function getValue($amount, $base, $target)
{
$app =& App::getInstance();
$cache_file_path = sprintf('%s/%s-to-%s', $this->getParam('cache_dir'), $base, $target);
$cache_file_mtime = @filemtime($cache_file_path);
if (!$this->getParam('cache_result') || !$cache_file_mtime || $cache_file_mtime < time() - $this->getParam('cache_age')) {
// Get fresh data and create cached file if missing or expired.
$app->logMsg(sprintf('Getting fresh currency exchange rates: %s-to-%s', $base, $target), LOG_DEBUG, __FILE__, __LINE__);
$value = $this->_performAPICall('getValue', array(
'amount' => $amount,
'base' => $base,
'target' => $target
));
if (false === $value || !is_numeric($value)) {
// Failed retrieving SOAP value. Use cached copy for now.
$app->logMsg(sprintf('Failed getting SOAP currency exchange rates: %s-to-%s, using cached copy', $base, $target), LOG_NOTICE, __FILE__, __LINE__);
if (!$value = file_get_contents($cache_file_path)) {
$app->logMsg(sprintf('Failed reading target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
return false;
}
} else if ($this->getParam('cache_result') && !filePutContents($cache_file_path, $value, LOCK_EX)) {
$app->logMsg(sprintf('Failed writing to target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
return false;
}
} else {
$app->logMsg(sprintf('Getting cached currency exchange rates: %s-to-%s', $base, $target), LOG_DEBUG, __FILE__, __LINE__);
if (!$value = file_get_contents($cache_file_path)) {
$app->logMsg(sprintf('Failed reading target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);
return false;
}
}
return trim($value);
}
/**
* @param string
* @param array
* @return mixed
* @access private
*/
function _performAPICall($api_call, $parameters=null)
{
$app =& App::getInstance();
$result = $this->_soap_client->call($api_call, $parameters);
if (PEAR::isError($result)) {
$app->logMsg(sprintf('SOAP Pear error: %s', $result->getMessage()), LOG_WARNING, __FILE__, __LINE__);
return false;
}
return $result;
}
}
?>