|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace ZBateson\MailMimeParser\Header\Part; |
|
8
|
|
|
|
|
9
|
|
|
use ZBateson\MbWrapper\MbWrapper; |
|
10
|
|
|
use DateTime; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Parses a header into a DateTime object. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Zaahid Bateson |
|
17
|
|
|
*/ |
|
18
|
|
|
class DatePart extends LiteralPart |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var DateTime the parsed date, or null if the date could not be parsed |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $date; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Tries parsing the passed token as an RFC 2822 date, and failing that into |
|
27
|
|
|
* an RFC 822 date, and failing that, tries to parse it by calling |
|
28
|
|
|
* ``` new DateTime($value) ```. |
|
29
|
|
|
* |
|
30
|
|
|
* @param MbWrapper $charsetConverter |
|
31
|
|
|
* @param string $token |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function __construct(MbWrapper $charsetConverter, $token) { |
|
34
|
|
|
|
|
35
|
|
|
// parent::__construct converts character encoding -- may cause problems |
|
36
|
|
|
// sometimes. |
|
37
|
2 |
|
$dateToken = trim($token); |
|
38
|
2 |
|
parent::__construct($charsetConverter, $dateToken); |
|
39
|
|
|
|
|
40
|
2 |
|
$date = $this->parseDateToken($dateToken); |
|
41
|
|
|
|
|
42
|
|
|
// @see https://bugs.php.net/bug.php?id=42486 |
|
43
|
2 |
|
if ($date === false && preg_match('#UT$#', $dateToken)) { |
|
44
|
1 |
|
$date = $this->parseDateToken($dateToken . 'C'); |
|
45
|
|
|
} |
|
46
|
|
|
// Missing "+" in timezone definition. eg: Thu, 13 Mar 2014 15:02:47 0000 (not RFC compliant) |
|
47
|
2 |
|
if ($date === false && preg_match('# [0-9]{4}$#', $dateToken)) { |
|
48
|
1 |
|
$date = $this->parseDateToken(preg_replace('# ([0-9]{4})$#', ' +$1', $dateToken)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
2 |
|
$this->date = ($date) ?: new DateTime($dateToken); |
|
53
|
1 |
|
} catch (Exception $e) { |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
2 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Parse date string token |
|
59
|
|
|
* @param string $dateToken Date token as string |
|
60
|
|
|
* |
|
61
|
|
|
* @return \DateTime|false Returns \DateTime or false on failure. |
|
62
|
|
|
*/ |
|
63
|
2 |
|
private function parseDateToken($dateToken) |
|
64
|
|
|
{ |
|
65
|
|
|
// First check as RFC822 which allows only 2-digit years |
|
66
|
2 |
|
$date = DateTime::createFromFormat(DateTime::RFC822, $dateToken); |
|
67
|
2 |
|
if ($date === false) { |
|
68
|
2 |
|
$date = DateTime::createFromFormat(DateTime::RFC2822, $dateToken); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
return $date; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns a DateTime object or false if it can't be parsed. |
|
76
|
|
|
* |
|
77
|
|
|
* @return DateTime |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function getDateTime() |
|
80
|
|
|
{ |
|
81
|
2 |
|
return $this->date; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|