|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of SwiftMailer. |
|
5
|
|
|
* (c) 2004-2009 Chris Corbyn |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The Message class for building emails. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Chris Corbyn |
|
15
|
|
|
*/ |
|
16
|
|
|
class Swift_Message extends Swift_Mime_SimpleMessage |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Swift_Signers_HeaderSigner[] |
|
20
|
|
|
*/ |
|
21
|
|
|
private $headerSigners = array(); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Swift_Signers_BodySigner[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $bodySigners = array(); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $savedMessage = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create a new Message. |
|
35
|
|
|
* |
|
36
|
|
|
* Details may be optionally passed into the constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $subject |
|
39
|
|
|
* @param string $body |
|
40
|
|
|
* @param string $contentType |
|
41
|
|
|
* @param string $charset |
|
42
|
|
|
*/ |
|
43
|
109 |
|
public function __construct($subject = null, $body = null, $contentType = null, $charset = null) |
|
44
|
|
|
{ |
|
45
|
109 |
|
call_user_func_array( |
|
46
|
109 |
|
array($this, 'Swift_Mime_SimpleMessage::__construct'), |
|
47
|
109 |
|
Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message') |
|
48
|
109 |
|
); |
|
49
|
|
|
|
|
50
|
109 |
|
if (!isset($charset)) { |
|
51
|
109 |
|
$charset = Swift_DependencyContainer::getInstance()->lookup('properties.charset'); |
|
52
|
109 |
|
} |
|
53
|
|
|
|
|
54
|
109 |
|
$this->setSubject($subject); |
|
55
|
109 |
|
$this->setBody($body); |
|
56
|
109 |
|
$this->setCharset($charset); |
|
57
|
109 |
|
if ($contentType) { |
|
|
|
|
|
|
58
|
10 |
|
$this->setContentType($contentType); |
|
59
|
10 |
|
} |
|
60
|
109 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Create a new Message. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $subject |
|
66
|
|
|
* @param string $body |
|
67
|
|
|
* @param string $contentType |
|
68
|
|
|
* @param string $charset |
|
69
|
|
|
* |
|
70
|
|
|
* @return Swift_Message |
|
71
|
|
|
*/ |
|
72
|
59 |
|
public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null) |
|
73
|
|
|
{ |
|
74
|
59 |
|
return new self($subject, $body, $contentType, $charset); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add a MimePart to this Message. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string|Swift_OutputByteStream $body |
|
81
|
|
|
* @param string $contentType |
|
82
|
|
|
* @param string $charset |
|
83
|
|
|
* |
|
84
|
|
|
* @return Swift_Mime_SimpleMessage |
|
85
|
|
|
*/ |
|
86
|
3 |
|
public function addPart($body, $contentType = null, $charset = null) |
|
87
|
|
|
{ |
|
88
|
3 |
|
return $this->attach( |
|
89
|
3 |
|
Swift_MimePart::newInstance( |
|
90
|
3 |
|
$body, |
|
|
|
|
|
|
91
|
3 |
|
$contentType, |
|
92
|
|
|
$charset |
|
93
|
3 |
|
) |
|
94
|
3 |
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Attach a new signature handler to the message. |
|
99
|
|
|
* |
|
100
|
|
|
* @param Swift_Signer $signer |
|
101
|
|
|
* |
|
102
|
|
|
* @return Swift_Message |
|
103
|
|
|
*/ |
|
104
|
9 |
|
public function attachSigner(Swift_Signer $signer) |
|
105
|
|
|
{ |
|
106
|
9 |
|
if ($signer instanceof Swift_Signers_HeaderSigner) { |
|
107
|
1 |
|
$this->headerSigners[] = $signer; |
|
108
|
9 |
|
} elseif ($signer instanceof Swift_Signers_BodySigner) { |
|
109
|
8 |
|
$this->bodySigners[] = $signer; |
|
110
|
8 |
|
} |
|
111
|
|
|
|
|
112
|
9 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Attach a new signature handler to the message. |
|
117
|
|
|
* |
|
118
|
|
|
* @param Swift_Signer $signer |
|
119
|
|
|
* |
|
120
|
|
|
* @return Swift_Message |
|
121
|
|
|
*/ |
|
122
|
|
|
public function detachSigner(Swift_Signer $signer) |
|
123
|
|
|
{ |
|
124
|
|
|
if ($signer instanceof Swift_Signers_HeaderSigner) { |
|
125
|
|
|
foreach ($this->headerSigners as $k => $headerSigner) { |
|
126
|
|
|
if ($headerSigner === $signer) { |
|
127
|
|
|
unset($this->headerSigners[$k]); |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} elseif ($signer instanceof Swift_Signers_BodySigner) { |
|
133
|
|
|
foreach ($this->bodySigners as $k => $bodySigner) { |
|
134
|
|
|
if ($bodySigner === $signer) { |
|
135
|
|
|
unset($this->bodySigners[$k]); |
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get this message as a complete string. |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
82 |
View Code Duplication |
public function toString() |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
82 |
|
if (empty($this->headerSigners) && empty($this->bodySigners)) { |
|
153
|
82 |
|
return parent::toString(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$this->saveMessage(); |
|
157
|
|
|
|
|
158
|
|
|
$this->doSign(); |
|
159
|
|
|
|
|
160
|
|
|
$string = parent::toString(); |
|
161
|
|
|
|
|
162
|
|
|
$this->restoreMessage(); |
|
163
|
|
|
|
|
164
|
|
|
return $string; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Write this message to a {@link Swift_InputByteStream}. |
|
169
|
|
|
* |
|
170
|
|
|
* @param Swift_InputByteStream $is |
|
171
|
|
|
*/ |
|
172
|
17 |
View Code Duplication |
public function toByteStream(Swift_InputByteStream $is) |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
17 |
|
if (empty($this->headerSigners) && empty($this->bodySigners)) { |
|
175
|
17 |
|
parent::toByteStream($is); |
|
176
|
|
|
|
|
177
|
17 |
|
return; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
8 |
|
$this->saveMessage(); |
|
181
|
|
|
|
|
182
|
8 |
|
$this->doSign(); |
|
183
|
|
|
|
|
184
|
8 |
|
parent::toByteStream($is); |
|
185
|
|
|
|
|
186
|
8 |
|
$this->restoreMessage(); |
|
187
|
8 |
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function __wakeup() |
|
190
|
|
|
{ |
|
191
|
|
|
Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message'); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* loops through signers and apply the signatures. |
|
196
|
|
|
*/ |
|
197
|
8 |
|
protected function doSign() |
|
198
|
|
|
{ |
|
199
|
8 |
|
foreach ($this->bodySigners as $signer) { |
|
200
|
8 |
|
$altered = $signer->getAlteredHeaders(); |
|
201
|
8 |
|
$this->saveHeaders($altered); |
|
202
|
8 |
|
$signer->signMessage($this); |
|
203
|
8 |
|
} |
|
204
|
|
|
|
|
205
|
8 |
|
foreach ($this->headerSigners as $signer) { |
|
206
|
|
|
$altered = $signer->getAlteredHeaders(); |
|
207
|
|
|
$this->saveHeaders($altered); |
|
208
|
5 |
|
$signer->reset(); |
|
209
|
|
|
|
|
210
|
|
|
$signer->setHeaders($this->getHeaders()); |
|
211
|
|
|
|
|
212
|
|
|
$signer->startBody(); |
|
213
|
|
|
$this->_bodyToByteStream($signer); |
|
214
|
|
|
$signer->endBody(); |
|
215
|
|
|
|
|
216
|
|
|
$signer->addSignature($this->getHeaders()); |
|
217
|
8 |
|
} |
|
218
|
8 |
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* save the message before any signature is applied. |
|
222
|
|
|
*/ |
|
223
|
8 |
|
protected function saveMessage() |
|
224
|
|
|
{ |
|
225
|
8 |
|
$this->savedMessage = array('headers' => array()); |
|
226
|
8 |
|
$this->savedMessage['body'] = $this->getBody(); |
|
227
|
8 |
|
$this->savedMessage['children'] = $this->getChildren(); |
|
228
|
|
|
|
|
229
|
|
|
if ( |
|
230
|
8 |
|
count($this->savedMessage['children']) > 0 |
|
231
|
8 |
|
&& |
|
232
|
1 |
|
$this->getBody() != '' |
|
233
|
8 |
|
) { |
|
234
|
1 |
|
$this->setChildren(array_merge(array($this->_becomeMimePart()), $this->savedMessage['children'])); |
|
235
|
1 |
|
$this->setBody(''); |
|
236
|
1 |
|
} |
|
237
|
8 |
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* save the original headers. |
|
241
|
|
|
* |
|
242
|
|
|
* @param array $altered |
|
243
|
|
|
*/ |
|
244
|
8 |
|
protected function saveHeaders(array $altered) |
|
245
|
|
|
{ |
|
246
|
8 |
|
foreach ($altered as $head) { |
|
247
|
8 |
|
$lc = strtolower($head); |
|
248
|
|
|
|
|
249
|
8 |
|
if (!isset($this->savedMessage['headers'][$lc])) { |
|
250
|
8 |
|
$this->savedMessage['headers'][$lc] = $this->getHeaders()->getAll($head); |
|
251
|
8 |
|
} |
|
252
|
8 |
|
} |
|
253
|
8 |
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Remove or restore altered headers. |
|
257
|
|
|
*/ |
|
258
|
8 |
|
protected function restoreHeaders() |
|
259
|
|
|
{ |
|
260
|
8 |
|
foreach ($this->savedMessage['headers'] as $name => $savedValue) { |
|
261
|
8 |
|
$headers = $this->getHeaders()->getAll($name); |
|
262
|
|
|
|
|
263
|
8 |
|
foreach ($headers as $key => $value) { |
|
264
|
8 |
|
if (!isset($savedValue[$key])) { |
|
265
|
4 |
|
$this->getHeaders()->remove($name, $key); |
|
266
|
4 |
|
} |
|
267
|
8 |
|
} |
|
268
|
8 |
|
} |
|
269
|
8 |
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Restore message body. |
|
273
|
|
|
*/ |
|
274
|
8 |
|
protected function restoreMessage() |
|
275
|
|
|
{ |
|
276
|
8 |
|
$this->setBody($this->savedMessage['body']); |
|
277
|
8 |
|
$this->setChildren($this->savedMessage['children']); |
|
278
|
|
|
|
|
279
|
8 |
|
$this->restoreHeaders(); |
|
280
|
8 |
|
$this->savedMessage = array(); |
|
281
|
8 |
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Clone Message Signers. |
|
285
|
|
|
* |
|
286
|
|
|
* @see Swift_Mime_SimpleMimeEntity::__clone() |
|
287
|
|
|
*/ |
|
288
|
5 |
|
public function __clone() |
|
289
|
|
|
{ |
|
290
|
5 |
|
parent::__clone(); |
|
291
|
|
|
|
|
292
|
5 |
|
foreach ($this->bodySigners as $key => $bodySigner) { |
|
293
|
|
|
$this->bodySigners[$key] = clone($bodySigner); |
|
294
|
5 |
|
} |
|
295
|
|
|
|
|
296
|
5 |
|
foreach ($this->headerSigners as $key => $headerSigner) { |
|
297
|
1 |
|
$this->headerSigners[$key] = clone($headerSigner); |
|
298
|
5 |
|
} |
|
299
|
5 |
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.