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\Consumer; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Header\Part\Token; |
10
|
|
|
use Iterator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Parses a Received header into ReceivedParts, ReceivedDomainParts, a DatePart, |
14
|
|
|
* and CommentParts. |
15
|
|
|
* |
16
|
|
|
* Parts that don't correspond to any of the above are discarded. |
17
|
|
|
* |
18
|
|
|
* @author Zaahid Bateson |
19
|
|
|
*/ |
20
|
|
|
class ReceivedConsumer extends AbstractConsumer |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* ReceivedConsumer doesn't have any token separators of its own. |
24
|
|
|
* Sub-Consumers will return separators matching 'part' word separators, for |
25
|
|
|
* example 'from' and 'by', and ';' for date, etc... |
26
|
|
|
* |
27
|
|
|
* @return string[] an array of regex pattern matchers |
28
|
|
|
*/ |
29
|
|
|
protected function getTokenSeparators() |
30
|
|
|
{ |
31
|
|
|
return []; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ReceivedConsumer doesn't have an end token, and so this just returns |
36
|
|
|
* false. |
37
|
|
|
* |
38
|
|
|
* @param string $token |
39
|
|
|
* @return boolean false |
40
|
|
|
*/ |
41
|
|
|
protected function isEndToken($token) |
42
|
|
|
{ |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* ReceivedConsumer doesn't start consuming at a specific token, it's the |
48
|
|
|
* base handler for the Received header, and so this always returns false. |
49
|
|
|
* |
50
|
|
|
* @codeCoverageIgnore |
51
|
|
|
* @param string $token |
52
|
|
|
* @return boolean false |
53
|
|
|
*/ |
54
|
|
|
protected function isStartToken($token) |
55
|
|
|
{ |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns two {@see Received/DomainConsumer} instances, with FROM and BY as |
61
|
|
|
* part names, and 4 {@see Received/GenericReceivedConsumer} instances for |
62
|
|
|
* VIA, WITH, ID, and FOR part names, and |
63
|
|
|
* 1 {@see Received/ReceivedDateConsumer} for the date/time stamp, and one |
64
|
|
|
* {@see CommentConsumer} to consume any comments. |
65
|
|
|
* |
66
|
|
|
* @return AbstractConsumer[] the sub-consumers |
67
|
|
|
*/ |
68
|
|
|
protected function getSubConsumers() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
$this->consumerService->getSubReceivedConsumer('from'), |
72
|
|
|
$this->consumerService->getSubReceivedConsumer('by'), |
73
|
|
|
$this->consumerService->getSubReceivedConsumer('via'), |
74
|
|
|
$this->consumerService->getSubReceivedConsumer('with'), |
75
|
|
|
$this->consumerService->getSubReceivedConsumer('id'), |
76
|
|
|
$this->consumerService->getSubReceivedConsumer('for'), |
77
|
|
|
$this->consumerService->getSubReceivedConsumer('date'), |
78
|
|
|
$this->consumerService->getCommentConsumer() |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Overridden to exclude the MimeLiteralPart pattern that comes by default |
84
|
|
|
* in AbstractConsumer. |
85
|
|
|
* |
86
|
|
|
* @return string the regex pattern |
87
|
|
|
*/ |
88
|
|
|
protected function getTokenSplitPattern() |
89
|
|
|
{ |
90
|
|
|
$sChars = implode('|', $this->getAllTokenSeparators()); |
91
|
|
|
return '~(' . $sChars . ')~'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Overridden to /not/ advance when the end token matches a start token for |
96
|
|
|
* a sub-consumer. |
97
|
|
|
* |
98
|
|
|
* @param Iterator $tokens |
99
|
|
|
* @param bool $isStartToken |
100
|
|
|
*/ |
101
|
|
|
protected function advanceToNextToken(Iterator $tokens, $isStartToken) |
102
|
|
|
{ |
103
|
|
|
if ($isStartToken) { |
104
|
|
|
$tokens->next(); |
105
|
|
|
} elseif ($tokens->valid() && !$this->isEndToken($tokens->current())) { |
106
|
|
|
foreach ($this->getSubConsumers() as $consumer) { |
107
|
|
|
if ($consumer->isStartToken($tokens->current())) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
$tokens->next(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Overridden to combine all part values into a single string and return it |
117
|
|
|
* as an array with a single element. |
118
|
|
|
* |
119
|
|
|
* @param \ZBateson\MailMimeParser\Header\Part\HeaderPart[] $parts |
120
|
|
|
* @return \ZBateson\MailMimeParser\Header\Part\HeaderPart[]| |
121
|
|
|
* \ZBateson\MailMimeParser\Header\Part\ReceivedDomainPart[]| |
122
|
|
|
* \ZBateson\MailMimeParser\Header\Part\ReceivedPart[]| |
123
|
|
|
* \ZBateson\MailMimeParser\Header\Part\DatePart[]| |
124
|
|
|
* \ZBateson\MailMimeParser\Header\Part\CommentPart[]|array |
125
|
|
|
*/ |
126
|
|
|
protected function processParts(array $parts) |
127
|
|
|
{ |
128
|
|
|
$ret = []; |
129
|
|
|
foreach ($parts as $part) { |
130
|
|
|
if ($part instanceof Token) { |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
$ret[] = $part; |
134
|
|
|
} |
135
|
|
|
return $ret; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|