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\Helper; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\MailMimeParser; |
10
|
|
|
use ZBateson\MailMimeParser\Message; |
11
|
|
|
use ZBateson\MailMimeParser\Message\Part\MimePart; |
12
|
|
|
use ZBateson\MailMimeParser\Message\Part\ParentHeaderPart; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Provides common Message helper routines for Message manipulation. |
16
|
|
|
* |
17
|
|
|
* @author Zaahid Bateson |
18
|
|
|
*/ |
19
|
|
|
class GenericHelper extends AbstractHelper |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string[] List of content headers grabbed from |
23
|
|
|
* https://tools.ietf.org/html/rfc4021#section-2.2 |
24
|
|
|
*/ |
25
|
|
|
private static $contentHeaders = [ |
26
|
|
|
'Content-Type', |
27
|
|
|
'Content-Transfer-Encoding', |
28
|
|
|
'Content-Disposition', |
29
|
|
|
'Content-ID', |
30
|
|
|
'Content-Description', |
31
|
|
|
'Content-Language', |
32
|
|
|
'Content-Base', |
33
|
|
|
'Content-Location', |
34
|
|
|
'Content-features', |
35
|
|
|
'Content-Alternative', |
36
|
|
|
'Content-MD5', |
37
|
|
|
'Content-Duration' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Copies the passed $header from $from, to $to or sets the header to |
42
|
|
|
* $default if it doesn't exist in $from. |
43
|
|
|
* |
44
|
|
|
* @param ParentHeaderPart $from |
45
|
|
|
* @param ParentHeaderPart $to |
46
|
|
|
* @param string $header |
47
|
|
|
* @param string $default |
48
|
|
|
*/ |
49
|
4 |
|
public function copyHeader(ParentHeaderPart $from, ParentHeaderPart $to, $header, $default = null) |
50
|
|
|
{ |
51
|
4 |
|
$fromHeader = $from->getHeader($header); |
52
|
4 |
|
$set = ($fromHeader !== null) ? $fromHeader->getRawValue() : $default; |
53
|
4 |
|
if ($set !== null) { |
54
|
2 |
|
$to->setRawHeader($header, $set); |
55
|
|
|
} |
56
|
4 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Removes Content-* headers (permanent ones as defined in |
60
|
|
|
* https://tools.ietf.org/html/rfc4021#section-2.2) from the passed part, |
61
|
|
|
* then detaches its content stream. |
62
|
|
|
* |
63
|
|
|
* @param ParentHeaderPart $part |
64
|
|
|
*/ |
65
|
3 |
|
public function removeContentHeadersAndContent(ParentHeaderPart $part) |
66
|
|
|
{ |
67
|
3 |
|
foreach (static::$contentHeaders as $header) { |
|
|
|
|
68
|
3 |
|
$part->removeHeader($header); |
69
|
|
|
} |
70
|
3 |
|
$part->detachContentStream(); |
71
|
3 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Copies Content-* headers (permanent ones as defined in |
75
|
|
|
* https://tools.ietf.org/html/rfc4021#section-2.2) |
76
|
|
|
* from the $from header into the $to header. If the Content-Type header |
77
|
|
|
* isn't defined in $from, defaults to text/plain with utf-8 and |
78
|
|
|
* quoted-printable. |
79
|
|
|
* |
80
|
|
|
* @param ParentHeaderPart $from |
81
|
|
|
* @param ParentHeaderPart $to |
82
|
|
|
*/ |
83
|
3 |
|
public function copyContentHeadersAndContent(ParentHeaderPart $from, ParentHeaderPart $to, $move = false) |
84
|
|
|
{ |
85
|
3 |
|
$this->copyHeader($from, $to, 'Content-Type', 'text/plain; charset=utf-8'); |
86
|
3 |
|
if ($from->getHeader('Content-Type') === null) { |
87
|
1 |
|
$this->copyHeader($from, $to, 'Content-Transfer-Encoding', 'quoted-printable'); |
88
|
|
|
} else { |
89
|
2 |
|
$this->copyHeader($from, $to, 'Content-Transfer-Encoding'); |
90
|
|
|
} |
91
|
3 |
|
$rem = array_diff(static::$contentHeaders, [ 'Content-Type', 'Content-Transfer-Encoding']); |
|
|
|
|
92
|
3 |
|
foreach ($rem as $header) { |
93
|
3 |
|
$this->copyHeader($from, $to, $header); |
94
|
|
|
} |
95
|
3 |
|
if ($from->hasContent()) { |
96
|
2 |
|
$to->attachContentStream($from->getContentStream(), MailMimeParser::DEFAULT_CHARSET); |
97
|
|
|
} |
98
|
3 |
|
if ($move) { |
99
|
2 |
|
$this->removeContentHeadersAndContent($from); |
100
|
|
|
} |
101
|
3 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Creates a new content part from the passed part, allowing the part to be |
105
|
|
|
* used for something else (e.g. changing a non-mime message to a multipart |
106
|
|
|
* mime message). |
107
|
|
|
* |
108
|
|
|
* @param ParentHeaderPart $part |
109
|
|
|
* @return MimePart the newly-created MimePart |
110
|
|
|
*/ |
111
|
1 |
|
public function createNewContentPartFrom(ParentHeaderPart $part) |
112
|
|
|
{ |
113
|
1 |
|
$mime = $this->partBuilderFactory->newPartBuilder($this->mimePartFactory)->createMessagePart(); |
114
|
1 |
|
$this->copyContentHeadersAndContent($part, $mime, true); |
115
|
1 |
|
return $mime; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Copies type headers (Content-Type, Content-Disposition, |
120
|
|
|
* Content-Transfer-Encoding) from the $from MimePart to $to. Attaches the |
121
|
|
|
* content resource handle of $from to $to, and loops over child parts, |
122
|
|
|
* removing them from $from and adding them to $to. |
123
|
|
|
* |
124
|
|
|
* @param ParentHeaderPart $from |
125
|
|
|
* @param ParentHeaderPart $to |
126
|
|
|
*/ |
127
|
1 |
|
public function movePartContentAndChildren(ParentHeaderPart $from, ParentHeaderPart $to) |
128
|
|
|
{ |
129
|
1 |
|
$this->copyContentHeadersAndContent($from, $to, true); |
130
|
1 |
|
foreach ($from->getChildParts() as $child) { |
131
|
1 |
|
$from->removePart($child); |
132
|
1 |
|
$to->addChild($child); |
133
|
|
|
} |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Replaces the $part ParentHeaderPart with $replacement. |
138
|
|
|
* |
139
|
|
|
* Essentially removes $part from its parent, and adds $replacement in its |
140
|
|
|
* same position. If $part is this Message, then $part can't be removed and |
141
|
|
|
* replaced, and instead $replacement's type headers are copied to $message, |
142
|
|
|
* and any children below $replacement are added directly below $message. |
143
|
|
|
* |
144
|
|
|
* @param ParentHeaderPart $part |
145
|
|
|
* @param ParentHeaderPart $replacement |
146
|
|
|
*/ |
147
|
2 |
|
public function replacePart(Message $message, ParentHeaderPart $part, ParentHeaderPart $replacement) |
148
|
|
|
{ |
149
|
2 |
|
$position = $message->removePart($replacement); |
150
|
2 |
|
if ($part === $message) { |
|
|
|
|
151
|
1 |
|
$this->movePartContentAndChildren($replacement, $part); |
152
|
1 |
|
return; |
153
|
|
|
} |
154
|
1 |
|
$parent = $part->getParent(); |
155
|
1 |
|
$parent->addChild($replacement, $position); |
156
|
1 |
|
} |
157
|
|
|
} |
158
|
|
|
|