composer package updates
[openemr.git] / vendor / zendframework / zend-mime / src / Mime.php
blob9b5ded0753335a05d79ba336e8ea8d9ce6f0bb20
1 <?php
2 /**
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
8 */
10 namespace Zend\Mime;
12 /**
13 * Support class for MultiPart Mime Messages
15 class Mime
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;
28 const LINEEND = "\n";
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
35 protected $boundary;
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",
60 "\xFF"
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",
84 "=FF"
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
91 /**
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.
97 * @param string $str
98 * @return bool
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.
108 * @param string $str
109 * @param int $lineLength Defaults to {@link LINELENGTH}
110 * @param string $lineEnd Defaults to {@link LINEEND}
111 * @return string
113 public static function encodeQuotedPrintable(
114 $str,
115 $lineLength = self::LINELENGTH,
116 $lineEnd = self::LINEEND
118 $out = '';
119 $str = self::_encodeQuotedPrintable($str);
121 // Split encoded text into separate lines
122 while ($str) {
123 $ptr = strlen($str);
124 if ($ptr > $lineLength) {
125 $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) {
131 $ptr = $pos;
134 if (ord($str[0]) == 0x2E) { // 0x2E is a dot
135 $str = '=2E' . substr($str, 1);
136 $ptr += 2;
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);
143 $ptr += 2;
144 break;
145 case 0x20: // Space
146 $str = substr_replace($str, '=20', $ptr - 1, 1);
147 $ptr += 2;
148 break;
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, '=');
158 return $out;
162 * Converts a string into quoted printable format.
164 * @param string $str
165 * @return string
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);
173 $str = rtrim($str);
174 return $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.
183 * @param string $str
184 * @param string $charset
185 * @param int $lineLength Defaults to {@link LINELENGTH}
186 * @param string $lineEnd Defaults to {@link LINEEND}
187 * @return string
189 public static function encodeQuotedPrintableHeader(
190 $str,
191 $charset,
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
205 $lines = [0 => ''];
207 // Split encoded text into separate lines
208 $tmp = '';
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;
215 $tmp .= $token;
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;
226 } else {
227 $lines[$currentLine] .= $tmp;
229 $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));
242 return $str;
246 * Retrieves the first token from a quoted printable string.
248 * @param string $str
249 * @return string
251 private static function getNextQuotedPrintableToken($str)
253 if (0 === strpos($str, '=')) {
254 $token = substr($str, 0, 3);
255 } else {
256 $token = substr($str, 0, 1);
258 return $token;
262 * Encode a given string in mail header compatible base64 encoding.
264 * @param string $str
265 * @param string $charset
266 * @param int $lineLength Defaults to {@link LINELENGTH}
267 * @param string $lineEnd Defaults to {@link LINEEND}
268 * @return string
270 public static function encodeBase64Header(
271 $str,
272 $charset,
273 $lineLength = self::LINELENGTH,
274 $lineEnd = self::LINEEND
276 $prefix = '=?' . $charset . '?B?';
277 $suffix = '?=';
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.
290 * @param string $str
291 * @param int $lineLength Defaults to {@link LINELENGTH}
292 * @param string $lineEnd Defaults to {@link LINEEND}
293 * @return string
295 public static function encodeBase64(
296 $str,
297 $lineLength = self::LINELENGTH,
298 $lineEnd = self::LINEEND
300 $lineLength = $lineLength - ($lineLength % 4);
301 return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd));
305 * Constructor
307 * @param null|string $boundary
308 * @access public
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++);
315 } else {
316 $this->boundary = $boundary;
321 * Encode the given string with the given encoding.
323 * @param string $str
324 * @param string $encoding
325 * @param string $EOL EOL string; defaults to {@link LINEEND}
326 * @return string
328 public static function encode($str, $encoding, $EOL = self::LINEEND)
330 switch ($encoding) {
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);
337 default:
339 * @todo 7Bit and 8Bit is currently handled the same way.
341 return $str;
346 * Return a MIME boundary
348 * @access public
349 * @return string
351 public function boundary()
353 return $this->boundary;
357 * Return a MIME boundary line
359 * @param string $EOL Defaults to {@link LINEEND}
360 * @access public
361 * @return string
363 public function boundaryLine($EOL = self::LINEEND)
365 return $EOL . '--' . $this->boundary . $EOL;
369 * Return MIME ending
371 * @param string $EOL Defaults to {@link LINEEND}
372 * @access public
373 * @return string
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
385 * @param string $str
386 * @return string
388 public static function mimeDetectCharset($str)
390 if (preg_match(self::CHARSET_REGEX, $str, $matches)) {
391 return strtoupper($matches['charset']);
394 return 'ASCII';