Improved handling of RFC 2822 dates #1694

This commit is contained in:
the-djmaze 2024-08-12 16:50:11 +02:00
parent 13cb08f64a
commit 8c6f0adc66

View file

@ -31,7 +31,7 @@ abstract class DateTimeHelper
/** /**
* Parse date string formated as "Thu, 10 Jun 2010 08:58:33 -0700 (PDT)" * 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 public static function ParseRFC2822DateString(string $sDateTime) : int
{ {
@ -41,10 +41,19 @@ abstract class DateTimeHelper
return 0; 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); $sDateTime = \preg_replace('/ ([0-9]+:[0-9]+) /', ' $1:00 ', $sDateTime);
$oDateTime = \DateTime::createFromFormat(\DateTime::RFC2822, $sDateTime, static::GetUtcTimeZoneObject()) $sDateTime = \trim($sDateTime);
?: \DateTime::createFromFormat('d M Y H:i:s O', $sDateTime, static::GetUtcTimeZoneObject()); $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 // 398045302 is 1982-08-13 00:08:22 the date RFC 822 was created
if (!$oDateTime || 398045302 > $oDateTime->getTimestamp()) { if (!$oDateTime || 398045302 > $oDateTime->getTimestamp()) {
\SnappyMail\Log::notice('', "Failed to parse RFC 2822 date '{$sDateTime}'"); \SnappyMail\Log::notice('', "Failed to parse RFC 2822 date '{$sDateTime}'");