[ Index ]

MailPress 7.2

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/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 string|null
  20       */
  21      private $charset;
  22  
  23      /**
  24       * @param string|null $charset
  25       */
  26      public function __construct($charset = null)
  27      {
  28          $this->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 ('utf-8' !== $this->charset) {
  54              throw new RuntimeException(sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
  55          }
  56  
  57          $string = '';
  58  
  59          while (false !== $bytes = $os->read(8192)) {
  60              $string .= $bytes;
  61          }
  62  
  63          $is->write($this->encodeString($string));
  64      }
  65  
  66      /**
  67       * Get the MIME name of this content encoding scheme.
  68       *
  69       * @return string
  70       */
  71      public function getName()
  72      {
  73          return 'quoted-printable';
  74      }
  75  
  76      /**
  77       * Encode a given string to produce an encoded string.
  78       *
  79       * @param string $string
  80       * @param int    $firstLineOffset if first line needs to be shorter
  81       * @param int    $maxLineLength   0 indicates the default length for this encoding
  82       *
  83       * @throws RuntimeException
  84       *
  85       * @return string
  86       */
  87      public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
  88      {
  89          if ('utf-8' !== $this->charset) {
  90              throw new RuntimeException(sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
  91          }
  92  
  93          return $this->standardize(quoted_printable_encode($string));
  94      }
  95  
  96      /**
  97       * Make sure CRLF is correct and HT/SPACE are in valid places.
  98       *
  99       * @param string $string
 100       *
 101       * @return string
 102       */
 103      protected function standardize($string)
 104      {
 105          // transform CR or LF to CRLF
 106          $string = preg_replace('~=0D(?!=0A)|(?<!=0D)=0A~', '=0D=0A', $string);
 107          // transform =0D=0A to CRLF
 108          $string = str_replace(["\t=0D=0A", ' =0D=0A', '=0D=0A'], ["=09\r\n", "=20\r\n", "\r\n"], $string);
 109  
 110          switch (ord(substr($string, -1))) {
 111              case 0x09:
 112                  $string = substr_replace($string, '=09', -1);
 113                  break;
 114              case 0x20:
 115                  $string = substr_replace($string, '=20', -1);
 116                  break;
 117          }
 118  
 119          return $string;
 120      }
 121  }


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