From 0c8419e31c399fde0556ff24835efd3db03b4dd7 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Tue, 27 Sep 2016 17:03:02 +0200 Subject: [PATCH] Simplify mbstring code path in rcube_charset::convert() --- program/lib/Roundcube/rcube_charset.php | 58 ++++++++++--------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/program/lib/Roundcube/rcube_charset.php b/program/lib/Roundcube/rcube_charset.php index 2d6d9d355..6bb00e55b 100644 --- a/program/lib/Roundcube/rcube_charset.php +++ b/program/lib/Roundcube/rcube_charset.php @@ -171,8 +171,7 @@ class rcube_charset public static function convert($str, $from, $to = null) { static $iconv_options = null; - static $mbstring_list = null; - static $mbstring_sch = null; + static $mbstring_sc = null; $to = empty($to) ? RCUBE_CHARSET : strtoupper($to); $from = self::parse_charset($from); @@ -219,49 +218,38 @@ class rcube_charset } } - if ($mbstring_list === null) { - if (extension_loaded('mbstring')) { - $mbstring_sch = mb_substitute_character(); - $mbstring_list = mb_list_encodings(); - $mbstring_list = array_map('strtoupper', $mbstring_list); - } - else { - $mbstring_list = false; - } + if ($mbstring_sc === null) { + $mbstring_sc = extension_loaded('mbstring') ? mb_substitute_character() : false; } // convert charset using mbstring module - if ($mbstring_list !== false) { - $aliases['WINDOWS-1257'] = 'ISO-8859-13'; - // it happens that mbstring supports ASCII but not US-ASCII - if (($from == 'US-ASCII' || $to == 'US-ASCII') && !in_array('US-ASCII', $mbstring_list)) { - $aliases['US-ASCII'] = 'ASCII'; - } + if ($mbstring_sc !== false) { + $aliases = array( + 'WINDOWS-1257' => 'ISO-8859-13', + 'US-ASCII' => 'ASCII', + ); $mb_from = $aliases[$from] ?: $from; $mb_to = $aliases[$to] ?: $to; - // return if encoding found, string matches encoding and convert succeeded - if (in_array($mb_from, $mbstring_list) && in_array($mb_to, $mbstring_list)) { - // Do the same as //IGNORE with iconv - mb_substitute_character('none'); + // Do the same as //IGNORE with iconv + mb_substitute_character('none'); - // throw an exception if mbstring reports an illegal character in input - // using mb_check_encoding() is much slower - set_error_handler(array('rcube_charset', 'error_handler'), E_WARNING); - try { - $out = mb_convert_encoding($str, $mb_to, $mb_from); - } - catch (ErrorException $e) { - $out = false; - } - restore_error_handler(); + // throw an exception if mbstring reports an illegal character in input + // using mb_check_encoding() is much slower + set_error_handler(array('rcube_charset', 'error_handler'), E_WARNING); + try { + $out = mb_convert_encoding($str, $mb_to, $mb_from); + } + catch (ErrorException $e) { + $out = false; + } + restore_error_handler(); - mb_substitute_character($mbstring_sch); + mb_substitute_character($mbstring_sc); - if ($out !== false) { - return $out; - } + if ($out !== false) { + return $out; } }