123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- <?php
- function _api_convert_encoding($string, $to_encoding, $from_encoding) {
- static $character_map = array();
- static $utf8_like = array('UTF-8', 'US-ASCII');
- if (empty($string)) {
- return $string;
- }
- $to_encoding = api_refine_encoding_id($to_encoding);
- $from_encoding = api_refine_encoding_id($from_encoding);
- if (api_equal_encodings($to_encoding, $from_encoding)) {
- return $string;
- }
- $to = _api_get_character_map_name($to_encoding);
- $from = _api_get_character_map_name($from_encoding);
- if (empty($to) || empty($from) || $to == $from || (in_array($to, $utf8_like) && in_array($from, $utf8_like))) {
- return $string;
- }
- if (!isset($character_map[$to])) {
- $character_map[$to] = _api_parse_character_map($to);
- }
- if ($character_map[$to] === false) {
- return $string;
- }
- if (!isset($character_map[$from])) {
- $character_map[$from] = _api_parse_character_map($from);
- }
- if ($character_map[$from] === false) {
- return $string;
- }
- if ($from != 'UTF-8') {
- $len = api_byte_count($string);
- $codepoints = array();
- for ($i = 0; $i < $len; $i++) {
- $ord = ord($string[$i]);
- if ($ord > 127) {
- if (isset($character_map[$from]['local'][$ord])) {
- $codepoints[] = $character_map[$from]['local'][$ord];
- } else {
- $codepoints[] = 0xFFFD;
- }
- } else {
- $codepoints[] = $ord;
- }
- }
- } else {
- $codepoints = _api_utf8_to_unicode($string);
- }
- if ($to != 'UTF-8') {
- foreach ($codepoints as $i => &$codepoint) {
- if ($codepoint > 127) {
- if (isset($character_map[$from]['local'][$codepoint])) {
- $codepoint = chr($character_map[$from]['local'][$codepoint]);
- } else {
- $codepoint = '?';
- }
- } else {
- $codepoint = chr($codepoint);
- }
- }
- $string = implode($codepoints);
- } else {
- $string = _api_utf8_from_unicode($codepoints);
- }
- return $string;
- }
- function _api_get_character_map_name($encoding) {
- static $character_map_selector;
- if (!isset($character_map_selector)) {
- $file = dirname(__FILE__) . '/multibyte_string_database/conversion/character_map_selector.php';
- if (file_exists($file)) {
- $character_map_selector = include ($file);
- } else {
- $character_map_selector = array();
- }
- }
- return isset($character_map_selector[$encoding]) ? $character_map_selector[$encoding] : '';
- }
- function &_api_parse_character_map($name) {
- $result = array('local' => array(), 'unicode' => array());
- $file = dirname(__FILE__) . '/multibyte_string_database/conversion/' . $name . '.TXT';
- if (file_exists($file)) {
- $text = @file_get_contents($file);
- if ($text !== false) {
- $text = explode(chr(10), $text);
- foreach ($text as $line) {
- if (empty($line)) {
- continue;
- }
- if (!empty($line) && trim($line) && $line[0] != '#') {
- $matches = array();
- preg_match('/[[:space:]]*0x([[:alnum:]]*)[[:space:]]+0x([[:alnum:]]*)[[:space:]]+/', $line, $matches);
- $ord = hexdec(trim($matches[1]));
- if ($ord > 127) {
- $result['local'][$ord] = hexdec(trim($matches[2]));
- $result['unicode'][$result['local'][$ord]] = $ord;
- }
- }
- }
- } else {
- return false ;
- }
- } else {
- return false;
- }
- return $result;
- }
- function _api_utf8_to_unicode($string) {
- $state = 0;
-
- $codepoint = 0;
- $bytes = 1;
- $result = array();
- $len = api_byte_count($string);
- for ($i = 0; $i < $len; $i++) {
- $byte = ord($string[$i]);
- if ($state == 0) {
-
-
- if (0 == (0x80 & ($byte))) {
-
- $result[] = $byte;
- $bytes = 1;
- } else if (0xC0 == (0xE0 & ($byte))) {
-
- $codepoint = ($byte);
- $codepoint = ($codepoint & 0x1F) << 6;
- $state = 1;
- $bytes = 2;
- } else if (0xE0 == (0xF0 & ($byte))) {
-
- $codepoint = ($byte);
- $codepoint = ($codepoint & 0x0F) << 12;
- $state = 2;
- $bytes = 3;
- } else if (0xF0 == (0xF8 & ($byte))) {
-
- $codepoint = ($byte);
- $codepoint = ($codepoint & 0x07) << 18;
- $state = 3;
- $bytes = 4;
- } else if (0xF8 == (0xFC & ($byte))) {
-
-
-
-
-
-
- $codepoint = ($byte);
- $codepoint = ($codepoint & 0x03) << 24;
- $state = 4;
- $bytes = 5;
- } else if (0xFC == (0xFE & ($byte))) {
-
- $codepoint = ($byte);
- $codepoint = ($codepoint & 1) << 30;
- $state = 5;
- $bytes = 6;
- } else {
-
-
- $state = 0;
- $codepoint = 0;
- $bytes = 1;
- $result[] = 0xFFFD;
- continue ;
- }
- } else {
-
-
- if (0x80 == (0xC0 & ($byte))) {
-
- $shift = ($state - 1) * 6;
- $tmp = $byte;
- $tmp = ($tmp & 0x0000003F) << $shift;
- $codepoint |= $tmp;
-
-
- if (0 == --$state) {
-
-
- if (((2 == $bytes) && ($codepoint < 0x0080)) ||
- ((3 == $bytes) && ($codepoint < 0x0800)) ||
- ((4 == $bytes) && ($codepoint < 0x10000)) ||
- (4 < $bytes) ||
-
- (($codepoint & 0xFFFFF800) == 0xD800) ||
-
- ($codepoint > 0x10FFFF)) {
- $state = 0;
- $codepoint = 0;
- $bytes = 1;
- $result[] = 0xFFFD;
- continue ;
- }
- if (0xFEFF != $codepoint) {
-
- $result[] = $codepoint;
- }
-
- $state = 0;
- $codepoint = 0;
- $bytes = 1;
- }
- } else {
-
-
- $state = 0;
- $codepoint = 0;
- $bytes = 1;
- $result[] = 0xFFFD;
- }
- }
- }
- return $result;
- }
- function _api_utf8_from_unicode($codepoints) {
- return implode(array_map('_api_utf8_chr', $codepoints));
- }
- function _api_utf8_chr($codepoint) {
-
- if ( ($codepoint >= 0) && ($codepoint <= 0x007f) ) {
- $result = chr($codepoint);
-
- } else if ($codepoint <= 0x07ff) {
- $result = chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x003f));
-
- } else if($codepoint == 0xFEFF) {
-
- $result = '';
-
- } else if ($codepoint >= 0xD800 && $codepoint <= 0xDFFF) {
-
- $result = _api_utf8_chr(0xFFFD);
-
- } else if ($codepoint <= 0xffff) {
- $result = chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x003f)) . chr(0x80 | ($codepoint & 0x003f));
-
- } else if ($codepoint <= 0x10ffff) {
- $result = chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
- } else {
-
- $result = _api_utf8_chr(0xFFFD);
- }
- return $result;
- }
- function _api_utf8_ord($utf8_character) {
- if (empty($utf8_character)) {
- return 0xFFFD;
- }
- $codepoints = _api_utf8_to_unicode($utf8_character);
- return $codepoints[0];
- }
- function _api_utf8_get_letter_case_properties($codepoint, $type = 'lower') {
- static $config = array();
- static $range = array();
- if (!isset($range[$codepoint])) {
- if ($codepoint > 128 && $codepoint < 256) {
- $range[$codepoint] = '0080_00ff';
- } elseif ($codepoint < 384) {
- $range[$codepoint] = '0100_017f';
- } elseif ($codepoint < 592) {
- $range[$codepoint] = '0180_024F';
- } elseif ($codepoint < 688) {
- $range[$codepoint] = '0250_02af';
- } elseif ($codepoint >= 880 && $codepoint < 1024) {
- $range[$codepoint] = '0370_03ff';
- } elseif ($codepoint < 1280) {
- $range[$codepoint] = '0400_04ff';
- } elseif ($codepoint < 1328) {
- $range[$codepoint] = '0500_052f';
- } elseif ($codepoint < 1424) {
- $range[$codepoint] = '0530_058f';
- } elseif ($codepoint >= 7680 && $codepoint < 7936) {
- $range[$codepoint] = '1e00_1eff';
- } elseif ($codepoint < 8192) {
- $range[$codepoint] = '1f00_1fff';
- } elseif ($codepoint >= 8448 && $codepoint < 8528) {
- $range[$codepoint] = '2100_214f';
- } elseif ($codepoint < 8592) {
- $range[$codepoint] = '2150_218f';
- } elseif ($codepoint >= 9312 && $codepoint < 9472) {
- $range[$codepoint] = '2460_24ff';
- } elseif ($codepoint >= 11264 && $codepoint < 11360) {
- $range[$codepoint] = '2c00_2c5f';
- } elseif ($codepoint < 11392) {
- $range[$codepoint] = '2c60_2c7f';
- } elseif ($codepoint < 11520) {
- $range[$codepoint] = '2c80_2cff';
- } elseif ($codepoint >= 65280 && $codepoint < 65520) {
- $range[$codepoint] = 'ff00_ffef';
- } else {
- $range[$codepoint] = false;
- }
- if ($range[$codepoint] === false) {
- return null;
- }
- if (!isset($config[$range[$codepoint]])) {
- $file = dirname(__FILE__) . '/multibyte_string_database/casefolding/' . $range[$codepoint] . '.php';
- if (file_exists($file)) {
- include $file;
- }
- }
- }
- if ($range[$codepoint] === false || !isset($config[$range[$codepoint]])) {
- return null;
- }
- $result = array();
- $count = count($config[$range[$codepoint]]);
- for ($i = 0; $i < $count; $i++) {
- if ($type === 'lower' && $config[$range[$codepoint]][$i][$type][0] === $codepoint) {
- $result[] = $config[$range[$codepoint]][$i];
- } elseif ($type === 'upper' && $config[$range[$codepoint]][$i][$type] === $codepoint) {
- $result[] = $config[$range[$codepoint]][$i];
- }
- }
- return $result;
- }
- function _api_array_utf8_decode($variable, $encoding) {
- if (is_array($variable)) {
- return array_map('_api_array_utf8_decode', $variable, $encoding);
- }
- if (is_string($var)) {
- return api_utf8_decode($variable, $encoding);
- }
- return $variable;
- }
- function _api_get_collator($language = null) {
- static $collator = array();
- if (!isset($collator[$language])) {
- $locale = api_get_locale_from_language($language);
- $collator[$language] = collator_create($locale);
- if (is_object($collator[$language])) {
- collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
- }
- }
- return $collator[$language];
- }
- function _api_get_alpha_numerical_collator($language = null) {
- static $collator = array();
- if (!isset($collator[$language])) {
- $locale = api_get_locale_from_language($language);
- $collator[$language] = collator_create($locale);
- if (is_object($collator[$language])) {
- collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
- collator_set_attribute($collator[$language], Collator::NUMERIC_COLLATION, Collator::ON);
- }
- }
- return $collator[$language];
- }
- $_api_collator = null;
- $_api_encoding = null;
- function _api_cmp($string1, $string2) {
- global $_api_collator, $_api_encoding;
- $result = collator_compare($_api_collator, api_utf8_encode($string1, $_api_encoding), api_utf8_encode($string2, $_api_encoding));
- return $result === false ? 0 : $result;
- }
- function _api_rcmp($string1, $string2) {
- global $_api_collator, $_api_encoding;
- $result = collator_compare($_api_collator, api_utf8_encode($string2, $_api_encoding), api_utf8_encode($string1, $_api_encoding));
- return $result === false ? 0 : $result;
- }
- function _api_casecmp($string1, $string2) {
- global $_api_collator, $_api_encoding;
- $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'));
- return $result === false ? 0 : $result;
- }
- function _api_casercmp($string1, $string2) {
- global $_api_collator, $_api_encoding;
- $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'));
- return $result === false ? 0 : $result;
- }
- function _api_strnatrcmp($string1, $string2) {
- return strnatcmp($string2, $string1);
- }
- function _api_strnatcasercmp($string1, $string2) {
- return strnatcasecmp($string2, $string1);
- }
- function _api_get_collator_sort_flag($sort_flag = SORT_REGULAR) {
- switch ($sort_flag) {
- case SORT_STRING:
- case SORT_SORT_LOCALE_STRING:
- return Collator::SORT_STRING;
- case SORT_NUMERIC:
- return Collator::SORT_NUMERIC;
- }
- return Collator::SORT_REGULAR;
- }
- function _api_is_single_byte_encoding($encoding) {
- static $checked = array();
- if (!isset($checked[$encoding])) {
- $character_map = _api_get_character_map_name(api_refine_encoding_id($encoding));
- $checked[$encoding] = (!empty($character_map) && $character_map != 'UTF-8');
- }
- return $checked[$encoding];
- }
- function _api_convert_encoding_supports($encoding) {
- static $supports = array();
- if (!isset($supports[encoding])) {
- $supports[encoding] = _api_get_character_map_name($encoding) != '';
- }
- return $supports[encoding];
- }
- if (MBSTRING_INSTALLED && !function_exists('mb_strchr')) {
- function mb_strchr($haystack, $needle, $part = false, $encoding = null) {
- if (empty($encoding)) {
- $encoding = mb_internal_encoding();
- }
- return mb_strstr($haystack, $needle, $part, $encoding);
- }
- }
- if (MBSTRING_INSTALLED && !function_exists('mb_stripos')) {
- function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) {
- if (empty($encoding)) {
- $encoding = mb_internal_encoding();
- }
- return mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), $offset, $encoding);
- }
- }
- if (MBSTRING_INSTALLED && !function_exists('mb_stristr')) {
- function mb_stristr($haystack, $needle, $part = false, $encoding = null) {
- if (empty($encoding)) {
- $encoding = mb_internal_encoding();
- }
- $pos = mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), 0, $encoding);
- if ($pos === false) {
- return false;
- }
- elseif($part == true) {
- return mb_substr($haystack, 0, $pos + 1, $encoding);
- } else {
- return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
- }
- }
- }
- if (MBSTRING_INSTALLED && !function_exists('mb_strrchr')) {
- function mb_strrchr($haystack, $needle, $part = false, $encoding = null) {
- if (empty($encoding)) {
- $encoding = mb_internal_encoding();
- }
- $needle = mb_substr($needle, 0, 1, $encoding);
- $pos = mb_strrpos($haystack, $needle, mb_strlen($haystack, $encoding) - 1, $encoding);
- if ($pos === false) {
- return false;
- } elseif($part == true) {
- return mb_substr($haystack, 0, $pos + 1, $encoding);
- } else {
- return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
- }
- }
- }
- if (MBSTRING_INSTALLED && !function_exists('mb_strstr')) {
- function mb_strstr($haystack, $needle, $part = false, $encoding = null) {
- if (empty($encoding)) {
- $encoding = mb_internal_encoding();
- }
- $pos = mb_strpos($haystack, $needle, 0, $encoding);
- if ($pos === false) {
- return false;
- } elseif($part == true) {
- return mb_substr($haystack, 0, $pos + 1, $encoding);
- } else {
- return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
- }
- }
- }
|