3 * The Horde's class allows MS-TNEF data to be displayed.
5 * The TNEF rendering is based on code by:
6 * Graham Norbury <gnorbury@bondcar.com>
8 * Thomas Boll <tb@boll.ch>, Mark Simpson <damned@world.std.com>
10 * Copyright 2002-2010 The Horde Project (http://www.horde.org/)
12 * See the enclosed file COPYING for license information (LGPL). If you
13 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
15 * @author Jan Schneider <jan@horde.org>
16 * @author Michael Slusarz <slusarz@horde.org>
17 * @package Horde_Compress
21 const SIGNATURE = 0x223e9f78;
22 const LVL_MESSAGE = 0x01;
23 const LVL_ATTACHMENT = 0x02;
25 const ASUBJECT = 0x88004;
26 const AMCLASS = 0x78008;
27 const ATTACHDATA = 0x6800f;
28 const AFILENAME = 0x18010;
29 const ARENDDATA = 0x69002;
30 const AMAPIATTRS = 0x69005;
31 const AVERSION = 0x89006;
33 const MAPI_NULL = 0x0001;
34 const MAPI_SHORT = 0x0002;
35 const MAPI_INT = 0x0003;
36 const MAPI_FLOAT = 0x0004;
37 const MAPI_DOUBLE = 0x0005;
38 const MAPI_CURRENCY = 0x0006;
39 const MAPI_APPTIME = 0x0007;
40 const MAPI_ERROR = 0x000a;
41 const MAPI_BOOLEAN = 0x000b;
42 const MAPI_OBJECT = 0x000d;
43 const MAPI_INT8BYTE = 0x0014;
44 const MAPI_STRING = 0x001e;
45 const MAPI_UNICODE_STRING = 0x001f;
46 const MAPI_SYSTIME = 0x0040;
47 const MAPI_CLSID = 0x0048;
48 const MAPI_BINARY = 0x0102;
50 const MAPI_ATTACH_LONG_FILENAME = 0x3707;
51 const MAPI_ATTACH_MIME_TAG = 0x370E;
53 const MAPI_NAMED_TYPE_ID = 0x0000;
54 const MAPI_NAMED_TYPE_STRING = 0x0001;
55 const MAPI_MV_FLAG = 0x1000;
58 * Decompress the data.
60 * @param string $data The data to decompress.
61 * @param array $params An array of arguments needed to decompress the
64 * @return mixed The decompressed data.
66 public function decompress($data, $params = array())
70 if ($this->_geti($data, 32) == self::SIGNATURE) {
71 $this->_geti($data, 16);
73 while (strlen($data) > 0) {
74 switch ($this->_geti($data, 8)) {
75 case self::LVL_MESSAGE:
76 $this->_decodeMessage($data);
79 case self::LVL_ATTACHMENT:
80 $this->_decodeAttachment($data, $out);
86 return array_reverse($out);
92 * @param string &$data The data string.
93 * @param integer $bits How many bits to retrieve.
97 protected function _getx(&$data, $bits)
101 if (strlen($data) >= $bits) {
102 $value = substr($data, 0, $bits);
103 $data = substr_replace($data, '', 0, $bits);
112 * @param string &$data The data string.
113 * @param integer $bits How many bits to retrieve.
117 protected function _geti(&$data, $bits)
122 if (strlen($data) >= $bytes) {
123 $value = ord($data[0]);
125 $value += (ord($data[1]) << 8);
128 $value += (ord($data[2]) << 16) + (ord($data[3]) << 24);
130 $data = substr_replace($data, '', 0, $bytes);
139 * @param string &$data The data string.
140 * @param string $attribute TODO
142 protected function _decodeAttribute(&$data, $attribute)
145 $this->_getx($data, $this->_geti($data, 32));
148 $this->_geti($data, 16);
154 * @param string $data The data string.
155 * @param array &$attachment_data TODO
157 protected function _extractMapiAttributes($data, &$attachment_data)
159 /* Number of attributes. */
160 $number = $this->_geti($data, 32);
162 while ((strlen($data) > 0) && $number--) {
165 $named_id = $value = null;
166 $attr_type = $this->_geti($data, 16);
167 $attr_name = $this->_geti($data, 16);
169 if (($attr_type & self::MAPI_MV_FLAG) != 0) {
171 $attr_type = $attr_type & ~self::MAPI_MV_FLAG;
174 if (($attr_name >= 0x8000) && ($attr_name < 0xFFFE)) {
175 $this->_getx($data, 16);
176 $named_type = $this->_geti($data, 32);
178 switch ($named_type) {
179 case self::MAPI_NAMED_TYPE_ID:
180 $named_id = $this->_geti($data, 32);
181 $attr_name = $named_id;
184 case self::MAPI_NAMED_TYPE_STRING:
186 $idlen = $this->_geti($data, 32);
187 $datalen = $idlen + ((4 - ($idlen % 4)) % 4);
188 $named_id = substr($this->_getx($data, $datalen), 0, $idlen);
194 $num_mval = $this->_geti($data, 32);
197 switch ($attr_type) {
198 case self::MAPI_SHORT:
199 $value = $this->_geti($data, 16);
203 case self::MAPI_BOOLEAN:
204 for ($i = 0; $i < $num_mval; $i++) {
205 $value = $this->_geti($data, 32);
209 case self::MAPI_FLOAT:
210 case self::MAPI_ERROR:
211 $value = $this->_getx($data, 4);
214 case self::MAPI_DOUBLE:
215 case self::MAPI_APPTIME:
216 case self::MAPI_CURRENCY:
217 case self::MAPI_INT8BYTE:
218 case self::MAPI_SYSTIME:
219 $value = $this->_getx($data, 8);
222 case self::MAPI_STRING:
223 case self::MAPI_UNICODE_STRING:
224 case self::MAPI_BINARY:
225 case self::MAPI_OBJECT:
226 $num_vals = ($have_mval) ? $num_mval : $this->_geti($data, 32);
227 for ($i = 0; $i < $num_vals; $i++) {
228 $length = $this->_geti($data, 32);
230 /* Pad to next 4 byte boundary. */
231 $datalen = $length + ((4 - ($length % 4)) % 4);
233 if ($attr_type == self::MAPI_STRING) {
237 /* Read and truncate to length. */
238 $value = substr($this->_getx($data, $datalen), 0, $length);
243 /* Store any interesting attributes. */
244 switch ($attr_name) {
245 case self::MAPI_ATTACH_LONG_FILENAME:
246 /* Used in preference to AFILENAME value. */
247 $attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $value);
248 $attachment_data[0]['name'] = str_replace("\0", '', $attachment_data[0]['name']);
251 case self::MAPI_ATTACH_MIME_TAG:
252 /* Is this ever set, and what is format? */
253 $attachment_data[0]['type'] = preg_replace('/^(.*)\/.*/', '\1', $value);
254 $attachment_data[0]['subtype'] = preg_replace('/.*\/(.*)$/', '\1', $value);
255 $attachment_data[0]['subtype'] = str_replace("\0", '', $attachment_data[0]['subtype']);
264 * @param string &$data The data string.
266 protected function _decodeMessage(&$data)
268 $this->_decodeAttribute($data, $this->_geti($data, 32));
274 * @param string &$data The data string.
275 * @param array &$attachment_data TODO
277 protected function _decodeAttachment(&$data, &$attachment_data)
279 $attribute = $this->_geti($data, 32);
281 switch ($attribute) {
282 case self::ARENDDATA:
283 /* Marks start of new attachment. */
284 $this->_getx($data, $this->_geti($data, 32));
287 $this->_geti($data, 16);
289 /* Add a new default data block to hold details of this
290 attachment. Reverse order is easier to handle later! */
291 array_unshift($attachment_data, array('type' => 'application',
292 'subtype' => 'octet-stream',
297 case self::AFILENAME:
299 $attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $this->_getx($data, $this->_geti($data, 32)));
300 $attachment_data[0]['name'] = str_replace("\0", '', $attachment_data[0]['name']);
303 $this->_geti($data, 16);
306 case self::ATTACHDATA:
307 /* The attachment itself. */
308 $length = $this->_geti($data, 32);
309 $attachment_data[0]['size'] = $length;
310 $attachment_data[0]['stream'] = $this->_getx($data, $length);
313 $this->_geti($data, 16);
316 case self::AMAPIATTRS:
317 $length = $this->_geti($data, 32);
318 $value = $this->_getx($data, $length);
321 $this->_geti($data, 16);
322 $this->_extractMapiAttributes($value, $attachment_data);
326 $this->_decodeAttribute($data, $attribute);