9270438d7255919b8fa859bbe955708c97c805f9
2 /********************************************************************************
3 * banana/mimepart.inc.php : class for MIME parts
4 * ------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
12 public $headers = null
; /* Should be protected */
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
;
24 private $multipart = null
;
26 protected function __construct($data = null
)
28 if ($data instanceof BananaMimePart
) {
29 foreach ($this as $key=>$value) {
30 $this->$key = $data->$key;
32 } elseif (!is_null($data)) {
33 $this->fromRaw($data);
37 protected function makeTextPart($body, $content_type, $encoding, $charset = null
, $format = 'fixed')
40 $this->charset
= $charset;
41 $this->encoding
= $encoding;
42 $this->content_type
= $content_type;
43 $this->format
= strtolower($format);
47 protected function makeDataPart($body, $content_type, $encoding, $filename, $disposition, $id = null
)
50 $this->content_type
= $content_type;
51 $this->encoding
= $encoding;
52 $this->filename
= $filename;
53 $this->disposition
= $disposition;
55 if (is_null($content_type) ||
$content_type == 'application/octet-stream') {
56 $this->decodeContent();
57 $this->content_type
= BananaMimePart
::getMimeType($this->body
, false
);
61 protected function makeFilePart(array $file, $content_type = null
, $disposition = 'attachment')
63 $body = file_get_contents($file['tmp_name']);
64 if ($body === false ||
strlen($body) != $file['size']) {
67 if (is_null($content_type) ||
$content_type == 'application/octet-stream') {
68 $content_type = BananaMimePart
::getMimeType($file['tmp_name']);
70 if (substr($content_type, 0, 5) == 'text/') {
74 $body = chunk_split(base64_encode($body));
76 $this->filename
= $file['name'];
77 $this->content_type
= $content_type;
78 $this->disposition
= $disposition;
80 $this->encoding
= $encoding;
84 protected function makeMultiPart($body, $content_type, $encoding, $boundary)
87 $this->content_type
= $content_type;
88 $this->encoding
= $encoding;
89 $this->boundary
= $boundary;
93 protected function convertToMultiPart()
95 if (!$this->isType('multipart', 'mixed')) {
96 $newpart = new BananaMimePart($this);
97 $this->content_type
= 'multipart/mixed';
98 $this->encoding
= '8bit';
99 $this->multipart
= array($newpart);
100 $this->headers
= null
;
101 $this->charset
= null
;
102 $this->disposition
= null
;
103 $this->filename
= null
;
104 $this->boundary
= null
;
106 $this->format
= null
;
111 public function addAttachment(array $file, $content_type = null
, $disposition = 'attachment')
113 if (!is_uploaded_file($file['tmp_name'])) {
116 $newpart = new BananaMimePart
;
117 if ($newpart->makeFilePart($file, $content_type, $disposition)) {
118 $this->convertToMultiPart();
119 $this->multipart
[] = $newpart;
125 protected function getHeader($title, $filter = null
)
127 if (!isset($this->headers
[$title])) {
130 $header =& $this->headers
[$title];
131 if (is_null($filter)) {
132 return trim($header);
133 } elseif (preg_match($filter, $header, $matches)) {
134 return trim($matches[1]);
139 protected function fromRaw($data)
141 if (is_array($data)) {
142 if (array_key_exists('From', $data)) {
143 $this->headers
= array_map(array($this, 'decodeHeader'), array_change_key_case($data));
149 $lines = explode("\n", $data);
151 $headers = BananaMimePart
::parseHeaders($lines);
152 $this->headers
=& $headers;
153 if (empty($headers) ||
empty($lines)) {
156 $content = join("\n", $lines);
157 $test = trim($content);
162 $content_type = strtolower($this->getHeader('content-type', '/^\s*([^ ;]+?)(;|$)/'));
163 if (empty($content_type)) {
166 $content_type = 'text/plain';
167 $format = strtolower($this->getHeader('x-rfc2646', '/format="?([^ w@"]+?)"?\s*(;|$)/i'));
169 $encoding = strtolower($this->getHeader('content-transfer-encoding'));
170 $disposition = $this->getHeader('content-disposition', '/(inline|attachment)/i');
171 $boundary = $this->getHeader('content-type', '/boundary="?([^ "]+?)"?\s*(;|$)/i');
172 $charset = strtolower($this->getHeader('content-type', '/charset="?([^ "]+?)"?\s*(;|$)/i'));
173 $filename = $this->getHeader('content-disposition', '/filename="?([^ "]+?)"?\s*(;|$)/i');
174 $format = strtolower($this->getHeader('content-type', '/format="?([^ "]+?)"?\s*(;|$)/i'));
175 $id = $this->getHeader('content-id', '/<(.*?)>/');
176 if (empty($filename)) {
177 $filename = $this->getHeader('content-type', '/name="?([^"]+)"?/');
180 list($type, $subtype) = explode('/', $content_type);
182 case 'text': case 'message':
183 $this->makeTextPart($content, $content_type, $encoding, $charset, $format);
186 $this->makeMultiPart($content, $content_type, $encoding, $boundary);
189 $this->makeDataPart($content, $content_type, $encoding, $filename, $disposition, $id);
193 private function parse()
195 if ($this->isType('multipart')) {
196 $this->splitMultipart();
198 $parts = $this->findUUEncoded();
200 $this->convertToMultiPart();
201 $this->multipart
= array_merge(array($textpart), $parts);
206 private function splitMultipart()
208 $this->decodeContent();
209 if (is_null($this->multipart
)) {
210 $this->multipart
= array();
212 $boundary =& $this->boundary
;
213 $parts = preg_split("/\n--" . preg_quote($boundary, '/') . "(--|\n)/", $this->body
, -1, PREG_SPLIT_NO_EMPTY
);
214 foreach ($parts as &$part) {
215 $newpart = new BananaMimePart($part);
216 if (!is_null($newpart->content_type
)) {
217 $this->multipart
[] = $newpart;
223 public static function getMimeType($data, $is_filename = true
)
226 $type = mime_content_type($data);
228 $arg = escapeshellarg($data);
229 $type = preg_replace('/;.*/', '', trim(shell_exec("echo $arg | file -bi -")));
231 return empty($type) ?
'application/octet-stream' : $type;
234 private function findUUEncoded()
236 $this->decodeContent();
238 if (preg_match_all("/\n(begin \d+ ([^\r\n]+)\r?(?:\n(?!end)[^\n]*)*\nend)/",
239 $this->body
, $matches, PREG_SET_ORDER
)) {
240 foreach ($matches as &$match) {
241 $data = convert_uudecode($match[1]);
242 $mime = BananaMimePart
::getMimeType($data, false
);
243 if ($mime != 'application/x-empty') {
244 $this->body
= trim(str_replace($match[0], '', $this->body
));
245 $newpart = new BananaMimePart
;
246 $newpart->makeDataPart($data, $mime, '8bit', $match[2], 'attachment');
254 static private function _decodeHeader($charset, $c, $str)
256 $s = ($c == 'Q' ||
$c == 'q') ?
quoted_printable_decode($str) : base64_decode($str);
257 $s = @iconv
($charset, 'UTF-8', $s);
258 return str_replace('_', ' ', $s);
261 static public function decodeHeader(&$val, $key)
263 if (preg_match('/[\x80-\xff]/', $val)) {
264 if (!is_utf8($val)) {
265 $val = utf8_encode($val);
267 } elseif (strpos($val, '=') !== false
) {
268 $val = preg_replace('/(=\?.*?\?[bq]\?.*?\?=) (=\?.*?\?[bq]\?.*?\?=)/i', '\1\2', $val);
269 $val = preg_replace('/=\?(.*?)\?([bq])\?(.*?)\?=/ie', 'BananaMimePart::_decodeHeader("\1", "\2", "\3")', $val);
273 static public function &parseHeaders(array &$lines)
277 $line = array_shift($lines);
278 if (isset($hdr) && $line && ctype_space($line{0})) {
279 $headers[$hdr] .= ' ' . trim($line);
280 } elseif (!empty($line)) {
281 if (strpos($line, ':') !== false
) {
282 list($hdr, $val) = explode(":", $line, 2);
283 $hdr = strtolower($hdr);
284 if (in_array($hdr, Banana
::$msgparse_headers)) {
285 $headers[$hdr] = ltrim($val);
294 array_walk($headers, array('BananaMimePart', 'decodeHeader'));
298 static public function encodeHeader($value, $trim = 0)
301 if (strlen($value) > $trim) {
302 $value = substr($value, 0, $trim);
305 $value = preg_replace('/([\x80-\xff]+)/e', '"=?UTF-8?B?" . base64_encode("\1") . "?="', $value);
309 private function decodeContent()
311 $encodings = Array('quoted-printable' => 'quoted_printable_decode',
312 'base64' => 'base64_decode',
313 'x-uuencode' => 'convert_uudecode');
314 foreach ($encodings as $encoding => $callback) {
315 if ($this->encoding
== $encoding) {
316 $this->body
= $callback($this->body
);
317 $this->encoding
= '8bit';
321 if (!$this->isType('text')) {
325 if (!is_null($this->charset
)) {
326 $body = iconv($this->charset
, 'UTF-8//IGNORE', $this->body
);
332 $this->body
= utf8_encode($this->body
);
334 $this->charset
= 'utf-8';
337 public function send($force_inline = false
)
339 $this->decodeContent();
341 $dispostion = $this->disposition
;
342 $this->disposition
= 'inline';
344 $headers = $this->getHeaders();
345 foreach ($headers as $key => $value) {
346 header("$key: $value");
349 $this->disposition
= $disposition;
355 private function setBoundary()
357 if ($this->isType('multipart') && is_null($this->boundary
)) {
358 $this->boundary
= '--banana-bound-' . time() . rand(0, 255) . '-';
362 public function getHeaders()
365 $this->setBoundary();
366 $headers['Content-Type'] = $this->content_type
. ";"
367 . ($this->filename ?
" name=\"{$this->filename}\";" : '')
368 . ($this->charset ?
" charset=\"{$this->charset}\";" : '')
369 . ($this->boundary ?
" boundary=\"{$this->boundary}\";" : "")
370 . ($this->format ?
" format={$this->format}" : "");
371 if ($this->encoding
) {
372 $headers['Content-Transfer-Encoding'] = $this->encoding
;
374 if ($this->disposition
) {
375 $headers['Content-Disposition'] = $this->disposition
376 . ($this->filename ?
"; filename=\"{$this->filename}\"" : '');
378 return array_map(array($this, 'encodeHeader'), $headers);
381 public function hasBody()
383 if (is_null($this->content
) && !$this->isType('multipart')) {
389 public function get($with_headers = false
)
393 foreach ($this->getHeaders() as $key => $value) {
394 $line = "$key: $value";
395 $line = explode("\n", wordwrap($line, Banana
::$msgshow_wrap));
396 for ($i = 1 ; $i < count($line) ; $i++
) {
397 $line[$i] = "\t" . $line[$i];
399 $content .= implode("\n", $line) . "\n";
403 if ($this->isType('multipart')) {
404 $this->setBoundary();
405 foreach ($this->multipart
as &$part) {
406 $content .= "\n--{$this->boundary}\n" . $part->get(true
);
408 $content .= "\n--{$this->boundary}--";
409 } elseif ($this->isType('text', 'plain')) {
410 $content .= banana_flow($this->body
);
412 $content .= banana_wordwrap($this->body
);
417 public function getText()
419 $this->decodeContent();
423 public function toHtml()
425 @list
($type, $subtype) = $this->getType();
426 if ($type == 'image') {
427 $part = $this->id ?
$this->id
: $this->filename
;
428 return '<img class="multipart" src="'
429 . banana_htmlentities(Banana
::$page->makeUrl(array('group' => Banana
::$group,
430 'artid' => Banana
::$artid,
432 . '" alt="' . banana_htmlentities($this->filename
) . '" />';
433 } elseif ((!in_array($type, Banana
::$msgshow_mimeparts)
434 && !in_array($this->content_type
, Banana
::$msgshow_mimeparts))
435 ||
$this->disposition
== 'attachment') {
436 $part = $this->id ?
$this->id
: $this->filename
;
438 $part = $this->content_type
;
440 return '[' . Banana
::$page->makeImgLink(array('group' => Banana
::$group,
441 'artid' => Banana
::$artid,
443 'text' => $this->filename ?
$this->filename
: $this->content_type
,
444 'img' => 'save')) . ']';
445 } elseif ($type == 'multipart' && ($subtype == 'mixed' ||
$subtype == 'report')) {
447 foreach ($this->multipart
as &$part) {
448 $text .= $part->toHtml();
453 case 'html': return banana_formatHtml($this);
454 case 'enriched': case 'richtext': return banana_formatRichText($this);
456 if ($type == 'message') { // we have a raw source of data (no specific pre-formatting)
457 return '<hr />' . utf8_encode(banana_formatPlainText($this));
459 return banana_formatPlainText($this);
465 public function quote()
467 list($type, $subtype) = $this->getType();
468 if (in_array($type, Banana
::$msgedit_mimeparts) ||
in_array($this->content_type
, Banana
::$msgedit_mimeparts)) {
469 if ($type == 'multipart' && ($subtype == 'mixed' ||
$subtype == 'report')) {
471 foreach ($this->multipart
as &$part) {
472 $qt = $part->quote();
475 $text .= "\n" . banana_quote("", 1) . "\n";
482 case 'html': return banana_quoteHtml($this);
483 case 'enriched': case 'richtext': return banana_quoteRichText($this);
484 default: return banana_quotePlainText($this);
489 protected function getType()
491 return explode('/', $this->content_type
);
494 protected function isType($type, $subtype = null
)
496 list($mytype, $mysub) = $this->getType();
497 return ($mytype == $type) && (is_null($subtype) ||
$mysub == $subtype);
500 public function isFlowed()
502 return $this->format
== 'flowed';
505 public function getFilename()
507 return $this->filename
;
510 protected function getParts($type, $subtype = null
)
513 if ($this->isType($type, $subtype)) {
515 } elseif ($this->isType('multipart')) {
516 foreach ($this->multipart
as &$part) {
517 $parts = array_merge($parts, $part->getParts($type, $subtype));
523 public function getFile($filename)
525 if ($this->filename
== $filename) {
527 } elseif ($this->isType('multipart')) {
528 foreach ($this->multipart
as &$part) {
529 $file = $part->getFile($filename);
530 if (!is_null($file)) {
538 public function getAttachments()
540 if (!is_null($this->filename
)) {
542 } elseif ($this->isType('multipart')) {
544 foreach ($this->multipart
as &$part) {
545 $parts = array_merge($parts, $part->getAttachments());
552 public function getAlternatives()
554 $types =& Banana
::$msgshow_mimeparts;
555 $names =& Banana
::$mimeparts;
557 if (in_array('source', $types)) {
558 $source = @$names['source'] ?
$names['source'] : 'source';
560 if (!$this->isType('multipart', 'alternative') && !$this->isType('multipart', 'related')) {
562 $parts = array($this);
567 $parts =& $this->multipart
;
570 foreach ($parts as &$part) {
571 list($type, $subtype) = $part->getType();
572 $ct = $type . '/' . $subtype;
573 if (in_array($ct, $types) ||
in_array($type, $types)) {
574 if (isset($names[$ct])) {
575 $alt[$ct] = $names[$ct];
576 } elseif (isset($names[$type])) {
577 $alt[$ct] = $names[$type];
584 $alt['source'] = $source;
589 public function getPartById($id)
591 if ($this->id
== $id) {
593 } elseif ($this->isType('multipart')) {
594 foreach ($this->multipart
as &$part) {
595 $res = $part->getPartById($id);
596 if (!is_null($res)) {
605 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: