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
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Reads headers separated into parameters consisting of a main value, and |
13
|
|
|
* subsequent name/value pairs - for example text/html; charset=utf-8. |
14
|
|
|
* |
15
|
|
|
* A ParameterConsumer's parts are separated by a semi-colon. Its name/value |
16
|
|
|
* pairs are separated with an '=' character. |
17
|
|
|
* |
18
|
|
|
* Parts may be mime-encoded entities. Additionally, a value can be quoted and |
19
|
|
|
* comments may exist. |
20
|
|
|
* |
21
|
|
|
* @author Zaahid Bateson |
22
|
|
|
*/ |
23
|
|
|
class ParameterConsumer extends GenericConsumer |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Returns semi-colon and equals char as token separators. |
27
|
|
|
* |
28
|
|
|
* @return string[] |
29
|
|
|
*/ |
30
|
|
|
protected function getTokenSeparators() |
31
|
|
|
{ |
32
|
|
|
return [';', '=']; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates and returns a \ZBateson\MailMimeParser\Header\Part\Token out of |
37
|
|
|
* the passed string token and returns it, unless the token is an escaped |
38
|
|
|
* literal, in which case a LiteralPart is returned. |
39
|
|
|
* |
40
|
|
|
* @param string $token |
41
|
|
|
* @param bool $isLiteral |
42
|
|
|
* @return \ZBateson\MailMimeParser\Header\Part\HeaderPart |
43
|
|
|
*/ |
44
|
|
|
protected function getPartForToken($token, $isLiteral) |
45
|
|
|
{ |
46
|
|
|
if ($isLiteral) { |
47
|
|
|
return $this->partFactory->newLiteralPart($token); |
48
|
|
|
} |
49
|
|
|
return $this->partFactory->newToken($token); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Instanciates and returns either a MimeLiteralPart if $strName is empty, |
54
|
|
|
* or a ParameterPart otherwise. |
55
|
|
|
* |
56
|
|
|
* @param string $strName |
57
|
|
|
* @param string $strValue |
58
|
|
|
* @return \ZBateson\MailMimeParser\Header\Part\MimeLiteralPart| |
59
|
|
|
* \ZBateson\MailMimeParser\Header\Part\ParameterPart |
60
|
|
|
*/ |
61
|
|
|
private function getPartFor($strName, $strValue) |
62
|
|
|
{ |
63
|
|
|
if (empty($strName)) { |
64
|
|
|
return $this->partFactory->newMimeLiteralPart($strValue); |
65
|
|
|
} |
66
|
|
|
return $this->partFactory->newParameterPart($strName, $strValue); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Handles parameter separator tokens during final processing. |
71
|
|
|
* |
72
|
|
|
* If the end token is found, a new HeaderPart is assigned to the passed |
73
|
|
|
* $combined array. If an '=' character is found, $strCat is assigned to |
74
|
|
|
* $strName and emptied. |
75
|
|
|
* |
76
|
|
|
* Returns true if the token was processed, and false otherwise. |
77
|
|
|
* |
78
|
|
|
* @param string $tokenValue |
79
|
|
|
* @param array $combined |
80
|
|
|
* @param string $strName |
81
|
|
|
* @param string $strCat |
82
|
|
|
* @return boolean |
83
|
|
|
*/ |
84
|
|
|
private function processTokenPart($tokenValue, array &$combined, &$strName, &$strCat) |
85
|
|
|
{ |
86
|
|
|
if ($tokenValue === ';') { |
87
|
|
|
$combined[] = $this->getPartFor($strName, $strCat); |
88
|
|
|
$strName = ''; |
89
|
|
|
$strCat = ''; |
90
|
|
|
return true; |
91
|
|
|
} elseif ($tokenValue === '=') { |
92
|
|
|
$strName = $strCat; |
93
|
|
|
$strCat = ''; |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Post processing involves creating Part\LiteralPart or Part\ParameterPart |
101
|
|
|
* objects out of created Token and LiteralParts. |
102
|
|
|
* |
103
|
|
|
* @param \ZBateson\MailMimeParser\Header\Part\HeaderPart[] $parts |
104
|
|
|
* @return \ZBateson\MailMimeParser\Header\Part\HeaderPart[]|array |
105
|
|
|
*/ |
106
|
|
|
protected function processParts(array $parts) |
107
|
|
|
{ |
108
|
|
|
$combined = []; |
109
|
|
|
$strCat = ''; |
110
|
|
|
$strName = ''; |
111
|
|
|
$parts[] = $this->partFactory->newToken(';'); |
112
|
|
|
foreach ($parts as $part) { |
113
|
|
|
$pValue = $part->getValue(); |
114
|
|
|
if ($part instanceof Token && $this->processTokenPart($pValue, $combined, $strName, $strCat)) { |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
$strCat .= $pValue; |
118
|
|
|
} |
119
|
|
|
return $this->filterIgnoredSpaces($combined); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|