[ Index ]

MailPress 7.2

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/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 = [];
  27  
  28      /** Bodies of children before they are replaced */
  29      private $originalChildBodies = [];
  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      public function beforeSendPerformed(Swift_Events_SendEvent $evt)
  79      {
  80          $message = $evt->getMessage();
  81          $this->restoreMessage($message);
  82          $to = array_keys($message->getTo());
  83          $address = array_shift($to);
  84          if ($replacements = $this->getReplacementsFor($address)) {
  85              $body = $message->getBody();
  86              $search = array_keys($replacements);
  87              $replace = array_values($replacements);
  88              $bodyReplaced = str_replace(
  89                  $search, $replace, $body
  90                  );
  91              if ($body != $bodyReplaced) {
  92                  $this->originalBody = $body;
  93                  $message->setBody($bodyReplaced);
  94              }
  95  
  96              foreach ($message->getHeaders()->getAll() as $header) {
  97                  $body = $header->getFieldBodyModel();
  98                  $count = 0;
  99                  if (is_array($body)) {
 100                      $bodyReplaced = [];
 101                      foreach ($body as $key => $value) {
 102                          $count1 = 0;
 103                          $count2 = 0;
 104                          $key = is_string($key) ? str_replace($search, $replace, $key, $count1) : $key;
 105                          $value = is_string($value) ? str_replace($search, $replace, $value, $count2) : $value;
 106                          $bodyReplaced[$key] = $value;
 107  
 108                          if (!$count && ($count1 || $count2)) {
 109                              $count = 1;
 110                          }
 111                      }
 112                  } elseif (is_string($body)) {
 113                      $bodyReplaced = str_replace($search, $replace, $body, $count);
 114                  }
 115  
 116                  if ($count) {
 117                      $this->originalHeaders[$header->getFieldName()] = $body;
 118                      $header->setFieldBodyModel($bodyReplaced);
 119                  }
 120              }
 121  
 122              $children = (array) $message->getChildren();
 123              foreach ($children as $child) {
 124                  list($type) = sscanf($child->getContentType(), '%[^/]/%s');
 125                  if ('text' == $type) {
 126                      $body = $child->getBody();
 127                      $bodyReplaced = str_replace(
 128                          $search, $replace, $body
 129                          );
 130                      if ($body != $bodyReplaced) {
 131                          $child->setBody($bodyReplaced);
 132                          $this->originalChildBodies[$child->getId()] = $body;
 133                      }
 134                  }
 135              }
 136              $this->lastMessage = $message;
 137          }
 138      }
 139  
 140      /**
 141       * Find a map of replacements for the address.
 142       *
 143       * If this plugin was provided with a delegate instance of
 144       * {@link Swift_Plugins_Decorator_Replacements} then the call will be
 145       * delegated to it.  Otherwise, it will attempt to find the replacements
 146       * from the array provided in the constructor.
 147       *
 148       * If no replacements can be found, an empty value (NULL) is returned.
 149       *
 150       * @param string $address
 151       *
 152       * @return array
 153       */
 154      public function getReplacementsFor($address)
 155      {
 156          if ($this->replacements instanceof Swift_Plugins_Decorator_Replacements) {
 157              return $this->replacements->getReplacementsFor($address);
 158          }
 159  
 160          return $this->replacements[$address] ?? null;
 161      }
 162  
 163      /**
 164       * Invoked immediately after the Message is sent.
 165       */
 166      public function sendPerformed(Swift_Events_SendEvent $evt)
 167      {
 168          $this->restoreMessage($evt->getMessage());
 169      }
 170  
 171      /** Restore a changed message back to its original state */
 172      private function restoreMessage(Swift_Mime_SimpleMessage $message)
 173      {
 174          if ($this->lastMessage === $message) {
 175              if (isset($this->originalBody)) {
 176                  $message->setBody($this->originalBody);
 177                  $this->originalBody = null;
 178              }
 179              if (!empty($this->originalHeaders)) {
 180                  foreach ($message->getHeaders()->getAll() as $header) {
 181                      if (array_key_exists($header->getFieldName(), $this->originalHeaders)) {
 182                          $header->setFieldBodyModel($this->originalHeaders[$header->getFieldName()]);
 183                      }
 184                  }
 185                  $this->originalHeaders = [];
 186              }
 187              if (!empty($this->originalChildBodies)) {
 188                  $children = (array) $message->getChildren();
 189                  foreach ($children as $child) {
 190                      $id = $child->getId();
 191                      if (array_key_exists($id, $this->originalChildBodies)) {
 192                          $child->setBody($this->originalChildBodies[$id]);
 193                      }
 194                  }
 195                  $this->originalChildBodies = [];
 196              }
 197              $this->lastMessage = null;
 198          }
 199      }
 200  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1