diff options
Diffstat (limited to 'program')
-rw-r--r-- | program/include/rcube_message.php | 15 | ||||
-rw-r--r-- | program/lib/tnef_decoder.inc | 352 | ||||
-rw-r--r-- | program/lib/tnef_decoder.php | 330 |
3 files changed, 337 insertions, 360 deletions
diff --git a/program/include/rcube_message.php b/program/include/rcube_message.php index e51ab26e0..c3753b290 100644 --- a/program/include/rcube_message.php +++ b/program/include/rcube_message.php @@ -500,7 +500,7 @@ class rcube_message { if (strlen($part->mime_id)) $this->mime_parts[$part->mime_id] = &$part; - + if (is_array($part->parts)) for ($i=0; $i<count($part->parts); $i++) $this->get_mime_numbers($part->parts[$i]); @@ -518,18 +518,17 @@ class rcube_message if (!isset($part->body)) $part->body = $this->imap->get_message_part($this->uid, $part->mime_id, $part); - require_once('lib/tnef_decoder.inc'); - $parts = array(); - $tnef_arr = tnef_decode($part->body); + $tnef = new tnef_decoder; + $tnef_arr = $tnef->decompress($part->body); foreach ($tnef_arr as $pid => $winatt) { $tpart = new rcube_message_part; $tpart->filename = trim($winatt['name']); $tpart->encoding = 'stream'; - $tpart->ctype_primary = trim(strtolower($winatt['type0'])); - $tpart->ctype_secondary = trim(strtolower($winatt['type1'])); + $tpart->ctype_primary = trim(strtolower($winatt['type'])); + $tpart->ctype_secondary = trim(strtolower($winatt['subtype'])); $tpart->mimetype = $tpart->ctype_primary . '/' . $tpart->ctype_secondary; $tpart->mime_id = 'winmail.' . $part->mime_id . '.' . $pid; $tpart->size = $winatt['size']; @@ -538,7 +537,7 @@ class rcube_message $parts[] = $tpart; unset($tnef_arr[$pid]); } - + return $parts; } @@ -584,7 +583,7 @@ class rcube_message unset($matches[$pid]); } } - + return $parts; } diff --git a/program/lib/tnef_decoder.inc b/program/lib/tnef_decoder.inc deleted file mode 100644 index 750faf657..000000000 --- a/program/lib/tnef_decoder.inc +++ /dev/null @@ -1,352 +0,0 @@ -<?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; - - $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 ($debug) - { - 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'] = preg_replace('/.*[\/](.*)$/', '\1', $value); // strip path - break; - - case TNEF_MAPI_ATTACH_MIME_TAG: // Is this ever set, and what is format? - $attachment_data[0]['type0'] = preg_replace('/^(.*)\/.*/', '\1', $value); - $attachment_data[0]['type1'] = preg_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'] = preg_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); - -} - -?> diff --git a/program/lib/tnef_decoder.php b/program/lib/tnef_decoder.php new file mode 100644 index 000000000..28d368989 --- /dev/null +++ b/program/lib/tnef_decoder.php @@ -0,0 +1,330 @@ +<?php +/** + * The Horde's class allows MS-TNEF data to be displayed. + * + * The TNEF rendering is based on code by: + * Graham Norbury <gnorbury@bondcar.com> + * Original design by: + * Thomas Boll <tb@boll.ch>, Mark Simpson <damned@world.std.com> + * + * Copyright 2002-2010 The Horde Project (http://www.horde.org/) + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @author Jan Schneider <jan@horde.org> + * @author Michael Slusarz <slusarz@horde.org> + * @package Horde_Compress + */ +class tnef_decoder +{ + const SIGNATURE = 0x223e9f78; + const LVL_MESSAGE = 0x01; + const LVL_ATTACHMENT = 0x02; + + const ASUBJECT = 0x88004; + const AMCLASS = 0x78008; + const ATTACHDATA = 0x6800f; + const AFILENAME = 0x18010; + const ARENDDATA = 0x69002; + const AMAPIATTRS = 0x69005; + const AVERSION = 0x89006; + + const MAPI_NULL = 0x0001; + const MAPI_SHORT = 0x0002; + const MAPI_INT = 0x0003; + const MAPI_FLOAT = 0x0004; + const MAPI_DOUBLE = 0x0005; + const MAPI_CURRENCY = 0x0006; + const MAPI_APPTIME = 0x0007; + const MAPI_ERROR = 0x000a; + const MAPI_BOOLEAN = 0x000b; + const MAPI_OBJECT = 0x000d; + const MAPI_INT8BYTE = 0x0014; + const MAPI_STRING = 0x001e; + const MAPI_UNICODE_STRING = 0x001f; + const MAPI_SYSTIME = 0x0040; + const MAPI_CLSID = 0x0048; + const MAPI_BINARY = 0x0102; + + const MAPI_ATTACH_LONG_FILENAME = 0x3707; + const MAPI_ATTACH_MIME_TAG = 0x370E; + + const MAPI_NAMED_TYPE_ID = 0x0000; + const MAPI_NAMED_TYPE_STRING = 0x0001; + const MAPI_MV_FLAG = 0x1000; + + /** + * Decompress the data. + * + * @param string $data The data to decompress. + * @param array $params An array of arguments needed to decompress the + * data. + * + * @return mixed The decompressed data. + */ + public function decompress($data, $params = array()) + { + $out = array(); + + if ($this->_geti($data, 32) == self::SIGNATURE) { + $this->_geti($data, 16); + + while (strlen($data) > 0) { + switch ($this->_geti($data, 8)) { + case self::LVL_MESSAGE: + $this->_decodeMessage($data); + break; + + case self::LVL_ATTACHMENT: + $this->_decodeAttachment($data, $out); + break; + } + } + } + + return array_reverse($out); + } + + /** + * TODO + * + * @param string &$data The data string. + * @param integer $bits How many bits to retrieve. + * + * @return TODO + */ + protected function _getx(&$data, $bits) + { + $value = null; + + if (strlen($data) >= $bits) { + $value = substr($data, 0, $bits); + $data = substr_replace($data, '', 0, $bits); + } + + return $value; + } + + /** + * TODO + * + * @param string &$data The data string. + * @param integer $bits How many bits to retrieve. + * + * @return TODO + */ + protected function _geti(&$data, $bits) + { + $bytes = $bits / 8; + $value = null; + + if (strlen($data) >= $bytes) { + $value = ord($data[0]); + if ($bytes >= 2) { + $value += (ord($data[1]) << 8); + } + if ($bytes >= 4) { + $value += (ord($data[2]) << 16) + (ord($data[3]) << 24); + } + $data = substr_replace($data, '', 0, $bytes); + } + + return $value; + } + + /** + * TODO + * + * @param string &$data The data string. + * @param string $attribute TODO + */ + protected function _decodeAttribute(&$data, $attribute) + { + /* Data. */ + $this->_getx($data, $this->_geti($data, 32)); + + /* Checksum. */ + $this->_geti($data, 16); + } + + /** + * TODO + * + * @param string $data The data string. + * @param array &$attachment_data TODO + */ + protected function _extractMapiAttributes($data, &$attachment_data) + { + /* Number of attributes. */ + $number = $this->_geti($data, 32); + + while ((strlen($data) > 0) && $number--) { + $have_mval = false; + $num_mval = 1; + $named_id = $value = null; + $attr_type = $this->_geti($data, 16); + $attr_name = $this->_geti($data, 16); + + if (($attr_type & self::MAPI_MV_FLAG) != 0) { + $have_mval = true; + $attr_type = $attr_type & ~self::MAPI_MV_FLAG; + } + + if (($attr_name >= 0x8000) && ($attr_name < 0xFFFE)) { + $this->_getx($data, 16); + $named_type = $this->_geti($data, 32); + + switch ($named_type) { + case self::MAPI_NAMED_TYPE_ID: + $named_id = $this->_geti($data, 32); + $attr_name = $named_id; + break; + + case self::MAPI_NAMED_TYPE_STRING: + $attr_name = 0x9999; + $idlen = $this->_geti($data, 32); + $datalen = $idlen + ((4 - ($idlen % 4)) % 4); + $named_id = substr($this->_getx($data, $datalen), 0, $idlen); + break; + } + } + + if ($have_mval) { + $num_mval = $this->_geti($data, 32); + } + + switch ($attr_type) { + case self::MAPI_SHORT: + $value = $this->_geti($data, 16); + break; + + case self::MAPI_INT: + case self::MAPI_BOOLEAN: + for ($i = 0; $i < $num_mval; $i++) { + $value = $this->_geti($data, 32); + } + break; + + case self::MAPI_FLOAT: + case self::MAPI_ERROR: + $value = $this->_getx($data, 4); + break; + + case self::MAPI_DOUBLE: + case self::MAPI_APPTIME: + case self::MAPI_CURRENCY: + case self::MAPI_INT8BYTE: + case self::MAPI_SYSTIME: + $value = $this->_getx($data, 8); + break; + + case self::MAPI_STRING: + case self::MAPI_UNICODE_STRING: + case self::MAPI_BINARY: + case self::MAPI_OBJECT: + $num_vals = ($have_mval) ? $num_mval : $this->_geti($data, 32); + for ($i = 0; $i < $num_vals; $i++) { + $length = $this->_geti($data, 32); + + /* Pad to next 4 byte boundary. */ + $datalen = $length + ((4 - ($length % 4)) % 4); + + if ($attr_type == self::MAPI_STRING) { + --$length; + } + + /* Read and truncate to length. */ + $value = substr($this->_getx($data, $datalen), 0, $length); + } + break; + } + + /* Store any interesting attributes. */ + switch ($attr_name) { + case self::MAPI_ATTACH_LONG_FILENAME: + /* Used in preference to AFILENAME value. */ + $attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $value); + $attachment_data[0]['name'] = str_replace("\0", '', $attachment_data[0]['name']); + break; + + case self::MAPI_ATTACH_MIME_TAG: + /* Is this ever set, and what is format? */ + $attachment_data[0]['type'] = preg_replace('/^(.*)\/.*/', '\1', $value); + $attachment_data[0]['subtype'] = preg_replace('/.*\/(.*)$/', '\1', $value); + $attachment_data[0]['subtype'] = str_replace("\0", '', $attachment_data[0]['subtype']); + break; + } + } + } + + /** + * TODO + * + * @param string &$data The data string. + */ + protected function _decodeMessage(&$data) + { + $this->_decodeAttribute($data, $this->_geti($data, 32)); + } + + /** + * TODO + * + * @param string &$data The data string. + * @param array &$attachment_data TODO + */ + protected function _decodeAttachment(&$data, &$attachment_data) + { + $attribute = $this->_geti($data, 32); + + switch ($attribute) { + case self::ARENDDATA: + /* Marks start of new attachment. */ + $this->_getx($data, $this->_geti($data, 32)); + + /* Checksum */ + $this->_geti($data, 16); + + /* Add a new default data block to hold details of this + attachment. Reverse order is easier to handle later! */ + array_unshift($attachment_data, array('type' => 'application', + 'subtype' => 'octet-stream', + 'name' => 'unknown', + 'stream' => '')); + break; + + case self::AFILENAME: + /* Strip path. */ + $attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $this->_getx($data, $this->_geti($data, 32))); + $attachment_data[0]['name'] = str_replace("\0", '', $attachment_data[0]['name']); + + /* Checksum */ + $this->_geti($data, 16); + break; + + case self::ATTACHDATA: + /* The attachment itself. */ + $length = $this->_geti($data, 32); + $attachment_data[0]['size'] = $length; + $attachment_data[0]['stream'] = $this->_getx($data, $length); + + /* Checksum */ + $this->_geti($data, 16); + break; + + case self::AMAPIATTRS: + $length = $this->_geti($data, 32); + $value = $this->_getx($data, $length); + + /* Checksum */ + $this->_geti($data, 16); + $this->_extractMapiAttributes($value, $attachment_data); + break; + + default: + $this->_decodeAttribute($data, $attribute); + } + } + +} |