php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

Parsing date with arabian symbols throw Webklex\PHPIMAP\Exceptions\GetMessagesFailedException Invalid message date

Open shadow762 opened this issue 3 years ago • 4 comments

Hi! I have one message with date which contains arabian symbols. As far as I know Carbon can't parse this kind of date by default so first I get exception from Carbon

"Could not parse '��, 19 ��� 2018 16:20:35': DateTime::__construct(): Failed to parse time string (��, 19 ��� 2018 16:20:35) at position 0 (�): Unexpected character"

and InvalidMessageDateException later.

This exception stops reading the mailbox at all. Is there way to solve this issue? Perhaps not to set date at all in this case?

shadow762 avatar Dec 20 '21 09:12 shadow762

Hi @shadow762 , many thanks for your report. Can you provide me with such an date?

Can you think of a way to parse those dates?

Best regards,

Webklex avatar Feb 03 '22 15:02 Webklex

Hi @shadow762 , many thanks for your report. Can you provide me with such an date?

Can you think of a way to parse those dates?

Best regards,

Hi i have the same problem

PHP Warning: DateTime::modify(): Failed to parse time string (Fri, 11 Feb 2022 16:03:10 +0300 (RUS03)) at position 36 (0): Unexpected character in /opt/www/img.voltag.ru/vendor/nesbot/carbon/src/Carbon/Traits/Modifiers.php on line 439

Creatik avatar Feb 28 '22 07:02 Creatik

If mail time is Mon, 20 Dec 2021 10:25:08 +0800 sync fails and program aborts.

The parseDate method of file webklex\php-imap\src\Header.php can be changed to the following

Old

private function parseDate($header) {
        if (property_exists($header, 'date')) {
            $parsed_date = null;
            $date = $header->date;

            if(preg_match('/\+0580/', $date)) {
                $date = str_replace('+0580', '+0530', $date);
            }

            $date = trim(rtrim($date));
            try {
                $parsed_date = Carbon::parse($date);
            } catch (\Exception $e) {
                switch (true) {
                    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:
                    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}\ UT)+$/i', $date) > 0:
                        $date .= 'C';
                        break;
                    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]{2,4}\ \(\+[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}.*)+$/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;
                }
                try{
                    $parsed_date = Carbon::parse($date);
                } catch (\Exception $_e) {
                    throw new InvalidMessageDateException("Invalid message date. ID:".$this->get("message_id"), 1100, $e);
                }
            }

            $this->set("date", $parsed_date);
        }
    }

New

private function parseDate($header) {
        if (property_exists($header, 'date')) {
            $parsed_date = null;
            $date = $header->date;

            if(preg_match('/\+0580/', $date)) {
                $date = str_replace('+0580', '+0530', $date);
            }

            $date = trim(rtrim($date));
            try {
                if(strpos($date, ' ') !== false){
                    $date = str_replace(' ', ' ', $date);
                }
                $parsed_date = Carbon::parse($date);
            } catch (\Exception $e) {
                switch (true) {
                    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:
                    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}\ UT)+$/i', $date) > 0:
                        $date .= 'C';
                        break;
                    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]{2,4}\ \(\+[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}.*)+$/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;
                }
                try{
                    if(strpos($date, ' ') !== false){
                        $date = str_replace(' ', ' ', $date);
                    }
                    $parsed_date = Carbon::parse($date);
                } catch (\Exception $_e) {
                    throw new InvalidMessageDateException("Invalid message date. ID:".$this->get("message_id"), 1100, $e);
                }
            }

            $this->set("date", $parsed_date);
        }
    }

Hanaver avatar Aug 04 '22 07:08 Hanaver

Hi @Creatik @Hanaver , man those timestamps are getting weirder and weirder. Thanks for providing new specimens for our collection :)

Best regards & happy coding,

Webklex avatar Aug 08 '22 10:08 Webklex