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.
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
24 * Support class for MultiPart Mime Messages
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
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;
44 const MULTIPART_ALTERNATIVE
= 'multipart/alternative';
45 const MULTIPART_MIXED
= 'multipart/mixed';
46 const MULTIPART_RELATED
= 'multipart/related';
49 protected static $makeUnique = 0;
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",
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",
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";
104 * Check if the given string is "printable"
106 * Checks that a string contains no unprintable characters. If this returns
107 * false, encode the string for secure delivery.
112 public static function isPrintable($str)
114 return (strcspn($str, self
::$qpKeysString) == strlen($str));
118 * Encode a given string with the QUOTED_PRINTABLE mechanism and wrap the lines.
121 * @param int $lineLength Defaults to {@link LINELENGTH}
122 * @param int $lineEnd Defaults to {@link LINEEND}
125 public static function encodeQuotedPrintable($str,
126 $lineLength = self
::LINELENGTH
,
127 $lineEnd = self
::LINEEND
)
130 $str = self
::_encodeQuotedPrintable($str);
132 // Split encoded text into separate lines
135 if ($ptr > $lineLength) {
139 // Ensure we are not splitting across an encoded character
140 $pos = strrpos(substr($str, 0, $ptr), '=');
141 if ($pos !== false && $pos >= $ptr - 2) {
145 // Check if there is a space at the end of the line and rewind
146 if ($ptr > 0 && $str[$ptr - 1] == ' ') {
150 // Add string and continue
151 $out .= substr($str, 0, $ptr) . '=' . $lineEnd;
152 $str = substr($str, $ptr);
155 $out = rtrim($out, $lineEnd);
156 $out = rtrim($out, '=');
161 * Converts a string into quoted printable format.
166 private static function _encodeQuotedPrintable($str)
168 $str = str_replace('=', '=3D', $str);
169 $str = str_replace(self
::$qpKeys, self
::$qpReplaceValues, $str);
175 * Encode a given string with the QUOTED_PRINTABLE mechanism for Mail Headers.
177 * Mail headers depend on an extended quoted printable algorithm otherwise
178 * a range of bugs can occur.
181 * @param string $charset
182 * @param int $lineLength Defaults to {@link LINELENGTH}
183 * @param int $lineEnd Defaults to {@link LINEEND}
186 public static function encodeQuotedPrintableHeader($str, $charset,
187 $lineLength = self
::LINELENGTH
,
188 $lineEnd = self
::LINEEND
)
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;
194 $str = self
::_encodeQuotedPrintable($str);
196 // Mail-Header required chars have to be encoded also:
197 $str = str_replace(array('?', ' ', '_'), array('=3F', '=20', '=5F'), $str);
199 // initialize first line, we need it anyways
200 $lines = array(0 => "");
202 // Split encoded text into separate lines
204 while(strlen($str) > 0) {
205 $currentLine = max(count($lines)-1, 0);
206 $token = self
::getNextQuotedPrintableToken($str);
207 $str = substr($str, strlen($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;
216 $lines[$currentLine] .= $tmp;
220 // don't forget to append the rest to the last line
221 if(strlen($str) == 0) {
222 $lines[$currentLine] .= $tmp;
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]."?=";
230 $str = trim(implode($lineEnd, $lines));
235 * Retrieves the first token from a quoted printable string.
240 private static function getNextQuotedPrintableToken($str)
242 if(substr($str, 0, 1) == "=") {
243 $token = substr($str, 0, 3);
245 $token = substr($str, 0, 1);
251 * Encode a given string in mail header compatible base64 encoding.
254 * @param string $charset
255 * @param int $lineLength Defaults to {@link LINELENGTH}
256 * @param int $lineEnd Defaults to {@link LINEEND}
259 public static function encodeBase64Header($str,
261 $lineLength = self
::LINELENGTH
,
262 $lineEnd = self
::LINEEND
)
264 $prefix = '=?' . $charset . '?B?';
266 $remainingLength = $lineLength - strlen($prefix) - strlen($suffix);
268 $encodedValue = self
::encodeBase64($str, $remainingLength, $lineEnd);
269 $encodedValue = str_replace($lineEnd, $suffix . $lineEnd . ' ' . $prefix, $encodedValue);
270 $encodedValue = $prefix . $encodedValue . $suffix;
271 return $encodedValue;
275 * Encode a given string in base64 encoding and break lines
276 * according to the maximum linelength.
279 * @param int $lineLength Defaults to {@link LINELENGTH}
280 * @param int $lineEnd Defaults to {@link LINEEND}
283 public static function encodeBase64($str,
284 $lineLength = self
::LINELENGTH
,
285 $lineEnd = self
::LINEEND
)
287 return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd));
293 * @param null|string $boundary
297 public function __construct($boundary = null)
299 // This string needs to be somewhat unique
300 if ($boundary === null) {
301 $this->_boundary
= '=_' . md5(microtime(1) . self
::$makeUnique++
);
303 $this->_boundary
= $boundary;
308 * Encode the given string with the given encoding.
311 * @param string $encoding
312 * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
315 public static function encode($str, $encoding, $EOL = self
::LINEEND
)
318 case self
::ENCODING_BASE64
:
319 return self
::encodeBase64($str, self
::LINELENGTH
, $EOL);
321 case self
::ENCODING_QUOTEDPRINTABLE
:
322 return self
::encodeQuotedPrintable($str, self
::LINELENGTH
, $EOL);
326 * @todo 7Bit and 8Bit is currently handled the same way.
333 * Return a MIME boundary
338 public function boundary()
340 return $this->_boundary
;
344 * Return a MIME boundary line
346 * @param mixed $EOL Defaults to {@link LINEEND}
350 public function boundaryLine($EOL = self
::LINEEND
)
352 return $EOL . '--' . $this->_boundary
. $EOL;
361 public function mimeEnd($EOL = self
::LINEEND
)
363 return $EOL . '--' . $this->_boundary
. '--' . $EOL;