vendor/swiftmailer/lib/classes/Swift/Encoder/Rfc2231Encoder.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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 /**
       
    13  * Handles RFC 2231 specified Encoding in Swift Mailer.
       
    14  * @package Swift
       
    15  * @subpackage Encoder
       
    16  * @author Chris Corbyn
       
    17  */
       
    18 class Swift_Encoder_Rfc2231Encoder implements Swift_Encoder
       
    19 {
       
    20   
       
    21   /**
       
    22    * A character stream to use when reading a string as characters instead of bytes.
       
    23    * @var Swift_CharacterStream
       
    24    * @access private
       
    25    */
       
    26   private $_charStream;
       
    27   
       
    28   /**
       
    29    * Creates a new Rfc2231Encoder using the given character stream instance.
       
    30    * @param Swift_CharacterStream
       
    31    */
       
    32   public function __construct(Swift_CharacterStream $charStream)
       
    33   {
       
    34     $this->_charStream = $charStream;
       
    35   }
       
    36   
       
    37   /**
       
    38    * Takes an unencoded string and produces a string encoded according to
       
    39    * RFC 2231 from it.
       
    40    * @param string $string to encode
       
    41    * @param int $firstLineOffset
       
    42    * @param int $maxLineLength, optional, 0 indicates the default of 75 bytes
       
    43    * @return string
       
    44    */
       
    45   public function encodeString($string, $firstLineOffset = 0,
       
    46     $maxLineLength = 0)
       
    47   {
       
    48     $lines = array(); $lineCount = 0;
       
    49     $lines[] = '';
       
    50     $currentLine =& $lines[$lineCount++];
       
    51     
       
    52     if (0 >= $maxLineLength)
       
    53     {
       
    54       $maxLineLength = 75;
       
    55     }
       
    56     
       
    57     $this->_charStream->flushContents();
       
    58     $this->_charStream->importString($string);
       
    59     
       
    60     $thisLineLength = $maxLineLength - $firstLineOffset;
       
    61     
       
    62     while (false !== $char = $this->_charStream->read(4))
       
    63     {
       
    64       $encodedChar = rawurlencode($char);
       
    65       if (0 != strlen($currentLine)
       
    66         && strlen($currentLine . $encodedChar) > $thisLineLength)
       
    67       {
       
    68         $lines[] = '';
       
    69         $currentLine =& $lines[$lineCount++];
       
    70         $thisLineLength = $maxLineLength;
       
    71       }
       
    72       $currentLine .= $encodedChar;
       
    73     }
       
    74     
       
    75     return implode("\r\n", $lines);
       
    76   }
       
    77   
       
    78   /**
       
    79    * Updates the charset used.
       
    80    * @param string $charset
       
    81    */
       
    82   public function charsetChanged($charset)
       
    83   {
       
    84     $this->_charStream->setCharacterSet($charset);
       
    85   }
       
    86   
       
    87 }