From 35882c06953b4b6fa359819de114e79ae14c365b Mon Sep 17 00:00:00 2001 From: Peter Rotich <peter@osticket.com> Date: Mon, 10 Feb 2014 22:04:08 +0000 Subject: [PATCH] Assume ISO-8859-1 when charset is unknown or bogus Unfortunately, this is necessary because some mail clients send in content without defining the charset or at worse using freaking wrong/invalid charsets like 'default'! --- include/class.format.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/include/class.format.php b/include/class.format.php index 8ee72d8d3..2a9934a6d 100644 --- a/include/class.format.php +++ b/include/class.format.php @@ -34,21 +34,24 @@ class Format { function encode($text, $charset=null, $encoding='utf-8') { //Try auto-detecting charset/encoding - if(!$charset && function_exists('mb_detect_encoding')) + if (!$charset && function_exists('mb_detect_encoding')) $charset = mb_detect_encoding($text); // Cleanup - incorrect, bogus, or ambiguous charsets - if($charset && in_array(strtolower(trim($charset)), + // ISO-8859-1 is assumed for empty charset. + if (!$charset || in_array(strtolower(trim($charset)), array('default','x-user-defined','iso','us-ascii'))) $charset = 'ISO-8859-1'; $original = $text; - if(function_exists('iconv') && $charset) + if (function_exists('iconv')) $text = iconv($charset, $encoding.'//IGNORE', $text); - elseif(function_exists('mb_convert_encoding') && $charset && $encoding) + elseif (function_exists('mb_convert_encoding')) $text = mb_convert_encoding($text, $encoding, $charset); - elseif(!strcasecmp($encoding, 'utf-8')) //forced blind utf8 encoding. - $text = function_exists('imap_utf8')?imap_utf8($text):utf8_encode($text); + elseif (!strcasecmp($encoding, 'utf-8') + && function_exists('utf8_encode') + && !strcasecmp($charset, 'ISO-8859-1')) + $text = utf8_encode($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) @@ -176,7 +179,7 @@ class Format { if (isset($attributes['style'])) { $styles = explode(';', $attributes['style']); foreach ($styles as $i=>$s) { - list($prop, $val) = explode(':', $s); + @list($prop, $val) = explode(':', $s); if (!$val || !$prop || $prop[0] == '-') unset($styles[$i]); } -- GitLab