commit | author | age
|
f19d86
|
1 |
<?php |
2883fc
|
2 |
|
a95874
|
3 |
/** |
2883fc
|
4 |
+-----------------------------------------------------------------------+ |
AM |
5 |
| This file is part of the Roundcube Webmail client | |
|
6 |
| Copyright (C) 2008-2014, The Roundcube Dev Team | |
|
7 |
| Copyright (C) 2002-2010, The Horde Project (http://www.horde.org/) | |
|
8 |
| | |
|
9 |
| Licensed under the GNU General Public License version 3 or | |
|
10 |
| any later version with exceptions for skins & plugins. | |
|
11 |
| See the README file for a full license statement. | |
|
12 |
| | |
|
13 |
| PURPOSE: | |
|
14 |
| MS-TNEF format decoder | |
|
15 |
+-----------------------------------------------------------------------+ |
|
16 |
| Author: Jan Schneider <jan@horde.org> | |
|
17 |
| Author: Michael Slusarz <slusarz@horde.org> | |
|
18 |
+-----------------------------------------------------------------------+ |
|
19 |
*/ |
|
20 |
|
f19d86
|
21 |
/** |
2883fc
|
22 |
* MS-TNEF format decoder based on code by: |
f19d86
|
23 |
* Graham Norbury <gnorbury@bondcar.com> |
A |
24 |
* Original design by: |
|
25 |
* Thomas Boll <tb@boll.ch>, Mark Simpson <damned@world.std.com> |
|
26 |
* |
2883fc
|
27 |
* @package Framework |
AM |
28 |
* @subpackage Storage |
f19d86
|
29 |
*/ |
2883fc
|
30 |
class rcube_tnef_decoder |
f19d86
|
31 |
{ |
A |
32 |
const SIGNATURE = 0x223e9f78; |
|
33 |
const LVL_MESSAGE = 0x01; |
|
34 |
const LVL_ATTACHMENT = 0x02; |
|
35 |
|
|
36 |
const ASUBJECT = 0x88004; |
|
37 |
const AMCLASS = 0x78008; |
|
38 |
const ATTACHDATA = 0x6800f; |
|
39 |
const AFILENAME = 0x18010; |
|
40 |
const ARENDDATA = 0x69002; |
|
41 |
const AMAPIATTRS = 0x69005; |
|
42 |
const AVERSION = 0x89006; |
|
43 |
|
|
44 |
const MAPI_NULL = 0x0001; |
|
45 |
const MAPI_SHORT = 0x0002; |
|
46 |
const MAPI_INT = 0x0003; |
|
47 |
const MAPI_FLOAT = 0x0004; |
|
48 |
const MAPI_DOUBLE = 0x0005; |
|
49 |
const MAPI_CURRENCY = 0x0006; |
|
50 |
const MAPI_APPTIME = 0x0007; |
|
51 |
const MAPI_ERROR = 0x000a; |
|
52 |
const MAPI_BOOLEAN = 0x000b; |
|
53 |
const MAPI_OBJECT = 0x000d; |
|
54 |
const MAPI_INT8BYTE = 0x0014; |
|
55 |
const MAPI_STRING = 0x001e; |
|
56 |
const MAPI_UNICODE_STRING = 0x001f; |
|
57 |
const MAPI_SYSTIME = 0x0040; |
|
58 |
const MAPI_CLSID = 0x0048; |
|
59 |
const MAPI_BINARY = 0x0102; |
|
60 |
|
|
61 |
const MAPI_ATTACH_LONG_FILENAME = 0x3707; |
|
62 |
const MAPI_ATTACH_MIME_TAG = 0x370E; |
|
63 |
|
|
64 |
const MAPI_NAMED_TYPE_ID = 0x0000; |
|
65 |
const MAPI_NAMED_TYPE_STRING = 0x0001; |
|
66 |
const MAPI_MV_FLAG = 0x1000; |
|
67 |
|
|
68 |
/** |
|
69 |
* Decompress the data. |
|
70 |
* |
|
71 |
* @param string $data The data to decompress. |
|
72 |
* @param array $params An array of arguments needed to decompress the |
|
73 |
* data. |
|
74 |
* |
|
75 |
* @return mixed The decompressed data. |
|
76 |
*/ |
|
77 |
public function decompress($data, $params = array()) |
|
78 |
{ |
|
79 |
$out = array(); |
|
80 |
|
|
81 |
if ($this->_geti($data, 32) == self::SIGNATURE) { |
|
82 |
$this->_geti($data, 16); |
|
83 |
|
|
84 |
while (strlen($data) > 0) { |
|
85 |
switch ($this->_geti($data, 8)) { |
|
86 |
case self::LVL_MESSAGE: |
|
87 |
$this->_decodeMessage($data); |
|
88 |
break; |
|
89 |
|
|
90 |
case self::LVL_ATTACHMENT: |
|
91 |
$this->_decodeAttachment($data, $out); |
|
92 |
break; |
|
93 |
} |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
return array_reverse($out); |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* TODO |
|
102 |
* |
|
103 |
* @param string &$data The data string. |
|
104 |
* @param integer $bits How many bits to retrieve. |
|
105 |
* |
|
106 |
* @return TODO |
|
107 |
*/ |
|
108 |
protected function _getx(&$data, $bits) |
|
109 |
{ |
|
110 |
$value = null; |
|
111 |
|
|
112 |
if (strlen($data) >= $bits) { |
|
113 |
$value = substr($data, 0, $bits); |
|
114 |
$data = substr_replace($data, '', 0, $bits); |
|
115 |
} |
|
116 |
|
|
117 |
return $value; |
|
118 |
} |
|
119 |
|
|
120 |
/** |
|
121 |
* TODO |
|
122 |
* |
|
123 |
* @param string &$data The data string. |
|
124 |
* @param integer $bits How many bits to retrieve. |
|
125 |
* |
|
126 |
* @return TODO |
|
127 |
*/ |
|
128 |
protected function _geti(&$data, $bits) |
|
129 |
{ |
|
130 |
$bytes = $bits / 8; |
|
131 |
$value = null; |
|
132 |
|
|
133 |
if (strlen($data) >= $bytes) { |
|
134 |
$value = ord($data[0]); |
|
135 |
if ($bytes >= 2) { |
|
136 |
$value += (ord($data[1]) << 8); |
|
137 |
} |
|
138 |
if ($bytes >= 4) { |
|
139 |
$value += (ord($data[2]) << 16) + (ord($data[3]) << 24); |
|
140 |
} |
|
141 |
$data = substr_replace($data, '', 0, $bytes); |
|
142 |
} |
|
143 |
|
|
144 |
return $value; |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* TODO |
|
149 |
* |
|
150 |
* @param string &$data The data string. |
|
151 |
* @param string $attribute TODO |
|
152 |
*/ |
|
153 |
protected function _decodeAttribute(&$data, $attribute) |
|
154 |
{ |
|
155 |
/* Data. */ |
|
156 |
$this->_getx($data, $this->_geti($data, 32)); |
|
157 |
|
|
158 |
/* Checksum. */ |
|
159 |
$this->_geti($data, 16); |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* TODO |
|
164 |
* |
|
165 |
* @param string $data The data string. |
|
166 |
* @param array &$attachment_data TODO |
|
167 |
*/ |
|
168 |
protected function _extractMapiAttributes($data, &$attachment_data) |
|
169 |
{ |
|
170 |
/* Number of attributes. */ |
|
171 |
$number = $this->_geti($data, 32); |
|
172 |
|
|
173 |
while ((strlen($data) > 0) && $number--) { |
|
174 |
$have_mval = false; |
|
175 |
$num_mval = 1; |
|
176 |
$named_id = $value = null; |
|
177 |
$attr_type = $this->_geti($data, 16); |
|
178 |
$attr_name = $this->_geti($data, 16); |
|
179 |
|
|
180 |
if (($attr_type & self::MAPI_MV_FLAG) != 0) { |
|
181 |
$have_mval = true; |
|
182 |
$attr_type = $attr_type & ~self::MAPI_MV_FLAG; |
|
183 |
} |
|
184 |
|
|
185 |
if (($attr_name >= 0x8000) && ($attr_name < 0xFFFE)) { |
|
186 |
$this->_getx($data, 16); |
|
187 |
$named_type = $this->_geti($data, 32); |
|
188 |
|
|
189 |
switch ($named_type) { |
|
190 |
case self::MAPI_NAMED_TYPE_ID: |
|
191 |
$named_id = $this->_geti($data, 32); |
|
192 |
$attr_name = $named_id; |
|
193 |
break; |
|
194 |
|
|
195 |
case self::MAPI_NAMED_TYPE_STRING: |
|
196 |
$attr_name = 0x9999; |
|
197 |
$idlen = $this->_geti($data, 32); |
|
198 |
$datalen = $idlen + ((4 - ($idlen % 4)) % 4); |
|
199 |
$named_id = substr($this->_getx($data, $datalen), 0, $idlen); |
|
200 |
break; |
|
201 |
} |
|
202 |
} |
|
203 |
|
|
204 |
if ($have_mval) { |
|
205 |
$num_mval = $this->_geti($data, 32); |
|
206 |
} |
|
207 |
|
|
208 |
switch ($attr_type) { |
|
209 |
case self::MAPI_SHORT: |
|
210 |
$value = $this->_geti($data, 16); |
|
211 |
break; |
|
212 |
|
|
213 |
case self::MAPI_INT: |
|
214 |
case self::MAPI_BOOLEAN: |
|
215 |
for ($i = 0; $i < $num_mval; $i++) { |
|
216 |
$value = $this->_geti($data, 32); |
|
217 |
} |
|
218 |
break; |
|
219 |
|
|
220 |
case self::MAPI_FLOAT: |
|
221 |
case self::MAPI_ERROR: |
|
222 |
$value = $this->_getx($data, 4); |
|
223 |
break; |
|
224 |
|
|
225 |
case self::MAPI_DOUBLE: |
|
226 |
case self::MAPI_APPTIME: |
|
227 |
case self::MAPI_CURRENCY: |
|
228 |
case self::MAPI_INT8BYTE: |
|
229 |
case self::MAPI_SYSTIME: |
|
230 |
$value = $this->_getx($data, 8); |
|
231 |
break; |
|
232 |
|
|
233 |
case self::MAPI_STRING: |
|
234 |
case self::MAPI_UNICODE_STRING: |
|
235 |
case self::MAPI_BINARY: |
|
236 |
case self::MAPI_OBJECT: |
7e3298
|
237 |
$num_vals = $have_mval ? $num_mval : $this->_geti($data, 32); |
f19d86
|
238 |
for ($i = 0; $i < $num_vals; $i++) { |
A |
239 |
$length = $this->_geti($data, 32); |
|
240 |
|
|
241 |
/* Pad to next 4 byte boundary. */ |
|
242 |
$datalen = $length + ((4 - ($length % 4)) % 4); |
|
243 |
|
|
244 |
if ($attr_type == self::MAPI_STRING) { |
|
245 |
--$length; |
|
246 |
} |
|
247 |
|
|
248 |
/* Read and truncate to length. */ |
|
249 |
$value = substr($this->_getx($data, $datalen), 0, $length); |
|
250 |
} |
|
251 |
break; |
|
252 |
} |
|
253 |
|
|
254 |
/* Store any interesting attributes. */ |
|
255 |
switch ($attr_name) { |
|
256 |
case self::MAPI_ATTACH_LONG_FILENAME: |
7bba00
|
257 |
$value = str_replace("\0", '', $value); |
f19d86
|
258 |
/* Used in preference to AFILENAME value. */ |
A |
259 |
$attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $value); |
|
260 |
break; |
|
261 |
|
|
262 |
case self::MAPI_ATTACH_MIME_TAG: |
7bba00
|
263 |
$value = str_replace("\0", '', $value); |
f19d86
|
264 |
/* Is this ever set, and what is format? */ |
7bba00
|
265 |
$attachment_data[0]['type'] = preg_replace('/^(.*)\/.*/', '\1', $value); |
f19d86
|
266 |
$attachment_data[0]['subtype'] = preg_replace('/.*\/(.*)$/', '\1', $value); |
A |
267 |
break; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
|
|
272 |
/** |
|
273 |
* TODO |
|
274 |
* |
|
275 |
* @param string &$data The data string. |
|
276 |
*/ |
|
277 |
protected function _decodeMessage(&$data) |
|
278 |
{ |
|
279 |
$this->_decodeAttribute($data, $this->_geti($data, 32)); |
|
280 |
} |
|
281 |
|
|
282 |
/** |
|
283 |
* TODO |
|
284 |
* |
|
285 |
* @param string &$data The data string. |
|
286 |
* @param array &$attachment_data TODO |
|
287 |
*/ |
|
288 |
protected function _decodeAttachment(&$data, &$attachment_data) |
|
289 |
{ |
|
290 |
$attribute = $this->_geti($data, 32); |
|
291 |
|
|
292 |
switch ($attribute) { |
|
293 |
case self::ARENDDATA: |
|
294 |
/* Marks start of new attachment. */ |
|
295 |
$this->_getx($data, $this->_geti($data, 32)); |
|
296 |
|
|
297 |
/* Checksum */ |
|
298 |
$this->_geti($data, 16); |
|
299 |
|
|
300 |
/* Add a new default data block to hold details of this |
|
301 |
attachment. Reverse order is easier to handle later! */ |
|
302 |
array_unshift($attachment_data, array('type' => 'application', |
|
303 |
'subtype' => 'octet-stream', |
|
304 |
'name' => 'unknown', |
|
305 |
'stream' => '')); |
|
306 |
break; |
|
307 |
|
|
308 |
case self::AFILENAME: |
7bba00
|
309 |
$value = $this->_getx($data, $this->_geti($data, 32)); |
AM |
310 |
$value = str_replace("\0", '', $value); |
f19d86
|
311 |
/* Strip path. */ |
7bba00
|
312 |
$attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $value); |
f19d86
|
313 |
|
A |
314 |
/* Checksum */ |
|
315 |
$this->_geti($data, 16); |
|
316 |
break; |
|
317 |
|
|
318 |
case self::ATTACHDATA: |
|
319 |
/* The attachment itself. */ |
|
320 |
$length = $this->_geti($data, 32); |
|
321 |
$attachment_data[0]['size'] = $length; |
|
322 |
$attachment_data[0]['stream'] = $this->_getx($data, $length); |
|
323 |
|
|
324 |
/* Checksum */ |
|
325 |
$this->_geti($data, 16); |
|
326 |
break; |
|
327 |
|
|
328 |
case self::AMAPIATTRS: |
|
329 |
$length = $this->_geti($data, 32); |
|
330 |
$value = $this->_getx($data, $length); |
|
331 |
|
|
332 |
/* Checksum */ |
|
333 |
$this->_geti($data, 16); |
|
334 |
$this->_extractMapiAttributes($value, $attachment_data); |
|
335 |
break; |
|
336 |
|
|
337 |
default: |
|
338 |
$this->_decodeAttribute($data, $attribute); |
|
339 |
} |
|
340 |
} |
|
341 |
} |