PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/btkm/correct_fb
PHP | 291 lines | 154 code | 41 blank | 96 comment | 15 complexity | 10938b02ea39aca46d7b4359b3974903 MD5 | raw file
  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 = array();
  20. /**
  21. * @var Swift_Signers_BodySigner[]
  22. */
  23. private $bodySigners = array();
  24. /**
  25. * @var array
  26. */
  27. private $savedMessage = array();
  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. array($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. * Create a new Message.
  58. *
  59. * @param string $subject
  60. * @param string $body
  61. * @param string $contentType
  62. * @param string $charset
  63. *
  64. * @return Swift_Message
  65. */
  66. public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null)
  67. {
  68. return new self($subject, $body, $contentType, $charset);
  69. }
  70. /**
  71. * Add a MimePart to this Message.
  72. *
  73. * @param string|Swift_OutputByteStream $body
  74. * @param string $contentType
  75. * @param string $charset
  76. *
  77. * @return Swift_Mime_SimpleMessage
  78. */
  79. public function addPart($body, $contentType = null, $charset = null)
  80. {
  81. return $this->attach(Swift_MimePart::newInstance(
  82. $body, $contentType, $charset
  83. ));
  84. }
  85. /**
  86. * Attach a new signature handler to the message.
  87. *
  88. * @param Swift_Signer $signer
  89. *
  90. * @return Swift_Message
  91. */
  92. public function attachSigner(Swift_Signer $signer)
  93. {
  94. if ($signer instanceof Swift_Signers_HeaderSigner) {
  95. $this->headerSigners[] = $signer;
  96. } elseif ($signer instanceof Swift_Signers_BodySigner) {
  97. $this->bodySigners[] = $signer;
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Attach a new signature handler to the message.
  103. *
  104. * @param Swift_Signer $signer
  105. *
  106. * @return Swift_Message
  107. */
  108. public function detachSigner(Swift_Signer $signer)
  109. {
  110. if ($signer instanceof Swift_Signers_HeaderSigner) {
  111. foreach ($this->headerSigners as $k => $headerSigner) {
  112. if ($headerSigner === $signer) {
  113. unset($this->headerSigners[$k]);
  114. return $this;
  115. }
  116. }
  117. } elseif ($signer instanceof Swift_Signers_BodySigner) {
  118. foreach ($this->bodySigners as $k => $bodySigner) {
  119. if ($bodySigner === $signer) {
  120. unset($this->bodySigners[$k]);
  121. return $this;
  122. }
  123. }
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Get this message as a complete string.
  129. *
  130. * @return string
  131. */
  132. public function toString()
  133. {
  134. if (empty($this->headerSigners) && empty($this->bodySigners)) {
  135. return parent::toString();
  136. }
  137. $this->saveMessage();
  138. $this->doSign();
  139. $string = parent::toString();
  140. $this->restoreMessage();
  141. return $string;
  142. }
  143. /**
  144. * Write this message to a {@link Swift_InputByteStream}.
  145. *
  146. * @param Swift_InputByteStream $is
  147. */
  148. public function toByteStream(Swift_InputByteStream $is)
  149. {
  150. if (empty($this->headerSigners) && empty($this->bodySigners)) {
  151. parent::toByteStream($is);
  152. return;
  153. }
  154. $this->saveMessage();
  155. $this->doSign();
  156. parent::toByteStream($is);
  157. $this->restoreMessage();
  158. }
  159. public function __wakeup()
  160. {
  161. Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message');
  162. }
  163. /**
  164. * loops through signers and apply the signatures.
  165. */
  166. protected function doSign()
  167. {
  168. foreach ($this->bodySigners as $signer) {
  169. $altered = $signer->getAlteredHeaders();
  170. $this->saveHeaders($altered);
  171. $signer->signMessage($this);
  172. }
  173. foreach ($this->headerSigners as $signer) {
  174. $altered = $signer->getAlteredHeaders();
  175. $this->saveHeaders($altered);
  176. $signer->reset();
  177. $signer->setHeaders($this->getHeaders());
  178. $signer->startBody();
  179. $this->_bodyToByteStream($signer);
  180. $signer->endBody();
  181. $signer->addSignature($this->getHeaders());
  182. }
  183. }
  184. /**
  185. * save the message before any signature is applied.
  186. */
  187. protected function saveMessage()
  188. {
  189. $this->savedMessage = array('headers' => array());
  190. $this->savedMessage['body'] = $this->getBody();
  191. $this->savedMessage['children'] = $this->getChildren();
  192. if (count($this->savedMessage['children']) > 0 && $this->getBody() != '') {
  193. $this->setChildren(array_merge(array($this->_becomeMimePart()), $this->savedMessage['children']));
  194. $this->setBody('');
  195. }
  196. }
  197. /**
  198. * save the original headers.
  199. *
  200. * @param array $altered
  201. */
  202. protected function saveHeaders(array $altered)
  203. {
  204. foreach ($altered as $head) {
  205. $lc = strtolower($head);
  206. if (!isset($this->savedMessage['headers'][$lc])) {
  207. $this->savedMessage['headers'][$lc] = $this->getHeaders()->getAll($head);
  208. }
  209. }
  210. }
  211. /**
  212. * Remove or restore altered headers.
  213. */
  214. protected function restoreHeaders()
  215. {
  216. foreach ($this->savedMessage['headers'] as $name => $savedValue) {
  217. $headers = $this->getHeaders()->getAll($name);
  218. foreach ($headers as $key => $value) {
  219. if (!isset($savedValue[$key])) {
  220. $this->getHeaders()->remove($name, $key);
  221. }
  222. }
  223. }
  224. }
  225. /**
  226. * Restore message body.
  227. */
  228. protected function restoreMessage()
  229. {
  230. $this->setBody($this->savedMessage['body']);
  231. $this->setChildren($this->savedMessage['children']);
  232. $this->restoreHeaders();
  233. $this->savedMessage = array();
  234. }
  235. /**
  236. * Clone Message Signers.
  237. *
  238. * @see Swift_Mime_SimpleMimeEntity::__clone()
  239. */
  240. public function __clone()
  241. {
  242. parent::__clone();
  243. foreach ($this->bodySigners as $key => $bodySigner) {
  244. $this->bodySigners[$key] = clone($bodySigner);
  245. }
  246. foreach ($this->headerSigners as $key => $headerSigner) {
  247. $this->headerSigners[$key] = clone($headerSigner);
  248. }
  249. }
  250. }