Message.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * @return Swift_Message
  90. */
  91. public function attachSigner(Swift_Signer $signer)
  92. {
  93. if ($signer instanceof Swift_Signers_HeaderSigner) {
  94. $this->headerSigners[] = $signer;
  95. } elseif ($signer instanceof Swift_Signers_BodySigner) {
  96. $this->bodySigners[] = $signer;
  97. }
  98. return $this;
  99. }
  100. /**
  101. * Attach a new signature handler to the message.
  102. *
  103. * @param Swift_Signer $signer
  104. * @return Swift_Message
  105. */
  106. public function detachSigner(Swift_Signer $signer)
  107. {
  108. if ($signer instanceof Swift_Signers_HeaderSigner) {
  109. foreach ($this->headerSigners as $k => $headerSigner) {
  110. if ($headerSigner === $signer) {
  111. unset($this->headerSigners[$k]);
  112. return $this;
  113. }
  114. }
  115. } elseif ($signer instanceof Swift_Signers_BodySigner) {
  116. foreach ($this->bodySigners as $k => $bodySigner) {
  117. if ($bodySigner === $signer) {
  118. unset($this->bodySigners[$k]);
  119. return $this;
  120. }
  121. }
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Get this message as a complete string.
  127. *
  128. * @return string
  129. */
  130. public function toString()
  131. {
  132. if (empty($this->headerSigners) && empty($this->bodySigners)) {
  133. return parent::toString();
  134. }
  135. $this->saveMessage();
  136. $this->doSign();
  137. $string = parent::toString();
  138. $this->restoreMessage();
  139. return $string;
  140. }
  141. /**
  142. * Write this message to a {@link Swift_InputByteStream}.
  143. *
  144. * @param Swift_InputByteStream $is
  145. */
  146. public function toByteStream(Swift_InputByteStream $is)
  147. {
  148. if (empty($this->headerSigners) && empty($this->bodySigners)) {
  149. parent::toByteStream($is);
  150. return;
  151. }
  152. $this->saveMessage();
  153. $this->doSign();
  154. parent::toByteStream($is);
  155. $this->restoreMessage();
  156. }
  157. public function __wakeup()
  158. {
  159. Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message');
  160. }
  161. /**
  162. * loops through signers and apply the signatures
  163. */
  164. protected function doSign()
  165. {
  166. foreach ($this->bodySigners as $signer) {
  167. $altered = $signer->getAlteredHeaders();
  168. $this->saveHeaders($altered);
  169. $signer->signMessage($this);
  170. }
  171. foreach ($this->headerSigners as $signer) {
  172. $altered = $signer->getAlteredHeaders();
  173. $this->saveHeaders($altered);
  174. $signer->reset();
  175. $signer->setHeaders($this->getHeaders());
  176. $signer->startBody();
  177. $this->_bodyToByteStream($signer);
  178. $signer->endBody();
  179. $signer->addSignature($this->getHeaders());
  180. }
  181. }
  182. /**
  183. * save the message before any signature is applied
  184. */
  185. protected function saveMessage()
  186. {
  187. $this->savedMessage = array('headers'=> array());
  188. $this->savedMessage['body'] = $this->getBody();
  189. $this->savedMessage['children'] = $this->getChildren();
  190. if (count($this->savedMessage['children']) > 0 && $this->getBody() != '') {
  191. $this->setChildren(array_merge(array($this->_becomeMimePart()), $this->savedMessage['children']));
  192. $this->setBody('');
  193. }
  194. }
  195. /**
  196. * save the original headers
  197. * @param array $altered
  198. */
  199. protected function saveHeaders(array $altered)
  200. {
  201. foreach ($altered as $head) {
  202. $lc = strtolower($head);
  203. if (!isset($this->savedMessage['headers'][$lc])) {
  204. $this->savedMessage['headers'][$lc] = $this->getHeaders()->getAll($head);
  205. }
  206. }
  207. }
  208. /**
  209. * Remove or restore altered headers
  210. */
  211. protected function restoreHeaders()
  212. {
  213. foreach ($this->savedMessage['headers'] as $name => $savedValue) {
  214. $headers = $this->getHeaders()->getAll($name);
  215. foreach ($headers as $key => $value) {
  216. if (!isset($savedValue[$key])) {
  217. $this->getHeaders()->remove($name, $key);
  218. }
  219. }
  220. }
  221. }
  222. /**
  223. * Restore message body
  224. */
  225. protected function restoreMessage()
  226. {
  227. $this->setBody($this->savedMessage['body']);
  228. $this->setChildren($this->savedMessage['children']);
  229. $this->restoreHeaders();
  230. $this->savedMessage = array();
  231. }
  232. }