From 9a5bdb727c5a2caf048f91367f2fb8551399a9ef Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 18 Feb 2014 17:53:34 -0600
Subject: [PATCH] Careful transcoding attachments with a charset

If a non-text attachment specifies a charset in the content-type header,
don't transcode the content before saving it to the database. This can
corrupt attachments which have a header like the following:

Content-Type: application/pdf; charset=UTF-8

Since a PDF contains binary data, coercing it to UTF-8 encoding will drop
characters not valid in UTF-8 and will corrupt the attachment data.
---
 include/class.mailparse.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 27ea94701..37f9b3bf2 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -370,7 +370,8 @@ class Mail_Parse {
                     'type'  => strtolower($part->ctype_primary.'/'.$part->ctype_secondary),
                     );
 
-            if ($part->ctype_parameters['charset'])
+            if ($part->ctype_parameters['charset']
+                    && 0 === strcasecmp($part->ctype_primary, 'text'))
                 $file['data'] = $this->mime_encode($part->body,
                     $part->ctype_parameters['charset']);
             else
-- 
GitLab