Skip to content

Commit

Permalink
Message date validation extended and custom exception added
Browse files Browse the repository at this point in the history
Fixing issue #192 and #45
  • Loading branch information
Webklex committed Mar 11, 2019
1 parent 29a2a73 commit c79ad58
Showing 1 changed file with 50 additions and 38 deletions.
88 changes: 50 additions & 38 deletions src/IMAP/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
}
}
}

/**
Expand Down

0 comments on commit c79ad58

Please sign in to comment.