|
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
|
|
|
* Copies the passed $header from $from, to $to or sets the header to |
|
23
|
|
|
* $default if it doesn't exist in $from. |
|
24
|
|
|
* |
|
25
|
|
|
* @param ParentHeaderPart $from |
|
26
|
|
|
* @param ParentHeaderPart $to |
|
27
|
|
|
* @param string $header |
|
28
|
|
|
* @param string $default |
|
29
|
|
|
*/ |
|
30
|
|
|
public function copyHeader(ParentHeaderPart $from, ParentHeaderPart $to, $header, $default = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$fromHeader = $from->getHeader($header); |
|
33
|
|
|
$set = ($fromHeader !== null) ? $fromHeader->getRawValue() : $default; |
|
34
|
|
|
if ($set !== null) { |
|
35
|
|
|
$to->setRawHeader($header, $set); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Removes the following headers from the passed part: Content-Type, |
|
41
|
|
|
* Content-Transfer-Encoding, Content-Disposition, Content-ID and |
|
42
|
|
|
* Content-Description, then detaches its content stream. |
|
43
|
|
|
* |
|
44
|
|
|
* @param ParentHeaderPart $part |
|
45
|
|
|
*/ |
|
46
|
|
|
public function removeTypeHeadersAndContent(ParentHeaderPart $part) |
|
47
|
|
|
{ |
|
48
|
|
|
$part->removeHeader('Content-Type'); |
|
49
|
|
|
$part->removeHeader('Content-Transfer-Encoding'); |
|
50
|
|
|
$part->removeHeader('Content-Disposition'); |
|
51
|
|
|
$part->removeHeader('Content-ID'); |
|
52
|
|
|
$part->removeHeader('Content-Description'); |
|
53
|
|
|
$part->detachContentStream(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Copies Content-Type, Content-Disposition and Content-Transfer-Encoding |
|
58
|
|
|
* headers from the $from header into the $to header. If the Content-Type |
|
59
|
|
|
* header isn't defined in $from, defaults to text/plain with utf-8 and |
|
60
|
|
|
* quoted-printable. |
|
61
|
|
|
* |
|
62
|
|
|
* @param ParentHeaderPart $from |
|
63
|
|
|
* @param ParentHeaderPart $to |
|
64
|
|
|
*/ |
|
65
|
|
|
public function copyTypeHeadersAndContent(ParentHeaderPart $from, ParentHeaderPart $to, $move = false) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->copyHeader($from, $to, 'Content-Type', 'text/plain; charset=utf-8'); |
|
68
|
|
|
if ($from->getHeader('Content-Type') === null) { |
|
69
|
|
|
$this->copyHeader($from, $to, 'Content-Transfer-Encoding', 'quoted-printable'); |
|
70
|
|
|
} else { |
|
71
|
|
|
$this->copyHeader($from, $to, 'Content-Transfer-Encoding'); |
|
72
|
|
|
} |
|
73
|
|
|
$this->copyHeader($from, $to, 'Content-Disposition'); |
|
74
|
|
|
$this->copyHeader($from, $to, 'Content-ID'); |
|
75
|
|
|
$this->copyHeader($from, $to, 'Content-Description'); |
|
76
|
|
|
if ($from->hasContent()) { |
|
77
|
|
|
$to->attachContentStream($from->getContentStream(), MailMimeParser::DEFAULT_CHARSET); |
|
78
|
|
|
} |
|
79
|
|
|
if ($move) { |
|
80
|
|
|
$this->removeTypeHeadersAndContent($from); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Creates a new content part from the passed part, allowing the part to be |
|
86
|
|
|
* used for something else (e.g. changing a non-mime message to a multipart |
|
87
|
|
|
* mime message). |
|
88
|
|
|
* |
|
89
|
|
|
* @param ParentHeaderPart $part |
|
90
|
|
|
* @return MimePart the newly-created MimePart |
|
91
|
|
|
*/ |
|
92
|
|
|
public function createNewContentPartFrom(ParentHeaderPart $part) |
|
93
|
|
|
{ |
|
94
|
|
|
$mime = $this->partBuilderFactory->newPartBuilder($this->mimePartFactory)->createMessagePart(); |
|
95
|
|
|
$this->copyTypeHeadersAndContent($part, $mime, true); |
|
96
|
|
|
return $mime; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Copies type headers (Content-Type, Content-Disposition, |
|
101
|
|
|
* Content-Transfer-Encoding) from the $from MimePart to $to. Attaches the |
|
102
|
|
|
* content resource handle of $from to $to, and loops over child parts, |
|
103
|
|
|
* removing them from $from and adding them to $to. |
|
104
|
|
|
* |
|
105
|
|
|
* @param ParentHeaderPart $from |
|
106
|
|
|
* @param ParentHeaderPart $to |
|
107
|
|
|
*/ |
|
108
|
|
|
public function movePartContentAndChildren(ParentHeaderPart $from, ParentHeaderPart $to) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->copyTypeHeadersAndContent($from, $to, true); |
|
111
|
|
|
foreach ($from->getChildParts() as $child) { |
|
112
|
|
|
$from->removePart($child); |
|
113
|
|
|
$to->addChild($child); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Replaces the $part ParentHeaderPart with $replacement. |
|
119
|
|
|
* |
|
120
|
|
|
* Essentially removes $part from its parent, and adds $replacement in its |
|
121
|
|
|
* same position. If $part is this Message, its type headers are moved from |
|
122
|
|
|
* this message to $replacement, the content resource is moved, and children |
|
123
|
|
|
* are assigned to $replacement. |
|
124
|
|
|
* |
|
125
|
|
|
* @param ParentHeaderPart $part |
|
126
|
|
|
* @param ParentHeaderPart $replacement |
|
127
|
|
|
*/ |
|
128
|
|
|
public function replacePart(Message $message, ParentHeaderPart $part, ParentHeaderPart $replacement) |
|
129
|
|
|
{ |
|
130
|
|
|
$message->removePart($replacement); |
|
131
|
|
|
if ($part === $message) { |
|
|
|
|
|
|
132
|
|
|
$this->movePartContentAndChildren($replacement, $part); |
|
133
|
|
|
return; |
|
134
|
|
|
} |
|
135
|
|
|
$parent = $part->getParent(); |
|
136
|
|
|
$position = $parent->removePart($part); |
|
137
|
|
|
$parent->addChild($replacement, $position); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|