TYPO3 API  SVNRelease
QpHeaderEncoder.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_once dirname(__FILE__) . '/../HeaderEncoder.php';
00012 require_once dirname(__FILE__) . '/../../Encoder/QpEncoder.php';
00013 require_once dirname(__FILE__) . '/../../CharacterStream.php';
00014 
00015 /**
00016  * Handles Quoted Printable (Q) Header Encoding in Swift Mailer.
00017  * @package Swift
00018  * @subpackage Mime
00019  * @author Chris Corbyn
00020  */
00021 class Swift_Mime_HeaderEncoder_QpHeaderEncoder extends Swift_Encoder_QpEncoder
00022   implements Swift_Mime_HeaderEncoder
00023 {
00024 
00025   private static $_headerSafeMap = array();
00026 
00027   /**
00028    * Creates a new QpHeaderEncoder for the given CharacterStream.
00029    * @param Swift_CharacterStream $charStream to use for reading characters
00030    */
00031   public function __construct(Swift_CharacterStream $charStream)
00032   {
00033     parent::__construct($charStream);
00034     if (empty(self::$_headerSafeMap))
00035     {
00036       foreach (array_merge(
00037         range(0x61, 0x7A), range(0x41, 0x5A),
00038         range(0x30, 0x39), array(0x20, 0x21, 0x2A, 0x2B, 0x2D, 0x2F)
00039         ) as $byte)
00040       {
00041         self::$_headerSafeMap[$byte] = chr($byte);
00042       }
00043     }
00044   }
00045 
00046   /**
00047    * Get the name of this encoding scheme.
00048    * Returns the string 'Q'.
00049    * @return string
00050    */
00051   public function getName()
00052   {
00053     return 'Q';
00054   }
00055 
00056   /**
00057    * Takes an unencoded string and produces a Q encoded string from it.
00058    * @param string $string to encode
00059    * @param int $firstLineOffset, optional
00060    * @param int $maxLineLength, optional, 0 indicates the default of 76 chars
00061    * @return string
00062    */
00063   public function encodeString($string, $firstLineOffset = 0,
00064     $maxLineLength = 0)
00065   {
00066     return str_replace(array(' ', '=20', "=\r\n"), array('_', '_', "\r\n"),
00067       parent::encodeString($string, $firstLineOffset, $maxLineLength)
00068       );
00069   }
00070 
00071   // -- Overridden points of extension
00072 
00073   /**
00074    * Encode the given byte array into a verbatim QP form.
00075    * @param int[] $bytes
00076    * @return string
00077    * @access protected
00078    */
00079   protected function _encodeByteSequence(array $bytes, &$size)
00080   {
00081     $ret = '';
00082     $size=0;
00083     foreach ($bytes as $b)
00084     {
00085       if (isset(self::$_headerSafeMap[$b]))
00086       {
00087         $ret .= self::$_headerSafeMap[$b];
00088         ++$size;
00089       }
00090       else
00091       {
00092         $ret .= self::$_qpMap[$b];
00093         $size+=3;
00094       }
00095     }
00096     return $ret;
00097   }
00098 
00099 }