PageRenderTime 64ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/etc/apps/webmail/program/lib/Roundcube/rcube_charset.php

https://github.com/raiman264/zpanelx
PHP | 823 lines | 573 code | 101 blank | 149 comment | 230 complexity | dbf75643eebd9c6e47726b812f037f2c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.1, CC-BY-SA-4.0, GPL-3.0
  1. <?php
  2. /*
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2005-2012, The Roundcube Dev Team |
  6. | Copyright (C) 2011-2012, Kolab Systems AG |
  7. | Copyright (C) 2000 Edmund Grimley Evans <edmundo@rano.org> |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | Provide charset conversion functionality |
  15. +-----------------------------------------------------------------------+
  16. | Author: Thomas Bruederli <roundcube@gmail.com> |
  17. | Author: Aleksander Machniak <alec@alec.pl> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. /**
  21. * Character sets conversion functionality
  22. *
  23. * @package Framework
  24. * @subpackage Core
  25. * @author Thomas Bruederli <roundcube@gmail.com>
  26. * @author Aleksander Machniak <alec@alec.pl>
  27. * @author Edmund Grimley Evans <edmundo@rano.org>
  28. */
  29. class rcube_charset
  30. {
  31. // Aliases: some of them from HTML5 spec.
  32. static public $aliases = array(
  33. 'USASCII' => 'WINDOWS-1252',
  34. 'ANSIX31101983' => 'WINDOWS-1252',
  35. 'ANSIX341968' => 'WINDOWS-1252',
  36. 'UNKNOWN8BIT' => 'ISO-8859-15',
  37. 'UNKNOWN' => 'ISO-8859-15',
  38. 'USERDEFINED' => 'ISO-8859-15',
  39. 'KSC56011987' => 'EUC-KR',
  40. 'GB2312' => 'GBK',
  41. 'GB231280' => 'GBK',
  42. 'UNICODE' => 'UTF-8',
  43. 'UTF7IMAP' => 'UTF7-IMAP',
  44. 'TIS620' => 'WINDOWS-874',
  45. 'ISO88599' => 'WINDOWS-1254',
  46. 'ISO885911' => 'WINDOWS-874',
  47. 'MACROMAN' => 'MACINTOSH',
  48. '77' => 'MAC',
  49. '128' => 'SHIFT-JIS',
  50. '129' => 'CP949',
  51. '130' => 'CP1361',
  52. '134' => 'GBK',
  53. '136' => 'BIG5',
  54. '161' => 'WINDOWS-1253',
  55. '162' => 'WINDOWS-1254',
  56. '163' => 'WINDOWS-1258',
  57. '177' => 'WINDOWS-1255',
  58. '178' => 'WINDOWS-1256',
  59. '186' => 'WINDOWS-1257',
  60. '204' => 'WINDOWS-1251',
  61. '222' => 'WINDOWS-874',
  62. '238' => 'WINDOWS-1250',
  63. 'MS950' => 'CP950',
  64. 'WINDOWS949' => 'UHC',
  65. );
  66. /**
  67. * Catch an error and throw an exception.
  68. *
  69. * @param int Level of the error
  70. * @param string Error message
  71. */
  72. public static function error_handler($errno, $errstr)
  73. {
  74. throw new ErrorException($errstr, 0, $errno);
  75. }
  76. /**
  77. * Parse and validate charset name string (see #1485758).
  78. * Sometimes charset string is malformed, there are also charset aliases
  79. * but we need strict names for charset conversion (specially utf8 class)
  80. *
  81. * @param string $input Input charset name
  82. *
  83. * @return string The validated charset name
  84. */
  85. public static function parse_charset($input)
  86. {
  87. static $charsets = array();
  88. $charset = strtoupper($input);
  89. if (isset($charsets[$input])) {
  90. return $charsets[$input];
  91. }
  92. $charset = preg_replace(array(
  93. '/^[^0-9A-Z]+/', // e.g. _ISO-8859-JP$SIO
  94. '/\$.*$/', // e.g. _ISO-8859-JP$SIO
  95. '/UNICODE-1-1-*/', // RFC1641/1642
  96. '/^X-/', // X- prefix (e.g. X-ROMAN8 => ROMAN8)
  97. ), '', $charset);
  98. if ($charset == 'BINARY') {
  99. return $charsets[$input] = null;
  100. }
  101. // allow A-Z and 0-9 only
  102. $str = preg_replace('/[^A-Z0-9]/', '', $charset);
  103. if (isset(self::$aliases[$str])) {
  104. $result = self::$aliases[$str];
  105. }
  106. // UTF
  107. else if (preg_match('/U[A-Z][A-Z](7|8|16|32)(BE|LE)*/', $str, $m)) {
  108. $result = 'UTF-' . $m[1] . $m[2];
  109. }
  110. // ISO-8859
  111. else if (preg_match('/ISO8859([0-9]{0,2})/', $str, $m)) {
  112. $iso = 'ISO-8859-' . ($m[1] ? $m[1] : 1);
  113. // some clients sends windows-1252 text as latin1,
  114. // it is safe to use windows-1252 for all latin1
  115. $result = $iso == 'ISO-8859-1' ? 'WINDOWS-1252' : $iso;
  116. }
  117. // handle broken charset names e.g. WINDOWS-1250HTTP-EQUIVCONTENT-TYPE
  118. else if (preg_match('/(WIN|WINDOWS)([0-9]+)/', $str, $m)) {
  119. $result = 'WINDOWS-' . $m[2];
  120. }
  121. // LATIN
  122. else if (preg_match('/LATIN(.*)/', $str, $m)) {
  123. $aliases = array('2' => 2, '3' => 3, '4' => 4, '5' => 9, '6' => 10,
  124. '7' => 13, '8' => 14, '9' => 15, '10' => 16,
  125. 'ARABIC' => 6, 'CYRILLIC' => 5, 'GREEK' => 7, 'GREEK1' => 7, 'HEBREW' => 8
  126. );
  127. // some clients sends windows-1252 text as latin1,
  128. // it is safe to use windows-1252 for all latin1
  129. if ($m[1] == 1) {
  130. $result = 'WINDOWS-1252';
  131. }
  132. // if iconv is not supported we need ISO labels, it's also safe for iconv
  133. else if (!empty($aliases[$m[1]])) {
  134. $result = 'ISO-8859-'.$aliases[$m[1]];
  135. }
  136. // iconv requires convertion of e.g. LATIN-1 to LATIN1
  137. else {
  138. $result = $str;
  139. }
  140. }
  141. else {
  142. $result = $charset;
  143. }
  144. $charsets[$input] = $result;
  145. return $result;
  146. }
  147. /**
  148. * Convert a string from one charset to another.
  149. * Uses mbstring and iconv functions if possible
  150. *
  151. * @param string Input string
  152. * @param string Suspected charset of the input string
  153. * @param string Target charset to convert to; defaults to RCUBE_CHARSET
  154. *
  155. * @return string Converted string
  156. */
  157. public static function convert($str, $from, $to = null)
  158. {
  159. static $iconv_options = null;
  160. static $mbstring_list = null;
  161. static $mbstring_sch = null;
  162. static $conv = null;
  163. $to = empty($to) ? RCUBE_CHARSET : $to;
  164. $from = self::parse_charset($from);
  165. // It is a common case when UTF-16 charset is used with US-ASCII content (#1488654)
  166. // In that case we can just skip the conversion (use UTF-8)
  167. if ($from == 'UTF-16' && !preg_match('/[^\x00-\x7F]/', $str)) {
  168. $from = 'UTF-8';
  169. }
  170. if ($from == $to || empty($str) || empty($from)) {
  171. return $str;
  172. }
  173. if ($iconv_options === null) {
  174. if (function_exists('iconv')) {
  175. // ignore characters not available in output charset
  176. $iconv_options = '//IGNORE';
  177. if (iconv('', $iconv_options, '') === false) {
  178. // iconv implementation does not support options
  179. $iconv_options = '';
  180. }
  181. }
  182. else {
  183. $iconv_options = false;
  184. }
  185. }
  186. // convert charset using iconv module
  187. if ($iconv_options !== false && $from != 'UTF7-IMAP' && $to != 'UTF7-IMAP') {
  188. // throw an exception if iconv reports an illegal character in input
  189. // it means that input string has been truncated
  190. set_error_handler(array('rcube_charset', 'error_handler'), E_NOTICE);
  191. try {
  192. $_iconv = iconv($from, $to . $iconv_options, $str);
  193. } catch (ErrorException $e) {
  194. $_iconv = false;
  195. }
  196. restore_error_handler();
  197. if ($_iconv !== false) {
  198. return $_iconv;
  199. }
  200. }
  201. if ($mbstring_list === null) {
  202. if (extension_loaded('mbstring')) {
  203. $mbstring_sch = mb_substitute_character();
  204. $mbstring_list = mb_list_encodings();
  205. $mbstring_list = array_map('strtoupper', $mbstring_list);
  206. }
  207. else {
  208. $mbstring_list = false;
  209. }
  210. }
  211. // convert charset using mbstring module
  212. if ($mbstring_list !== false) {
  213. $aliases['WINDOWS-1257'] = 'ISO-8859-13';
  214. // it happens that mbstring supports ASCII but not US-ASCII
  215. if (($from == 'US-ASCII' || $to == 'US-ASCII') && !in_array('US-ASCII', $mbstring_list)) {
  216. $aliases['US-ASCII'] = 'ASCII';
  217. }
  218. $mb_from = $aliases[$from] ? $aliases[$from] : $from;
  219. $mb_to = $aliases[$to] ? $aliases[$to] : $to;
  220. // return if encoding found, string matches encoding and convert succeeded
  221. if (in_array($mb_from, $mbstring_list) && in_array($mb_to, $mbstring_list)) {
  222. if (mb_check_encoding($str, $mb_from)) {
  223. // Do the same as //IGNORE with iconv
  224. mb_substitute_character('none');
  225. $out = mb_convert_encoding($str, $mb_to, $mb_from);
  226. mb_substitute_character($mbstring_sch);
  227. if ($out !== false) {
  228. return $out;
  229. }
  230. }
  231. }
  232. }
  233. // convert charset using bundled classes/functions
  234. if ($to == 'UTF-8') {
  235. if ($from == 'UTF7-IMAP') {
  236. if ($_str = self::utf7imap_to_utf8($str)) {
  237. return $_str;
  238. }
  239. }
  240. else if ($from == 'UTF-7') {
  241. if ($_str = self::utf7_to_utf8($str)) {
  242. return $_str;
  243. }
  244. }
  245. else if ($from == 'ISO-8859-1' && function_exists('utf8_encode')) {
  246. return utf8_encode($str);
  247. }
  248. else if (class_exists('utf8')) {
  249. if (!$conv) {
  250. $conv = new utf8($from);
  251. }
  252. else {
  253. $conv->loadCharset($from);
  254. }
  255. if ($_str = $conv->strToUtf8($str)) {
  256. return $_str;
  257. }
  258. }
  259. }
  260. // encode string for output
  261. if ($from == 'UTF-8') {
  262. // @TODO: we need a function for UTF-7 (RFC2152) conversion
  263. if ($to == 'UTF7-IMAP' || $to == 'UTF-7') {
  264. if ($_str = self::utf8_to_utf7imap($str)) {
  265. return $_str;
  266. }
  267. }
  268. else if ($to == 'ISO-8859-1' && function_exists('utf8_decode')) {
  269. return utf8_decode($str);
  270. }
  271. else if (class_exists('utf8')) {
  272. if (!$conv) {
  273. $conv = new utf8($to);
  274. }
  275. else {
  276. $conv->loadCharset($from);
  277. }
  278. if ($_str = $conv->strToUtf8($str)) {
  279. return $_str;
  280. }
  281. }
  282. }
  283. // return original string
  284. return $str;
  285. }
  286. /**
  287. * Converts string from standard UTF-7 (RFC 2152) to UTF-8.
  288. *
  289. * @param string Input string (UTF-7)
  290. *
  291. * @return string Converted string (UTF-8)
  292. */
  293. public static function utf7_to_utf8($str)
  294. {
  295. $Index_64 = array(
  296. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
  297. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
  298. 0,0,0,0, 0,0,0,0, 0,0,0,1, 0,0,0,0,
  299. 1,1,1,1, 1,1,1,1, 1,1,0,0, 0,0,0,0,
  300. 0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
  301. 1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,0,
  302. 0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
  303. 1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,0,
  304. );
  305. $u7len = strlen($str);
  306. $str = strval($str);
  307. $res = '';
  308. for ($i=0; $u7len > 0; $i++, $u7len--) {
  309. $u7 = $str[$i];
  310. if ($u7 == '+') {
  311. $i++;
  312. $u7len--;
  313. $ch = '';
  314. for (; $u7len > 0; $i++, $u7len--) {
  315. $u7 = $str[$i];
  316. if (!$Index_64[ord($u7)]) {
  317. break;
  318. }
  319. $ch .= $u7;
  320. }
  321. if ($ch == '') {
  322. if ($u7 == '-') {
  323. $res .= '+';
  324. }
  325. continue;
  326. }
  327. $res .= self::utf16_to_utf8(base64_decode($ch));
  328. }
  329. else {
  330. $res .= $u7;
  331. }
  332. }
  333. return $res;
  334. }
  335. /**
  336. * Converts string from UTF-16 to UTF-8 (helper for utf-7 to utf-8 conversion)
  337. *
  338. * @param string Input string
  339. *
  340. * @return string The converted string
  341. */
  342. public static function utf16_to_utf8($str)
  343. {
  344. $len = strlen($str);
  345. $dec = '';
  346. for ($i = 0; $i < $len; $i += 2) {
  347. $c = ord($str[$i]) << 8 | ord($str[$i + 1]);
  348. if ($c >= 0x0001 && $c <= 0x007F) {
  349. $dec .= chr($c);
  350. }
  351. else if ($c > 0x07FF) {
  352. $dec .= chr(0xE0 | (($c >> 12) & 0x0F));
  353. $dec .= chr(0x80 | (($c >> 6) & 0x3F));
  354. $dec .= chr(0x80 | (($c >> 0) & 0x3F));
  355. }
  356. else {
  357. $dec .= chr(0xC0 | (($c >> 6) & 0x1F));
  358. $dec .= chr(0x80 | (($c >> 0) & 0x3F));
  359. }
  360. }
  361. return $dec;
  362. }
  363. /**
  364. * Convert the data ($str) from RFC 2060's UTF-7 to UTF-8.
  365. * If input data is invalid, return the original input string.
  366. * RFC 2060 obviously intends the encoding to be unique (see
  367. * point 5 in section 5.1.3), so we reject any non-canonical
  368. * form, such as &ACY- (instead of &-) or &AMA-&AMA- (instead
  369. * of &AMAAwA-).
  370. *
  371. * Translated from C to PHP by Thomas Bruederli <roundcube@gmail.com>
  372. *
  373. * @param string $str Input string (UTF7-IMAP)
  374. *
  375. * @return string Output string (UTF-8)
  376. */
  377. public static function utf7imap_to_utf8($str)
  378. {
  379. $Index_64 = array(
  380. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  381. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  382. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, 63,-1,-1,-1,
  383. 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
  384. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  385. 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
  386. -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  387. 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
  388. );
  389. $u7len = strlen($str);
  390. $str = strval($str);
  391. $p = '';
  392. $err = '';
  393. for ($i=0; $u7len > 0; $i++, $u7len--) {
  394. $u7 = $str[$i];
  395. if ($u7 == '&') {
  396. $i++;
  397. $u7len--;
  398. $u7 = $str[$i];
  399. if ($u7len && $u7 == '-') {
  400. $p .= '&';
  401. continue;
  402. }
  403. $ch = 0;
  404. $k = 10;
  405. for (; $u7len > 0; $i++, $u7len--) {
  406. $u7 = $str[$i];
  407. if ((ord($u7) & 0x80) || ($b = $Index_64[ord($u7)]) == -1) {
  408. break;
  409. }
  410. if ($k > 0) {
  411. $ch |= $b << $k;
  412. $k -= 6;
  413. }
  414. else {
  415. $ch |= $b >> (-$k);
  416. if ($ch < 0x80) {
  417. // Printable US-ASCII
  418. if (0x20 <= $ch && $ch < 0x7f) {
  419. return $err;
  420. }
  421. $p .= chr($ch);
  422. }
  423. else if ($ch < 0x800) {
  424. $p .= chr(0xc0 | ($ch >> 6));
  425. $p .= chr(0x80 | ($ch & 0x3f));
  426. }
  427. else {
  428. $p .= chr(0xe0 | ($ch >> 12));
  429. $p .= chr(0x80 | (($ch >> 6) & 0x3f));
  430. $p .= chr(0x80 | ($ch & 0x3f));
  431. }
  432. $ch = ($b << (16 + $k)) & 0xffff;
  433. $k += 10;
  434. }
  435. }
  436. // Non-zero or too many extra bits
  437. if ($ch || $k < 6) {
  438. return $err;
  439. }
  440. // BASE64 not properly terminated
  441. if (!$u7len || $u7 != '-') {
  442. return $err;
  443. }
  444. // Adjacent BASE64 sections
  445. if ($u7len > 2 && $str[$i+1] == '&' && $str[$i+2] != '-') {
  446. return $err;
  447. }
  448. }
  449. // Not printable US-ASCII
  450. else if (ord($u7) < 0x20 || ord($u7) >= 0x7f) {
  451. return $err;
  452. }
  453. else {
  454. $p .= $u7;
  455. }
  456. }
  457. return $p;
  458. }
  459. /**
  460. * Convert the data ($str) from UTF-8 to RFC 2060's UTF-7.
  461. * Unicode characters above U+FFFF are replaced by U+FFFE.
  462. * If input data is invalid, return an empty string.
  463. *
  464. * Translated from C to PHP by Thomas Bruederli <roundcube@gmail.com>
  465. *
  466. * @param string $str Input string (UTF-8)
  467. *
  468. * @return string Output string (UTF7-IMAP)
  469. */
  470. public static function utf8_to_utf7imap($str)
  471. {
  472. $B64Chars = array(
  473. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  474. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  475. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
  476. 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  477. '8', '9', '+', ','
  478. );
  479. $u8len = strlen($str);
  480. $base64 = 0;
  481. $i = 0;
  482. $p = '';
  483. $err = '';
  484. while ($u8len) {
  485. $u8 = $str[$i];
  486. $c = ord($u8);
  487. if ($c < 0x80) {
  488. $ch = $c;
  489. $n = 0;
  490. }
  491. else if ($c < 0xc2) {
  492. return $err;
  493. }
  494. else if ($c < 0xe0) {
  495. $ch = $c & 0x1f;
  496. $n = 1;
  497. }
  498. else if ($c < 0xf0) {
  499. $ch = $c & 0x0f;
  500. $n = 2;
  501. }
  502. else if ($c < 0xf8) {
  503. $ch = $c & 0x07;
  504. $n = 3;
  505. }
  506. else if ($c < 0xfc) {
  507. $ch = $c & 0x03;
  508. $n = 4;
  509. }
  510. else if ($c < 0xfe) {
  511. $ch = $c & 0x01;
  512. $n = 5;
  513. }
  514. else {
  515. return $err;
  516. }
  517. $i++;
  518. $u8len--;
  519. if ($n > $u8len) {
  520. return $err;
  521. }
  522. for ($j=0; $j < $n; $j++) {
  523. $o = ord($str[$i+$j]);
  524. if (($o & 0xc0) != 0x80) {
  525. return $err;
  526. }
  527. $ch = ($ch << 6) | ($o & 0x3f);
  528. }
  529. if ($n > 1 && !($ch >> ($n * 5 + 1))) {
  530. return $err;
  531. }
  532. $i += $n;
  533. $u8len -= $n;
  534. if ($ch < 0x20 || $ch >= 0x7f) {
  535. if (!$base64) {
  536. $p .= '&';
  537. $base64 = 1;
  538. $b = 0;
  539. $k = 10;
  540. }
  541. if ($ch & ~0xffff) {
  542. $ch = 0xfffe;
  543. }
  544. $p .= $B64Chars[($b | $ch >> $k)];
  545. $k -= 6;
  546. for (; $k >= 0; $k -= 6) {
  547. $p .= $B64Chars[(($ch >> $k) & 0x3f)];
  548. }
  549. $b = ($ch << (-$k)) & 0x3f;
  550. $k += 16;
  551. }
  552. else {
  553. if ($base64) {
  554. if ($k > 10) {
  555. $p .= $B64Chars[$b];
  556. }
  557. $p .= '-';
  558. $base64 = 0;
  559. }
  560. $p .= chr($ch);
  561. if (chr($ch) == '&') {
  562. $p .= '-';
  563. }
  564. }
  565. }
  566. if ($base64) {
  567. if ($k > 10) {
  568. $p .= $B64Chars[$b];
  569. }
  570. $p .= '-';
  571. }
  572. return $p;
  573. }
  574. /**
  575. * A method to guess character set of a string.
  576. *
  577. * @param string $string String
  578. * @param string $failover Default result for failover
  579. * @param string $language User language
  580. *
  581. * @return string Charset name
  582. */
  583. public static function detect($string, $failover = null, $language = null)
  584. {
  585. if (substr($string, 0, 4) == "\0\0\xFE\xFF") return 'UTF-32BE'; // Big Endian
  586. if (substr($string, 0, 4) == "\xFF\xFE\0\0") return 'UTF-32LE'; // Little Endian
  587. if (substr($string, 0, 2) == "\xFE\xFF") return 'UTF-16BE'; // Big Endian
  588. if (substr($string, 0, 2) == "\xFF\xFE") return 'UTF-16LE'; // Little Endian
  589. if (substr($string, 0, 3) == "\xEF\xBB\xBF") return 'UTF-8';
  590. // heuristics
  591. if ($string[0] == "\0" && $string[1] == "\0" && $string[2] == "\0" && $string[3] != "\0") return 'UTF-32BE';
  592. if ($string[0] != "\0" && $string[1] == "\0" && $string[2] == "\0" && $string[3] == "\0") return 'UTF-32LE';
  593. if ($string[0] == "\0" && $string[1] != "\0" && $string[2] == "\0" && $string[3] != "\0") return 'UTF-16BE';
  594. if ($string[0] != "\0" && $string[1] == "\0" && $string[2] != "\0" && $string[3] == "\0") return 'UTF-16LE';
  595. if (function_exists('mb_detect_encoding')) {
  596. if (empty($language)) {
  597. $rcube = rcube::get_instance();
  598. $language = $rcube->get_user_language();
  599. }
  600. // Prioritize charsets according to current language (#1485669)
  601. switch ($language) {
  602. case 'ja_JP':
  603. $prio = array('ISO-2022-JP', 'JIS', 'UTF-8', 'EUC-JP', 'eucJP-win', 'SJIS', 'SJIS-win');
  604. break;
  605. case 'zh_CN':
  606. case 'zh_TW':
  607. $prio = array('UTF-8', 'BIG-5', 'GB2312', 'EUC-TW');
  608. break;
  609. case 'ko_KR':
  610. $prio = array('UTF-8', 'EUC-KR', 'ISO-2022-KR');
  611. break;
  612. case 'ru_RU':
  613. $prio = array('UTF-8', 'WINDOWS-1251', 'KOI8-R');
  614. break;
  615. case 'tr_TR':
  616. $prio = array('UTF-8', 'ISO-8859-9', 'WINDOWS-1254');
  617. break;
  618. default:
  619. $prio = array('UTF-8', 'SJIS', 'GB2312',
  620. 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4',
  621. 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9',
  622. 'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16',
  623. 'WINDOWS-1252', 'WINDOWS-1251', 'EUC-JP', 'EUC-TW', 'KOI8-R', 'BIG-5',
  624. 'ISO-2022-KR', 'ISO-2022-JP',
  625. );
  626. }
  627. $encodings = array_unique(array_merge($prio, mb_list_encodings()));
  628. return mb_detect_encoding($string, $encodings);
  629. }
  630. // No match, check for UTF-8
  631. // from http://w3.org/International/questions/qa-forms-utf-8.html
  632. if (preg_match('/\A(
  633. [\x09\x0A\x0D\x20-\x7E]
  634. | [\xC2-\xDF][\x80-\xBF]
  635. | \xE0[\xA0-\xBF][\x80-\xBF]
  636. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}
  637. | \xED[\x80-\x9F][\x80-\xBF]
  638. | \xF0[\x90-\xBF][\x80-\xBF]{2}
  639. | [\xF1-\xF3][\x80-\xBF]{3}
  640. | \xF4[\x80-\x8F][\x80-\xBF]{2}
  641. )*\z/xs', substr($string, 0, 2048))
  642. ) {
  643. return 'UTF-8';
  644. }
  645. return $failover;
  646. }
  647. /**
  648. * Removes non-unicode characters from input.
  649. *
  650. * @param mixed $input String or array.
  651. *
  652. * @return mixed String or array
  653. */
  654. public static function clean($input)
  655. {
  656. // handle input of type array
  657. if (is_array($input)) {
  658. foreach ($input as $idx => $val) {
  659. $input[$idx] = self::clean($val);
  660. }
  661. return $input;
  662. }
  663. if (!is_string($input) || $input == '') {
  664. return $input;
  665. }
  666. // iconv/mbstring are much faster (especially with long strings)
  667. if (function_exists('mb_convert_encoding')) {
  668. if (($res = mb_convert_encoding($input, 'UTF-8', 'UTF-8')) !== false) {
  669. return $res;
  670. }
  671. }
  672. if (function_exists('iconv')) {
  673. if (($res = @iconv('UTF-8', 'UTF-8//IGNORE', $input)) !== false) {
  674. return $res;
  675. }
  676. }
  677. $seq = '';
  678. $out = '';
  679. $regexp = '/^('.
  680. // '[\x00-\x7F]'. // UTF8-1
  681. '|[\xC2-\xDF][\x80-\xBF]'. // UTF8-2
  682. '|\xE0[\xA0-\xBF][\x80-\xBF]'. // UTF8-3
  683. '|[\xE1-\xEC][\x80-\xBF][\x80-\xBF]'. // UTF8-3
  684. '|\xED[\x80-\x9F][\x80-\xBF]'. // UTF8-3
  685. '|[\xEE-\xEF][\x80-\xBF][\x80-\xBF]'. // UTF8-3
  686. '|\xF0[\x90-\xBF][\x80-\xBF][\x80-\xBF]'. // UTF8-4
  687. '|[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]'.// UTF8-4
  688. '|\xF4[\x80-\x8F][\x80-\xBF][\x80-\xBF]'. // UTF8-4
  689. ')$/';
  690. for ($i = 0, $len = strlen($input); $i < $len; $i++) {
  691. $chr = $input[$i];
  692. $ord = ord($chr);
  693. // 1-byte character
  694. if ($ord <= 0x7F) {
  695. if ($seq) {
  696. $out .= preg_match($regexp, $seq) ? $seq : '';
  697. }
  698. $seq = '';
  699. $out .= $chr;
  700. // first (or second) byte of multibyte sequence
  701. }
  702. else if ($ord >= 0xC0) {
  703. if (strlen($seq) > 1) {
  704. $out .= preg_match($regexp, $seq) ? $seq : '';
  705. $seq = '';
  706. }
  707. else if ($seq && ord($seq) < 0xC0) {
  708. $seq = '';
  709. }
  710. $seq .= $chr;
  711. // next byte of multibyte sequence
  712. }
  713. else if ($seq) {
  714. $seq .= $chr;
  715. }
  716. }
  717. if ($seq) {
  718. $out .= preg_match($regexp, $seq) ? $seq : '';
  719. }
  720. return $out;
  721. }
  722. }