From 80e769d01b9d0c6130962e3a1e00352f622907e0 Mon Sep 17 00:00:00 2001 From: Dale Allan Date: Tue, 10 May 2016 11:46:43 +0200 Subject: [PATCH 01/10] Add function to fetch and clear imap errors --- src/Fetch/Server.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Fetch/Server.php b/src/Fetch/Server.php index 32e57c1..a90c7e7 100644 --- a/src/Fetch/Server.php +++ b/src/Fetch/Server.php @@ -439,6 +439,16 @@ public function getMessageByUid($uid) } } + /** + * This function returns all the imap errors to prevent the following + * warning from imap_close and imap_expunge + * 'Unknown: Warning: MIME header encountered in non-MIME message (errflg=3)' + */ + public function getImapErrors() + { + return imap_errors(); + } + /** * This function removes all of the messages flagged for deletion from the mailbox. * From a0e732932ec807e60500f4b2ce52d98fe43f6aa3 Mon Sep 17 00:00:00 2001 From: Dale Allan Date: Mon, 16 May 2016 09:54:23 +0200 Subject: [PATCH 02/10] pass //IGNORE flag to prevent utf-8 decode error throwing an exception --- src/Fetch/Message.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index e382678..26e4510 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -191,6 +191,14 @@ class Message */ public static $charsetFlag = '//TRANSLIT'; + /** + * This value defines the flag set for encoding for iconv to ignore the + * iconv(): Detected an illegal character in input string. + * + * @var string + */ + public static $charsetAltFlag = '//IGNORE'; + /** * These constants can be used to easily access available flags */ @@ -233,7 +241,7 @@ protected function loadMessage() return false; - $this->subject = MIME::decode($messageOverview->subject, self::$charset); + $this->subject = MIME::decode($messageOverview->subject, self::$charset . self::$charsetAltFlag); $this->date = strtotime($messageOverview->date); $this->size = $messageOverview->size; @@ -674,7 +682,7 @@ protected function processAddressObject($addresses) $currentAddress = array(); $currentAddress['address'] = $address->mailbox . '@' . $address->host; if (isset($address->personal)) { - $currentAddress['name'] = MIME::decode($address->personal, self::$charset); + $currentAddress['name'] = MIME::decode($address->personal, self::$charset . self::$charsetAltFlag); } $outputAddresses[] = $currentAddress; } From 7659863d5895a6f0afc1b3d590d3665b77d3822b Mon Sep 17 00:00:00 2001 From: Marnu Lombard Date: Tue, 24 May 2016 11:45:52 +0200 Subject: [PATCH 03/10] Add check for Bytes in attachments before assigning. As per unmerged pull request https://github.com/tedious/Fetch/pull/156 --- src/Fetch/Attachment.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Fetch/Attachment.php b/src/Fetch/Attachment.php index e77984a..4079c5a 100644 --- a/src/Fetch/Attachment.php +++ b/src/Fetch/Attachment.php @@ -97,8 +97,10 @@ public function __construct(Message $message, $structure, $partIdentifier = null } elseif (isset($parameters['name'])) { $this->setFileName($parameters['name']); } - - $this->size = $structure->bytes; + + if (isset($structure->bytes)) { + $this->size = $structure->bytes; + } $this->mimeType = Message::typeIdToString($structure->type); From 8d0813c8994ba772943e7259a03b8cbac10ba039 Mon Sep 17 00:00:00 2001 From: Marnu Lombard Date: Thu, 2 Jun 2016 07:51:18 +0200 Subject: [PATCH 04/10] Add checks for subject, date & size on the message object before attempting to use them. --- src/Fetch/Message.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index 26e4510..384b08b 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -238,12 +238,15 @@ protected function loadMessage() /* First load the message overview information */ if(!is_object($messageOverview = $this->getOverview())) - return false; - $this->subject = MIME::decode($messageOverview->subject, self::$charset . self::$charsetAltFlag); - $this->date = strtotime($messageOverview->date); - $this->size = $messageOverview->size; + $subject = property_exists($messageOverview, 'subject')? $messageOverview->subject : ''; + $date = property_exists($messageOverview, 'date')? $messageOverview->date : ''; + $size = property_exists($messageOverview, 'size')? $messageOverview->size : ''; + + $this->subject = MIME::decode($subject, self::$charset . self::$charsetAltFlag); + $this->date = strtotime($date); + $this->size = $size; foreach (self::$flagTypes as $flag) $this->status[$flag] = ($messageOverview->$flag == 1); From 7dd1cff8d56f7ad5232365fda63a2e55ae6f45d1 Mon Sep 17 00:00:00 2001 From: Marnu Lombard Date: Thu, 2 Jun 2016 08:01:58 +0200 Subject: [PATCH 05/10] Check for $address->host before assigning it --- src/Fetch/Message.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index 384b08b..7faee6d 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -683,7 +683,8 @@ protected function processAddressObject($addresses) foreach ($addresses as $address) { if (property_exists($address, 'mailbox') && $address->mailbox != 'undisclosed-recipients') { $currentAddress = array(); - $currentAddress['address'] = $address->mailbox . '@' . $address->host; + $host = property_exists($address, 'host')?$address->host:''; + $currentAddress['address'] = $address->mailbox . '@' . $host; if (isset($address->personal)) { $currentAddress['name'] = MIME::decode($address->personal, self::$charset . self::$charsetAltFlag); } From 0c8f31e74e0ab78b4483601b986f8c14c8f288d7 Mon Sep 17 00:00:00 2001 From: Marnu Lombard Date: Wed, 17 Apr 2019 08:59:37 +0200 Subject: [PATCH 06/10] Convert encoding of strings before running through `iconv()` We were receiving a lot of errors with mandarin strings, This fix will change unknown characters to `?` and prevent `iconv()` throwing an error Signed-off-by: Marnu Lombard --- src/Fetch/MIME.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Fetch/MIME.php b/src/Fetch/MIME.php index b63d72b..db69cc2 100644 --- a/src/Fetch/MIME.php +++ b/src/Fetch/MIME.php @@ -36,8 +36,14 @@ public static function decode($text, $targetCharset = 'utf-8') foreach (imap_mime_header_decode($text) as $word) { $ch = 'default' === $word->charset ? 'ascii' : $word->charset; + $text = $word->text; + if (function_exists('mb_convert_encoding')) { + // This will strip any unrecognised characters and ensure we avoid + // "Detected an incomplete multibyte character in input string" errors + $text = mb_convert_encoding($text, $targetCharset, mb_detect_encoding($text, mb_detect_order())); + } - $result .= iconv($ch, $targetCharset, $word->text); + $result .= iconv($ch, $targetCharset, $text); } return $result; From 95cbaffeeca812edb8b8b51f00e85c907a416f53 Mon Sep 17 00:00:00 2001 From: Marnu Lombard Date: Wed, 17 Apr 2019 09:05:45 +0200 Subject: [PATCH 07/10] Improve string encoding conversion logic Instead of using the passed in encoding which could contain `//TRANSLIT` or `//IGNORE`, Use the detected encoding of the string that `imap_mime_header_decode()` supplies us. Signed-off-by: Marnu Lombard --- src/Fetch/MIME.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fetch/MIME.php b/src/Fetch/MIME.php index db69cc2..9ddbb4a 100644 --- a/src/Fetch/MIME.php +++ b/src/Fetch/MIME.php @@ -40,7 +40,7 @@ public static function decode($text, $targetCharset = 'utf-8') if (function_exists('mb_convert_encoding')) { // This will strip any unrecognised characters and ensure we avoid // "Detected an incomplete multibyte character in input string" errors - $text = mb_convert_encoding($text, $targetCharset, mb_detect_encoding($text, mb_detect_order())); + $text = mb_convert_encoding($text, $ch, $ch); } $result .= iconv($ch, $targetCharset, $text); From 8530a9133601d9da63516b6776e626216778222a Mon Sep 17 00:00:00 2001 From: Marnu Lombard Date: Tue, 7 Jan 2020 08:10:40 +0200 Subject: [PATCH 08/10] [bugfix] Prevent the attempt to use encodings that `mb_convert_encoding()` can't use --- src/Fetch/MIME.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Fetch/MIME.php b/src/Fetch/MIME.php index 9ddbb4a..3fc5535 100644 --- a/src/Fetch/MIME.php +++ b/src/Fetch/MIME.php @@ -37,7 +37,8 @@ public static function decode($text, $targetCharset = 'utf-8') foreach (imap_mime_header_decode($text) as $word) { $ch = 'default' === $word->charset ? 'ascii' : $word->charset; $text = $word->text; - if (function_exists('mb_convert_encoding')) { + // Use the possible false return of `mb_encoding_aliases()` to detect whether we can process the encoding + if (function_exists('mb_convert_encoding') && @mb_encoding_aliases($ch)) { // This will strip any unrecognised characters and ensure we avoid // "Detected an incomplete multibyte character in input string" errors $text = mb_convert_encoding($text, $ch, $ch); From be50d776d2a72dd2b6143e8f3a0859ee88965382 Mon Sep 17 00:00:00 2001 From: William Ryan Date: Wed, 23 Nov 2022 09:22:31 +0200 Subject: [PATCH 09/10] allow nested attachments allow nested attachments in attached emails of email --- src/Fetch/Message.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index 7faee6d..6d0fa7a 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -573,15 +573,21 @@ protected function processStructure($structure, $partIdentifier = null) } } - if (isset($structure->parts)) { // multipart: iterate through each part + if (! empty($structure->parts)) { - foreach ($structure->parts as $partIndex => $part) { - $partId = $partIndex + 1; + if (isset($structure->subtype) && strtolower($structure->subtype) === 'rfc822') { + // rfc822: The root part is processed with the current part identifier + $this->processStructure($structure->parts[0], $partIdentifier); + } else { + // multipart: iterate through each part + foreach ($structure->parts as $partIndex => $part) { + $partId = $partIndex + 1; - if (isset($partIdentifier)) - $partId = $partIdentifier . '.' . $partId; + if (isset($partIdentifier)) + $partId = $partIdentifier . '.' . $partId; - $this->processStructure($part, $partId); + $this->processStructure($part, $partId); + } } } } From 99cf09752a07cd945bb44811bb75262f3cb220c5 Mon Sep 17 00:00:00 2001 From: Joel Mnisi Date: Mon, 12 Jun 2023 08:40:11 +0200 Subject: [PATCH 10/10] Rename filename - replace any characters that are not letters --- src/Fetch/Attachment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fetch/Attachment.php b/src/Fetch/Attachment.php index 4079c5a..e6b0478 100644 --- a/src/Fetch/Attachment.php +++ b/src/Fetch/Attachment.php @@ -137,7 +137,7 @@ public function getData() */ public function getFileName() { - return (isset($this->filename)) ? $this->filename : false; + return (isset($this->filename)) ? preg_replace('/[^A-Za-z0-9\-_.]/', '_', $this->filename) : false; } /**