1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21:
22:
23: 24: 25: 26: 27: 28: 29: 30:
31: class Zend_Mime
32: {
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 = 200;
43: const LINEEND = "\n";
44: const MULTIPART_ALTERNATIVE = 'multipart/alternative';
45: const MULTIPART_MIXED = 'multipart/mixed';
46: const MULTIPART_RELATED = 'multipart/related';
47:
48: protected $_boundary;
49: protected static $makeUnique = 0;
50:
51:
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",
73: "\xFF"
74: );
75:
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",
97: "=FF"
98: );
99:
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";
102:
103: 104: 105: 106: 107: 108: 109: 110: 111:
112: public static function isPrintable($str)
113: {
114: return (strcspn($str, self::$qpKeysString) == strlen($str));
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124:
125: public static function encodeQuotedPrintable($str,
126: $lineLength = self::LINELENGTH,
127: $lineEnd = self::LINEEND)
128: {
129: $out = '';
130: $str = self::_encodeQuotedPrintable($str);
131:
132:
133: while ($str) {
134: $ptr = strlen($str);
135: if ($ptr > $lineLength) {
136: $ptr = $lineLength;
137: }
138:
139:
140: $pos = strrpos(substr($str, 0, $ptr), '=');
141: if ($pos !== false && $pos >= $ptr - 2) {
142: $ptr = $pos;
143: }
144:
145:
146: if ($ptr > 0 && $str[$ptr - 1] == ' ') {
147: --$ptr;
148: }
149:
150:
151: $out .= substr($str, 0, $ptr) . '=' . $lineEnd;
152: $str = substr($str, $ptr);
153: }
154:
155: $out = rtrim($out, $lineEnd);
156: $out = rtrim($out, '=');
157: return $out;
158: }
159:
160: 161: 162: 163: 164: 165:
166: private static function _encodeQuotedPrintable($str)
167: {
168: $str = str_replace('=', '=3D', $str);
169: $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str);
170: $str = rtrim($str);
171: return $str;
172: }
173:
174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185:
186: public static function ($str, $charset,
187: $lineLength = self::LINELENGTH,
188: $lineEnd = self::LINEEND)
189: {
190:
191: $prefix = sprintf('=?%s?Q?', $charset);
192: $lineLength = $lineLength-strlen($prefix)-3;
193:
194: $str = self::_encodeQuotedPrintable($str);
195:
196:
197: $str = str_replace(array('?', ' ', '_'), array('=3F', '=20', '=5F'), $str);
198:
199:
200: $lines = array(0 => "");
201:
202:
203: $tmp = "";
204: while(strlen($str) > 0) {
205: $currentLine = max(count($lines)-1, 0);
206: $token = self::getNextQuotedPrintableToken($str);
207: $str = substr($str, strlen($token));
208:
209: $tmp .= $token;
210: if($token == '=20') {
211:
212:
213: if(strlen($lines[$currentLine].$tmp) > $lineLength) {
214: $lines[$currentLine+1] = $tmp;
215: } else {
216: $lines[$currentLine] .= $tmp;
217: }
218: $tmp = "";
219: }
220:
221: if(strlen($str) == 0) {
222: $lines[$currentLine] .= $tmp;
223: }
224: }
225:
226:
227: for($i = 0; $i < count($lines); $i++) {
228: $lines[$i] = " ".$prefix.$lines[$i]."?=";
229: }
230: $str = trim(implode($lineEnd, $lines));
231: return $str;
232: }
233:
234: 235: 236: 237: 238: 239:
240: private static function getNextQuotedPrintableToken($str)
241: {
242: if(substr($str, 0, 1) == "=") {
243: $token = substr($str, 0, 3);
244: } else {
245: $token = substr($str, 0, 1);
246: }
247: return $token;
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258:
259: public static function ($str,
260: $charset,
261: $lineLength = self::LINELENGTH,
262: $lineEnd = self::LINEEND)
263: {
264: $prefix = '=?' . $charset . '?B?';
265: $suffix = '?=';
266: $remainingLength = $lineLength - strlen($prefix) - strlen($suffix);
267:
268: $encodedValue = self::encodeBase64($str, $remainingLength, $lineEnd);
269: $encodedValue = str_replace($lineEnd, $suffix . $lineEnd . ' ' . $prefix, $encodedValue);
270: $encodedValue = $prefix . $encodedValue . $suffix;
271: return $encodedValue;
272: }
273:
274: 275: 276: 277: 278: 279: 280: 281: 282:
283: public static function encodeBase64($str,
284: $lineLength = self::LINELENGTH,
285: $lineEnd = self::LINEEND)
286: {
287: return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd));
288: }
289:
290: 291: 292: 293: 294: 295: 296:
297: public function __construct($boundary = null)
298: {
299:
300: if ($boundary === null) {
301: $this->_boundary = '=_' . md5(microtime(1) . self::$makeUnique++);
302: } else {
303: $this->_boundary = $boundary;
304: }
305: }
306:
307: 308: 309: 310: 311: 312: 313: 314:
315: public static function encode($str, $encoding, $EOL = self::LINEEND)
316: {
317: switch ($encoding) {
318: case self::ENCODING_BASE64:
319: return self::encodeBase64($str, self::LINELENGTH, $EOL);
320:
321: case self::ENCODING_QUOTEDPRINTABLE:
322: return self::encodeQuotedPrintable($str, self::LINELENGTH, $EOL);
323:
324: default:
325: 326: 327:
328: return $str;
329: }
330: }
331:
332: 333: 334: 335: 336: 337:
338: public function boundary()
339: {
340: return $this->_boundary;
341: }
342:
343: 344: 345: 346: 347: 348: 349:
350: public function boundaryLine($EOL = self::LINEEND)
351: {
352: return $EOL . '--' . $this->_boundary . $EOL;
353: }
354:
355: 356: 357: 358: 359: 360:
361: public function mimeEnd($EOL = self::LINEEND)
362: {
363: return $EOL . '--' . $this->_boundary . '--' . $EOL;
364: }
365: }
366: