[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Plugins/ -> DecoratorPlugin.php (source)

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


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1