CurrencyLib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * other webservices:
  4. * - http://www.webservicex.net/WS/WSDetails.aspx?WSID=10 (XML)
  5. * - http://www.webserviceshare.com/business/financial/currency/service/Noon-Foreign-Exchange-Rates.htm (XML)
  6. */
  7. App::uses('HttpSocket', 'Network/Http');
  8. App::uses('Xml', 'Utility');
  9. /**
  10. * Component to retreive calculate currencies
  11. *
  12. * example:
  13. * $result = $this->Currency->convert(2.5, 'EUR', 'USD');
  14. *
  15. * from: http://www.studiocanaria.com/articles/cakephp_currency_conversion_component
  16. * alternativly: http://www.currencyserver.de/webservice/CurrencyServerWebService.asmx/getXmlStream?provider=AVERAGE
  17. *
  18. * @author Mark Scherer
  19. * @license MIT
  20. * @cakephp 2.x
  21. * 2009-11-23 ms
  22. */
  23. class CurrencyLib {
  24. const URL = 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml';
  25. const URL_HISTORY = 'http://www.ecb.int/stats/eurofxref/eurofxref-hist.xml';
  26. //TODO: get information about a currency (name, ...)
  27. const URL_TABLE = 'http://www.ecb.int/rss/fxref-{currency}.html';
  28. public $baseCurrency = 'EUR';
  29. public $includeBitcoin = true;
  30. public $cacheFileUsed = false;
  31. public $cacheTime = DAY;
  32. /**
  33. * Converts the $amount from $fromCurrency to $toCurrency, formatted to
  34. * $decimals decimal places.
  35. *
  36. * @return float [Converted Currency Amount] or boolean FALSE on failure
  37. * @param $amount float
  38. * @param $fromCurrency string
  39. * @param $toCurrency string
  40. * @param $decimals integer[optional]default=2
  41. */
  42. public function convert($amount, $fromCurrency, $toCurrency, $decimals = 2) {
  43. //Get the rate table
  44. $rates = $this->_retrieveCurrencies();
  45. //Return result of conversion
  46. if (!array_key_exists($fromCurrency, $rates) || !array_key_exists($toCurrency, $rates)) {
  47. return false;
  48. }
  49. return number_format($amount/$rates[$fromCurrency]*$rates[$toCurrency], $decimals);
  50. }
  51. /**
  52. * Returns an array of rates in comparison the the $base currency given to $decimals
  53. * number of decimal places.
  54. *
  55. * @param $base string[optional]default='EUR'
  56. * @param $decimals integer[optional]default=2
  57. * @return array $table or boolean FALSE on failure
  58. */
  59. public function table($base = 'EUR', $decimals = 2) {
  60. //Create array to holds rates
  61. $rateTable = array();
  62. //Get rate table array
  63. $rates = $this->_retrieveCurrencies();
  64. if (!array_key_exists($base, $rates)) {
  65. return false;
  66. }
  67. //Iterate throught each rate converting it against $base
  68. foreach ($rates as $key => $value) {
  69. $rate = 0;
  70. if ($value > 0) {
  71. $rate = 1 / $rates[$base] * $rates[$key];
  72. }
  73. $rateTable[$key] = number_format($rate, $decimals);
  74. }
  75. //Return result array
  76. return $rateTable;
  77. }
  78. /**
  79. * CurrencyComponent::isAvailable()
  80. *
  81. * @param mixed $currency
  82. * @return boolean Success.
  83. */
  84. public function isAvailable($currency) {
  85. $rates = $this->_retrieveCurrencies();
  86. return array_key_exists($currency, $rates);
  87. }
  88. /**
  89. * @param string $name: "" (none), "history", "full" (both)
  90. * @return boolean Success.
  91. * 2010-09-19 ms
  92. */
  93. public function reset($name = 'full') {
  94. if ($name === 'full') {
  95. $name = '';
  96. Cache::delete('currencyListHistory');
  97. }
  98. return Cache::delete('currencyList' . ucfirst($name));
  99. }
  100. public function cacheFileUsed() {
  101. return $this->cacheFileUsed;
  102. }
  103. /**
  104. * @param string $code (3digit - e.g. EUR)
  105. * @param mixed $default (defaults to bool false)
  106. */
  107. public function getName($currency, $default = false) {
  108. if (empty($currency)) {
  109. return $default;
  110. }
  111. $currency = strtoupper($currency);
  112. $currencies = $this->currencies;
  113. if ($this->includeBitcoin) {
  114. $currencies['BTC'] = 'Bitcoin';
  115. }
  116. if (array_key_exists($currency, $currencies)) {
  117. return $currencies[$currency];
  118. }
  119. return $default;
  120. }
  121. /**
  122. * CurrencyComponent::_retrieveHistory()
  123. *
  124. * @return array
  125. */
  126. protected function _retrieveHistory() {
  127. if ($historyList = $this->_retrieve('history')) {
  128. return $historyList;
  129. }
  130. $Xml = Xml::build(self::URL_HISTORY);
  131. $currencies = Xml::toArray($Xml);
  132. //Filter down to just the rates
  133. $currencies = $currencies['Envelope']['Cube']['Cube']['Cube'];
  134. $historyList = array();
  135. //European Central bank gives us everything against Euro so add this manually
  136. $historyList[$this->baseCurrency] = 1;
  137. foreach ($currencies as $currency) {
  138. $historyList[$currency['currency']] = $currency['rate'];
  139. }
  140. $this->_store($historyList, 'history');
  141. return $currencyList;
  142. }
  143. /**
  144. * CurrencyComponent::_retrieveCurrencies()
  145. *
  146. * @return array
  147. */
  148. protected function _retrieveCurrencies() {
  149. if ($currencyList = $this->_retrieve()) {
  150. return $currencyList;
  151. }
  152. // Retrieve rates as an XML object
  153. $CurrencyXml = Xml::build(self::URL);
  154. $currencies = Xml::toArray($CurrencyXml);
  155. //Filter down to just the rates
  156. $currencies = $currencies['Envelope']['Cube']['Cube']['Cube'];
  157. //Create an array to hold the rates
  158. $currencyList = array();
  159. //European Central bank gives us everything against Euro so add this manually
  160. $currencyList[$this->baseCurrency] = 1;
  161. //Now iterate through and add the rates provided
  162. foreach ($currencies as $currency) {
  163. $currencyList[$currency['@currency']] = $currency['@rate'];
  164. }
  165. if ($this->includeBitcoin && ($res = $this->_getBitcoin())) {
  166. $currencyList['BTC'] = $res;
  167. }
  168. //Cache
  169. $this->_store($currencyList);
  170. return $currencyList;
  171. }
  172. protected function _getBitcoin() {
  173. App::uses('CurrencyBitcoinLib', 'Tools.Lib');
  174. $Btc = new CurrencyBitcoinLib();
  175. return $Btc->rate(array('currency'=>$this->baseCurrency));
  176. }
  177. /**
  178. * @param string $name: "" (none), "history", "full" (both)
  179. * 2010-09-19 ms
  180. */
  181. protected function _store($currencyList, $name = '') {
  182. $this->cacheFileUsed = false;
  183. Cache::write('currencyList' . ucfirst($name), serialize($currencyList));
  184. }
  185. /**
  186. * @param string $name: "" (none), "history", "full" (both)
  187. * 2010-09-19 ms
  188. */
  189. protected function _retrieve($name = '') {
  190. $res = Cache::read('currencyList' . ucfirst($name));
  191. if ($res !== false) {
  192. $this->cacheFileUsed = true;
  193. return unserialize($res);
  194. }
  195. return false;
  196. }
  197. public $currencies = array(
  198. 'AFA' => 'Afghanistan Afghani',
  199. 'ALL' => 'Albanian Lek',
  200. 'DZD' => 'Algerian Dinar',
  201. 'ARS' => 'Argentine Peso',
  202. 'AWG' => 'Aruba Florin',
  203. 'AUD' => 'Australian Dollar',
  204. 'BSD' => 'Bahamian Dollar',
  205. 'BHD' => 'Bahraini Dinar',
  206. 'BDT' => 'Bangladesh Taka',
  207. 'BBD' => 'Barbados Dollar',
  208. 'BZD' => 'Belize Dollar',
  209. 'BMD' => 'Bermuda Dollar',
  210. 'BTN' => 'Bhutan Ngultrum',
  211. 'BOB' => 'Bolivian Boliviano',
  212. 'BWP' => 'Botswana Pula',
  213. 'BRL' => 'Brazilian Real',
  214. 'GBP' => 'British Pound',
  215. 'BND' => 'Brunei Dollar',
  216. 'BIF' => 'Burundi Franc',
  217. 'XOF' => 'CFA Franc (BCEAO)',
  218. 'XAF' => 'CFA Franc (BEAC)',
  219. 'KHR' => 'Cambodia Riel',
  220. 'CAD' => 'Canadian Dollar',
  221. 'CVE' => 'Cape Verde Escudo',
  222. 'KYD' => 'Cayman Islands Dollar',
  223. 'CLP' => 'Chilean Peso',
  224. 'CNY' => 'Chinese Yuan',
  225. 'COP' => 'Colombian Peso',
  226. 'KMF' => 'Comoros Franc',
  227. 'CRC' => 'Costa Rica Colon',
  228. 'HRK' => 'Croatian Kuna',
  229. 'CUP' => 'Cuban Peso',
  230. 'CYP' => 'Cyprus Pound',
  231. 'CZK' => 'Czech Koruna',
  232. 'DKK' => 'Danish Krone',
  233. 'DJF' => 'Dijibouti Franc',
  234. 'DOP' => 'Dominican Peso',
  235. 'XCD' => 'East Caribbean Dollar',
  236. 'EGP' => 'Egyptian Pound',
  237. 'SVC' => 'El Salvador Colon',
  238. 'EEK' => 'Estonian Kroon',
  239. 'ETB' => 'Ethiopian Birr',
  240. 'EUR' => 'Euro',
  241. 'FKP' => 'Falkland Islands Pound',
  242. 'GMD' => 'Gambian Dalasi',
  243. 'GHC' => 'Ghanian Cedi',
  244. 'GIP' => 'Gibraltar Pound',
  245. 'XAU' => 'Gold Ounces',
  246. 'GTQ' => 'Guatemala Quetzal',
  247. 'GNF' => 'Guinea Franc',
  248. 'GYD' => 'Guyana Dollar',
  249. 'HTG' => 'Haiti Gourde',
  250. 'HNL' => 'Honduras Lempira',
  251. 'HKD' => 'Hong Kong Dollar',
  252. 'HUF' => 'Hungarian Forint',
  253. 'ISK' => 'Iceland Krona',
  254. 'INR' => 'Indian Rupee',
  255. 'IDR' => 'Indonesian Rupiah',
  256. 'IQD' => 'Iraqi Dinar',
  257. 'ILS' => 'Israeli Shekel',
  258. 'JMD' => 'Jamaican Dollar',
  259. 'JPY' => 'Japanese Yen',
  260. 'JOD' => 'Jordanian Dinar',
  261. 'KZT' => 'Kazakhstan Tenge',
  262. 'KES' => 'Kenyan Shilling',
  263. 'KRW' => 'Korean Won',
  264. 'KWD' => 'Kuwaiti Dinar',
  265. 'LAK' => 'Lao Kip',
  266. 'LVL' => 'Latvian Lat',
  267. 'LBP' => 'Lebanese Pound',
  268. 'LSL' => 'Lesotho Loti',
  269. 'LRD' => 'Liberian Dollar',
  270. 'LYD' => 'Libyan Dinar',
  271. 'LTL' => 'Lithuanian Lita',
  272. 'MOP' => 'Macau Pataca',
  273. 'MKD' => 'Macedonian Denar',
  274. 'MGF' => 'Malagasy Franc',
  275. 'MWK' => 'Malawi Kwacha',
  276. 'MYR' => 'Malaysian Ringgit',
  277. 'MVR' => 'Maldives Rufiyaa',
  278. 'MTL' => 'Maltese Lira',
  279. 'MRO' => 'Mauritania Ougulya',
  280. 'MUR' => 'Mauritius Rupee',
  281. 'MXN' => 'Mexican Peso',
  282. 'MDL' => 'Moldovan Leu',
  283. 'MNT' => 'Mongolian Tugrik',
  284. 'MAD' => 'Moroccan Dirham',
  285. 'MZM' => 'Mozambique Metical',
  286. 'MMK' => 'Myanmar Kyat',
  287. 'NAD' => 'Namibian Dollar',
  288. 'NPR' => 'Nepalese Rupee',
  289. 'ANG' => 'Neth Antilles Guilder',
  290. 'NZD' => 'New Zealand Dollar',
  291. 'NIO' => 'Nicaragua Cordoba',
  292. 'NGN' => 'Nigerian Naira',
  293. 'KPW' => 'North Korean Won',
  294. 'NOK' => 'Norwegian Krone',
  295. 'OMR' => 'Omani Rial',
  296. 'XPF' => 'Pacific Franc',
  297. 'PKR' => 'Pakistani Rupee',
  298. 'XPD' => 'Palladium Ounces',
  299. 'PAB' => 'Panama Balboa',
  300. 'PGK' => 'Papua New Guinea Kina',
  301. 'PYG' => 'Paraguayan Guarani',
  302. 'PEN' => 'Peruvian Nuevo Sol',
  303. 'PHP' => 'Philippine Peso',
  304. 'XPT' => 'Platinum Ounces',
  305. 'PLN' => 'Polish Zloty',
  306. 'QAR' => 'Qatar Rial',
  307. 'ROL' => 'Romanian Leu',
  308. 'RUB' => 'Russian Rouble',
  309. 'WST' => 'Samoa Tala',
  310. 'STD' => 'Sao Tome Dobra',
  311. 'SAR' => 'Saudi Arabian Riyal',
  312. 'SCR' => 'Seychelles Rupee',
  313. 'SLL' => 'Sierra Leone Leone',
  314. 'XAG' => 'Silver Ounces',
  315. 'SGD' => 'Singapore Dollar',
  316. 'SKK' => 'Slovak Koruna',
  317. 'SIT' => 'Slovenian Tolar',
  318. 'SBD' => 'Solomon Islands Dollar',
  319. 'SOS' => 'Somali Shilling',
  320. 'ZAR' => 'South African Rand',
  321. 'LKR' => 'Sri Lanka Rupee',
  322. 'SHP' => 'St Helena Pound',
  323. 'SDD' => 'Sudanese Dinar',
  324. 'SRG' => 'Surinam Guilder',
  325. 'SZL' => 'Swaziland Lilageni',
  326. 'SEK' => 'Swedish Krona',
  327. 'TRY' => 'Turkey Lira',
  328. 'CHF' => 'Swiss Franc',
  329. 'SYP' => 'Syrian Pound',
  330. 'TWD' => 'Taiwan Dollar',
  331. 'TZS' => 'Tanzanian Shilling',
  332. 'THB' => 'Thai Baht',
  333. 'TOP' => 'Tonga Pa\'anga',
  334. 'TTD' => 'Trinidad & Tobago Dollar',
  335. 'TND' => 'Tunisian Dinar',
  336. 'TRL' => 'Turkish Lira',
  337. 'USD' => 'U.S. Dollar',
  338. 'AED' => 'UAE Dirham',
  339. 'UGX' => 'Ugandan Shilling',
  340. 'UAH' => 'Ukraine Hryvnia',
  341. 'UYU' => 'Uruguayan New Peso',
  342. 'VUV' => 'Vanuatu Vatu',
  343. 'VEB' => 'Venezuelan Bolivar',
  344. 'VND' => 'Vietnam Dong',
  345. 'YER' => 'Yemen Riyal',
  346. 'YUM' => 'Yugoslav Dinar',
  347. 'ZMK' => 'Zambian Kwacha',
  348. 'ZWD' => 'Zimbabwe Dollar',
  349. );
  350. }