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