Mercurial > hg > rc2
comparison program/lib/Roundcube/rcube_tnef_decoder.php @ 0:4681f974d28b
vanilla 1.3.3 distro, I hope
| author | Charlie Root |
|---|---|
| date | Thu, 04 Jan 2018 15:52:31 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4681f974d28b |
|---|---|
| 1 <?php | |
| 2 | |
| 3 /** | |
| 4 +-----------------------------------------------------------------------+ | |
| 5 | This file is part of the Roundcube Webmail client | | |
| 6 | Copyright (C) 2008-2017, 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 | Author: Aleksander Machniak <alec@alec.pl> | | |
| 19 +-----------------------------------------------------------------------+ | |
| 20 */ | |
| 21 | |
| 22 /** | |
| 23 * MS-TNEF format decoder based on code by: | |
| 24 * Graham Norbury <gnorbury@bondcar.com> | |
| 25 * Original design by: | |
| 26 * Thomas Boll <tb@boll.ch>, Mark Simpson <damned@world.std.com> | |
| 27 * | |
| 28 * @package Framework | |
| 29 * @subpackage Storage | |
| 30 */ | |
| 31 class rcube_tnef_decoder | |
| 32 { | |
| 33 const SIGNATURE = 0x223e9f78; | |
| 34 const LVL_MESSAGE = 0x01; | |
| 35 const LVL_ATTACHMENT = 0x02; | |
| 36 | |
| 37 const ASUBJECT = 0x88004; | |
| 38 const AMCLASS = 0x78008; | |
| 39 const ATTACHDATA = 0x6800f; | |
| 40 const AFILENAME = 0x18010; | |
| 41 const ARENDDATA = 0x69002; | |
| 42 const AMAPIATTRS = 0x69005; | |
| 43 const AOEMCODEPAGE = 0x69007; | |
| 44 const AVERSION = 0x89006; | |
| 45 | |
| 46 const MAPI_NULL = 0x0001; | |
| 47 const MAPI_SHORT = 0x0002; | |
| 48 const MAPI_INT = 0x0003; | |
| 49 const MAPI_FLOAT = 0x0004; | |
| 50 const MAPI_DOUBLE = 0x0005; | |
| 51 const MAPI_CURRENCY = 0x0006; | |
| 52 const MAPI_APPTIME = 0x0007; | |
| 53 const MAPI_ERROR = 0x000a; | |
| 54 const MAPI_BOOLEAN = 0x000b; | |
| 55 const MAPI_OBJECT = 0x000d; | |
| 56 const MAPI_INT8BYTE = 0x0014; | |
| 57 const MAPI_STRING = 0x001e; | |
| 58 const MAPI_UNICODE_STRING = 0x001f; | |
| 59 const MAPI_SYSTIME = 0x0040; | |
| 60 const MAPI_CLSID = 0x0048; | |
| 61 const MAPI_BINARY = 0x0102; | |
| 62 | |
| 63 const MAPI_DISPLAY_NAME = 0x3001; | |
| 64 const MAPI_ADDRTYPE = 0x3002; | |
| 65 const MAPI_EMAIL_ADDRESS = 0x3003; | |
| 66 const MAPI_COMMENT = 0x3004; | |
| 67 const MAPI_DEPTH = 0x3005; | |
| 68 const MAPI_PROVIDER_DISPLAY = 0x3006; | |
| 69 const MAPI_CREATION_TIME = 0x3007; | |
| 70 const MAPI_LAST_MODIFICATION_TIME = 0x3008; | |
| 71 const MAPI_RESOURCE_FLAGS = 0x3009; | |
| 72 const MAPI_PROVIDER_DLL_NAME = 0x300A; | |
| 73 const MAPI_SEARCH_KEY = 0x300B; | |
| 74 const MAPI_ATTACHMENT_X400_PARAMETERS = 0x3700; | |
| 75 const MAPI_ATTACH_DATA_OBJ = 0x3701; | |
| 76 const MAPI_ATTACH_ENCODING = 0x3702; | |
| 77 const MAPI_ATTACH_EXTENSION = 0x3703; | |
| 78 const MAPI_ATTACH_FILENAME = 0x3704; | |
| 79 const MAPI_ATTACH_METHOD = 0x3705; | |
| 80 const MAPI_ATTACH_LONG_FILENAME = 0x3707; | |
| 81 const MAPI_ATTACH_PATHNAME = 0x3708; | |
| 82 const MAPI_ATTACH_RENDERING = 0x3709; | |
| 83 const MAPI_ATTACH_TAG = 0x370A; | |
| 84 const MAPI_RENDERING_POSITION = 0x370B; | |
| 85 const MAPI_ATTACH_TRANSPORT_NAME = 0x370C; | |
| 86 const MAPI_ATTACH_LONG_PATHNAME = 0x370D; | |
| 87 const MAPI_ATTACH_MIME_TAG = 0x370E; | |
| 88 const MAPI_ATTACH_ADDITIONAL_INFO = 0x370F; | |
| 89 const MAPI_ATTACH_MIME_SEQUENCE = 0x3710; | |
| 90 const MAPI_ATTACH_CONTENT_ID = 0x3712; | |
| 91 const MAPI_ATTACH_CONTENT_LOCATION = 0x3713; | |
| 92 const MAPI_ATTACH_FLAGS = 0x3714; | |
| 93 | |
| 94 const MAPI_NAMED_TYPE_ID = 0x0000; | |
| 95 const MAPI_NAMED_TYPE_STRING = 0x0001; | |
| 96 const MAPI_MV_FLAG = 0x1000; | |
| 97 | |
| 98 /** | |
| 99 * Decompress the data. | |
| 100 * | |
| 101 * @param string $data The data to decompress. | |
| 102 * @param array $params An array of arguments needed to decompress the | |
| 103 * data. | |
| 104 * | |
| 105 * @return mixed The decompressed data. | |
| 106 */ | |
| 107 public function decompress($data, $params = array()) | |
| 108 { | |
| 109 $out = array(); | |
| 110 | |
| 111 if ($this->_geti($data, 32) == self::SIGNATURE) { | |
| 112 $this->_geti($data, 16); | |
| 113 | |
| 114 while (strlen($data) > 0) { | |
| 115 switch ($this->_geti($data, 8)) { | |
| 116 case self::LVL_MESSAGE: | |
| 117 $this->_decodeMessage($data); | |
| 118 break; | |
| 119 | |
| 120 case self::LVL_ATTACHMENT: | |
| 121 $this->_decodeAttachment($data, $out); | |
| 122 break; | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 return array_reverse($out); | |
| 128 } | |
| 129 | |
| 130 /** | |
| 131 * TODO | |
| 132 * | |
| 133 * @param string &$data The data string. | |
| 134 * @param integer $bits How many bits to retrieve. | |
| 135 * | |
| 136 * @return TODO | |
| 137 */ | |
| 138 protected function _getx(&$data, $bits) | |
| 139 { | |
| 140 $value = null; | |
| 141 | |
| 142 if (strlen($data) >= $bits) { | |
| 143 $value = substr($data, 0, $bits); | |
| 144 $data = substr_replace($data, '', 0, $bits); | |
| 145 } | |
| 146 | |
| 147 return $value; | |
| 148 } | |
| 149 | |
| 150 /** | |
| 151 * TODO | |
| 152 * | |
| 153 * @param string &$data The data string. | |
| 154 * @param integer $bits How many bits to retrieve. | |
| 155 * | |
| 156 * @return TODO | |
| 157 */ | |
| 158 protected function _geti(&$data, $bits) | |
| 159 { | |
| 160 $bytes = $bits / 8; | |
| 161 $value = null; | |
| 162 | |
| 163 if (strlen($data) >= $bytes) { | |
| 164 $value = ord($data[0]); | |
| 165 if ($bytes >= 2) { | |
| 166 $value += (ord($data[1]) << 8); | |
| 167 } | |
| 168 if ($bytes >= 4) { | |
| 169 $value += (ord($data[2]) << 16) + (ord($data[3]) << 24); | |
| 170 } | |
| 171 $data = substr_replace($data, '', 0, $bytes); | |
| 172 } | |
| 173 | |
| 174 return $value; | |
| 175 } | |
| 176 | |
| 177 /** | |
| 178 * TODO | |
| 179 * | |
| 180 * @param string &$data The data string. | |
| 181 * @param string $attribute TODO | |
| 182 */ | |
| 183 protected function _decodeAttribute(&$data, $attribute) | |
| 184 { | |
| 185 /* Data. */ | |
| 186 $value = $this->_getx($data, $this->_geti($data, 32)); | |
| 187 | |
| 188 /* Checksum. */ | |
| 189 $this->_geti($data, 16); | |
| 190 | |
| 191 return $value; | |
| 192 } | |
| 193 | |
| 194 /** | |
| 195 * TODO | |
| 196 * | |
| 197 * @param string $data The data string. | |
| 198 * @param array &$attachment_data TODO | |
| 199 */ | |
| 200 protected function _extractMapiAttributes($data, &$attachment_data) | |
| 201 { | |
| 202 /* Number of attributes. */ | |
| 203 $number = $this->_geti($data, 32); | |
| 204 | |
| 205 while ((strlen($data) > 0) && $number--) { | |
| 206 $have_mval = false; | |
| 207 $num_mval = 1; | |
| 208 $named_id = $value = null; | |
| 209 $attr_type = $this->_geti($data, 16); | |
| 210 $attr_name = $this->_geti($data, 16); | |
| 211 | |
| 212 if (($attr_type & self::MAPI_MV_FLAG) != 0) { | |
| 213 $have_mval = true; | |
| 214 $attr_type = $attr_type & ~self::MAPI_MV_FLAG; | |
| 215 } | |
| 216 | |
| 217 if (($attr_name >= 0x8000) && ($attr_name < 0xFFFE)) { | |
| 218 $this->_getx($data, 16); | |
| 219 $named_type = $this->_geti($data, 32); | |
| 220 | |
| 221 switch ($named_type) { | |
| 222 case self::MAPI_NAMED_TYPE_ID: | |
| 223 $named_id = $this->_geti($data, 32); | |
| 224 $attr_name = $named_id; | |
| 225 break; | |
| 226 | |
| 227 case self::MAPI_NAMED_TYPE_STRING: | |
| 228 $attr_name = 0x9999; | |
| 229 $idlen = $this->_geti($data, 32); | |
| 230 $datalen = $idlen + ((4 - ($idlen % 4)) % 4); | |
| 231 $named_id = substr($this->_getx($data, $datalen), 0, $idlen); | |
| 232 break; | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 if ($have_mval) { | |
| 237 $num_mval = $this->_geti($data, 32); | |
| 238 } | |
| 239 | |
| 240 switch ($attr_type) { | |
| 241 case self::MAPI_SHORT: | |
| 242 $value = $this->_geti($data, 16); | |
| 243 break; | |
| 244 | |
| 245 case self::MAPI_INT: | |
| 246 case self::MAPI_BOOLEAN: | |
| 247 for ($i = 0; $i < $num_mval; $i++) { | |
| 248 $value = $this->_geti($data, 32); | |
| 249 } | |
| 250 break; | |
| 251 | |
| 252 case self::MAPI_FLOAT: | |
| 253 case self::MAPI_ERROR: | |
| 254 $value = $this->_getx($data, 4); | |
| 255 break; | |
| 256 | |
| 257 case self::MAPI_DOUBLE: | |
| 258 case self::MAPI_APPTIME: | |
| 259 case self::MAPI_CURRENCY: | |
| 260 case self::MAPI_INT8BYTE: | |
| 261 case self::MAPI_SYSTIME: | |
| 262 $value = $this->_getx($data, 8); | |
| 263 break; | |
| 264 | |
| 265 case self::MAPI_STRING: | |
| 266 case self::MAPI_UNICODE_STRING: | |
| 267 case self::MAPI_BINARY: | |
| 268 case self::MAPI_OBJECT: | |
| 269 $num_vals = $have_mval ? $num_mval : $this->_geti($data, 32); | |
| 270 for ($i = 0; $i < $num_vals; $i++) { | |
| 271 $length = $this->_geti($data, 32); | |
| 272 | |
| 273 /* Pad to next 4 byte boundary. */ | |
| 274 $datalen = $length + ((4 - ($length % 4)) % 4); | |
| 275 | |
| 276 if ($attr_type == self::MAPI_STRING) { | |
| 277 --$length; | |
| 278 } | |
| 279 | |
| 280 /* Read and truncate to length. */ | |
| 281 $value = substr($this->_getx($data, $datalen), 0, $length); | |
| 282 } | |
| 283 break; | |
| 284 } | |
| 285 | |
| 286 /* Store any interesting attributes. */ | |
| 287 switch ($attr_name) { | |
| 288 case self::MAPI_ATTACH_LONG_FILENAME: | |
| 289 $value = $this->convertString($value); | |
| 290 /* Used in preference to AFILENAME value. */ | |
| 291 $attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $value); | |
| 292 break; | |
| 293 | |
| 294 case self::MAPI_ATTACH_MIME_TAG: | |
| 295 $value = $this->convertString($value); | |
| 296 /* Is this ever set, and what is format? */ | |
| 297 $attachment_data[0]['type'] = preg_replace('/^(.*)\/.*/', '\1', $value); | |
| 298 $attachment_data[0]['subtype'] = preg_replace('/.*\/(.*)$/', '\1', $value); | |
| 299 break; | |
| 300 } | |
| 301 } | |
| 302 } | |
| 303 | |
| 304 /** | |
| 305 * TODO | |
| 306 * | |
| 307 * @param string &$data The data string. | |
| 308 */ | |
| 309 protected function _decodeMessage(&$data) | |
| 310 { | |
| 311 $attribute = $this->_geti($data, 32); | |
| 312 $value = $this->_decodeAttribute($data, $attribute); | |
| 313 | |
| 314 switch ($attribute) { | |
| 315 case self::AOEMCODEPAGE: | |
| 316 // Find codepage of the message | |
| 317 $value = unpack('V', $value); | |
| 318 $this->codepage = $value[1]; | |
| 319 break; | |
| 320 | |
| 321 default: | |
| 322 } | |
| 323 } | |
| 324 | |
| 325 /** | |
| 326 * TODO | |
| 327 * | |
| 328 * @param string &$data The data string. | |
| 329 * @param array &$attachment_data TODO | |
| 330 */ | |
| 331 protected function _decodeAttachment(&$data, &$attachment_data) | |
| 332 { | |
| 333 $attribute = $this->_geti($data, 32); | |
| 334 | |
| 335 switch ($attribute) { | |
| 336 case self::ARENDDATA: | |
| 337 /* Marks start of new attachment. */ | |
| 338 $this->_getx($data, $this->_geti($data, 32)); | |
| 339 | |
| 340 /* Checksum */ | |
| 341 $this->_geti($data, 16); | |
| 342 | |
| 343 /* Add a new default data block to hold details of this | |
| 344 attachment. Reverse order is easier to handle later! */ | |
| 345 array_unshift($attachment_data, array('type' => 'application', | |
| 346 'subtype' => 'octet-stream', | |
| 347 'name' => 'unknown', | |
| 348 'stream' => '')); | |
| 349 break; | |
| 350 | |
| 351 case self::AFILENAME: | |
| 352 $value = $this->_getx($data, $this->_geti($data, 32)); | |
| 353 $value = $this->convertString($value, true); | |
| 354 | |
| 355 /* Strip path. */ | |
| 356 $attachment_data[0]['name'] = preg_replace('/.*[\/](.*)$/', '\1', $value); | |
| 357 | |
| 358 /* Checksum */ | |
| 359 $this->_geti($data, 16); | |
| 360 break; | |
| 361 | |
| 362 case self::ATTACHDATA: | |
| 363 /* The attachment itself. */ | |
| 364 $length = $this->_geti($data, 32); | |
| 365 $attachment_data[0]['size'] = $length; | |
| 366 $attachment_data[0]['stream'] = $this->_getx($data, $length); | |
| 367 | |
| 368 /* Checksum */ | |
| 369 $this->_geti($data, 16); | |
| 370 break; | |
| 371 | |
| 372 case self::AMAPIATTRS: | |
| 373 $value = $this->_getx($data, $this->_geti($data, 32)); | |
| 374 | |
| 375 /* Checksum */ | |
| 376 $this->_geti($data, 16); | |
| 377 $this->_extractMapiAttributes($value, $attachment_data); | |
| 378 break; | |
| 379 | |
| 380 default: | |
| 381 $this->_decodeAttribute($data, $attribute); | |
| 382 } | |
| 383 } | |
| 384 | |
| 385 /** | |
| 386 * Convert string value to system charset according to defined codepage | |
| 387 */ | |
| 388 protected function convertString($str, $use_codepage = false) | |
| 389 { | |
| 390 if ($convert && $this->codepage | |
| 391 && ($charset = rcube_charset::$windows_codepages[$this->codepage]) | |
| 392 ) { | |
| 393 $str = rcube_charset::convert($str, $charset); | |
| 394 } | |
| 395 else if (strpos($str, "\0") !== false) { | |
| 396 $str = rcube_charset::convert($str, 'UTF-16LE'); | |
| 397 } | |
| 398 | |
| 399 $str = rtrim($str, "\0"); | |
| 400 | |
| 401 return $str; | |
| 402 } | |
| 403 } |
