CurrencyLib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. $Xml = Xml::build(self::URL_HISTORY);
  129. $currencies = Xml::toArray($Xml);
  130. //Filter down to just the rates
  131. $currencies = $currencies['Envelope']['Cube']['Cube']['Cube'];
  132. $historyList = array();
  133. //European Central bank gives us everything against Euro so add this manually
  134. $historyList[$this->baseCurrency] = 1;
  135. foreach ($currencies as $currency) {
  136. $historyList[$currency['currency']] = $currency['rate'];
  137. }
  138. $this->_store($historyList, 'history');
  139. return $currencyList;
  140. }
  141. /**
  142. * CurrencyComponent::_retrieveCurrencies()
  143. *
  144. * @return array
  145. */
  146. protected function _retrieveCurrencies() {
  147. if ($currencyList = $this->_retrieve()) {
  148. return $currencyList;
  149. }
  150. // Retrieve rates as an XML object
  151. $CurrencyXml = Xml::build(self::URL);
  152. $currencies = Xml::toArray($CurrencyXml);
  153. //Filter down to just the rates
  154. $currencies = $currencies['Envelope']['Cube']['Cube']['Cube'];
  155. //Create an array to hold the rates
  156. $currencyList = array();
  157. //European Central bank gives us everything against Euro so add this manually
  158. $currencyList[$this->baseCurrency] = 1;
  159. //Now iterate through and add the rates provided
  160. foreach ($currencies as $currency) {
  161. $currencyList[$currency['@currency']] = $currency['@rate'];
  162. }
  163. if ($this->includeBitcoin && ($res = $this->_getBitcoin())) {
  164. $currencyList['BTC'] = $res;
  165. }
  166. //Cache
  167. $this->_store($currencyList);
  168. return $currencyList;
  169. }
  170. protected function _getBitcoin() {
  171. App::uses('CurrencyBitcoinLib', 'Tools.Lib');
  172. $Btc = new CurrencyBitcoinLib();
  173. return $Btc->rate(array('currency' => $this->baseCurrency));
  174. }
  175. /**
  176. * @param string $name: "" (none), "history", "full" (both)
  177. */
  178. protected function _store($currencyList, $name = '') {
  179. $this->cacheFileUsed = false;
  180. Cache::write('currencyList' . ucfirst($name), serialize($currencyList));
  181. }
  182. /**
  183. * @param string $name: "" (none), "history", "full" (both)
  184. */
  185. protected function _retrieve($name = '') {
  186. $res = Cache::read('currencyList' . ucfirst($name));
  187. if ($res !== false) {
  188. $this->cacheFileUsed = true;
  189. return unserialize($res);
  190. }
  191. return false;
  192. }
  193. public $currencies = array(
  194. 'AFA' => 'Afghanistan Afghani',
  195. 'ALL' => 'Albanian Lek',
  196. 'DZD' => 'Algerian Dinar',
  197. 'ARS' => 'Argentine Peso',
  198. 'AWG' => 'Aruba Florin',
  199. 'AUD' => 'Australian Dollar',
  200. 'BSD' => 'Bahamian Dollar',
  201. 'BHD' => 'Bahraini Dinar',
  202. 'BDT' => 'Bangladesh Taka',
  203. 'BBD' => 'Barbados Dollar',
  204. 'BZD' => 'Belize Dollar',
  205. 'BMD' => 'Bermuda Dollar',
  206. 'BTN' => 'Bhutan Ngultrum',
  207. 'BOB' => 'Bolivian Boliviano',
  208. 'BWP' => 'Botswana Pula',
  209. 'BRL' => 'Brazilian Real',
  210. 'GBP' => 'British Pound',
  211. 'BND' => 'Brunei Dollar',
  212. 'BIF' => 'Burundi Franc',
  213. 'XOF' => 'CFA Franc (BCEAO)',
  214. 'XAF' => 'CFA Franc (BEAC)',
  215. 'KHR' => 'Cambodia Riel',
  216. 'CAD' => 'Canadian Dollar',
  217. 'CVE' => 'Cape Verde Escudo',
  218. 'KYD' => 'Cayman Islands Dollar',
  219. 'CLP' => 'Chilean Peso',
  220. 'CNY' => 'Chinese Yuan',
  221. 'COP' => 'Colombian Peso',
  222. 'KMF' => 'Comoros Franc',
  223. 'CRC' => 'Costa Rica Colon',
  224. 'HRK' => 'Croatian Kuna',
  225. 'CUP' => 'Cuban Peso',
  226. 'CYP' => 'Cyprus Pound',
  227. 'CZK' => 'Czech Koruna',
  228. 'DKK' => 'Danish Krone',
  229. 'DJF' => 'Dijibouti Franc',
  230. 'DOP' => 'Dominican Peso',
  231. 'XCD' => 'East Caribbean Dollar',
  232. 'EGP' => 'Egyptian Pound',
  233. 'SVC' => 'El Salvador Colon',
  234. 'EEK' => 'Estonian Kroon',
  235. 'ETB' => 'Ethiopian Birr',
  236. 'EUR' => 'Euro',
  237. 'FKP' => 'Falkland Islands Pound',
  238. 'GMD' => 'Gambian Dalasi',
  239. 'GHC' => 'Ghanian Cedi',
  240. 'GIP' => 'Gibraltar Pound',
  241. 'XAU' => 'Gold Ounces',
  242. 'GTQ' => 'Guatemala Quetzal',
  243. 'GNF' => 'Guinea Franc',
  244. 'GYD' => 'Guyana Dollar',
  245. 'HTG' => 'Haiti Gourde',
  246. 'HNL' => 'Honduras Lempira',
  247. 'HKD' => 'Hong Kong Dollar',
  248. 'HUF' => 'Hungarian Forint',
  249. 'ISK' => 'Iceland Krona',
  250. 'INR' => 'Indian Rupee',
  251. 'IDR' => 'Indonesian Rupiah',
  252. 'IQD' => 'Iraqi Dinar',
  253. 'ILS' => 'Israeli Shekel',
  254. 'JMD' => 'Jamaican Dollar',
  255. 'JPY' => 'Japanese Yen',
  256. 'JOD' => 'Jordanian Dinar',
  257. 'KZT' => 'Kazakhstan Tenge',
  258. 'KES' => 'Kenyan Shilling',
  259. 'KRW' => 'Korean Won',
  260. 'KWD' => 'Kuwaiti Dinar',
  261. 'LAK' => 'Lao Kip',
  262. 'LVL' => 'Latvian Lat',
  263. 'LBP' => 'Lebanese Pound',
  264. 'LSL' => 'Lesotho Loti',
  265. 'LRD' => 'Liberian Dollar',
  266. 'LYD' => 'Libyan Dinar',
  267. 'LTL' => 'Lithuanian Lita',
  268. 'MOP' => 'Macau Pataca',
  269. 'MKD' => 'Macedonian Denar',
  270. 'MGF' => 'Malagasy Franc',
  271. 'MWK' => 'Malawi Kwacha',
  272. 'MYR' => 'Malaysian Ringgit',
  273. 'MVR' => 'Maldives Rufiyaa',
  274. 'MTL' => 'Maltese Lira',
  275. 'MRO' => 'Mauritania Ougulya',
  276. 'MUR' => 'Mauritius Rupee',
  277. 'MXN' => 'Mexican Peso',
  278. 'MDL' => 'Moldovan Leu',
  279. 'MNT' => 'Mongolian Tugrik',
  280. 'MAD' => 'Moroccan Dirham',
  281. 'MZM' => 'Mozambique Metical',
  282. 'MMK' => 'Myanmar Kyat',
  283. 'NAD' => 'Namibian Dollar',
  284. 'NPR' => 'Nepalese Rupee',
  285. 'ANG' => 'Neth Antilles Guilder',
  286. 'NZD' => 'New Zealand Dollar',
  287. 'NIO' => 'Nicaragua Cordoba',
  288. 'NGN' => 'Nigerian Naira',
  289. 'KPW' => 'North Korean Won',
  290. 'NOK' => 'Norwegian Krone',
  291. 'OMR' => 'Omani Rial',
  292. 'XPF' => 'Pacific Franc',
  293. 'PKR' => 'Pakistani Rupee',
  294. 'XPD' => 'Palladium Ounces',
  295. 'PAB' => 'Panama Balboa',
  296. 'PGK' => 'Papua New Guinea Kina',
  297. 'PYG' => 'Paraguayan Guarani',
  298. 'PEN' => 'Peruvian Nuevo Sol',
  299. 'PHP' => 'Philippine Peso',
  300. 'XPT' => 'Platinum Ounces',
  301. 'PLN' => 'Polish Zloty',
  302. 'QAR' => 'Qatar Rial',
  303. 'ROL' => 'Romanian Leu',
  304. 'RUB' => 'Russian Rouble',
  305. 'WST' => 'Samoa Tala',
  306. 'STD' => 'Sao Tome Dobra',
  307. 'SAR' => 'Saudi Arabian Riyal',
  308. 'SCR' => 'Seychelles Rupee',
  309. 'SLL' => 'Sierra Leone Leone',
  310. 'XAG' => 'Silver Ounces',
  311. 'SGD' => 'Singapore Dollar',
  312. 'SKK' => 'Slovak Koruna',
  313. 'SIT' => 'Slovenian Tolar',
  314. 'SBD' => 'Solomon Islands Dollar',
  315. 'SOS' => 'Somali Shilling',
  316. 'ZAR' => 'South African Rand',
  317. 'LKR' => 'Sri Lanka Rupee',
  318. 'SHP' => 'St Helena Pound',
  319. 'SDD' => 'Sudanese Dinar',
  320. 'SRG' => 'Surinam Guilder',
  321. 'SZL' => 'Swaziland Lilageni',
  322. 'SEK' => 'Swedish Krona',
  323. 'TRY' => 'Turkey Lira',
  324. 'CHF' => 'Swiss Franc',
  325. 'SYP' => 'Syrian Pound',
  326. 'TWD' => 'Taiwan Dollar',
  327. 'TZS' => 'Tanzanian Shilling',
  328. 'THB' => 'Thai Baht',
  329. 'TOP' => 'Tonga Pa\'anga',
  330. 'TTD' => 'Trinidad & Tobago Dollar',
  331. 'TND' => 'Tunisian Dinar',
  332. 'TRL' => 'Turkish Lira',
  333. 'USD' => 'U.S. Dollar',
  334. 'AED' => 'UAE Dirham',
  335. 'UGX' => 'Ugandan Shilling',
  336. 'UAH' => 'Ukraine Hryvnia',
  337. 'UYU' => 'Uruguayan New Peso',
  338. 'VUV' => 'Vanuatu Vatu',
  339. 'VEB' => 'Venezuelan Bolivar',
  340. 'VND' => 'Vietnam Dong',
  341. 'YER' => 'Yemen Riyal',
  342. 'YUM' => 'Yugoslav Dinar',
  343. 'ZMK' => 'Zambian Kwacha',
  344. 'ZWD' => 'Zimbabwe Dollar',
  345. );
  346. }