Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

two issues found and corrected #208

Open
beskhu opened this issue Feb 21, 2018 · 1 comment
Open

two issues found and corrected #208

beskhu opened this issue Feb 21, 2018 · 1 comment

Comments

@beskhu
Copy link

beskhu commented Feb 21, 2018

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
Copy link
Author

beskhu commented Mar 3, 2018

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
}

nurtext pushed a commit to nurtext/Fetch that referenced this issue Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant