Changeset 380
- Timestamp:
- May 25, 2011 9:15:54 AM (13 years ago)
- Location:
- trunk
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/docs/version.txt
r377 r380 1 2.1. 51 2.1.7dev -
trunk/lib/ACL.inc.php
r376 r380 73 73 * @static 74 74 */ 75 function &getInstance()75 static function &getInstance() 76 76 { 77 77 static $instance = null; -
trunk/lib/App.inc.php
r376 r380 166 166 * @static 167 167 */ 168 function &getInstance($namespace='')168 static function &getInstance($namespace='') 169 169 { 170 170 static $instance = null; -
trunk/lib/Cache.inc.php
r376 r380 88 88 * @static 89 89 */ 90 function &getInstance($namespace='')90 static function &getInstance($namespace='') 91 91 { 92 92 static $instances = array(); -
trunk/lib/Currency.inc.php
r376 r380 27 27 * 28 28 * @author Quinn Comendant <quinn@strangecode.com> 29 * @version 1. 029 * @version 1.5 30 30 * 31 31 * Example of use: 32 32 --------------------------------------------------------------------- 33 33 $currency = new Currency(); 34 echo $currency->getValue(1, 'eur', 'usd'); 34 $currency->setParam(array('cache_dir' => COMMON_BASE . '/tmp/xcache')); 35 echo $currency->getRage('eur', 'usd'); 36 echo $currency->getValue(125.50, 'eur', 'usd'); 35 37 --------------------------------------------------------------------- 36 38 */ 37 38 require_once 'SOAP/Client.php';39 39 40 40 class Currency { … … 44 44 'cache_result' => true, 45 45 'cache_dir' => '', 46 'cache_age' => 28800, // 8 hours. 47 'soap_api_url' => 'http://xurrency.com/servidor_soap.php', 46 'cache_age' => 86400, // 24 hours. 47 'api_type' => 'json', // 'json' or 'soap'. 48 'api_url' => 'http://xurrency.com/api/%s/%s/%s', 49 'api_key' => '', 48 50 ); 49 51 50 // Object to hold SOAP_Client instance.51 var $_soap_client;52 53 52 /** 54 53 * Cart constructor. … … 57 56 { 58 57 $app =& App::getInstance(); 59 60 // Initialize.61 if (!isset($_SESSION['_currency'])) {62 $_SESSION['_currency'] = array();63 }64 58 65 59 // Set custom parameters. … … 79 73 } 80 74 } 81 82 // Setup SOAP object.83 $this->_soap_client = new SOAP_Client($this->getParam('soap_api_url'));84 75 } 85 76 … … 117 108 118 109 /* 119 * Return the exchange value between the two given currencies .110 * Return the exchange value between the two given currencies for given amount. 120 111 * 121 112 * @access public 122 * @param float Base amount to convert from.123 * @param string 3-letter currency code to convert from.124 * @param string 3-letter currency code to convert to.125 * @return mixed Float exchange ratevalue, or false on error.113 * @param float $amount Base amount to convert from. 114 * @param string $base 3-letter currency code to convert from. 115 * @param string $target 3-letter currency code to convert to. 116 * @return mixed Float converted currency value, or false on error. 126 117 * @author Quinn Comendant <quinn@strangecode.com> 127 118 * @version 1.0 … … 130 121 function getValue($amount, $base, $target) 131 122 { 123 if (false !== $rate = $this->getRate($base, $target)) { 124 return abs($rate * $amount); 125 } else { 126 return false; 127 } 128 } 129 130 /* 131 * Return the currency conversion rate as a ratio. 132 * 133 * @access public 134 * @param string $base 3-letter currency code to convert from. 135 * @param string $target 3-letter currency code to convert to. 136 * @return mixed Float exchange rate value, or false on error. 137 * @author Quinn Comendant <quinn@strangecode.com> 138 * @version 1.0 139 * @since 25 May 2011 01:26:24 140 */ 141 function getRate($base, $target) 142 { 132 143 $app =& App::getInstance(); 133 144 … … 136 147 if (!$this->getParam('cache_result') || !$cache_file_mtime || $cache_file_mtime < time() - $this->getParam('cache_age')) { 137 148 // Get fresh data and create cached file if missing or expired. 138 $app->logMsg(sprintf('Getting fresh currency exchange rate s: %s-to-%s', $base, $target), LOG_DEBUG, __FILE__, __LINE__);139 $value = $this->_performAPICall( 'getValue',array(140 'amount' => $amount,149 $app->logMsg(sprintf('Getting fresh currency exchange rate: %s-to-%s', $base, $target), LOG_DEBUG, __FILE__, __LINE__); 150 $value = $this->_performAPICall(array( 151 'amount' => '1', 141 152 'base' => $base, 142 153 'target' => $target 143 154 )); 144 155 if (false === $value || !is_numeric($value)) { 145 // Failed retrieving SOAPvalue. Use cached copy for now.146 $app->logMsg(sprintf('Failed getting SOAP currency exchange rates: %s-to-%s, using cached copy', $base, $target), LOG_NOTICE, __FILE__, __LINE__);156 // Failed retrieving value. Use cached copy for now. 157 $app->logMsg(sprintf('Failed getting currency exchange rate: %s-to-%s, using cached copy', $base, $target), LOG_NOTICE, __FILE__, __LINE__); 147 158 if (!$value = file_get_contents($cache_file_path)) { 148 $app->logMsg(sprintf('Failed reading targetrate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__);159 $app->logMsg(sprintf('Failed reading cached exchange rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__); 149 160 return false; 150 161 } … … 154 165 } 155 166 } else { 156 $app->logMsg(sprintf('Getting cached currency exchange rate s: %s-to-%s', $base, $target), LOG_DEBUG, __FILE__, __LINE__);167 $app->logMsg(sprintf('Getting cached currency exchange rate: %s-to-%s', $base, $target), LOG_DEBUG, __FILE__, __LINE__); 157 168 if (!$value = file_get_contents($cache_file_path)) { 158 169 $app->logMsg(sprintf('Failed reading target rate file: %s', $cache_file_path), LOG_ERR, __FILE__, __LINE__); … … 160 171 } 161 172 } 173 $app->logMsg(sprintf('Found currency exchange rate: %s-to-%s = %s', $base, $target, $value), LOG_DEBUG, __FILE__, __LINE__); 162 174 return trim($value); 163 175 } … … 169 181 * @access private 170 182 */ 171 function _performAPICall($ api_call, $parameters=null)183 function _performAPICall($parameters=null) 172 184 { 173 185 $app =& App::getInstance(); 174 186 175 $result = $this->_soap_client->call($api_call, $parameters); 176 177 if (PEAR::isError($result)) { 178 $app->logMsg(sprintf('SOAP Pear error: %s', $result->getMessage()), LOG_WARNING, __FILE__, __LINE__); 179 return false; 180 } 181 182 return $result; 187 switch ($this->getParam('api_type')) { 188 case 'json' : 189 $api_url = $this->getParam('api_url'); 190 if ('' != $this->getParam('api_key')) { 191 $api_url .= '?key=' . $this->getParam('api_key'); 192 } 193 $json_response = file_get_contents(sprintf($api_url, $parameters['base'], $parameters['target'], $parameters['amount'])); 194 $json = json_decode($json_response); 195 if (null === $json) { 196 $app->logMsg(sprintf('Could not decode JSON response: %s', getDump($json_response)), LOG_WARNING, __FILE__, __LINE__); 197 return false; 198 } else if ($json->status === 'fail') { 199 if ($json->code == 3) { 200 $app->logMsg(sprintf('Xurrency error LimitReachedException: %s', $json->message), LOG_WARNING, __FILE__, __LINE__); 201 } elseif ($json->code == 2) { 202 $app->logMsg(sprintf('Xurrency error InvalidCurrencies: %s', $json->message), LOG_WARNING, __FILE__, __LINE__); 203 } elseif ($json->code == 4 || $json->code == 5) { 204 $app->logMsg(sprintf('Xurrency error InvalidKey: %s', $json->message), LOG_WARNING, __FILE__, __LINE__); 205 } else { 206 $app->logMsg(sprintf('Xurrency unknown error: %s', $json->message), LOG_WARNING, __FILE__, __LINE__); 207 } 208 return false; 209 } else { 210 return $json->result->value; 211 } 212 break; 213 214 case 'soap' : 215 // Xurrency doesn't appear to support SOAP anymore, but we'll leave this here in case it comes back. 216 217 // Setup SOAP object. 218 require_once 'SOAP/Client.php'; 219 $soap_client = new SOAP_Client($this->getParam('api_url')); 220 // Call API 221 $result = $soap_client->call('getValue', $parameters); 222 // Check for errors. 223 if (PEAR::isError($result)) { 224 $app->logMsg(sprintf('SOAP Pear error: %s', $result->getMessage()), LOG_WARNING, __FILE__, __LINE__); 225 return false; 226 } 227 return $result; 228 break; 229 230 default : 231 trigger_error('Unknown api_type: ' . $this->getParam('api_type'), E_USER_ERROR); 232 die; 233 break; 234 } 183 235 } 184 236 } -
trunk/lib/DB.inc.php
r376 r380 80 80 * @static 81 81 */ 82 function &getInstance()82 static function &getInstance() 83 83 { 84 84 static $instance = null; -
trunk/lib/Lock.inc.php
r376 r380 55 55 * @static 56 56 */ 57 function &getInstance($auth_object)57 static function &getInstance($auth_object) 58 58 { 59 59 static $instance = null; -
trunk/lib/Utilities.inc.php
r376 r380 35 35 function dump($var, $display=false, $var_dump=false) 36 36 { 37 echo $display ? "\n<br /><pre>\n" : "\n\n\n<!--\n"; 37 if (defined('_CLI')) { 38 echo "\n"; 39 } else { 40 echo $display ? "\n<br /><pre>\n" : "\n\n\n<!--\n"; 41 } 38 42 if ($var_dump) { 39 43 var_dump($var); … … 41 45 print_r($var); 42 46 } 43 echo $display ? "\n</pre><br />\n" : "\n-->\n\n\n"; 47 if (defined('_CLI')) { 48 echo "\n"; 49 } else { 50 echo $display ? "\n</pre><br />\n" : "\n-->\n\n\n"; 51 } 44 52 } 45 53 -
trunk/lib/Version.inc.php
r376 r380 61 61 * @static 62 62 */ 63 function &getInstance($auth_object)63 static function &getInstance($auth_object) 64 64 { 65 65 static $instance = null;
Note: See TracChangeset
for help on using the changeset viewer.