Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mime.php
Go to the documentation of this file.
1 <?php
30 class Zend_Mime
31 {
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';
46 
52  protected $_boundary;
53 
57  protected static $makeUnique = 0;
58 
64  public static $qpKeys = array(
65  "\x00",
66  "\x01",
67  "\x02",
68  "\x03",
69  "\x04",
70  "\x05",
71  "\x06",
72  "\x07",
73  "\x08",
74  "\x09",
75  "\x0A",
76  "\x0B",
77  "\x0C",
78  "\x0D",
79  "\x0E",
80  "\x0F",
81  "\x10",
82  "\x11",
83  "\x12",
84  "\x13",
85  "\x14",
86  "\x15",
87  "\x16",
88  "\x17",
89  "\x18",
90  "\x19",
91  "\x1A",
92  "\x1B",
93  "\x1C",
94  "\x1D",
95  "\x1E",
96  "\x1F",
97  "\x7F",
98  "\x80",
99  "\x81",
100  "\x82",
101  "\x83",
102  "\x84",
103  "\x85",
104  "\x86",
105  "\x87",
106  "\x88",
107  "\x89",
108  "\x8A",
109  "\x8B",
110  "\x8C",
111  "\x8D",
112  "\x8E",
113  "\x8F",
114  "\x90",
115  "\x91",
116  "\x92",
117  "\x93",
118  "\x94",
119  "\x95",
120  "\x96",
121  "\x97",
122  "\x98",
123  "\x99",
124  "\x9A",
125  "\x9B",
126  "\x9C",
127  "\x9D",
128  "\x9E",
129  "\x9F",
130  "\xA0",
131  "\xA1",
132  "\xA2",
133  "\xA3",
134  "\xA4",
135  "\xA5",
136  "\xA6",
137  "\xA7",
138  "\xA8",
139  "\xA9",
140  "\xAA",
141  "\xAB",
142  "\xAC",
143  "\xAD",
144  "\xAE",
145  "\xAF",
146  "\xB0",
147  "\xB1",
148  "\xB2",
149  "\xB3",
150  "\xB4",
151  "\xB5",
152  "\xB6",
153  "\xB7",
154  "\xB8",
155  "\xB9",
156  "\xBA",
157  "\xBB",
158  "\xBC",
159  "\xBD",
160  "\xBE",
161  "\xBF",
162  "\xC0",
163  "\xC1",
164  "\xC2",
165  "\xC3",
166  "\xC4",
167  "\xC5",
168  "\xC6",
169  "\xC7",
170  "\xC8",
171  "\xC9",
172  "\xCA",
173  "\xCB",
174  "\xCC",
175  "\xCD",
176  "\xCE",
177  "\xCF",
178  "\xD0",
179  "\xD1",
180  "\xD2",
181  "\xD3",
182  "\xD4",
183  "\xD5",
184  "\xD6",
185  "\xD7",
186  "\xD8",
187  "\xD9",
188  "\xDA",
189  "\xDB",
190  "\xDC",
191  "\xDD",
192  "\xDE",
193  "\xDF",
194  "\xE0",
195  "\xE1",
196  "\xE2",
197  "\xE3",
198  "\xE4",
199  "\xE5",
200  "\xE6",
201  "\xE7",
202  "\xE8",
203  "\xE9",
204  "\xEA",
205  "\xEB",
206  "\xEC",
207  "\xED",
208  "\xEE",
209  "\xEF",
210  "\xF0",
211  "\xF1",
212  "\xF2",
213  "\xF3",
214  "\xF4",
215  "\xF5",
216  "\xF6",
217  "\xF7",
218  "\xF8",
219  "\xF9",
220  "\xFA",
221  "\xFB",
222  "\xFC",
223  "\xFD",
224  "\xFE",
225  "\xFF"
226  );
227 
231  public static $qpReplaceValues = array(
232  "=00",
233  "=01",
234  "=02",
235  "=03",
236  "=04",
237  "=05",
238  "=06",
239  "=07",
240  "=08",
241  "=09",
242  "=0A",
243  "=0B",
244  "=0C",
245  "=0D",
246  "=0E",
247  "=0F",
248  "=10",
249  "=11",
250  "=12",
251  "=13",
252  "=14",
253  "=15",
254  "=16",
255  "=17",
256  "=18",
257  "=19",
258  "=1A",
259  "=1B",
260  "=1C",
261  "=1D",
262  "=1E",
263  "=1F",
264  "=7F",
265  "=80",
266  "=81",
267  "=82",
268  "=83",
269  "=84",
270  "=85",
271  "=86",
272  "=87",
273  "=88",
274  "=89",
275  "=8A",
276  "=8B",
277  "=8C",
278  "=8D",
279  "=8E",
280  "=8F",
281  "=90",
282  "=91",
283  "=92",
284  "=93",
285  "=94",
286  "=95",
287  "=96",
288  "=97",
289  "=98",
290  "=99",
291  "=9A",
292  "=9B",
293  "=9C",
294  "=9D",
295  "=9E",
296  "=9F",
297  "=A0",
298  "=A1",
299  "=A2",
300  "=A3",
301  "=A4",
302  "=A5",
303  "=A6",
304  "=A7",
305  "=A8",
306  "=A9",
307  "=AA",
308  "=AB",
309  "=AC",
310  "=AD",
311  "=AE",
312  "=AF",
313  "=B0",
314  "=B1",
315  "=B2",
316  "=B3",
317  "=B4",
318  "=B5",
319  "=B6",
320  "=B7",
321  "=B8",
322  "=B9",
323  "=BA",
324  "=BB",
325  "=BC",
326  "=BD",
327  "=BE",
328  "=BF",
329  "=C0",
330  "=C1",
331  "=C2",
332  "=C3",
333  "=C4",
334  "=C5",
335  "=C6",
336  "=C7",
337  "=C8",
338  "=C9",
339  "=CA",
340  "=CB",
341  "=CC",
342  "=CD",
343  "=CE",
344  "=CF",
345  "=D0",
346  "=D1",
347  "=D2",
348  "=D3",
349  "=D4",
350  "=D5",
351  "=D6",
352  "=D7",
353  "=D8",
354  "=D9",
355  "=DA",
356  "=DB",
357  "=DC",
358  "=DD",
359  "=DE",
360  "=DF",
361  "=E0",
362  "=E1",
363  "=E2",
364  "=E3",
365  "=E4",
366  "=E5",
367  "=E6",
368  "=E7",
369  "=E8",
370  "=E9",
371  "=EA",
372  "=EB",
373  "=EC",
374  "=ED",
375  "=EE",
376  "=EF",
377  "=F0",
378  "=F1",
379  "=F2",
380  "=F3",
381  "=F4",
382  "=F5",
383  "=F6",
384  "=F7",
385  "=F8",
386  "=F9",
387  "=FA",
388  "=FB",
389  "=FC",
390  "=FD",
391  "=FE",
392  "=FF"
393  );
394 
398  public static $qpKeysString =
399  "\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";
400 
410  public static function isPrintable($str)
411  {
412  return (strcspn($str, self::$qpKeysString) == strlen($str));
413  }
414 
423  public static function encodeQuotedPrintable(
424  $str,
425  $lineLength = self::LINELENGTH,
426  $lineEnd = self::LINEEND
427  )
428  {
429  $out = '';
430  $str = self::_encodeQuotedPrintable($str);
431 
432  // Split encoded text into separate lines
433  while (strlen($str) > 0) {
434  $ptr = strlen($str);
435  if ($ptr > $lineLength) {
436  $ptr = $lineLength;
437  }
438 
439  // Ensure we are not splitting across an encoded character
440  $pos = strrpos(substr($str, 0, $ptr), '=');
441  if ($pos !== false && $pos >= $ptr - 2) {
442  $ptr = $pos;
443  }
444 
445  // Check if there is a space at the end of the line and rewind
446  if ($ptr > 0 && $str[$ptr - 1] == ' ') {
447  --$ptr;
448  }
449 
450  // Add string and continue
451  $out .= substr($str, 0, $ptr) . '=' . $lineEnd;
452  $str = substr($str, $ptr);
453  }
454 
455  $out = rtrim($out, $lineEnd);
456  $out = rtrim($out, '=');
457 
458  return $out;
459  }
460 
467  private static function _encodeQuotedPrintable($str)
468  {
469  $str = str_replace('=', '=3D', $str);
470  $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str);
471  $str = rtrim($str);
472 
473  return $str;
474  }
475 
488  public static function encodeQuotedPrintableHeader(
489  $str, $charset, $lineLength = self::LINELENGTH, $lineEnd = self::LINEEND
490  )
491  {
492  // Reduce line-length by the length of the required delimiter, charsets and encoding
493  $prefix = sprintf('=?%s?Q?', $charset);
494  $lineLength = $lineLength - strlen($prefix) - 3;
495 
496  $str = self::_encodeQuotedPrintable($str);
497 
498  // Mail-Header required chars have to be encoded also:
499  $str = str_replace(
500  array('?', ' ', '_', ','), array('=3F', '=20', '=5F', '=2C'), $str
501  );
502 
503  // initialize first line, we need it anyways
504  $lines = array(0 => "");
505 
506  // Split encoded text into separate lines
507  $tmp = "";
508  while (strlen($str) > 0) {
509  $currentLine = max(count($lines) - 1, 0);
510  $token = self::getNextQuotedPrintableToken($str);
511  $str = substr($str, strlen($token));
512 
513  $tmp .= $token;
514  if ($token == '=20') {
515  // only if we have a single char token or space, we can append the
516  // tempstring it to the current line or start a new line if necessary.
517  if (strlen($lines[$currentLine] . $tmp) > $lineLength) {
518  $lines[$currentLine + 1] = $tmp;
519  } else {
520  $lines[$currentLine] .= $tmp;
521  }
522  $tmp = "";
523  }
524  // don't forget to append the rest to the last line
525  if (strlen($str) == 0) {
526  $lines[$currentLine] .= $tmp;
527  }
528  }
529 
530  // assemble the lines together by pre- and appending delimiters, charset, encoding.
531  for ($i = 0; $i < count($lines); $i++) {
532  $lines[$i] = " " . $prefix . $lines[$i] . "?=";
533  }
534  $str = trim(implode($lineEnd, $lines));
535 
536  return $str;
537  }
538 
545  private static function getNextQuotedPrintableToken($str)
546  {
547  if (substr($str, 0, 1) == "=") {
548  $token = substr($str, 0, 3);
549  } else {
550  $token = substr($str, 0, 1);
551  }
552 
553  return $token;
554  }
555 
565  public static function encodeBase64Header(
566  $str, $charset, $lineLength = self::LINELENGTH, $lineEnd = self::LINEEND
567  )
568  {
569  $prefix = '=?' . $charset . '?B?';
570  $suffix = '?=';
571  $remainingLength = $lineLength - strlen($prefix) - strlen($suffix);
572 
573  $encodedValue = self::encodeBase64($str, $remainingLength, $lineEnd);
574  $encodedValue = str_replace(
575  $lineEnd, $suffix . $lineEnd . ' ' . $prefix, $encodedValue
576  );
577  $encodedValue = $prefix . $encodedValue . $suffix;
578 
579  return $encodedValue;
580  }
581 
591  public static function encodeBase64(
592  $str, $lineLength = self::LINELENGTH, $lineEnd = self::LINEEND
593  )
594  {
595  return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd));
596  }
597 
603  public function __construct($boundary = null)
604  {
605  // This string needs to be somewhat unique
606  if ($boundary === null) {
607  $this->_boundary = '=_' . md5(microtime(1) . self::$makeUnique++);
608  } else {
609  $this->_boundary = $boundary;
610  }
611  }
612 
621  public static function encode($str, $encoding, $EOL = self::LINEEND)
622  {
623  switch ($encoding) {
625  return self::encodeBase64($str, self::LINELENGTH, $EOL);
626 
628  return self::encodeQuotedPrintable($str, self::LINELENGTH, $EOL);
629 
630  default:
634  return $str;
635  }
636  }
637 
644  public function boundary()
645  {
646  return $this->_boundary;
647  }
648 
655  public function boundaryLine($EOL = self::LINEEND)
656  {
657  return $EOL . '--' . $this->_boundary . $EOL;
658  }
659 
666  public function mimeEnd($EOL = self::LINEEND)
667  {
668  return $EOL . '--' . $this->_boundary . '--' . $EOL;
669  }
670 }
const TYPE_TEXT
Definition: Mime.php:33
const TYPE_HTML
Definition: Mime.php:34
$suffix
Definition: name.phtml:27
const MULTIPART_RELATED
Definition: Mime.php:45
static encode($str, $encoding, $EOL=self::LINEEND)
Definition: Mime.php:621
const DISPOSITION_INLINE
Definition: Mime.php:40
const ENCODING_QUOTEDPRINTABLE
Definition: Mime.php:37
const ENCODING_7BIT
Definition: Mime.php:35
const TYPE_OCTETSTREAM
Definition: Mime.php:32
const ENCODING_BASE64
Definition: Mime.php:38
static encodeQuotedPrintableHeader( $str, $charset, $lineLength=self::LINELENGTH, $lineEnd=self::LINEEND)
Definition: Mime.php:488
const ENCODING_8BIT
Definition: Mime.php:36
static $qpReplaceValues
Definition: Mime.php:231
const LINEEND
Definition: Mime.php:42
const LINELENGTH
Definition: Mime.php:41
$prefix
Definition: name.phtml:25
$pos
Definition: list.phtml:42
static encodeBase64( $str, $lineLength=self::LINELENGTH, $lineEnd=self::LINEEND)
Definition: Mime.php:591
static $makeUnique
Definition: Mime.php:57
static $qpKeys
Definition: Mime.php:64
$_boundary
Definition: Mime.php:52
static $qpKeysString
Definition: Mime.php:398
boundary()
Definition: Mime.php:644
static encodeQuotedPrintable( $str, $lineLength=self::LINELENGTH, $lineEnd=self::LINEEND)
Definition: Mime.php:423
const MULTIPART_ALTERNATIVE
Definition: Mime.php:43
const DISPOSITION_ATTACHMENT
Definition: Mime.php:39
$i
Definition: gallery.phtml:31
const MULTIPART_MIXED
Definition: Mime.php:44
static isPrintable($str)
Definition: Mime.php:410
static encodeBase64Header( $str, $charset, $lineLength=self::LINELENGTH, $lineEnd=self::LINEEND)
Definition: Mime.php:565
__construct($boundary=null)
Definition: Mime.php:603
boundaryLine($EOL=self::LINEEND)
Definition: Mime.php:655
mimeEnd($EOL=self::LINEEND)
Definition: Mime.php:666