TYPO3 API  SVNRelease
PlainContentEncoder.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of SwiftMailer.
00005  * (c) 2004-2009 Chris Corbyn
00006  *
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00011 //@require 'Swift/Mime/ContentEncoder.php';
00012 //@require 'Swift/InputByteStream.php';
00013 //@require 'Swift/OutputByteStream.php';
00014 
00015 /**
00016  * Handles binary/7/8-bit Transfer Encoding in Swift Mailer.
00017  * @package Swift
00018  * @subpackage Mime
00019  * @author Chris Corbyn
00020  */
00021 class Swift_Mime_ContentEncoder_PlainContentEncoder
00022   implements Swift_Mime_ContentEncoder
00023 {
00024 
00025   /**
00026    * The name of this encoding scheme (probably 7bit or 8bit).
00027    * @var string
00028    * @access private
00029    */
00030   private $_name;
00031 
00032   /**
00033    * True if canonical transformations should be done.
00034    * @var boolean
00035    * @access private
00036    */
00037   private $_canonical;
00038 
00039   /**
00040    * Creates a new PlainContentEncoder with $name (probably 7bit or 8bit).
00041    * @param string $name
00042    * @param boolean $canonical If canonicalization transformation should be done.
00043    */
00044   public function __construct($name, $canonical = false)
00045   {
00046     $this->_name = $name;
00047     $this->_canonical = $canonical;
00048   }
00049 
00050   /**
00051    * Encode a given string to produce an encoded string.
00052    * @param string $string
00053    * @param int $firstLineOffset, ignored
00054    * @param int $maxLineLength - 0 means no wrapping will occur
00055    * @return string
00056    */
00057   public function encodeString($string, $firstLineOffset = 0,
00058     $maxLineLength = 0)
00059   {
00060     if ($this->_canonical)
00061     {
00062       $string = $this->_canonicalize($string);
00063     }
00064     return $this->_safeWordWrap($string, $maxLineLength, "\r\n");
00065   }
00066 
00067   /**
00068    * Encode stream $in to stream $out.
00069    * @param Swift_OutputByteStream $in
00070    * @param Swift_InputByteStream $out
00071    * @param int $firstLineOffset, ignored
00072    * @param int $maxLineLength, optional, 0 means no wrapping will occur
00073    */
00074   public function encodeByteStream(
00075     Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0,
00076     $maxLineLength = 0)
00077   {
00078     $leftOver = '';
00079     while (false !== $bytes = $os->read(8192))
00080     {
00081       $toencode = $leftOver . $bytes;
00082       if ($this->_canonical)
00083       {
00084         $toencode = $this->_canonicalize($toencode);
00085       }
00086       $wrapped = $this->_safeWordWrap($toencode, $maxLineLength, "\r\n");
00087       $lastLinePos = strrpos($wrapped, "\r\n");
00088       $leftOver = substr($wrapped, $lastLinePos);
00089       $wrapped = substr($wrapped, 0, $lastLinePos);
00090 
00091       $is->write($wrapped);
00092     }
00093     if (strlen($leftOver))
00094     {
00095       $is->write($leftOver);
00096     }
00097   }
00098 
00099   /**
00100    * Get the name of this encoding scheme.
00101    * @return string
00102    */
00103   public function getName()
00104   {
00105     return $this->_name;
00106   }
00107 
00108   /**
00109    * Not used.
00110    */
00111   public function charsetChanged($charset)
00112   {
00113   }
00114 
00115   // -- Private methods
00116 
00117   /**
00118    * A safer (but weaker) wordwrap for unicode.
00119    * @param string $string
00120    * @param int $length
00121    * @param string $le
00122    * @return string
00123    * @access private
00124    */
00125   private function _safeWordwrap($string, $length = 75, $le = "\r\n")
00126   {
00127     if (0 >= $length)
00128     {
00129       return $string;
00130     }
00131 
00132     $originalLines = explode($le, $string);
00133 
00134     $lines = array();
00135     $lineCount = 0;
00136 
00137     foreach ($originalLines as $originalLine)
00138     {
00139       $lines[] = '';
00140       $currentLine =& $lines[$lineCount++];
00141 
00142       //$chunks = preg_split('/(?<=[\ \t,\.!\?\-&\+\/])/', $originalLine);
00143       $chunks = preg_split('/(?<=\s)/', $originalLine);
00144 
00145       foreach ($chunks as $chunk)
00146       {
00147         if (0 != strlen($currentLine)
00148           && strlen($currentLine . $chunk) > $length)
00149         {
00150           $lines[] = '';
00151           $currentLine =& $lines[$lineCount++];
00152         }
00153         $currentLine .= $chunk;
00154       }
00155     }
00156 
00157     return implode("\r\n", $lines);
00158   }
00159 
00160   /**
00161    * Canonicalize string input (fix CRLF).
00162    * @param string $string
00163    * @return string
00164    * @access private
00165    */
00166   private function _canonicalize($string)
00167   {
00168     return str_replace(
00169       array("\r\n", "\r", "\n"),
00170       array("\n", "\n", "\r\n"),
00171       $string
00172       );
00173   }
00174 
00175 }