CurrencyLib.php 10 KB

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