vendor/swiftmailer/swiftmailer/lib/classes/Swift/Message.php line 48

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