[ Index ]

MailPress 7.1

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/ -> Message.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   * The Message class for building emails.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Message extends Swift_Mime_SimpleMessage
  17  {
  18      /**
  19       * @var Swift_Signers_HeaderSigner[]
  20       */
  21      private $headerSigners = [];
  22  
  23      /**
  24       * @var Swift_Signers_BodySigner[]
  25       */
  26      private $bodySigners = [];
  27  
  28      /**
  29       * @var array
  30       */
  31      private $savedMessage = [];
  32  
  33      /**
  34       * Create a new Message.
  35       *
  36       * Details may be optionally passed into the constructor.
  37       *
  38       * @param string $subject
  39       * @param string $body
  40       * @param string $contentType
  41       * @param string $charset
  42       */
  43      public function __construct($subject = null, $body = null, $contentType = null, $charset = null)
  44      {
  45          call_user_func_array(
  46              [$this, 'Swift_Mime_SimpleMessage::__construct'],
  47              Swift_DependencyContainer::getInstance()
  48                  ->createDependenciesFor('mime.message')
  49              );
  50  
  51          if (!isset($charset)) {
  52              $charset = Swift_DependencyContainer::getInstance()
  53                  ->lookup('properties.charset');
  54          }
  55          $this->setSubject($subject);
  56          $this->setBody($body);
  57          $this->setCharset($charset);
  58          if ($contentType) {
  59              $this->setContentType($contentType);
  60          }
  61      }
  62  
  63      /**
  64       * Add a MimePart to this Message.
  65       *
  66       * @param string|Swift_OutputByteStream $body
  67       * @param string                        $contentType
  68       * @param string                        $charset
  69       *
  70       * @return $this
  71       */
  72      public function addPart($body, $contentType = null, $charset = null)
  73      {
  74          return $this->attach((new Swift_MimePart($body, $contentType, $charset))->setEncoder($this->getEncoder()));
  75      }
  76  
  77      /**
  78       * Attach a new signature handler to the message.
  79       *
  80       * @return $this
  81       */
  82      public function attachSigner(Swift_Signer $signer)
  83      {
  84          if ($signer instanceof Swift_Signers_HeaderSigner) {
  85              $this->headerSigners[] = $signer;
  86          } elseif ($signer instanceof Swift_Signers_BodySigner) {
  87              $this->bodySigners[] = $signer;
  88          }
  89  
  90          return $this;
  91      }
  92  
  93      /**
  94       * Detach a signature handler from a message.
  95       *
  96       * @return $this
  97       */
  98      public function detachSigner(Swift_Signer $signer)
  99      {
 100          if ($signer instanceof Swift_Signers_HeaderSigner) {
 101              foreach ($this->headerSigners as $k => $headerSigner) {
 102                  if ($headerSigner === $signer) {
 103                      unset($this->headerSigners[$k]);
 104  
 105                      return $this;
 106                  }
 107              }
 108          } elseif ($signer instanceof Swift_Signers_BodySigner) {
 109              foreach ($this->bodySigners as $k => $bodySigner) {
 110                  if ($bodySigner === $signer) {
 111                      unset($this->bodySigners[$k]);
 112  
 113                      return $this;
 114                  }
 115              }
 116          }
 117  
 118          return $this;
 119      }
 120  
 121      /**
 122       * Clear all signature handlers attached to the message.
 123       *
 124       * @return $this
 125       */
 126      public function clearSigners()
 127      {
 128          $this->headerSigners = [];
 129          $this->bodySigners = [];
 130  
 131          return $this;
 132      }
 133  
 134      /**
 135       * Get this message as a complete string.
 136       *
 137       * @return string
 138       */
 139      public function toString()
 140      {
 141          if (empty($this->headerSigners) && empty($this->bodySigners)) {
 142              return parent::toString();
 143          }
 144  
 145          $this->saveMessage();
 146  
 147          $this->doSign();
 148  
 149          $string = parent::toString();
 150  
 151          $this->restoreMessage();
 152  
 153          return $string;
 154      }
 155  
 156      /**
 157       * Write this message to a {@link Swift_InputByteStream}.
 158       */
 159      public function toByteStream(Swift_InputByteStream $is)
 160      {
 161          if (empty($this->headerSigners) && empty($this->bodySigners)) {
 162              parent::toByteStream($is);
 163  
 164              return;
 165          }
 166  
 167          $this->saveMessage();
 168  
 169          $this->doSign();
 170  
 171          parent::toByteStream($is);
 172  
 173          $this->restoreMessage();
 174      }
 175  
 176      public function __wakeup()
 177      {
 178          Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message');
 179      }
 180  
 181      /**
 182       * loops through signers and apply the signatures.
 183       */
 184      protected function doSign()
 185      {
 186          foreach ($this->bodySigners as $signer) {
 187              $altered = $signer->getAlteredHeaders();
 188              $this->saveHeaders($altered);
 189              $signer->signMessage($this);
 190          }
 191  
 192          foreach ($this->headerSigners as $signer) {
 193              $altered = $signer->getAlteredHeaders();
 194              $this->saveHeaders($altered);
 195              $signer->reset();
 196  
 197              $signer->setHeaders($this->getHeaders());
 198  
 199              $signer->startBody();
 200              $this->bodyToByteStream($signer);
 201              $signer->endBody();
 202  
 203              $signer->addSignature($this->getHeaders());
 204          }
 205      }
 206  
 207      /**
 208       * save the message before any signature is applied.
 209       */
 210      protected function saveMessage()
 211      {
 212          $this->savedMessage = ['headers' => []];
 213          $this->savedMessage['body'] = $this->getBody();
 214          $this->savedMessage['children'] = $this->getChildren();
 215          if (count($this->savedMessage['children']) > 0 && '' != $this->getBody()) {
 216              $this->setChildren(array_merge([$this->becomeMimePart()], $this->savedMessage['children']));
 217              $this->setBody('');
 218          }
 219      }
 220  
 221      /**
 222       * save the original headers.
 223       */
 224      protected function saveHeaders(array $altered)
 225      {
 226          foreach ($altered as $head) {
 227              $lc = strtolower($head);
 228  
 229              if (!isset($this->savedMessage['headers'][$lc])) {
 230                  $this->savedMessage['headers'][$lc] = $this->getHeaders()->getAll($head);
 231              }
 232          }
 233      }
 234  
 235      /**
 236       * Remove or restore altered headers.
 237       */
 238      protected function restoreHeaders()
 239      {
 240          foreach ($this->savedMessage['headers'] as $name => $savedValue) {
 241              $headers = $this->getHeaders()->getAll($name);
 242  
 243              foreach ($headers as $key => $value) {
 244                  if (!isset($savedValue[$key])) {
 245                      $this->getHeaders()->remove($name, $key);
 246                  }
 247              }
 248          }
 249      }
 250  
 251      /**
 252       * Restore message body.
 253       */
 254      protected function restoreMessage()
 255      {
 256          $this->setBody($this->savedMessage['body']);
 257          $this->setChildren($this->savedMessage['children']);
 258  
 259          $this->restoreHeaders();
 260          $this->savedMessage = [];
 261      }
 262  
 263      /**
 264       * Clone Message Signers.
 265       *
 266       * @see Swift_Mime_SimpleMimeEntity::__clone()
 267       */
 268      public function __clone()
 269      {
 270          parent::__clone();
 271          foreach ($this->bodySigners as $key => $bodySigner) {
 272              $this->bodySigners[$key] = clone $bodySigner;
 273          }
 274  
 275          foreach ($this->headerSigners as $key => $headerSigner) {
 276              $this->headerSigners[$key] = clone $headerSigner;
 277          }
 278      }
 279  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1