PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/Nls/lib/Horde/Nls.php

https://github.com/imr/horde
PHP | 252 lines | 126 code | 29 blank | 97 comment | 20 complexity | acb912c54454441e31e0999d2361abd2 MD5 | raw file
  1. <?php
  2. /**
  3. * The Horde_Nls:: class provides Native Language Support. This includes
  4. * common methods for handling language data, timezones, and hostname->country
  5. * lookups.
  6. *
  7. * Copyright 1999-2014 Horde LLC (http://www.horde.org/)
  8. *
  9. * See the enclosed file COPYING for license information (LGPL). If you
  10. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  11. *
  12. * @author Jon Parise <jon@horde.org>
  13. * @author Chuck Hagenbuch <chuck@horde.org>
  14. * @author Jan Schneider <jan@horde.org>
  15. * @author Michael Slusarz <slusarz@horde.org>
  16. * @category Horde
  17. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  18. * @package Nls
  19. */
  20. class Horde_Nls
  21. {
  22. /**
  23. * DNS resolver.
  24. *
  25. * @var Net_DNS2_Resolver
  26. */
  27. static public $dnsResolver;
  28. /**
  29. * Cached values.
  30. *
  31. * @var array
  32. */
  33. static protected $_cache = array();
  34. /**
  35. * Check to see if character set is valid for htmlspecialchars() calls.
  36. *
  37. * @param string $charset The character set to check.
  38. *
  39. * @return boolean Is charset valid for the current system?
  40. */
  41. static public function checkCharset($charset)
  42. {
  43. if (is_null($charset) || empty($charset)) {
  44. return false;
  45. }
  46. $valid = true;
  47. ini_set('track_errors', 1);
  48. @htmlspecialchars('', ENT_COMPAT, $charset);
  49. if (isset($php_errormsg)) {
  50. $valid = false;
  51. }
  52. ini_restore('track_errors');
  53. return $valid;
  54. }
  55. /**
  56. * Returns a list of available timezones.
  57. *
  58. * @return array List of timezones.
  59. */
  60. static public function getTimezones()
  61. {
  62. $timezones = DateTimeZone::listIdentifiers();
  63. return array_combine($timezones, $timezones);
  64. }
  65. /**
  66. * Get the locale info returned by localeconv(), but cache it, to
  67. * avoid repeated calls.
  68. *
  69. * @return array The results of localeconv().
  70. */
  71. static public function getLocaleInfo()
  72. {
  73. if (!isset(self::$_cache['lc_info'])) {
  74. self::$_cache['lc_info'] = localeconv();
  75. }
  76. return self::$_cache['lc_info'];
  77. }
  78. /**
  79. * Get the language info returned by nl_langinfo(), but cache it, to
  80. * avoid repeated calls.
  81. *
  82. * @param const $item The langinfo item to return.
  83. *
  84. * @return array The results of nl_langinfo().
  85. */
  86. static public function getLangInfo($item)
  87. {
  88. if (!function_exists('nl_langinfo')) {
  89. return false;
  90. }
  91. if (!isset(self::$_cache['nl_info'])) {
  92. self::$_cache['nl_info'] = array();
  93. }
  94. if (!isset(self::$_cache['nl_info'][$item])) {
  95. self::$_cache['nl_info'][$item] = nl_langinfo($item);
  96. }
  97. return self::$_cache['nl_info'][$item];
  98. }
  99. /**
  100. * Get country information from a hostname or IP address.
  101. *
  102. * @param string $host The hostname or IP address.
  103. * @param string $datafile The datafile for the GeoIP lookup. If not set,
  104. * will skip this lookup.
  105. *
  106. * @return mixed On success, return an array with the following entries:
  107. * 'code' => Country Code
  108. * 'name' => Country Name
  109. * On failure, return false.
  110. */
  111. static public function getCountryByHost($host, $datafile = null)
  112. {
  113. /* List of generic domains that we know is not in the country TLD
  114. list. See: http://www.iana.org/gtld/gtld.htm */
  115. $generic = array(
  116. 'aero', 'biz', 'com', 'coop', 'edu', 'gov', 'info', 'int', 'mil',
  117. 'museum', 'name', 'net', 'org', 'pro'
  118. );
  119. $checkHost = null;
  120. if (preg_match('/^\d+\.\d+\.\d+\.\d+$/', $host)) {
  121. if (isset(self::$dnsResolver)) {
  122. try {
  123. $response = self::$dnsResolver->query($host, 'PTR');
  124. foreach ($response->answer as $val) {
  125. if (isset($val->ptrdname)) {
  126. $checkHost = $val->ptrdname;
  127. break;
  128. }
  129. }
  130. } catch (Net_DNS2_Exception $e) {}
  131. }
  132. if (is_null($checkHost)) {
  133. $checkHost = @gethostbyaddr($host);
  134. }
  135. } else {
  136. $checkHost = $host;
  137. }
  138. /* Get the TLD of the hostname. */
  139. $pos = strrpos($checkHost, '.');
  140. if ($pos === false) {
  141. return false;
  142. }
  143. $domain = Horde_String::lower(substr($checkHost, $pos + 1));
  144. /* Try lookup via TLD first. */
  145. if (!in_array($domain, $generic)) {
  146. $name = self::tldLookup($domain);
  147. if ($name) {
  148. return array(
  149. 'code' => $domain,
  150. 'name' => $name
  151. );
  152. }
  153. }
  154. /* Try GeoIP lookup next. */
  155. $geoip = new Horde_Nls_Geoip($datafile);
  156. return $geoip->getCountryInfo($checkHost);
  157. }
  158. /**
  159. * Do a top level domain (TLD) lookup.
  160. *
  161. * @param string $code A 2-letter country code.
  162. *
  163. * @return mixed The localized country name, or null if not found.
  164. */
  165. static public function tldLookup($code)
  166. {
  167. if (!isset(self::$_cache['tld'])) {
  168. include __DIR__ . '/Nls/Tld.php';
  169. self::$_cache['tld'] = $tld;
  170. }
  171. $code = Horde_String::lower($code);
  172. return isset(self::$_cache['tld'][$code])
  173. ? self::$_cache['tld'][$code]
  174. : null;
  175. }
  176. /**
  177. * Returns either a specific or all ISO-3166 country names.
  178. *
  179. * @param string $code The ISO 3166 country code.
  180. *
  181. * @return mixed If a country code has been requested will return the
  182. * corresponding country name. If empty will return an
  183. * array of all the country codes and their names.
  184. */
  185. static public function getCountryISO($code = null)
  186. {
  187. if (!isset(self::$_cache['iso3166'])) {
  188. include __DIR__ . '/Nls/Countries.php';
  189. self::$_cache['iso3166'] = $countries;
  190. }
  191. if (empty($code)) {
  192. return self::$_cache['iso3166'];
  193. }
  194. $code = Horde_String::upper($code);
  195. return isset(self::$_cache['iso3166'][$code])
  196. ? self::$_cache['iso3166'][$code]
  197. : null;
  198. }
  199. /**
  200. * Returns either a specific or all ISO-639 language names.
  201. *
  202. * @param string $code The ISO 639 language code.
  203. *
  204. * @return mixed If a language code has been requested will return the
  205. * corresponding language name. If empty will return an
  206. * array of all the language codes (keys) and their names
  207. * (values).
  208. */
  209. static public function getLanguageISO($code = null)
  210. {
  211. if (!isset(self::$_cache['iso639'])) {
  212. include __DIR__ . '/Nls/Languages.php';
  213. self::$_cache['iso639'] = $languages;
  214. }
  215. if (empty($code)) {
  216. return self::$_cache['iso639'];
  217. }
  218. $code = substr(Horde_String::lower(trim($code)), 0, 2);
  219. return isset(self::$_cache['iso639'][$code])
  220. ? self::$_cache['iso639'][$code]
  221. : null;
  222. }
  223. }