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