Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shadow762 opened this issue Dec 20, 2021 · 4 comments
Labels
bug Something isn't working validating

Comments

@shadow762
Copy link

shadow762 commented Dec 20, 2021

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?

@Webklex Webklex added bug Something isn't working validating labels Feb 3, 2022
@Webklex
Copy link
Owner

Webklex commented Feb 3, 2022

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,

@Creatik
Copy link

Creatik commented Feb 28, 2022

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

@Hanaver
Copy link

Hanaver commented Aug 4, 2022

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);
        }
    }

@Webklex
Copy link
Owner

Webklex commented Aug 8, 2022

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

Best regards & happy coding,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working validating
Projects
None yet
Development

No branches or pull requests

4 participants