TYPO3 API  SVNRelease
QpContentEncoder.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/Encoder/QpEncoder.php';
00013 //@require 'Swift/InputByteStrean.php';
00014 //@require 'Swift/OutputByteStream.php';
00015 //@require 'Swift/CharacterStream.php';
00016 
00017 /**
00018  * Handles Quoted Printable (QP) Transfer Encoding in Swift Mailer.
00019  * @package Swift
00020  * @subpackage Mime
00021  * @author Chris Corbyn
00022  */
00023 class Swift_Mime_ContentEncoder_QpContentEncoder extends Swift_Encoder_QpEncoder
00024   implements Swift_Mime_ContentEncoder
00025 {
00026 
00027   /**
00028    * Creates a new QpContentEncoder for the given CharacterStream.
00029    * @param Swift_CharacterStream $charStream to use for reading characters
00030    * @param Swift_StreamFilter $filter if canonicalization should occur
00031    */
00032   public function __construct(Swift_CharacterStream $charStream,
00033     Swift_StreamFilter $filter = null)
00034   {
00035     parent::__construct($charStream, $filter);
00036   }
00037 
00038   /**
00039    * Encode stream $in to stream $out.
00040    * QP encoded strings have a maximum line length of 76 characters.
00041    * If the first line needs to be shorter, indicate the difference with
00042    * $firstLineOffset.
00043    * @param Swift_OutputByteStream $os output stream
00044    * @param Swift_InputByteStream $is input stream
00045    * @param int $firstLineOffset
00046    * @param int $maxLineLength
00047    */
00048   public function encodeByteStream(
00049     Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0,
00050     $maxLineLength = 0)
00051   {
00052     if ($maxLineLength > 76 || $maxLineLength <= 0)
00053     {
00054       $maxLineLength = 76;
00055     }
00056 
00057     $thisLineLength = $maxLineLength - $firstLineOffset;
00058 
00059     $this->_charStream->flushContents();
00060     $this->_charStream->importByteStream($os);
00061 
00062     $currentLine = '';
00063     $prepend = '';
00064     $size=$lineLen=0;
00065 
00066     while (false !== $bytes = $this->_nextSequence())
00067     {
00068       //If we're filtering the input
00069       if (isset($this->_filter))
00070       {
00071         //If we can't filter because we need more bytes
00072         while ($this->_filter->shouldBuffer($bytes))
00073         {
00074           //Then collect bytes into the buffer
00075           if (false === $moreBytes = $this->_nextSequence(1))
00076           {
00077             break;
00078           }
00079 
00080           foreach ($moreBytes as $b)
00081           {
00082             $bytes[] = $b;
00083           }
00084         }
00085         //And filter them
00086         $bytes = $this->_filter->filter($bytes);
00087       }
00088 
00089       $enc = $this->_encodeByteSequence($bytes, $size);
00090       if ($currentLine && $lineLen+$size >= $thisLineLength)
00091       {
00092         $is->write($prepend . $this->_standardize($currentLine));
00093         $currentLine = '';
00094         $prepend = "=\r\n";
00095         $thisLineLength = $maxLineLength;
00096         $lineLen=0;
00097       }
00098       $lineLen+=$size;
00099       $currentLine .= $enc;
00100     }
00101     if (strlen($currentLine))
00102     {
00103       $is->write($prepend . $this->_standardize($currentLine));
00104     }
00105   }
00106 
00107   /**
00108    * Get the name of this encoding scheme.
00109    * Returns the string 'quoted-printable'.
00110    * @return string
00111    */
00112   public function getName()
00113   {
00114     return 'quoted-printable';
00115   }
00116 
00117 }