diff options
author | Aleksander Machniak <alec@alec.pl> | 2014-11-07 11:43:42 +0100 |
---|---|---|
committer | Aleksander Machniak <alec@alec.pl> | 2014-11-07 11:43:42 +0100 |
commit | a7a778c157426f12f176648802a51c5e5397a81a (patch) | |
tree | 273248415c4bc9bd289543985f334d8e34ba48f9 /program/lib | |
parent | 9ba5b878d10818d58b7b99bc7d8404a08ac2e7c0 (diff) |
Fix some character sets detection (#1490135)
Diffstat (limited to 'program/lib')
-rw-r--r-- | program/lib/Roundcube/rcube_charset.php | 63 |
1 files changed, 38 insertions, 25 deletions
diff --git a/program/lib/Roundcube/rcube_charset.php b/program/lib/Roundcube/rcube_charset.php index d6ca3c052..3e2dac19c 100644 --- a/program/lib/Roundcube/rcube_charset.php +++ b/program/lib/Roundcube/rcube_charset.php @@ -654,36 +654,47 @@ class rcube_charset if ($string[0] == "\0" && $string[1] != "\0" && $string[2] == "\0" && $string[3] != "\0") return 'UTF-16BE'; if ($string[0] != "\0" && $string[1] == "\0" && $string[2] != "\0" && $string[3] == "\0") return 'UTF-16LE'; - if (function_exists('mb_detect_encoding')) { - if (empty($language)) { - $rcube = rcube::get_instance(); - $language = $rcube->get_user_language(); - } + if (empty($language)) { + $rcube = rcube::get_instance(); + $language = $rcube->get_user_language(); + } - // Prioritize charsets according to current language (#1485669) - switch ($language) { - case 'ja_JP': - $prio = array('ISO-2022-JP', 'JIS', 'UTF-8', 'EUC-JP', 'eucJP-win', 'SJIS', 'SJIS-win'); - break; + // Prioritize charsets according to current language (#1485669) + switch ($language) { + case 'ja_JP': + $prio = array('ISO-2022-JP', 'JIS', 'UTF-8', 'EUC-JP', 'eucJP-win', 'SJIS', 'SJIS-win'); + break; - case 'zh_CN': - case 'zh_TW': - $prio = array('UTF-8', 'BIG-5', 'GB2312', 'EUC-TW'); - break; + case 'zh_CN': + case 'zh_TW': + $prio = array('UTF-8', 'BIG-5', 'GB2312', 'EUC-TW'); + break; - case 'ko_KR': - $prio = array('UTF-8', 'EUC-KR', 'ISO-2022-KR'); - break; + case 'ko_KR': + $prio = array('UTF-8', 'EUC-KR', 'ISO-2022-KR'); + break; - case 'ru_RU': - $prio = array('UTF-8', 'WINDOWS-1251', 'KOI8-R'); - break; + case 'ru_RU': + $prio = array('UTF-8', 'WINDOWS-1251', 'KOI8-R'); + break; + + case 'tr_TR': + $prio = array('UTF-8', 'ISO-8859-9', 'WINDOWS-1254'); + break; + } - case 'tr_TR': - $prio = array('UTF-8', 'ISO-8859-9', 'WINDOWS-1254'); - break; + // mb_detect_encoding() is not reliable for some charsets (#1490135) + // use mb_check_encoding() to make charset priority lists really working + if ($prio && function_exists('mb_check_encoding')) { + foreach ($prio as $encoding) { + if (mb_check_encoding($string, $encoding)) { + return $encoding; + } + } + } - default: + if (function_exists('mb_detect_encoding')) { + if (!$prio) { $prio = array('UTF-8', 'SJIS', 'GB2312', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', @@ -695,7 +706,9 @@ class rcube_charset $encodings = array_unique(array_merge($prio, mb_list_encodings())); - return mb_detect_encoding($string, $encodings); + if ($encoding = mb_detect_encoding($string, $encodings)) { + return $encoding; + } } // No match, check for UTF-8 |