Source Code Documentation


s2member\includes\classes\utils-cur.inc.php


  1. <?php
  2. /**
  3. * Currency utilities.
  4. *
  5. * Copyright: © 2009-2011
  6. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  7. * (coded in the USA)
  8. *
  9. * Released under the terms of the GNU General Public License.
  10. * You should have received a copy of the GNU General Public License,
  11. * along with this software. In the main directory, see: /licensing/
  12. * If not, see: {@link http://www.gnu.org/licenses/}.
  13. *
  14. * @package s2Member\Utilities
  15. * @since 110531
  16. */
  17. if(realpath(__FILE__) === realpath($_SERVER["SCRIPT_FILENAME"]))
  18. exit("Do not access this file directly.");
  19.  
  20. if(!class_exists("c_ws_plugin__s2member_utils_cur"))
  21. {
  22. /**
  23. * Currency utilities.
  24. *
  25. * @package s2Member\Utilities
  26. * @since 3.5
  27. */
  28. class c_ws_plugin__s2member_utils_cur
  29. {
  30. /**
  31. * Currency converter.
  32. *
  33. * Uses the Google® currency conversion API.
  34. *
  35. * @package s2Member\Utilities
  36. * @since 3.5
  37. *
  38. * @param int|str $a The amount, in ``$from``.
  39. * @param str $from A 3 character Currency Code.
  40. * @param str $to A 3 character Currency Code.
  41. * @return float|str|bool A numeric amount in ``$to``,
  42. * after having been converted. Else false.
  43. *
  44. * @see http://www.techmug.com/ajax-currency-converter-with-google-api/
  45. */
  46. public static function convert($a = FALSE, $from = FALSE, $to = FALSE)
  47. {
  48. if(is_numeric($a) && strlen($from = strtoupper($from)) === 3 && strlen($to = strtoupper($to)) === 3)
  49. {
  50. $q = number_format($a, 2, ".", "").$from."=?".$to;
  51. $api = "http://www.google.com/ig/calculator?hl=en&q=".urlencode($q);
  52.  
  53. if(($json = preg_replace('/([{,])\s*([^"]+?)\s*:/', '$1"$2":', c_ws_plugin__s2member_utils_urls::remote($api))) && is_array($json = json_decode($json, true)) && !empty($json["icc"]) && isset($json["rhs"]) && strlen($json["rhs"]))
  54. {
  55. if(is_numeric($c_a = preg_replace("/ .*$/", "", trim($json["rhs"]))) && $c_a >= 0)
  56. return number_format($c_a, 2, ".", "");
  57. }
  58. }
  59.  
  60. return false; // Default return value.
  61. }
  62. /**
  63. * Converts Currency Codes to Currency Symbols.
  64. *
  65. * Defaults to the `$` dollar sign.
  66. *
  67. * @package s2Member\Utilities
  68. * @since 110531
  69. *
  70. * @param str $currency Expects a 3 character Currency Code.
  71. * @return str A Currency Symbol. Defaults to the `$` sign.
  72. */
  73. public static function symbol($currency = FALSE)
  74. {
  75. $symbols["AUD"] = "$"; // Australian Dollar
  76. $symbols["BRL"] = "R$"; // Brazilian Real
  77. $symbols["CAD"] = "$"; // Canadian Dollar
  78. $symbols["CZK"] = "Kč"; // Czech Koruna
  79. $symbols["DKK"] = "kr"; // Danish Krone
  80. $symbols["EUR"] = "€"; // Euro
  81. $symbols["HKD"] = "$"; // Hong Kong Dollar
  82. $symbols["HUF"] = "Ft"; // Hungarian Forint
  83. $symbols["ILS"] = "₪"; // Israeli New Sheqel
  84. $symbols["JPY"] = "¥"; // Japanese Yen
  85. $symbols["MYR"] = "RM"; // Malaysian Ringgit
  86. $symbols["MXN"] = "$"; // Mexican Peso
  87. $symbols["NOK"] = "kr"; // Norwegian Krone
  88. $symbols["NZD"] = "$"; // New Zealand Dollar
  89. $symbols["PHP"] = "Php"; // Philippine Peso
  90. $symbols["PLN"] = "zł"; // Polish Zloty
  91. $symbols["GBP"] = "£"; // Pound Sterling
  92. $symbols["SGD"] = "$"; // Singapore Dollar
  93. $symbols["SEK"] = "kr"; // Swedish Krona
  94. $symbols["CHF"] = "CHF"; // Swiss Franc
  95. $symbols["TWD"] = "NT$"; // Taiwan New Dollar
  96. $symbols["THB"] = "฿"; // Thai Baht
  97. $symbols["USD"] = "$"; // U.S. Dollar
  98.  
  99. if(($currency = strtoupper($currency)) && !empty($symbols[$currency]))
  100. return $symbols[$currency];
  101.  
  102. else // Else `$` sign.
  103. return "$";
  104. }
  105. }
  106. }
  107. ?>

s2member\includes\classes\utils-cur.inc.php

Source Code Documentation