ab02e8a9 |
1 | <?php |
2 | /******************************************************************************** |
3 | * banana/mimepart.inc.php : class for MIME parts |
4 | * ------------------------ |
5 | * |
6 | * This file is part of the banana distribution |
7 | * Copyright: See COPYING files that comes with this distribution |
8 | ********************************************************************************/ |
9 | |
10 | class BananaMimePart |
11 | { |
12 | public $headers = null; /* Should be protected */ |
13 | |
14 | private $id = null; |
15 | private $content_type = null; |
16 | private $charset = null; |
17 | private $encoding = null; |
18 | private $disposition = null; |
19 | private $boundary = null; |
20 | private $filename = null; |
21 | private $format = null; |
22 | |
23 | private $body = null; |
24 | private $multipart = null; |
25 | |
26 | protected function __construct($data = null) |
27 | { |
28 | if (!is_null($data)) { |
29 | $this->fromRaw($data); |
30 | } |
31 | } |
32 | |
33 | protected function makeTextPart($body, $content_type, $encoding, $charset = null, $format = 'fixed') |
34 | { |
35 | $this->body = $body; |
36 | $this->charset = $charset; |
37 | $this->encoding = $encoding; |
38 | $this->content_type = $content_type; |
39 | $this->format = strtolower($format); |
40 | $this->parse(); |
41 | } |
42 | |
43 | protected function makeDataPart($body, $content_type, $encoding, $filename, $disposition, $id = null) |
44 | { |
45 | $this->body = $body; |
46 | $this->content_type = $content_type; |
47 | $this->encoding = $encoding; |
48 | $this->filename = $filename; |
49 | $this->disposition = $disposition; |
50 | $this->id = $id; |
51 | if (is_null($content_type) || $content_type == 'application/octet-stream') { |
52 | $this->decodeContent(); |
53 | $this->content_type = BananaMimePart::getMimeType($body, false); |
54 | } |
55 | } |
56 | |
57 | protected function makeFilePart($file, $content_type =null, $disposition = 'attachment') |
58 | { |
59 | $body = file_get_contents($file['tmp_name']); |
60 | if ($body === false || strlen($body) != $file['size']) { |
61 | return false; |
62 | } |
63 | if (is_null($content_type) || $content_type == 'application/octet-stream') { |
64 | $content_type = BananaMimePart::getMimeType($file['tmp_name']); |
65 | } |
66 | if (substr($content_type, 0, 5) == 'text/') { |
67 | $encoding = '8bit'; |
68 | } else { |
69 | $encoding = 'base64'; |
70 | $body = chunk_split(base64_encode($body)); |
71 | } |
72 | $this->filename = $file['name']; |
73 | $this->content_type = $content_type; |
74 | $this->disposition = $disposition; |
75 | $this->body = $body; |
76 | $this->encoding = $encoding; |
77 | return true; |
78 | } |
79 | |
80 | protected function makeMultiPart($body, $content_type, $encoding, $boundary) |
81 | { |
82 | $this->body = $body; |
83 | $this->content_type = $content_type; |
84 | $this->encoding = $encoding; |
85 | $this->boundary = $boundary; |
86 | $this->parse(); |
87 | } |
88 | |
89 | protected function convertToMultiPart() |
90 | { |
91 | if (!$this->isType('multipart', 'mixed')) { |
92 | $newpart = $this; |
93 | $this->content_type = 'multipart/mixed'; |
94 | $this->encoding = '8bit'; |
95 | $this->multipart = array($newpart); |
96 | $this->headers = null; |
97 | $this->charset = null; |
98 | $this->disposition = null; |
99 | $this->filename = null; |
100 | $this->boundary = null; |
101 | $this->body = null; |
102 | $this->format = null; |
103 | $this->id = null; |
104 | } |
105 | } |
106 | |
107 | public function addAttachment(array $file, $content_type = null, $disposition = 'attachment') |
108 | { |
109 | $newpart = new BananaMimePart; |
110 | if (!is_uploaded_file($file['tmp_name'])) { |
111 | return false; |
112 | } |
113 | if ($newpart->makeFilePart($file, $content_type, $disposition)) { |
114 | $this->convertToMultiPart(); |
115 | $this->multipart[] = $newpart; |
116 | return true; |
117 | } |
118 | return false; |
119 | } |
120 | |
121 | protected function getHeader($title, $filter = null) |
122 | { |
123 | if (!isset($this->headers[$title])) { |
124 | return null; |
125 | } |
126 | $header =& $this->headers[$title]; |
127 | if (is_null($filter)) { |
128 | return trim($header); |
129 | } elseif (preg_match($filter, $header, $matches)) { |
130 | return trim($matches[1]); |
131 | } |
132 | return null; |
133 | } |
134 | |
135 | protected function fromRaw($data) |
136 | { |
137 | if (is_array($data)) { |
138 | if (array_key_exists('From', $data)) { |
139 | $this->headers = array_map(array($this, 'decodeHeader'), array_change_key_case($data)); |
140 | return; |
141 | } else { |
142 | $lines = $data; |
143 | } |
144 | } else { |
145 | $lines = explode("\n", $data); |
146 | } |
147 | $headers = BananaMimePart::parseHeaders($lines); |
148 | $this->headers =& $headers; |
149 | if (empty($headers) || empty($lines)) { |
150 | return; |
151 | } |
152 | $content = join("\n", $lines); |
153 | $test = trim($content); |
154 | if (empty($test)) { |
155 | return; |
156 | } |
157 | |
158 | $content_type = strtolower($this->getHeader('content-type', '/^\s*([^ ;]+?)(;|$)/')); |
159 | if (empty($content_type)) { |
160 | $encoding = '8bit'; |
161 | $charset = 'CP1252'; |
162 | $content_type = 'text/plain'; |
163 | $format = strtolower($this->getHeader('x-rfc2646', '/format="?([^"]+?)"?\s*(;|$)/i')); |
164 | } else { |
165 | $encoding = strtolower($this->getHeader('content-transfer-encoding')); |
166 | $disposition = $this->getHeader('content-disposition', '/(inline|attachment)/i'); |
167 | $boundary = $this->getHeader('content-type', '/boundary="?([^"]+?)"?\s*(;|$)/i'); |
168 | $charset = strtolower($this->getHeader('content-type', '/charset="?([^"]+?)"?\s*(;|$)/i')); |
169 | $filename = $this->getHeader('content-disposition', '/filename="?([^"]+?)"?\s*(;|$)/i'); |
170 | $format = strtolower($this->getHeader('content-type', '/format="?([^"]+?)"?\s*(;|$)/i')); |
171 | $id = $this->getHeader('content-id', '/<(.*?)>/'); |
172 | if (empty($filename)) { |
173 | $filename = $this->getHeader('content-type', '/name="?([^"]+)"?/'); |
174 | } |
175 | } |
176 | list($type, $subtype) = explode('/', $content_type); |
177 | switch ($type) { |
178 | case 'text': case 'message': |
179 | $this->makeTextPart($content, $content_type, $encoding, $charset, $format); |
180 | break; |
181 | case 'multipart': |
182 | $this->makeMultiPart($content, $content_type, $encoding, $boundary); |
183 | break; |
184 | default: |
185 | $this->makeDataPart($content, $content_type, $encoding, $filename, $disposition, $id); |
186 | } |
187 | } |
188 | |
189 | private function parse() |
190 | { |
191 | if ($this->isType('multipart')) { |
192 | $this->splitMultipart(); |
193 | } else { |
194 | $parts = $this->findUUEncoded(); |
195 | if (count($parts)) { |
196 | $this->convertToMultiPart(); |
197 | $this->multipart = array_merge(array($textpart), $parts); |
198 | } |
199 | } |
200 | } |
201 | |
202 | private function splitMultipart() |
203 | { |
204 | $this->decodeContent(); |
205 | if (is_null($this->multipart)) { |
206 | $this->multipart = array(); |
207 | } |
208 | $boundary =& $this->boundary; |
209 | $parts = preg_split("/\n--" . preg_quote($boundary, '/') . "(--|\n)/", $this->body, -1, PREG_SPLIT_NO_EMPTY); |
210 | foreach ($parts as &$part) { |
211 | $newpart = new BananaMimePart($part); |
212 | if (!is_null($newpart->content_type)) { |
213 | $this->multipart[] = $newpart; |
214 | } |
215 | } |
216 | $this->body = null; |
217 | } |
218 | |
219 | public static function getMimeType($data, $is_filename = true) |
220 | { |
221 | if ($is_filename) { |
222 | $type = mime_content_type($arg); |
223 | } else { |
224 | $arg = escapeshellarg($data); |
225 | $type = preg_replace('/;.*/', '', trim(shell_exec("echo $arg | file -bi -"))); |
226 | } |
227 | return empty($type) ? 'application/octet-stream' : $type; |
228 | } |
229 | |
230 | private function findUUEncoded() |
231 | { |
232 | $this->decodeContent(); |
233 | $parts = array(); |
234 | if (preg_match_all("/\n(begin \d+ ([^\r\n]+)\r?(?:\n(?!end)[^\n]*)*\nend)/", |
235 | $this->body, $matches, PREG_SET_ORDER)) { |
236 | foreach ($matches as &$match) { |
237 | $data = convert_uudecode($match[1]); |
238 | $mime = BananaMimePart::getMimeType($data, false); |
239 | if ($mime != 'application/x-empty') { |
240 | $this->body = trim(str_replace($match[0], '', $this->body)); |
241 | $newpart = new BananaMimePart; |
242 | $newpart->makeDataPart($data, $mime, '8bit', $match[2], 'attachment'); |
243 | $parts[] = $newpart; |
244 | } |
245 | } |
246 | } |
247 | return $parts; |
248 | } |
249 | |
250 | static private function _decodeHeader($charset, $c, $str) |
251 | { |
252 | $s = ($c == 'Q' || $c == 'q') ? quoted_printable_decode($str) : base64_decode($str); |
253 | $s = @iconv($charset, 'UTF-8', $s); |
254 | return str_replace('_', ' ', $s); |
255 | } |
256 | |
257 | static public function decodeHeader(&$val, $key) |
258 | { |
259 | if (preg_match('/[\x80-\xff]/', $val)) { |
260 | if (!is_utf8($val)) { |
261 | $val = utf8_encode($val); |
262 | } |
263 | } else { |
264 | $val = preg_replace('/(=\?.*?\?[bq]\?.*?\?=) (=\?.*?\?[bq]\?.*?\?=)/i', '\1\2', $val); |
265 | $val = preg_replace('/=\?(.*?)\?([bq])\?(.*?)\?=/ie', 'BananaMimePart::_decodeHeader("\1", "\2", "\3")', $val); |
266 | } |
267 | } |
268 | |
269 | static public function &parseHeaders(array &$lines) |
270 | { |
271 | $headers = array(); |
272 | while (count($lines)) { |
273 | $line = array_shift($lines); |
274 | if (preg_match('/^[\t\r ]+/', $line) && isset($hdr)) { |
275 | $headers[$hdr] .= ' ' . trim($line); |
276 | } elseif (!empty($line)) { |
277 | if (preg_match("/:[ \t\r]*/", $line)) { |
278 | list($hdr, $val) = split(":[ \t\r]*", $line, 2); |
279 | $hdr = strtolower($hdr); |
7972645b |
280 | if (in_array($hdr, Banana::$msgparse_headers)) { |
ab02e8a9 |
281 | $headers[$hdr] = $val; |
282 | } else { |
283 | unset($hdr); |
284 | } |
285 | } |
286 | } else { |
287 | break; |
288 | } |
289 | } |
290 | array_walk($headers, array('BananaMimePart', 'decodeHeader')); |
291 | return $headers; |
292 | } |
293 | |
294 | static public function encodeHeader($value, $trim = 0) |
295 | { |
296 | if ($trim) { |
297 | if (strlen($value) > $trim) { |
298 | $value = substr($value, 0, $trim); |
299 | } |
300 | } |
301 | if (preg_match('/[\x80-\xff]/', $value)) { |
302 | return '=?UTF-8?B?' . base64_encode($value) . '?='; |
303 | } |
304 | return $value; |
305 | } |
306 | |
307 | private function decodeContent() |
308 | { |
309 | $encodings = Array('quoted-printable' => 'quoted_printable_decode', |
310 | 'base64' => 'base64_decode', |
311 | 'x-uuencode' => 'convert_uudecode'); |
312 | foreach ($encodings as $encoding => $callback) { |
313 | if ($this->encoding == $encoding) { |
314 | $this->body = $callback($this->body); |
315 | $this->encoding = '8bit'; |
316 | break; |
317 | } |
318 | } |
319 | if (!$this->isType('text')) { |
320 | return; |
321 | } |
322 | |
323 | if (!is_null($this->charset)) { |
324 | $body = iconv($this->charset, 'UTF-8//IGNORE', $this->body); |
325 | if (empty($body)) { |
326 | return; |
327 | } |
328 | $this->body = $body; |
329 | } else { |
330 | $this->body = utf8_encode($this->body); |
331 | } |
332 | $this->charset = 'utf-8'; |
333 | } |
334 | |
335 | public function send($force_inline = false) |
336 | { |
337 | $this->decodeContent(); |
338 | if ($force_inline) { |
339 | $dispostion = $this->disposition; |
340 | $this->disposition = 'inline'; |
341 | } |
342 | $headers = $this->getHeaders(); |
343 | foreach ($headers as $key => $value) { |
344 | header("$key: $value"); |
345 | } |
346 | if ($force_inline) { |
347 | $this->disposition = $disposition; |
348 | } |
349 | echo $this->body; |
350 | exit; |
351 | } |
352 | |
353 | private function setBoundary() |
354 | { |
355 | if ($this->isType('multipart') && is_null($this->boundary)) { |
356 | $this->boundary = '--banana-bound-' . time() . rand(0, 255) . '-'; |
357 | } |
358 | } |
359 | |
360 | public function getHeaders() |
361 | { |
362 | $headers = array(); |
363 | $this->setBoundary(); |
364 | $headers['Content-Type'] = $this->content_type . ";" |
365 | . ($this->filename ? " name=\"{$this->filename}\";" : '') |
366 | . ($this->charset ? " charset=\"{$this->charset}\";" : '') |
367 | . ($this->boundary ? " boundary=\"{$this->boundary}\";" : ""); |
368 | if ($this->encoding) { |
369 | $headers['Content-Transfer-Encoding'] = $this->encoding; |
370 | } |
371 | if ($this->disposition) { |
372 | $headers['Content-Disposition'] = $this->disposition |
373 | . ($this->filename ? "; filename=\"{$this->filename}\"" : ''); |
374 | } |
375 | return array_map(array($this, 'encodeHeader'), $headers); |
376 | } |
377 | |
378 | public function hasBody() |
379 | { |
380 | if (is_null($this->content) && !$this->isType('multipart')) { |
381 | return false; |
382 | } |
383 | return true; |
384 | } |
385 | |
386 | public function get($with_headers = false) |
387 | { |
388 | $content = ""; |
389 | if ($with_headers) { |
390 | foreach ($this->getHeaders() as $key => $value) { |
7b98ea62 |
391 | $line = "$key: $value"; |
392 | $line = explode("\n", wordwrap($line, Banana::$msgshow_wrap)); |
393 | for ($i = 1 ; $i < count($line) ; $i++) { |
394 | $line[$i] = "\t" . $line[$i]; |
395 | } |
396 | $content .= implode("\n", $line) . "\n"; |
397 | } |
ab02e8a9 |
398 | $content .= "\n"; |
399 | } |
400 | if ($this->isType('multipart')) { |
401 | $this->setBoundary(); |
402 | foreach ($this->multipart as &$part) { |
403 | $content .= "\n--{$this->boundary}\n" . $part->get(true); |
404 | } |
405 | $content .= "\n--{$this->boundary}--"; |
406 | } else { |
7b98ea62 |
407 | $content .= banana_wordwrap($this->body); |
ab02e8a9 |
408 | } |
409 | return $content; |
410 | } |
411 | |
412 | public function getText() |
413 | { |
ab02e8a9 |
414 | $this->decodeContent(); |
415 | return $this->body; |
416 | } |
417 | |
bf791d69 |
418 | public function toHtml() |
419 | { |
420 | list($type, $subtype) = $this->getType(); |
421 | if ($type == 'image') { |
422 | $part = $this->id ? $this->id : $this->filename; |
97d8d911 |
423 | return '<img class="multipart" src="' |
bf791d69 |
424 | . banana_htmlentities(Banana::$page->makeUrl(array('group' => Banana::$group, |
425 | 'artid' => Banana::$artid, |
426 | 'part' => $part))) |
427 | . '" alt="' . banana_htmlentities($this->filename) . '" />'; |
428 | } elseif (!in_array($type, Banana::$msgshow_mimeparts) |
429 | && !in_array($this->content_type, Banana::$msgshow_mimeparts)) { |
430 | $part = $this->id ? $this->id : $this->filename; |
431 | if (!$part) { |
432 | $part = $this->content_type; |
433 | } |
434 | return '[' . Banana::$page->makeImgLink(array('group' => Banana::$group, |
435 | 'artid' => Banana::$artid, |
436 | 'part' => $part, |
437 | 'text' => $this->filename ? $this->filename : $this->content_type, |
438 | 'img' => 'save')) . ']'; |
439 | } else { |
440 | if ($type == 'multipart' && ($subtype == 'mixed' || $subtype == 'report')) { |
441 | $text = ''; |
442 | foreach ($this->multipart as &$part) { |
443 | $text .= $part->toHtml(); |
444 | } |
445 | return $text; |
446 | } |
447 | switch ($subtype) { |
448 | case 'html': return banana_formatHtml($this); |
449 | case 'enriched': case 'richtext': return banana_formatRichText($this); |
450 | default: |
451 | if ($type == 'message') { |
452 | return '<hr />' . banana_formatPlainText($this); |
453 | } |
454 | return banana_formatPlainText($this); |
455 | } |
456 | } |
457 | return null; |
458 | } |
459 | |
460 | public function quote() |
461 | { |
462 | list($type, $subtype) = $this->getType(); |
463 | if (in_array($type, Banana::$msgedit_mimeparts) || in_array($this->content_type, Banana::$msgedit_mimeparts)) { |
464 | if ($type == 'multipart' && ($subtype == 'mixed' || $subtype == 'report')) { |
465 | $text = ''; |
466 | foreach ($this->multipart as &$part) { |
467 | $qt = $part->quote(); |
468 | $qt = rtrim($qt); |
469 | if (!empty($text)) { |
470 | $text .= "\n" . banana_quote("", 1) . "\n"; |
471 | } |
472 | $text .= $qt; |
473 | } |
474 | return $text; |
475 | } |
476 | switch ($subtype) { |
477 | case 'html': return banana_quoteHtml($this); |
478 | case 'enriched': case 'richtext': return banana_quoteRichText($this); |
479 | default: return banana_quotePlainText($this); |
480 | } |
481 | } |
482 | } |
483 | |
ab02e8a9 |
484 | protected function getType() |
485 | { |
486 | return explode('/', $this->content_type); |
487 | } |
488 | |
489 | protected function isType($type, $subtype = null) |
490 | { |
491 | list($mytype, $mysub) = $this->getType(); |
492 | return ($mytype == $type) && (is_null($subtype) || $mysub == $subtype); |
493 | } |
494 | |
495 | public function isFlowed() |
496 | { |
497 | return $this->format == 'flowed'; |
498 | } |
499 | |
500 | public function getFilename() |
501 | { |
502 | return $this->filename; |
503 | } |
504 | |
505 | protected function getParts($type, $subtype = null) |
506 | { |
507 | $parts = array(); |
508 | if ($this->isType($type, $subtype)) { |
509 | return array($this); |
510 | } elseif ($this->isType('multipart')) { |
511 | foreach ($this->multipart as &$part) { |
512 | $parts = array_merge($parts, $part->getParts($type, $subtype)); |
513 | } |
514 | } |
515 | return $parts; |
516 | } |
517 | |
ab02e8a9 |
518 | public function getFile($filename) |
519 | { |
520 | if ($this->filename == $filename) { |
521 | return $this; |
522 | } elseif ($this->isType('multipart')) { |
523 | foreach ($this->multipart as &$part) { |
524 | $file = $part->getFile($filename); |
525 | if (!is_null($file)) { |
526 | return $file; |
527 | } |
528 | } |
529 | } |
530 | return null; |
531 | } |
532 | |
533 | public function getAttachments() |
534 | { |
535 | if (!is_null($this->filename)) { |
536 | return array($this); |
537 | } elseif ($this->isType('multipart')) { |
538 | $parts = array(); |
539 | foreach ($this->multipart as &$part) { |
540 | $parts = array_merge($parts, $part->getAttachments()); |
541 | } |
542 | return $parts; |
543 | } |
544 | return array(); |
545 | } |
546 | |
1273e8f4 |
547 | public function getAlternatives() |
548 | { |
549 | $types =& Banana::$msgshow_mimeparts; |
550 | $names =& Banana::$mimeparts; |
551 | $source = null; |
552 | if (in_array('source', $types)) { |
553 | $source = @$names['source'] ? $names['source'] : 'source'; |
554 | } |
555 | if (!$this->isType('multipart', 'alternative') && !$this->isType('multipart', 'related')) { |
556 | if ($source) { |
557 | $parts = array($this); |
558 | } else { |
559 | return array(); |
560 | } |
561 | } else { |
562 | $parts =& $this->multipart; |
563 | } |
564 | $alt = array(); |
565 | foreach ($parts as &$part) { |
566 | list($type, $subtype) = $part->getType(); |
567 | $ct = $type . '/' . $subtype; |
568 | if (in_array($ct, $types) || in_array($type, $types)) { |
569 | if (isset($names[$ct])) { |
570 | $alt[$ct] = $names[$ct]; |
571 | } elseif (isset($names[$type])) { |
572 | $alt[$ct] = $names[$type]; |
573 | } else { |
574 | $alt[$ct] = $ct; |
575 | } |
576 | } |
577 | } |
578 | if ($source) { |
579 | $alt['source'] = $source; |
580 | } |
581 | return $alt; |
582 | } |
583 | |
ab02e8a9 |
584 | public function getPartById($id) |
585 | { |
586 | if ($this->id == $id) { |
587 | return $this; |
588 | } elseif ($this->isType('multipart')) { |
589 | foreach ($this->multipart as &$part) { |
590 | $res = $part->getPartById($id); |
591 | if (!is_null($res)) { |
592 | return $res; |
593 | } |
594 | } |
595 | } |
596 | return null; |
597 | } |
598 | } |
599 | |
d8d416c4 |
600 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
ab02e8a9 |
601 | ?> |