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