internationalization_internal.lib.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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. * Returns returns person name convention for a given language.
  194. * @param string $language The input language.
  195. * @param string $type The type of the requested convention.
  196. * It may be 'format' for name order convention or 'sort_by' for name sorting convention.
  197. * @return mixed Depending of the requested type,
  198. * the returned result may be string or boolean; null is returned on error;
  199. */
  200. function _api_get_person_name_convention($language, $type)
  201. {
  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(
  210. 'english' => array(
  211. 'format' => 'title first_name last_name',
  212. 'sort_by' => 'first_name'
  213. )
  214. );
  215. }
  216. // Overwrite classic conventions
  217. $customConventions = api_get_configuration_value('name_order_conventions');
  218. if (!empty($customConventions)) {
  219. foreach ($customConventions as $key => $data) {
  220. $conventions[$key] = $data;
  221. }
  222. }
  223. $search1 = array('FIRST_NAME', 'LAST_NAME', 'TITLE');
  224. $replacement1 = array('%F', '%L', '%T');
  225. $search2 = array('first_name', 'last_name', 'title');
  226. $replacement2 = array('%f', '%l', '%t');
  227. foreach (array_keys($conventions) as $key) {
  228. $conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
  229. $conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search2, $replacement2, $conventions[$key]['format']))));
  230. $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
  231. }
  232. }
  233. switch ($type) {
  234. case 'format':
  235. return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l';
  236. case 'sort_by':
  237. return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true;
  238. }
  239. return null;
  240. }
  241. /**
  242. * Replaces non-valid formats for person names with the default (English) format.
  243. * @param string $format The input format to be verified.
  244. * @return bool Returns the same format if is is valid, otherwise returns a valid English format.
  245. */
  246. function _api_validate_person_name_format($format) {
  247. if (empty($format) || stripos($format, '%f') === false || stripos($format, '%l') === false) {
  248. return '%t %f %l';
  249. }
  250. return $format;
  251. }
  252. /**
  253. * Removes leading, trailing and duplicate whitespace and/or commas in a full person name.
  254. * 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.
  255. * @param string $person_name The input person name.
  256. * @return string Returns cleaned person name.
  257. */
  258. function _api_clean_person_name($person_name) {
  259. return preg_replace(array('/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'), array(' ', ', ', ',', '', ''), $person_name);
  260. }
  261. /**
  262. * Appendix to "Multibyte string conversion functions"
  263. */
  264. /**
  265. * This is a php-implementation of a function that is similar to mb_convert_encoding() from mbstring extension.
  266. * The function converts a given string from one to another character encoding.
  267. * @param string $string The string being converted.
  268. * @param string $to_encoding The encoding that $string is being converted to.
  269. * @param string $from_encoding The encoding that $string is being converted from.
  270. * @return string Returns the converted string.
  271. */
  272. function _api_convert_encoding(&$string, $to_encoding, $from_encoding) {
  273. $str = (string)$string;
  274. static $character_map = array();
  275. static $utf8_compatible = array('UTF-8', 'US-ASCII');
  276. if (empty($str)) {
  277. return $str;
  278. }
  279. $to_encoding = api_refine_encoding_id($to_encoding);
  280. $from_encoding = api_refine_encoding_id($from_encoding);
  281. if (api_equal_encodings($to_encoding, $from_encoding)) {
  282. return $str;
  283. }
  284. if ($to_encoding == 'HTML-ENTITIES') {
  285. return api_htmlentities($str, ENT_QUOTES, $from_encoding);
  286. }
  287. if ($from_encoding == 'HTML-ENTITIES') {
  288. return api_html_entity_decode($str, ENT_QUOTES, $to_encoding);
  289. }
  290. $to = _api_get_character_map_name($to_encoding);
  291. $from = _api_get_character_map_name($from_encoding);
  292. if (empty($to) || empty($from) || $to == $from || (in_array($to, $utf8_compatible) && in_array($from, $utf8_compatible))) {
  293. return $str;
  294. }
  295. if (!isset($character_map[$to])) {
  296. $character_map[$to] = &_api_parse_character_map($to);
  297. }
  298. if ($character_map[$to] === false) {
  299. return $str;
  300. }
  301. if (!isset($character_map[$from])) {
  302. $character_map[$from] = &_api_parse_character_map($from);
  303. }
  304. if ($character_map[$from] === false) {
  305. return $str;
  306. }
  307. if ($from != 'UTF-8') {
  308. $len = api_byte_count($str);
  309. $codepoints = array();
  310. for ($i = 0; $i < $len; $i++) {
  311. $ord = ord($str[$i]);
  312. if ($ord > 127) {
  313. if (isset($character_map[$from]['local'][$ord])) {
  314. $codepoints[] = $character_map[$from]['local'][$ord];
  315. } else {
  316. $codepoints[] = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  317. }
  318. } else {
  319. $codepoints[] = $ord;
  320. }
  321. }
  322. } else {
  323. $codepoints = _api_utf8_to_unicode($str);
  324. }
  325. if ($to != 'UTF-8') {
  326. foreach ($codepoints as $i => &$codepoint) {
  327. if ($codepoint > 127) {
  328. if (isset($character_map[$to]['unicode'][$codepoint])) {
  329. $codepoint = chr($character_map[$to]['unicode'][$codepoint]);
  330. } else {
  331. $codepoint = '?'; // Unknown character.
  332. }
  333. } else {
  334. $codepoint = chr($codepoint);
  335. }
  336. }
  337. $str = implode($codepoints);
  338. } else {
  339. $str = _api_utf8_from_unicode($codepoints);
  340. }
  341. return $str;
  342. }
  343. /**
  344. * This function determines the name of corresponding to a given encoding conversion table.
  345. * It is able to deal with some aliases of the encoding.
  346. * @param string $encoding The given encoding identificator, for example 'WINDOWS-1252'.
  347. * @return string Returns the name of the corresponding conversion table, for the same example - 'CP1252'.
  348. */
  349. function _api_get_character_map_name($encoding) {
  350. static $character_map_selector;
  351. if (!isset($character_map_selector)) {
  352. $file = dirname(__FILE__).'/internationalization_database/conversion/character_map_selector.php';
  353. if (file_exists($file)) {
  354. $character_map_selector = include ($file);
  355. } else {
  356. $character_map_selector = array();
  357. }
  358. }
  359. return isset($character_map_selector[$encoding]) ? $character_map_selector[$encoding] : '';
  360. }
  361. /**
  362. * This function parses a given conversion table (a text file) and creates in the memory
  363. * two tables for conversion - character set from/to Unicode codepoints.
  364. * @param string $name The name of the thext file that contains the conversion table, for example 'CP1252' (file CP1252.TXT will be parsed).
  365. * @return array Returns an array that contains forward and reverse tables (from/to Unicode).
  366. */
  367. function &_api_parse_character_map($name) {
  368. $result = array();
  369. $file = dirname(__FILE__).'/internationalization_database/conversion/' . $name . '.TXT';
  370. if (file_exists($file)) {
  371. $text = @file_get_contents($file);
  372. if ($text !== false) {
  373. $text = explode(chr(10), $text);
  374. foreach ($text as $line) {
  375. if (empty($line)) {
  376. continue;
  377. }
  378. if (!empty($line) && trim($line) && $line[0] != '#') {
  379. $matches = array();
  380. preg_match('/[[:space:]]*0x([[:alnum:]]*)[[:space:]]+0x([[:alnum:]]*)[[:space:]]+/', $line, $matches);
  381. $ord = hexdec(trim($matches[1]));
  382. if ($ord > 127) {
  383. $codepoint = hexdec(trim($matches[2]));
  384. $result['local'][$ord] = $codepoint;
  385. $result['unicode'][$codepoint] = $ord;
  386. }
  387. }
  388. }
  389. } else {
  390. return false ;
  391. }
  392. } else {
  393. return false;
  394. }
  395. return $result;
  396. }
  397. /**
  398. * Takes an UTF-8 string and returns an array of integer values representing the Unicode characters.
  399. * Astral planes are supported ie. the ints in the output can be > 0xFFFF. Occurrances of the BOM are ignored.
  400. * Surrogates are not allowed.
  401. * @param string $string The UTF-8 encoded string.
  402. * @return array Returns an array of unicode code points.
  403. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  404. * @link http://hsivonen.iki.fi/php-utf8/
  405. * @author Ivan Tcholakov, August 2009, adaptation for the Dokeos LMS.
  406. */
  407. function _api_utf8_to_unicode(&$string) {
  408. $str = (string)$string;
  409. $state = 0; // cached expected number of octets after the current octet
  410. // until the beginning of the next UTF8 character sequence
  411. $codepoint = 0; // cached Unicode character
  412. $bytes = 1; // cached expected number of octets in the current sequence
  413. $result = array();
  414. $len = api_byte_count($str);
  415. for ($i = 0; $i < $len; $i++) {
  416. $byte = ord($str[$i]);
  417. if ($state == 0) {
  418. // When state is zero we expect either a US-ASCII character or a multi-octet sequence.
  419. if (0 == (0x80 & ($byte))) {
  420. // US-ASCII, pass straight through.
  421. $result[] = $byte;
  422. $bytes = 1;
  423. } else if (0xC0 == (0xE0 & ($byte))) {
  424. // First octet of 2 octet sequence
  425. $codepoint = ($byte);
  426. $codepoint = ($codepoint & 0x1F) << 6;
  427. $state = 1;
  428. $bytes = 2;
  429. } else if (0xE0 == (0xF0 & ($byte))) {
  430. // First octet of 3 octet sequence
  431. $codepoint = ($byte);
  432. $codepoint = ($codepoint & 0x0F) << 12;
  433. $state = 2;
  434. $bytes = 3;
  435. } else if (0xF0 == (0xF8 & ($byte))) {
  436. // First octet of 4 octet sequence
  437. $codepoint = ($byte);
  438. $codepoint = ($codepoint & 0x07) << 18;
  439. $state = 3;
  440. $bytes = 4;
  441. } else if (0xF8 == (0xFC & ($byte))) {
  442. // First octet of 5 octet sequence.
  443. // This is illegal because the encoded codepoint must be either
  444. // (a) not the shortest form or
  445. // (b) outside the Unicode range of 0-0x10FFFF.
  446. // Rather than trying to resynchronize, we will carry on until the end
  447. // of the sequence and let the later error handling code catch it.
  448. $codepoint = ($byte);
  449. $codepoint = ($codepoint & 0x03) << 24;
  450. $state = 4;
  451. $bytes = 5;
  452. } else if (0xFC == (0xFE & ($byte))) {
  453. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  454. $codepoint = ($byte);
  455. $codepoint = ($codepoint & 1) << 30;
  456. $state = 5;
  457. $bytes = 6;
  458. } else {
  459. // Current octet is neither in the US-ASCII range nor a legal first octet of a multi-octet sequence.
  460. $state = 0;
  461. $codepoint = 0;
  462. $bytes = 1;
  463. $result[] = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  464. continue ;
  465. }
  466. } else {
  467. // When state is non-zero, we expect a continuation of the multi-octet sequence
  468. if (0x80 == (0xC0 & ($byte))) {
  469. // Legal continuation.
  470. $shift = ($state - 1) * 6;
  471. $tmp = $byte;
  472. $tmp = ($tmp & 0x0000003F) << $shift;
  473. $codepoint |= $tmp;
  474. // End of the multi-octet sequence. $codepoint now contains the final Unicode codepoint to be output
  475. if (0 == --$state) {
  476. // Check for illegal sequences and codepoints.
  477. // From Unicode 3.1, non-shortest form is illegal
  478. if (((2 == $bytes) && ($codepoint < 0x0080)) ||
  479. ((3 == $bytes) && ($codepoint < 0x0800)) ||
  480. ((4 == $bytes) && ($codepoint < 0x10000)) ||
  481. (4 < $bytes) ||
  482. // From Unicode 3.2, surrogate characters are illegal
  483. (($codepoint & 0xFFFFF800) == 0xD800) ||
  484. // Codepoints outside the Unicode range are illegal
  485. ($codepoint > 0x10FFFF)) {
  486. $state = 0;
  487. $codepoint = 0;
  488. $bytes = 1;
  489. $result[] = 0xFFFD;
  490. continue ;
  491. }
  492. if (0xFEFF != $codepoint) {
  493. // BOM is legal but we don't want to output it
  494. $result[] = $codepoint;
  495. }
  496. // Initialize UTF8 cache
  497. $state = 0;
  498. $codepoint = 0;
  499. $bytes = 1;
  500. }
  501. } else {
  502. // ((0xC0 & (*in) != 0x80) && (state != 0))
  503. // Incomplete multi-octet sequence.
  504. $state = 0;
  505. $codepoint = 0;
  506. $bytes = 1;
  507. $result[] = 0xFFFD;
  508. }
  509. }
  510. }
  511. return $result;
  512. }
  513. /**
  514. * Takes an array of Unicode codepoints and returns a UTF-8 string.
  515. * @param array $codepoints An array of Unicode codepoints representing a string.
  516. * @return string Returns a UTF-8 string constructed using the given codepoints.
  517. */
  518. function _api_utf8_from_unicode($codepoints) {
  519. return implode(array_map('_api_utf8_chr', $codepoints));
  520. }
  521. /**
  522. * Takes a codepoint and returns its correspondent UTF-8 encoded character.
  523. * Astral planes are supported, ie the intger input can be > 0xFFFF. Occurrances of the BOM are ignored.
  524. * Surrogates are not allowed.
  525. * @param int $codepoint The Unicode codepoint.
  526. * @return string Returns the corresponding UTF-8 character.
  527. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  528. * @link http://hsivonen.iki.fi/php-utf8/
  529. * @author Ivan Tcholakov, 2009, modifications for the Dokeos LMS.
  530. * @see _api_utf8_from_unicode()
  531. * This is a UTF-8 aware version of the function chr().
  532. * @link http://php.net/manual/en/function.chr.php
  533. */
  534. function _api_utf8_chr($codepoint) {
  535. // ASCII range (including control chars)
  536. if ( ($codepoint >= 0) && ($codepoint <= 0x007f) ) {
  537. $result = chr($codepoint);
  538. // 2 byte sequence
  539. } else if ($codepoint <= 0x07ff) {
  540. $result = chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x003f));
  541. // Byte order mark (skip)
  542. } else if($codepoint == 0xFEFF) {
  543. // nop -- zap the BOM
  544. $result = '';
  545. // Test for illegal surrogates
  546. } else if ($codepoint >= 0xD800 && $codepoint <= 0xDFFF) {
  547. // found a surrogate
  548. $result = _api_utf8_chr(0xFFFD); // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  549. // 3 byte sequence
  550. } else if ($codepoint <= 0xffff) {
  551. $result = chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x003f)) . chr(0x80 | ($codepoint & 0x003f));
  552. // 4 byte sequence
  553. } else if ($codepoint <= 0x10ffff) {
  554. $result = chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  555. } else {
  556. // out of range
  557. $result = _api_utf8_chr(0xFFFD);
  558. }
  559. return $result;
  560. }
  561. /**
  562. * Takes the first UTF-8 character in a string and returns its Unicode codepoint.
  563. * @param string $utf8_character The UTF-8 encoded character.
  564. * @return int Returns: the codepoint; or 0xFFFD (unknown character) when the input string is empty.
  565. * This is a UTF-8 aware version of the function ord().
  566. * @link http://php.net/manual/en/function.ord.php
  567. * Note about a difference with the original funtion ord(): ord('') returns 0.
  568. */
  569. function _api_utf8_ord($utf8_character) {
  570. if ($utf8_character == '') {
  571. return 0xFFFD;
  572. }
  573. $codepoints = _api_utf8_to_unicode($utf8_character);
  574. return $codepoints[0];
  575. }
  576. /**
  577. * Makes a html-entity from Unicode codepoint.
  578. * @param int $codepoint The Unicode codepoint.
  579. * @return string Returns the corresponding html-entity; or ASCII character if $codepoint < 128.
  580. */
  581. function _api_html_entity_from_unicode($codepoint) {
  582. if ($codepoint < 128) {
  583. return chr($codepoint);
  584. }
  585. return '&#'.$codepoint.';';
  586. }
  587. /**
  588. * Appendix to "Common multibyte string functions"
  589. */
  590. /**
  591. * The following function reads case folding properties about a given character from a file-based "database".
  592. * @param int $codepoint The Unicode codepoint that represents a caharacter.
  593. * @param string $type (optional) The type of initial case to be altered: 'lower' (default) or 'upper'.
  594. * @return array Returns an array with properties used to change case of the character.
  595. */
  596. function &_api_utf8_get_letter_case_properties($codepoint, $type = 'lower') {
  597. static $config = array();
  598. static $range = array();
  599. if (!isset($range[$codepoint])) {
  600. if ($codepoint > 128 && $codepoint < 256) {
  601. $range[$codepoint] = '0080_00ff'; // Latin-1 Supplement
  602. } elseif ($codepoint < 384) {
  603. $range[$codepoint] = '0100_017f'; // Latin Extended-A
  604. } elseif ($codepoint < 592) {
  605. $range[$codepoint] = '0180_024F'; // Latin Extended-B
  606. } elseif ($codepoint < 688) {
  607. $range[$codepoint] = '0250_02af'; // IPA Extensions
  608. } elseif ($codepoint >= 880 && $codepoint < 1024) {
  609. $range[$codepoint] = '0370_03ff'; // Greek and Coptic
  610. } elseif ($codepoint < 1280) {
  611. $range[$codepoint] = '0400_04ff'; // Cyrillic
  612. } elseif ($codepoint < 1328) {
  613. $range[$codepoint] = '0500_052f'; // Cyrillic Supplement
  614. } elseif ($codepoint < 1424) {
  615. $range[$codepoint] = '0530_058f'; // Armenian
  616. } elseif ($codepoint >= 7680 && $codepoint < 7936) {
  617. $range[$codepoint] = '1e00_1eff'; // Latin Extended Additional
  618. } elseif ($codepoint < 8192) {
  619. $range[$codepoint] = '1f00_1fff'; // Greek Extended
  620. } elseif ($codepoint >= 8448 && $codepoint < 8528) {
  621. $range[$codepoint] = '2100_214f'; // Letterlike Symbols
  622. } elseif ($codepoint < 8592) {
  623. $range[$codepoint] = '2150_218f'; // Number Forms
  624. } elseif ($codepoint >= 9312 && $codepoint < 9472) {
  625. $range[$codepoint] = '2460_24ff'; // Enclosed Alphanumerics
  626. } elseif ($codepoint >= 11264 && $codepoint < 11360) {
  627. $range[$codepoint] = '2c00_2c5f'; // Glagolitic
  628. } elseif ($codepoint < 11392) {
  629. $range[$codepoint] = '2c60_2c7f'; // Latin Extended-C
  630. } elseif ($codepoint < 11520) {
  631. $range[$codepoint] = '2c80_2cff'; // Coptic
  632. } elseif ($codepoint >= 65280 && $codepoint < 65520) {
  633. $range[$codepoint] = 'ff00_ffef'; // Halfwidth and Fullwidth Forms
  634. } else {
  635. $range[$codepoint] = false;
  636. }
  637. if ($range[$codepoint] === false) {
  638. return null;
  639. }
  640. if (!isset($config[$range[$codepoint]])) {
  641. $file = dirname(__FILE__).'/internationalization_database/casefolding/' . $range[$codepoint] . '.php';
  642. if (file_exists($file)) {
  643. include $file;
  644. }
  645. }
  646. }
  647. if ($range[$codepoint] === false || !isset($config[$range[$codepoint]])) {
  648. return null;
  649. }
  650. $result = array();
  651. $count = count($config[$range[$codepoint]]);
  652. for ($i = 0; $i < $count; $i++) {
  653. if ($type === 'lower' && $config[$range[$codepoint]][$i][$type][0] === $codepoint) {
  654. $result[] = $config[$range[$codepoint]][$i];
  655. } elseif ($type === 'upper' && $config[$range[$codepoint]][$i][$type] === $codepoint) {
  656. $result[] = $config[$range[$codepoint]][$i];
  657. }
  658. }
  659. return $result;
  660. }
  661. /**
  662. * A callback for serving the function api_ucwords().
  663. * @param array $matches Input array of matches corresponding to a single word
  664. * @return string Returns a with first char of the word in uppercase
  665. */
  666. function _api_utf8_ucwords_callback($matches) {
  667. return $matches[2] . api_ucfirst(ltrim($matches[0]), 'UTF-8');
  668. }
  669. /**
  670. * Appendix to "Common sting operations with arrays"
  671. */
  672. /**
  673. * This callback function converts from UTF-8 to other encoding. It works with strings or arrays of strings.
  674. * @param mixed $variable The variable to be converted, a string or an array.
  675. * @return mixed Returns the converted form UTF-8 $variable with the same type, string or array.
  676. */
  677. function _api_array_utf8_decode($variable) {
  678. global $_api_encoding;
  679. if (is_array($variable)) {
  680. return array_map('_api_array_utf8_decode', $variable);
  681. }
  682. if (is_string($variable)) {
  683. return api_utf8_decode($variable, $_api_encoding);
  684. }
  685. return $variable;
  686. }
  687. /**
  688. * Appendix to "String comparison"
  689. */
  690. /**
  691. * Returns an instance of Collator class (ICU) created for a specified language.
  692. * @param string $language (optional) Language indentificator: 'english', 'french' ... If it is omited, the current interface language is assumed.
  693. * @return object Returns a instance of Collator class that is suitable for common string comparisons.
  694. */
  695. function _api_get_collator($language = null) {
  696. static $collator = array();
  697. if (empty($language)) {
  698. $language = api_get_interface_language();
  699. }
  700. if (!isset($collator[$language])) {
  701. $locale = _api_get_locale_from_language($language);
  702. $collator[$language] = collator_create($locale);
  703. if (is_object($collator[$language])) {
  704. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  705. }
  706. }
  707. return $collator[$language];
  708. }
  709. /**
  710. * Returns an instance of Collator class (ICU) created for a specified language. This collator treats substrings of digits as numbers.
  711. * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed.
  712. * @return object Returns a instance of Collator class that is suitable for alpha-numerical comparisons.
  713. */
  714. function _api_get_alpha_numerical_collator($language = null) {
  715. static $collator = array();
  716. if (empty($language)) {
  717. $language = api_get_interface_language();
  718. }
  719. if (!isset($collator[$language])) {
  720. $locale = _api_get_locale_from_language($language);
  721. $collator[$language] = collator_create($locale);
  722. if (is_object($collator[$language])) {
  723. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  724. collator_set_attribute($collator[$language], Collator::NUMERIC_COLLATION, Collator::ON);
  725. }
  726. }
  727. return $collator[$language];
  728. }
  729. /**
  730. * A string comparison callback function for sorting.
  731. * @param string $string1 The first string.
  732. * @param string $string2 The second string.
  733. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 > $string2; -1 if $string1 < $string2.
  734. */
  735. function _api_cmp($string1, $string2) {
  736. global $_api_collator, $_api_encoding;
  737. $result = collator_compare($_api_collator, api_utf8_encode($string1, $_api_encoding), api_utf8_encode($string2, $_api_encoding));
  738. return $result === false ? 0 : $result;
  739. }
  740. /**
  741. * A reverse string comparison callback function for sorting.
  742. * @param string $string1 The first string.
  743. * @param string $string2 The second string.
  744. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 < $string2; -1 if $string1 > $string2.
  745. */
  746. function _api_rcmp($string1, $string2) {
  747. global $_api_collator, $_api_encoding;
  748. $result = collator_compare($_api_collator, api_utf8_encode($string2, $_api_encoding), api_utf8_encode($string1, $_api_encoding));
  749. return $result === false ? 0 : $result;
  750. }
  751. /**
  752. * A case-insensitive string comparison callback function for sorting.
  753. * @param string $string1 The first string.
  754. * @param string $string2 The second string.
  755. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 > $string2; -1 if $string1 < $string2.
  756. */
  757. function _api_casecmp($string1, $string2) {
  758. global $_api_collator, $_api_encoding;
  759. $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'));
  760. return $result === false ? 0 : $result;
  761. }
  762. /**
  763. * A reverse case-insensitive string comparison callback function for sorting.
  764. * @param string $string1 The first string.
  765. * @param string $string2 The second string.
  766. * @return int Returns 0 if $string1 = $string2 or if there is an error; 1 if $string1 < $string2; -1 if $string1 > $string2.
  767. */
  768. function _api_casercmp($string1, $string2) {
  769. global $_api_collator, $_api_encoding;
  770. $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'));
  771. return $result === false ? 0 : $result;
  772. }
  773. /**
  774. * A reverse function from php-core function strnatcmp(), performs string comparison in reverse natural (alpha-numerical) order.
  775. * @param string $string1 The first string.
  776. * @param string $string2 The second string.
  777. * @return int Returns 0 if $string1 = $string2; >0 if $string1 < $string2; <0 if $string1 > $string2.
  778. */
  779. function _api_strnatrcmp($string1, $string2) {
  780. return strnatcmp($string2, $string1);
  781. }
  782. /**
  783. * A reverse function from php-core function strnatcasecmp(), performs string comparison in reverse case-insensitive natural (alpha-numerical) order.
  784. * @param string $string1 The first string.
  785. * @param string $string2 The second string.
  786. * @return int Returns 0 if $string1 = $string2; >0 if $string1 < $string2; <0 if $string1 > $string2.
  787. */
  788. function _api_strnatcasercmp($string1, $string2) {
  789. return strnatcasecmp($string2, $string1);
  790. }
  791. /**
  792. * A function that translates sorting flag constants from php core to correspondent constants from intl extension.
  793. * @param int $sort_flag (optional) Sorting modifier flag as it is defined for php core. The default value is SORT_REGULAR.
  794. * @return int Retturns the corresponding sorting modifier flag as it is defined in intl php-extension.
  795. */
  796. function _api_get_collator_sort_flag($sort_flag = SORT_REGULAR) {
  797. switch ($sort_flag) {
  798. case SORT_STRING:
  799. case SORT_LOCALE_STRING:
  800. return Collator::SORT_STRING;
  801. case SORT_NUMERIC:
  802. return Collator::SORT_NUMERIC;
  803. }
  804. return Collator::SORT_REGULAR;
  805. }
  806. /**
  807. * ICU locales (accessible through intl extension).
  808. */
  809. /**
  810. * Returns isocode (see api_get_language_isocode()) which is purified accordingly to
  811. * be used by the php intl extension (ICU library).
  812. * @param string $language (optional) This is the name of the folder containing translations for the corresponding language.
  813. * If $language is omitted, interface language is assumed then.
  814. * @return string The found language locale id or null on error. Examples: bg, en, pt_BR, ...
  815. */
  816. function _api_get_locale_from_language($language = null) {
  817. static $locale = array();
  818. if (empty($language)) {
  819. $language = api_get_interface_language();
  820. }
  821. if (!isset($locale[$language])) {
  822. $locale[$language] = str_replace('-', '_', api_get_language_isocode($language));
  823. }
  824. return $locale[$language];
  825. }
  826. /**
  827. * Sets/gets the default internal value of the locale id (for the intl extension, ICU).
  828. * @param string $locale (optional) The locale id to be set. When it is omitted, the function returns (gets, reads) the default internal value.
  829. * @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.
  830. */
  831. function _api_set_default_locale($locale = null) {
  832. static $default_locale = 'en';
  833. if (!empty($locale)) {
  834. $default_locale = $locale;
  835. if (INTL_INSTALLED) {
  836. return @locale_set_default($locale);
  837. }
  838. return true;
  839. } else {
  840. if (INTL_INSTALLED) {
  841. $default_locale = @locale_get_default();
  842. }
  843. }
  844. return $default_locale;
  845. }
  846. /**
  847. * Gets the default internal value of the locale id (for the intl extension, ICU).
  848. * @return string Returns as string the current default value.
  849. */
  850. function api_get_default_locale() {
  851. return _api_set_default_locale();
  852. }
  853. /**
  854. * Appendix to "Encoding management functions"
  855. */
  856. /**
  857. * Returns a table with non-UTF-8 encodings for all system languages.
  858. * @return array Returns an array in the form array('language1' => array('encoding1', encoding2', ...), ...)
  859. * Note: The function api_get_non_utf8_encoding() returns the first encoding from this array that is correspondent to the given language.
  860. */
  861. function & _api_non_utf8_encodings() {
  862. static $encodings;
  863. if (!isset($encodings)) {
  864. $file = dirname(__FILE__).'/internationalization_database/non_utf8_encodings.php';
  865. if (file_exists($file)) {
  866. $encodings = include ($file);
  867. } else {
  868. $encodings = array('english' => array('ISO-8859-15'));
  869. }
  870. }
  871. return $encodings;
  872. }
  873. /**
  874. * Sets/Gets internal character encoding of the common string functions within the PHP mbstring extension.
  875. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding.
  876. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  877. * Note: This function is used in the global initialization script for setting the internal encoding to the platform's character set.
  878. * @link http://php.net/manual/en/function.mb-internal-encoding
  879. */
  880. function _api_mb_internal_encoding($encoding = null) {
  881. static $mb_internal_encoding = null;
  882. if (empty($encoding)) {
  883. if (is_null($mb_internal_encoding)) {
  884. if (MBSTRING_INSTALLED) {
  885. $mb_internal_encoding = @mb_internal_encoding();
  886. } else {
  887. $mb_internal_encoding = 'UTF-8';
  888. }
  889. }
  890. return $mb_internal_encoding;
  891. }
  892. $mb_internal_encoding = $encoding;
  893. if (_api_mb_supports($encoding)) {
  894. return @mb_internal_encoding($encoding);
  895. }
  896. return false;
  897. }
  898. /**
  899. * Sets/Gets internal character encoding of the regular expression functions (ereg-like) within the PHP mbstring extension.
  900. * @param string $encoding (optional) When this parameter is given, the function sets the internal encoding.
  901. * @return string When $encoding parameter is not given, the function returns the internal encoding.
  902. * Note: This function is used in the global initialization script for setting the internal encoding to the platform's character set.
  903. * @link http://php.net/manual/en/function.mb-regex-encoding
  904. */
  905. function _api_mb_regex_encoding($encoding = null) {
  906. static $mb_regex_encoding = null;
  907. if (empty($encoding)) {
  908. if (is_null($mb_regex_encoding)) {
  909. if (MBSTRING_INSTALLED) {
  910. $mb_regex_encoding = @mb_regex_encoding();
  911. } else {
  912. $mb_regex_encoding = 'UTF-8';
  913. }
  914. }
  915. return $mb_regex_encoding;
  916. }
  917. $mb_regex_encoding = $encoding;
  918. if (_api_mb_supports($encoding)) {
  919. return @mb_regex_encoding($encoding);
  920. }
  921. return false;
  922. }
  923. /**
  924. * Retrieves specified internal encoding configuration variable within the PHP iconv extension.
  925. * @param string $type The parameter $type could be: 'iconv_internal_encoding', 'iconv_input_encoding', or 'iconv_output_encoding'.
  926. * @return mixed The function returns the requested encoding or FALSE on error.
  927. * @link http://php.net/manual/en/function.iconv-get-encoding
  928. */
  929. function _api_iconv_get_encoding($type) {
  930. return _api_iconv_set_encoding($type);
  931. }
  932. /**
  933. * Sets specified internal encoding configuration variables within the PHP iconv extension.
  934. * @param string $type The parameter $type could be: 'iconv_internal_encoding', 'iconv_input_encoding', or 'iconv_output_encoding'.
  935. * @param string $encoding (optional) The desired encoding to be set.
  936. * @return bool Returns TRUE on success, FALSE on error.
  937. * Note: This function is used in the global initialization script for setting these three internal encodings to the platform's character set.
  938. * @link http://php.net/manual/en/function.iconv-set-encoding
  939. */
  940. function _api_iconv_set_encoding($type, $encoding = null) {
  941. static $iconv_internal_encoding = null;
  942. static $iconv_input_encoding = null;
  943. static $iconv_output_encoding = null;
  944. if (!ICONV_INSTALLED) {
  945. return false;
  946. }
  947. switch ($type) {
  948. case 'iconv_internal_encoding':
  949. if (empty($encoding)) {
  950. if (is_null($iconv_internal_encoding)) {
  951. $iconv_internal_encoding = @iconv_get_encoding($type);
  952. }
  953. return $iconv_internal_encoding;
  954. }
  955. if (_api_iconv_supports($encoding)) {
  956. if(@iconv_set_encoding($type, $encoding)) {
  957. $iconv_internal_encoding = $encoding;
  958. return true;
  959. }
  960. return false;
  961. }
  962. return false;
  963. case 'iconv_input_encoding':
  964. if (empty($encoding)) {
  965. if (is_null($iconv_input_encoding)) {
  966. $iconv_input_encoding = @iconv_get_encoding($type);
  967. }
  968. return $iconv_input_encoding;
  969. }
  970. if (_api_iconv_supports($encoding)) {
  971. if(@iconv_set_encoding($type, $encoding)) {
  972. $iconv_input_encoding = $encoding;
  973. return true;
  974. }
  975. return false;
  976. }
  977. return false;
  978. case 'iconv_output_encoding':
  979. if (empty($encoding)) {
  980. if (is_null($iconv_output_encoding)) {
  981. $iconv_output_encoding = @iconv_get_encoding($type);
  982. }
  983. return $iconv_output_encoding;
  984. }
  985. if (_api_iconv_supports($encoding)) {
  986. if(@iconv_set_encoding($type, $encoding)) {
  987. $iconv_output_encoding = $encoding;
  988. return true;
  989. }
  990. return false;
  991. }
  992. return false;
  993. }
  994. return false;
  995. }
  996. /**
  997. * Checks whether a given encoding is known to define single-byte characters only.
  998. * The result might be not accurate for unknown by this library encodings. This is not fatal,
  999. * then the library picks up conversions plus Unicode related internal algorithms.
  1000. * @param string $encoding A given encoding identificator.
  1001. * @return bool TRUE if the encoding is known as single-byte (for ISO-8859-15, WINDOWS-1251, etc.), FALSE otherwise.
  1002. */
  1003. function _api_is_single_byte_encoding($encoding) {
  1004. static $checked = array();
  1005. if (!isset($checked[$encoding])) {
  1006. $character_map = _api_get_character_map_name(api_refine_encoding_id($encoding));
  1007. $checked[$encoding] = (!empty($character_map)
  1008. && !in_array($character_map, array('UTF-8', 'HTML-ENTITIES')));
  1009. }
  1010. return $checked[$encoding];
  1011. }
  1012. /**
  1013. * Checks whether the specified encoding is supported by the PHP mbstring extension.
  1014. * @param string $encoding The specified encoding.
  1015. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  1016. */
  1017. function _api_mb_supports($encoding) {
  1018. static $supported = array();
  1019. if (!isset($supported[$encoding])) {
  1020. if (MBSTRING_INSTALLED) {
  1021. $supported[$encoding] = api_equal_encodings($encoding, mb_list_encodings(), true);
  1022. } else {
  1023. $supported[$encoding] = false;
  1024. }
  1025. }
  1026. return $supported[$encoding];
  1027. }
  1028. /**
  1029. * Checks whether the specified encoding is supported by the PHP iconv extension.
  1030. * @param string $encoding The specified encoding.
  1031. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  1032. */
  1033. function _api_iconv_supports($encoding) {
  1034. static $supported = array();
  1035. if (!isset($supported[$encoding])) {
  1036. if (ICONV_INSTALLED) {
  1037. $enc = api_refine_encoding_id($encoding);
  1038. if ($enc != 'HTML-ENTITIES') {
  1039. $test_string = '';
  1040. for ($i = 32; $i < 128; $i++) {
  1041. $test_string .= chr($i);
  1042. }
  1043. $supported[$encoding] = (@iconv_strlen($test_string, $enc)) ? true : false;
  1044. } else {
  1045. $supported[$encoding] = false;
  1046. }
  1047. } else {
  1048. $supported[$encoding] = false;
  1049. }
  1050. }
  1051. return $supported[$encoding];
  1052. }
  1053. // This function checks whether the function _api_convert_encoding() (the php-
  1054. // implementation) is able to convert from/to a given encoding.
  1055. function _api_convert_encoding_supports($encoding) {
  1056. static $supports = array();
  1057. if (!isset($supports[$encoding])) {
  1058. $supports[$encoding] = _api_get_character_map_name(api_refine_encoding_id($encoding)) != '';
  1059. }
  1060. return $supports[$encoding];
  1061. }
  1062. /**
  1063. * Checks whether the specified encoding is supported by the html-entitiy related functions.
  1064. * @param string $encoding The specified encoding.
  1065. * @return bool Returns TRUE when the specified encoding is supported, FALSE othewise.
  1066. */
  1067. function _api_html_entity_supports($encoding) {
  1068. static $supports = array();
  1069. if (!isset($supports[$encoding])) {
  1070. // See http://php.net/manual/en/function.htmlentities.php
  1071. $html_entity_encodings = array(
  1072. 'ISO-8859-1',
  1073. 'ISO-8859-15',
  1074. 'UTF-8',
  1075. 'CP866',
  1076. 'CP1251',
  1077. 'CP1252',
  1078. 'KOI8-R',
  1079. 'BIG5', '950',
  1080. 'GB2312', '936',
  1081. 'BIG5-HKSCS',
  1082. 'Shift_JIS', 'SJIS', '932',
  1083. 'EUC-JP', 'EUCJP'
  1084. );
  1085. $supports[$encoding] = api_equal_encodings($encoding, $html_entity_encodings);
  1086. }
  1087. return $supports[$encoding];
  1088. }