internationalization_internal.lib.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. * Generates statistical, based on n-grams language profile from the given text.
  49. * @param string $string The input text. It should be UTF-8 encoded. Practically it should be at least 3000 characters long, 40000 characters size is for increased accuracy.
  50. * @param int $n_grams_max (optional) The size of the array of the generated n-grams.
  51. * @param int $n_max (optional) The limit if the number of characters that a n-gram may contain.
  52. * @return array An array that contains cunstructed n-grams, sorted in reverse order by their frequences. Frequences are not stored in the array.
  53. */
  54. function &_api_generate_n_grams(&$string, $encoding, $n_grams_max = 350, $n_max = 4) {
  55. if (empty($string)) {
  56. return array();
  57. }
  58. // We construct only lowercase n-grams if it is applicable for the given language.
  59. // Removing all puntuation and some other non-letter characters. Apostrophe characters stay.
  60. // Splitting the sample text into separate words.
  61. $words = preg_split('/_/u', preg_replace('/[\x00-\x1F\x20-\x26\x28-\x3E\?@\x5B-\x60{|}~\x7F]/u', '_', ' '.api_strtolower(api_utf8_encode($string, $encoding), 'UTF-8').' '), -1, PREG_SPLIT_NO_EMPTY);
  62. $prefix = '_'; // Beginning of a word.
  63. $suffix = str_repeat('_', $n_max); // End of a word. Only the last '_' stays.
  64. $n_grams = array(); // The array that will contain the constructed n-grams.
  65. foreach ($words as $word) {
  66. $k = api_strlen($word, 'UTF-8') + 1;
  67. $word = $prefix.$word.$suffix;
  68. for ($n = 1; $n <= $n_max; $n++) {
  69. for ($i = 0; $i < $k; $i++) {
  70. $n_gram = api_utf8_decode(api_substr($word, $i, $n, 'UTF-8'), $encoding);
  71. if (isset($n_grams[$n_gram])) {
  72. $n_grams[$n_gram]++;
  73. } else {
  74. $n_grams[$n_gram] = 1;
  75. }
  76. }
  77. }
  78. }
  79. // Sorting the n-grams in reverse order by their frequences.
  80. arsort($n_grams);
  81. // Reduction the number of n-grams.
  82. return array_keys(array_slice($n_grams, 0, $n_grams_max));
  83. }
  84. /**
  85. *
  86. * The value $max_delta = 80000 is good enough for speed and detection accuracy.
  87. * If you set the value of $max_delta too low, no language will be recognized.
  88. * $max_delta = 400 * 350 = 140000 is the best detection with lowest speed.
  89. */
  90. function & _api_compare_n_grams(&$n_grams, $encoding, $max_delta = LANGUAGE_DETECT_MAX_DELTA) {
  91. static $language_profiles;
  92. if (!isset($language_profiles)) {
  93. // Reading the language profile files from the internationalization database.
  94. $exceptions = array('.', '..', 'CVS', '.htaccess', '.svn', '_svn', 'index.html');
  95. $path = str_replace("\\", '/', dirname(__FILE__).'/internationalization_database/language_detection/language_profiles/');
  96. $non_utf8_encodings = & _api_non_utf8_encodings();
  97. if (is_dir($path)) {
  98. if ($handle = @opendir($path)) {
  99. while (($dir_entry = @readdir($handle)) !== false) {
  100. if (api_in_array_nocase($dir_entry, $exceptions)) continue;
  101. if (strpos($dir_entry, '.txt') === false) continue;
  102. $dir_entry_full_path = $path .'/'. $dir_entry;
  103. if (@filetype($dir_entry_full_path) != 'dir') {
  104. if (false !== $data = @file_get_contents($dir_entry_full_path)) {
  105. $language = basename($dir_entry_full_path, '.txt');
  106. $encodings = array('UTF-8');
  107. if (!empty($non_utf8_encodings[$language])) {
  108. $encodings = array_merge($encodings, $non_utf8_encodings[$language]);
  109. }
  110. foreach ($encodings as $enc) {
  111. $data_enc = api_utf8_decode($data, $enc);
  112. if (empty($data_enc)) {
  113. continue;
  114. }
  115. $key = $language.':'.$enc;
  116. $language_profiles[$key]['data'] = array_flip(explode("\n", $data_enc));
  117. $language_profiles[$key]['language'] = $language;
  118. $language_profiles[$key]['encoding'] = $enc;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. @closedir($handle);
  126. ksort($language_profiles);
  127. }
  128. if (!is_array($n_grams) || empty($n_grams)) {
  129. return array();
  130. }
  131. // Comparison between the input n-grams and the lanuage profiles.
  132. foreach ($language_profiles as $key => &$language_profile) {
  133. if (!api_is_language_supported($language_profile['language']) || !api_equal_encodings($encoding, $language_profile['encoding'])) {
  134. continue;
  135. }
  136. $delta = 0; // This is a summary measurment for matching between the input text and the current language profile.
  137. // Searching each n-gram from the input text into the language profile.
  138. foreach ($n_grams as $rank => &$n_gram) {
  139. if (isset($language_profile['data'][$n_gram])) {
  140. // The n-gram has been found, the difference between places in both
  141. // arrays is calculated (so called delta-points are adopted for
  142. // measuring distances between n-gram ranks.
  143. $delta += abs($rank - $language_profile['data'][$n_gram]);
  144. } else {
  145. // The n-gram has not been found in the profile. We add then
  146. // a large enough "distance" in delta-points.
  147. $delta += 400;
  148. }
  149. // Abort: This language already differs too much.
  150. if ($delta > $max_delta) {
  151. break;
  152. }
  153. }
  154. // Include only non-aborted languages in result array.
  155. if ($delta < ($max_delta - 400)) {
  156. $result[$key] = $delta;
  157. }
  158. }
  159. if (!isset($result)) {
  160. return array();
  161. }
  162. asort($result);
  163. return $result;
  164. }
  165. /**
  166. * Appendix to "Date and time formats"
  167. */
  168. /**
  169. * Returns an array of translated week days and months, short and normal names.
  170. * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed.
  171. * @return array Returns a multidimensional array with translated week days and months.
  172. */
  173. function &_api_get_day_month_names($language = null) {
  174. static $date_parts = array();
  175. if (empty($language)) {
  176. $language = api_get_interface_language();
  177. }
  178. if (!isset($date_parts[$language])) {
  179. $week_day = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  180. $month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
  181. for ($i = 0; $i < 7; $i++) {
  182. $date_parts[$language]['days_short'][] = get_lang($week_day[$i].'Short', '', $language);
  183. $date_parts[$language]['days_long'][] = get_lang($week_day[$i].'Long', '', $language);
  184. }
  185. for ($i = 0; $i < 12; $i++) {
  186. $date_parts[$language]['months_short'][] = get_lang($month[$i].'Short', '', $language);
  187. $date_parts[$language]['months_long'][] = get_lang($month[$i].'Long', '', $language);
  188. }
  189. }
  190. return $date_parts[$language];
  191. }
  192. /**
  193. * Appendix to "Name order conventions"
  194. */
  195. /**
  196. * Returns returns person name convention for a given language.
  197. * @param string $language The input language.
  198. * @param string $type The type of the requested convention. It may be 'format' for name order convention or 'sort_by' for name sorting convention.
  199. * @return mixed Depending of the requested type, the returned result may be string or boolean; null is returned on error;
  200. */
  201. function _api_get_person_name_convention($language, $type) {
  202. static $conventions;
  203. $language = api_purify_language_id($language);
  204. if (!isset($conventions)) {
  205. $file = dirname(__FILE__).'/internationalization_database/name_order_conventions.php';
  206. if (file_exists($file)) {
  207. $conventions = include ($file);
  208. } else {
  209. $conventions = array('english' => array('format' => 'title first_name last_name', 'sort_by' => 'first_name'));
  210. }
  211. $search1 = array('FIRST_NAME', 'LAST_NAME', 'TITLE');
  212. $replacement1 = array('%F', '%L', '%T');
  213. $search2 = array('first_name', 'last_name', 'title');
  214. $replacement2 = array('%f', '%l', '%t');
  215. foreach (array_keys($conventions) as $key) {
  216. $conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
  217. $conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search2, $replacement2, $conventions[$key]['format']))));
  218. $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
  219. }
  220. }
  221. switch ($type) {
  222. case 'format':
  223. return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l';
  224. case 'sort_by':
  225. return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true;
  226. }
  227. return null;
  228. }
  229. /**
  230. * Replaces non-valid formats for person names with the default (English) format.
  231. * @param string $format The input format to be verified.
  232. * @return bool Returns the same format if is is valid, otherwise returns a valid English format.
  233. */
  234. function _api_validate_person_name_format($format) {
  235. if (empty($format) || stripos($format, '%f') === false || stripos($format, '%l') === false) {
  236. return '%t %f %l';
  237. }
  238. return $format;
  239. }
  240. /**
  241. * Removes leading, trailing and duplicate whitespace and/or commas in a full person name.
  242. * 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.
  243. * @param string $person_name The input person name.
  244. * @return string Returns cleaned person name.
  245. */
  246. function _api_clean_person_name($person_name) {
  247. return preg_replace(array('/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'), array(' ', ', ', ',', '', ''), $person_name);
  248. }
  249. /**
  250. * Appendix to "Multibyte string conversion functions"
  251. */
  252. /**
  253. * This is a php-implementation of a function that is similar to mb_convert_encoding() from mbstring extension.
  254. * The function converts a given string from one to another character encoding.
  255. * @param string $string The string being converted.
  256. * @param string $to_encoding The encoding that $string is being converted to.
  257. * @param string $from_encoding The encoding that $string is being converted from.
  258. * @return string Returns the converted string.
  259. */
  260. function _api_convert_encoding(&$string, $to_encoding, $from_encoding) {
  261. $str = (string)$string;
  262. static $character_map = array();
  263. static $utf8_compatible = array('UTF-8', 'US-ASCII');
  264. if (empty($str)) {
  265. return $str;
  266. }
  267. $to_encoding = api_refine_encoding_id($to_encoding);
  268. $from_encoding = api_refine_encoding_id($from_encoding);
  269. if (api_equal_encodings($to_encoding, $from_encoding)) {
  270. return $str;
  271. }
  272. if ($to_encoding == 'HTML-ENTITIES') {
  273. return api_htmlentities($str, ENT_QUOTES, $from_encoding);
  274. }
  275. if ($from_encoding == 'HTML-ENTITIES') {
  276. return api_html_entity_decode($str, ENT_QUOTES, $to_encoding);
  277. }
  278. $to = _api_get_character_map_name($to_encoding);
  279. $from = _api_get_character_map_name($from_encoding);
  280. if (empty($to) || empty($from) || $to == $from || (in_array($to, $utf8_compatible) && in_array($from, $utf8_compatible))) {
  281. return $str;
  282. }
  283. if (!isset($character_map[$to])) {
  284. $character_map[$to] = &_api_parse_character_map($to);
  285. }
  286. if ($character_map[$to] === false) {
  287. return $str;
  288. }
  289. if (!isset($character_map[$from])) {
  290. $character_map[$from] = &_api_parse_character_map($from);
  291. }
  292. if ($character_map[$from] === false) {
  293. return $str;
  294. }
  295. if ($from != 'UTF-8') {
  296. $len = api_byte_count($str);
  297. $codepoints = array();
  298. for ($i = 0; $i < $len; $i++) {
  299. $ord = ord($str[$i]);
  300. if ($ord > 127) {
  301. if (isset($character_map[$from]['local'][$ord])) {
  302. $codepoints[] = $character_map[$from]['local'][$ord];
  303. } else {
  304. $codepoints[] = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  305. }
  306. } else {
  307. $codepoints[] = $ord;
  308. }
  309. }
  310. } else {
  311. $codepoints = _api_utf8_to_unicode($str);
  312. }
  313. if ($to != 'UTF-8') {
  314. foreach ($codepoints as $i => &$codepoint) {
  315. if ($codepoint > 127) {
  316. if (isset($character_map[$to]['unicode'][$codepoint])) {
  317. $codepoint = chr($character_map[$to]['unicode'][$codepoint]);
  318. } else {
  319. $codepoint = '?'; // Unknown character.
  320. }
  321. } else {
  322. $codepoint = chr($codepoint);
  323. }
  324. }
  325. $str = implode($codepoints);
  326. } else {
  327. $str = _api_utf8_from_unicode($codepoints);
  328. }
  329. return $str;
  330. }
  331. /**
  332. * This function determines the name of corresponding to a given encoding conversion table.
  333. * It is able to deal with some aliases of the encoding.
  334. * @param string $encoding The given encoding identificator, for example 'WINDOWS-1252'.
  335. * @return string Returns the name of the corresponding conversion table, for the same example - 'CP1252'.
  336. */
  337. function _api_get_character_map_name($encoding) {
  338. static $character_map_selector;
  339. if (!isset($character_map_selector)) {
  340. $file = dirname(__FILE__).'/internationalization_database/conversion/character_map_selector.php';
  341. if (file_exists($file)) {
  342. $character_map_selector = include ($file);
  343. } else {
  344. $character_map_selector = array();
  345. }
  346. }
  347. return isset($character_map_selector[$encoding]) ? $character_map_selector[$encoding] : '';
  348. }
  349. /**
  350. * This function parses a given conversion table (a text file) and creates in the memory
  351. * two tables for conversion - character set from/to Unicode codepoints.
  352. * @param string $name The name of the thext file that contains the conversion table, for example 'CP1252' (file CP1252.TXT will be parsed).
  353. * @return array Returns an array that contains forward and reverse tables (from/to Unicode).
  354. */
  355. function &_api_parse_character_map($name) {
  356. $result = array();
  357. $file = dirname(__FILE__).'/internationalization_database/conversion/' . $name . '.TXT';
  358. if (file_exists($file)) {
  359. $text = @file_get_contents($file);
  360. if ($text !== false) {
  361. $text = explode(chr(10), $text);
  362. foreach ($text as $line) {
  363. if (empty($line)) {
  364. continue;
  365. }
  366. if (!empty($line) && trim($line) && $line[0] != '#') {
  367. $matches = array();
  368. preg_match('/[[:space:]]*0x([[:alnum:]]*)[[:space:]]+0x([[:alnum:]]*)[[:space:]]+/', $line, $matches);
  369. $ord = hexdec(trim($matches[1]));
  370. if ($ord > 127) {
  371. $codepoint = hexdec(trim($matches[2]));
  372. $result['local'][$ord] = $codepoint;
  373. $result['unicode'][$codepoint] = $ord;
  374. }
  375. }
  376. }
  377. } else {
  378. return false ;
  379. }
  380. } else {
  381. return false;
  382. }
  383. return $result;
  384. }
  385. /**
  386. * Takes an UTF-8 string and returns an array of integer values representing the Unicode characters.
  387. * Astral planes are supported ie. the ints in the output can be > 0xFFFF. Occurrances of the BOM are ignored.
  388. * Surrogates are not allowed.
  389. * @param string $string The UTF-8 encoded string.
  390. * @return array Returns an array of unicode code points.
  391. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  392. * @link http://hsivonen.iki.fi/php-utf8/
  393. * @author Ivan Tcholakov, August 2009, adaptation for the Dokeos LMS.
  394. */
  395. function _api_utf8_to_unicode(&$string) {
  396. $str = (string)$string;
  397. $state = 0; // cached expected number of octets after the current octet
  398. // until the beginning of the next UTF8 character sequence
  399. $codepoint = 0; // cached Unicode character
  400. $bytes = 1; // cached expected number of octets in the current sequence
  401. $result = array();
  402. $len = api_byte_count($str);
  403. for ($i = 0; $i < $len; $i++) {
  404. $byte = ord($str[$i]);
  405. if ($state == 0) {
  406. // When state is zero we expect either a US-ASCII character or a multi-octet sequence.
  407. if (0 == (0x80 & ($byte))) {
  408. // US-ASCII, pass straight through.
  409. $result[] = $byte;
  410. $bytes = 1;
  411. } else if (0xC0 == (0xE0 & ($byte))) {
  412. // First octet of 2 octet sequence
  413. $codepoint = ($byte);
  414. $codepoint = ($codepoint & 0x1F) << 6;
  415. $state = 1;
  416. $bytes = 2;
  417. } else if (0xE0 == (0xF0 & ($byte))) {
  418. // First octet of 3 octet sequence
  419. $codepoint = ($byte);
  420. $codepoint = ($codepoint & 0x0F) << 12;
  421. $state = 2;
  422. $bytes = 3;
  423. } else if (0xF0 == (0xF8 & ($byte))) {
  424. // First octet of 4 octet sequence
  425. $codepoint = ($byte);
  426. $codepoint = ($codepoint & 0x07) << 18;
  427. $state = 3;
  428. $bytes = 4;
  429. } else if (0xF8 == (0xFC & ($byte))) {
  430. // First octet of 5 octet sequence.
  431. // This is illegal because the encoded codepoint must be either
  432. // (a) not the shortest form or
  433. // (b) outside the Unicode range of 0-0x10FFFF.
  434. // Rather than trying to resynchronize, we will carry on until the end
  435. // of the sequence and let the later error handling code catch it.
  436. $codepoint = ($byte);
  437. $codepoint = ($codepoint & 0x03) << 24;
  438. $state = 4;
  439. $bytes = 5;
  440. } else if (0xFC == (0xFE & ($byte))) {
  441. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  442. $codepoint = ($byte);
  443. $codepoint = ($codepoint & 1) << 30;
  444. $state = 5;
  445. $bytes = 6;
  446. } else {
  447. // Current octet is neither in the US-ASCII range nor a legal first octet of a multi-octet sequence.
  448. $state = 0;
  449. $codepoint = 0;
  450. $bytes = 1;
  451. $result[] = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  452. continue ;
  453. }
  454. } else {
  455. // When state is non-zero, we expect a continuation of the multi-octet sequence
  456. if (0x80 == (0xC0 & ($byte))) {
  457. // Legal continuation.
  458. $shift = ($state - 1) * 6;
  459. $tmp = $byte;
  460. $tmp = ($tmp & 0x0000003F) << $shift;
  461. $codepoint |= $tmp;
  462. // End of the multi-octet sequence. $codepoint now contains the final Unicode codepoint to be output
  463. if (0 == --$state) {
  464. // Check for illegal sequences and codepoints.
  465. // From Unicode 3.1, non-shortest form is illegal
  466. if (((2 == $bytes) && ($codepoint < 0x0080)) ||
  467. ((3 == $bytes) && ($codepoint < 0x0800)) ||
  468. ((4 == $bytes) && ($codepoint < 0x10000)) ||
  469. (4 < $bytes) ||
  470. // From Unicode 3.2, surrogate characters are illegal
  471. (($codepoint & 0xFFFFF800) == 0xD800) ||
  472. // Codepoints outside the Unicode range are illegal
  473. ($codepoint > 0x10FFFF)) {
  474. $state = 0;
  475. $codepoint = 0;
  476. $bytes = 1;
  477. $result[] = 0xFFFD;
  478. continue ;
  479. }
  480. if (0xFEFF != $codepoint) {
  481. // BOM is legal but we don't want to output it
  482. $result[] = $codepoint;
  483. }
  484. // Initialize UTF8 cache
  485. $state = 0;
  486. $codepoint = 0;
  487. $bytes = 1;
  488. }
  489. } else {
  490. // ((0xC0 & (*in) != 0x80) && (state != 0))
  491. // Incomplete multi-octet sequence.
  492. $state = 0;
  493. $codepoint = 0;
  494. $bytes = 1;
  495. $result[] = 0xFFFD;
  496. }
  497. }
  498. }
  499. return $result;
  500. }
  501. /**
  502. * Takes an array of Unicode codepoints and returns a UTF-8 string.
  503. * @param array $codepoints An array of Unicode codepoints representing a string.
  504. * @return string Returns a UTF-8 string constructed using the given codepoints.
  505. */
  506. function _api_utf8_from_unicode($codepoints) {
  507. return implode(array_map('_api_utf8_chr', $codepoints));
  508. }
  509. /**
  510. * Takes a codepoint and returns its correspondent UTF-8 encoded character.
  511. * Astral planes are supported, ie the intger input can be > 0xFFFF. Occurrances of the BOM are ignored.
  512. * Surrogates are not allowed.
  513. * @param int $codepoint The Unicode codepoint.
  514. * @return string Returns the corresponding UTF-8 character.
  515. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  516. * @link http://hsivonen.iki.fi/php-utf8/
  517. * @author Ivan Tcholakov, 2009, modifications for the Dokeos LMS.
  518. * @see _api_utf8_from_unicode()
  519. * This is a UTF-8 aware version of the function chr().
  520. * @link http://php.net/manual/en/function.chr.php
  521. */
  522. function _api_utf8_chr($codepoint) {
  523. // ASCII range (including control chars)
  524. if ( ($codepoint >= 0) && ($codepoint <= 0x007f) ) {
  525. $result = chr($codepoint);
  526. // 2 byte sequence
  527. } else if ($codepoint <= 0x07ff) {
  528. $result = chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x003f));
  529. // Byte order mark (skip)
  530. } else if($codepoint == 0xFEFF) {
  531. // nop -- zap the BOM
  532. $result = '';
  533. // Test for illegal surrogates
  534. } else if ($codepoint >= 0xD800 && $codepoint <= 0xDFFF) {
  535. // found a surrogate
  536. $result = _api_utf8_chr(0xFFFD); // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  537. // 3 byte sequence
  538. } else if ($codepoint <= 0xffff) {
  539. $result = chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x003f)) . chr(0x80 | ($codepoint & 0x003f));
  540. // 4 byte sequence
  541. } else if ($codepoint <= 0x10ffff) {
  542. $result = chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  543. } else {
  544. // out of range
  545. $result = _api_utf8_chr(0xFFFD);
  546. }
  547. return $result;
  548. }
  549. /**
  550. * Takes the first UTF-8 character in a string and returns its Unicode codepoint.
  551. * @param string $utf8_character The UTF-8 encoded character.
  552. * @return int Returns: the codepoint; or 0xFFFD (unknown character) when the input string is empty.
  553. * This is a UTF-8 aware version of the function ord().
  554. * @link http://php.net/manual/en/function.ord.php
  555. * Note about a difference with the original funtion ord(): ord('') returns 0.
  556. */
  557. function _api_utf8_ord($utf8_character) {
  558. if ($utf8_character == '') {
  559. return 0xFFFD;
  560. }
  561. $codepoints = _api_utf8_to_unicode($utf8_character);
  562. return $codepoints[0];
  563. }
  564. /**
  565. * Makes a html-entity from Unicode codepoint.
  566. * @param int $codepoint The Unicode codepoint.
  567. * @return string Returns the corresponding html-entity; or ASCII character if $codepoint < 128.
  568. */
  569. function _api_html_entity_from_unicode($codepoint) {
  570. if ($codepoint < 128) {
  571. return chr($codepoint);
  572. }
  573. return '&#'.$codepoint.';';
  574. }
  575. /**
  576. * Appendix to "Common multibyte string functions"
  577. */
  578. /**
  579. * The following function reads case folding properties about a given character from a file-based "database".
  580. * @param int $codepoint The Unicode codepoint that represents a caharacter.
  581. * @param string $type (optional) The type of initial case to be altered: 'lower' (default) or 'upper'.
  582. * @return array Returns an array with properties used to change case of the character.
  583. */
  584. function &_api_utf8_get_letter_case_properties($codepoint, $type = 'lower') {
  585. static $config = array();
  586. static $range = array();
  587. if (!isset($range[$codepoint])) {
  588. if ($codepoint > 128 && $codepoint < 256) {
  589. $range[$codepoint] = '0080_00ff'; // Latin-1 Supplement
  590. } elseif ($codepoint < 384) {
  591. $range[$codepoint] = '0100_017f'; // Latin Extended-A
  592. } elseif ($codepoint < 592) {
  593. $range[$codepoint] = '0180_024F'; // Latin Extended-B
  594. } elseif ($codepoint < 688) {
  595. $range[$codepoint] = '0250_02af'; // IPA Extensions
  596. } elseif ($codepoint >= 880 && $codepoint < 1024) {
  597. $range[$codepoint] = '0370_03ff'; // Greek and Coptic
  598. } elseif ($codepoint < 1280) {
  599. $range[$codepoint] = '0400_04ff'; // Cyrillic
  600. } elseif ($codepoint < 1328) {
  601. $range[$codepoint] = '0500_052f'; // Cyrillic Supplement
  602. } elseif ($codepoint < 1424) {
  603. $range[$codepoint] = '0530_058f'; // Armenian
  604. } elseif ($codepoint >= 7680 && $codepoint < 7936) {
  605. $range[$codepoint] = '1e00_1eff'; // Latin Extended Additional
  606. } elseif ($codepoint < 8192) {
  607. $range[$codepoint] = '1f00_1fff'; // Greek Extended
  608. } elseif ($codepoint >= 8448 && $codepoint < 8528) {
  609. $range[$codepoint] = '2100_214f'; // Letterlike Symbols
  610. } elseif ($codepoint < 8592) {
  611. $range[$codepoint] = '2150_218f'; // Number Forms
  612. } elseif ($codepoint >= 9312 && $codepoint < 9472) {
  613. $range[$codepoint] = '2460_24ff'; // Enclosed Alphanumerics
  614. } elseif ($codepoint >= 11264 && $codepoint < 11360) {
  615. $range[$codepoint] = '2c00_2c5f'; // Glagolitic
  616. } elseif ($codepoint < 11392) {
  617. $range[$codepoint] = '2c60_2c7f'; // Latin Extended-C
  618. } elseif ($codepoint < 11520) {
  619. $range[$codepoint] = '2c80_2cff'; // Coptic
  620. } elseif ($codepoint >= 65280 && $codepoint < 65520) {
  621. $range[$codepoint] = 'ff00_ffef'; // Halfwidth and Fullwidth Forms
  622. } else {
  623. $range[$codepoint] = false;
  624. }
  625. if ($range[$codepoint] === false) {
  626. return null;
  627. }
  628. if (!isset($config[$range[$codepoint]])) {
  629. $file = dirname(__FILE__).'/internationalization_database/casefolding/' . $range[$codepoint] . '.php';
  630. if (file_exists($file)) {
  631. include $file;
  632. }
  633. }
  634. }
  635. if ($range[$codepoint] === false || !isset($config[$range[$codepoint]])) {
  636. return null;
  637. }
  638. $result = array();
  639. $count = count($config[$range[$codepoint]]);
  640. for ($i = 0; $i < $count; $i++) {
  641. if ($type === 'lower' && $config[$range[$codepoint]][$i][$type][0] === $codepoint) {
  642. $result[] = $config[$range[$codepoint]][$i];
  643. } elseif ($type === 'upper' && $config[$range[$codepoint]][$i][$type] === $codepoint) {
  644. $result[] = $config[$range[$codepoint]][$i];
  645. }
  646. }
  647. return $result;
  648. }
  649. /**
  650. * A callback for serving the function api_ucwords().
  651. * @param array $matches Input array of matches corresponding to a single word
  652. * @return string Returns a with first char of the word in uppercase
  653. */
  654. function _api_utf8_ucwords_callback($matches) {
  655. return $matches[2] . api_ucfirst(ltrim($matches[0]), 'UTF-8');
  656. }
  657. /**
  658. * Appendix to "Common sting operations with arrays"
  659. */
  660. /**
  661. * This callback function converts from UTF-8 to other encoding. It works with strings or arrays of strings.
  662. * @param mixed $variable The variable to be converted, a string or an array.
  663. * @return mixed Returns the converted form UTF-8 $variable with the same type, string or array.
  664. */
  665. function _api_array_utf8_decode($variable) {
  666. global $_api_encoding;
  667. if (is_array($variable)) {
  668. return array_map('_api_array_utf8_decode', $variable);
  669. }
  670. if (is_string($variable)) {
  671. return api_utf8_decode($variable, $_api_encoding);
  672. }
  673. return $variable;
  674. }
  675. /**
  676. * Appendix to "String comparison"
  677. */
  678. /**
  679. * Returns an instance of Collator class (ICU) created for a specified language.
  680. * @param string $language (optional) Language indentificator: 'english', 'french' ... If it is omited, the current interface language is assumed.
  681. * @return object Returns a instance of Collator class that is suitable for common string comparisons.
  682. */
  683. function _api_get_collator($language = null) {
  684. static $collator = array();
  685. if (empty($language)) {
  686. $language = api_get_interface_language();
  687. }
  688. if (!isset($collator[$language])) {
  689. $locale = _api_get_locale_from_language($language);
  690. $collator[$language] = collator_create($locale);
  691. if (is_object($collator[$language])) {
  692. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  693. }
  694. }
  695. return $collator[$language];
  696. }
  697. /**
  698. * Returns an instance of Collator class (ICU) created for a specified language. This collator treats substrings of digits as numbers.
  699. * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed.
  700. * @return object Returns a instance of Collator class that is suitable for alpha-numerical comparisons.
  701. */
  702. function _api_get_alpha_numerical_collator($language = null) {
  703. static $collator = array();
  704. if (empty($language)) {
  705. $language = api_get_interface_language();
  706. }
  707. if (!isset($collator[$language])) {
  708. $locale = _api_get_locale_from_language($language);
  709. $collator[$language] = collator_create($locale);
  710. if (is_object($collator[$language])) {
  711. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  712. collator_set_attribute($collator[$language], Collator::NUMERIC_COLLATION, Collator::ON);
  713. }
  714. }
  715. return $collator[$language];
  716. }
  717. /**
  718. * A string comparison callback function for sorting.
  719. * @param string $string1 The first string.
  720. * @param string $string2 The second string.
  721. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 > $string2; -1 if $string1 < $string2.
  722. */
  723. function _api_cmp($string1, $string2) {
  724. global $_api_collator, $_api_encoding;
  725. $result = collator_compare($_api_collator, api_utf8_encode($string1, $_api_encoding), api_utf8_encode($string2, $_api_encoding));
  726. return $result === false ? 0 : $result;
  727. }
  728. /**
  729. * A reverse string comparison callback function for sorting.
  730. * @param string $string1 The first string.
  731. * @param string $string2 The second string.
  732. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 < $string2; -1 if $string1 > $string2.
  733. */
  734. function _api_rcmp($string1, $string2) {
  735. global $_api_collator, $_api_encoding;
  736. $result = collator_compare($_api_collator, api_utf8_encode($string2, $_api_encoding), api_utf8_encode($string1, $_api_encoding));
  737. return $result === false ? 0 : $result;
  738. }
  739. /**
  740. * A case-insensitive string comparison callback function for sorting.
  741. * @param string $string1 The first string.
  742. * @param string $string2 The second string.
  743. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 > $string2; -1 if $string1 < $string2.
  744. */
  745. function _api_casecmp($string1, $string2) {
  746. global $_api_collator, $_api_encoding;
  747. $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'));
  748. return $result === false ? 0 : $result;
  749. }
  750. /**
  751. * A reverse case-insensitive string comparison callback function for sorting.
  752. * @param string $string1 The first string.
  753. * @param string $string2 The second string.
  754. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 < $string2; -1 if $string1 > $string2.
  755. */
  756. function _api_casercmp($string1, $string2) {
  757. global $_api_collator, $_api_encoding;
  758. $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'));
  759. return $result === false ? 0 : $result;
  760. }
  761. /**
  762. * A reverse function from php-core function strnatcmp(), performs string comparison in reverse natural (alpha-numerical) order.
  763. * @param string $string1 The first string.
  764. * @param string $string2 The second string.
  765. * @return int Returns 0 if $string1 = $string2; >0 if $string1 < $string2; <0 if $string1 > $string2.
  766. */
  767. function _api_strnatrcmp($string1, $string2) {
  768. return strnatcmp($string2, $string1);
  769. }
  770. /**
  771. * A reverse function from php-core function strnatcasecmp(), performs string comparison in reverse case-insensitive natural (alpha-numerical) order.
  772. * @param string $string1 The first string.
  773. * @param string $string2 The second string.
  774. * @return int Returns 0 if $string1 = $string2; >0 if $string1 < $string2; <0 if $string1 > $string2.
  775. */
  776. function _api_strnatcasercmp($string1, $string2) {
  777. return strnatcasecmp($string2, $string1);
  778. }
  779. /**
  780. * A fuction that translates sorting flag constants from php core to correspondent constants from intl extension.
  781. * @param int $sort_flag (optional) Sorting modifier flag as it is defined for php core. The default value is SORT_REGULAR.
  782. * @return int Retturns the corresponding sorting modifier flag as it is defined in intl php-extension.
  783. */
  784. function _api_get_collator_sort_flag($sort_flag = SORT_REGULAR) {
  785. switch ($sort_flag) {
  786. case SORT_STRING:
  787. case SORT_SORT_LOCALE_STRING:
  788. return Collator::SORT_STRING;
  789. case SORT_NUMERIC:
  790. return Collator::SORT_NUMERIC;
  791. }
  792. return Collator::SORT_REGULAR;
  793. }
  794. /**
  795. * ICU locales (accessible through intl extension).
  796. */
  797. /**
  798. * Returns isocode (see api_get_language_isocode()) which is purified accordingly to
  799. * be used by the php intl extension (ICU library).
  800. * @param string $language (optional) This is the name of the folder containing translations for the corresponding language.
  801. * If $language is omitted, interface language is assumed then.
  802. * @return string The found language locale id or null on error. Examples: bg, en, pt_BR, ...
  803. */
  804. function _api_get_locale_from_language($language = null) {
  805. static $locale = array();
  806. if (empty($language)) {
  807. $language = api_get_interface_language();
  808. }
  809. if (!isset($locale[$language])) {
  810. $locale[$language] = str_replace('-', '_', api_get_language_isocode($language));
  811. }
  812. return $locale[$language];
  813. }
  814. /**
  815. * Sets/gets the default internal value of the locale id (for the intl extension, ICU).
  816. * @param string $locale (optional) The locale id to be set. When it is omitted, the function returns (gets, reads) the default internal value.
  817. * @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.
  818. */
  819. function _api_set_default_locale($locale = null) {
  820. static $default_locale = 'en';
  821. if (!empty($locale)) {
  822. $default_locale = $locale;
  823. if (INTL_INSTALLED) {
  824. return @locale_set_default($locale);
  825. }
  826. return true;
  827. } else {
  828. if (INTL_INSTALLED) {
  829. $default_locale = @locale_get_default();
  830. }
  831. }
  832. return $default_locale;
  833. }
  834. /**
  835. * Gets the default internal value of the locale id (for the intl extension, ICU).
  836. * @return string Returns as string the current default value.
  837. */
  838. function api_get_default_locale() {
  839. return _api_set_default_locale();
  840. }
  841. /**
  842. * Appendix to "Encoding management functions"
  843. */
  844. /**
  845. * Returns a table with non-UTF-8 encodings for all system languages.
  846. * @return array Returns an array in the form array('language1' => array('encoding1', encoding2', ...), ...)
  847. * Note: The function api_get_non_utf8_encoding() returns the first encoding from this array that is correspondent to the given language.
  848. */
  849. function & _api_non_utf8_encodings() {
  850. static $encodings;
  851. if (!isset($encodings)) {
  852. $file = dirname(__FILE__).'/internationalization_database/non_utf8_encodings.php';
  853. if (file_exists($file)) {
  854. $encodings = include ($file);
  855. } else {
  856. $encodings = array('english' => array('ISO-8859-15'));
  857. }
  858. }
  859. return $encodings;
  860. }
  861. /**
  862. * Sets/Gets internal character encoding of the common string functions within the PHP mbstring extension.
  863. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding.
  864. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  865. * Note: This function is used in the global initialization script for setting the internal encoding to the platform's character set.
  866. * @link http://php.net/manual/en/function.mb-internal-encoding
  867. */
  868. function _api_mb_internal_encoding($encoding = null) {
  869. static $mb_internal_encoding = null;
  870. if (empty($encoding)) {
  871. if (is_null($mb_internal_encoding)) {
  872. if (MBSTRING_INSTALLED) {
  873. $mb_internal_encoding = @mb_internal_encoding();
  874. } else {
  875. $mb_internal_encoding = 'UTF-8';
  876. }
  877. }
  878. return $mb_internal_encoding;
  879. }
  880. $mb_internal_encoding = $encoding;
  881. if (_api_mb_supports($encoding)) {
  882. return @mb_internal_encoding($encoding);
  883. }
  884. return false;
  885. }
  886. /**
  887. * Sets/Gets internal character encoding of the regular expression functions (ereg-like) within the PHP mbstring extension.
  888. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding.
  889. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  890. * Note: This function is used in the global initialization script for setting the internal encoding to the platform's character set.
  891. * @link http://php.net/manual/en/function.mb-regex-encoding
  892. */
  893. function _api_mb_regex_encoding($encoding = null) {
  894. static $mb_regex_encoding = null;
  895. if (empty($encoding)) {
  896. if (is_null($mb_regex_encoding)) {
  897. if (MBSTRING_INSTALLED) {
  898. $mb_regex_encoding = @mb_regex_encoding();
  899. } else {
  900. $mb_regex_encoding = 'UTF-8';
  901. }
  902. }
  903. return $mb_regex_encoding;
  904. }
  905. $mb_regex_encoding = $encoding;
  906. if (_api_mb_supports($encoding)) {
  907. return @mb_regex_encoding($encoding);
  908. }
  909. return false;
  910. }
  911. /**
  912. * Retrieves specified internal encoding configuration variable within the PHP iconv extension.
  913. * @param string $type The parameter $type could be: 'iconv_internal_encoding', 'iconv_input_encoding', or 'iconv_output_encoding'.
  914. * @return mixed The function returns the requested encoding or FALSE on error.
  915. * @link http://php.net/manual/en/function.iconv-get-encoding
  916. */
  917. function _api_iconv_get_encoding($type) {
  918. return _api_iconv_set_encoding($type);
  919. }
  920. /**
  921. * Sets specified internal encoding configuration variables within the PHP iconv extension.
  922. * @param string $type The parameter $type could be: 'iconv_internal_encoding', 'iconv_input_encoding', or 'iconv_output_encoding'.
  923. * @param string $encoding (optional) The desired encoding to be set.
  924. * @return bool Returns TRUE on success, FALSE on error.
  925. * Note: This function is used in the global initialization script for setting these three internal encodings to the platform's character set.
  926. * @link http://php.net/manual/en/function.iconv-set-encoding
  927. */
  928. function _api_iconv_set_encoding($type, $encoding = null) {
  929. static $iconv_internal_encoding = null;
  930. static $iconv_input_encoding = null;
  931. static $iconv_output_encoding = null;
  932. if (!ICONV_INSTALLED) {
  933. return false;
  934. }
  935. switch ($type) {
  936. case 'iconv_internal_encoding':
  937. if (empty($encoding)) {
  938. if (is_null($iconv_internal_encoding)) {
  939. $iconv_internal_encoding = @iconv_get_encoding($type);
  940. }
  941. return $iconv_internal_encoding;
  942. }
  943. if (_api_iconv_supports($encoding)) {
  944. if(@iconv_set_encoding($type, $encoding)) {
  945. $iconv_internal_encoding = $encoding;
  946. return true;
  947. }
  948. return false;
  949. }
  950. return false;
  951. case 'iconv_input_encoding':
  952. if (empty($encoding)) {
  953. if (is_null($iconv_input_encoding)) {
  954. $iconv_input_encoding = @iconv_get_encoding($type);
  955. }
  956. return $iconv_input_encoding;
  957. }
  958. if (_api_iconv_supports($encoding)) {
  959. if(@iconv_set_encoding($type, $encoding)) {
  960. $iconv_input_encoding = $encoding;
  961. return true;
  962. }
  963. return false;
  964. }
  965. return false;
  966. case 'iconv_output_encoding':
  967. if (empty($encoding)) {
  968. if (is_null($iconv_output_encoding)) {
  969. $iconv_output_encoding = @iconv_get_encoding($type);
  970. }
  971. return $iconv_output_encoding;
  972. }
  973. if (_api_iconv_supports($encoding)) {
  974. if(@iconv_set_encoding($type, $encoding)) {
  975. $iconv_output_encoding = $encoding;
  976. return true;
  977. }
  978. return false;
  979. }
  980. return false;
  981. }
  982. return false;
  983. }
  984. /**
  985. * Ckecks whether a given encoding is known to define single-byte characters only.
  986. * The result might be not accurate for unknown by this library encodings. This is not fatal,
  987. * then the library picks up conversions plus Unicode related internal algorithms.
  988. * @param string $encoding A given encoding identificator.
  989. * @return bool TRUE if the encoding is known as single-byte (for ISO-8859-15, WINDOWS-1251, etc.), FALSE otherwise.
  990. */
  991. function _api_is_single_byte_encoding($encoding) {
  992. static $checked = array();
  993. if (!isset($checked[$encoding])) {
  994. $character_map = _api_get_character_map_name(api_refine_encoding_id($encoding));
  995. $checked[$encoding] = (!empty($character_map)
  996. && !in_array($character_map, array('UTF-8', 'HTML-ENTITIES')));
  997. }
  998. return $checked[$encoding];
  999. }
  1000. /**
  1001. * Checks whether the specified encoding is supported by the PHP mbstring extension.
  1002. * @param string $encoding The specified encoding.
  1003. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  1004. */
  1005. function _api_mb_supports($encoding) {
  1006. static $supported = array();
  1007. if (!isset($supported[$encoding])) {
  1008. if (MBSTRING_INSTALLED) {
  1009. $supported[$encoding] = api_equal_encodings($encoding, mb_list_encodings(), true);
  1010. } else {
  1011. $supported[$encoding] = false;
  1012. }
  1013. }
  1014. return $supported[$encoding];
  1015. }
  1016. /**
  1017. * Checks whether the specified encoding is supported by the PHP iconv extension.
  1018. * @param string $encoding The specified encoding.
  1019. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  1020. */
  1021. function _api_iconv_supports($encoding) {
  1022. static $supported = array();
  1023. if (!isset($supported[$encoding])) {
  1024. if (ICONV_INSTALLED) {
  1025. $enc = api_refine_encoding_id($encoding);
  1026. if ($enc != 'HTML-ENTITIES') {
  1027. $test_string = '';
  1028. for ($i = 32; $i < 128; $i++) {
  1029. $test_string .= chr($i);
  1030. }
  1031. $supported[$encoding] = (@iconv_strlen($test_string, $enc)) ? true : false;
  1032. } else {
  1033. $supported[$encoding] = false;
  1034. }
  1035. } else {
  1036. $supported[$encoding] = false;
  1037. }
  1038. }
  1039. return $supported[$encoding];
  1040. }
  1041. // This function checks whether the function _api_convert_encoding() (the php-
  1042. // implementation) is able to convert from/to a given encoding.
  1043. function _api_convert_encoding_supports($encoding) {
  1044. static $supports = array();
  1045. if (!isset($supports[$encoding])) {
  1046. $supports[$encoding] = _api_get_character_map_name(api_refine_encoding_id($encoding)) != '';
  1047. }
  1048. return $supports[$encoding];
  1049. }
  1050. /**
  1051. * Checks whether the specified encoding is supported by the html-entitiy related functions.
  1052. * @param string $encoding The specified encoding.
  1053. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  1054. */
  1055. function _api_html_entity_supports($encoding) {
  1056. static $supports = array();
  1057. if (!isset($supports[$encoding])) {
  1058. // See http://php.net/manual/en/function.htmlentities.php
  1059. $html_entity_encodings = array(
  1060. 'ISO-8859-1',
  1061. 'ISO-8859-15',
  1062. 'UTF-8',
  1063. 'CP866',
  1064. 'CP1251',
  1065. 'CP1252',
  1066. 'KOI8-R',
  1067. 'BIG5', '950',
  1068. 'GB2312', '936',
  1069. 'BIG5-HKSCS',
  1070. 'Shift_JIS', 'SJIS', '932',
  1071. 'EUC-JP', 'EUCJP'
  1072. );
  1073. $supports[$encoding] = api_equal_encodings($encoding, $html_entity_encodings);
  1074. }
  1075. return $supports[$encoding];
  1076. }
  1077. /**
  1078. * Upgrading the PHP5 mbstring extension
  1079. */
  1080. // A multibyte replacement of strchr(). This function exists in PHP 5 >= 5.2.0
  1081. // See http://php.net/manual/en/function.mb-strrchr
  1082. if (MBSTRING_INSTALLED && !function_exists('mb_strchr')) {
  1083. function mb_strchr($haystack, $needle, $part = false, $encoding = null) {
  1084. if (empty($encoding)) {
  1085. $encoding = mb_internal_encoding();
  1086. }
  1087. return mb_strstr($haystack, $needle, $part, $encoding);
  1088. }
  1089. }
  1090. // A multibyte replacement of stripos(). This function exists in PHP 5 >= 5.2.0
  1091. // See http://php.net/manual/en/function.mb-stripos
  1092. if (MBSTRING_INSTALLED && !function_exists('mb_stripos')) {
  1093. function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) {
  1094. if (empty($encoding)) {
  1095. $encoding = mb_internal_encoding();
  1096. }
  1097. return mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), $offset, $encoding);
  1098. }
  1099. }
  1100. // A multibyte replacement of stristr(). This function exists in PHP 5 >= 5.2.0
  1101. // See http://php.net/manual/en/function.mb-stristr
  1102. if (MBSTRING_INSTALLED && !function_exists('mb_stristr')) {
  1103. function mb_stristr($haystack, $needle, $part = false, $encoding = null) {
  1104. if (empty($encoding)) {
  1105. $encoding = mb_internal_encoding();
  1106. }
  1107. $pos = mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), 0, $encoding);
  1108. if ($pos === false) {
  1109. return false;
  1110. }
  1111. if ($part) {
  1112. return mb_substr($haystack, 0, $pos + 1, $encoding);
  1113. }
  1114. return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
  1115. }
  1116. }
  1117. // A multibyte replacement of strrchr(). This function exists in PHP 5 >= 5.2.0
  1118. // See http://php.net/manual/en/function.mb-strrchr
  1119. if (MBSTRING_INSTALLED && !function_exists('mb_strrchr')) {
  1120. function mb_strrchr($haystack, $needle, $part = false, $encoding = null) {
  1121. if (empty($encoding)) {
  1122. $encoding = mb_internal_encoding();
  1123. }
  1124. $needle = mb_substr($needle, 0, 1, $encoding);
  1125. $pos = mb_strrpos($haystack, $needle, mb_strlen($haystack, $encoding) - 1, $encoding);
  1126. if ($pos === false) {
  1127. return false;
  1128. }
  1129. if ($part) {
  1130. return mb_substr($haystack, 0, $pos + 1, $encoding);
  1131. }
  1132. return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
  1133. }
  1134. }
  1135. // A multibyte replacement of strstr(). This function exists in PHP 5 >= 5.2.0
  1136. // See http://php.net/manual/en/function.mb-strstr
  1137. if (MBSTRING_INSTALLED && !function_exists('mb_strstr')) {
  1138. function mb_strstr($haystack, $needle, $part = false, $encoding = null) {
  1139. if (empty($encoding)) {
  1140. $encoding = mb_internal_encoding();
  1141. }
  1142. $pos = mb_strpos($haystack, $needle, 0, $encoding);
  1143. if ($pos === false) {
  1144. return false;
  1145. }
  1146. if ($part) {
  1147. return mb_substr($haystack, 0, $pos + 1, $encoding);
  1148. }
  1149. return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
  1150. }
  1151. }