[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/Swiftmailer/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 = array();
  22  
  23      /**
  24       * @var Swift_Signers_BodySigner[]
  25       */
  26      private $bodySigners = array();
  27  
  28      /**
  29       * @var array
  30       */
  31      private $savedMessage = array();
  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              array($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       * Create a new Message.
  65       *
  66       * @param string $subject
  67       * @param string $body
  68       * @param string $contentType
  69       * @param string $charset
  70       *
  71       * @return Swift_Message
  72       */
  73      public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null)
  74      {
  75          return new self($subject, $body, $contentType, $charset);
  76      }
  77  
  78      /**
  79       * Add a MimePart to this Message.
  80       *
  81       * @param string|Swift_OutputByteStream $body
  82       * @param string                        $contentType
  83       * @param string                        $charset
  84       *
  85       * @return Swift_Mime_SimpleMessage
  86       */
  87      public function addPart($body, $contentType = null, $charset = null)
  88      {
  89          return $this->attach(Swift_MimePart::newInstance(
  90              $body, $contentType, $charset
  91              ));
  92      }
  93  
  94      /**
  95       * Attach a new signature handler to the message.
  96       *
  97       * @param Swift_Signer $signer
  98       *
  99       * @return Swift_Message
 100       */
 101      public function attachSigner(Swift_Signer $signer)
 102      {
 103          if ($signer instanceof Swift_Signers_HeaderSigner) {
 104              $this->headerSigners[] = $signer;
 105          } elseif ($signer instanceof Swift_Signers_BodySigner) {
 106              $this->bodySigners[] = $signer;
 107          }
 108  
 109          return $this;
 110      }
 111  
 112      /**
 113       * Attach a new signature handler to the message.
 114       *
 115       * @param Swift_Signer $signer
 116       *
 117       * @return Swift_Message
 118       */
 119      public function detachSigner(Swift_Signer $signer)
 120      {
 121          if ($signer instanceof Swift_Signers_HeaderSigner) {
 122              foreach ($this->headerSigners as $k => $headerSigner) {
 123                  if ($headerSigner === $signer) {
 124                      unset($this->headerSigners[$k]);
 125  
 126                      return $this;
 127                  }
 128              }
 129          } elseif ($signer instanceof Swift_Signers_BodySigner) {
 130              foreach ($this->bodySigners as $k => $bodySigner) {
 131                  if ($bodySigner === $signer) {
 132                      unset($this->bodySigners[$k]);
 133  
 134                      return $this;
 135                  }
 136              }
 137          }
 138  
 139          return $this;
 140      }
 141  
 142      /**
 143       * Get this message as a complete string.
 144       *
 145       * @return string
 146       */
 147      public function toString()
 148      {
 149          if (empty($this->headerSigners) && empty($this->bodySigners)) {
 150              return parent::toString();
 151          }
 152  
 153          $this->saveMessage();
 154  
 155          $this->doSign();
 156  
 157          $string = parent::toString();
 158  
 159          $this->restoreMessage();
 160  
 161          return $string;
 162      }
 163  
 164      /**
 165       * Write this message to a {@link Swift_InputByteStream}.
 166       *
 167       * @param Swift_InputByteStream $is
 168       */
 169      public function toByteStream(Swift_InputByteStream $is)
 170      {
 171          if (empty($this->headerSigners) && empty($this->bodySigners)) {
 172              parent::toByteStream($is);
 173  
 174              return;
 175          }
 176  
 177          $this->saveMessage();
 178  
 179          $this->doSign();
 180  
 181          parent::toByteStream($is);
 182  
 183          $this->restoreMessage();
 184      }
 185  
 186      public function __wakeup()
 187      {
 188          Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message');
 189      }
 190  
 191      /**
 192       * loops through signers and apply the signatures.
 193       */
 194      protected function doSign()
 195      {
 196          foreach ($this->bodySigners as $signer) {
 197              $altered = $signer->getAlteredHeaders();
 198              $this->saveHeaders($altered);
 199              $signer->signMessage($this);
 200          }
 201  
 202          foreach ($this->headerSigners as $signer) {
 203              $altered = $signer->getAlteredHeaders();
 204              $this->saveHeaders($altered);
 205              $signer->reset();
 206  
 207              $signer->setHeaders($this->getHeaders());
 208  
 209              $signer->startBody();
 210              $this->_bodyToByteStream($signer);
 211              $signer->endBody();
 212  
 213              $signer->addSignature($this->getHeaders());
 214          }
 215      }
 216  
 217      /**
 218       * save the message before any signature is applied.
 219       */
 220      protected function saveMessage()
 221      {
 222          $this->savedMessage = array('headers' => array());
 223          $this->savedMessage['body'] = $this->getBody();
 224          $this->savedMessage['children'] = $this->getChildren();
 225          if (count($this->savedMessage['children']) > 0 && $this->getBody() != '') {
 226              $this->setChildren(array_merge(array($this->_becomeMimePart()), $this->savedMessage['children']));
 227              $this->setBody('');
 228          }
 229      }
 230  
 231      /**
 232       * save the original headers.
 233       *
 234       * @param array $altered
 235       */
 236      protected function saveHeaders(array $altered)
 237      {
 238          foreach ($altered as $head) {
 239              $lc = strtolower($head);
 240  
 241              if (!isset($this->savedMessage['headers'][$lc])) {
 242                  $this->savedMessage['headers'][$lc] = $this->getHeaders()->getAll($head);
 243              }
 244          }
 245      }
 246  
 247      /**
 248       * Remove or restore altered headers.
 249       */
 250      protected function restoreHeaders()
 251      {
 252          foreach ($this->savedMessage['headers'] as $name => $savedValue) {
 253              $headers = $this->getHeaders()->getAll($name);
 254  
 255              foreach ($headers as $key => $value) {
 256                  if (!isset($savedValue[$key])) {
 257                      $this->getHeaders()->remove($name, $key);
 258                  }
 259              }
 260          }
 261      }
 262  
 263      /**
 264       * Restore message body.
 265       */
 266      protected function restoreMessage()
 267      {
 268          $this->setBody($this->savedMessage['body']);
 269          $this->setChildren($this->savedMessage['children']);
 270  
 271          $this->restoreHeaders();
 272          $this->savedMessage = array();
 273      }
 274  
 275      /**
 276       * Clone Message Signers.
 277       *
 278       * @see Swift_Mime_SimpleMimeEntity::__clone()
 279       */
 280      public function __clone()
 281      {
 282          parent::__clone();
 283          foreach ($this->bodySigners as $key => $bodySigner) {
 284              $this->bodySigners[$key] = clone($bodySigner);
 285          }
 286  
 287          foreach ($this->headerSigners as $key => $headerSigner) {
 288              $this->headerSigners[$key] = clone($headerSigner);
 289          }
 290      }
 291  }


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