internationalization_internal.lib.php 44 KB

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