[ Index ]

MailPress 7.2

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

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/ContentEncoder/ -> PlainContentEncoder.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 binary/7/8-bit Transfer Encoding in Swift Mailer.
  13   *
  14   * When sending 8-bit content over SMTP, you should use
  15   * Swift_Transport_Esmtp_EightBitMimeHandler to enable the 8BITMIME SMTP
  16   * extension.
  17   *
  18   * @author Chris Corbyn
  19   */
  20  class Swift_Mime_ContentEncoder_PlainContentEncoder implements Swift_Mime_ContentEncoder
  21  {
  22      /**
  23       * The name of this encoding scheme (probably 7bit or 8bit).
  24       *
  25       * @var string
  26       */
  27      private $name;
  28  
  29      /**
  30       * True if canonical transformations should be done.
  31       *
  32       * @var bool
  33       */
  34      private $canonical;
  35  
  36      /**
  37       * Creates a new PlainContentEncoder with $name (probably 7bit or 8bit).
  38       *
  39       * @param string $name
  40       * @param bool   $canonical If canonicalization transformation should be done.
  41       */
  42      public function __construct($name, $canonical = false)
  43      {
  44          $this->name = $name;
  45          $this->canonical = $canonical;
  46      }
  47  
  48      /**
  49       * Encode a given string to produce an encoded string.
  50       *
  51       * @param string $string
  52       * @param int    $firstLineOffset ignored
  53       * @param int    $maxLineLength   - 0 means no wrapping will occur
  54       *
  55       * @return string
  56       */
  57      public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
  58      {
  59          if ($this->canonical) {
  60              $string = $this->canonicalize($string);
  61          }
  62  
  63          return $this->safeWordwrap($string, $maxLineLength, "\r\n");
  64      }
  65  
  66      /**
  67       * Encode stream $in to stream $out.
  68       *
  69       * @param int $firstLineOffset ignored
  70       * @param int $maxLineLength   optional, 0 means no wrapping will occur
  71       */
  72      public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
  73      {
  74          $leftOver = '';
  75          while (false !== $bytes = $os->read(8192)) {
  76              $toencode = $leftOver.$bytes;
  77              if ($this->canonical) {
  78                  $toencode = $this->canonicalize($toencode);
  79              }
  80              $wrapped = $this->safeWordwrap($toencode, $maxLineLength, "\r\n");
  81              $lastLinePos = strrpos($wrapped, "\r\n");
  82              $leftOver = substr($wrapped, $lastLinePos);
  83              $wrapped = substr($wrapped, 0, $lastLinePos);
  84  
  85              $is->write($wrapped);
  86          }
  87          if (strlen($leftOver)) {
  88              $is->write($leftOver);
  89          }
  90      }
  91  
  92      /**
  93       * Get the name of this encoding scheme.
  94       *
  95       * @return string
  96       */
  97      public function getName()
  98      {
  99          return $this->name;
 100      }
 101  
 102      /**
 103       * Not used.
 104       */
 105      public function charsetChanged($charset)
 106      {
 107      }
 108  
 109      /**
 110       * A safer (but weaker) wordwrap for unicode.
 111       *
 112       * @param string $string
 113       * @param int    $length
 114       * @param string $le
 115       *
 116       * @return string
 117       */
 118      private function safeWordwrap($string, $length = 75, $le = "\r\n")
 119      {
 120          if (0 >= $length) {
 121              return $string;
 122          }
 123  
 124          $originalLines = explode($le, $string);
 125  
 126          $lines = [];
 127          $lineCount = 0;
 128  
 129          foreach ($originalLines as $originalLine) {
 130              $lines[] = '';
 131              $currentLine = &$lines[$lineCount++];
 132  
 133              //$chunks = preg_split('/(?<=[\ \t,\.!\?\-&\+\/])/', $originalLine);
 134              $chunks = preg_split('/(?<=\s)/', $originalLine);
 135  
 136              foreach ($chunks as $chunk) {
 137                  if (0 != strlen($currentLine)
 138                      && strlen($currentLine.$chunk) > $length) {
 139                      $lines[] = '';
 140                      $currentLine = &$lines[$lineCount++];
 141                  }
 142                  $currentLine .= $chunk;
 143              }
 144          }
 145  
 146          return implode("\r\n", $lines);
 147      }
 148  
 149      /**
 150       * Canonicalize string input (fix CRLF).
 151       *
 152       * @param string $string
 153       *
 154       * @return string
 155       */
 156      private function canonicalize($string)
 157      {
 158          return str_replace(
 159              ["\r\n", "\r", "\n"],
 160              ["\n", "\n", "\r\n"],
 161              $string
 162              );
 163      }
 164  }


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