TYPO3 API  SVNRelease
Rfc2231Encoder.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/Encoder.php';
00012 //@require 'Swift/CharacterStream.php';
00013 
00014 /**
00015  * Handles RFC 2231 specified Encoding in Swift Mailer.
00016  * @package Swift
00017  * @subpackage Encoder
00018  * @author Chris Corbyn
00019  */
00020 class Swift_Encoder_Rfc2231Encoder implements Swift_Encoder
00021 {
00022 
00023   /**
00024    * A character stream to use when reading a string as characters instead of bytes.
00025    * @var Swift_CharacterStream
00026    * @access private
00027    */
00028   private $_charStream;
00029 
00030   /**
00031    * Creates a new Rfc2231Encoder using the given character stream instance.
00032    * @param Swift_CharacterStream
00033    */
00034   public function __construct(Swift_CharacterStream $charStream)
00035   {
00036     $this->_charStream = $charStream;
00037   }
00038 
00039   /**
00040    * Takes an unencoded string and produces a string encoded according to
00041    * RFC 2231 from it.
00042    * @param string $string to encode
00043    * @param int $firstLineOffset
00044    * @param int $maxLineLength, optional, 0 indicates the default of 75 bytes
00045    * @return string
00046    */
00047   public function encodeString($string, $firstLineOffset = 0,
00048     $maxLineLength = 0)
00049   {
00050     $lines = array(); $lineCount = 0;
00051     $lines[] = '';
00052     $currentLine =& $lines[$lineCount++];
00053 
00054     if (0 >= $maxLineLength)
00055     {
00056       $maxLineLength = 75;
00057     }
00058 
00059     $this->_charStream->flushContents();
00060     $this->_charStream->importString($string);
00061 
00062     $thisLineLength = $maxLineLength - $firstLineOffset;
00063 
00064     while (false !== $char = $this->_charStream->read(4))
00065     {
00066       $encodedChar = rawurlencode($char);
00067       if (0 != strlen($currentLine)
00068         && strlen($currentLine . $encodedChar) > $thisLineLength)
00069       {
00070         $lines[] = '';
00071         $currentLine =& $lines[$lineCount++];
00072         $thisLineLength = $maxLineLength;
00073       }
00074       $currentLine .= $encodedChar;
00075     }
00076 
00077     return implode("\r\n", $lines);
00078   }
00079 
00080   /**
00081    * Updates the charset used.
00082    * @param string $charset
00083    */
00084   public function charsetChanged($charset)
00085   {
00086     $this->_charStream->setCharacterSet($charset);
00087   }
00088 
00089 }