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