Simplify mbstring code path in rcube_charset::convert()

pull/5754/head
Aleksander Machniak 8 years ago
parent af4ddddb21
commit 0c8419e31c

@ -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;
}
}

Loading…
Cancel
Save