1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
<?php
/*
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| Copyright (C) 2008-2014, The Roundcube Dev Team |
| Copyright (C) 2002-2010, The Horde Project (http://www.horde.org/) |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| MS-TNEF format decoder |
+-----------------------------------------------------------------------+
| Author: Jan Schneider <jan@horde.org> |
| Author: Michael Slusarz <slusarz@horde.org> |
+-----------------------------------------------------------------------+
*/
/**
* MS-TNEF format decoder based on code by:
* Graham Norbury <gnorbury@bondcar.com>
* Original design by:
* Thomas Boll <tb@boll.ch>, Mark Simpson <damned@world.std.com>
*
* @package Framework
* @subpackage Storage
*/
class rcube_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:
$value = str_replace("\0", '', $value);
/* Used in preference to AFILENAME value. */
$attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $value);
break;
case self::MAPI_ATTACH_MIME_TAG:
$value = str_replace("\0", '', $value);
/* Is this ever set, and what is format? */
$attachment_data[0]['type'] = preg_replace('/^(.*)\/.*/', '\1', $value);
$attachment_data[0]['subtype'] = preg_replace('/.*\/(.*)$/', '\1', $value);
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:
$value = $this->_getx($data, $this->_geti($data, 32));
$value = str_replace("\0", '', $value);
/* Strip path. */
$attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $value);
/* 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);
}
}
}
|