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 Quoted Printable (QP) Transfer Encoding in Swift Mailer.
13: * @package Swift
14: * @subpackage Mime
15: * @author Chris Corbyn
16: */
17: class Swift_Mime_ContentEncoder_QpContentEncoder extends Swift_Encoder_QpEncoder implements Swift_Mime_ContentEncoder
18: {
19: protected $_dotEscape;
20:
21: /**
22: * Creates a new QpContentEncoder for the given CharacterStream.
23: * @param Swift_CharacterStream $charStream to use for reading characters
24: * @param Swift_StreamFilter $filter if canonicalization should occur
25: * @param boolean $dotEscape if dot stuffing workaround must be enabled
26: */
27: public function __construct(Swift_CharacterStream $charStream, Swift_StreamFilter $filter = null, $dotEscape=false)
28: {
29: $this->_dotEscape = $dotEscape;
30: parent::__construct($charStream, $filter);
31: }
32:
33: public function __sleep()
34: {
35: return array('_charStream', '_filter', '_dotEscape');
36: }
37:
38: protected function getSafeMapShareId()
39: {
40: return get_class($this).($this->_dotEscape ? '.dotEscape' : '');
41: }
42:
43: protected function initSafeMap()
44: {
45: parent::initSafeMap();
46: if ($this->_dotEscape) {
47: /* Encode . as =2e for buggy remote servers */
48: unset($this->_safeMap[0x2e]);
49: }
50: }
51:
52: /**
53: * Encode stream $in to stream $out.
54: * QP encoded strings have a maximum line length of 76 characters.
55: * If the first line needs to be shorter, indicate the difference with
56: * $firstLineOffset.
57: * @param Swift_OutputByteStream $os output stream
58: * @param Swift_InputByteStream $is input stream
59: * @param int $firstLineOffset
60: * @param int $maxLineLength
61: */
62: public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
63: {
64: if ($maxLineLength > 76 || $maxLineLength <= 0) {
65: $maxLineLength = 76;
66: }
67:
68: $thisLineLength = $maxLineLength - $firstLineOffset;
69:
70: $this->_charStream->flushContents();
71: $this->_charStream->importByteStream($os);
72:
73: $currentLine = '';
74: $prepend = '';
75: $size=$lineLen=0;
76:
77: while (false !== $bytes = $this->_nextSequence()) {
78: //If we're filtering the input
79: if (isset($this->_filter)) {
80: //If we can't filter because we need more bytes
81: while ($this->_filter->shouldBuffer($bytes)) {
82: //Then collect bytes into the buffer
83: if (false === $moreBytes = $this->_nextSequence(1)) {
84: break;
85: }
86:
87: foreach ($moreBytes as $b) {
88: $bytes[] = $b;
89: }
90: }
91: //And filter them
92: $bytes = $this->_filter->filter($bytes);
93: }
94:
95: $enc = $this->_encodeByteSequence($bytes, $size);
96: if ($currentLine && $lineLen+$size >= $thisLineLength) {
97: $is->write($prepend . $this->_standardize($currentLine));
98: $currentLine = '';
99: $prepend = "=\r\n";
100: $thisLineLength = $maxLineLength;
101: $lineLen=0;
102: }
103: $lineLen+=$size;
104: $currentLine .= $enc;
105: }
106: if (strlen($currentLine)) {
107: $is->write($prepend . $this->_standardize($currentLine));
108: }
109: }
110:
111: /**
112: * Get the name of this encoding scheme.
113: * Returns the string 'quoted-printable'.
114: * @return string
115: */
116: public function getName()
117: {
118: return 'quoted-printable';
119: }
120: }
121: