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
|
|
|
|
8
|
|
|
namespace ZBateson\MailMimeParser\Header\Consumer; |
9
|
|
|
|
10
|
|
|
use ZBateson\MailMimeParser\Header\Part\MimeLiteralPartFactory; |
11
|
|
|
use ZBateson\MailMimeParser\Header\IHeaderPart; |
12
|
|
|
use Iterator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Extends AbstractGenericConsumerService to use a MimeLiteralPartFactory, and |
16
|
|
|
* to preserve all whitespace and escape sequences as-is (unlike other headers |
17
|
|
|
* subject headers don't have escape chars such as '\\' for a backslash). |
18
|
|
|
* |
19
|
|
|
* SubjectConsumerService doesn't define any sub-consumers. |
20
|
|
|
* |
21
|
|
|
* @author Zaahid Bateson |
22
|
|
|
*/ |
23
|
|
|
class SubjectConsumerService extends AbstractGenericConsumerService |
24
|
|
|
{ |
25
|
2 |
|
public function __construct(MimeLiteralPartFactory $partFactory) |
26
|
|
|
{ |
27
|
2 |
|
parent::__construct($partFactory); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Overridden to preserve whitespace. |
32
|
|
|
* |
33
|
|
|
* Whitespace between two words is preserved unless the whitespace begins |
34
|
|
|
* with a newline (\n or \r\n), in which case the entire string of |
35
|
|
|
* whitespace is discarded, and a single space ' ' character is used in its |
36
|
|
|
* place. |
37
|
|
|
*/ |
38
|
97 |
|
protected function getPartForToken(string $token, bool $isLiteral) : ?IHeaderPart |
39
|
|
|
{ |
40
|
97 |
|
if ($isLiteral) { |
41
|
|
|
return $this->partFactory->newLiteralPart($token); |
42
|
97 |
|
} elseif (\preg_match('/^\s+$/', $token)) { |
43
|
75 |
|
if (\preg_match('/^[\r\n]/', $token)) { |
44
|
10 |
|
return $this->partFactory->newToken(' '); |
45
|
|
|
} |
46
|
75 |
|
return $this->partFactory->newToken($token); |
47
|
|
|
} |
48
|
97 |
|
return $this->partFactory->newInstance($token); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns an array of \ZBateson\MailMimeParser\Header\Part\HeaderPart for |
53
|
|
|
* the current token on the iterator. |
54
|
|
|
* |
55
|
|
|
* Overridden from AbstractConsumerService to remove special filtering for |
56
|
|
|
* backslash escaping, which also seems to not apply to Subject headers at |
57
|
|
|
* least in ThunderBird's implementation. |
58
|
|
|
* |
59
|
|
|
* @return IHeaderPart[] |
60
|
|
|
*/ |
61
|
97 |
|
protected function getTokenParts(Iterator $tokens) : array |
62
|
|
|
{ |
63
|
97 |
|
return $this->getConsumerTokenParts($tokens); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Overridden to not split out backslash characters and its next character |
68
|
|
|
* as a special case defined in AbstractConsumerService |
69
|
|
|
* |
70
|
|
|
* @return string the regex pattern |
71
|
|
|
*/ |
72
|
2 |
|
protected function getTokenSplitPattern() : string |
73
|
|
|
{ |
74
|
2 |
|
$sChars = \implode('|', $this->getAllTokenSeparators()); |
75
|
2 |
|
return '~(' . $sChars . ')~'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Overridden to combine all part values into a single string and return it |
80
|
|
|
* as an array with a single element. |
81
|
|
|
* |
82
|
|
|
* The returned IHeaderParts are all LiteralParts. |
83
|
|
|
* |
84
|
|
|
* @param IHeaderPart[] $parts |
85
|
|
|
* @return IHeaderPart[] |
86
|
|
|
*/ |
87
|
97 |
|
protected function processParts(array $parts) : array |
88
|
|
|
{ |
89
|
97 |
|
$strValue = ''; |
90
|
97 |
|
$filtered = $this->filterIgnoredSpaces($parts); |
91
|
97 |
|
foreach ($filtered as $part) { |
92
|
97 |
|
$strValue .= $part->getValue(); |
93
|
|
|
} |
94
|
97 |
|
return [$this->partFactory->newLiteralPart($strValue)]; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|