3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
13 * Support class for MultiPart Mime Messages
17 // @codingStandardsIgnoreStart
18 const TYPE_OCTETSTREAM
= 'application/octet-stream';
19 const TYPE_TEXT
= 'text/plain';
20 const TYPE_HTML
= 'text/html';
21 const ENCODING_7BIT
= '7bit';
22 const ENCODING_8BIT
= '8bit';
23 const ENCODING_QUOTEDPRINTABLE
= 'quoted-printable';
24 const ENCODING_BASE64
= 'base64';
25 const DISPOSITION_ATTACHMENT
= 'attachment';
26 const DISPOSITION_INLINE
= 'inline';
27 const LINELENGTH
= 72;
29 const MULTIPART_ALTERNATIVE
= 'multipart/alternative';
30 const MULTIPART_MIXED
= 'multipart/mixed';
31 const MULTIPART_RELATED
= 'multipart/related';
32 const CHARSET_REGEX
= '#=\?(?P<charset>[\x21\x23-\x26\x2a\x2b\x2d\x5e\5f\60\x7b-\x7ea-zA-Z0-9]+)\?(?P<encoding>[\x21\x23-\x26\x2a\x2b\x2d\x5e\5f\60\x7b-\x7ea-zA-Z0-9]+)\?(?P<text>[\x21-\x3e\x40-\x7e]+)#';
33 // @codingStandardsIgnoreEnd
36 protected static $makeUnique = 0;
38 // lookup-Tables for QuotedPrintable
39 public static $qpKeys = [
40 "\x00","\x01","\x02","\x03","\x04","\x05","\x06","\x07",
41 "\x08","\x09","\x0A","\x0B","\x0C","\x0D","\x0E","\x0F",
42 "\x10","\x11","\x12","\x13","\x14","\x15","\x16","\x17",
43 "\x18","\x19","\x1A","\x1B","\x1C","\x1D","\x1E","\x1F",
44 "\x7F","\x80","\x81","\x82","\x83","\x84","\x85","\x86",
45 "\x87","\x88","\x89","\x8A","\x8B","\x8C","\x8D","\x8E",
46 "\x8F","\x90","\x91","\x92","\x93","\x94","\x95","\x96",
47 "\x97","\x98","\x99","\x9A","\x9B","\x9C","\x9D","\x9E",
48 "\x9F","\xA0","\xA1","\xA2","\xA3","\xA4","\xA5","\xA6",
49 "\xA7","\xA8","\xA9","\xAA","\xAB","\xAC","\xAD","\xAE",
50 "\xAF","\xB0","\xB1","\xB2","\xB3","\xB4","\xB5","\xB6",
51 "\xB7","\xB8","\xB9","\xBA","\xBB","\xBC","\xBD","\xBE",
52 "\xBF","\xC0","\xC1","\xC2","\xC3","\xC4","\xC5","\xC6",
53 "\xC7","\xC8","\xC9","\xCA","\xCB","\xCC","\xCD","\xCE",
54 "\xCF","\xD0","\xD1","\xD2","\xD3","\xD4","\xD5","\xD6",
55 "\xD7","\xD8","\xD9","\xDA","\xDB","\xDC","\xDD","\xDE",
56 "\xDF","\xE0","\xE1","\xE2","\xE3","\xE4","\xE5","\xE6",
57 "\xE7","\xE8","\xE9","\xEA","\xEB","\xEC","\xED","\xEE",
58 "\xEF","\xF0","\xF1","\xF2","\xF3","\xF4","\xF5","\xF6",
59 "\xF7","\xF8","\xF9","\xFA","\xFB","\xFC","\xFD","\xFE",
63 public static $qpReplaceValues = [
64 "=00","=01","=02","=03","=04","=05","=06","=07",
65 "=08","=09","=0A","=0B","=0C","=0D","=0E","=0F",
66 "=10","=11","=12","=13","=14","=15","=16","=17",
67 "=18","=19","=1A","=1B","=1C","=1D","=1E","=1F",
68 "=7F","=80","=81","=82","=83","=84","=85","=86",
69 "=87","=88","=89","=8A","=8B","=8C","=8D","=8E",
70 "=8F","=90","=91","=92","=93","=94","=95","=96",
71 "=97","=98","=99","=9A","=9B","=9C","=9D","=9E",
72 "=9F","=A0","=A1","=A2","=A3","=A4","=A5","=A6",
73 "=A7","=A8","=A9","=AA","=AB","=AC","=AD","=AE",
74 "=AF","=B0","=B1","=B2","=B3","=B4","=B5","=B6",
75 "=B7","=B8","=B9","=BA","=BB","=BC","=BD","=BE",
76 "=BF","=C0","=C1","=C2","=C3","=C4","=C5","=C6",
77 "=C7","=C8","=C9","=CA","=CB","=CC","=CD","=CE",
78 "=CF","=D0","=D1","=D2","=D3","=D4","=D5","=D6",
79 "=D7","=D8","=D9","=DA","=DB","=DC","=DD","=DE",
80 "=DF","=E0","=E1","=E2","=E3","=E4","=E5","=E6",
81 "=E7","=E8","=E9","=EA","=EB","=EC","=ED","=EE",
82 "=EF","=F0","=F1","=F2","=F3","=F4","=F5","=F6",
83 "=F7","=F8","=F9","=FA","=FB","=FC","=FD","=FE",
86 // @codingStandardsIgnoreStart
87 public static $qpKeysString =
88 "\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";
89 // @codingStandardsIgnoreEnd
92 * Check if the given string is "printable"
94 * Checks that a string contains no unprintable characters. If this returns
95 * false, encode the string for secure delivery.
100 public static function isPrintable($str)
102 return (strcspn($str, static::$qpKeysString) == strlen($str));
106 * Encode a given string with the QUOTED_PRINTABLE mechanism and wrap the lines.
109 * @param int $lineLength Defaults to {@link LINELENGTH}
110 * @param string $lineEnd Defaults to {@link LINEEND}
113 public static function encodeQuotedPrintable(
115 $lineLength = self
::LINELENGTH
,
116 $lineEnd = self
::LINEEND
119 $str = self
::_encodeQuotedPrintable($str);
121 // Split encoded text into separate lines
124 if ($ptr > $lineLength) {
128 // Ensure we are not splitting across an encoded character
129 $pos = strrpos(substr($str, 0, $ptr), '=');
130 if ($pos !== false && $pos >= $ptr - 2) {
134 if (ord($str[0]) == 0x2E) { // 0x2E is a dot
135 $str = '=2E' . substr($str, 1);
139 // copied from swiftmailer https://git.io/vAXU1
140 switch (ord(substr($str, $ptr - 1))) {
141 case 0x09: // Horizontal Tab
142 $str = substr_replace($str, '=09', $ptr - 1, 1);
146 $str = substr_replace($str, '=20', $ptr - 1, 1);
151 // Add string and continue
152 $out .= substr($str, 0, $ptr) . '=' . $lineEnd;
153 $str = substr($str, $ptr);
156 $out = rtrim($out, $lineEnd);
157 $out = rtrim($out, '=');
162 * Converts a string into quoted printable format.
167 // @codingStandardsIgnoreStart
168 private static function _encodeQuotedPrintable($str)
170 // @codingStandardsIgnoreEnd
171 $str = str_replace('=', '=3D', $str);
172 $str = str_replace(static::$qpKeys, static::$qpReplaceValues, $str);
178 * Encode a given string with the QUOTED_PRINTABLE mechanism for Mail Headers.
180 * Mail headers depend on an extended quoted printable algorithm otherwise
181 * a range of bugs can occur.
184 * @param string $charset
185 * @param int $lineLength Defaults to {@link LINELENGTH}
186 * @param string $lineEnd Defaults to {@link LINEEND}
189 public static function encodeQuotedPrintableHeader(
192 $lineLength = self
::LINELENGTH
,
193 $lineEnd = self
::LINEEND
195 // Reduce line-length by the length of the required delimiter, charsets and encoding
196 $prefix = sprintf('=?%s?Q?', $charset);
197 $lineLength = $lineLength - strlen($prefix) - 3;
199 $str = self
::_encodeQuotedPrintable($str);
201 // Mail-Header required chars have to be encoded also:
202 $str = str_replace(['?', ',', ' ', '_'], ['=3F', '=2C', '=20', '=5F'], $str);
204 // initialize first line, we need it anyways
207 // Split encoded text into separate lines
209 while (strlen($str) > 0) {
210 $currentLine = max(count($lines) - 1, 0);
211 $token = static::getNextQuotedPrintableToken($str);
212 $substr = substr($str, strlen($token));
213 $str = (false === $substr) ?
'' : $substr;
216 if ($token === '=20') {
217 // only if we have a single char token or space, we can append the
218 // tempstring it to the current line or start a new line if necessary.
219 $lineLimitReached = (strlen($lines[$currentLine] . $tmp) > $lineLength);
220 $noCurrentLine = ($lines[$currentLine] === '');
221 if ($noCurrentLine && $lineLimitReached) {
222 $lines[$currentLine] = $tmp;
223 $lines[$currentLine +
1] = '';
224 } elseif ($lineLimitReached) {
225 $lines[$currentLine +
1] = $tmp;
227 $lines[$currentLine] .= $tmp;
231 // don't forget to append the rest to the last line
232 if (strlen($str) === 0) {
233 $lines[$currentLine] .= $tmp;
237 // assemble the lines together by pre- and appending delimiters, charset, encoding.
238 for ($i = 0, $count = count($lines); $i < $count; $i++
) {
239 $lines[$i] = " " . $prefix . $lines[$i] . "?=";
241 $str = trim(implode($lineEnd, $lines));
246 * Retrieves the first token from a quoted printable string.
251 private static function getNextQuotedPrintableToken($str)
253 if (0 === strpos($str, '=')) {
254 $token = substr($str, 0, 3);
256 $token = substr($str, 0, 1);
262 * Encode a given string in mail header compatible base64 encoding.
265 * @param string $charset
266 * @param int $lineLength Defaults to {@link LINELENGTH}
267 * @param string $lineEnd Defaults to {@link LINEEND}
270 public static function encodeBase64Header(
273 $lineLength = self
::LINELENGTH
,
274 $lineEnd = self
::LINEEND
276 $prefix = '=?' . $charset . '?B?';
278 $remainingLength = $lineLength - strlen($prefix) - strlen($suffix);
280 $encodedValue = static::encodeBase64($str, $remainingLength, $lineEnd);
281 $encodedValue = str_replace($lineEnd, $suffix . $lineEnd . ' ' . $prefix, $encodedValue);
282 $encodedValue = $prefix . $encodedValue . $suffix;
283 return $encodedValue;
287 * Encode a given string in base64 encoding and break lines
288 * according to the maximum linelength.
291 * @param int $lineLength Defaults to {@link LINELENGTH}
292 * @param string $lineEnd Defaults to {@link LINEEND}
295 public static function encodeBase64(
297 $lineLength = self
::LINELENGTH
,
298 $lineEnd = self
::LINEEND
300 $lineLength = $lineLength - ($lineLength %
4);
301 return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd));
307 * @param null|string $boundary
310 public function __construct($boundary = null)
312 // This string needs to be somewhat unique
313 if ($boundary === null) {
314 $this->boundary
= '=_' . md5(microtime(1) . static::$makeUnique++
);
316 $this->boundary
= $boundary;
321 * Encode the given string with the given encoding.
324 * @param string $encoding
325 * @param string $EOL EOL string; defaults to {@link LINEEND}
328 public static function encode($str, $encoding, $EOL = self
::LINEEND
)
331 case self
::ENCODING_BASE64
:
332 return static::encodeBase64($str, self
::LINELENGTH
, $EOL);
334 case self
::ENCODING_QUOTEDPRINTABLE
:
335 return static::encodeQuotedPrintable($str, self
::LINELENGTH
, $EOL);
339 * @todo 7Bit and 8Bit is currently handled the same way.
346 * Return a MIME boundary
351 public function boundary()
353 return $this->boundary
;
357 * Return a MIME boundary line
359 * @param string $EOL Defaults to {@link LINEEND}
363 public function boundaryLine($EOL = self
::LINEEND
)
365 return $EOL . '--' . $this->boundary
. $EOL;
371 * @param string $EOL Defaults to {@link LINEEND}
375 public function mimeEnd($EOL = self
::LINEEND
)
377 return $EOL . '--' . $this->boundary
. '--' . $EOL;
381 * Detect MIME charset
383 * Extract parts according to https://tools.ietf.org/html/rfc2047#section-2
388 public static function mimeDetectCharset($str)
390 if (preg_match(self
::CHARSET_REGEX
, $str, $matches)) {
391 return strtoupper($matches['charset']);