PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/lib/swift/classes/Swift/Plugins/DecoratorPlugin.php

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