Fix convertion to/from US-ASCII using mbstring

Define mbstring substitute_character to work as iconv //IGNORE
pull/29/merge
Aleksander Machniak 12 years ago
parent 40e1c2d14d
commit bc1e4faa70

@ -176,6 +176,7 @@ class rcube_charset
{ {
static $iconv_options = null; static $iconv_options = null;
static $mbstring_list = null; static $mbstring_list = null;
static $mbstring_sch = null;
static $conv = null; static $conv = null;
$to = empty($to) ? strtoupper(RCMAIL_CHARSET) : $to; $to = empty($to) ? strtoupper(RCMAIL_CHARSET) : $to;
@ -221,6 +222,7 @@ class rcube_charset
if ($mbstring_list === null) { if ($mbstring_list === null) {
if (extension_loaded('mbstring')) { if (extension_loaded('mbstring')) {
$mbstring_sch = mb_substitute_character();
$mbstring_list = mb_list_encodings(); $mbstring_list = mb_list_encodings();
$mbstring_list = array_map('strtoupper', $mbstring_list); $mbstring_list = array_map('strtoupper', $mbstring_list);
} }
@ -229,17 +231,28 @@ class rcube_charset
// convert charset using mbstring module // convert charset using mbstring module
if ($mbstring_list !== null) { if ($mbstring_list !== null) {
$aliases['WINDOWS-1257'] = 'ISO-8859-13'; $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';
}
$mb_from = $aliases[$from] ? $aliases[$from] : $from; $mb_from = $aliases[$from] ? $aliases[$from] : $from;
$mb_to = $aliases[$to] ? $aliases[$to] : $to; $mb_to = $aliases[$to] ? $aliases[$to] : $to;
// return if encoding found, string matches encoding and convert succeeded // return if encoding found, string matches encoding and convert succeeded
if (in_array($mb_from, $mbstring_list) && in_array($mb_to, $mbstring_list)) { if (in_array($mb_from, $mbstring_list) && in_array($mb_to, $mbstring_list)) {
if (mb_check_encoding($str, $mb_from) && ($out = mb_convert_encoding($str, $mb_to, $mb_from))) { if (mb_check_encoding($str, $mb_from)) {
// Do the same as //IGNORE with iconv
mb_substitute_character('none');
$out = mb_convert_encoding($str, $mb_to, $mb_from);
mb_substitute_character($mbstring_sch);
if ($out !== false) {
return $out; return $out;
} }
} }
} }
}
// convert charset using bundled classes/functions // convert charset using bundled classes/functions
if ($to == 'UTF-8') { if ($to == 'UTF-8') {

Loading…
Cancel
Save