internationalization_internal.lib.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. * This function returns an array of those languages that can use Latin 1 encoding.
  22. * Appendix to "Language support"
  23. * @return array The array of languages that can use Latin 1 encoding (ISO-8859-15, ISO-8859-1, WINDOWS-1252, ...).
  24. * Note: The returned language identificators are purified, without suffixes.
  25. */
  26. function _api_get_latin1_compatible_languages() {
  27. static $latin1_languages;
  28. if (!isset($latin1_languages)) {
  29. $latin1_languages = array();
  30. $encodings = & _api_non_utf8_encodings();
  31. foreach ($encodings as $key => $value) {
  32. if (api_is_latin1($value[0])) {
  33. $latin1_languages[] = $key;
  34. }
  35. }
  36. }
  37. return $latin1_languages;
  38. }
  39. /**
  40. * Appendix to "Language recognition"
  41. * Based on the publication:
  42. * W. B. Cavnar and J. M. Trenkle. N-gram-based text categorization.
  43. * Proceedings of SDAIR-94, 3rd Annual Symposium on Document Analysis
  44. * and Information Retrieval, 1994.
  45. * @link http://citeseer.ist.psu.edu/cache/papers/cs/810/http:zSzzSzwww.info.unicaen.frzSz~giguetzSzclassifzSzcavnar_trenkle_ngram.pdf/n-gram-based-text.pdf
  46. */
  47. /**
  48. * Appendix to "Date and time formats"
  49. */
  50. /**
  51. * Returns an array of translated week days and months, short and normal names.
  52. * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed.
  53. * @return array Returns a multidimensional array with translated week days and months.
  54. */
  55. function &_api_get_day_month_names($language = null) {
  56. static $date_parts = array();
  57. if (empty($language)) {
  58. $language = api_get_interface_language();
  59. }
  60. if (!isset($date_parts[$language])) {
  61. $week_day = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  62. $month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
  63. for ($i = 0; $i < 7; $i++) {
  64. $date_parts[$language]['days_short'][] = get_lang($week_day[$i].'Short', '', $language);
  65. $date_parts[$language]['days_long'][] = get_lang($week_day[$i].'Long', '', $language);
  66. }
  67. for ($i = 0; $i < 12; $i++) {
  68. $date_parts[$language]['months_short'][] = get_lang($month[$i].'Short', '', $language);
  69. $date_parts[$language]['months_long'][] = get_lang($month[$i].'Long', '', $language);
  70. }
  71. }
  72. return $date_parts[$language];
  73. }
  74. /**
  75. * Returns returns person name convention for a given language.
  76. * @param string $language The input language.
  77. * @param string $type The type of the requested convention.
  78. * It may be 'format' for name order convention or 'sort_by' for name sorting convention.
  79. * @return mixed Depending of the requested type,
  80. * the returned result may be string or boolean; null is returned on error;
  81. */
  82. function _api_get_person_name_convention($language, $type)
  83. {
  84. static $conventions;
  85. $language = api_purify_language_id($language);
  86. if (!isset($conventions)) {
  87. $file = dirname(__FILE__).'/internationalization_database/name_order_conventions.php';
  88. if (file_exists($file)) {
  89. $conventions = include ($file);
  90. } else {
  91. $conventions = array(
  92. 'english' => array(
  93. 'format' => 'title first_name last_name',
  94. 'sort_by' => 'first_name'
  95. )
  96. );
  97. }
  98. // Overwrite classic conventions
  99. $customConventions = api_get_configuration_value('name_order_conventions');
  100. if (!empty($customConventions)) {
  101. foreach ($customConventions as $key => $data) {
  102. $conventions[$key] = $data;
  103. }
  104. }
  105. $search1 = array('FIRST_NAME', 'LAST_NAME', 'TITLE');
  106. $replacement1 = array('%F', '%L', '%T');
  107. $search2 = array('first_name', 'last_name', 'title');
  108. $replacement2 = array('%f', '%l', '%t');
  109. foreach (array_keys($conventions) as $key) {
  110. $conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
  111. $conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search2, $replacement2, $conventions[$key]['format']))));
  112. $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
  113. }
  114. }
  115. switch ($type) {
  116. case 'format':
  117. return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l';
  118. case 'sort_by':
  119. return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true;
  120. }
  121. return null;
  122. }
  123. /**
  124. * Replaces non-valid formats for person names with the default (English) format.
  125. * @param string $format The input format to be verified.
  126. * @return bool Returns the same format if is is valid, otherwise returns a valid English format.
  127. */
  128. function _api_validate_person_name_format($format) {
  129. if (empty($format) || stripos($format, '%f') === false || stripos($format, '%l') === false) {
  130. return '%t %f %l';
  131. }
  132. return $format;
  133. }
  134. /**
  135. * Removes leading, trailing and duplicate whitespace and/or commas in a full person name.
  136. * 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.
  137. * @param string $person_name The input person name.
  138. * @return string Returns cleaned person name.
  139. */
  140. function _api_clean_person_name($person_name) {
  141. return preg_replace(array('/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'), array(' ', ', ', ',', '', ''), $person_name);
  142. }
  143. /**
  144. * Appendix to "Multibyte string conversion functions"
  145. */
  146. /**
  147. * This is a php-implementation of a function that is similar to mb_convert_encoding() from mbstring extension.
  148. * The function converts a given string from one to another character encoding.
  149. * @param string $string The string being converted.
  150. * @param string $to_encoding The encoding that $string is being converted to.
  151. * @param string $from_encoding The encoding that $string is being converted from.
  152. * @return string Returns the converted string.
  153. */
  154. function _api_convert_encoding(&$string, $to_encoding, $from_encoding)
  155. {
  156. return mb_convert_encoding($string, $to_encoding, $from_encoding);
  157. /*
  158. $str = (string)$string;
  159. static $character_map = array();
  160. static $utf8_compatible = array('UTF-8', 'US-ASCII');
  161. if (empty($str)) {
  162. return $str;
  163. }
  164. $to_encoding = api_refine_encoding_id($to_encoding);
  165. $from_encoding = api_refine_encoding_id($from_encoding);
  166. if (api_equal_encodings($to_encoding, $from_encoding)) {
  167. return $str;
  168. }
  169. if ($to_encoding == 'HTML-ENTITIES') {
  170. return api_htmlentities($str, ENT_QUOTES, $from_encoding);
  171. }
  172. if ($from_encoding == 'HTML-ENTITIES') {
  173. return api_html_entity_decode($str, ENT_QUOTES, $to_encoding);
  174. }
  175. $to = _api_get_character_map_name($to_encoding);
  176. $from = _api_get_character_map_name($from_encoding);
  177. if (empty($to) || empty($from) || $to == $from || (in_array($to, $utf8_compatible) && in_array($from, $utf8_compatible))) {
  178. return $str;
  179. }
  180. if (!isset($character_map[$to])) {
  181. $character_map[$to] = &_api_parse_character_map($to);
  182. }
  183. if ($character_map[$to] === false) {
  184. return $str;
  185. }
  186. if (!isset($character_map[$from])) {
  187. $character_map[$from] = &_api_parse_character_map($from);
  188. }
  189. if ($character_map[$from] === false) {
  190. return $str;
  191. }
  192. if ($from != 'UTF-8') {
  193. $len = api_byte_count($str);
  194. $codepoints = array();
  195. for ($i = 0; $i < $len; $i++) {
  196. $ord = ord($str[$i]);
  197. if ($ord > 127) {
  198. if (isset($character_map[$from]['local'][$ord])) {
  199. $codepoints[] = $character_map[$from]['local'][$ord];
  200. } else {
  201. $codepoints[] = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  202. }
  203. } else {
  204. $codepoints[] = $ord;
  205. }
  206. }
  207. } else {
  208. $codepoints = _api_utf8_to_unicode($str);
  209. }
  210. if ($to != 'UTF-8') {
  211. foreach ($codepoints as $i => &$codepoint) {
  212. if ($codepoint > 127) {
  213. if (isset($character_map[$to]['unicode'][$codepoint])) {
  214. $codepoint = chr($character_map[$to]['unicode'][$codepoint]);
  215. } else {
  216. $codepoint = '?'; // Unknown character.
  217. }
  218. } else {
  219. $codepoint = chr($codepoint);
  220. }
  221. }
  222. $str = implode($codepoints);
  223. } else {
  224. $str = _api_utf8_from_unicode($codepoints);
  225. }
  226. return $str;*/
  227. }
  228. /**
  229. * This function determines the name of corresponding to a given encoding conversion table.
  230. * It is able to deal with some aliases of the encoding.
  231. * @param string $encoding The given encoding identificator, for example 'WINDOWS-1252'.
  232. * @return string Returns the name of the corresponding conversion table, for the same example - 'CP1252'.
  233. */
  234. function _api_get_character_map_name($encoding) {
  235. static $character_map_selector;
  236. if (!isset($character_map_selector)) {
  237. $file = dirname(__FILE__).'/internationalization_database/conversion/character_map_selector.php';
  238. if (file_exists($file)) {
  239. $character_map_selector = include ($file);
  240. } else {
  241. $character_map_selector = array();
  242. }
  243. }
  244. return isset($character_map_selector[$encoding]) ? $character_map_selector[$encoding] : '';
  245. }
  246. /**
  247. * Takes an UTF-8 string and returns an array of integer values representing the Unicode characters.
  248. * Astral planes are supported ie. the ints in the output can be > 0xFFFF. Occurrances of the BOM are ignored.
  249. * Surrogates are not allowed.
  250. * @param string $string The UTF-8 encoded string.
  251. * @return array Returns an array of unicode code points.
  252. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  253. * @link http://hsivonen.iki.fi/php-utf8/
  254. * @author Ivan Tcholakov, August 2009, adaptation for the Dokeos LMS.
  255. */
  256. function _api_utf8_to_unicode(&$string) {
  257. $str = (string)$string;
  258. $state = 0; // cached expected number of octets after the current octet
  259. // until the beginning of the next UTF8 character sequence
  260. $codepoint = 0; // cached Unicode character
  261. $bytes = 1; // cached expected number of octets in the current sequence
  262. $result = array();
  263. $len = api_byte_count($str);
  264. for ($i = 0; $i < $len; $i++) {
  265. $byte = ord($str[$i]);
  266. if ($state == 0) {
  267. // When state is zero we expect either a US-ASCII character or a multi-octet sequence.
  268. if (0 == (0x80 & ($byte))) {
  269. // US-ASCII, pass straight through.
  270. $result[] = $byte;
  271. $bytes = 1;
  272. } else if (0xC0 == (0xE0 & ($byte))) {
  273. // First octet of 2 octet sequence
  274. $codepoint = ($byte);
  275. $codepoint = ($codepoint & 0x1F) << 6;
  276. $state = 1;
  277. $bytes = 2;
  278. } else if (0xE0 == (0xF0 & ($byte))) {
  279. // First octet of 3 octet sequence
  280. $codepoint = ($byte);
  281. $codepoint = ($codepoint & 0x0F) << 12;
  282. $state = 2;
  283. $bytes = 3;
  284. } else if (0xF0 == (0xF8 & ($byte))) {
  285. // First octet of 4 octet sequence
  286. $codepoint = ($byte);
  287. $codepoint = ($codepoint & 0x07) << 18;
  288. $state = 3;
  289. $bytes = 4;
  290. } else if (0xF8 == (0xFC & ($byte))) {
  291. // First octet of 5 octet sequence.
  292. // This is illegal because the encoded codepoint must be either
  293. // (a) not the shortest form or
  294. // (b) outside the Unicode range of 0-0x10FFFF.
  295. // Rather than trying to resynchronize, we will carry on until the end
  296. // of the sequence and let the later error handling code catch it.
  297. $codepoint = ($byte);
  298. $codepoint = ($codepoint & 0x03) << 24;
  299. $state = 4;
  300. $bytes = 5;
  301. } else if (0xFC == (0xFE & ($byte))) {
  302. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  303. $codepoint = ($byte);
  304. $codepoint = ($codepoint & 1) << 30;
  305. $state = 5;
  306. $bytes = 6;
  307. } else {
  308. // Current octet is neither in the US-ASCII range nor a legal first octet of a multi-octet sequence.
  309. $state = 0;
  310. $codepoint = 0;
  311. $bytes = 1;
  312. $result[] = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  313. continue ;
  314. }
  315. } else {
  316. // When state is non-zero, we expect a continuation of the multi-octet sequence
  317. if (0x80 == (0xC0 & ($byte))) {
  318. // Legal continuation.
  319. $shift = ($state - 1) * 6;
  320. $tmp = $byte;
  321. $tmp = ($tmp & 0x0000003F) << $shift;
  322. $codepoint |= $tmp;
  323. // End of the multi-octet sequence. $codepoint now contains the final Unicode codepoint to be output
  324. if (0 == --$state) {
  325. // Check for illegal sequences and codepoints.
  326. // From Unicode 3.1, non-shortest form is illegal
  327. if (((2 == $bytes) && ($codepoint < 0x0080)) ||
  328. ((3 == $bytes) && ($codepoint < 0x0800)) ||
  329. ((4 == $bytes) && ($codepoint < 0x10000)) ||
  330. (4 < $bytes) ||
  331. // From Unicode 3.2, surrogate characters are illegal
  332. (($codepoint & 0xFFFFF800) == 0xD800) ||
  333. // Codepoints outside the Unicode range are illegal
  334. ($codepoint > 0x10FFFF)) {
  335. $state = 0;
  336. $codepoint = 0;
  337. $bytes = 1;
  338. $result[] = 0xFFFD;
  339. continue ;
  340. }
  341. if (0xFEFF != $codepoint) {
  342. // BOM is legal but we don't want to output it
  343. $result[] = $codepoint;
  344. }
  345. // Initialize UTF8 cache
  346. $state = 0;
  347. $codepoint = 0;
  348. $bytes = 1;
  349. }
  350. } else {
  351. // ((0xC0 & (*in) != 0x80) && (state != 0))
  352. // Incomplete multi-octet sequence.
  353. $state = 0;
  354. $codepoint = 0;
  355. $bytes = 1;
  356. $result[] = 0xFFFD;
  357. }
  358. }
  359. }
  360. return $result;
  361. }
  362. /**
  363. * Takes an array of Unicode codepoints and returns a UTF-8 string.
  364. * @param array $codepoints An array of Unicode codepoints representing a string.
  365. * @return string Returns a UTF-8 string constructed using the given codepoints.
  366. */
  367. function _api_utf8_from_unicode($codepoints) {
  368. return implode(array_map('_api_utf8_chr', $codepoints));
  369. }
  370. /**
  371. * Takes a codepoint and returns its correspondent UTF-8 encoded character.
  372. * Astral planes are supported, ie the intger input can be > 0xFFFF. Occurrances of the BOM are ignored.
  373. * Surrogates are not allowed.
  374. * @param int $codepoint The Unicode codepoint.
  375. * @return string Returns the corresponding UTF-8 character.
  376. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  377. * @link http://hsivonen.iki.fi/php-utf8/
  378. * @author Ivan Tcholakov, 2009, modifications for the Dokeos LMS.
  379. * @see _api_utf8_from_unicode()
  380. * This is a UTF-8 aware version of the function chr().
  381. * @link http://php.net/manual/en/function.chr.php
  382. */
  383. function _api_utf8_chr($codepoint) {
  384. // ASCII range (including control chars)
  385. if ( ($codepoint >= 0) && ($codepoint <= 0x007f) ) {
  386. $result = chr($codepoint);
  387. // 2 byte sequence
  388. } else if ($codepoint <= 0x07ff) {
  389. $result = chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x003f));
  390. // Byte order mark (skip)
  391. } else if($codepoint == 0xFEFF) {
  392. // nop -- zap the BOM
  393. $result = '';
  394. // Test for illegal surrogates
  395. } else if ($codepoint >= 0xD800 && $codepoint <= 0xDFFF) {
  396. // found a surrogate
  397. $result = _api_utf8_chr(0xFFFD); // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  398. // 3 byte sequence
  399. } else if ($codepoint <= 0xffff) {
  400. $result = chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x003f)) . chr(0x80 | ($codepoint & 0x003f));
  401. // 4 byte sequence
  402. } else if ($codepoint <= 0x10ffff) {
  403. $result = chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  404. } else {
  405. // out of range
  406. $result = _api_utf8_chr(0xFFFD);
  407. }
  408. return $result;
  409. }
  410. /**
  411. * Takes the first UTF-8 character in a string and returns its Unicode codepoint.
  412. * @param string $utf8_character The UTF-8 encoded character.
  413. * @return int Returns: the codepoint; or 0xFFFD (unknown character) when the input string is empty.
  414. * This is a UTF-8 aware version of the function ord().
  415. * @link http://php.net/manual/en/function.ord.php
  416. * Note about a difference with the original funtion ord(): ord('') returns 0.
  417. */
  418. function _api_utf8_ord($utf8_character) {
  419. if ($utf8_character == '') {
  420. return 0xFFFD;
  421. }
  422. $codepoints = _api_utf8_to_unicode($utf8_character);
  423. return $codepoints[0];
  424. }
  425. /**
  426. * Makes a html-entity from Unicode codepoint.
  427. * @param int $codepoint The Unicode codepoint.
  428. * @return string Returns the corresponding html-entity; or ASCII character if $codepoint < 128.
  429. */
  430. function _api_html_entity_from_unicode($codepoint) {
  431. if ($codepoint < 128) {
  432. return chr($codepoint);
  433. }
  434. return '&#'.$codepoint.';';
  435. }
  436. /**
  437. * Appendix to "Common multibyte string functions"
  438. */
  439. /**
  440. * The following function reads case folding properties about a given character from a file-based "database".
  441. * @param int $codepoint The Unicode codepoint that represents a caharacter.
  442. * @param string $type (optional) The type of initial case to be altered: 'lower' (default) or 'upper'.
  443. * @return array Returns an array with properties used to change case of the character.
  444. */
  445. function &_api_utf8_get_letter_case_properties($codepoint, $type = 'lower') {
  446. static $config = array();
  447. static $range = array();
  448. if (!isset($range[$codepoint])) {
  449. if ($codepoint > 128 && $codepoint < 256) {
  450. $range[$codepoint] = '0080_00ff'; // Latin-1 Supplement
  451. } elseif ($codepoint < 384) {
  452. $range[$codepoint] = '0100_017f'; // Latin Extended-A
  453. } elseif ($codepoint < 592) {
  454. $range[$codepoint] = '0180_024F'; // Latin Extended-B
  455. } elseif ($codepoint < 688) {
  456. $range[$codepoint] = '0250_02af'; // IPA Extensions
  457. } elseif ($codepoint >= 880 && $codepoint < 1024) {
  458. $range[$codepoint] = '0370_03ff'; // Greek and Coptic
  459. } elseif ($codepoint < 1280) {
  460. $range[$codepoint] = '0400_04ff'; // Cyrillic
  461. } elseif ($codepoint < 1328) {
  462. $range[$codepoint] = '0500_052f'; // Cyrillic Supplement
  463. } elseif ($codepoint < 1424) {
  464. $range[$codepoint] = '0530_058f'; // Armenian
  465. } elseif ($codepoint >= 7680 && $codepoint < 7936) {
  466. $range[$codepoint] = '1e00_1eff'; // Latin Extended Additional
  467. } elseif ($codepoint < 8192) {
  468. $range[$codepoint] = '1f00_1fff'; // Greek Extended
  469. } elseif ($codepoint >= 8448 && $codepoint < 8528) {
  470. $range[$codepoint] = '2100_214f'; // Letterlike Symbols
  471. } elseif ($codepoint < 8592) {
  472. $range[$codepoint] = '2150_218f'; // Number Forms
  473. } elseif ($codepoint >= 9312 && $codepoint < 9472) {
  474. $range[$codepoint] = '2460_24ff'; // Enclosed Alphanumerics
  475. } elseif ($codepoint >= 11264 && $codepoint < 11360) {
  476. $range[$codepoint] = '2c00_2c5f'; // Glagolitic
  477. } elseif ($codepoint < 11392) {
  478. $range[$codepoint] = '2c60_2c7f'; // Latin Extended-C
  479. } elseif ($codepoint < 11520) {
  480. $range[$codepoint] = '2c80_2cff'; // Coptic
  481. } elseif ($codepoint >= 65280 && $codepoint < 65520) {
  482. $range[$codepoint] = 'ff00_ffef'; // Halfwidth and Fullwidth Forms
  483. } else {
  484. $range[$codepoint] = false;
  485. }
  486. if ($range[$codepoint] === false) {
  487. return null;
  488. }
  489. if (!isset($config[$range[$codepoint]])) {
  490. $file = dirname(__FILE__).'/internationalization_database/casefolding/' . $range[$codepoint] . '.php';
  491. if (file_exists($file)) {
  492. include $file;
  493. }
  494. }
  495. }
  496. if ($range[$codepoint] === false || !isset($config[$range[$codepoint]])) {
  497. return null;
  498. }
  499. $result = array();
  500. $count = count($config[$range[$codepoint]]);
  501. for ($i = 0; $i < $count; $i++) {
  502. if ($type === 'lower' && $config[$range[$codepoint]][$i][$type][0] === $codepoint) {
  503. $result[] = $config[$range[$codepoint]][$i];
  504. } elseif ($type === 'upper' && $config[$range[$codepoint]][$i][$type] === $codepoint) {
  505. $result[] = $config[$range[$codepoint]][$i];
  506. }
  507. }
  508. return $result;
  509. }
  510. /**
  511. * A callback for serving the function api_ucwords().
  512. * @param array $matches Input array of matches corresponding to a single word
  513. * @return string Returns a with first char of the word in uppercase
  514. */
  515. function _api_utf8_ucwords_callback($matches) {
  516. return $matches[2] . api_ucfirst(ltrim($matches[0]), 'UTF-8');
  517. }
  518. /**
  519. * Appendix to "Common sting operations with arrays"
  520. */
  521. /**
  522. * This callback function converts from UTF-8 to other encoding. It works with strings or arrays of strings.
  523. * @param mixed $variable The variable to be converted, a string or an array.
  524. * @return mixed Returns the converted form UTF-8 $variable with the same type, string or array.
  525. */
  526. function _api_array_utf8_decode($variable) {
  527. global $_api_encoding;
  528. if (is_array($variable)) {
  529. return array_map('_api_array_utf8_decode', $variable);
  530. }
  531. if (is_string($variable)) {
  532. return api_utf8_decode($variable, $_api_encoding);
  533. }
  534. return $variable;
  535. }
  536. /**
  537. * Appendix to "String comparison"
  538. */
  539. /**
  540. * Returns an instance of Collator class (ICU) created for a specified language.
  541. * @param string $language (optional) Language indentificator: 'english', 'french' ... If it is omited, the current interface language is assumed.
  542. * @return object Returns a instance of Collator class that is suitable for common string comparisons.
  543. */
  544. function _api_get_collator($language = null) {
  545. static $collator = array();
  546. if (empty($language)) {
  547. $language = api_get_interface_language();
  548. }
  549. if (!isset($collator[$language])) {
  550. $locale = _api_get_locale_from_language($language);
  551. $collator[$language] = collator_create($locale);
  552. if (is_object($collator[$language])) {
  553. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  554. }
  555. }
  556. return $collator[$language];
  557. }
  558. /**
  559. * Returns an instance of Collator class (ICU) created for a specified language. This collator treats substrings of digits as numbers.
  560. * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed.
  561. * @return object Returns a instance of Collator class that is suitable for alpha-numerical comparisons.
  562. */
  563. function _api_get_alpha_numerical_collator($language = null) {
  564. static $collator = array();
  565. if (empty($language)) {
  566. $language = api_get_interface_language();
  567. }
  568. if (!isset($collator[$language])) {
  569. $locale = _api_get_locale_from_language($language);
  570. $collator[$language] = collator_create($locale);
  571. if (is_object($collator[$language])) {
  572. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  573. collator_set_attribute($collator[$language], Collator::NUMERIC_COLLATION, Collator::ON);
  574. }
  575. }
  576. return $collator[$language];
  577. }
  578. /**
  579. * A string comparison callback function for sorting.
  580. * @param string $string1 The first string.
  581. * @param string $string2 The second string.
  582. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 > $string2; -1 if $string1 < $string2.
  583. */
  584. function _api_cmp($string1, $string2) {
  585. global $_api_collator, $_api_encoding;
  586. $result = collator_compare($_api_collator, api_utf8_encode($string1, $_api_encoding), api_utf8_encode($string2, $_api_encoding));
  587. return $result === false ? 0 : $result;
  588. }
  589. /**
  590. * A reverse string comparison callback function for sorting.
  591. * @param string $string1 The first string.
  592. * @param string $string2 The second string.
  593. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 < $string2; -1 if $string1 > $string2.
  594. */
  595. function _api_rcmp($string1, $string2) {
  596. global $_api_collator, $_api_encoding;
  597. $result = collator_compare($_api_collator, api_utf8_encode($string2, $_api_encoding), api_utf8_encode($string1, $_api_encoding));
  598. return $result === false ? 0 : $result;
  599. }
  600. /**
  601. * A case-insensitive string comparison callback function for sorting.
  602. * @param string $string1 The first string.
  603. * @param string $string2 The second string.
  604. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 > $string2; -1 if $string1 < $string2.
  605. */
  606. function _api_casecmp($string1, $string2) {
  607. global $_api_collator, $_api_encoding;
  608. $result = collator_compare($_api_collator, api_strtolower(api_utf8_encode($string1, $_api_encoding), 'UTF-8'), api_strtolower(api_utf8_encode($string2, $_api_encoding), 'UTF-8'));
  609. return $result === false ? 0 : $result;
  610. }
  611. /**
  612. * A reverse case-insensitive string comparison callback function for sorting.
  613. * @param string $string1 The first string.
  614. * @param string $string2 The second string.
  615. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 < $string2; -1 if $string1 > $string2.
  616. */
  617. function _api_casercmp($string1, $string2) {
  618. global $_api_collator, $_api_encoding;
  619. $result = collator_compare($_api_collator, api_strtolower(api_utf8_encode($string2, $_api_encoding), 'UTF-8'), api_strtolower(api_utf8_encode($string1, $_api_encoding), 'UTF-8'));
  620. return $result === false ? 0 : $result;
  621. }
  622. /**
  623. * A reverse function from php-core function strnatcmp(), performs string comparison in reverse natural (alpha-numerical) order.
  624. * @param string $string1 The first string.
  625. * @param string $string2 The second string.
  626. * @return int Returns 0 if $string1 = $string2; >0 if $string1 < $string2; <0 if $string1 > $string2.
  627. */
  628. function _api_strnatrcmp($string1, $string2) {
  629. return strnatcmp($string2, $string1);
  630. }
  631. /**
  632. * A reverse function from php-core function strnatcasecmp(), performs string comparison in reverse case-insensitive natural (alpha-numerical) order.
  633. * @param string $string1 The first string.
  634. * @param string $string2 The second string.
  635. * @return int Returns 0 if $string1 = $string2; >0 if $string1 < $string2; <0 if $string1 > $string2.
  636. */
  637. function _api_strnatcasercmp($string1, $string2) {
  638. return strnatcasecmp($string2, $string1);
  639. }
  640. /**
  641. * A function that translates sorting flag constants from php core to correspondent constants from intl extension.
  642. * @param int $sort_flag (optional) Sorting modifier flag as it is defined for php core. The default value is SORT_REGULAR.
  643. * @return int Retturns the corresponding sorting modifier flag as it is defined in intl php-extension.
  644. */
  645. function _api_get_collator_sort_flag($sort_flag = SORT_REGULAR) {
  646. switch ($sort_flag) {
  647. case SORT_STRING:
  648. case SORT_LOCALE_STRING:
  649. return Collator::SORT_STRING;
  650. case SORT_NUMERIC:
  651. return Collator::SORT_NUMERIC;
  652. }
  653. return Collator::SORT_REGULAR;
  654. }
  655. /**
  656. * ICU locales (accessible through intl extension).
  657. */
  658. /**
  659. * Returns isocode (see api_get_language_isocode()) which is purified accordingly to
  660. * be used by the php intl extension (ICU library).
  661. * @param string $language (optional) This is the name of the folder containing translations for the corresponding language.
  662. * If $language is omitted, interface language is assumed then.
  663. * @return string The found language locale id or null on error. Examples: bg, en, pt_BR, ...
  664. */
  665. function _api_get_locale_from_language($language = null) {
  666. static $locale = array();
  667. if (empty($language)) {
  668. $language = api_get_interface_language();
  669. }
  670. if (!isset($locale[$language])) {
  671. $locale[$language] = str_replace('-', '_', api_get_language_isocode($language));
  672. }
  673. return $locale[$language];
  674. }
  675. /**
  676. * Sets/gets the default internal value of the locale id (for the intl extension, ICU).
  677. * @param string $locale (optional) The locale id to be set. When it is omitted, the function returns (gets, reads) the default internal value.
  678. * @return mixed When the function sets the default value, it returns TRUE on success or FALSE on error. Otherwise the function returns as string the current default value.
  679. */
  680. function _api_set_default_locale($locale = null) {
  681. static $default_locale = 'en';
  682. if (!empty($locale)) {
  683. $default_locale = $locale;
  684. if (INTL_INSTALLED) {
  685. return @locale_set_default($locale);
  686. }
  687. return true;
  688. } else {
  689. if (INTL_INSTALLED) {
  690. $default_locale = @locale_get_default();
  691. }
  692. }
  693. return $default_locale;
  694. }
  695. /**
  696. * Gets the default internal value of the locale id (for the intl extension, ICU).
  697. * @return string Returns as string the current default value.
  698. */
  699. function api_get_default_locale() {
  700. return _api_set_default_locale();
  701. }
  702. /**
  703. * Appendix to "Encoding management functions"
  704. */
  705. /**
  706. * Returns a table with non-UTF-8 encodings for all system languages.
  707. * @return array Returns an array in the form array('language1' => array('encoding1', encoding2', ...), ...)
  708. * Note: The function api_get_non_utf8_encoding() returns the first encoding from this array that is correspondent to the given language.
  709. */
  710. function & _api_non_utf8_encodings() {
  711. static $encodings;
  712. if (!isset($encodings)) {
  713. $file = dirname(__FILE__).'/internationalization_database/non_utf8_encodings.php';
  714. if (file_exists($file)) {
  715. $encodings = include ($file);
  716. } else {
  717. $encodings = array('english' => array('ISO-8859-15'));
  718. }
  719. }
  720. return $encodings;
  721. }
  722. /**
  723. * Sets/Gets internal character encoding of the common string functions within the PHP mbstring extension.
  724. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding.
  725. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  726. * Note: This function is used in the global initialization script for setting the internal encoding to the platform's character set.
  727. * @link http://php.net/manual/en/function.mb-internal-encoding
  728. */
  729. function _api_mb_internal_encoding($encoding = null)
  730. {
  731. static $mb_internal_encoding = null;
  732. if (empty($encoding)) {
  733. if (is_null($mb_internal_encoding)) {
  734. if (MBSTRING_INSTALLED) {
  735. $mb_internal_encoding = @mb_internal_encoding();
  736. } else {
  737. $mb_internal_encoding = 'UTF-8';
  738. }
  739. }
  740. return $mb_internal_encoding;
  741. }
  742. $mb_internal_encoding = $encoding;
  743. if (_api_mb_supports($encoding)) {
  744. return @mb_internal_encoding($encoding);
  745. }
  746. return false;
  747. }
  748. /**
  749. * Sets/Gets internal character encoding of the regular expression functions (ereg-like) within the PHP mbstring extension.
  750. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding.
  751. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  752. * Note: This function is used in the global initialization script for setting the internal encoding to the platform's character set.
  753. * @link http://php.net/manual/en/function.mb-regex-encoding
  754. */
  755. function _api_mb_regex_encoding($encoding = null) {
  756. static $mb_regex_encoding = null;
  757. if (empty($encoding)) {
  758. if (is_null($mb_regex_encoding)) {
  759. if (MBSTRING_INSTALLED) {
  760. $mb_regex_encoding = @mb_regex_encoding();
  761. } else {
  762. $mb_regex_encoding = 'UTF-8';
  763. }
  764. }
  765. return $mb_regex_encoding;
  766. }
  767. $mb_regex_encoding = $encoding;
  768. if (_api_mb_supports($encoding)) {
  769. return @mb_regex_encoding($encoding);
  770. }
  771. return false;
  772. }
  773. /**
  774. * Checks whether the specified encoding is supported by the PHP mbstring extension.
  775. * @param string $encoding The specified encoding.
  776. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  777. */
  778. function _api_mb_supports($encoding) {
  779. static $supported = array();
  780. if (!isset($supported[$encoding])) {
  781. if (MBSTRING_INSTALLED) {
  782. $supported[$encoding] = api_equal_encodings($encoding, mb_list_encodings(), true);
  783. } else {
  784. $supported[$encoding] = false;
  785. }
  786. }
  787. return $supported[$encoding];
  788. }
  789. /**
  790. * Checks whether the specified encoding is supported by the PHP iconv extension.
  791. * @param string $encoding The specified encoding.
  792. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  793. */
  794. function _api_iconv_supports($encoding) {
  795. static $supported = array();
  796. if (!isset($supported[$encoding])) {
  797. if (ICONV_INSTALLED) {
  798. $enc = api_refine_encoding_id($encoding);
  799. if ($enc != 'HTML-ENTITIES') {
  800. $test_string = '';
  801. for ($i = 32; $i < 128; $i++) {
  802. $test_string .= chr($i);
  803. }
  804. $supported[$encoding] = (@iconv_strlen($test_string, $enc)) ? true : false;
  805. } else {
  806. $supported[$encoding] = false;
  807. }
  808. } else {
  809. $supported[$encoding] = false;
  810. }
  811. }
  812. return $supported[$encoding];
  813. }
  814. // This function checks whether the function _api_convert_encoding() (the php-
  815. // implementation) is able to convert from/to a given encoding.
  816. function _api_convert_encoding_supports($encoding) {
  817. static $supports = array();
  818. if (!isset($supports[$encoding])) {
  819. $supports[$encoding] = _api_get_character_map_name(api_refine_encoding_id($encoding)) != '';
  820. }
  821. return $supports[$encoding];
  822. }
  823. /**
  824. * Checks whether the specified encoding is supported by the html-entitiy related functions.
  825. * @param string $encoding The specified encoding.
  826. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  827. */
  828. function _api_html_entity_supports($encoding) {
  829. static $supports = array();
  830. if (!isset($supports[$encoding])) {
  831. // See http://php.net/manual/en/function.htmlentities.php
  832. $html_entity_encodings = array(
  833. 'ISO-8859-1',
  834. 'ISO-8859-15',
  835. 'UTF-8',
  836. 'CP866',
  837. 'CP1251',
  838. 'CP1252',
  839. 'KOI8-R',
  840. 'BIG5', '950',
  841. 'GB2312', '936',
  842. 'BIG5-HKSCS',
  843. 'Shift_JIS', 'SJIS', '932',
  844. 'EUC-JP', 'EUCJP'
  845. );
  846. $supports[$encoding] = api_equal_encodings($encoding, $html_entity_encodings);
  847. }
  848. return $supports[$encoding];
  849. }