1 | <?php |
---|
2 | /** |
---|
3 | * Zend Framework |
---|
4 | * |
---|
5 | * LICENSE |
---|
6 | * |
---|
7 | * This source file is subject to the new BSD license that is bundled |
---|
8 | * with this package in the file LICENSE.txt. |
---|
9 | * It is also available through the world-wide-web at this URL: |
---|
10 | * http://framework.zend.com/license/new-bsd |
---|
11 | * If you did not receive a copy of the license and are unable to |
---|
12 | * obtain it through the world-wide-web, please send an email |
---|
13 | * to license@zend.com so we can send you a copy immediately. |
---|
14 | * |
---|
15 | * @category Zend |
---|
16 | * @package Zend_Mime |
---|
17 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
18 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
19 | * @version $Id: Mime.php 20096 2010-01-06 02:05:09Z bkarwin $ |
---|
20 | */ |
---|
21 | |
---|
22 | |
---|
23 | /** |
---|
24 | * Support class for MultiPart Mime Messages |
---|
25 | * |
---|
26 | * @category Zend |
---|
27 | * @package Zend_Mime |
---|
28 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
29 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
30 | */ |
---|
31 | class Zend_Mime |
---|
32 | { |
---|
33 | const TYPE_OCTETSTREAM = 'application/octet-stream'; |
---|
34 | const TYPE_TEXT = 'text/plain'; |
---|
35 | const TYPE_HTML = 'text/html'; |
---|
36 | const ENCODING_7BIT = '7bit'; |
---|
37 | const ENCODING_8BIT = '8bit'; |
---|
38 | const ENCODING_QUOTEDPRINTABLE = 'quoted-printable'; |
---|
39 | const ENCODING_BASE64 = 'base64'; |
---|
40 | const DISPOSITION_ATTACHMENT = 'attachment'; |
---|
41 | const DISPOSITION_INLINE = 'inline'; |
---|
42 | const LINELENGTH = 72; |
---|
43 | const LINEEND = "\n"; |
---|
44 | const MULTIPART_ALTERNATIVE = 'multipart/alternative'; |
---|
45 | const MULTIPART_MIXED = 'multipart/mixed'; |
---|
46 | const MULTIPART_RELATED = 'multipart/related'; |
---|
47 | |
---|
48 | protected $_boundary; |
---|
49 | protected static $makeUnique = 0; |
---|
50 | |
---|
51 | // lookup-Tables for QuotedPrintable |
---|
52 | public static $qpKeys = array( |
---|
53 | "\x00","\x01","\x02","\x03","\x04","\x05","\x06","\x07", |
---|
54 | "\x08","\x09","\x0A","\x0B","\x0C","\x0D","\x0E","\x0F", |
---|
55 | "\x10","\x11","\x12","\x13","\x14","\x15","\x16","\x17", |
---|
56 | "\x18","\x19","\x1A","\x1B","\x1C","\x1D","\x1E","\x1F", |
---|
57 | "\x7F","\x80","\x81","\x82","\x83","\x84","\x85","\x86", |
---|
58 | "\x87","\x88","\x89","\x8A","\x8B","\x8C","\x8D","\x8E", |
---|
59 | "\x8F","\x90","\x91","\x92","\x93","\x94","\x95","\x96", |
---|
60 | "\x97","\x98","\x99","\x9A","\x9B","\x9C","\x9D","\x9E", |
---|
61 | "\x9F","\xA0","\xA1","\xA2","\xA3","\xA4","\xA5","\xA6", |
---|
62 | "\xA7","\xA8","\xA9","\xAA","\xAB","\xAC","\xAD","\xAE", |
---|
63 | "\xAF","\xB0","\xB1","\xB2","\xB3","\xB4","\xB5","\xB6", |
---|
64 | "\xB7","\xB8","\xB9","\xBA","\xBB","\xBC","\xBD","\xBE", |
---|
65 | "\xBF","\xC0","\xC1","\xC2","\xC3","\xC4","\xC5","\xC6", |
---|
66 | "\xC7","\xC8","\xC9","\xCA","\xCB","\xCC","\xCD","\xCE", |
---|
67 | "\xCF","\xD0","\xD1","\xD2","\xD3","\xD4","\xD5","\xD6", |
---|
68 | "\xD7","\xD8","\xD9","\xDA","\xDB","\xDC","\xDD","\xDE", |
---|
69 | "\xDF","\xE0","\xE1","\xE2","\xE3","\xE4","\xE5","\xE6", |
---|
70 | "\xE7","\xE8","\xE9","\xEA","\xEB","\xEC","\xED","\xEE", |
---|
71 | "\xEF","\xF0","\xF1","\xF2","\xF3","\xF4","\xF5","\xF6", |
---|
72 | "\xF7","\xF8","\xF9","\xFA","\xFB","\xFC","\xFD","\xFE", |
---|
73 | "\xFF" |
---|
74 | ); |
---|
75 | |
---|
76 | public static $qpReplaceValues = array( |
---|
77 | "=00","=01","=02","=03","=04","=05","=06","=07", |
---|
78 | "=08","=09","=0A","=0B","=0C","=0D","=0E","=0F", |
---|
79 | "=10","=11","=12","=13","=14","=15","=16","=17", |
---|
80 | "=18","=19","=1A","=1B","=1C","=1D","=1E","=1F", |
---|
81 | "=7F","=80","=81","=82","=83","=84","=85","=86", |
---|
82 | "=87","=88","=89","=8A","=8B","=8C","=8D","=8E", |
---|
83 | "=8F","=90","=91","=92","=93","=94","=95","=96", |
---|
84 | "=97","=98","=99","=9A","=9B","=9C","=9D","=9E", |
---|
85 | "=9F","=A0","=A1","=A2","=A3","=A4","=A5","=A6", |
---|
86 | "=A7","=A8","=A9","=AA","=AB","=AC","=AD","=AE", |
---|
87 | "=AF","=B0","=B1","=B2","=B3","=B4","=B5","=B6", |
---|
88 | "=B7","=B8","=B9","=BA","=BB","=BC","=BD","=BE", |
---|
89 | "=BF","=C0","=C1","=C2","=C3","=C4","=C5","=C6", |
---|
90 | "=C7","=C8","=C9","=CA","=CB","=CC","=CD","=CE", |
---|
91 | "=CF","=D0","=D1","=D2","=D3","=D4","=D5","=D6", |
---|
92 | "=D7","=D8","=D9","=DA","=DB","=DC","=DD","=DE", |
---|
93 | "=DF","=E0","=E1","=E2","=E3","=E4","=E5","=E6", |
---|
94 | "=E7","=E8","=E9","=EA","=EB","=EC","=ED","=EE", |
---|
95 | "=EF","=F0","=F1","=F2","=F3","=F4","=F5","=F6", |
---|
96 | "=F7","=F8","=F9","=FA","=FB","=FC","=FD","=FE", |
---|
97 | "=FF" |
---|
98 | ); |
---|
99 | |
---|
100 | public static $qpKeysString = |
---|
101 | "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"; |
---|
102 | |
---|
103 | /** |
---|
104 | * Check if the given string is "printable" |
---|
105 | * |
---|
106 | * Checks that a string contains no unprintable characters. If this returns |
---|
107 | * false, encode the string for secure delivery. |
---|
108 | * |
---|
109 | * @param string $str |
---|
110 | * @return boolean |
---|
111 | */ |
---|
112 | public static function isPrintable($str) |
---|
113 | { |
---|
114 | return (strcspn($str, self::$qpKeysString) == strlen($str)); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Encode a given string with the QUOTED_PRINTABLE mechanism and wrap the lines. |
---|
119 | * |
---|
120 | * @param string $str |
---|
121 | * @param int $lineLength Defaults to {@link LINELENGTH} |
---|
122 | * @param int $lineEnd Defaults to {@link LINEEND} |
---|
123 | * @return string |
---|
124 | */ |
---|
125 | public static function encodeQuotedPrintable($str, |
---|
126 | $lineLength = self::LINELENGTH, |
---|
127 | $lineEnd = self::LINEEND) |
---|
128 | { |
---|
129 | $out = ''; |
---|
130 | $str = self::_encodeQuotedPrintable($str); |
---|
131 | |
---|
132 | // Split encoded text into separate lines |
---|
133 | while ($str) { |
---|
134 | $ptr = strlen($str); |
---|
135 | if ($ptr > $lineLength) { |
---|
136 | $ptr = $lineLength; |
---|
137 | } |
---|
138 | |
---|
139 | // Ensure we are not splitting across an encoded character |
---|
140 | $pos = strrpos(substr($str, 0, $ptr), '='); |
---|
141 | if ($pos !== false && $pos >= $ptr - 2) { |
---|
142 | $ptr = $pos; |
---|
143 | } |
---|
144 | |
---|
145 | // Check if there is a space at the end of the line and rewind |
---|
146 | if ($ptr > 0 && $str[$ptr - 1] == ' ') { |
---|
147 | --$ptr; |
---|
148 | } |
---|
149 | |
---|
150 | // Add string and continue |
---|
151 | $out .= substr($str, 0, $ptr) . '=' . $lineEnd; |
---|
152 | $str = substr($str, $ptr); |
---|
153 | } |
---|
154 | |
---|
155 | $out = rtrim($out, $lineEnd); |
---|
156 | $out = rtrim($out, '='); |
---|
157 | return $out; |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Converts a string into quoted printable format. |
---|
162 | * |
---|
163 | * @param string $str |
---|
164 | * @return string |
---|
165 | */ |
---|
166 | private static function _encodeQuotedPrintable($str) |
---|
167 | { |
---|
168 | $str = str_replace('=', '=3D', $str); |
---|
169 | $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str); |
---|
170 | $str = rtrim($str); |
---|
171 | return $str; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * Encode a given string with the QUOTED_PRINTABLE mechanism for Mail Headers. |
---|
176 | * |
---|
177 | * Mail headers depend on an extended quoted printable algorithm otherwise |
---|
178 | * a range of bugs can occur. |
---|
179 | * |
---|
180 | * @param string $str |
---|
181 | * @param string $charset |
---|
182 | * @param int $lineLength Defaults to {@link LINELENGTH} |
---|
183 | * @param int $lineEnd Defaults to {@link LINEEND} |
---|
184 | * @return string |
---|
185 | */ |
---|
186 | public static function encodeQuotedPrintableHeader($str, $charset, |
---|
187 | $lineLength = self::LINELENGTH, |
---|
188 | $lineEnd = self::LINEEND) |
---|
189 | { |
---|
190 | // Reduce line-length by the length of the required delimiter, charsets and encoding |
---|
191 | $prefix = sprintf('=?%s?Q?', $charset); |
---|
192 | $lineLength = $lineLength-strlen($prefix)-3; |
---|
193 | |
---|
194 | $str = self::_encodeQuotedPrintable($str); |
---|
195 | |
---|
196 | // Mail-Header required chars have to be encoded also: |
---|
197 | $str = str_replace(array('?', ' ', '_'), array('=3F', '=20', '=5F'), $str); |
---|
198 | |
---|
199 | // initialize first line, we need it anyways |
---|
200 | $lines = array(0 => ""); |
---|
201 | |
---|
202 | // Split encoded text into separate lines |
---|
203 | $tmp = ""; |
---|
204 | while(strlen($str) > 0) { |
---|
205 | $currentLine = max(count($lines)-1, 0); |
---|
206 | $token = self::getNextQuotedPrintableToken($str); |
---|
207 | $str = substr($str, strlen($token)); |
---|
208 | |
---|
209 | $tmp .= $token; |
---|
210 | if($token == '=20') { |
---|
211 | // only if we have a single char token or space, we can append the |
---|
212 | // tempstring it to the current line or start a new line if necessary. |
---|
213 | if(strlen($lines[$currentLine].$tmp) > $lineLength) { |
---|
214 | $lines[$currentLine+1] = $tmp; |
---|
215 | } else { |
---|
216 | $lines[$currentLine] .= $tmp; |
---|
217 | } |
---|
218 | $tmp = ""; |
---|
219 | } |
---|
220 | // don't forget to append the rest to the last line |
---|
221 | if(strlen($str) == 0) { |
---|
222 | $lines[$currentLine] .= $tmp; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | // assemble the lines together by pre- and appending delimiters, charset, encoding. |
---|
227 | for($i = 0; $i < count($lines); $i++) { |
---|
228 | $lines[$i] = " ".$prefix.$lines[$i]."?="; |
---|
229 | } |
---|
230 | $str = trim(implode($lineEnd, $lines)); |
---|
231 | return $str; |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * Retrieves the first token from a quoted printable string. |
---|
236 | * |
---|
237 | * @param string $str |
---|
238 | * @return string |
---|
239 | */ |
---|
240 | private static function getNextQuotedPrintableToken($str) |
---|
241 | { |
---|
242 | if(substr($str, 0, 1) == "=") { |
---|
243 | $token = substr($str, 0, 3); |
---|
244 | } else { |
---|
245 | $token = substr($str, 0, 1); |
---|
246 | } |
---|
247 | return $token; |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * Encode a given string in mail header compatible base64 encoding. |
---|
252 | * |
---|
253 | * @param string $str |
---|
254 | * @param string $charset |
---|
255 | * @param int $lineLength Defaults to {@link LINELENGTH} |
---|
256 | * @param int $lineEnd Defaults to {@link LINEEND} |
---|
257 | * @return string |
---|
258 | */ |
---|
259 | public static function encodeBase64Header($str, |
---|
260 | $charset, |
---|
261 | $lineLength = self::LINELENGTH, |
---|
262 | $lineEnd = self::LINEEND) |
---|
263 | { |
---|
264 | $prefix = '=?' . $charset . '?B?'; |
---|
265 | $suffix = '?='; |
---|
266 | $remainingLength = $lineLength - strlen($prefix) - strlen($suffix); |
---|
267 | |
---|
268 | $encodedValue = self::encodeBase64($str, $remainingLength, $lineEnd); |
---|
269 | $encodedValue = str_replace($lineEnd, $suffix . $lineEnd . ' ' . $prefix, $encodedValue); |
---|
270 | $encodedValue = $prefix . $encodedValue . $suffix; |
---|
271 | return $encodedValue; |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * Encode a given string in base64 encoding and break lines |
---|
276 | * according to the maximum linelength. |
---|
277 | * |
---|
278 | * @param string $str |
---|
279 | * @param int $lineLength Defaults to {@link LINELENGTH} |
---|
280 | * @param int $lineEnd Defaults to {@link LINEEND} |
---|
281 | * @return string |
---|
282 | */ |
---|
283 | public static function encodeBase64($str, |
---|
284 | $lineLength = self::LINELENGTH, |
---|
285 | $lineEnd = self::LINEEND) |
---|
286 | { |
---|
287 | return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd)); |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * Constructor |
---|
292 | * |
---|
293 | * @param null|string $boundary |
---|
294 | * @access public |
---|
295 | * @return void |
---|
296 | */ |
---|
297 | public function __construct($boundary = null) |
---|
298 | { |
---|
299 | // This string needs to be somewhat unique |
---|
300 | if ($boundary === null) { |
---|
301 | $this->_boundary = '=_' . md5(microtime(1) . self::$makeUnique++); |
---|
302 | } else { |
---|
303 | $this->_boundary = $boundary; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | * Encode the given string with the given encoding. |
---|
309 | * |
---|
310 | * @param string $str |
---|
311 | * @param string $encoding |
---|
312 | * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND} |
---|
313 | * @return string |
---|
314 | */ |
---|
315 | public static function encode($str, $encoding, $EOL = self::LINEEND) |
---|
316 | { |
---|
317 | switch ($encoding) { |
---|
318 | case self::ENCODING_BASE64: |
---|
319 | return self::encodeBase64($str, self::LINELENGTH, $EOL); |
---|
320 | |
---|
321 | case self::ENCODING_QUOTEDPRINTABLE: |
---|
322 | return self::encodeQuotedPrintable($str, self::LINELENGTH, $EOL); |
---|
323 | |
---|
324 | default: |
---|
325 | /** |
---|
326 | * @todo 7Bit and 8Bit is currently handled the same way. |
---|
327 | */ |
---|
328 | return $str; |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * Return a MIME boundary |
---|
334 | * |
---|
335 | * @access public |
---|
336 | * @return string |
---|
337 | */ |
---|
338 | public function boundary() |
---|
339 | { |
---|
340 | return $this->_boundary; |
---|
341 | } |
---|
342 | |
---|
343 | /** |
---|
344 | * Return a MIME boundary line |
---|
345 | * |
---|
346 | * @param mixed $EOL Defaults to {@link LINEEND} |
---|
347 | * @access public |
---|
348 | * @return string |
---|
349 | */ |
---|
350 | public function boundaryLine($EOL = self::LINEEND) |
---|
351 | { |
---|
352 | return $EOL . '--' . $this->_boundary . $EOL; |
---|
353 | } |
---|
354 | |
---|
355 | /** |
---|
356 | * Return MIME ending |
---|
357 | * |
---|
358 | * @access public |
---|
359 | * @return string |
---|
360 | */ |
---|
361 | public function mimeEnd($EOL = self::LINEEND) |
---|
362 | { |
---|
363 | return $EOL . '--' . $this->_boundary . '--' . $EOL; |
---|
364 | } |
---|
365 | } |
---|