Skip to content
Snippets Groups Projects
Commit 795fcca0 authored by Jared Hancock's avatar Jared Hancock
Browse files

Use a failsafe encoding for unrecognized charsets

If the body of an email message is written and encoded with an unrecognized
charset (like 'iso', for instance), then use the original text, assumed to
be 8-bit encoded. This patch will create idiosyncrasies, where text might be
transcoded to utf-8 incorrectly, but it should eliminate instances where
email message bodies are missing due to incorrect charset labels.
parent fdfba0ec
Branches
Tags
No related merge requests found
...@@ -41,18 +41,23 @@ class Format { ...@@ -41,18 +41,23 @@ class Format {
if(!$charset && function_exists('mb_detect_encoding')) if(!$charset && function_exists('mb_detect_encoding'))
$charset = mb_detect_encoding($text); $charset = mb_detect_encoding($text);
//Cleanup - junk // Cleanup - incorrect, bogus, or ambiguous charsets
if($charset && in_array(trim($charset), array('default','x-user-defined'))) if($charset && in_array(strtolower(trim($charset)),
array('default','x-user-defined','iso')))
$charset = 'ISO-8859-1'; $charset = 'ISO-8859-1';
$original = $text;
if(function_exists('iconv') && $charset) if(function_exists('iconv') && $charset)
$text = iconv($charset, $encoding.'//IGNORE', $text); $text = iconv($charset, $encoding.'//IGNORE', $text);
elseif(function_exists('mb_convert_encoding') && $charset && $encoding) elseif(function_exists('mb_convert_encoding') && $charset && $encoding)
$text = mb_convert_encoding($text, $encoding, $charset); $text = mb_convert_encoding($text, $encoding, $charset);
elseif(!strcasecmp($encoding, 'utf-8')) //forced blind utf8 encoding. elseif(!strcasecmp($encoding, 'utf-8')) //forced blind utf8 encoding.
$text = function_exists('imap_utf8')?imap_utf8($text):utf8_encode($text); $text = function_exists('imap_utf8')?imap_utf8($text):utf8_encode($text);
return $text; // If $text is false, then we have a (likely) invalid charset, use
// the original text and assume 8-bit (latin-1 / iso-8859-1)
// encoding
return (!$text && $original) ? $original : $text;
} }
//Wrapper for utf-8 encoding. //Wrapper for utf-8 encoding.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment