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: * @package Swift
14: * @subpackage Encoder
15: * @author Chris Corbyn
16: */
17: class Swift_Encoder_Rfc2231Encoder implements Swift_Encoder
18: {
19: /**
20: * A character stream to use when reading a string as characters instead of bytes.
21: * @var Swift_CharacterStream
22: * @access private
23: */
24: private $_charStream;
25:
26: /**
27: * Creates a new Rfc2231Encoder using the given character stream instance.
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: * @param string $string to encode
39: * @param int $firstLineOffset
40: * @param int $maxLineLength, optional, 0 indicates the default of 75 bytes
41: * @return string
42: */
43: public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
44: {
45: $lines = array(); $lineCount = 0;
46: $lines[] = '';
47: $currentLine =& $lines[$lineCount++];
48:
49: if (0 >= $maxLineLength) {
50: $maxLineLength = 75;
51: }
52:
53: $this->_charStream->flushContents();
54: $this->_charStream->importString($string);
55:
56: $thisLineLength = $maxLineLength - $firstLineOffset;
57:
58: while (false !== $char = $this->_charStream->read(4)) {
59: $encodedChar = rawurlencode($char);
60: if (0 != strlen($currentLine)
61: && strlen($currentLine . $encodedChar) > $thisLineLength)
62: {
63: $lines[] = '';
64: $currentLine =& $lines[$lineCount++];
65: $thisLineLength = $maxLineLength;
66: }
67: $currentLine .= $encodedChar;
68: }
69:
70: return implode("\r\n", $lines);
71: }
72:
73: /**
74: * Updates the charset used.
75: * @param string $charset
76: */
77: public function charsetChanged($charset)
78: {
79: $this->_charStream->setCharacterSet($charset);
80: }
81: }
82: