|
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; |
|
8
|
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Header\HeaderFactory; |
|
10
|
|
|
use ZBateson\MailMimeParser\Header\ParameterHeader; |
|
11
|
|
|
use ZBateson\MailMimeParser\Message\Writer\MimePartWriter; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Represents a single part of a multi-part mime message. |
|
15
|
|
|
* |
|
16
|
|
|
* A MimePart object may have any number of child parts, or may be a child |
|
17
|
|
|
* itself with its own parent or parents. |
|
18
|
|
|
* |
|
19
|
|
|
* The content of the part can be read from its PartStream resource handle, |
|
20
|
|
|
* accessible via MimePart::getContentResourceHanlde. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Zaahid Bateson |
|
23
|
|
|
*/ |
|
24
|
|
|
class WritableMessage |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var \ZBateson\MailMimeParser\Message\Part\MimePartFactory a MimePartFactory to create |
|
28
|
|
|
* parts for attachments/content |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $mimePartFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \ZBateson\MailMimeParser\Message\Writer\MessageWriter the part |
|
34
|
|
|
* writer for this Message. The same object is assigned to $partWriter |
|
35
|
|
|
* but as an AbstractWriter -- not really needed in PHP but helps with |
|
36
|
|
|
* auto-complete and code analyzers. |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $messageWriter = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Creates a unique mime boundary and assigns it to the passed part's |
|
42
|
|
|
* Content-Type header with the passed mime type. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \ZBateson\MailMimeParser\Message\Part\MimePart $part |
|
45
|
|
|
* @param string $mimeType |
|
46
|
|
|
*/ |
|
47
|
|
|
private function setMimeHeaderBoundaryOnPart(MimePart $part, $mimeType) |
|
48
|
|
|
{ |
|
49
|
|
|
$part->setRawHeader( |
|
50
|
|
|
'Content-Type', |
|
51
|
|
|
"$mimeType;\r\n\tboundary=\"" |
|
52
|
|
|
. $this->getUniqueBoundary($mimeType) . '"' |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Sets this message to be a multipart/alternative message, making space for |
|
58
|
|
|
* a second content part. |
|
59
|
|
|
* |
|
60
|
|
|
* Creates a content part and assigns the content stream from the message to |
|
61
|
|
|
* that newly created part. |
|
62
|
|
|
*/ |
|
63
|
|
|
private function setMessageAsAlternative() |
|
64
|
|
|
{ |
|
65
|
|
|
$contentPart = $this->mimePartFactory->newMimePart(); |
|
|
|
|
|
|
66
|
|
|
$contentPart->attachContentResourceHandle($this->handle); |
|
|
|
|
|
|
67
|
|
|
$this->detachContentResourceHandle(); |
|
|
|
|
|
|
68
|
|
|
$contentType = 'text/plain; charset="us-ascii"'; |
|
69
|
|
|
$contentHeader = $this->getHeader('Content-Type'); |
|
|
|
|
|
|
70
|
|
|
if ($contentHeader !== null) { |
|
71
|
|
|
$contentType = $contentHeader->getRawValue(); |
|
72
|
|
|
} |
|
73
|
|
|
$contentPart->setRawHeader('Content-Type', $contentType); |
|
74
|
|
|
$this->setMimeHeaderBoundaryOnPart($this, 'multipart/alternative'); |
|
|
|
|
|
|
75
|
|
|
$this->addPart($contentPart, 0); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Moves all parts under $from into this message except those with a |
|
80
|
|
|
* content-type equal to $exceptMimeType. If the message is not a |
|
81
|
|
|
* multipart/mixed message, it is set to multipart/mixed first. |
|
82
|
|
|
* |
|
83
|
|
|
* @param MimePart $from |
|
84
|
|
|
* @param string $exceptMimeType |
|
85
|
|
|
*/ |
|
86
|
|
|
private function moveAllPartsAsAttachmentsExcept(MimePart $from, $exceptMimeType) |
|
87
|
|
|
{ |
|
88
|
|
|
$parts = $from->getAllParts(new PartFilter([ |
|
89
|
|
|
'multipart' => PartFilter::FILTER_EXCLUDE, |
|
90
|
|
|
'headers' => [ |
|
91
|
|
|
PartFilter::FILTER_EXCLUDE => [ |
|
92
|
|
|
'Content-Type' => $exceptMimeType |
|
93
|
|
|
] |
|
94
|
|
|
] |
|
95
|
|
|
])); |
|
96
|
|
|
if ($this->getHeaderValue('Content-Type') !== 'multipart/mixed') { |
|
|
|
|
|
|
97
|
|
|
$this->setMessageAsMixed(); |
|
98
|
|
|
} |
|
99
|
|
|
foreach ($parts as $part) { |
|
100
|
|
|
$from->removePart($part); |
|
101
|
|
|
$this->addPart($part); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns an open resource handle for the passed string or resource handle. |
|
107
|
|
|
* |
|
108
|
|
|
* For a string, creates a php://temp stream and returns it. |
|
109
|
|
|
* |
|
110
|
|
|
* @param resource|string $stringOrHandle |
|
111
|
|
|
* @return resource |
|
112
|
|
|
*/ |
|
113
|
|
|
private function getHandleForStringOrHandle($stringOrHandle) |
|
114
|
|
|
{ |
|
115
|
|
|
$tempHandle = fopen('php://temp', 'r+'); |
|
116
|
|
|
if (is_string($stringOrHandle)) { |
|
117
|
|
|
fwrite($tempHandle, $stringOrHandle); |
|
118
|
|
|
} else { |
|
119
|
|
|
stream_copy_to_stream($stringOrHandle, $tempHandle); |
|
120
|
|
|
} |
|
121
|
|
|
rewind($tempHandle); |
|
122
|
|
|
return $tempHandle; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Creates and returns a unique boundary. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $mimeType first 3 characters of a multipart type are used, |
|
129
|
|
|
* e.g. REL for relative or ALT for alternative |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
private function getUniqueBoundary($mimeType) |
|
133
|
|
|
{ |
|
134
|
|
|
$type = ltrim(strtoupper(preg_replace('/^(multipart\/(.{3}).*|.*)$/i', '$2-', $mimeType)), '-'); |
|
135
|
|
|
return uniqid('----=MMP-' . $type . $this->messageObjectId . '.', true); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Returns the direct child of $alternativePart containing a part of |
|
140
|
|
|
* $mimeType. |
|
141
|
|
|
* |
|
142
|
|
|
* Used for alternative mime types that have a multipart/mixed or |
|
143
|
|
|
* multipart/related child containing a content part of $mimeType, where |
|
144
|
|
|
* the whole mixed/related part should be removed. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $mimeType the content-type to find below $alternativePart |
|
147
|
|
|
* @param MimePart $alternativePart The multipart/alternative part to look |
|
148
|
|
|
* under |
|
149
|
|
|
* @return boolean|MimePart false if a part is not found |
|
150
|
|
|
*/ |
|
151
|
|
|
private function getContentPartContainerFromAlternative($mimeType, MimePart $alternativePart) |
|
152
|
|
|
{ |
|
153
|
|
|
$part = $alternativePart->getPart(0, PartFilter::fromInlineContentType($mimeType)); |
|
154
|
|
|
$contPart = null; |
|
|
|
|
|
|
155
|
|
|
do { |
|
156
|
|
|
if ($part === null) { |
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
$contPart = $part; |
|
160
|
|
|
$part = $part->getParent(); |
|
161
|
|
|
} while ($part !== $alternativePart); |
|
162
|
|
|
return $contPart; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Removes all parts of $mimeType from $alternativePart. |
|
167
|
|
|
* |
|
168
|
|
|
* If $alternativePart contains a multipart/mixed or multipart/relative part |
|
169
|
|
|
* with other parts of different content-types, the multipart part is |
|
170
|
|
|
* removed, and parts of different content-types can optionally be moved to |
|
171
|
|
|
* the main message part. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $mimeType |
|
174
|
|
|
* @param MimePart $alternativePart |
|
175
|
|
|
* @param bool $keepOtherContent |
|
176
|
|
|
* @return bool |
|
177
|
|
|
*/ |
|
178
|
|
|
private function removeAllContentPartsFromAlternative($mimeType, $alternativePart, $keepOtherContent) |
|
179
|
|
|
{ |
|
180
|
|
|
$rmPart = $this->getContentPartContainerFromAlternative($mimeType, $alternativePart); |
|
181
|
|
|
if ($rmPart === false) { |
|
182
|
|
|
return false; |
|
183
|
|
|
} |
|
184
|
|
|
if ($keepOtherContent) { |
|
185
|
|
|
$this->moveAllPartsAsAttachmentsExcept($rmPart, $mimeType); |
|
|
|
|
|
|
186
|
|
|
$alternativePart = $this->getPart(0, PartFilter::fromInlineContentType('multipart/alternative')); |
|
|
|
|
|
|
187
|
|
|
} else { |
|
188
|
|
|
$rmPart->removeAllParts(); |
|
189
|
|
|
} |
|
190
|
|
|
$this->removePart($rmPart); |
|
|
|
|
|
|
191
|
|
|
if ($alternativePart !== null) { |
|
192
|
|
|
if ($alternativePart->getChildCount() === 1) { |
|
193
|
|
|
$this->replacePart($alternativePart, $alternativePart->getChild(0)); |
|
194
|
|
|
} elseif ($alternativePart->getChildCount() === 0) { |
|
195
|
|
|
$this->removePart($alternativePart); |
|
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
while ($this->getChildCount() === 1) { |
|
|
|
|
|
|
199
|
|
|
$this->replacePart($this, $this->getChild(0)); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
return true; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Removes the content part of the message with the passed mime type. If |
|
206
|
|
|
* there is a remaining content part and it is an alternative part of the |
|
207
|
|
|
* main message, the content part is moved to the message part. |
|
208
|
|
|
* |
|
209
|
|
|
* If the content part is part of an alternative part beneath the message, |
|
210
|
|
|
* the alternative part is replaced by the remaining content part, |
|
211
|
|
|
* optionally keeping other parts if $keepOtherContent is set to true. |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $mimeType |
|
214
|
|
|
* @param bool $keepOtherContent |
|
215
|
|
|
* @return boolean true on success |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function removeAllContentPartsByMimeType($mimeType, $keepOtherContent = false) |
|
218
|
|
|
{ |
|
219
|
|
|
$alt = $this->getPart(0, PartFilter::fromInlineContentType('multipart/alternative')); |
|
|
|
|
|
|
220
|
|
|
if ($alt !== null) { |
|
221
|
|
|
return $this->removeAllContentPartsFromAlternative($mimeType, $alt, $keepOtherContent); |
|
222
|
|
|
} |
|
223
|
|
|
$this->removeAllParts(PartFilter::fromInlineContentType($mimeType)); |
|
|
|
|
|
|
224
|
|
|
return true; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Removes the 'inline' part with the passed contentType, at the given index |
|
229
|
|
|
* defaulting to the first |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $contentType |
|
|
|
|
|
|
232
|
|
|
* @param int $index |
|
233
|
|
|
* @return boolean true on success |
|
234
|
|
|
*/ |
|
235
|
|
|
protected function removePartByMimeType($mimeType, $index = 0) |
|
236
|
|
|
{ |
|
237
|
|
|
$parts = $this->getAllParts(PartFilter::fromInlineContentType($mimeType)); |
|
|
|
|
|
|
238
|
|
|
$alt = $this->getPart(0, PartFilter::fromInlineContentType('multipart/alternative')); |
|
|
|
|
|
|
239
|
|
|
if ($parts === null || !isset($parts[$index])) { |
|
240
|
|
|
return false; |
|
241
|
|
|
} elseif (count($parts) === 1) { |
|
242
|
|
|
return $this->removeAllContentPartsByMimeType($mimeType, true); |
|
243
|
|
|
} |
|
244
|
|
|
$part = $parts[$index]; |
|
245
|
|
|
$this->removePart($part); |
|
|
|
|
|
|
246
|
|
|
if ($alt !== null && $alt->getChildCount() === 1) { |
|
247
|
|
|
$this->replacePart($alt, $alt->getChild(0)); |
|
248
|
|
|
} |
|
249
|
|
|
return true; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Creates a new mime part as a multipart/alternative and assigns the passed |
|
254
|
|
|
* $contentPart as a part below it before returning it. |
|
255
|
|
|
* |
|
256
|
|
|
* @param MimePart $contentPart |
|
257
|
|
|
* @return MimePart the alternative part |
|
258
|
|
|
*/ |
|
259
|
|
|
private function createAlternativeContentPart(MimePart $contentPart) |
|
260
|
|
|
{ |
|
261
|
|
|
$altPart = $this->mimePartFactory->newMimePart(); |
|
|
|
|
|
|
262
|
|
|
$this->setMimeHeaderBoundaryOnPart($altPart, 'multipart/alternative'); |
|
263
|
|
|
$this->removePart($contentPart); |
|
|
|
|
|
|
264
|
|
|
$this->addPart($altPart, 0); |
|
|
|
|
|
|
265
|
|
|
$altPart->addPart($contentPart, 0); |
|
266
|
|
|
return $altPart; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Copies type headers (Content-Type, Content-Disposition, |
|
271
|
|
|
* Content-Transfer-Encoding) from the $from MimePart to $to. Attaches the |
|
272
|
|
|
* content resource handle of $from to $to, and loops over child parts, |
|
273
|
|
|
* removing them from $from and adding them to $to. |
|
274
|
|
|
* |
|
275
|
|
|
* @param MimePart $from |
|
276
|
|
|
* @param MimePart $to |
|
277
|
|
|
*/ |
|
278
|
|
|
private function movePartContentAndChildrenToPart(MimePart $from, MimePart $to) |
|
279
|
|
|
{ |
|
280
|
|
|
$this->copyTypeHeadersFromPartToPart($from, $to); |
|
281
|
|
|
$to->attachContentResourceHandle($from->getContentResourceHandle()); |
|
282
|
|
|
$from->detachContentResourceHandle(); |
|
283
|
|
|
foreach ($from->getChildParts() as $child) { |
|
284
|
|
|
$from->removePart($child); |
|
285
|
|
|
$to->addPart($child); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Replaces the $part MimePart with $replacement. |
|
291
|
|
|
* |
|
292
|
|
|
* Essentially removes $part from its parent, and adds $replacement in its |
|
293
|
|
|
* same position. If $part is this Message, its type headers are moved from |
|
294
|
|
|
* this message to $replacement, the content resource is moved, and children |
|
295
|
|
|
* are assigned to $replacement. |
|
296
|
|
|
* |
|
297
|
|
|
* @param MimePart $part |
|
298
|
|
|
* @param MimePart $replacement |
|
299
|
|
|
*/ |
|
300
|
|
|
private function replacePart(MimePart $part, MimePart $replacement) |
|
301
|
|
|
{ |
|
302
|
|
|
$this->removePart($replacement); |
|
|
|
|
|
|
303
|
|
|
if ($part === $this) { |
|
304
|
|
|
$this->movePartContentAndChildrenToPart($replacement, $part); |
|
305
|
|
|
return; |
|
306
|
|
|
} |
|
307
|
|
|
$parent = $part->getParent(); |
|
308
|
|
|
$position = $parent->removePart($part); |
|
309
|
|
|
$parent->addPart($replacement, $position); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Copies Content-Type, Content-Disposition and Content-Transfer-Encoding |
|
314
|
|
|
* headers from the $from header into the $to header. If the Content-Type |
|
315
|
|
|
* header isn't defined in $from, defaults to text/plain and |
|
316
|
|
|
* quoted-printable. |
|
317
|
|
|
* |
|
318
|
|
|
* @param \ZBateson\MailMimeParser\Message\Part\MimePart $from |
|
319
|
|
|
* @param \ZBateson\MailMimeParser\Message\Part\MimePart $to |
|
320
|
|
|
*/ |
|
321
|
|
|
private function copyTypeHeadersFromPartToPart(MimePart $from, MimePart $to) |
|
322
|
|
|
{ |
|
323
|
|
|
$typeHeader = $from->getHeader('Content-Type'); |
|
324
|
|
|
if ($typeHeader !== null) { |
|
325
|
|
|
$to->setRawHeader('Content-Type', $typeHeader->getRawValue()); |
|
326
|
|
|
$encodingHeader = $from->getHeader('Content-Transfer-Encoding'); |
|
327
|
|
|
if ($encodingHeader !== null) { |
|
328
|
|
|
$to->setRawHeader('Content-Transfer-Encoding', $encodingHeader->getRawValue()); |
|
329
|
|
|
} |
|
330
|
|
|
$dispositionHeader = $from->getHeader('Content-Disposition'); |
|
331
|
|
|
if ($dispositionHeader !== null) { |
|
332
|
|
|
$to->setRawHeader('Content-Disposition', $dispositionHeader->getRawValue()); |
|
333
|
|
|
} |
|
334
|
|
|
} else { |
|
335
|
|
|
$to->setRawHeader('Content-Type', 'text/plain;charset=us-ascii'); |
|
336
|
|
|
$to->setRawHeader('Content-Transfer-Encoding', 'quoted-printable'); |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Creates a new content part from the passed part, allowing the part to be |
|
342
|
|
|
* used for something else (e.g. changing a non-mime message to a multipart |
|
343
|
|
|
* mime message). |
|
344
|
|
|
* |
|
345
|
|
|
* @param MimePart $part |
|
346
|
|
|
* @return MimePart the newly-created MimePart |
|
347
|
|
|
*/ |
|
348
|
|
|
private function createNewContentPartFromPart(MimePart $part) |
|
349
|
|
|
{ |
|
350
|
|
|
$contPart = $this->mimePartFactory->newMimePart(); |
|
|
|
|
|
|
351
|
|
|
$this->copyTypeHeadersFromPartToPart($part, $contPart); |
|
352
|
|
|
$contPart->attachContentResourceHandle($part->handle); |
|
353
|
|
|
$part->detachContentResourceHandle(); |
|
354
|
|
|
return $contPart; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Creates a new part out of the current contentPart and sets the message's |
|
359
|
|
|
* type to be multipart/mixed. |
|
360
|
|
|
*/ |
|
361
|
|
|
private function setMessageAsMixed() |
|
362
|
|
|
{ |
|
363
|
|
|
if ($this->handle !== null) { |
|
364
|
|
|
$part = $this->createNewContentPartFromPart($this); |
|
|
|
|
|
|
365
|
|
|
$this->addPart($part, 0); |
|
|
|
|
|
|
366
|
|
|
} |
|
367
|
|
|
$this->setMimeHeaderBoundaryOnPart($this, 'multipart/mixed'); |
|
|
|
|
|
|
368
|
|
|
$this->removeHeader('Content-Transfer-Encoding'); |
|
|
|
|
|
|
369
|
|
|
$this->removeHeader('Content-Disposition'); |
|
|
|
|
|
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* This function makes space by moving the main message part down one level. |
|
374
|
|
|
* |
|
375
|
|
|
* The content-type, content-disposition and content-transfer-encoding |
|
376
|
|
|
* headers are copied from this message to the newly created part, the |
|
377
|
|
|
* resource handle is moved and detached, any attachments and content parts |
|
378
|
|
|
* with parents set to this message get their parents set to the newly |
|
379
|
|
|
* created part. |
|
380
|
|
|
*/ |
|
381
|
|
|
private function makeSpaceForMultipartSignedMessage() |
|
382
|
|
|
{ |
|
383
|
|
|
$this->enforceMime(); |
|
384
|
|
|
$messagePart = $this->mimePartFactory->newMimePart(); |
|
|
|
|
|
|
385
|
|
|
|
|
386
|
|
|
$this->copyTypeHeadersFromPartToPart($this, $messagePart); |
|
|
|
|
|
|
387
|
|
|
$messagePart->attachContentResourceHandle($this->handle); |
|
388
|
|
|
$this->detachContentResourceHandle(); |
|
|
|
|
|
|
389
|
|
|
|
|
390
|
|
|
foreach ($this->getChildParts() as $part) { |
|
|
|
|
|
|
391
|
|
|
$this->removePart($part); |
|
|
|
|
|
|
392
|
|
|
$messagePart->addPart($part); |
|
393
|
|
|
} |
|
394
|
|
|
$this->addPart($messagePart, 0); |
|
|
|
|
|
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Creates and returns a new MimePart for the signature part of a |
|
399
|
|
|
* multipart/signed message |
|
400
|
|
|
* |
|
401
|
|
|
* @param string $body |
|
402
|
|
|
*/ |
|
403
|
|
|
public function createSignaturePart($body) |
|
404
|
|
|
{ |
|
405
|
|
|
$signedPart = $this->getSignaturePart(); |
|
406
|
|
|
if ($signedPart === null) { |
|
407
|
|
|
$signedPart = $this->mimePartFactory->newMimePart(); |
|
|
|
|
|
|
408
|
|
|
$this->addPart($signedPart); |
|
|
|
|
|
|
409
|
|
|
} |
|
410
|
|
|
$signedPart->setRawHeader( |
|
411
|
|
|
'Content-Type', |
|
412
|
|
|
$this->getHeaderParameter('Content-Type', 'protocol') |
|
|
|
|
|
|
413
|
|
|
); |
|
414
|
|
|
$signedPart->setContent($body); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* Loops over parts of this message and sets the content-transfer-encoding |
|
419
|
|
|
* header to quoted-printable for text/* mime parts, and to base64 |
|
420
|
|
|
* otherwise for parts that are '8bit' encoded. |
|
421
|
|
|
* |
|
422
|
|
|
* Used for multipart/signed messages which doesn't support 8bit transfer |
|
423
|
|
|
* encodings. |
|
424
|
|
|
*/ |
|
425
|
|
|
private function overwrite8bitContentEncoding() |
|
426
|
|
|
{ |
|
427
|
|
|
$parts = $this->getAllParts(new PartFilter([ |
|
|
|
|
|
|
428
|
|
|
'headers' => [ PartFilter::FILTER_INCLUDE => [ |
|
429
|
|
|
'Content-Transfer-Encoding' => '8bit' |
|
430
|
|
|
] ] |
|
431
|
|
|
])); |
|
432
|
|
|
foreach ($parts as $part) { |
|
433
|
|
|
$contentType = strtolower($part->getHeaderValue('Content-Type', 'text/plain')); |
|
434
|
|
|
if ($contentType === 'text/plain' || $contentType === 'text/html') { |
|
435
|
|
|
$part->setRawHeader('Content-Transfer-Encoding', 'quoted-printable'); |
|
436
|
|
|
} else { |
|
437
|
|
|
$part->setRawHeader('Content-Transfer-Encoding', 'base64'); |
|
438
|
|
|
} |
|
439
|
|
|
} |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
|
|
/** |
|
443
|
|
|
* Ensures a non-text part comes first in a signed multipart/alternative |
|
444
|
|
|
* message as some clients seem to prefer the first content part if the |
|
445
|
|
|
* client doesn't understand multipart/signed. |
|
446
|
|
|
*/ |
|
447
|
|
|
private function ensureHtmlPartFirstForSignedMessage() |
|
448
|
|
|
{ |
|
449
|
|
|
$alt = $this->getPartByMimeType('multipart/alternative'); |
|
|
|
|
|
|
450
|
|
|
if ($alt !== null) { |
|
451
|
|
|
$cont = $this->getContentPartContainerFromAlternative('text/html', $alt); |
|
452
|
|
|
$pos = array_search($cont, $alt->parts, true); |
|
453
|
|
|
if ($pos !== false && $pos !== 0) { |
|
454
|
|
|
$tmp = $alt->parts[0]; |
|
455
|
|
|
$alt->parts[0] = $alt->parts[$pos]; |
|
456
|
|
|
$alt->parts[$pos] = $tmp; |
|
457
|
|
|
} |
|
458
|
|
|
} |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
/** |
|
462
|
|
|
* Turns the message into a multipart/signed message, moving the actual |
|
463
|
|
|
* message into a child part, sets the content-type of the main message to |
|
464
|
|
|
* multipart/signed and adds a signature part as well. |
|
465
|
|
|
* |
|
466
|
|
|
* @param string $micalg The Message Integrity Check algorithm being used |
|
467
|
|
|
* @param string $protocol The mime-type of the signature body |
|
468
|
|
|
*/ |
|
469
|
|
|
public function setAsMultipartSigned($micalg, $protocol) |
|
470
|
|
|
{ |
|
471
|
|
|
$contentType = $this->getHeaderValue('Content-Type', 'text/plain'); |
|
|
|
|
|
|
472
|
|
|
if (strcasecmp($contentType, 'multipart/signed') !== 0) { |
|
473
|
|
|
$this->makeSpaceForMultipartSignedMessage(); |
|
474
|
|
|
$boundary = $this->getUniqueBoundary('multipart/signed'); |
|
475
|
|
|
$this->setRawHeader( |
|
|
|
|
|
|
476
|
|
|
'Content-Type', |
|
477
|
|
|
"multipart/signed;\r\n\tboundary=\"$boundary\";\r\n\tmicalg=\"$micalg\"; protocol=\"$protocol\"" |
|
478
|
|
|
); |
|
479
|
|
|
$this->removeHeader('Content-Disposition'); |
|
|
|
|
|
|
480
|
|
|
$this->removeHeader('Content-Transfer-Encoding'); |
|
|
|
|
|
|
481
|
|
|
} |
|
482
|
|
|
$this->overwrite8bitContentEncoding(); |
|
483
|
|
|
$this->ensureHtmlPartFirstForSignedMessage(); |
|
484
|
|
|
$this->createSignaturePart('Not set'); |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
/** |
|
488
|
|
|
* Returns the signed part or null if not set. |
|
489
|
|
|
* |
|
490
|
|
|
* @return \ZBateson\MailMimeParser\Message\Part\MimePart |
|
491
|
|
|
*/ |
|
492
|
|
|
public function getSignaturePart() |
|
493
|
|
|
{ |
|
494
|
|
|
return $this->getChild(0, new PartFilter([ 'signedpart' => PartFilter::FILTER_INCLUDE ])); |
|
|
|
|
|
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
/** |
|
498
|
|
|
* Enforces the message to be a mime message for a non-mime (e.g. uuencoded |
|
499
|
|
|
* or unspecified) message. If the message has uuencoded attachments, sets |
|
500
|
|
|
* up the message as a multipart/mixed message and creates a content part. |
|
501
|
|
|
*/ |
|
502
|
|
|
private function enforceMime() |
|
503
|
|
|
{ |
|
504
|
|
|
if (!$this->isMime()) { |
|
|
|
|
|
|
505
|
|
|
if ($this->getAttachmentCount()) { |
|
|
|
|
|
|
506
|
|
|
$this->setMessageAsMixed(); |
|
507
|
|
|
} else { |
|
508
|
|
|
$this->setRawHeader('Content-Type', "text/plain;\r\n\tcharset=\"us-ascii\""); |
|
|
|
|
|
|
509
|
|
|
} |
|
510
|
|
|
$this->setRawHeader('Mime-Version', '1.0'); |
|
|
|
|
|
|
511
|
|
|
} |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* Creates a multipart/related part out of 'inline' children of $parent and |
|
516
|
|
|
* returns it. |
|
517
|
|
|
* |
|
518
|
|
|
* @param MimePart $parent |
|
519
|
|
|
* @return MimePart |
|
520
|
|
|
*/ |
|
521
|
|
|
private function createMultipartRelatedPartForInlineChildrenOf(MimePart $parent) |
|
522
|
|
|
{ |
|
523
|
|
|
$relatedPart = $this->mimePartFactory->newMimePart(); |
|
|
|
|
|
|
524
|
|
|
$this->setMimeHeaderBoundaryOnPart($relatedPart, 'multipart/related'); |
|
525
|
|
|
foreach ($parent->getChildParts(PartFilter::fromDisposition('inline', PartFilter::FILTER_EXCLUDE)) as $part) { |
|
526
|
|
|
$this->removePart($part); |
|
|
|
|
|
|
527
|
|
|
$relatedPart->addPart($part); |
|
528
|
|
|
} |
|
529
|
|
|
$parent->addPart($relatedPart, 0); |
|
530
|
|
|
return $relatedPart; |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
/** |
|
534
|
|
|
* Finds an alternative inline part in the message and returns it if one |
|
535
|
|
|
* exists. |
|
536
|
|
|
* |
|
537
|
|
|
* If the passed $mimeType is text/plain, searches for a text/html part. |
|
538
|
|
|
* Otherwise searches for a text/plain part to return. |
|
539
|
|
|
* |
|
540
|
|
|
* @param string $mimeType |
|
541
|
|
|
* @return MimeType or null if not found |
|
542
|
|
|
*/ |
|
543
|
|
|
private function findOtherContentPartFor($mimeType) |
|
544
|
|
|
{ |
|
545
|
|
|
$altPart = $this->getPart( |
|
|
|
|
|
|
546
|
|
|
0, |
|
547
|
|
|
PartFilter::fromInlineContentType(($mimeType === 'text/plain') ? 'text/html' : 'text/plain') |
|
548
|
|
|
); |
|
549
|
|
|
if ($altPart !== null && $altPart->getParent() !== null && $altPart->getParent()->isMultiPart()) { |
|
550
|
|
|
$altPartParent = $altPart->getParent(); |
|
551
|
|
|
if ($altPartParent->getPartCount(PartFilter::fromDisposition('inline', PartFilter::FILTER_EXCLUDE)) !== 1) { |
|
552
|
|
|
$altPart = $this->createMultipartRelatedPartForInlineChildrenOf($altPartParent); |
|
553
|
|
|
} |
|
554
|
|
|
} |
|
555
|
|
|
return $altPart; |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
/** |
|
559
|
|
|
* Creates a new content part for the passed mimeType and charset, making |
|
560
|
|
|
* space by creating a multipart/alternative if needed |
|
561
|
|
|
* |
|
562
|
|
|
* @param string $mimeType |
|
563
|
|
|
* @param string $charset |
|
564
|
|
|
* @return \ZBateson\MailMimeParser\Message\Part\MimePart |
|
565
|
|
|
*/ |
|
566
|
|
|
private function createContentPartForMimeType($mimeType, $charset) |
|
567
|
|
|
{ |
|
568
|
|
|
$mimePart = $this->mimePartFactory->newMimePart(); |
|
|
|
|
|
|
569
|
|
|
$mimePart->setRawHeader('Content-Type', "$mimeType;\r\n\tcharset=\"$charset\""); |
|
570
|
|
|
$mimePart->setRawHeader('Content-Transfer-Encoding', 'quoted-printable'); |
|
571
|
|
|
$this->enforceMime(); |
|
572
|
|
|
|
|
573
|
|
|
$altPart = $this->findOtherContentPartFor($mimeType); |
|
574
|
|
|
|
|
575
|
|
|
if ($altPart === $this) { |
|
576
|
|
|
$this->setMessageAsAlternative(); |
|
577
|
|
|
$this->addPart($mimePart); |
|
|
|
|
|
|
578
|
|
|
} elseif ($altPart !== null) { |
|
579
|
|
|
$mimeAltPart = $this->createAlternativeContentPart($altPart); |
|
580
|
|
|
$mimeAltPart->addPart($mimePart, 1); |
|
581
|
|
|
} else { |
|
582
|
|
|
$this->addPart($mimePart, 0); |
|
|
|
|
|
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
return $mimePart; |
|
586
|
|
|
} |
|
587
|
|
|
|
|
588
|
|
|
/** |
|
589
|
|
|
* Either creates a mime part or sets the existing mime part with the passed |
|
590
|
|
|
* mimeType to $strongOrHandle. |
|
591
|
|
|
* |
|
592
|
|
|
* @param string $mimeType |
|
593
|
|
|
* @param string|resource $stringOrHandle |
|
594
|
|
|
* @param string $charset |
|
595
|
|
|
*/ |
|
596
|
|
|
protected function setContentPartForMimeType($mimeType, $stringOrHandle, $charset) |
|
597
|
|
|
{ |
|
598
|
|
|
$part = ($mimeType === 'text/html') ? $this->getHtmlPart() : $this->getTextPart(); |
|
|
|
|
|
|
599
|
|
|
$handle = $this->getHandleForStringOrHandle($stringOrHandle); |
|
600
|
|
|
if ($part === null) { |
|
601
|
|
|
$part = $this->createContentPartForMimeType($mimeType, $charset); |
|
602
|
|
|
} else { |
|
603
|
|
|
$contentType = $part->getHeaderValue('Content-Type', 'text/plain'); |
|
604
|
|
|
$part->setRawHeader('Content-Type', "$contentType;\r\n\tcharset=\"$charset\""); |
|
605
|
|
|
} |
|
606
|
|
|
$part->attachContentResourceHandle($handle); |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
/** |
|
610
|
|
|
* Sets the text/plain part of the message to the passed $stringOrHandle, |
|
611
|
|
|
* either creating a new part if one doesn't exist for text/plain, or |
|
612
|
|
|
* assigning the value of $stringOrHandle to an existing text/plain part. |
|
613
|
|
|
* |
|
614
|
|
|
* The optional $charset parameter is the charset for saving to. |
|
615
|
|
|
* $stringOrHandle is expected to be in UTF-8 regardless of the target |
|
616
|
|
|
* charset. |
|
617
|
|
|
* |
|
618
|
|
|
* @param string|resource $stringOrHandle |
|
619
|
|
|
* @param string $charset |
|
620
|
|
|
*/ |
|
621
|
|
|
public function setTextPart($stringOrHandle, $charset = 'UTF-8') |
|
622
|
|
|
{ |
|
623
|
|
|
$this->setContentPartForMimeType('text/plain', $stringOrHandle, $charset); |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
/** |
|
627
|
|
|
* Sets the text/html part of the message to the passed $stringOrHandle, |
|
628
|
|
|
* either creating a new part if one doesn't exist for text/html, or |
|
629
|
|
|
* assigning the value of $stringOrHandle to an existing text/html part. |
|
630
|
|
|
* |
|
631
|
|
|
* The optional $charset parameter is the charset for saving to. |
|
632
|
|
|
* $stringOrHandle is expected to be in UTF-8 regardless of the target |
|
633
|
|
|
* charset. |
|
634
|
|
|
* |
|
635
|
|
|
* @param string|resource $stringOrHandle |
|
636
|
|
|
* @param string $charset |
|
637
|
|
|
*/ |
|
638
|
|
|
public function setHtmlPart($stringOrHandle, $charset = 'UTF-8') |
|
639
|
|
|
{ |
|
640
|
|
|
$this->setContentPartForMimeType('text/html', $stringOrHandle, $charset); |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
/** |
|
644
|
|
|
* Removes the text/plain part of the message at the passed index if one |
|
645
|
|
|
* exists. Returns true on success. |
|
646
|
|
|
* |
|
647
|
|
|
* @return bool true on success |
|
648
|
|
|
*/ |
|
649
|
|
|
public function removeTextPart($index = 0) |
|
650
|
|
|
{ |
|
651
|
|
|
return $this->removePartByMimeType('text/plain', $index); |
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
|
|
/** |
|
655
|
|
|
* Removes all text/plain inline parts in this message, optionally keeping |
|
656
|
|
|
* other inline parts as attachments on the main message (defaults to |
|
657
|
|
|
* keeping them). |
|
658
|
|
|
* |
|
659
|
|
|
* @param bool $keepOtherPartsAsAttachments |
|
660
|
|
|
* @return bool true on success |
|
661
|
|
|
*/ |
|
662
|
|
|
public function removeAllTextParts($keepOtherPartsAsAttachments = true) |
|
663
|
|
|
{ |
|
664
|
|
|
return $this->removeAllContentPartsByMimeType('text/plain', $keepOtherPartsAsAttachments); |
|
665
|
|
|
} |
|
666
|
|
|
|
|
667
|
|
|
/** |
|
668
|
|
|
* Removes the html part of the message if one exists. Returns true on |
|
669
|
|
|
* success. |
|
670
|
|
|
* |
|
671
|
|
|
* @return bool true on success |
|
672
|
|
|
*/ |
|
673
|
|
|
public function removeHtmlPart($index = 0) |
|
674
|
|
|
{ |
|
675
|
|
|
return $this->removePartByMimeType('text/html', $index); |
|
676
|
|
|
} |
|
677
|
|
|
|
|
678
|
|
|
/** |
|
679
|
|
|
* Removes all text/html inline parts in this message, optionally keeping |
|
680
|
|
|
* other inline parts as attachments on the main message (defaults to |
|
681
|
|
|
* keeping them). |
|
682
|
|
|
* |
|
683
|
|
|
* @param bool $keepOtherPartsAsAttachments |
|
684
|
|
|
* @return bool true on success |
|
685
|
|
|
*/ |
|
686
|
|
|
public function removeAllHtmlParts($keepOtherPartsAsAttachments = true) |
|
687
|
|
|
{ |
|
688
|
|
|
return $this->removeAllContentPartsByMimeType('text/html', $keepOtherPartsAsAttachments); |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
/** |
|
692
|
|
|
* Removes the attachment with the given index |
|
693
|
|
|
* |
|
694
|
|
|
* @param int $index |
|
695
|
|
|
*/ |
|
696
|
|
|
public function removeAttachmentPart($index) |
|
697
|
|
|
{ |
|
698
|
|
|
$part = $this->getAttachmentPart($index); |
|
|
|
|
|
|
699
|
|
|
$this->removePart($part); |
|
|
|
|
|
|
700
|
|
|
} |
|
701
|
|
|
|
|
702
|
|
|
/** |
|
703
|
|
|
* Creates and returns a MimePart for use with a new attachment part being |
|
704
|
|
|
* created. |
|
705
|
|
|
* |
|
706
|
|
|
* @return \ZBateson\MailMimeParser\Message\Part\MimePart |
|
707
|
|
|
*/ |
|
708
|
|
|
protected function createPartForAttachment() |
|
709
|
|
|
{ |
|
710
|
|
|
if ($this->isMime()) { |
|
|
|
|
|
|
711
|
|
|
$part = $this->mimePartFactory->newMimePart(); |
|
|
|
|
|
|
712
|
|
|
$part->setRawHeader('Content-Transfer-Encoding', 'base64'); |
|
713
|
|
|
if ($this->getHeaderValue('Content-Type') !== 'multipart/mixed') { |
|
|
|
|
|
|
714
|
|
|
$this->setMessageAsMixed(); |
|
715
|
|
|
} |
|
716
|
|
|
return $part; |
|
717
|
|
|
} |
|
718
|
|
|
return $this->mimePartFactory->newUUEncodedPart(); |
|
|
|
|
|
|
719
|
|
|
} |
|
720
|
|
|
|
|
721
|
|
|
/** |
|
722
|
|
|
* Adds an attachment part for the passed raw data string or handle and |
|
723
|
|
|
* given parameters. |
|
724
|
|
|
* |
|
725
|
|
|
* @param string|handle $stringOrHandle |
|
726
|
|
|
* @param strubg $mimeType |
|
727
|
|
|
* @param string $filename |
|
728
|
|
|
* @param string $disposition |
|
729
|
|
|
*/ |
|
730
|
|
|
public function addAttachmentPart($stringOrHandle, $mimeType, $filename = null, $disposition = 'attachment') |
|
731
|
|
|
{ |
|
732
|
|
|
if ($filename === null) { |
|
733
|
|
|
$filename = 'file' . uniqid(); |
|
734
|
|
|
} |
|
735
|
|
|
$filename = iconv('UTF-8', 'US-ASCII//translit//ignore', $filename); |
|
736
|
|
|
$part = $this->createPartForAttachment(); |
|
737
|
|
|
$part->setRawHeader('Content-Type', "$mimeType;\r\n\tname=\"$filename\""); |
|
|
|
|
|
|
738
|
|
|
$part->setRawHeader('Content-Disposition', "$disposition;\r\n\tfilename=\"$filename\""); |
|
|
|
|
|
|
739
|
|
|
$part->attachContentResourceHandle($this->getHandleForStringOrHandle($stringOrHandle)); |
|
|
|
|
|
|
740
|
|
|
$this->addPart($part); |
|
|
|
|
|
|
741
|
|
|
} |
|
742
|
|
|
|
|
743
|
|
|
/** |
|
744
|
|
|
* Adds an attachment part using the passed file. |
|
745
|
|
|
* |
|
746
|
|
|
* Essentially creates a file stream and uses it. |
|
747
|
|
|
* |
|
748
|
|
|
* @param string $file |
|
749
|
|
|
* @param string $mimeType |
|
750
|
|
|
* @param string $filename |
|
751
|
|
|
* @param string $disposition |
|
752
|
|
|
*/ |
|
753
|
|
|
public function addAttachmentPartFromFile($file, $mimeType, $filename = null, $disposition = 'attachment') |
|
754
|
|
|
{ |
|
755
|
|
|
$handle = fopen($file, 'r'); |
|
756
|
|
|
if ($filename === null) { |
|
757
|
|
|
$filename = basename($file); |
|
758
|
|
|
} |
|
759
|
|
|
$filename = iconv('UTF-8', 'US-ASCII//translit//ignore', $filename); |
|
760
|
|
|
$part = $this->createPartForAttachment(); |
|
761
|
|
|
$part->setRawHeader('Content-Type', "$mimeType;\r\n\tname=\"$filename\""); |
|
|
|
|
|
|
762
|
|
|
$part->setRawHeader('Content-Disposition', "$disposition;\r\n\tfilename=\"$filename\""); |
|
|
|
|
|
|
763
|
|
|
$part->attachContentResourceHandle($handle); |
|
|
|
|
|
|
764
|
|
|
$this->addPart($part); |
|
|
|
|
|
|
765
|
|
|
} |
|
766
|
|
|
|
|
767
|
|
|
/** |
|
768
|
|
|
* Saves the message as a MIME message to the passed resource handle. |
|
769
|
|
|
* |
|
770
|
|
|
* @param resource $handle |
|
771
|
|
|
*/ |
|
772
|
|
|
public function save($handle) |
|
773
|
|
|
{ |
|
774
|
|
|
$this->messageWriter->writeMessageTo($this, $handle); |
|
|
|
|
|
|
775
|
|
|
} |
|
776
|
|
|
|
|
777
|
|
|
/** |
|
778
|
|
|
* Returns the content part of a signed message for a signature to be |
|
779
|
|
|
* calculated on the message. |
|
780
|
|
|
* |
|
781
|
|
|
* @return string |
|
782
|
|
|
*/ |
|
783
|
|
|
public function getSignableBody() |
|
784
|
|
|
{ |
|
785
|
|
|
return $this->messageWriter->getSignableBody($this); |
|
|
|
|
|
|
786
|
|
|
} |
|
787
|
|
|
} |
|
788
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.