TYPO3  7.6
vendor/swiftmailer/swiftmailer/lib/classes/Swift/Message.php
Go to the documentation of this file.
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 
17 {
21  private $headerSigners = array();
22 
26  private $bodySigners = array();
27 
31  private $savedMessage = array();
32 
43  public function __construct($subject = null, $body = null, $contentType = null, $charset = null)
44  {
45  call_user_func_array(
46  array($this, 'Swift_Mime_SimpleMessage::__construct'),
48  ->createDependenciesFor('mime.message')
49  );
50 
51  if (!isset($charset)) {
53  ->lookup('properties.charset');
54  }
55  $this->setSubject($subject);
56  $this->setBody($body);
57  $this->setCharset($charset);
58  if ($contentType) {
59  $this->setContentType($contentType);
60  }
61  }
62 
73  public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null)
74  {
75  return new self($subject, $body, $contentType, $charset);
76  }
77 
87  public function addPart($body, $contentType = null, $charset = null)
88  {
89  return $this->attach(Swift_MimePart::newInstance(
90  $body, $contentType, $charset
91  ));
92  }
93 
101  public function attachSigner(Swift_Signer $signer)
102  {
103  if ($signer instanceof Swift_Signers_HeaderSigner) {
104  $this->headerSigners[] = $signer;
105  } elseif ($signer instanceof Swift_Signers_BodySigner) {
106  $this->bodySigners[] = $signer;
107  }
108 
109  return $this;
110  }
111 
119  public function detachSigner(Swift_Signer $signer)
120  {
121  if ($signer instanceof Swift_Signers_HeaderSigner) {
122  foreach ($this->headerSigners as $k => $headerSigner) {
123  if ($headerSigner === $signer) {
124  unset($this->headerSigners[$k]);
125 
126  return $this;
127  }
128  }
129  } elseif ($signer instanceof Swift_Signers_BodySigner) {
130  foreach ($this->bodySigners as $k => $bodySigner) {
131  if ($bodySigner === $signer) {
132  unset($this->bodySigners[$k]);
133 
134  return $this;
135  }
136  }
137  }
138 
139  return $this;
140  }
141 
147  public function toString()
148  {
149  if (empty($this->headerSigners) && empty($this->bodySigners)) {
150  return parent::toString();
151  }
152 
153  $this->saveMessage();
154 
155  $this->doSign();
156 
157  $string = parent::toString();
158 
159  $this->restoreMessage();
160 
161  return $string;
162  }
163 
169  public function toByteStream(Swift_InputByteStream $is)
170  {
171  if (empty($this->headerSigners) && empty($this->bodySigners)) {
172  parent::toByteStream($is);
173 
174  return;
175  }
176 
177  $this->saveMessage();
178 
179  $this->doSign();
180 
181  parent::toByteStream($is);
182 
183  $this->restoreMessage();
184  }
185 
186  public function __wakeup()
187  {
188  Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message');
189  }
190 
194  protected function doSign()
195  {
196  foreach ($this->bodySigners as $signer) {
197  $altered = $signer->getAlteredHeaders();
198  $this->saveHeaders($altered);
199  $signer->signMessage($this);
200  }
201 
202  foreach ($this->headerSigners as $signer) {
203  $altered = $signer->getAlteredHeaders();
204  $this->saveHeaders($altered);
205  $signer->reset();
206 
207  $signer->setHeaders($this->getHeaders());
208 
209  $signer->startBody();
210  $this->_bodyToByteStream($signer);
211  $signer->endBody();
212 
213  $signer->addSignature($this->getHeaders());
214  }
215  }
216 
220  protected function saveMessage()
221  {
222  $this->savedMessage = array('headers' => array());
223  $this->savedMessage['body'] = $this->getBody();
224  $this->savedMessage['children'] = $this->getChildren();
225  if (count($this->savedMessage['children']) > 0 && $this->getBody() != '') {
226  $this->setChildren(array_merge(array($this->_becomeMimePart()), $this->savedMessage['children']));
227  $this->setBody('');
228  }
229  }
230 
236  protected function saveHeaders(array $altered)
237  {
238  foreach ($altered as $head) {
239  $lc = strtolower($head);
240 
241  if (!isset($this->savedMessage['headers'][$lc])) {
242  $this->savedMessage['headers'][$lc] = $this->getHeaders()->getAll($head);
243  }
244  }
245  }
246 
250  protected function restoreHeaders()
251  {
252  foreach ($this->savedMessage['headers'] as $name => $savedValue) {
253  $headers = $this->getHeaders()->getAll($name);
254 
255  foreach ($headers as $key => $value) {
256  if (!isset($savedValue[$key])) {
257  $this->getHeaders()->remove($name, $key);
258  }
259  }
260  }
261  }
262 
266  protected function restoreMessage()
267  {
268  $this->setBody($this->savedMessage['body']);
269  $this->setChildren($this->savedMessage['children']);
270 
271  $this->restoreHeaders();
272  $this->savedMessage = array();
273  }
274 
280  public function __clone()
281  {
282  parent::__clone();
283  foreach ($this->bodySigners as $key => $bodySigner) {
284  $this->bodySigners[$key] = clone($bodySigner);
285  }
286 
287  foreach ($this->headerSigners as $key => $headerSigner) {
288  $this->headerSigners[$key] = clone($headerSigner);
289  }
290  }
291 }