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\Message\Part; |
8
|
|
|
|
9
|
|
|
use Psr\Http\Message\StreamInterface; |
10
|
|
|
use ZBateson\MailMimeParser\Header\ParameterHeader; |
11
|
|
|
use ZBateson\MailMimeParser\Stream\StreamFactory; |
12
|
|
|
use ZBateson\MailMimeParser\Message\PartFilterFactory; |
13
|
|
|
use ZBateson\MailMimeParser\Header\HeaderContainer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A parent part containing headers. |
17
|
|
|
* |
18
|
|
|
* @author Zaahid Bateson |
19
|
|
|
*/ |
20
|
|
|
abstract class ParentHeaderPart extends ParentPart |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var HeaderContainer Contains headers for this part. |
24
|
|
|
*/ |
25
|
|
|
protected $headerContainer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
* |
30
|
|
|
* @param PartStreamFilterManager $partStreamFilterManager |
31
|
|
|
* @param StreamFactory $streamFactory |
32
|
|
|
* @param PartFilterFactory $partFilterFactory |
33
|
|
|
* @param PartBuilder $partBuilder |
34
|
|
|
* @param StreamInterface $stream |
35
|
|
|
* @param StreamInterface $contentStream |
36
|
|
|
*/ |
37
|
136 |
|
public function __construct( |
38
|
|
|
PartStreamFilterManager $partStreamFilterManager, |
39
|
|
|
StreamFactory $streamFactory, |
40
|
|
|
PartFilterFactory $partFilterFactory, |
41
|
|
|
PartBuilder $partBuilder, |
42
|
|
|
StreamInterface $stream = null, |
43
|
|
|
StreamInterface $contentStream = null |
44
|
|
|
) { |
45
|
136 |
|
parent::__construct( |
46
|
136 |
|
$partStreamFilterManager, |
47
|
136 |
|
$streamFactory, |
48
|
136 |
|
$partFilterFactory, |
49
|
136 |
|
$partBuilder, |
50
|
136 |
|
$stream, |
51
|
136 |
|
$contentStream |
52
|
|
|
); |
53
|
136 |
|
$this->headerContainer = $partBuilder->getHeaderContainer(); |
54
|
136 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the AbstractHeader object for the header with the given $name. |
58
|
|
|
* If the optional $offset is passed, and multiple headers exist with the |
59
|
|
|
* same name, the one at the passed offset is returned. |
60
|
|
|
* |
61
|
|
|
* Note that mime headers aren't case sensitive. |
62
|
|
|
* |
63
|
|
|
* @param string $name |
64
|
|
|
* @param int $offset |
65
|
|
|
* @return \ZBateson\MailMimeParser\Header\AbstractHeader| |
66
|
|
|
* \ZBateson\MailMimeParser\Header\AddressHeader| |
67
|
|
|
* \ZBateson\MailMimeParser\Header\DateHeader| |
68
|
|
|
* \ZBateson\MailMimeParser\Header\GenericHeader| |
69
|
|
|
* \ZBateson\MailMimeParser\Header\IdHeader| |
70
|
|
|
* \ZBateson\MailMimeParser\Header\ParameterHeader| |
71
|
|
|
* \ZBateson\MailMimeParser\Header\ReceivedHeader| |
72
|
|
|
* \ZBateson\MailMimeParser\Header\SubjectHeader |
73
|
|
|
*/ |
74
|
124 |
|
public function getHeader($name, $offset = 0) |
75
|
|
|
{ |
76
|
124 |
|
return $this->headerContainer->get($name, $offset); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns an array of headers in this part. |
81
|
|
|
* |
82
|
|
|
* @return \ZBateson\MailMimeParser\Header\AbstractHeader[] |
83
|
|
|
*/ |
84
|
|
|
public function getAllHeaders() |
85
|
|
|
{ |
86
|
|
|
return $this->headerContainer->getHeaderObjects(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns an array of headers that match the passed name. |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
* @return \ZBateson\MailMimeParser\Header\AbstractHeader[] |
94
|
|
|
*/ |
95
|
|
|
public function getAllHeadersByName($name) |
96
|
|
|
{ |
97
|
|
|
return $this->headerContainer->getAll($name); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns an array of all headers for the mime part with the first element |
102
|
|
|
* holding the name, and the second its value. |
103
|
|
|
* |
104
|
|
|
* @return string[][] |
105
|
|
|
*/ |
106
|
1 |
|
public function getRawHeaders() |
107
|
|
|
{ |
108
|
1 |
|
return $this->headerContainer->getHeaders(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns an iterator to the headers in this collection. Each returned |
113
|
|
|
* element is an array with its first element set to the header's name, and |
114
|
|
|
* the second to its raw value: |
115
|
|
|
* |
116
|
|
|
* [ 'Header-Name', 'Header Value' ] |
117
|
|
|
* |
118
|
|
|
* @return \Iterator |
119
|
|
|
*/ |
120
|
92 |
|
public function getRawHeaderIterator() |
121
|
|
|
{ |
122
|
92 |
|
return $this->headerContainer->getIterator(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the string value for the header with the given $name. |
127
|
|
|
* |
128
|
|
|
* Note that mime headers aren't case sensitive. |
129
|
|
|
* |
130
|
|
|
* @param string $name |
131
|
|
|
* @param string $defaultValue |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
120 |
|
public function getHeaderValue($name, $defaultValue = null) |
135
|
|
|
{ |
136
|
120 |
|
$header = $this->getHeader($name); |
137
|
120 |
|
if ($header !== null) { |
138
|
119 |
|
return $header->getValue(); |
139
|
|
|
} |
140
|
102 |
|
return $defaultValue; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns a parameter of the header $header, given the parameter named |
145
|
|
|
* $param. |
146
|
|
|
* |
147
|
|
|
* Only headers of type |
148
|
|
|
* \ZBateson\MailMimeParser\Header\ParameterHeader have parameters. |
149
|
|
|
* Content-Type and Content-Disposition are examples of headers with |
150
|
|
|
* parameters. "Charset" is a common parameter of Content-Type. |
151
|
|
|
* |
152
|
|
|
* @param string $header |
153
|
|
|
* @param string $param |
154
|
|
|
* @param string $defaultValue |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
118 |
|
public function getHeaderParameter($header, $param, $defaultValue = null) |
158
|
|
|
{ |
159
|
118 |
|
$obj = $this->getHeader($header); |
160
|
118 |
|
if ($obj && $obj instanceof ParameterHeader) { |
161
|
113 |
|
return $obj->getValueFor($param, $defaultValue); |
162
|
|
|
} |
163
|
12 |
|
return $defaultValue; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Adds a header with the given $name and $value. An optional $offset may |
168
|
|
|
* be passed, which will overwrite a header if one exists with the given |
169
|
|
|
* name and offset. Otherwise a new header is added. The passed $offset may |
170
|
|
|
* be ignored in that case if it doesn't represent the next insert position |
171
|
|
|
* for the header with the passed name... instead it would be 'pushed' on at |
172
|
|
|
* the next position. |
173
|
|
|
* |
174
|
|
|
* ```php |
175
|
|
|
* $part = $myParentHeaderPart; |
176
|
|
|
* $part->setRawHeader('New-Header', 'value'); |
177
|
|
|
* echo $part->getHeaderValue('New-Header'); // 'value' |
178
|
|
|
* |
179
|
|
|
* $part->setRawHeader('New-Header', 'second', 4); |
180
|
|
|
* echo is_null($part->getHeader('New-Header', 4)); // '1' (true) |
181
|
|
|
* echo $part->getHeader('New-Header', 1) |
182
|
|
|
* ->getValue(); // 'second' |
183
|
|
|
* ``` |
184
|
|
|
* |
185
|
|
|
* A new \ZBateson\MailMimeParser\Header\AbstractHeader object is created |
186
|
|
|
* from the passed value. No processing on the passed string is performed, |
187
|
|
|
* and so the passed name and value must be formatted correctly according to |
188
|
|
|
* related RFCs. In particular, be careful to encode non-ascii data, to |
189
|
|
|
* keep lines under 998 characters in length, and to follow any special |
190
|
|
|
* formatting required for the type of header. |
191
|
|
|
* |
192
|
|
|
* @param string $name |
193
|
|
|
* @param string $value |
194
|
|
|
* @param int $offset |
195
|
|
|
*/ |
196
|
22 |
|
public function setRawHeader($name, $value, $offset = 0) |
197
|
|
|
{ |
198
|
22 |
|
$this->headerContainer->set($name, $value, $offset); |
199
|
22 |
|
$this->onChange(); |
200
|
22 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Adds a header with the given $name and $value. |
204
|
|
|
* |
205
|
|
|
* Note: If a header with the passed name already exists, a new header is |
206
|
|
|
* created with the same name. This should only be used when that is |
207
|
|
|
* intentional - in most cases setRawHeader should be called. |
208
|
|
|
* |
209
|
|
|
* Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and |
210
|
|
|
* registers it as a header. |
211
|
|
|
* |
212
|
|
|
* @param string $name |
213
|
|
|
* @param string $value |
214
|
|
|
*/ |
215
|
1 |
|
public function addRawHeader($name, $value) |
216
|
|
|
{ |
217
|
1 |
|
$this->headerContainer->add($name, $value); |
218
|
1 |
|
$this->onChange(); |
219
|
1 |
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Removes all headers from this part with the passed name. |
223
|
|
|
* |
224
|
|
|
* @param string $name |
225
|
|
|
*/ |
226
|
19 |
|
public function removeHeader($name) |
227
|
|
|
{ |
228
|
19 |
|
$this->headerContainer->removeAll($name); |
229
|
19 |
|
$this->onChange(); |
230
|
19 |
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Removes a single header with the passed name (in cases where more than |
234
|
|
|
* one may exist, and others should be preserved). |
235
|
|
|
* |
236
|
|
|
* @param string $name |
237
|
|
|
*/ |
238
|
1 |
|
public function removeSingleHeader($name, $offset = 0) |
239
|
|
|
{ |
240
|
1 |
|
$this->headerContainer->remove($name, $offset); |
241
|
1 |
|
$this->onChange(); |
242
|
1 |
|
} |
243
|
|
|
} |
244
|
|
|
|