Skip to content

Commit c79ad58

Browse files
committed
Message date validation extended and custom exception added
Fixing issue #192 and #45
1 parent 29a2a73 commit c79ad58

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

src/IMAP/Message.php

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -322,42 +322,6 @@ private function parseHeader() {
322322
if (property_exists($header, 'subject')) {
323323
$this->subject = mb_decode_mimeheader($header->subject);
324324
}
325-
326-
if (property_exists($header, 'date')) {
327-
$date = $header->date;
328-
329-
/**
330-
* Exception handling for invalid dates
331-
* Will be extended in the future
332-
*
333-
* Currently known invalid formats:
334-
* ^ Datetime ^ Problem ^ Cause
335-
* | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature
336-
* | | and invalid timezone (max 6 char) |
337-
* | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown
338-
* | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown
339-
* | | mail server |
340-
*
341-
* Please report any new invalid timestamps to [#45](https://github.com/Webklex/laravel-imap/issues/45)
342-
*/
343-
try {
344-
$this->date = Carbon::parse($date);
345-
} catch (\Exception $e) {
346-
switch (true) {
347-
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:
348-
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:
349-
$array = explode('(', $date);
350-
$array = array_reverse($array);
351-
$date = trim(array_pop($array));
352-
break;
353-
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:
354-
$date .= 'C';
355-
break;
356-
}
357-
$this->date = Carbon::parse($date);
358-
}
359-
}
360-
361325
if (property_exists($header, 'from')) {
362326
$this->from = $this->parseAddresses($header->from);
363327
}
@@ -373,7 +337,6 @@ private function parseHeader() {
373337
if (property_exists($header, 'references')) {
374338
$this->references = $header->references;
375339
}
376-
377340
if (property_exists($header, 'reply_to')) {
378341
$this->reply_to = $this->parseAddresses($header->reply_to);
379342
}
@@ -383,7 +346,6 @@ private function parseHeader() {
383346
if (property_exists($header, 'sender')) {
384347
$this->sender = $this->parseAddresses($header->sender);
385348
}
386-
387349
if (property_exists($header, 'message_id')) {
388350
$this->message_id = str_replace(['<', '>'], '', $header->message_id);
389351
}
@@ -393,6 +355,56 @@ private function parseHeader() {
393355
} else {
394356
$this->message_no = imap_msgno($this->client->getConnection(), $this->getUid());
395357
}
358+
359+
360+
if (property_exists($header, 'date')) {
361+
$date = $header->date;
362+
363+
/**
364+
* Exception handling for invalid dates
365+
* Will be extended in the future
366+
*
367+
* Currently known invalid formats:
368+
* ^ Datetime ^ Problem ^ Cause
369+
* | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature
370+
* | Thu, 8 Nov 2018 08:54:58 -0200 (-02) |
371+
* | | and invalid timezone (max 6 char) |
372+
* | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown
373+
* | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown
374+
* | | mail server |
375+
* | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/
376+
*
377+
* Please report any new invalid timestamps to [#45](https://github.com/Webklex/laravel-imap/issues/45)
378+
*/
379+
380+
if(preg_match('/\+0580/', $date)) {
381+
$date = str_replace('+0580', '+0530', $date);
382+
}
383+
384+
$date = trim(rtrim($date));
385+
try {
386+
$this->date = Carbon::parse($date);
387+
} catch (\Exception $e) {
388+
switch (true) {
389+
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:
390+
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:
391+
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:
392+
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:
393+
$array = explode('(', $date);
394+
$array = array_reverse($array);
395+
$date = trim(array_pop($array));
396+
break;
397+
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:
398+
$date .= 'C';
399+
break;
400+
}
401+
try{
402+
$this->date = Carbon::parse($date);
403+
} catch (\Exception $_e) {
404+
throw new InvalidMessageDateException("Invalid message date. ID:".$this->getMessageId(), 1000, $e);
405+
}
406+
}
407+
}
396408
}
397409

398410
/**

0 commit comments

Comments
 (0)