From c79ad58823ef5f72143de147e22c2507dc6d91bd Mon Sep 17 00:00:00 2001 From: Webklex Date: Mon, 11 Mar 2019 21:23:59 +0100 Subject: [PATCH] Message date validation extended and custom exception added Fixing issue #192 and #45 --- src/IMAP/Message.php | 88 +++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/src/IMAP/Message.php b/src/IMAP/Message.php index 4fb8f6a..9238358 100644 --- a/src/IMAP/Message.php +++ b/src/IMAP/Message.php @@ -322,42 +322,6 @@ private function parseHeader() { if (property_exists($header, 'subject')) { $this->subject = mb_decode_mimeheader($header->subject); } - - if (property_exists($header, 'date')) { - $date = $header->date; - - /** - * Exception handling for invalid dates - * Will be extended in the future - * - * Currently known invalid formats: - * ^ Datetime ^ Problem ^ Cause - * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature - * | | and invalid timezone (max 6 char) | - * | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown - * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown - * | | mail server | - * - * Please report any new invalid timestamps to [#45](https://github.com/Webklex/laravel-imap/issues/45) - */ - try { - $this->date = Carbon::parse($date); - } catch (\Exception $e) { - switch (true) { - case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0: - case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{2,4}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}\ [A-Z]{2}\ \-[0-9]{2}\:[0-9]{2}\ \([A-Z]{2,3}\ \-[0-9]{2}:[0-9]{2}\))+$/i', $date) > 0: - $array = explode('(', $date); - $array = array_reverse($array); - $date = trim(array_pop($array)); - break; - case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0: - $date .= 'C'; - break; - } - $this->date = Carbon::parse($date); - } - } - if (property_exists($header, 'from')) { $this->from = $this->parseAddresses($header->from); } @@ -373,7 +337,6 @@ private function parseHeader() { if (property_exists($header, 'references')) { $this->references = $header->references; } - if (property_exists($header, 'reply_to')) { $this->reply_to = $this->parseAddresses($header->reply_to); } @@ -383,7 +346,6 @@ private function parseHeader() { if (property_exists($header, 'sender')) { $this->sender = $this->parseAddresses($header->sender); } - if (property_exists($header, 'message_id')) { $this->message_id = str_replace(['<', '>'], '', $header->message_id); } @@ -393,6 +355,56 @@ private function parseHeader() { } else { $this->message_no = imap_msgno($this->client->getConnection(), $this->getUid()); } + + + if (property_exists($header, 'date')) { + $date = $header->date; + + /** + * Exception handling for invalid dates + * Will be extended in the future + * + * Currently known invalid formats: + * ^ Datetime ^ Problem ^ Cause + * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature + * | Thu, 8 Nov 2018 08:54:58 -0200 (-02) | + * | | and invalid timezone (max 6 char) | + * | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown + * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown + * | | mail server | + * | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/ + * + * Please report any new invalid timestamps to [#45](https://github.com/Webklex/laravel-imap/issues/45) + */ + + if(preg_match('/\+0580/', $date)) { + $date = str_replace('+0580', '+0530', $date); + } + + $date = trim(rtrim($date)); + try { + $this->date = Carbon::parse($date); + } catch (\Exception $e) { + switch (true) { + case preg_match('/([A-Z]{2,3}[\,|\ \,]\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}.*)+$/i', $date) > 0: + case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0: + case preg_match('/([A-Z]{2,3}\, \ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0: + case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{2,4}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}\ [A-Z]{2}\ \-[0-9]{2}\:[0-9]{2}\ \([A-Z]{2,3}\ \-[0-9]{2}:[0-9]{2}\))+$/i', $date) > 0: + $array = explode('(', $date); + $array = array_reverse($array); + $date = trim(array_pop($array)); + break; + case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0: + $date .= 'C'; + break; + } + try{ + $this->date = Carbon::parse($date); + } catch (\Exception $_e) { + throw new InvalidMessageDateException("Invalid message date. ID:".$this->getMessageId(), 1000, $e); + } + } + } } /**