From 4dedcf6040243e38172b8666699696cecbc4ac39 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Tue, 19 Nov 2013 09:16:13 -0600 Subject: [PATCH] email: Handle poorly encoded email headers Handle cases where email headers contain unicode or high-ascii characters and are not encoded according to RFC-2047. This patch handles a special case where a detectable unicode character set is used, such as UTF-8. For high-ascii characters in a character set other than iso-8859-1, the result of this patch is undefined. Fixes #826 --- include/class.mailfetch.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php index 9bf51bc9f..9d96aa66f 100644 --- a/include/class.mailfetch.php +++ b/include/class.mailfetch.php @@ -219,9 +219,28 @@ class MailFetcher { return imap_utf7_encode($mailbox); } - //Generic decoder - resulting text is utf8 encoded -> mirrors imap_utf8 + /** + * Mime header value decoder. Usually unicode characters are encoded + * according to RFC-2047. This function will decode RFC-2047 encoded + * header values as well as detect headers which are not encoded. + * + * Caveats: + * If headers contain non-ascii characters the result of this function + * is completely undefined. If osTicket is corrupting your email + * headers, your mail software is not encoding the header text + * correctly. + * + * Returns: + * Header value, transocded to UTF-8 + */ function mime_decode($text, $encoding='utf-8') { + // Handle poorly or completely un-encoded header values ( + if (function_exists('mb_detect_encoding')) + if (($src_enc = mb_detect_encoding($text)) + && (strcasecmp($src_enc, 'ASCII') !== 0)) + return Format::encode($text, $src_enc, $encoding); + // Handle ASCII text and RFC-2047 encoding $str = ''; $parts = imap_mime_header_decode($text); foreach ($parts as $part) -- GitLab