[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Mime/ContentEncoder/ -> NativeQpContentEncoder.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   * Handles Quoted Printable (QP) Transfer Encoding in Swift Mailer using the PHP core function.
  13   *
  14   * @author Lars Strojny
  15   */
  16  class Swift_Mime_ContentEncoder_NativeQpContentEncoder implements Swift_Mime_ContentEncoder
  17  {
  18      /**
  19       * @var null|string
  20       */
  21      private $charset;
  22  
  23      /**
  24       * @param null|string $charset
  25       */
  26      public function __construct($charset = null)
  27      {
  28          $this->charset = $charset ? $charset : 'utf-8';
  29      }
  30  
  31      /**
  32       * Notify this observer that the entity's charset has changed.
  33       *
  34       * @param string $charset
  35       */
  36      public function charsetChanged($charset)
  37      {
  38          $this->charset = $charset;
  39      }
  40  
  41      /**
  42       * Encode $in to $out.
  43       *
  44       * @param Swift_OutputByteStream $os              to read from
  45       * @param Swift_InputByteStream  $is              to write to
  46       * @param int                    $firstLineOffset
  47       * @param int                    $maxLineLength   0 indicates the default length for this encoding
  48       *
  49       * @throws RuntimeException
  50       */
  51      public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
  52      {
  53          if ($this->charset !== 'utf-8') {
  54              throw new RuntimeException(
  55                  sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
  56          }
  57  
  58          $string = '';
  59  
  60          while (false !== $bytes = $os->read(8192)) {
  61              $string .= $bytes;
  62          }
  63  
  64          $is->write($this->encodeString($string));
  65      }
  66  
  67      /**
  68       * Get the MIME name of this content encoding scheme.
  69       *
  70       * @return string
  71       */
  72      public function getName()
  73      {
  74          return 'quoted-printable';
  75      }
  76  
  77      /**
  78       * Encode a given string to produce an encoded string.
  79       *
  80       * @param string $string
  81       * @param int    $firstLineOffset if first line needs to be shorter
  82       * @param int    $maxLineLength   0 indicates the default length for this encoding
  83       *
  84       * @throws RuntimeException
  85       *
  86       * @return string
  87       */
  88      public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
  89      {
  90          if ($this->charset !== 'utf-8') {
  91              throw new RuntimeException(
  92                  sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
  93          }
  94  
  95          return $this->_standardize(quoted_printable_encode($string));
  96      }
  97  
  98      /**
  99       * Make sure CRLF is correct and HT/SPACE are in valid places.
 100       *
 101       * @param string $string
 102       *
 103       * @return string
 104       */
 105      protected function _standardize($string)
 106      {
 107          // transform CR or LF to CRLF
 108          $string = preg_replace('~=0D(?!=0A)|(?<!=0D)=0A~', '=0D=0A', $string);
 109          // transform =0D=0A to CRLF
 110          $string = str_replace(array("\t=0D=0A", ' =0D=0A', '=0D=0A'), array("=09\r\n", "=20\r\n", "\r\n"), $string);
 111  
 112          switch ($end = ord(substr($string, -1))) {
 113              case 0x09:
 114                  $string = substr_replace($string, '=09', -1);
 115                  break;
 116              case 0x20:
 117                  $string = substr_replace($string, '=20', -1);
 118                  break;
 119          }
 120  
 121          return $string;
 122      }
 123  }


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