[ Index ]

MailPress 544

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

title

Body

[close]

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


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