DecoratorPlugin.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * Allows customization of Messages on-the-fly.
  11. *
  12. * @package Swift
  13. * @subpackage Plugins
  14. *
  15. * @author Chris Corbyn
  16. * @author Fabien Potencier
  17. */
  18. class Swift_Plugins_DecoratorPlugin
  19. implements Swift_Events_SendListener, Swift_Plugins_Decorator_Replacements
  20. {
  21. /** The replacement map */
  22. private $_replacements;
  23. /** The body as it was before replacements */
  24. private $_originalBody;
  25. /** The original headers of the message, before replacements */
  26. private $_originalHeaders = array();
  27. /** Bodies of children before they are replaced */
  28. private $_originalChildBodies = array();
  29. /** The Message that was last replaced */
  30. private $_lastMessage;
  31. /**
  32. * Create a new DecoratorPlugin with $replacements.
  33. *
  34. * The $replacements can either be an associative array, or an implementation
  35. * of {@link Swift_Plugins_Decorator_Replacements}.
  36. *
  37. * When using an array, it should be of the form:
  38. * <code>
  39. * $replacements = array(
  40. * "address1@domain.tld" => array("{a}" => "b", "{c}" => "d"),
  41. * "address2@domain.tld" => array("{a}" => "x", "{c}" => "y")
  42. * )
  43. * </code>
  44. *
  45. * When using an instance of {@link Swift_Plugins_Decorator_Replacements},
  46. * the object should return just the array of replacements for the address
  47. * given to {@link Swift_Plugins_Decorator_Replacements::getReplacementsFor()}.
  48. *
  49. * @param mixed $replacements
  50. */
  51. public function __construct($replacements)
  52. {
  53. if (!($replacements instanceof Swift_Plugins_Decorator_Replacements))
  54. {
  55. $this->_replacements = (array) $replacements;
  56. }
  57. else
  58. {
  59. $this->_replacements = $replacements;
  60. }
  61. }
  62. /**
  63. * Invoked immediately before the Message is sent.
  64. *
  65. * @param Swift_Events_SendEvent $evt
  66. */
  67. public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  68. {
  69. $message = $evt->getMessage();
  70. $this->_restoreMessage($message);
  71. $to = array_keys($message->getTo());
  72. $address = array_shift($to);
  73. if ($replacements = $this->getReplacementsFor($address))
  74. {
  75. $body = $message->getBody();
  76. $search = array_keys($replacements);
  77. $replace = array_values($replacements);
  78. $bodyReplaced = str_replace(
  79. $search, $replace, $body
  80. );
  81. if ($body != $bodyReplaced)
  82. {
  83. $this->_originalBody = $body;
  84. $message->setBody($bodyReplaced);
  85. }
  86. foreach ($message->getHeaders()->getAll() as $header)
  87. {
  88. $body = $header->getFieldBodyModel();
  89. $count = 0;
  90. if (is_array($body))
  91. {
  92. $bodyReplaced = array();
  93. foreach ($body as $key => $value)
  94. {
  95. $count1 = 0;
  96. $count2 = 0;
  97. $key = is_string($key) ? str_replace($search, $replace, $key, $count1) : $key;
  98. $value = is_string($value) ? str_replace($search, $replace, $value, $count2) : $value;
  99. $bodyReplaced[$key] = $value;
  100. if (!$count && ($count1 || $count2))
  101. {
  102. $count = 1;
  103. }
  104. }
  105. }
  106. else
  107. {
  108. $bodyReplaced = str_replace($search, $replace, $body, $count);
  109. }
  110. if ($count)
  111. {
  112. $this->_originalHeaders[$header->getFieldName()] = $body;
  113. $header->setFieldBodyModel($bodyReplaced);
  114. }
  115. }
  116. $children = (array) $message->getChildren();
  117. foreach ($children as $child)
  118. {
  119. list($type, ) = sscanf($child->getContentType(), '%[^/]/%s');
  120. if ('text' == $type)
  121. {
  122. $body = $child->getBody();
  123. $bodyReplaced = str_replace(
  124. $search, $replace, $body
  125. );
  126. if ($body != $bodyReplaced)
  127. {
  128. $child->setBody($bodyReplaced);
  129. $this->_originalChildBodies[$child->getId()] = $body;
  130. }
  131. }
  132. }
  133. $this->_lastMessage = $message;
  134. }
  135. }
  136. /**
  137. * Find a map of replacements for the address.
  138. *
  139. * If this plugin was provided with a delegate instance of
  140. * {@link Swift_Plugins_Decorator_Replacements} then the call will be
  141. * delegated to it. Otherwise, it will attempt to find the replacements
  142. * from the array provided in the constructor.
  143. *
  144. * If no replacements can be found, an empty value (NULL) is returned.
  145. *
  146. * @param string $address
  147. *
  148. * @return array
  149. */
  150. public function getReplacementsFor($address)
  151. {
  152. if ($this->_replacements instanceof Swift_Plugins_Decorator_Replacements)
  153. {
  154. return $this->_replacements->getReplacementsFor($address);
  155. }
  156. else
  157. {
  158. return isset($this->_replacements[$address])
  159. ? $this->_replacements[$address]
  160. : null
  161. ;
  162. }
  163. }
  164. /**
  165. * Invoked immediately after the Message is sent.
  166. *
  167. * @param Swift_Events_SendEvent $evt
  168. */
  169. public function sendPerformed(Swift_Events_SendEvent $evt)
  170. {
  171. $this->_restoreMessage($evt->getMessage());
  172. }
  173. // -- Private methods
  174. /** Restore a changed message back to its original state */
  175. private function _restoreMessage(Swift_Mime_Message $message)
  176. {
  177. if ($this->_lastMessage === $message)
  178. {
  179. if (isset($this->_originalBody))
  180. {
  181. $message->setBody($this->_originalBody);
  182. $this->_originalBody = null;
  183. }
  184. if (!empty($this->_originalHeaders))
  185. {
  186. foreach ($message->getHeaders()->getAll() as $header)
  187. {
  188. if (array_key_exists($header->getFieldName(), $this->_originalHeaders))
  189. {
  190. $header->setFieldBodyModel($this->_originalHeaders[$header->getFieldName()]);
  191. }
  192. }
  193. $this->_originalHeaders = array();
  194. }
  195. if (!empty($this->_originalChildBodies))
  196. {
  197. $children = (array) $message->getChildren();
  198. foreach ($children as $child)
  199. {
  200. $id = $child->getId();
  201. if (array_key_exists($id, $this->_originalChildBodies))
  202. {
  203. $child->setBody($this->_originalChildBodies[$id]);
  204. }
  205. }
  206. $this->_originalChildBodies = array();
  207. }
  208. $this->_lastMessage = null;
  209. }
  210. }
  211. }