From d0fd9da592dc5d599450c3f072e844edfba7d1aa Mon Sep 17 00:00:00 2001 From: beskhu Date: Sat, 3 Mar 2018 04:26:04 +0100 Subject: [PATCH 1/2] correcting encodings detection --- src/Fetch/Message.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index e382678..ed897c2 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -523,7 +523,7 @@ protected function processStructure($structure, $partIdentifier = null) if (!empty($parameters['charset']) && $parameters['charset'] !== self::$charset) { $mb_converted = false; if (function_exists('mb_convert_encoding')) { - if (!in_array($parameters['charset'], mb_list_encodings())) { + if (!in_array(strtolower($parameters['charset']), array_map("strtolower", mb_list_encodings()))) { if ($structure->encoding === 0) { $parameters['charset'] = 'US-ASCII'; } else { @@ -531,11 +531,11 @@ protected function processStructure($structure, $partIdentifier = null) } } - $messageBody = @mb_convert_encoding($messageBody, self::$charset, $parameters['charset']); + $messageBody = @mb_convert_encoding($messageBody, self::$charset, strtoupper($parameters['charset'])); $mb_converted = true; } if (!$mb_converted) { - $messageBodyConv = @iconv($parameters['charset'], self::$charset . self::$charsetFlag, $messageBody); + $messageBodyConv = @iconv(strtoupper($parameters['charset']), self::$charset . self::$charsetFlag, $messageBody); if ($messageBodyConv !== false) { $messageBody = $messageBodyConv; From 8b9f05ca880eb766626e42e06e46399ba300109c Mon Sep 17 00:00:00 2001 From: beskhu Date: Sat, 3 Mar 2018 04:28:02 +0100 Subject: [PATCH 2/2] encoding detection for badly declared charset that can be detected --- src/Fetch/MIME.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Fetch/MIME.php b/src/Fetch/MIME.php index b63d72b..8f0c328 100644 --- a/src/Fetch/MIME.php +++ b/src/Fetch/MIME.php @@ -36,7 +36,9 @@ public static function decode($text, $targetCharset = 'utf-8') foreach (imap_mime_header_decode($text) as $word) { $ch = 'default' === $word->charset ? 'ascii' : $word->charset; - + if ($ch==="ascii" && ($c=strtoupper(mb_detect_encoding($word->text)))!==strtoupper($ch)) { + $ch=$c; + } $result .= iconv($ch, $targetCharset, $word->text); }