From 8c6f0adc66423283642ecfdf301266f79cb1b9d2 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Mon, 12 Aug 2024 16:50:11 +0200 Subject: [PATCH] Improved handling of RFC 2822 dates #1694 --- .../libraries/MailSo/Base/DateTimeHelper.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Base/DateTimeHelper.php b/snappymail/v/0.0.0/app/libraries/MailSo/Base/DateTimeHelper.php index 09a626a99..c4cb6c4bc 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Base/DateTimeHelper.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Base/DateTimeHelper.php @@ -31,7 +31,7 @@ abstract class DateTimeHelper /** * Parse date string formated as "Thu, 10 Jun 2010 08:58:33 -0700 (PDT)" - * RFC 2822 + * https://www.rfc-editor.org/rfc/rfc2822#section-3.3 */ public static function ParseRFC2822DateString(string $sDateTime) : int { @@ -41,10 +41,19 @@ abstract class DateTimeHelper return 0; } - $sDateTime = \trim(\preg_replace('/ \([a-zA-Z0-9]+\)$/', '', $sDateTime)); + // Strip invalid data after zone, https://github.com/the-djmaze/snappymail/issues/1554 + $sDateTime = \preg_replace('/\s+\([a-zA-Z0-9]+\)$/', '', $sDateTime); + // https://github.com/the-djmaze/snappymail/issues/1694#issuecomment-2270983942 + // Strip day-of-week + $sDateTime = \preg_replace('/^[^,]+,/', '', $sDateTime); + // Add optional seconds $sDateTime = \preg_replace('/ ([0-9]+:[0-9]+) /', ' $1:00 ', $sDateTime); - $oDateTime = \DateTime::createFromFormat(\DateTime::RFC2822, $sDateTime, static::GetUtcTimeZoneObject()) - ?: \DateTime::createFromFormat('d M Y H:i:s O', $sDateTime, static::GetUtcTimeZoneObject()); + $sDateTime = \trim($sDateTime); + $oDateTime = + // Using O (difference to Greenwich time (GMT) without colon between hours and minutes) + \DateTime::createFromFormat('d M Y H:i:s O', $sDateTime, static::GetUtcTimeZoneObject()) + // Using T (obsolete timezone abbreviation) + ?: \DateTime::createFromFormat('d M Y H:i:s T', $sDateTime, static::GetUtcTimeZoneObject()); // 398045302 is 1982-08-13 00:08:22 the date RFC 822 was created if (!$oDateTime || 398045302 > $oDateTime->getTimestamp()) { \SnappyMail\Log::notice('', "Failed to parse RFC 2822 date '{$sDateTime}'");