internationalization_internal.lib.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * File: internationalization_internal.lib.php
  5. * Main API extension library for Chamilo 1.8.7 LMS,
  6. * contains functions for internal use only.
  7. * License: GNU General Public License Version 3 (Free Software Foundation)
  8. * @author Ivan Tcholakov, <ivantcholakov@gmail.com>, 2009, 2010
  9. * @author More authors, mentioned in the correpsonding fragments of this source
  10. *
  11. * Note: All functions and data structures here are not to be used directly.
  12. * See the file internationalization.lib.php which contains the "public" API.
  13. * @package chamilo.library
  14. */
  15. /**
  16. * Global variables used by some callback functions
  17. */
  18. $_api_encoding = null;
  19. $_api_collator = null;
  20. /**
  21. * Appendix to "Language recognition"
  22. * Based on the publication:
  23. * W. B. Cavnar and J. M. Trenkle. N-gram-based text categorization.
  24. * Proceedings of SDAIR-94, 3rd Annual Symposium on Document Analysis
  25. * and Information Retrieval, 1994.
  26. * @link http://citeseer.ist.psu.edu/cache/papers/cs/810/http:zSzzSzwww.info.unicaen.frzSz~giguetzSzclassifzSzcavnar_trenkle_ngram.pdf/n-gram-based-text.pdf
  27. */
  28. /**
  29. * Appendix to "Date and time formats"
  30. */
  31. /**
  32. * Returns an array of translated week days and months, short and normal names.
  33. * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed.
  34. * @return array Returns a multidimensional array with translated week days and months.
  35. */
  36. function &_api_get_day_month_names($language = null) {
  37. static $date_parts = array();
  38. if (empty($language)) {
  39. $language = api_get_interface_language();
  40. }
  41. if (!isset($date_parts[$language])) {
  42. $week_day = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  43. $month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
  44. for ($i = 0; $i < 7; $i++) {
  45. $date_parts[$language]['days_short'][] = get_lang($week_day[$i].'Short', '', $language);
  46. $date_parts[$language]['days_long'][] = get_lang($week_day[$i].'Long', '', $language);
  47. }
  48. for ($i = 0; $i < 12; $i++) {
  49. $date_parts[$language]['months_short'][] = get_lang($month[$i].'Short', '', $language);
  50. $date_parts[$language]['months_long'][] = get_lang($month[$i].'Long', '', $language);
  51. }
  52. }
  53. return $date_parts[$language];
  54. }
  55. /**
  56. * Returns returns person name convention for a given language.
  57. * @param string $language The input language.
  58. * @param string $type The type of the requested convention.
  59. * It may be 'format' for name order convention or 'sort_by' for name sorting convention.
  60. * @return mixed Depending of the requested type,
  61. * the returned result may be string or boolean; null is returned on error;
  62. */
  63. function _api_get_person_name_convention($language, $type)
  64. {
  65. static $conventions;
  66. $language = api_purify_language_id($language);
  67. if (!isset($conventions)) {
  68. $file = dirname(__FILE__).'/internationalization_database/name_order_conventions.php';
  69. if (file_exists($file)) {
  70. $conventions = include ($file);
  71. } else {
  72. $conventions = array(
  73. 'english' => array(
  74. 'format' => 'title first_name last_name',
  75. 'sort_by' => 'first_name'
  76. )
  77. );
  78. }
  79. // Overwrite classic conventions
  80. $customConventions = api_get_configuration_value('name_order_conventions');
  81. if (!empty($customConventions)) {
  82. foreach ($customConventions as $key => $data) {
  83. $conventions[$key] = $data;
  84. }
  85. }
  86. $search1 = array('FIRST_NAME', 'LAST_NAME', 'TITLE');
  87. $replacement1 = array('%F', '%L', '%T');
  88. $search2 = array('first_name', 'last_name', 'title');
  89. $replacement2 = array('%f', '%l', '%t');
  90. foreach (array_keys($conventions) as $key) {
  91. $conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
  92. $conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search2, $replacement2, $conventions[$key]['format']))));
  93. $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
  94. }
  95. }
  96. switch ($type) {
  97. case 'format':
  98. return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l';
  99. case 'sort_by':
  100. return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true;
  101. }
  102. return null;
  103. }
  104. /**
  105. * Replaces non-valid formats for person names with the default (English) format.
  106. * @param string $format The input format to be verified.
  107. * @return bool Returns the same format if is is valid, otherwise returns a valid English format.
  108. */
  109. function _api_validate_person_name_format($format) {
  110. if (empty($format) || stripos($format, '%f') === false || stripos($format, '%l') === false) {
  111. return '%t %f %l';
  112. }
  113. return $format;
  114. }
  115. /**
  116. * Removes leading, trailing and duplicate whitespace and/or commas in a full person name.
  117. * Cleaning is needed for the cases when not all parts of the name are available or when the name is constructed using a "dirty" pattern.
  118. * @param string $person_name The input person name.
  119. * @return string Returns cleaned person name.
  120. */
  121. function _api_clean_person_name($person_name) {
  122. return preg_replace(array('/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'), array(' ', ', ', ',', '', ''), $person_name);
  123. }
  124. /**
  125. * Appendix to "Multibyte string conversion functions"
  126. */
  127. /**
  128. * This is a php-implementation of a function that is similar to mb_convert_encoding() from mbstring extension.
  129. * The function converts a given string from one to another character encoding.
  130. * @param string $string The string being converted.
  131. * @param string $to_encoding The encoding that $string is being converted to.
  132. * @param string $from_encoding The encoding that $string is being converted from.
  133. * @return string Returns the converted string.
  134. */
  135. function _api_convert_encoding(&$string, $to_encoding, $from_encoding)
  136. {
  137. return mb_convert_encoding($string, $to_encoding, $from_encoding);
  138. }
  139. /**
  140. * This function determines the name of corresponding to a given encoding conversion table.
  141. * It is able to deal with some aliases of the encoding.
  142. * @param string $encoding The given encoding identificator, for example 'WINDOWS-1252'.
  143. * @return string Returns the name of the corresponding conversion table, for the same example - 'CP1252'.
  144. */
  145. function _api_get_character_map_name($encoding) {
  146. static $character_map_selector;
  147. if (!isset($character_map_selector)) {
  148. $file = dirname(__FILE__).'/internationalization_database/conversion/character_map_selector.php';
  149. if (file_exists($file)) {
  150. $character_map_selector = include ($file);
  151. } else {
  152. $character_map_selector = array();
  153. }
  154. }
  155. return isset($character_map_selector[$encoding]) ? $character_map_selector[$encoding] : '';
  156. }
  157. /**
  158. * Takes an UTF-8 string and returns an array of integer values representing the Unicode characters.
  159. * Astral planes are supported ie. the ints in the output can be > 0xFFFF. Occurrances of the BOM are ignored.
  160. * Surrogates are not allowed.
  161. * @param string $string The UTF-8 encoded string.
  162. * @return array Returns an array of unicode code points.
  163. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  164. * @link http://hsivonen.iki.fi/php-utf8/
  165. * @author Ivan Tcholakov, August 2009, adaptation for the Dokeos LMS.
  166. */
  167. function _api_utf8_to_unicode(&$string) {
  168. $str = (string)$string;
  169. $state = 0; // cached expected number of octets after the current octet
  170. // until the beginning of the next UTF8 character sequence
  171. $codepoint = 0; // cached Unicode character
  172. $bytes = 1; // cached expected number of octets in the current sequence
  173. $result = array();
  174. $len = api_byte_count($str);
  175. for ($i = 0; $i < $len; $i++) {
  176. $byte = ord($str[$i]);
  177. if ($state == 0) {
  178. // When state is zero we expect either a US-ASCII character or a multi-octet sequence.
  179. if (0 == (0x80 & ($byte))) {
  180. // US-ASCII, pass straight through.
  181. $result[] = $byte;
  182. $bytes = 1;
  183. } else if (0xC0 == (0xE0 & ($byte))) {
  184. // First octet of 2 octet sequence
  185. $codepoint = ($byte);
  186. $codepoint = ($codepoint & 0x1F) << 6;
  187. $state = 1;
  188. $bytes = 2;
  189. } else if (0xE0 == (0xF0 & ($byte))) {
  190. // First octet of 3 octet sequence
  191. $codepoint = ($byte);
  192. $codepoint = ($codepoint & 0x0F) << 12;
  193. $state = 2;
  194. $bytes = 3;
  195. } else if (0xF0 == (0xF8 & ($byte))) {
  196. // First octet of 4 octet sequence
  197. $codepoint = ($byte);
  198. $codepoint = ($codepoint & 0x07) << 18;
  199. $state = 3;
  200. $bytes = 4;
  201. } else if (0xF8 == (0xFC & ($byte))) {
  202. // First octet of 5 octet sequence.
  203. // This is illegal because the encoded codepoint must be either
  204. // (a) not the shortest form or
  205. // (b) outside the Unicode range of 0-0x10FFFF.
  206. // Rather than trying to resynchronize, we will carry on until the end
  207. // of the sequence and let the later error handling code catch it.
  208. $codepoint = ($byte);
  209. $codepoint = ($codepoint & 0x03) << 24;
  210. $state = 4;
  211. $bytes = 5;
  212. } else if (0xFC == (0xFE & ($byte))) {
  213. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  214. $codepoint = ($byte);
  215. $codepoint = ($codepoint & 1) << 30;
  216. $state = 5;
  217. $bytes = 6;
  218. } else {
  219. // Current octet is neither in the US-ASCII range nor a legal first octet of a multi-octet sequence.
  220. $state = 0;
  221. $codepoint = 0;
  222. $bytes = 1;
  223. $result[] = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  224. continue ;
  225. }
  226. } else {
  227. // When state is non-zero, we expect a continuation of the multi-octet sequence
  228. if (0x80 == (0xC0 & ($byte))) {
  229. // Legal continuation.
  230. $shift = ($state - 1) * 6;
  231. $tmp = $byte;
  232. $tmp = ($tmp & 0x0000003F) << $shift;
  233. $codepoint |= $tmp;
  234. // End of the multi-octet sequence. $codepoint now contains the final Unicode codepoint to be output
  235. if (0 == --$state) {
  236. // Check for illegal sequences and codepoints.
  237. // From Unicode 3.1, non-shortest form is illegal
  238. if (((2 == $bytes) && ($codepoint < 0x0080)) ||
  239. ((3 == $bytes) && ($codepoint < 0x0800)) ||
  240. ((4 == $bytes) && ($codepoint < 0x10000)) ||
  241. (4 < $bytes) ||
  242. // From Unicode 3.2, surrogate characters are illegal
  243. (($codepoint & 0xFFFFF800) == 0xD800) ||
  244. // Codepoints outside the Unicode range are illegal
  245. ($codepoint > 0x10FFFF)) {
  246. $state = 0;
  247. $codepoint = 0;
  248. $bytes = 1;
  249. $result[] = 0xFFFD;
  250. continue ;
  251. }
  252. if (0xFEFF != $codepoint) {
  253. // BOM is legal but we don't want to output it
  254. $result[] = $codepoint;
  255. }
  256. // Initialize UTF8 cache
  257. $state = 0;
  258. $codepoint = 0;
  259. $bytes = 1;
  260. }
  261. } else {
  262. // ((0xC0 & (*in) != 0x80) && (state != 0))
  263. // Incomplete multi-octet sequence.
  264. $state = 0;
  265. $codepoint = 0;
  266. $bytes = 1;
  267. $result[] = 0xFFFD;
  268. }
  269. }
  270. }
  271. return $result;
  272. }
  273. /**
  274. * Takes an array of Unicode codepoints and returns a UTF-8 string.
  275. * @param array $codepoints An array of Unicode codepoints representing a string.
  276. * @return string Returns a UTF-8 string constructed using the given codepoints.
  277. */
  278. function _api_utf8_from_unicode($codepoints) {
  279. return implode(array_map('_api_utf8_chr', $codepoints));
  280. }
  281. /**
  282. * Takes a codepoint and returns its correspondent UTF-8 encoded character.
  283. * Astral planes are supported, ie the intger input can be > 0xFFFF. Occurrances of the BOM are ignored.
  284. * Surrogates are not allowed.
  285. * @param int $codepoint The Unicode codepoint.
  286. * @return string Returns the corresponding UTF-8 character.
  287. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  288. * @link http://hsivonen.iki.fi/php-utf8/
  289. * @author Ivan Tcholakov, 2009, modifications for the Dokeos LMS.
  290. * @see _api_utf8_from_unicode()
  291. * This is a UTF-8 aware version of the function chr().
  292. * @link http://php.net/manual/en/function.chr.php
  293. */
  294. function _api_utf8_chr($codepoint) {
  295. // ASCII range (including control chars)
  296. if ( ($codepoint >= 0) && ($codepoint <= 0x007f) ) {
  297. $result = chr($codepoint);
  298. // 2 byte sequence
  299. } else if ($codepoint <= 0x07ff) {
  300. $result = chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x003f));
  301. // Byte order mark (skip)
  302. } else if($codepoint == 0xFEFF) {
  303. // nop -- zap the BOM
  304. $result = '';
  305. // Test for illegal surrogates
  306. } else if ($codepoint >= 0xD800 && $codepoint <= 0xDFFF) {
  307. // found a surrogate
  308. $result = _api_utf8_chr(0xFFFD); // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  309. // 3 byte sequence
  310. } else if ($codepoint <= 0xffff) {
  311. $result = chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x003f)) . chr(0x80 | ($codepoint & 0x003f));
  312. // 4 byte sequence
  313. } else if ($codepoint <= 0x10ffff) {
  314. $result = chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  315. } else {
  316. // out of range
  317. $result = _api_utf8_chr(0xFFFD);
  318. }
  319. return $result;
  320. }
  321. /**
  322. * Appendix to "String comparison"
  323. */
  324. /**
  325. * A reverse function from php-core function strnatcmp(), performs string comparison in reverse natural (alpha-numerical) order.
  326. * @param string $string1 The first string.
  327. * @param string $string2 The second string.
  328. * @return int Returns 0 if $string1 = $string2; >0 if $string1 < $string2; <0 if $string1 > $string2.
  329. */
  330. function _api_strnatrcmp($string1, $string2) {
  331. return strnatcmp($string2, $string1);
  332. }
  333. /**
  334. * ICU locales (accessible through intl extension).
  335. */
  336. /**
  337. * Appendix to "Encoding management functions"
  338. */
  339. /**
  340. * Sets/Gets internal character encoding of the common string functions within the PHP mbstring extension.
  341. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding.
  342. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  343. * Note: This function is used in the global initialization script for setting the internal encoding to the platform's character set.
  344. * @link http://php.net/manual/en/function.mb-internal-encoding
  345. */
  346. function _api_mb_internal_encoding($encoding = null)
  347. {
  348. return mb_internal_encoding($encoding);
  349. }
  350. /**
  351. * Checks whether the specified encoding is supported by the PHP mbstring extension.
  352. * @param string $encoding The specified encoding.
  353. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  354. */
  355. function _api_mb_supports($encoding) {
  356. static $supported = array();
  357. if (!isset($supported[$encoding])) {
  358. if (MBSTRING_INSTALLED) {
  359. $supported[$encoding] = api_equal_encodings($encoding, mb_list_encodings(), true);
  360. } else {
  361. $supported[$encoding] = false;
  362. }
  363. }
  364. return $supported[$encoding];
  365. }
  366. /**
  367. * Checks whether the specified encoding is supported by the PHP iconv extension.
  368. * @param string $encoding The specified encoding.
  369. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  370. */
  371. function _api_iconv_supports($encoding) {
  372. static $supported = array();
  373. if (!isset($supported[$encoding])) {
  374. if (ICONV_INSTALLED) {
  375. $enc = api_refine_encoding_id($encoding);
  376. if ($enc != 'HTML-ENTITIES') {
  377. $test_string = '';
  378. for ($i = 32; $i < 128; $i++) {
  379. $test_string .= chr($i);
  380. }
  381. $supported[$encoding] = (@iconv_strlen($test_string, $enc)) ? true : false;
  382. } else {
  383. $supported[$encoding] = false;
  384. }
  385. } else {
  386. $supported[$encoding] = false;
  387. }
  388. }
  389. return $supported[$encoding];
  390. }
  391. // This function checks whether the function _api_convert_encoding() (the php-
  392. // implementation) is able to convert from/to a given encoding.
  393. function _api_convert_encoding_supports($encoding) {
  394. static $supports = array();
  395. if (!isset($supports[$encoding])) {
  396. $supports[$encoding] = _api_get_character_map_name(api_refine_encoding_id($encoding)) != '';
  397. }
  398. return $supports[$encoding];
  399. }
  400. /**
  401. * Checks whether the specified encoding is supported by the html-entitiy related functions.
  402. * @param string $encoding The specified encoding.
  403. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  404. */
  405. function _api_html_entity_supports($encoding) {
  406. static $supports = array();
  407. if (!isset($supports[$encoding])) {
  408. // See http://php.net/manual/en/function.htmlentities.php
  409. $html_entity_encodings = array(
  410. 'ISO-8859-1',
  411. 'ISO-8859-15',
  412. 'UTF-8',
  413. 'CP866',
  414. 'CP1251',
  415. 'CP1252',
  416. 'KOI8-R',
  417. 'BIG5', '950',
  418. 'GB2312', '936',
  419. 'BIG5-HKSCS',
  420. 'Shift_JIS', 'SJIS', '932',
  421. 'EUC-JP', 'EUCJP'
  422. );
  423. $supports[$encoding] = api_equal_encodings($encoding, $html_entity_encodings);
  424. }
  425. return $supports[$encoding];
  426. }