| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Message.class.php |
| 5 | * |
| 6 | * This file contains functions needed to handle mime messages. |
| 7 | * |
| 8 | * @copyright © 2003-2007 The SquirrelMail Project Team |
| 9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 10 | * @version $Id$ |
| 11 | * @package squirrelmail |
| 12 | * @subpackage mime |
| 13 | * @since 1.3.2 |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * The object that contains a message. |
| 18 | * |
| 19 | * message is the object that contains messages. It is a recursive object in |
| 20 | * that through the $entities variable, it can contain more objects of type |
| 21 | * message. See documentation in mime.txt for a better description of how this |
| 22 | * works. |
| 23 | * @package squirrelmail |
| 24 | * @subpackage mime |
| 25 | * @since 1.3.0 |
| 26 | */ |
| 27 | class Message { |
| 28 | /** |
| 29 | * rfc822header object |
| 30 | * @var object |
| 31 | */ |
| 32 | var $rfc822_header = ''; |
| 33 | /** |
| 34 | * MessageHeader object |
| 35 | * @var object |
| 36 | */ |
| 37 | var $mime_header = ''; |
| 38 | /** |
| 39 | * @var mixed |
| 40 | */ |
| 41 | var $flags = ''; |
| 42 | /** |
| 43 | * Media type |
| 44 | * @var string |
| 45 | */ |
| 46 | var $type0=''; |
| 47 | /** |
| 48 | * Media subtype |
| 49 | * @var string |
| 50 | */ |
| 51 | var $type1=''; |
| 52 | /** |
| 53 | * Nested mime parts |
| 54 | * @var array |
| 55 | */ |
| 56 | var $entities = array(); |
| 57 | /** |
| 58 | * Message part id |
| 59 | * @var string |
| 60 | */ |
| 61 | var $entity_id = ''; |
| 62 | /** |
| 63 | * Parent message part id |
| 64 | * @var string |
| 65 | */ |
| 66 | var $parent_ent; |
| 67 | /** |
| 68 | * @var mixed |
| 69 | */ |
| 70 | var $entity; |
| 71 | /** |
| 72 | * @var mixed |
| 73 | */ |
| 74 | var $parent = ''; |
| 75 | /** |
| 76 | * @var string |
| 77 | */ |
| 78 | var $decoded_body=''; |
| 79 | /** |
| 80 | * Message \seen status |
| 81 | * @var boolean |
| 82 | */ |
| 83 | var $is_seen = 0; |
| 84 | /** |
| 85 | * Message \answered status |
| 86 | * @var boolean |
| 87 | */ |
| 88 | var $is_answered = 0; |
| 89 | /** |
| 90 | * Message \deleted status |
| 91 | * @var boolean |
| 92 | */ |
| 93 | var $is_deleted = 0; |
| 94 | /** |
| 95 | * Message \flagged status |
| 96 | * @var boolean |
| 97 | */ |
| 98 | var $is_flagged = 0; |
| 99 | /** |
| 100 | * Message mdn status |
| 101 | * @var boolean |
| 102 | */ |
| 103 | var $is_mdnsent = 0; |
| 104 | /** |
| 105 | * Message text body |
| 106 | * @var string |
| 107 | */ |
| 108 | var $body_part = ''; |
| 109 | /** |
| 110 | * Message part offset |
| 111 | * for fetching body parts out of raw messages |
| 112 | * @var integer |
| 113 | */ |
| 114 | var $offset = 0; |
| 115 | /** |
| 116 | * Message part length |
| 117 | * for fetching body parts out of raw messages |
| 118 | * @var integer |
| 119 | */ |
| 120 | var $length = 0; |
| 121 | /** |
| 122 | * Local attachment filename location where the tempory attachment is |
| 123 | * stored. For use in delivery class. |
| 124 | * @var string |
| 125 | */ |
| 126 | var $att_local_name = ''; |
| 127 | |
| 128 | /** |
| 129 | * @param string $ent entity id |
| 130 | */ |
| 131 | function setEnt($ent) { |
| 132 | $this->entity_id= $ent; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Add nested message part |
| 137 | * @param object $msg |
| 138 | */ |
| 139 | function addEntity ($msg) { |
| 140 | $this->entities[] = $msg; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get file name used for mime part |
| 145 | * @return string file name |
| 146 | * @since 1.3.2 |
| 147 | */ |
| 148 | function getFilename() { |
| 149 | $filename = ''; |
| 150 | $header = $this->header; |
| 151 | if (is_object($header->disposition)) { |
| 152 | $filename = $header->disposition->getProperty('filename'); |
| 153 | if (trim($filename) == '') { |
| 154 | $name = decodeHeader($header->disposition->getProperty('name')); |
| 155 | if (!trim($name)) { |
| 156 | $name = $header->getParameter('name'); |
| 157 | if(!trim($name)) { |
| 158 | if (!trim( $header->id )) { |
| 159 | $filename = 'untitled-[' . $this->entity_id . ']' ; |
| 160 | } else { |
| 161 | $filename = 'cid: ' . $header->id; |
| 162 | } |
| 163 | } else { |
| 164 | $filename = $name; |
| 165 | } |
| 166 | } else { |
| 167 | $filename = $name; |
| 168 | } |
| 169 | } |
| 170 | } else { |
| 171 | $filename = $header->getParameter('filename'); |
| 172 | if (!trim($filename)) { |
| 173 | $filename = $header->getParameter('name'); |
| 174 | if (!trim($filename)) { |
| 175 | if (!trim( $header->id )) { |
| 176 | $filename = 'untitled-[' . $this->entity_id . ']' ; |
| 177 | } else { |
| 178 | $filename = 'cid: ' . $header->id; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return $filename; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Add header object to message object. |
| 188 | * WARNING: Unfinished code. Don't expect it to work in older sm versions. |
| 189 | * @param mixed $read array or string with message headers |
| 190 | * @todo FIXME: rfc822header->parseHeader() does not return rfc822header object |
| 191 | */ |
| 192 | function addRFC822Header($read) { |
| 193 | $header = new Rfc822Header(); |
| 194 | $this->rfc822_header = $header->parseHeader($read); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @param string $ent |
| 199 | * @return mixed (object or string?) |
| 200 | */ |
| 201 | function getEntity($ent) { |
| 202 | $cur_ent = $this->entity_id; |
| 203 | $msg = $this; |
| 204 | if (($cur_ent == '') || ($cur_ent == '0')) { |
| 205 | $cur_ent_a = array(); |
| 206 | } else { |
| 207 | $cur_ent_a = explode('.', $this->entity_id); |
| 208 | } |
| 209 | $ent_a = explode('.', $ent); |
| 210 | |
| 211 | for ($i = 0,$entCount = count($ent_a) - 1; $i < $entCount; ++$i) { |
| 212 | if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) { |
| 213 | $msg = $msg->parent; |
| 214 | $cur_ent_a = explode('.', $msg->entity_id); |
| 215 | --$i; |
| 216 | } else if (!isset($cur_ent_a[$i])) { |
| 217 | if (isset($msg->entities[($ent_a[$i]-1)])) { |
| 218 | $msg = $msg->entities[($ent_a[$i]-1)]; |
| 219 | } else { |
| 220 | $msg = $msg->entities[0]; |
| 221 | } |
| 222 | } |
| 223 | if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) { |
| 224 | /*this is a header for a message/rfc822 entity */ |
| 225 | $msg = $msg->entities[0]; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) { |
| 230 | /*this is a header for a message/rfc822 entity */ |
| 231 | $msg = $msg->entities[0]; |
| 232 | } |
| 233 | |
| 234 | if (isset($msg->entities[($ent_a[$entCount])-1])) { |
| 235 | if (is_object($msg->entities[($ent_a[$entCount])-1])) { |
| 236 | $msg = $msg->entities[($ent_a[$entCount]-1)]; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return $msg; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Set message body |
| 245 | * @param string $s message body |
| 246 | */ |
| 247 | function setBody($s) { |
| 248 | $this->body_part = $s; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Clean message object |
| 253 | */ |
| 254 | function clean_up() { |
| 255 | $msg = $this; |
| 256 | $msg->body_part = ''; |
| 257 | |
| 258 | foreach ($msg->entities as $m) { |
| 259 | $m->clean_up(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @return string |
| 265 | */ |
| 266 | function getMailbox() { |
| 267 | $msg = $this; |
| 268 | while (is_object($msg->parent)) { |
| 269 | $msg = $msg->parent; |
| 270 | } |
| 271 | return $msg->mailbox; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Bodystructure parser, a recursive function for generating the |
| 276 | * entity-tree with all the mime-parts. |
| 277 | * |
| 278 | * It follows RFC2060 and stores all the described fields in the |
| 279 | * message object. |
| 280 | * |
| 281 | * Question/Bugs: |
| 282 | * |
| 283 | * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net) |
| 284 | * @param string $read |
| 285 | * @param integer $i |
| 286 | * @param mixed $sub_msg |
| 287 | * @return object Message object |
| 288 | * @todo define argument and return types |
| 289 | */ |
| 290 | function parseStructure($read, &$i, $sub_msg = '') { |
| 291 | $msg = Message::parseBodyStructure($read, $i, $sub_msg); |
| 292 | if($msg) $msg->setEntIds($msg,false,0); |
| 293 | return $msg; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @param object $msg |
| 298 | * @param mixed $init |
| 299 | * @param integer $i |
| 300 | * @todo document me |
| 301 | * @since 1.4.0 |
| 302 | */ |
| 303 | function setEntIds(&$msg,$init=false,$i=0) { |
| 304 | $iCnt = count($msg->entities); |
| 305 | if ($init !==false) { |
| 306 | $iEntSub = $i+1; |
| 307 | if ($msg->parent->type0 == 'message' && |
| 308 | $msg->parent->type1 == 'rfc822' && |
| 309 | $msg->type0 == 'multipart') { |
| 310 | $iEntSub = '0'; |
| 311 | } |
| 312 | if ($init) { |
| 313 | $msg->entity_id = "$init.$iEntSub"; |
| 314 | } else { |
| 315 | $msg->entity_id = $iEntSub; |
| 316 | } |
| 317 | } else if ($iCnt) { |
| 318 | $msg->entity_id='0'; |
| 319 | } else { |
| 320 | $msg->entity_id='1'; |
| 321 | } |
| 322 | for ($i=0;$i<$iCnt;++$i) { |
| 323 | $msg->entities[$i]->parent =& $msg; |
| 324 | if (strrchr($msg->entity_id, '.') != '.0') { |
| 325 | $msg->entities[$i]->setEntIds($msg->entities[$i],$msg->entity_id,$i); |
| 326 | } else { |
| 327 | $msg->entities[$i]->setEntIds($msg->entities[$i],$msg->parent->entity_id,$i); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * @param string $read |
| 334 | * @param integer $i |
| 335 | * @param mixed $sub_msg |
| 336 | * @return object Message object |
| 337 | * @todo document me |
| 338 | * @since 1.4.0 (code was part of parseStructure() in 1.3.x) |
| 339 | */ |
| 340 | function parseBodyStructure($read, &$i, $sub_msg = '') { |
| 341 | $arg_no = 0; |
| 342 | $arg_a = array(); |
| 343 | if ($sub_msg) { |
| 344 | $message = $sub_msg; |
| 345 | } else { |
| 346 | $message = new Message(); |
| 347 | } |
| 348 | |
| 349 | for ($cnt = strlen($read); $i < $cnt; ++$i) { |
| 350 | $char = strtoupper($read{$i}); |
| 351 | switch ($char) { |
| 352 | case '(': |
| 353 | switch($arg_no) { |
| 354 | case 0: |
| 355 | if (!isset($msg)) { |
| 356 | $msg = new Message(); |
| 357 | $hdr = new MessageHeader(); |
| 358 | $hdr->type0 = 'text'; |
| 359 | $hdr->type1 = 'plain'; |
| 360 | $hdr->encoding = 'us-ascii'; |
| 361 | } else { |
| 362 | $msg->header->type0 = 'multipart'; |
| 363 | $msg->type0 = 'multipart'; |
| 364 | while ($read{$i} == '(') { |
| 365 | $msg->addEntity($msg->parseBodyStructure($read, $i, $msg)); |
| 366 | } |
| 367 | } |
| 368 | break; |
| 369 | case 1: |
| 370 | /* multipart properties */ |
| 371 | ++$i; |
| 372 | $arg_a[] = $msg->parseProperties($read, $i); |
| 373 | ++$arg_no; |
| 374 | break; |
| 375 | case 2: |
| 376 | if (isset($msg->type0) && ($msg->type0 == 'multipart')) { |
| 377 | ++$i; |
| 378 | $arg_a[] = $msg->parseDisposition($read, $i); |
| 379 | } else { /* properties */ |
| 380 | $arg_a[] = $msg->parseProperties($read, $i); |
| 381 | } |
| 382 | ++$arg_no; |
| 383 | break; |
| 384 | case 3: |
| 385 | if (isset($msg->type0) && ($msg->type0 == 'multipart')) { |
| 386 | ++$i; |
| 387 | $arg_a[]= $msg->parseLanguage($read, $i); |
| 388 | } |
| 389 | case 7: |
| 390 | if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) { |
| 391 | $msg->header->type0 = $arg_a[0]; |
| 392 | $msg->header->type1 = $arg_a[1]; |
| 393 | $msg->type0 = $arg_a[0]; |
| 394 | $msg->type1 = $arg_a[1]; |
| 395 | $rfc822_hdr = new Rfc822Header(); |
| 396 | $msg->rfc822_header = $msg->parseEnvelope($read, $i, $rfc822_hdr); |
| 397 | while (($i < $cnt) && ($read{$i} != '(')) { |
| 398 | ++$i; |
| 399 | } |
| 400 | $msg->addEntity($msg->parseBodyStructure($read, $i,$msg)); |
| 401 | } |
| 402 | break; |
| 403 | case 8: |
| 404 | ++$i; |
| 405 | $arg_a[] = $msg->parseDisposition($read, $i); |
| 406 | ++$arg_no; |
| 407 | break; |
| 408 | case 9: |
| 409 | ++$i; |
| 410 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
| 411 | $arg_a[] = $msg->parseDisposition($read, $i); |
| 412 | } else { |
| 413 | $arg_a[] = $msg->parseLanguage($read, $i); |
| 414 | } |
| 415 | ++$arg_no; |
| 416 | break; |
| 417 | case 10: |
| 418 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
| 419 | ++$i; |
| 420 | $arg_a[] = $msg->parseLanguage($read, $i); |
| 421 | } else { |
| 422 | $i = $msg->parseParenthesis($read, $i); |
| 423 | $arg_a[] = ''; /* not yet described in rfc2060 */ |
| 424 | } |
| 425 | ++$arg_no; |
| 426 | break; |
| 427 | default: |
| 428 | /* unknown argument, skip this part */ |
| 429 | $i = $msg->parseParenthesis($read, $i); |
| 430 | $arg_a[] = ''; |
| 431 | ++$arg_no; |
| 432 | break; |
| 433 | } /* switch */ |
| 434 | break; |
| 435 | case '"': |
| 436 | /* inside an entity -> start processing */ |
| 437 | $arg_s = $msg->parseQuote($read, $i); |
| 438 | ++$arg_no; |
| 439 | if ($arg_no < 3) { |
| 440 | $arg_s = strtolower($arg_s); /* type0 and type1 */ |
| 441 | } |
| 442 | $arg_a[] = $arg_s; |
| 443 | break; |
| 444 | case 'n': |
| 445 | case 'N': |
| 446 | /* probably NIL argument */ |
| 447 | $tmpnil = strtoupper(substr($read, $i, 4)); |
| 448 | if ($tmpnil == 'NIL ' || $tmpnil == 'NIL)') { |
| 449 | $arg_a[] = ''; |
| 450 | ++$arg_no; |
| 451 | $i += 2; |
| 452 | } |
| 453 | break; |
| 454 | case '{': |
| 455 | /* process the literal value */ |
| 456 | $arg_a[] = $msg->parseLiteral($read, $i); |
| 457 | ++$arg_no; |
| 458 | break; |
| 459 | case '0': |
| 460 | case is_numeric($read{$i}): |
| 461 | /* process integers */ |
| 462 | if ($read{$i} == ' ') { break; } |
| 463 | ++$arg_no; |
| 464 | if (preg_match('/^([0-9]+).*/',substr($read,$i), $regs)) { |
| 465 | $i += strlen($regs[1])-1; |
| 466 | $arg_a[] = $regs[1]; |
| 467 | } else { |
| 468 | $arg_a[] = 0; |
| 469 | } |
| 470 | break; |
| 471 | case ')': |
| 472 | $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart')); |
| 473 | if (!$multipart) { |
| 474 | $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))); |
| 475 | $hdr->type0 = $arg_a[0]; |
| 476 | $hdr->type1 = $arg_a[1]; |
| 477 | |
| 478 | $msg->type0 = $arg_a[0]; |
| 479 | $msg->type1 = $arg_a[1]; |
| 480 | $arr = $arg_a[2]; |
| 481 | if (is_array($arr)) { |
| 482 | $hdr->parameters = $arg_a[2]; |
| 483 | } |
| 484 | $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3])); |
| 485 | $hdr->description = $arg_a[4]; |
| 486 | $hdr->encoding = strtolower($arg_a[5]); |
| 487 | $hdr->entity_id = $msg->entity_id; |
| 488 | $hdr->size = $arg_a[6]; |
| 489 | if ($shifted_args) { |
| 490 | $hdr->lines = $arg_a[7]; |
| 491 | $s = 1; |
| 492 | } else { |
| 493 | $s = 0; |
| 494 | } |
| 495 | $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5); |
| 496 | $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition); |
| 497 | $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language); |
| 498 | $msg->header = $hdr; |
| 499 | } else { |
| 500 | $hdr->type0 = 'multipart'; |
| 501 | $hdr->type1 = $arg_a[0]; |
| 502 | $msg->type0 = 'multipart'; |
| 503 | $msg->type1 = $arg_a[0]; |
| 504 | $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters); |
| 505 | $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition); |
| 506 | $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language); |
| 507 | $msg->header = $hdr; |
| 508 | } |
| 509 | return $msg; |
| 510 | default: break; |
| 511 | } /* switch */ |
| 512 | } /* for */ |
| 513 | } /* parsestructure */ |
| 514 | |
| 515 | /** |
| 516 | * @param string $read |
| 517 | * @param integer $i |
| 518 | * @return array |
| 519 | */ |
| 520 | function parseProperties($read, &$i) { |
| 521 | $properties = array(); |
| 522 | $prop_name = ''; |
| 523 | |
| 524 | for (; $read{$i} != ')'; ++$i) { |
| 525 | $arg_s = ''; |
| 526 | if ($read{$i} == '"') { |
| 527 | $arg_s = $this->parseQuote($read, $i); |
| 528 | } else if ($read{$i} == '{') { |
| 529 | $arg_s = $this->parseLiteral($read, $i); |
| 530 | } |
| 531 | |
| 532 | if ($arg_s != '') { |
| 533 | if ($prop_name == '') { |
| 534 | $prop_name = strtolower($arg_s); |
| 535 | $properties[$prop_name] = ''; |
| 536 | } else if ($prop_name != '') { |
| 537 | $properties[$prop_name] = $arg_s; |
| 538 | $prop_name = ''; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | return $properties; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * @param string $read |
| 547 | * @param integer $i |
| 548 | * @param object $hdr MessageHeader object |
| 549 | * @return object MessageHeader object |
| 550 | */ |
| 551 | function parseEnvelope($read, &$i, $hdr) { |
| 552 | $arg_no = 0; |
| 553 | $arg_a = array(); |
| 554 | ++$i; |
| 555 | for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) { |
| 556 | $char = strtoupper($read{$i}); |
| 557 | switch ($char) { |
| 558 | case '"': |
| 559 | $arg_a[] = $this->parseQuote($read, $i); |
| 560 | ++$arg_no; |
| 561 | break; |
| 562 | case '{': |
| 563 | $arg_a[] = $this->parseLiteral($read, $i); |
| 564 | /* temp bugfix (SM 1.5 will have a working clean version) |
| 565 | too much work to implement that version right now */ |
| 566 | // --$i; |
| 567 | ++$arg_no; |
| 568 | break; |
| 569 | case 'N': |
| 570 | /* probably NIL argument */ |
| 571 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
| 572 | $arg_a[] = ''; |
| 573 | ++$arg_no; |
| 574 | $i += 2; |
| 575 | } |
| 576 | break; |
| 577 | case '(': |
| 578 | /* Address structure (with group support) |
| 579 | * Note: Group support is useless on SMTP connections |
| 580 | * because the protocol doesn't support it |
| 581 | */ |
| 582 | $addr_a = array(); |
| 583 | $group = ''; |
| 584 | $a=0; |
| 585 | for (; $i < $cnt && $read{$i} != ')'; ++$i) { |
| 586 | if ($read{$i} == '(') { |
| 587 | $addr = $this->parseAddress($read, $i); |
| 588 | if (($addr->host == '') && ($addr->mailbox != '')) { |
| 589 | /* start of group */ |
| 590 | $group = $addr->mailbox; |
| 591 | $group_addr = $addr; |
| 592 | $j = $a; |
| 593 | } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) { |
| 594 | /* end group */ |
| 595 | if ($a == ($j+1)) { /* no group members */ |
| 596 | $group_addr->group = $group; |
| 597 | $group_addr->mailbox = ''; |
| 598 | $group_addr->personal = "$group: Undisclosed recipients;"; |
| 599 | $addr_a[] = $group_addr; |
| 600 | $group =''; |
| 601 | } |
| 602 | } else { |
| 603 | $addr->group = $group; |
| 604 | $addr_a[] = $addr; |
| 605 | } |
| 606 | ++$a; |
| 607 | } |
| 608 | } |
| 609 | $arg_a[] = $addr_a; |
| 610 | break; |
| 611 | default: break; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | if (count($arg_a) > 9) { |
| 616 | $d = strtr($arg_a[0], array(' ' => ' ')); |
| 617 | $d = explode(' ', $d); |
| 618 | if (!$arg_a[1]) $arg_a[1] = _("(no subject)"); |
| 619 | |
| 620 | $hdr->date = getTimeStamp($d); /* argument 1: date */ |
| 621 | $hdr->subject = $arg_a[1]; /* argument 2: subject */ |
| 622 | $hdr->from = is_array($arg_a[2]) ? $arg_a[2][0] : ''; /* argument 3: from */ |
| 623 | $hdr->sender = is_array($arg_a[3]) ? $arg_a[3][0] : ''; /* argument 4: sender */ |
| 624 | $hdr->replyto = is_array($arg_a[4]) ? $arg_a[4][0] : ''; /* argument 5: reply-to */ |
| 625 | $hdr->to = $arg_a[5]; /* argument 6: to */ |
| 626 | $hdr->cc = $arg_a[6]; /* argument 7: cc */ |
| 627 | $hdr->bcc = $arg_a[7]; /* argument 8: bcc */ |
| 628 | $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */ |
| 629 | $hdr->message_id = $arg_a[9]; /* argument 10: message-id */ |
| 630 | } |
| 631 | return $hdr; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * @param string $read |
| 636 | * @param integer $i |
| 637 | * @return string |
| 638 | * @todo document me |
| 639 | */ |
| 640 | function parseLiteral($read, &$i) { |
| 641 | $lit_cnt = ''; |
| 642 | ++$i; |
| 643 | $iPos = strpos($read,'}',$i); |
| 644 | if ($iPos) { |
| 645 | $lit_cnt = substr($read, $i, $iPos - $i); |
| 646 | $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */ |
| 647 | /* Now read the literal */ |
| 648 | $s = ($lit_cnt ? substr($read,$i,$lit_cnt): ''); |
| 649 | $i += $lit_cnt; |
| 650 | /* temp bugfix (SM 1.5 will have a working clean version) |
| 651 | too much work to implement that version right now */ |
| 652 | --$i; |
| 653 | } else { /* should never happen */ |
| 654 | $i += 3; /* } + \r + \n */ |
| 655 | $s = ''; |
| 656 | } |
| 657 | return $s; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * function parseQuote |
| 662 | * |
| 663 | * This extract the string value from a quoted string. After the end-quote |
| 664 | * character is found it returns the string. The offset $i when calling |
| 665 | * this function points to the first double quote. At the end it points to |
| 666 | * The ending quote. This function takes care of escaped double quotes. |
| 667 | * "some \"string\"" |
| 668 | * ^ ^ |
| 669 | * initial $i end position $i |
| 670 | * |
| 671 | * @param string $read |
| 672 | * @param integer $i offset in $read |
| 673 | * @return string string inbetween the double quotes |
| 674 | * @author Marc Groot Koerkamp |
| 675 | */ |
| 676 | function parseQuote($read, &$i) { |
| 677 | $s = ''; |
| 678 | $iPos = ++$i; |
| 679 | $iPosStart = $iPos; |
| 680 | while (true) { |
| 681 | $iPos = strpos($read,'"',$iPos); |
| 682 | if (!$iPos) break; |
| 683 | if ($iPos && $read{$iPos -1} != '\\') { |
| 684 | $s = substr($read,$i,($iPos-$i)); |
| 685 | $i = $iPos; |
| 686 | break; |
| 687 | } else if ($iPos > 1 && $read{$iPos -1} == '\\' && $read{$iPos-2} == '\\') { |
| 688 | // This is an unique situation where the fast detection of the string |
| 689 | // fails. If the quote string ends with \\ then we need to iterate |
| 690 | // through the entire string to make sure we detect the unexcaped |
| 691 | // double quotes correctly. |
| 692 | $s = ''; |
| 693 | $bEscaped = false; |
| 694 | $k = 0; |
| 695 | for ($j=$iPosStart,$iCnt=strlen($read);$j<$iCnt;++$j) { |
| 696 | $cChar = $read{$j}; |
| 697 | switch ($cChar) { |
| 698 | case '\\': |
| 699 | $bEscaped = !$bEscaped; |
| 700 | $s .= $cChar; |
| 701 | break; |
| 702 | case '"': |
| 703 | if ($bEscaped) { |
| 704 | $s .= $cChar; |
| 705 | $bEscaped = false; |
| 706 | } else { |
| 707 | $i = $j; |
| 708 | break 3; |
| 709 | } |
| 710 | break; |
| 711 | default: |
| 712 | if ($bEscaped) { |
| 713 | $bEscaped = false; |
| 714 | } |
| 715 | $s .= $cChar; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | ++$iPos; |
| 721 | if ($iPos > strlen($read)) { |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | return $s; |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * @param string $read |
| 730 | * @param integer $i |
| 731 | * @return object AddressStructure object |
| 732 | */ |
| 733 | function parseAddress($read, &$i) { |
| 734 | $arg_a = array(); |
| 735 | for (; $read{$i} != ')'; ++$i) { |
| 736 | $char = strtoupper($read{$i}); |
| 737 | switch ($char) { |
| 738 | case '"': $arg_a[] = $this->parseQuote($read, $i); break; |
| 739 | case '{': $arg_a[] = $this->parseLiteral($read, $i); break; |
| 740 | case 'n': |
| 741 | case 'N': |
| 742 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
| 743 | $arg_a[] = ''; |
| 744 | $i += 2; |
| 745 | } |
| 746 | break; |
| 747 | default: break; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | if (count($arg_a) == 4) { |
| 752 | $adr = new AddressStructure(); |
| 753 | $adr->personal = $arg_a[0]; |
| 754 | $adr->adl = $arg_a[1]; |
| 755 | $adr->mailbox = $arg_a[2]; |
| 756 | $adr->host = $arg_a[3]; |
| 757 | } else { |
| 758 | $adr = ''; |
| 759 | } |
| 760 | return $adr; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * @param string $read |
| 765 | * @param integer $i |
| 766 | * @param object Disposition object or empty string |
| 767 | */ |
| 768 | function parseDisposition($read, &$i) { |
| 769 | $arg_a = array(); |
| 770 | for (; $read{$i} != ')'; ++$i) { |
| 771 | switch ($read{$i}) { |
| 772 | case '"': $arg_a[] = $this->parseQuote($read, $i); break; |
| 773 | case '{': $arg_a[] = $this->parseLiteral($read, $i); break; |
| 774 | case '(': $arg_a[] = $this->parseProperties($read, $i); break; |
| 775 | default: break; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if (isset($arg_a[0])) { |
| 780 | $disp = new Disposition($arg_a[0]); |
| 781 | if (isset($arg_a[1])) { |
| 782 | $disp->properties = $arg_a[1]; |
| 783 | } |
| 784 | } |
| 785 | return (is_object($disp) ? $disp : ''); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * @param string $read |
| 790 | * @param integer $i |
| 791 | * @return object Language object or empty string |
| 792 | */ |
| 793 | function parseLanguage($read, &$i) { |
| 794 | /* no idea how to process this one without examples */ |
| 795 | $arg_a = array(); |
| 796 | |
| 797 | for (; $read{$i} != ')'; ++$i) { |
| 798 | switch ($read{$i}) { |
| 799 | case '"': $arg_a[] = $this->parseQuote($read, $i); break; |
| 800 | case '{': $arg_a[] = $this->parseLiteral($read, $i); break; |
| 801 | case '(': $arg_a[] = $this->parseProperties($read, $i); break; |
| 802 | default: break; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if (isset($arg_a[0])) { |
| 807 | $lang = new Language($arg_a[0]); |
| 808 | if (isset($arg_a[1])) { |
| 809 | $lang->properties = $arg_a[1]; |
| 810 | } |
| 811 | } |
| 812 | return (is_object($lang) ? $lang : ''); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * Parse message text enclosed in parenthesis |
| 817 | * @param string $read |
| 818 | * @param integer $i |
| 819 | * @return integer |
| 820 | */ |
| 821 | function parseParenthesis($read, $i) { |
| 822 | for (; $read{$i} != ')'; ++$i) { |
| 823 | switch ($read{$i}) { |
| 824 | case '"': $this->parseQuote($read, $i); break; |
| 825 | case '{': $this->parseLiteral($read, $i); break; |
| 826 | case '(': $this->parseProperties($read, $i); break; |
| 827 | default: break; |
| 828 | } |
| 829 | } |
| 830 | return $i; |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Function to fill the message structure in case the |
| 835 | * bodystructure is not available |
| 836 | * NOT FINISHED YET |
| 837 | * @param string $read |
| 838 | * @param string $type0 message part type |
| 839 | * @param string $type1 message part subtype |
| 840 | * @return string (only when type0 is not message or multipart) |
| 841 | */ |
| 842 | function parseMessage($read, $type0, $type1) { |
| 843 | switch ($type0) { |
| 844 | case 'message': |
| 845 | $rfc822_header = true; |
| 846 | $mime_header = false; |
| 847 | break; |
| 848 | case 'multipart': |
| 849 | $rfc822_header = false; |
| 850 | $mime_header = true; |
| 851 | break; |
| 852 | default: return $read; |
| 853 | } |
| 854 | |
| 855 | for ($i = 1; $i < $count; ++$i) { |
| 856 | $line = trim($body[$i]); |
| 857 | if (($mime_header || $rfc822_header) && |
| 858 | (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) { |
| 859 | $bnd = $reg[1]; |
| 860 | $bndreg = $bnd; |
| 861 | $bndreg = str_replace("\\", "\\\\", $bndreg); |
| 862 | $bndreg = str_replace("?", "\\?", $bndreg); |
| 863 | $bndreg = str_replace("+", "\\+", $bndreg); |
| 864 | $bndreg = str_replace(".", "\\.", $bndreg); |
| 865 | $bndreg = str_replace("/", "\\/", $bndreg); |
| 866 | $bndreg = str_replace("-", "\\-", $bndreg); |
| 867 | $bndreg = str_replace("(", "\\(", $bndreg); |
| 868 | $bndreg = str_replace(")", "\\)", $bndreg); |
| 869 | } else if ($rfc822_header && $line == '') { |
| 870 | $rfc822_header = false; |
| 871 | if ($msg->type0 == 'multipart') { |
| 872 | $mime_header = true; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) { |
| 877 | $cnt = count($boundaries)-1; |
| 878 | $bnd = $boundaries[$cnt]['bnd']; |
| 879 | $bndreg = $boundaries[$cnt]['bndreg']; |
| 880 | |
| 881 | $regstr = '/^--'."($bndreg)".".*".'/'; |
| 882 | if (preg_match($regstr, $line, $reg)) { |
| 883 | $bndlen = strlen($reg[1]); |
| 884 | $bndend = false; |
| 885 | if (strlen($line) > ($bndlen + 3)) { |
| 886 | if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) { |
| 887 | $bndend = true; |
| 888 | } |
| 889 | } |
| 890 | if ($bndend) { |
| 891 | /* calc offset and return $msg */ |
| 892 | //$entStr = CalcEntity("$entStr", -1); |
| 893 | array_pop($boundaries); |
| 894 | $mime_header = true; |
| 895 | $bnd_end = true; |
| 896 | } else { |
| 897 | $mime_header = true; |
| 898 | $bnd_end = false; |
| 899 | //$entStr = CalcEntity("$entStr", 0); |
| 900 | ++$content_indx; |
| 901 | } |
| 902 | } else { |
| 903 | if ($header) { } |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * @param array $entity |
| 911 | * @param array $alt_order |
| 912 | * @param boolean $strict |
| 913 | * @return array |
| 914 | */ |
| 915 | function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) { |
| 916 | $found = false; |
| 917 | if ($this->type0 == 'multipart') { |
| 918 | if($this->type1 == 'alternative') { |
| 919 | $msg = $this->findAlternativeEntity($alt_order); |
| 920 | if (count($msg->entities) == 0) { |
| 921 | $entity[] = $msg->entity_id; |
| 922 | } else { |
| 923 | $entity = $msg->findDisplayEntity($entity, $alt_order, $strict); |
| 924 | } |
| 925 | $found = true; |
| 926 | } else if ($this->type1 == 'related') { /* RFC 2387 */ |
| 927 | $msgs = $this->findRelatedEntity(); |
| 928 | foreach ($msgs as $msg) { |
| 929 | if (count($msg->entities) == 0) { |
| 930 | $entity[] = $msg->entity_id; |
| 931 | } else { |
| 932 | $entity = $msg->findDisplayEntity($entity, $alt_order, $strict); |
| 933 | } |
| 934 | } |
| 935 | if (count($msgs) > 0) { |
| 936 | $found = true; |
| 937 | } |
| 938 | } else { /* Treat as multipart/mixed */ |
| 939 | foreach ($this->entities as $ent) { |
| 940 | if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') && |
| 941 | (!isset($ent->header->parameters['filename'])) && |
| 942 | (!isset($ent->header->parameters['name'])) && |
| 943 | (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) { |
| 944 | $entity = $ent->findDisplayEntity($entity, $alt_order, $strict); |
| 945 | $found = true; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | } else { /* If not multipart, then just compare with each entry from $alt_order */ |
| 950 | $type = $this->type0.'/'.$this->type1; |
| 951 | // $alt_order[] = "message/rfc822"; |
| 952 | foreach ($alt_order as $alt) { |
| 953 | if( ($alt == $type) && isset($this->entity_id) ) { |
| 954 | if ((count($this->entities) == 0) && |
| 955 | (!isset($this->header->parameters['filename'])) && |
| 956 | (!isset($this->header->parameters['name'])) && |
| 957 | isset($this->header->disposition) && is_object($this->header->disposition) && |
| 958 | !(is_object($this->header->disposition) && strtolower($this->header->disposition->name) == 'attachment')) { |
| 959 | $entity[] = $this->entity_id; |
| 960 | $found = true; |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | if(!$found) { |
| 966 | foreach ($this->entities as $ent) { |
| 967 | if(!(is_object($ent->header->disposition) && strtolower($ent->header->disposition->name) == 'attachment') && |
| 968 | (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) { |
| 969 | $entity = $ent->findDisplayEntity($entity, $alt_order, $strict); |
| 970 | $found = true; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | if(!$strict && !$found) { |
| 975 | if (($this->type0 == 'text') && |
| 976 | in_array($this->type1, array('plain', 'html', 'message')) && |
| 977 | isset($this->entity_id)) { |
| 978 | if (count($this->entities) == 0) { |
| 979 | if (!is_object($this->header->disposition) || strtolower($this->header->disposition->name) != 'attachment') { |
| 980 | $entity[] = $this->entity_id; |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | return $entity; |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * @param array $alt_order |
| 990 | * @return array |
| 991 | */ |
| 992 | function findAlternativeEntity($alt_order) { |
| 993 | /* If we are dealing with alternative parts then we */ |
| 994 | /* choose the best viewable message supported by SM. */ |
| 995 | $best_view = 0; |
| 996 | $entity = array(); |
| 997 | foreach($this->entities as $ent) { |
| 998 | $type = $ent->header->type0 . '/' . $ent->header->type1; |
| 999 | if ($type == 'multipart/related') { |
| 1000 | $type = $ent->header->getParameter('type'); |
| 1001 | // Mozilla bug. Mozilla does not provide the parameter type. |
| 1002 | if (!$type) $type = 'text/html'; |
| 1003 | } |
| 1004 | $altCount = count($alt_order); |
| 1005 | for ($j = $best_view; $j < $altCount; ++$j) { |
| 1006 | if (($alt_order[$j] == $type) && ($j >= $best_view)) { |
| 1007 | $best_view = $j; |
| 1008 | $entity = $ent; |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | return $entity; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * @return array |
| 1017 | */ |
| 1018 | function findRelatedEntity() { |
| 1019 | $msgs = array(); |
| 1020 | $related_type = $this->header->getParameter('type'); |
| 1021 | // Mozilla bug. Mozilla does not provide the parameter type. |
| 1022 | if (!$related_type) $related_type = 'text/html'; |
| 1023 | $entCount = count($this->entities); |
| 1024 | for ($i = 0; $i < $entCount; ++$i) { |
| 1025 | $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1; |
| 1026 | if ($related_type == $type) { |
| 1027 | $msgs[] = $this->entities[$i]; |
| 1028 | } |
| 1029 | } |
| 1030 | return $msgs; |
| 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * @param array $exclude_id |
| 1035 | * @param array $result |
| 1036 | * @return array |
| 1037 | */ |
| 1038 | function getAttachments($exclude_id=array(), $result = array()) { |
| 1039 | /* |
| 1040 | if (($this->type0 == 'message') && |
| 1041 | ($this->type1 == 'rfc822') && |
| 1042 | ($this->entity_id) ) { |
| 1043 | $this = $this->entities[0]; |
| 1044 | } |
| 1045 | */ |
| 1046 | if (count($this->entities)) { |
| 1047 | foreach ($this->entities as $entity) { |
| 1048 | $exclude = false; |
| 1049 | foreach ($exclude_id as $excl) { |
| 1050 | if ($entity->entity_id === $excl) { |
| 1051 | $exclude = true; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | if (!$exclude) { |
| 1056 | if (($entity->type0 == 'multipart') && |
| 1057 | ($entity->type1 != 'related')) { |
| 1058 | $result = $entity->getAttachments($exclude_id, $result); |
| 1059 | } else if ($entity->type0 != 'multipart') { |
| 1060 | $result[] = $entity; |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | } else { |
| 1065 | $exclude = false; |
| 1066 | foreach ($exclude_id as $excl) { |
| 1067 | $exclude = $exclude || ($this->entity_id == $excl); |
| 1068 | } |
| 1069 | |
| 1070 | if (!$exclude) { |
| 1071 | $result[] = $this; |
| 1072 | } |
| 1073 | } |
| 1074 | return $result; |
| 1075 | } |
| 1076 | |
| 1077 | /** |
| 1078 | * Add attachment to message object |
| 1079 | * @param string $type attachment type |
| 1080 | * @param string $name attachment name |
| 1081 | * @param string $location path to attachment |
| 1082 | */ |
| 1083 | function initAttachment($type, $name, $location) { |
| 1084 | $attachment = new Message(); |
| 1085 | $mime_header = new MessageHeader(); |
| 1086 | $mime_header->setParameter('name', $name); |
| 1087 | // FIXME: duplicate code. see ContentType class |
| 1088 | $pos = strpos($type, '/'); |
| 1089 | if ($pos > 0) { |
| 1090 | $mime_header->type0 = substr($type, 0, $pos); |
| 1091 | $mime_header->type1 = substr($type, $pos+1); |
| 1092 | } else { |
| 1093 | $mime_header->type0 = $type; |
| 1094 | } |
| 1095 | $attachment->att_local_name = $location; |
| 1096 | $disposition = new Disposition('attachment'); |
| 1097 | $disposition->properties['filename'] = $name; |
| 1098 | $mime_header->disposition = $disposition; |
| 1099 | $attachment->mime_header = $mime_header; |
| 1100 | $this->entities[]=$attachment; |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * Delete all attachments from this object from disk. |
| 1105 | * @since 1.5.1 |
| 1106 | */ |
| 1107 | function purgeAttachments() { |
| 1108 | if ($this->att_local_name && file_exists($this->att_local_name)) { |
| 1109 | unlink($this->att_local_name); |
| 1110 | } |
| 1111 | // recursively delete attachments from entities contained in this object |
| 1112 | for ($i=0, $entCount=count($this->entities);$i< $entCount; ++$i) { |
| 1113 | $this->entities[$i]->purgeAttachments(); |
| 1114 | } |
| 1115 | } |
| 1116 | } |