diff options
Diffstat (limited to 'program')
-rw-r--r-- | program/include/rcube_imap.php | 37 | ||||
-rw-r--r-- | program/include/rcube_message.php | 12 | ||||
-rw-r--r-- | program/lib/tnef_decoder.inc | 352 | ||||
-rw-r--r-- | program/steps/mail/get.inc | 17 |
4 files changed, 413 insertions, 5 deletions
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php index d4bcc7462..0e2dd6a07 100644 --- a/program/include/rcube_imap.php +++ b/program/include/rcube_imap.php @@ -26,6 +26,7 @@ */ require_once('lib/imap.inc'); require_once('lib/mime.inc'); +require_once('lib/tnef_decoder.inc'); /** @@ -1602,7 +1603,7 @@ class rcube_imap $from_mbox = $from_mbox ? $this->_mod_mailbox($from_mbox) : $this->mailbox; // make sure mailbox exists - if (!in_array($to_mbox, $this->_list_mailboxes())) + if ($to_mbox != 'INBOX' && !in_array($to_mbox, $this->_list_mailboxes())) { if (in_array($to_mbox_in, $this->default_folders)) $this->create_mailbox($to_mbox_in, TRUE); @@ -2492,6 +2493,40 @@ class rcube_imap return $out; } + + + /** + * Decode a Microsoft Outlook TNEF part (winmail.dat) + * + * @param object rcube_message_part Message part to decode + * @param string UID of the message + * @return array List of rcube_message_parts extracted from windmail.dat + */ + function tnef_decode(&$part, $uid) + { + if (!isset($part->body)) + $part->body = $this->get_message_part($uid, $part->mime_id, $part); + + $pid = 0; + $tnef_parts = array(); + $tnef_arr = tnef_decode($part->body); + foreach ($tnef_arr as $winatt) { + $tpart = new rcube_message_part; + $tpart->filename = $winatt["name"]; + $tpart->encoding = 'stream'; + $tpart->ctype_primary = $winatt["type0"]; + $tpart->ctype_secondary = $winatt["type1"]; + $tpart->mimetype = strtolower($winatt["type0"] . "/" . $winatt["type1"]); + $tpart->mime_id = "winmail." . $part->mime_id . ".$pid"; + $tpart->size = $winatt["size"]; + $tpart->body = $winatt['stream']; + + $tnef_parts[] = $tpart; + $pid++; + } + + return $tnef_parts; + } /** diff --git a/program/include/rcube_message.php b/program/include/rcube_message.php index fa2aebc5c..513cb4c07 100644 --- a/program/include/rcube_message.php +++ b/program/include/rcube_message.php @@ -366,6 +366,14 @@ class rcube_message // ignore "virtual" protocol parts else if ($primary_type == 'protocol') continue; + + // part is Microsoft outlook TNEF (winmail.dat) + else if ($primary_type == 'application' && $secondary_type == 'ms-tnef') { + foreach ((array)$this->imap->tnef_decode($mail_part, $structure->headers['uid']) as $tnef_part) { + $this->mime_parts[$tnef_part->mime_id] = $tnef_part; + $this->attachments[] = $tnef_part; + } + } // part is file/attachment else if ($mail_part->disposition == 'attachment' || $mail_part->disposition == 'inline' || @@ -381,10 +389,10 @@ class rcube_message if ($mail_part->headers['content-location']) $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location']; - if ($mail_part->content_id || $mail_part->content_location) { + if ($mail_part->content_id || $mail_part->content_location) { $this->inline_parts[] = $mail_part; } - } + } // is regular attachment else { if (!$mail_part->filename) diff --git a/program/lib/tnef_decoder.inc b/program/lib/tnef_decoder.inc new file mode 100644 index 000000000..f9b7c3684 --- /dev/null +++ b/program/lib/tnef_decoder.inc @@ -0,0 +1,352 @@ +<?php +/* + * tnef_decoder.php + * Graham Norbury <gnorbury@bondcar.com> + * (c) 2002 (GNU GPL - see ../../COPYING) + * + * Functions for decoding TNEF attachments in native PHP + * + * Adapted from original designs by: + * Thomas Boll <tb@boll.ch> [tnef.c] + * Mark Simpson <damned@world.std.com> [tnef-1.1.1] + * + */ + +define("TNEF_SIGNATURE", 0x223e9f78); +define("TNEF_LVL_MESSAGE", 0x01); +define("TNEF_LVL_ATTACHMENT", 0x02); + +define("TNEF_STRING", 0x00010000); +define("TNEF_TEXT", 0x00020000); +define("TNEF_BYTE", 0x00060000); +define("TNEF_WORD", 0x00070000); +define("TNEF_DWORD", 0x00080000); + +define("TNEF_ASUBJECT", TNEF_DWORD | 0x8004); +define("TNEF_AMCLASS", TNEF_WORD | 0x8008); +define("TNEF_BODYTEXT", TNEF_TEXT | 0x800c); +define("TNEF_ATTACHDATA", TNEF_BYTE | 0x800f); +define("TNEF_AFILENAME", TNEF_STRING | 0x8010); +define("TNEF_ARENDDATA", TNEF_BYTE | 0x9002); +define("TNEF_AMAPIATTRS", TNEF_BYTE | 0x9005); +define("TNEF_AVERSION", TNEF_DWORD | 0x9006); + +define("TNEF_MAPI_NULL", 0x0001); +define("TNEF_MAPI_SHORT", 0x0002); +define("TNEF_MAPI_INT", 0x0003); +define("TNEF_MAPI_FLOAT", 0x0004); +define("TNEF_MAPI_DOUBLE", 0x0005); +define("TNEF_MAPI_CURRENCY", 0x0006); +define("TNEF_MAPI_APPTIME", 0x0007); +define("TNEF_MAPI_ERROR", 0x000a); +define("TNEF_MAPI_BOOLEAN", 0x000b); +define("TNEF_MAPI_OBJECT", 0x000d); +define("TNEF_MAPI_INT8BYTE", 0x0014); +define("TNEF_MAPI_STRING", 0x001e); +define("TNEF_MAPI_UNICODE_STRING", 0x001f); +define("TNEF_MAPI_SYSTIME", 0x0040); +define("TNEF_MAPI_CLSID", 0x0048); +define("TNEF_MAPI_BINARY", 0x0102); + +define("TNEF_MAPI_ATTACH_MIME_TAG", 0x370E); +define("TNEF_MAPI_ATTACH_LONG_FILENAME", 0x3707); +define("TNEF_MAPI_ATTACH_DATA", 0x3701); + +function tnef_getx($size, &$buf) +{ + $value = null; + if (strlen($buf) >= $size) + { + $value = substr($buf, 0, $size); + $buf = substr_replace($buf, '', 0, $size); + } + return $value; +} + +function tnef_geti8(&$buf) +{ + $value = null; + if (strlen($buf) >= 1) + { + $value = ord($buf{0}); + $buf = substr_replace($buf, '', 0, 1); + } + return $value; +} + +function tnef_geti16(&$buf) +{ + $value = null; + if (strlen($buf) >= 2) + { + $value = ord($buf{0}) + + (ord($buf{1}) << 8); + $buf = substr_replace($buf, '', 0, 2); + } + return $value; +} + +function tnef_geti32(&$buf) +{ + $value = null; + if (strlen($buf) >= 4) + { + $value = ord($buf{0}) + + (ord($buf{1}) << 8) + + (ord($buf{2}) << 16) + + (ord($buf{3}) << 24); + $buf = substr_replace($buf, '', 0, 4); + } + return $value; +} + +function tnef_decode_attribute($attribute, &$buf) +{ + global $debug, $download; + + $length = tnef_geti32($buf); + $value = tnef_getx($length, $buf); //data + tnef_geti16($buf); //checksum + + if ($debug) + { + printf("ATTRIBUTE[%08x] %d bytes\n", $attribute, $length); + } + + switch($attribute) + { + case TNEF_BODYTEXT: + if (!$download) + { + printf("<b>Embedded message:</b><pre>%s</pre>",$value); + } + break; + + default: + } +} + +function extract_mapi_attrs($buf, &$attachment_data) +{ + global $debug; + + tnef_geti32($buf); // number of attributes + while(strlen($buf) > 0) + { + $value = null; + $length = 0; + $attr_type = tnef_geti16($buf); + $attr_name = tnef_geti16($buf); + if ($debug) + { + printf("mapi attribute: %04x:%04x\n", $attr_type, $attr_name); + } + switch($attr_type) + { + case TNEF_MAPI_SHORT: + $value = tnef_geti16($buf); + break; + + case TNEF_MAPI_INT: + case TNEF_MAPI_BOOLEAN: + $value = tnef_geti32($buf); + break; + + case TNEF_MAPI_FLOAT: + $value = tnef_getx(4, $buf); + break; + + case TNEF_MAPI_DOUBLE: + case TNEF_MAPI_SYSTIME: + $value = tnef_getx(8, $buf); + break; + + case TNEF_MAPI_STRING: + case TNEF_MAPI_UNICODE_STRING: + case TNEF_MAPI_BINARY: + case TNEF_MAPI_OBJECT: + $num_vals = tnef_geti32($buf); + for ($i = 0; $i < $num_vals; $i++) // usually just 1 + { + $length = tnef_geti32($buf); + $buflen = $length + ((4 - ($length % 4)) % 4); // pad to next 4 byte boundary + $value = substr(tnef_getx($buflen, $buf), 0, $length); // read and truncate to length + } + break; + + default: + if ($debug) + { + echo("Unknown mapi attribute!\n"); + } + } + + // store any interesting attributes + switch($attr_name) + { + case TNEF_MAPI_ATTACH_LONG_FILENAME: // used in preference to AFILENAME value + $attachment_data[0]['name'] = ereg_replace('.*[\/](.*)$', '\1', $value); // strip path + break; + + case TNEF_MAPI_ATTACH_MIME_TAG: // Is this ever set, and what is format? + $attachment_data[0]['type0'] = ereg_replace('^(.*)/.*', '\1', $value); + $attachment_data[0]['type1'] = ereg_replace('.*/(.*)$', '\1', $value); + break; + + case TNEF_MAPI_ATTACH_DATA: + tnef_getx(16, $value); // skip the next 16 bytes (unknown data) + array_shift($attachment_data); // eliminate the current (bogus) attachment + do_tnef_decode($value, $attachment_data); // recursively process the attached message + break; + + default: + } + } +} + +function tnef_decode_message(&$buf) +{ + global $debug; + + if ($debug) + { + echo("MESSAGE "); + } + + $attribute = tnef_geti32($buf); + tnef_decode_attribute($attribute, $buf); +} + +function tnef_decode_attachment(&$buf, &$attachment_data) +{ + global $debug; + + if ($debug) + { + echo("ATTACHMENT "); + } + + $attribute = tnef_geti32($buf); + switch($attribute) + { + case TNEF_ARENDDATA: // marks start of new attachment + $length = tnef_geti32($buf); + tnef_getx($length, $buf); + tnef_geti16($buf); //checksum + if ($debug) + { + printf("ARENDDATA[%08x]: %d bytes\n", $attribute, $length); + } + // add a new default data block to hold details of this attachment + // reverse order is easier to handle later! + array_unshift($attachment_data, array('type0' => 'application', + 'type1' => 'octet-stream', + 'name' => 'unknown', + 'stream' => '')); + break; + + case TNEF_AFILENAME: // filename + $length = tnef_geti32($buf); + $attachment_data[0]['name'] = ereg_replace('.*[\/](.*)$', + '\1', + tnef_getx($length, $buf)); // strip path + tnef_geti16($buf); //checksum + if ($debug) + { + printf("AFILENAME[%08x]: %s\n", $attribute, $attachment_data[0]['name']); + } + break; + + case TNEF_ATTACHDATA: // the attachment itself + $length = tnef_geti32($buf); + $attachment_data[0]['size'] = $length; + $attachment_data[0]['stream'] = tnef_getx($length, $buf); + tnef_geti16($buf); //checksum + if ($debug) + { + printf("ATTACHDATA[%08x]: %d bytes\n", $attribute, $length); + } + break; + + case TNEF_AMAPIATTRS: + $length = tnef_geti32($buf); + $value = tnef_getx($length, $buf); + tnef_geti16($buf); //checksum + if ($debug) + { + printf("AMAPIATTRS[%08x]: %d bytes\n", $attribute, $length); + } + extract_mapi_attrs($value, $attachment_data); + break; + + default: + tnef_decode_attribute($attribute, $buf); + } +} + +function do_tnef_decode(&$buf, &$attachment_data) +{ + global $debug; + + $tnef_signature = tnef_geti32($buf); + if ($tnef_signature == TNEF_SIGNATURE) + { + $tnef_key = tnef_geti16($buf); + if ($debug) + { + printf("Signature: 0x%08x\nKey: 0x%04x\n", $tnef_signature, $tnef_key); + } + + while (strlen($buf) > 0) + { + $lvl_type = tnef_geti8($buf); + switch($lvl_type) + { + case TNEF_LVL_MESSAGE: + tnef_decode_message($buf); + break; + + case TNEF_LVL_ATTACHMENT: + tnef_decode_attachment($buf, $attachment_data); + break; + + default: + if ($debug) + { + echo("Invalid file format!"); + } + break 2; + } + } + } + else + { + if ($debug) + { + echo("Invalid file format!"); + } + } +} + +function tnef_decode($buf) +{ + global $debug; + + $attachment_data = array(); + + if ($debug) + { + echo("<pre>"); + } + + do_tnef_decode($buf, $attachment_data); + + if ($debug) + { + echo("</pre>"); + } + return array_reverse($attachment_data); + +} + +?>
\ No newline at end of file diff --git a/program/steps/mail/get.inc b/program/steps/mail/get.inc index 8ed4a1ec1..d2863e8bc 100644 --- a/program/steps/mail/get.inc +++ b/program/steps/mail/get.inc @@ -48,6 +48,16 @@ if (!empty($_GET['_frame'])) { } else if ($pid = get_input_value('_part', RCUBE_INPUT_GET)) { + // TNEF encoded attachment part + if (preg_match('/^winmail\.([0-9.]+)\.([0-9]+)$/', $pid, $nt)) { + $pid = $nt[1]; $i = $nt[2]; + if ($part = $MESSAGE->mime_parts[$pid]) { + $tnef_arr = $IMAP->tnef_decode($part, $MESSAGE->uid); + if (is_a($tnef_arr[$i], 'rcube_message_part')) + $MESSAGE->mime_parts[$pid] = $tnef_arr[$i]; + } + } + if ($part = $MESSAGE->mime_parts[$pid]) { $ctype_primary = strtolower($part->ctype_primary); $ctype_secondary = strtolower($part->ctype_secondary); @@ -98,9 +108,12 @@ else if ($pid = get_input_value('_part', RCUBE_INPUT_GET)) { $disposition = !empty($_GET['_download']) ? 'attachment' : 'inline'; header("Content-Disposition: $disposition; filename=\"$filename\""); - + // turn off output buffering and print part content - $IMAP->get_message_part($MESSAGE->uid, $part->mime_id, $part, true); + if ($part->body) + echo $part->body; + else + $IMAP->get_message_part($MESSAGE->uid, $part->mime_id, $part, true); } exit; |