internationalization_internal.lib.php 49 KB

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