[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Mime/ -> MimePart.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   * A MIME part, in a multipart message.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Mime_MimePart extends Swift_Mime_SimpleMimeEntity
  17  {
  18      /** The format parameter last specified by the user */
  19      protected $_userFormat;
  20  
  21      /** The charset last specified by the user */
  22      protected $_userCharset;
  23  
  24      /** The delsp parameter last specified by the user */
  25      protected $_userDelSp;
  26  
  27      /** The nesting level of this MimePart */
  28      private $_nestingLevel = self::LEVEL_ALTERNATIVE;
  29  
  30      /**
  31       * Create a new MimePart with $headers, $encoder and $cache.
  32       *
  33       * @param Swift_Mime_HeaderSet      $headers
  34       * @param Swift_Mime_ContentEncoder $encoder
  35       * @param Swift_KeyCache            $cache
  36       * @param Swift_Mime_Grammar        $grammar
  37       * @param string                    $charset
  38       */
  39      public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $charset = null)
  40      {
  41          parent::__construct($headers, $encoder, $cache, $grammar);
  42          $this->setContentType('text/plain');
  43          if (!is_null($charset)) {
  44              $this->setCharset($charset);
  45          }
  46      }
  47  
  48      /**
  49       * Set the body of this entity, either as a string, or as an instance of
  50       * {@link Swift_OutputByteStream}.
  51       *
  52       * @param mixed  $body
  53       * @param string $contentType optional
  54       * @param string $charset     optional
  55       *
  56       * @return Swift_Mime_MimePart
  57       */
  58      public function setBody($body, $contentType = null, $charset = null)
  59      {
  60          if (isset($charset)) {
  61              $this->setCharset($charset);
  62          }
  63          $body = $this->_convertString($body);
  64  
  65          parent::setBody($body, $contentType);
  66  
  67          return $this;
  68      }
  69  
  70      /**
  71       * Get the character set of this entity.
  72       *
  73       * @return string
  74       */
  75      public function getCharset()
  76      {
  77          return $this->_getHeaderParameter('Content-Type', 'charset');
  78      }
  79  
  80      /**
  81       * Set the character set of this entity.
  82       *
  83       * @param string $charset
  84       *
  85       * @return Swift_Mime_MimePart
  86       */
  87      public function setCharset($charset)
  88      {
  89          $this->_setHeaderParameter('Content-Type', 'charset', $charset);
  90          if ($charset !== $this->_userCharset) {
  91              $this->_clearCache();
  92          }
  93          $this->_userCharset = $charset;
  94          parent::charsetChanged($charset);
  95  
  96          return $this;
  97      }
  98  
  99      /**
 100       * Get the format of this entity (i.e. flowed or fixed).
 101       *
 102       * @return string
 103       */
 104      public function getFormat()
 105      {
 106          return $this->_getHeaderParameter('Content-Type', 'format');
 107      }
 108  
 109      /**
 110       * Set the format of this entity (flowed or fixed).
 111       *
 112       * @param string $format
 113       *
 114       * @return Swift_Mime_MimePart
 115       */
 116      public function setFormat($format)
 117      {
 118          $this->_setHeaderParameter('Content-Type', 'format', $format);
 119          $this->_userFormat = $format;
 120  
 121          return $this;
 122      }
 123  
 124      /**
 125       * Test if delsp is being used for this entity.
 126       *
 127       * @return bool
 128       */
 129      public function getDelSp()
 130      {
 131          return ($this->_getHeaderParameter('Content-Type', 'delsp') == 'yes')
 132              ? true
 133              : false;
 134      }
 135  
 136      /**
 137       * Turn delsp on or off for this entity.
 138       *
 139       * @param bool $delsp
 140       *
 141       * @return Swift_Mime_MimePart
 142       */
 143      public function setDelSp($delsp = true)
 144      {
 145          $this->_setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
 146          $this->_userDelSp = $delsp;
 147  
 148          return $this;
 149      }
 150  
 151      /**
 152       * Get the nesting level of this entity.
 153       *
 154       * @see LEVEL_TOP, LEVEL_ALTERNATIVE, LEVEL_MIXED, LEVEL_RELATED
 155       *
 156       * @return int
 157       */
 158      public function getNestingLevel()
 159      {
 160          return $this->_nestingLevel;
 161      }
 162  
 163      /**
 164       * Receive notification that the charset has changed on this document, or a
 165       * parent document.
 166       *
 167       * @param string $charset
 168       */
 169      public function charsetChanged($charset)
 170      {
 171          $this->setCharset($charset);
 172      }
 173  
 174      /** Fix the content-type and encoding of this entity */
 175      protected function _fixHeaders()
 176      {
 177          parent::_fixHeaders();
 178          if (count($this->getChildren())) {
 179              $this->_setHeaderParameter('Content-Type', 'charset', null);
 180              $this->_setHeaderParameter('Content-Type', 'format', null);
 181              $this->_setHeaderParameter('Content-Type', 'delsp', null);
 182          } else {
 183              $this->setCharset($this->_userCharset);
 184              $this->setFormat($this->_userFormat);
 185              $this->setDelSp($this->_userDelSp);
 186          }
 187      }
 188  
 189      /** Set the nesting level of this entity */
 190      protected function _setNestingLevel($level)
 191      {
 192          $this->_nestingLevel = $level;
 193      }
 194  
 195      /** Encode charset when charset is not utf-8 */
 196      protected function _convertString($string)
 197      {
 198          $charset = strtolower($this->getCharset());
 199          if (!in_array($charset, array('utf-8', 'iso-8859-1', ''))) {
 200              // mb_convert_encoding must be the first one to check, since iconv cannot convert some words.
 201              if (function_exists('mb_convert_encoding')) {
 202                  $string = mb_convert_encoding($string, $charset, 'utf-8');
 203              } elseif (function_exists('iconv')) {
 204                  $string = iconv('utf-8//TRANSLIT//IGNORE', $charset, $string);
 205              } else {
 206                  throw new Swift_SwiftException('No suitable convert encoding function (use UTF-8 as your charset or install the mbstring or iconv extension).');
 207              }
 208  
 209              return $string;
 210          }
 211  
 212          return $string;
 213      }
 214  }


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