internationalization_internal.lib.php 45 KB

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