|
0
|
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 binary/7/8-bit Transfer Encoding in Swift Mailer. |
|
|
14 |
* @package Swift |
|
|
15 |
* @subpackage Mime |
|
|
16 |
* @author Chris Corbyn |
|
|
17 |
*/ |
|
|
18 |
class Swift_Mime_ContentEncoder_PlainContentEncoder |
|
|
19 |
implements Swift_Mime_ContentEncoder |
|
|
20 |
{ |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* The name of this encoding scheme (probably 7bit or 8bit). |
|
|
24 |
* @var string |
|
|
25 |
* @access private |
|
|
26 |
*/ |
|
|
27 |
private $_name; |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* True if canonical transformations should be done. |
|
|
31 |
* @var boolean |
|
|
32 |
* @access private |
|
|
33 |
*/ |
|
|
34 |
private $_canonical; |
|
|
35 |
|
|
|
36 |
/** |
|
|
37 |
* Creates a new PlainContentEncoder with $name (probably 7bit or 8bit). |
|
|
38 |
* @param string $name |
|
|
39 |
* @param boolean $canonical If canonicalization transformation should be done. |
|
|
40 |
*/ |
|
|
41 |
public function __construct($name, $canonical = false) |
|
|
42 |
{ |
|
|
43 |
$this->_name = $name; |
|
|
44 |
$this->_canonical = $canonical; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
/** |
|
|
48 |
* Encode a given string to produce an encoded string. |
|
|
49 |
* @param string $string |
|
|
50 |
* @param int $firstLineOffset, ignored |
|
|
51 |
* @param int $maxLineLength - 0 means no wrapping will occur |
|
|
52 |
* @return string |
|
|
53 |
*/ |
|
|
54 |
public function encodeString($string, $firstLineOffset = 0, |
|
|
55 |
$maxLineLength = 0) |
|
|
56 |
{ |
|
|
57 |
if ($this->_canonical) |
|
|
58 |
{ |
|
|
59 |
$string = $this->_canonicalize($string); |
|
|
60 |
} |
|
|
61 |
return $this->_safeWordWrap($string, $maxLineLength, "\r\n"); |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
/** |
|
|
65 |
* Encode stream $in to stream $out. |
|
|
66 |
* @param Swift_OutputByteStream $in |
|
|
67 |
* @param Swift_InputByteStream $out |
|
|
68 |
* @param int $firstLineOffset, ignored |
|
|
69 |
* @param int $maxLineLength, optional, 0 means no wrapping will occur |
|
|
70 |
*/ |
|
|
71 |
public function encodeByteStream( |
|
|
72 |
Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, |
|
|
73 |
$maxLineLength = 0) |
|
|
74 |
{ |
|
|
75 |
$leftOver = ''; |
|
|
76 |
while (false !== $bytes = $os->read(8192)) |
|
|
77 |
{ |
|
|
78 |
$toencode = $leftOver . $bytes; |
|
|
79 |
if ($this->_canonical) |
|
|
80 |
{ |
|
|
81 |
$toencode = $this->_canonicalize($toencode); |
|
|
82 |
} |
|
|
83 |
$wrapped = $this->_safeWordWrap($toencode, $maxLineLength, "\r\n"); |
|
|
84 |
$lastLinePos = strrpos($wrapped, "\r\n"); |
|
|
85 |
$leftOver = substr($wrapped, $lastLinePos); |
|
|
86 |
$wrapped = substr($wrapped, 0, $lastLinePos); |
|
|
87 |
|
|
|
88 |
$is->write($wrapped); |
|
|
89 |
} |
|
|
90 |
if (strlen($leftOver)) |
|
|
91 |
{ |
|
|
92 |
$is->write($leftOver); |
|
|
93 |
} |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
/** |
|
|
97 |
* Get the name of this encoding scheme. |
|
|
98 |
* @return string |
|
|
99 |
*/ |
|
|
100 |
public function getName() |
|
|
101 |
{ |
|
|
102 |
return $this->_name; |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
/** |
|
|
106 |
* Not used. |
|
|
107 |
*/ |
|
|
108 |
public function charsetChanged($charset) |
|
|
109 |
{ |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
// -- Private methods |
|
|
113 |
|
|
|
114 |
/** |
|
|
115 |
* A safer (but weaker) wordwrap for unicode. |
|
|
116 |
* @param string $string |
|
|
117 |
* @param int $length |
|
|
118 |
* @param string $le |
|
|
119 |
* @return string |
|
|
120 |
* @access private |
|
|
121 |
*/ |
|
|
122 |
private function _safeWordwrap($string, $length = 75, $le = "\r\n") |
|
|
123 |
{ |
|
|
124 |
if (0 >= $length) |
|
|
125 |
{ |
|
|
126 |
return $string; |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
$originalLines = explode($le, $string); |
|
|
130 |
|
|
|
131 |
$lines = array(); |
|
|
132 |
$lineCount = 0; |
|
|
133 |
|
|
|
134 |
foreach ($originalLines as $originalLine) |
|
|
135 |
{ |
|
|
136 |
$lines[] = ''; |
|
|
137 |
$currentLine =& $lines[$lineCount++]; |
|
|
138 |
|
|
|
139 |
//$chunks = preg_split('/(?<=[\ \t,\.!\?\-&\+\/])/', $originalLine); |
|
|
140 |
$chunks = preg_split('/(?<=\s)/', $originalLine); |
|
|
141 |
|
|
|
142 |
foreach ($chunks as $chunk) |
|
|
143 |
{ |
|
|
144 |
if (0 != strlen($currentLine) |
|
|
145 |
&& strlen($currentLine . $chunk) > $length) |
|
|
146 |
{ |
|
|
147 |
$lines[] = ''; |
|
|
148 |
$currentLine =& $lines[$lineCount++]; |
|
|
149 |
} |
|
|
150 |
$currentLine .= $chunk; |
|
|
151 |
} |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
return implode("\r\n", $lines); |
|
|
155 |
} |
|
|
156 |
|
|
|
157 |
/** |
|
|
158 |
* Canonicalize string input (fix CRLF). |
|
|
159 |
* @param string $string |
|
|
160 |
* @return string |
|
|
161 |
* @access private |
|
|
162 |
*/ |
|
|
163 |
private function _canonicalize($string) |
|
|
164 |
{ |
|
|
165 |
return str_replace( |
|
|
166 |
array("\r\n", "\r", "\n"), |
|
|
167 |
array("\n", "\n", "\r\n"), |
|
|
168 |
$string |
|
|
169 |
); |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
} |