internationalization_internal.lib.php 46 KB

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