Fetch icon indicating copy to clipboard operation
Fetch copied to clipboard

two issues found and corrected

Open beskhu opened this issue 8 years ago • 1 comments

I found a bug on "processStructure" in the class Message Some charsets are not declared with caps, which can lead to improper check by the function in_array on mb_list_encodings()... This can be corrected using strtoupper :

            if (function_exists('mb_convert_encoding')) {
                if (!in_array(strtoupper($parameters['charset']), mb_list_encodings())) {
                    if ($structure->encoding === 0) {
                        $parameters['charset'] = 'US-ASCII';
                    } else {
                        $parameters['charset'] = 'UTF-8';
                    }
                }

                $messageBody = @mb_convert_encoding($messageBody, self::$charset, $parameters['charset']);
                $mb_converted = true;
            }

I found an improvement on "decode" in the class MIME... Sometimes the charset in subjects are badly declared and wrongly interpreted as ASCII, and conversion can be handled doing like this :

public static function decode($text, $targetCharset = 'UTF-8')
{
    if (null === $text) {
        return null;
    }

    $result = '';

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

    return $result;
}

beskhu avatar Feb 21 '18 22:02 beskhu

Hello again, in_array detection would be even better with

if (in_array(strtolower($parameters['charset']), array_map("strtolower", mb_list_encodings()))) {
 // do the stuff
}

beskhu avatar Mar 03 '18 03:03 beskhu