| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * mime.class |
| 5 | * |
| 6 | * Copyright (c) 2002 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This contains functions needed to handle mime messages. |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * rdc822_header class |
| 16 | * input: header_string or array |
| 17 | */ |
| 18 | class rfc822_header { |
| 19 | var $date = '', |
| 20 | $subject = '', |
| 21 | $from = array(), |
| 22 | $sender = '', |
| 23 | $reply_to = array(), |
| 24 | $to = array(), |
| 25 | $cc = array(), |
| 26 | $bcc = array(), |
| 27 | $in_reply_to = '', |
| 28 | $message_id = '', |
| 29 | $mime = false, |
| 30 | $content_type = '', |
| 31 | $disposition = '', |
| 32 | $xmailer = '', |
| 33 | $priority = 3, |
| 34 | $dnt = '', |
| 35 | $mlist = array(), |
| 36 | $more_headers = array(); /* only needed for constructing headers |
| 37 | in smtp.php */ |
| 38 | function parseHeader($hdr) { |
| 39 | if (is_array($hdr)) { |
| 40 | $hdr = implode('', $hdr); |
| 41 | } |
| 42 | |
| 43 | /* First we unfold the header */ |
| 44 | $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr)); |
| 45 | |
| 46 | /* Now we can make a new header array with */ |
| 47 | /* each element representing a headerline */ |
| 48 | $hdr = explode("\r\n" , $hdr); |
| 49 | foreach ($hdr as $line) { |
| 50 | $pos = strpos($line, ':'); |
| 51 | if ($pos > 0) { |
| 52 | $field = substr($line, 0, $pos); |
| 53 | $value = trim(substr($line, $pos+1)); |
| 54 | if(!preg_match('/^X.*/i', $field) && |
| 55 | !preg_match('/^Subject/i', $field)) { |
| 56 | $value = $this->stripComments($value); |
| 57 | } |
| 58 | $this->parseField($field, $value); |
| 59 | } |
| 60 | } |
| 61 | if ($this->content_type == '') { |
| 62 | $this->parseContentType('text/plain; charset=us-ascii'); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | function stripComments($value) { |
| 67 | $result = ''; |
| 68 | |
| 69 | $cnt = strlen($value); |
| 70 | for ($i = 0; $i < $cnt; $i++) { |
| 71 | switch ($value{$i}) { |
| 72 | case '"': |
| 73 | $result .= '"'; |
| 74 | while ((++$i < $cnt) && ($value{$i} != '"')) { |
| 75 | if ($value{$i} == '\\') { |
| 76 | $result .= '\\'; |
| 77 | $i++; |
| 78 | } |
| 79 | $result .= $value{$i}; |
| 80 | } |
| 81 | $result .= $value{$i}; |
| 82 | break; |
| 83 | case '(': |
| 84 | $depth = 1; |
| 85 | while (($depth > 0) && (++$i < $cnt)) { |
| 86 | switch($value{$i}) { |
| 87 | case '\\': |
| 88 | $i++; |
| 89 | break; |
| 90 | case '(': |
| 91 | $depth++; |
| 92 | break; |
| 93 | case ')': |
| 94 | $depth--; |
| 95 | break; |
| 96 | default: |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | break; |
| 101 | default: |
| 102 | $result .= $value{$i}; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | return $result; |
| 107 | } |
| 108 | |
| 109 | function parseField($field, $value) { |
| 110 | $field = strtolower($field); |
| 111 | switch($field) { |
| 112 | case 'date': |
| 113 | $d = strtr($value, array(' ' => ' ')); |
| 114 | $d = explode(' ', $d); |
| 115 | $this->date = getTimeStamp($d); |
| 116 | break; |
| 117 | case 'subject': |
| 118 | $this->subject = $value; |
| 119 | break; |
| 120 | case 'from': |
| 121 | $this->from = $this->parseAddress($value,true); |
| 122 | break; |
| 123 | case 'sender': |
| 124 | $this->sender = $this->parseAddress($value); |
| 125 | break; |
| 126 | case 'reply-to': |
| 127 | $this->reply_to = $this->parseAddress($value, true); |
| 128 | break; |
| 129 | case 'to': |
| 130 | $this->to = $this->parseAddress($value, true); |
| 131 | break; |
| 132 | case 'cc': |
| 133 | $this->cc = $this->parseAddress($value, true); |
| 134 | break; |
| 135 | case 'bcc': |
| 136 | $this->bcc = $this->parseAddress($value, true); |
| 137 | break; |
| 138 | case 'in-reply-to': |
| 139 | $this->in_reply_to = $value; |
| 140 | break; |
| 141 | case 'message-id': |
| 142 | $this->message_id = $value; |
| 143 | break; |
| 144 | case 'disposition-notification-to': |
| 145 | $this->dnt = $this->parseAddress($value); |
| 146 | break; |
| 147 | case 'mime-version': |
| 148 | $value = str_replace(' ', '', $value); |
| 149 | $this->mime = ($value == '1.0' ? true : $this->mime); |
| 150 | break; |
| 151 | case 'content-type': |
| 152 | $this->parseContentType($value); |
| 153 | break; |
| 154 | case 'content-disposition': |
| 155 | $this->parseDisposition($value); |
| 156 | break; |
| 157 | case 'user-agent': |
| 158 | case 'x-mailer': |
| 159 | $this->xmailer = $value; |
| 160 | break; |
| 161 | case 'x-priority': |
| 162 | $this->priority = $value; |
| 163 | break; |
| 164 | case 'list-post': |
| 165 | $this->mlist('post', $value); |
| 166 | break; |
| 167 | case 'list-reply': |
| 168 | $this->mlist('reply', $value); |
| 169 | break; |
| 170 | case 'list-subscribe': |
| 171 | $this->mlist('subscribe', $value); |
| 172 | break; |
| 173 | case 'list-unsubscribe': |
| 174 | $this->mlist('unsubscribe', $value); |
| 175 | break; |
| 176 | case 'list-archive': |
| 177 | $this->mlist('archive', $value); |
| 178 | break; |
| 179 | case 'list-owner': |
| 180 | $this->mlist('owner', $value); |
| 181 | break; |
| 182 | case 'list-help': |
| 183 | $this->mlist('help', $value); |
| 184 | break; |
| 185 | case 'list-id': |
| 186 | $this->mlist('id', $value); |
| 187 | break; |
| 188 | default: |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | function parseAddress |
| 194 | ($address, $ar=false, $addr_ar = array(), $group = '') { |
| 195 | $pos = 0; |
| 196 | $j = strlen($address); |
| 197 | $name = ''; |
| 198 | $addr = ''; |
| 199 | while ($pos < $j) { |
| 200 | switch ($address{$pos}) { |
| 201 | case '"': /* get the personal name */ |
| 202 | if ($address{++$pos} == '"') { |
| 203 | ++$pos; |
| 204 | } else { |
| 205 | while ($pos < $j && $address{$pos} != '"') { |
| 206 | if ((substr($address, $pos, 2) == '\\"') || |
| 207 | (substr($address, $pos, 2) == '\\\\')) { |
| 208 | $name .= $address{$pos++}; |
| 209 | } |
| 210 | $name .= $address{$pos++}; |
| 211 | } |
| 212 | } |
| 213 | ++$pos; |
| 214 | break; |
| 215 | case '<': /* get email address */ |
| 216 | $addr_start = $pos++; |
| 217 | while ($pos < $j && $address{$pos} != '>') { |
| 218 | $addr .= $address{$pos++}; |
| 219 | } |
| 220 | ++$pos; |
| 221 | break; |
| 222 | case '(': /* rip off comments */ |
| 223 | $addr_start = $pos; |
| 224 | for (++$pos; $pos < $j && $address{$pos} != ')'; ++$pos) { |
| 225 | $addr .= $address{$pos}; |
| 226 | } |
| 227 | $address_start = substr($address, 0, $addr_start); |
| 228 | $address_end = substr($address, $pos + 1); |
| 229 | $address = $address_start . $address_end; |
| 230 | $j = strlen($address); |
| 231 | $pos = $addr_start + 1; |
| 232 | break; |
| 233 | case ',': /* we reached a delimiter */ |
| 234 | if ($addr == '') { |
| 235 | $addr = substr($address, 0, $pos); |
| 236 | } else if ($name == '') { |
| 237 | $name = trim(substr($address, 0, $addr_start)); |
| 238 | } |
| 239 | |
| 240 | $at = strpos($addr, '@'); |
| 241 | $addr_structure = new address_structure(); |
| 242 | $addr_structure->personal = $name; |
| 243 | $addr_structure->group = $group; |
| 244 | if ($at) { |
| 245 | $addr_structure->mailbox = substr($addr, 0, $at); |
| 246 | $addr_structure->host = substr($addr, $at+1); |
| 247 | } else { |
| 248 | $addr_structure->mailbox = $addr; |
| 249 | } |
| 250 | $address = trim(substr($address, $pos+1)); |
| 251 | $j = strlen($address); |
| 252 | $pos = 0; |
| 253 | $name = ''; |
| 254 | $addr = ''; |
| 255 | $addr_ar[] = $addr_structure; |
| 256 | break; |
| 257 | case ':': /* process the group addresses */ |
| 258 | /* group marker */ |
| 259 | $group = substr($address, 0, $pos); |
| 260 | $address = substr($address, $pos+1); |
| 261 | $result = $this->parseAddress($address, $ar, $addr_ar, $group); |
| 262 | $addr_ar = $result[0]; |
| 263 | $pos = $result[1]; |
| 264 | $address = substr($address, $pos++); |
| 265 | $j = strlen($address); |
| 266 | $group = ''; |
| 267 | break; |
| 268 | case ';': |
| 269 | if ($group) { |
| 270 | $address = substr($address, 0, $pos - 1); |
| 271 | } |
| 272 | ++$pos; |
| 273 | break; |
| 274 | default: |
| 275 | ++$pos; |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | if ($addr == '') { |
| 280 | $addr = substr($address, 0, $pos); |
| 281 | } else if ($name == '') { |
| 282 | $name = trim(substr($address, 0, $addr_start)); |
| 283 | } |
| 284 | $at = strpos($addr, '@'); |
| 285 | $addr_structure = new address_structure(); |
| 286 | $addr_structure->group = $group; |
| 287 | if ($at) { |
| 288 | $addr_structure->mailbox = trim(substr($addr, 0, $at)); |
| 289 | $addr_structure->host = trim(substr($addr, $at+1)); |
| 290 | } else { |
| 291 | $addr_structure->mailbox = trim($addr); |
| 292 | } |
| 293 | if ($group && $addr == '') { /* no addresses found in group */ |
| 294 | $name = "$group: Undisclosed recipients;"; |
| 295 | $addr_structure->personal = $name; |
| 296 | $addr_ar[] = $addr_structure; |
| 297 | return (array($addr_ar, $pos+1)); |
| 298 | } else { |
| 299 | $addr_structure->personal = $name; |
| 300 | if ($name || $addr) { |
| 301 | $addr_ar[] = $addr_structure; |
| 302 | } |
| 303 | } |
| 304 | if ($ar) { |
| 305 | return ($addr_ar); |
| 306 | } |
| 307 | return ($addr_ar[0]); |
| 308 | } |
| 309 | |
| 310 | function parseContentType($value) { |
| 311 | $pos = strpos($value, ';'); |
| 312 | $props = ''; |
| 313 | if ($pos > 0) { |
| 314 | $type = trim(substr($value, 0, $pos)); |
| 315 | $props = trim(substr($type, $pos+1)); |
| 316 | } else { |
| 317 | $type = $value; |
| 318 | } |
| 319 | $content_type = new content_type($type); |
| 320 | if ($props) { |
| 321 | $properties = $this->parseProperties($props); |
| 322 | if (!isset($properties['charset'])) { |
| 323 | $properties['charset'] = 'us-ascii'; |
| 324 | } |
| 325 | $content_type->properties = $this->parseProperties($props); |
| 326 | } |
| 327 | $this->content_type = $content_type; |
| 328 | } |
| 329 | |
| 330 | function parseProperties($value) { |
| 331 | $propArray = explode(';', $value); |
| 332 | $propResultArray = array(); |
| 333 | foreach ($propArray as $prop) { |
| 334 | $prop = trim($prop); |
| 335 | $pos = strpos($prop, '='); |
| 336 | if ($pos > 0) { |
| 337 | $key = trim(substr($prop, 0, $pos)); |
| 338 | $val = trim(substr($prop, $pos+1)); |
| 339 | if ($val{0} == '"') { |
| 340 | $val = substr($val, 1, -1); |
| 341 | } |
| 342 | $propResultArray[$key] = $val; |
| 343 | } |
| 344 | } |
| 345 | return $propResultArray; |
| 346 | } |
| 347 | |
| 348 | function parseDisposition($value) { |
| 349 | $pos = strpos($value, ';'); |
| 350 | $props = ''; |
| 351 | if ($pos > 0) { |
| 352 | $name = trim(substr($value, 0, $pos)); |
| 353 | $props = trim(substr($type, $pos+1)); |
| 354 | } else { |
| 355 | $name = $value; |
| 356 | } |
| 357 | $props_a = $this->parseProperties($props); |
| 358 | $disp = new disposition($name); |
| 359 | $disp->properties = $props_a; |
| 360 | $this->disposition = $disp; |
| 361 | } |
| 362 | |
| 363 | function mlist($field, $value) { |
| 364 | $res_a = array(); |
| 365 | $value_a = explode(',', $value); |
| 366 | foreach ($value_a as $val) { |
| 367 | $val = trim($val); |
| 368 | if ($val{0} == '<') { |
| 369 | $val = substr($val, 1, -1); |
| 370 | } |
| 371 | if (substr($val, 0, 7) == 'mailto:') { |
| 372 | $res_a['mailto'] = substr($val, 7); |
| 373 | } else { |
| 374 | $res_a['href'] = $val; |
| 375 | } |
| 376 | } |
| 377 | $this->mlist[$field] = $res_a; |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * function to get the addres strings out of the header. |
| 382 | * Arguments: string or array of strings ! |
| 383 | * example1: header->getAddr_s('to'). |
| 384 | * example2: header->getAddr_s(array('to', 'cc', 'bcc')) |
| 385 | */ |
| 386 | function getAddr_s($arr, $separator = ',') { |
| 387 | $s = ''; |
| 388 | |
| 389 | if (is_array($arr)) { |
| 390 | foreach($arr as $arg) { |
| 391 | if ($this->getAddr_s($arg)) { |
| 392 | $s .= $separator . $result; |
| 393 | } |
| 394 | } |
| 395 | $s = ($s ? substr($s, 2) : $s); |
| 396 | } else { |
| 397 | eval('$addr = $this->' . $arr . ';') ; |
| 398 | if (is_array($addr)) { |
| 399 | foreach ($addr as $addr_o) { |
| 400 | if (is_object($addr_o)) { |
| 401 | $s .= $addr_o->getAddress() . $separator; |
| 402 | } |
| 403 | } |
| 404 | $s = substr($s, 0, -strlen($separator)); |
| 405 | } else { |
| 406 | if (is_object($addr)) { |
| 407 | $s .= $addr->getAddress(); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | return $s; |
| 412 | } |
| 413 | |
| 414 | function getAddr_a($arg, $excl_arr = array(), $arr = array()) { |
| 415 | if (is_array($arg)) { |
| 416 | foreach($arg as $argument) { |
| 417 | $arr = $this->getAddr_a($argument, $excl_arr, $arr); |
| 418 | } |
| 419 | } else { |
| 420 | eval('$addr = $this->' . $arg . ';') ; |
| 421 | if (is_array($addr)) { |
| 422 | foreach ($addr as $next_addr) { |
| 423 | if (is_object($next_addr)) { |
| 424 | if (isset($next_addr->host) && ($next_addr->host != '')) { |
| 425 | $email = $next_addr->mailbox . '@' . $next_addr->host; |
| 426 | } else { |
| 427 | $email = $next_addr->mailbox; |
| 428 | } |
| 429 | $email = strtolower($email); |
| 430 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
| 431 | $arr[$email] = $next_addr->personal; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | } else { |
| 436 | if (is_object($addr)) { |
| 437 | $email = $addr->mailbox; |
| 438 | $email .= (isset($addr->host) ? '@' . $addr->host : ''); |
| 439 | $email = strtolower($email); |
| 440 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
| 441 | $arr[$email] = $addr->personal; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | return $arr; |
| 447 | } |
| 448 | |
| 449 | function getContentType($type0, $type1) { |
| 450 | $type0 = $this->content_type->type0; |
| 451 | $type1 = $this->content_type->type1; |
| 452 | return $this->content_type->properties; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | class msg_header { |
| 457 | /** msg_header contains all variables available in a bodystructure **/ |
| 458 | /** entity like described in rfc2060 **/ |
| 459 | |
| 460 | var $type0 = '', |
| 461 | $type1 = '', |
| 462 | $parameters = array(), |
| 463 | $id = 0, |
| 464 | $description = '', |
| 465 | $encoding='', |
| 466 | $size = 0, |
| 467 | $md5='', |
| 468 | $disposition = '', |
| 469 | $language=''; |
| 470 | |
| 471 | /* |
| 472 | * returns addres_list of supplied argument |
| 473 | * arguments: array('to', 'from', ...) or just a string like 'to'. |
| 474 | * result: string: address1, addres2, .... |
| 475 | */ |
| 476 | |
| 477 | function setVar($var, $value) { |
| 478 | $this->{$var} = $value; |
| 479 | } |
| 480 | |
| 481 | function getParameter($p) { |
| 482 | $value = strtolower($p); |
| 483 | return (isset($this->parameters[$p]) ? $this->parameters[$p] : ''); |
| 484 | } |
| 485 | |
| 486 | function setParameter($parameter, $value) { |
| 487 | $this->parameters[strtolower($parameter)] = $value; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
| 492 | |
| 493 | class address_structure { |
| 494 | var $personal = '', |
| 495 | $adl = '', |
| 496 | $mailbox = '', |
| 497 | $host = '', |
| 498 | $group = ''; |
| 499 | |
| 500 | function getAddress($full = true) { |
| 501 | $result = ''; |
| 502 | |
| 503 | if (is_object($this)) { |
| 504 | if (isset($this->host) && ($this->host != '')) { |
| 505 | $email = $this->mailbox.'@'.$this->host; |
| 506 | } else { |
| 507 | $email = $this->mailbox; |
| 508 | } |
| 509 | if (trim($this->personal) != '') { |
| 510 | if ($email) { |
| 511 | $addr = '"' . $this->personal . '" <' .$email.'>'; |
| 512 | } else { |
| 513 | $addr = $this->personal; |
| 514 | } |
| 515 | $best_dpl = $this->personal; |
| 516 | } else { |
| 517 | $addr = $email; |
| 518 | $best_dpl = $email; |
| 519 | } |
| 520 | $result = ($full ? $addr : $best_dpl); |
| 521 | } |
| 522 | return $result; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | class message { |
| 527 | /** message is the object that contains messages. It is a recursive |
| 528 | object in that through the $entities variable, it can contain |
| 529 | more objects of type message. See documentation in mime.txt for |
| 530 | a better description of how this works. |
| 531 | **/ |
| 532 | var $rfc822_header = '', |
| 533 | $mime_header = '', |
| 534 | $flags = '', |
| 535 | $type0='', |
| 536 | $type1='', |
| 537 | $entities = array(), |
| 538 | $parent_ent, $entity, |
| 539 | $parent = '', $decoded_body='', |
| 540 | $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0, |
| 541 | $is_mdnsent = 0, |
| 542 | $body_part = '', |
| 543 | $offset = 0, /* for fetching body parts out of raw messages */ |
| 544 | $length = 0; /* for fetching body parts out of raw messages */ |
| 545 | |
| 546 | function setEnt($ent) { |
| 547 | $this->entity_id= $ent; |
| 548 | } |
| 549 | |
| 550 | function addEntity ($msg) { |
| 551 | $msg->parent = &$this; |
| 552 | $this->entities[] = $msg; |
| 553 | } |
| 554 | |
| 555 | function getFilename() { |
| 556 | $filename = ''; |
| 557 | |
| 558 | if (is_object($this->header->disposition)) { |
| 559 | $filename = $this->header->disposition->getproperty('filename'); |
| 560 | if (!$filename) { |
| 561 | $filename = $this->header->disposition->getproperty('name'); |
| 562 | } |
| 563 | } |
| 564 | if (!$filename) { |
| 565 | $filename = 'untitled-'.$this->entity_id; |
| 566 | } |
| 567 | return $filename; |
| 568 | } |
| 569 | |
| 570 | |
| 571 | function addRFC822Header($read) { |
| 572 | $header = new rfc822_header(); |
| 573 | $this->rfc822_header = $header->parseHeader($read); |
| 574 | } |
| 575 | |
| 576 | function getEntity($ent) { |
| 577 | $cur_ent = $this->entity_id; |
| 578 | $msg = $this; |
| 579 | if (($cur_ent == '') || ($cur_ent == '0')) { |
| 580 | $cur_ent_a = array(); |
| 581 | } else { |
| 582 | $cur_ent_a = explode('.', $this->entity_id); |
| 583 | } |
| 584 | $ent_a = explode('.', $ent); |
| 585 | |
| 586 | $cnt = count($ent_a); |
| 587 | |
| 588 | for ($i = 0; $i < $cnt -1; ++$i) { |
| 589 | if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) { |
| 590 | $msg = $msg->parent; |
| 591 | $cur_ent_a = explode('.', $msg->entity_id); |
| 592 | --$i; |
| 593 | } else if (!isset($cur_ent_a[$i])) { |
| 594 | if (isset($msg->entities[($ent_a[$i]-1)])) { |
| 595 | $msg = $msg->entities[($ent_a[$i]-1)]; |
| 596 | } else { |
| 597 | $msg = $msg->entities[0]; |
| 598 | } |
| 599 | } |
| 600 | if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) { |
| 601 | /*this is a header for a message/rfc822 entity */ |
| 602 | $msg = $msg->entities[0]; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) { |
| 607 | /*this is a header for a message/rfc822 entity */ |
| 608 | $msg = $msg->entities[0]; |
| 609 | } |
| 610 | |
| 611 | if (isset($msg->entities[($ent_a[$cnt-1])-1])) { |
| 612 | if (is_object($msg->entities[($ent_a[$cnt-1])-1])) { |
| 613 | $msg = $msg->entities[($ent_a[$cnt-1]-1)]; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return $msg; |
| 618 | } |
| 619 | |
| 620 | function setBody($s) { |
| 621 | $this->body_part = $s; |
| 622 | } |
| 623 | |
| 624 | function clean_up() { |
| 625 | $msg = $this; |
| 626 | $msg->body_part = ''; |
| 627 | |
| 628 | foreach ($msg->entities as $m) { |
| 629 | $m->clean_up(); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | function getMailbox() { |
| 634 | $msg = $this; |
| 635 | while (is_object($msg->parent)) { |
| 636 | $msg = $msg->parent; |
| 637 | } |
| 638 | return $msg->mailbox; |
| 639 | } |
| 640 | |
| 641 | function calcEntity($msg) { |
| 642 | if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) { |
| 643 | $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */ |
| 644 | } else if (isset($this->entity_id) && ($this->entity_id != '')) { |
| 645 | $ent_no = count($this->entities)+1; |
| 646 | $par_ent = substr($this->entity_id, -2); |
| 647 | if ($par_ent{0} == '.') { |
| 648 | $par_ent = $par_ent{1}; |
| 649 | } |
| 650 | if ($par_ent == '0') { |
| 651 | $ent_no = count($this->entities) + 1; |
| 652 | if ($ent_no > 0) { |
| 653 | $ent = substr($this->entity_id, 0, strrpos($this->entity_id, '.')); |
| 654 | $ent = ($ent ? $ent . ".$ent_no" : $ent_no); |
| 655 | $msg->entity_id = $ent; |
| 656 | } else { |
| 657 | $msg->entity_id = $ent_no; |
| 658 | } |
| 659 | } else { |
| 660 | $ent = $this->entity_id . ".$ent_no"; |
| 661 | $msg->entity_id = $ent; |
| 662 | } |
| 663 | } else { |
| 664 | $msg->entity_id = '0'; |
| 665 | } |
| 666 | |
| 667 | return $msg->entity_id; |
| 668 | } |
| 669 | |
| 670 | |
| 671 | /* |
| 672 | * Bodystructure parser, a recursive function for generating the |
| 673 | * entity-tree with all the mime-parts. |
| 674 | * |
| 675 | * It follows RFC2060 and stores all the described fields in the |
| 676 | * message object. |
| 677 | * |
| 678 | * Question/Bugs: |
| 679 | * |
| 680 | * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net. |
| 681 | * |
| 682 | */ |
| 683 | function parseStructure($read, $i = 0) { |
| 684 | $arg_no = 0; |
| 685 | $arg_a = array(); |
| 686 | $cnt = strlen($read); |
| 687 | |
| 688 | for (; $i < $cnt; ++$i) { |
| 689 | $char = strtoupper($read{$i}); |
| 690 | switch ($char) { |
| 691 | case '(': |
| 692 | if ($arg_no == 0) { |
| 693 | if (!isset($msg)) { |
| 694 | $msg = new message(); |
| 695 | $hdr = new msg_header(); |
| 696 | $hdr->type0 = 'text'; |
| 697 | $hdr->type1 = 'plain'; |
| 698 | $hdr->encoding = 'us-ascii'; |
| 699 | $msg->entity_id = $this->calcEntity($msg); |
| 700 | } else { |
| 701 | $msg->header->type0 = 'multipart'; |
| 702 | $msg->type0 = 'multipart'; |
| 703 | while ($read{$i} == '(') { |
| 704 | $res = $msg->parseStructure($read, $i); |
| 705 | $i = $res[1]; |
| 706 | $msg->addEntity($res[0]); |
| 707 | } |
| 708 | } |
| 709 | } else { |
| 710 | switch ($arg_no) { |
| 711 | case 1: |
| 712 | /* multipart properties */ |
| 713 | ++$i; |
| 714 | $res = $this->parseProperties($read, $i); |
| 715 | $arg_a[] = $res[0]; |
| 716 | $i = $res[1]; |
| 717 | ++$arg_no; |
| 718 | break; |
| 719 | case 2: |
| 720 | if (isset($msg->type0) && ($msg->type0 == 'multipart')) { |
| 721 | ++$i; |
| 722 | $res = $msg->parseDisposition($read, $i); |
| 723 | $arg_a[] = $res[0]; |
| 724 | $i = $res[1]; |
| 725 | } else { /* properties */ |
| 726 | $res = $msg->parseProperties($read, $i); |
| 727 | $arg_a[] = $res[0]; |
| 728 | $i = $res[1]; |
| 729 | } |
| 730 | ++$arg_no; |
| 731 | break; |
| 732 | case 3: |
| 733 | if (isset($msg->type0) && ($msg->type0 == 'multipart')) { |
| 734 | ++$i; |
| 735 | $res= $msg->parseLanguage($read, $i); |
| 736 | $arg_a[] = $res[0]; |
| 737 | $i = $res[1]; |
| 738 | } |
| 739 | case 7: |
| 740 | if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) { |
| 741 | $msg->header->type0 = $arg_a[0]; |
| 742 | $msg->type0 = $arg_a[0]; |
| 743 | $msg->header->type1 = $arg_a[1]; |
| 744 | $msg->type1 = $arg_a[1]; |
| 745 | $rfc822_hdr = new rfc822_header(); |
| 746 | $res = $msg->parseEnvelope($read, $i, $rfc822_hdr); |
| 747 | $i = $res[1] + 1; |
| 748 | $msg->rfc822_header = $res[0]; |
| 749 | while (($i < $cnt) && ($read{$i} != '(')) { |
| 750 | ++$i; |
| 751 | } |
| 752 | $res = $msg->parseStructure($read, $i); |
| 753 | $i = $res[1]; |
| 754 | $msg->addEntity($res[0]); |
| 755 | } |
| 756 | break; |
| 757 | case 8: |
| 758 | ++$i; |
| 759 | $res = $msg->parseDisposition($read, $i); |
| 760 | $arg_a[] = $res[0]; |
| 761 | $i = $res[1]; |
| 762 | ++$arg_no; |
| 763 | break; |
| 764 | case 9: |
| 765 | ++$i; |
| 766 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
| 767 | $res = $msg->parseDisposition($read, $i); |
| 768 | } else { |
| 769 | $res = $msg->parseLanguage($read, $i); |
| 770 | } |
| 771 | $arg_a[] = $res[0]; |
| 772 | $i = $res[1]; |
| 773 | ++$arg_no; |
| 774 | break; |
| 775 | case 10: |
| 776 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
| 777 | ++$i; |
| 778 | $res = $msg->parseLanguage($read, $i); |
| 779 | $arg_a[] = $res[0]; |
| 780 | $i = $res[1]; |
| 781 | } else { |
| 782 | $i = $msg->parseParenthesis($read, $i); |
| 783 | $arg_a[] = ''; /* not yet desribed in rfc2060 */ |
| 784 | } |
| 785 | ++$arg_no; |
| 786 | break; |
| 787 | default: |
| 788 | /* unknown argument, skip this part */ |
| 789 | $i = $msg->parseParenthesis($read, $i); |
| 790 | $arg_a[] = ''; |
| 791 | ++$arg_no; |
| 792 | break; |
| 793 | } /* switch */ |
| 794 | } |
| 795 | break; |
| 796 | case '"': |
| 797 | /* inside an entity -> start processing */ |
| 798 | $debug = substr($read, $i, 20); |
| 799 | $res = $msg->parseQuote($read, $i); |
| 800 | $arg_s = $res[0]; |
| 801 | $i = $res[1]; |
| 802 | ++$arg_no; |
| 803 | if ($arg_no < 3) { |
| 804 | $arg_s = strtolower($arg_s); /* type0 and type1 */ |
| 805 | } |
| 806 | $arg_a[] = $arg_s; |
| 807 | break; |
| 808 | case 'n': |
| 809 | case 'N': |
| 810 | /* probably NIL argument */ |
| 811 | if (strtoupper(substr($read, $i, 4)) == 'NIL ') { |
| 812 | $arg_a[] = ''; |
| 813 | ++$arg_no; |
| 814 | $i += 2; |
| 815 | } |
| 816 | break; |
| 817 | case '{': |
| 818 | /* process the literal value */ |
| 819 | $res = $msg->parseLiteral($read, $i); |
| 820 | $arg_s = $res[0]; |
| 821 | $i = $res[1]; |
| 822 | ++$arg_no; |
| 823 | break; |
| 824 | case is_numeric($read{$i}): |
| 825 | /* process integers */ |
| 826 | if ($read{$i} == ' ') { break; } |
| 827 | $arg_s = $read{$i};; |
| 828 | for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) { |
| 829 | $arg_s .= $read{$i}; |
| 830 | } |
| 831 | ++$arg_no; |
| 832 | $arg_a[] = $arg_s; |
| 833 | break; |
| 834 | case ')': |
| 835 | $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart')); |
| 836 | if (!$multipart) { |
| 837 | $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))); |
| 838 | $hdr->type0 = $arg_a[0]; |
| 839 | $hdr->type1 = $arg_a[1]; |
| 840 | |
| 841 | $msg->type0 = $arg_a[0]; |
| 842 | $msg->type1 = $arg_a[1]; |
| 843 | |
| 844 | $arr = $arg_a[2]; |
| 845 | if (is_array($arr)) { |
| 846 | $hdr->parameters = $arg_a[2]; |
| 847 | } |
| 848 | $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3])); |
| 849 | $hdr->description = $arg_a[4]; |
| 850 | $hdr->encoding = strtolower($arg_a[5]); |
| 851 | $hdr->entity_id = $msg->entity_id; |
| 852 | $hdr->size = $arg_a[6]; |
| 853 | if ($shifted_args) { |
| 854 | $hdr->lines = $arg_a[7]; |
| 855 | $s = 1; |
| 856 | } else { |
| 857 | $s = 0; |
| 858 | } |
| 859 | $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5); |
| 860 | $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition); |
| 861 | $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language); |
| 862 | $msg->header = $hdr; |
| 863 | // $arg_no = 0; |
| 864 | ++$i; |
| 865 | if ((substr($msg->entity_id, -2) == '.0') && ($msg->type0 !='multipart')) { |
| 866 | $msg->entity_id++; |
| 867 | } |
| 868 | } else { |
| 869 | $hdr->type0 = 'multipart'; |
| 870 | $hdr->type1 = $arg_a[0]; |
| 871 | $msg->type0 = 'multipart'; |
| 872 | $msg->type1 = $arg_a[0]; |
| 873 | $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters); |
| 874 | $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition); |
| 875 | $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language); |
| 876 | $msg->header = $hdr; |
| 877 | } |
| 878 | return (array($msg, $i)); |
| 879 | default: break; |
| 880 | } /* switch */ |
| 881 | |
| 882 | } /* for */ |
| 883 | } /* parsestructure */ |
| 884 | |
| 885 | function parseProperties($read, $i) { |
| 886 | $properties = array(); |
| 887 | $prop_name = ''; |
| 888 | $arg_s = ''; |
| 889 | |
| 890 | for (; $read{$i} != ')'; ++$i) { |
| 891 | if ($read{$i} == '"') { |
| 892 | $res = $this->parseQuote($read, $i); |
| 893 | $arg_s = $res[0]; |
| 894 | $i = $res[1]; |
| 895 | } else if ($read{$i} == '{') { |
| 896 | $res = $this->parseLiteral($read, $i); |
| 897 | $arg_s = $res[0]; |
| 898 | $i = $res[1]; |
| 899 | } |
| 900 | |
| 901 | if (($prop_name == '') && $arg_s) { |
| 902 | $prop_name = strtolower($arg_s); |
| 903 | $properties[$prop_name] = ''; |
| 904 | $arg_s = ''; |
| 905 | } else if (($prop_name != '') && ($arg_s != '')) { |
| 906 | $properties[$prop_name] = $arg_s; |
| 907 | $prop_name = ''; |
| 908 | $arg_s = ''; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | return (array($properties, $i)); |
| 913 | } |
| 914 | |
| 915 | function parseEnvelope($read, $i, $hdr) { |
| 916 | $arg_no = 0; |
| 917 | $arg_a = array(); |
| 918 | $cnt = strlen($read); |
| 919 | |
| 920 | for (; ($i < $cnt) && ($read{$i} != ')'); ++$i) { |
| 921 | ++$i; |
| 922 | $char = strtoupper($read{$i}); |
| 923 | switch ($char) { |
| 924 | case '"': |
| 925 | $res = $this->parseQuote($read, $i); |
| 926 | $arg_a[] = $res[0]; |
| 927 | $i = $res[1]; |
| 928 | ++$arg_no; |
| 929 | break; |
| 930 | case '{': |
| 931 | $res = $this->parseLiteral($read, $i); |
| 932 | $arg_a[] = $res[0]; |
| 933 | $i = $res[1]; |
| 934 | ++$arg_no; |
| 935 | break; |
| 936 | case 'N': |
| 937 | /* probably NIL argument */ |
| 938 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
| 939 | $arg_a[] = ''; |
| 940 | ++$arg_no; |
| 941 | $i += 2; |
| 942 | } |
| 943 | break; |
| 944 | case '(': |
| 945 | /* Address structure (with group support) |
| 946 | * Note: Group support is useless on SMTP connections |
| 947 | * because the protocol doesn't support it |
| 948 | */ |
| 949 | $addr_a = array(); |
| 950 | $group = ''; |
| 951 | $a=0; |
| 952 | for (; $i < $cnt && $read{$i} != ')'; ++$i) { |
| 953 | if ($read{$i} == '(') { |
| 954 | $res = $this->parseAddress($read, $i); |
| 955 | $addr = $res[0]; |
| 956 | $i = $res[1]; |
| 957 | if (($addr->host == '') && ($addr->mailbox != '')) { |
| 958 | /* start of group */ |
| 959 | $group = $addr->mailbox; |
| 960 | $group_addr = $addr; |
| 961 | $j = $a; |
| 962 | } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) { |
| 963 | /* end group */ |
| 964 | if ($a == ($j+1)) { /* no group members */ |
| 965 | $group_addr->group = $group; |
| 966 | $group_addr->mailbox = ''; |
| 967 | $group_addr->personal = "$group: Undisclosed recipients;"; |
| 968 | $addr_a[] = $group_addr; |
| 969 | $group =''; |
| 970 | } |
| 971 | } else { |
| 972 | $addr->group = $group; |
| 973 | $addr_a[] = $addr; |
| 974 | } |
| 975 | ++$a; |
| 976 | } |
| 977 | } |
| 978 | $arg_a[] = $addr_a; |
| 979 | break; |
| 980 | default: break; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | if (count($arg_a) > 9) { |
| 985 | /* argument 1: date */ |
| 986 | $d = strtr($arg_a[0], array(' ' => ' ')); |
| 987 | $d = explode(' ', $d); |
| 988 | $hdr->date = getTimeStamp($d); |
| 989 | |
| 990 | /* argument 2: subject */ |
| 991 | $arg_a[1] = (!trim($arg_a[1]) ? _("(no subject)") : $arg_a[1]); |
| 992 | $hdr->subject = $arg_a[1]; |
| 993 | |
| 994 | $hdr->from = $arg_a[2][0]; /* argument 3: from */ |
| 995 | $hdr->sender = $arg_a[3][0]; /* argument 4: sender */ |
| 996 | $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */ |
| 997 | $hdr->to = $arg_a[5]; /* argument 6: to */ |
| 998 | $hdr->cc = $arg_a[6]; /* argument 7: cc */ |
| 999 | $hdr->bcc = $arg_a[7]; /* argument 8: bcc */ |
| 1000 | $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */ |
| 1001 | $hdr->message_id = $arg_a[9]; /* argument 10: message-id */ |
| 1002 | } |
| 1003 | return (array($hdr, $i)); |
| 1004 | } |
| 1005 | |
| 1006 | function parseLiteral($read, $i) { |
| 1007 | $lit_cnt = ''; |
| 1008 | for (++$i; $read{$i} != '}'; ++$i) { |
| 1009 | $lit_cnt .= $read{$i}; |
| 1010 | } |
| 1011 | |
| 1012 | $lit_cnt +=2; /* add the { and } characters */ |
| 1013 | $s = ''; |
| 1014 | for ($j = 0; $j < $lit_cnt; ++$j) { |
| 1015 | $s .= $read{++$i}; |
| 1016 | } |
| 1017 | return (array($s, $i)); |
| 1018 | } |
| 1019 | |
| 1020 | function parseQuote($read, $i) { |
| 1021 | $s = ''; |
| 1022 | for (++$i; $read{$i} != '"'; ++$i) { |
| 1023 | if ($read{$i} == '\\') { |
| 1024 | ++$i; |
| 1025 | } |
| 1026 | $s .= $read{$i}; |
| 1027 | } |
| 1028 | return (array($s, $i)); |
| 1029 | } |
| 1030 | |
| 1031 | function parseAddress($read, $i) { |
| 1032 | $arg_a = array(); |
| 1033 | |
| 1034 | for (; $read{$i} != ')'; ++$i) { |
| 1035 | $char = strtoupper($read{$i}); |
| 1036 | switch ($char) { |
| 1037 | case '"': |
| 1038 | case '{': |
| 1039 | $res = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i)); |
| 1040 | $arg_a[] = $res[0]; |
| 1041 | $i = $res[1]; |
| 1042 | break; |
| 1043 | case 'n': |
| 1044 | case 'N': |
| 1045 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
| 1046 | $arg_a[] = ''; |
| 1047 | $i += 2; |
| 1048 | } |
| 1049 | break; |
| 1050 | default: break; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | if (count($arg_a) == 4) { |
| 1055 | $adr = new address_structure(); |
| 1056 | $adr->personal = $arg_a[0]; |
| 1057 | $adr->adl = $arg_a[1]; |
| 1058 | $adr->mailbox = $arg_a[2]; |
| 1059 | $adr->host = $arg_a[3]; |
| 1060 | } else { |
| 1061 | $adr = ''; |
| 1062 | } |
| 1063 | return (array($adr, $i)); |
| 1064 | } |
| 1065 | |
| 1066 | function parseDisposition($read, $i) { |
| 1067 | $arg_a = array(); |
| 1068 | |
| 1069 | for (; $read{$i} != ')'; ++$i) { |
| 1070 | switch ($read{$i}) { |
| 1071 | case '"': |
| 1072 | case '{': |
| 1073 | case '(': |
| 1074 | switch ($read{$i}) { |
| 1075 | case '"': $res = $this->parseQuote($read, $i); break; |
| 1076 | case '{': $res = $this->parseLiteral($read, $i); break; |
| 1077 | case '(': $res = $this->parseProperties($read, $i); break; |
| 1078 | } |
| 1079 | $arg_a[] = $res[0]; |
| 1080 | $i = $res[1]; |
| 1081 | break; |
| 1082 | default: break; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | if (isset($arg_a[0])) { |
| 1087 | $disp = new disposition($arg_a[0]); |
| 1088 | if (isset($arg_a[1])) { |
| 1089 | $disp->properties = $arg_a[1]; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | return (is_object($disp) ? array($disp, $i) : array('', $i)); |
| 1094 | } |
| 1095 | |
| 1096 | function parseLanguage($read, $i) { |
| 1097 | /* no idea how to process this one without examples */ |
| 1098 | $arg_a = array(); |
| 1099 | |
| 1100 | for (; $read{$i} != ')'; ++$i) { |
| 1101 | switch ($read{$i}) { |
| 1102 | case '"': |
| 1103 | case '{': |
| 1104 | case '(': |
| 1105 | switch ($read{$i}) { |
| 1106 | case '"': $res = $this->parseQuote($read, $i); break; |
| 1107 | case '{': $res = $this->parseLiteral($read, $i); break; |
| 1108 | case '(': $res = $this->parseProperties($read, $i); break; |
| 1109 | } |
| 1110 | $arg_a[] = $res[0]; |
| 1111 | $i = $res[1]; |
| 1112 | break; |
| 1113 | default: break; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | if (isset($arg_a[0])) { |
| 1118 | $lang = new language($arg_a[0]); |
| 1119 | if (isset($arg_a[1])) { |
| 1120 | $lang->properties = $arg_a[1]; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | return (is_object($lang) ? array($lang, $i) : array('', $i)); |
| 1125 | } |
| 1126 | |
| 1127 | function parseParenthesis($read, $i) { |
| 1128 | for (; $read{$i} != ')'; ++$i) { |
| 1129 | switch ($read{$i}) { |
| 1130 | case '"': |
| 1131 | case '{': |
| 1132 | case '(': |
| 1133 | switch ($read{$i}) { |
| 1134 | case '"': $res = $this->parseQuote($read, $i); break; |
| 1135 | case '{': $res = $this->parseLiteral($read, $i); break; |
| 1136 | case '(': $res = $this->parseProperties($read, $i); break; |
| 1137 | } |
| 1138 | $i = $res[1]; |
| 1139 | break; |
| 1140 | default: break; |
| 1141 | } |
| 1142 | } |
| 1143 | return $i; |
| 1144 | } |
| 1145 | |
| 1146 | /* Function to fill the message structure in case the */ |
| 1147 | /* bodystructure is not available NOT FINISHED YET */ |
| 1148 | function parseMessage($read, $type0, $type1) { |
| 1149 | switch ($type0) { |
| 1150 | case 'message': |
| 1151 | $rfc822_header = true; |
| 1152 | $mime_header = false; |
| 1153 | break; |
| 1154 | case 'multipart': |
| 1155 | $rfc822_header = false; |
| 1156 | $mime_header = true; |
| 1157 | break; |
| 1158 | default: return $read; |
| 1159 | } |
| 1160 | |
| 1161 | for ($i = 1; $i < $count; ++$i) { |
| 1162 | $line = trim($body[$i]); |
| 1163 | if (($mime_header || $rfc822_header) && |
| 1164 | (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) { |
| 1165 | $bnd = $reg[1]; |
| 1166 | $bndreg = $bnd; |
| 1167 | $bndreg = str_replace("\\", "\\\\", $bndreg); |
| 1168 | $bndreg = str_replace("?", "\\?", $bndreg); |
| 1169 | $bndreg = str_replace("+", "\\+", $bndreg); |
| 1170 | $bndreg = str_replace(".", "\\.", $bndreg); |
| 1171 | $bndreg = str_replace("/", "\\/", $bndreg); |
| 1172 | $bndreg = str_replace("-", "\\-", $bndreg); |
| 1173 | $bndreg = str_replace("(", "\\(", $bndreg); |
| 1174 | $bndreg = str_replace(")", "\\)", $bndreg); |
| 1175 | } else if ($rfc822_header && $line == '') { |
| 1176 | $rfc822_header = false; |
| 1177 | if ($msg->type0 == 'multipart') { |
| 1178 | $mime_header = true; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) { |
| 1183 | $cnt=count($boundaries)-1; |
| 1184 | $bnd = $boundaries[$cnt]['bnd']; |
| 1185 | $bndreg = $boundaries[$cnt]['bndreg']; |
| 1186 | |
| 1187 | $regstr = '/^--'."($bndreg)".".*".'/'; |
| 1188 | if (preg_match($regstr, $line, $reg)) { |
| 1189 | $bndlen = strlen($reg[1]); |
| 1190 | $bndend = false; |
| 1191 | if (strlen($line) > ($bndlen + 3)) { |
| 1192 | if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) { |
| 1193 | $bndend = true; |
| 1194 | } |
| 1195 | } |
| 1196 | if ($bndend) { |
| 1197 | /* calc offset and return $msg */ |
| 1198 | //$entStr = CalcEntity("$entStr", -1); |
| 1199 | array_pop($boundaries); |
| 1200 | $mime_header = true; |
| 1201 | $bnd_end = true; |
| 1202 | } else { |
| 1203 | $mime_header = true; |
| 1204 | $bnd_end = false; |
| 1205 | //$entStr = CalcEntity("$entStr", 0); |
| 1206 | ++$content_indx; |
| 1207 | } |
| 1208 | } else { |
| 1209 | if ($header) { } |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html')) { |
| 1216 | $found = false; |
| 1217 | $type = $this->type0.'/'.$this->type1; |
| 1218 | if ($type == 'multipart/alternative') { |
| 1219 | $msg = $this->findAlternativeEntity($alt_order); |
| 1220 | if (count($msg->entities) == 0) { |
| 1221 | $entity[] = $msg->entity_id; |
| 1222 | } else { |
| 1223 | $entity = $msg->findDisplayEntity($entity, $alt_order); |
| 1224 | } |
| 1225 | $found = true; |
| 1226 | } else if ($type == 'multipart/related') { |
| 1227 | $msgs = $this->findRelatedEntity(); |
| 1228 | foreach ($msgs as $msg) { |
| 1229 | if (count($msg->entities) == 0) { |
| 1230 | $entity[] = $msg->entity_id; |
| 1231 | } else { |
| 1232 | $entity = $msg->findDisplayEntity($entity, $alt_order); |
| 1233 | } |
| 1234 | } |
| 1235 | if (count($msgs) > 0) { |
| 1236 | $found = true; |
| 1237 | } |
| 1238 | } else if (($this->type0 == 'text') && |
| 1239 | (($this->type1 == 'plain') || |
| 1240 | ($this->type1 == 'html') || |
| 1241 | ($this->type1 == 'message')) && |
| 1242 | isset($this->entity_id)) { |
| 1243 | if (count($this->entities) == 0) { |
| 1244 | if (strtolower($this->header->disposition->name) != 'attachment') { |
| 1245 | $entity[] = $this->entity_id; |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | $i = 0; |
| 1250 | if (!$found) { |
| 1251 | foreach ($this->entities as $ent) { |
| 1252 | if((strtolower($ent->header->disposition->name) != 'attachment') && |
| 1253 | (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) { |
| 1254 | $entity = $ent->findDisplayEntity($entity, $alt_order); |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | /* CAN THIS BE REMOVED? IF SO, DO IT!!! */ |
| 1260 | //while (isset($this->entities[$i]) && !$found && |
| 1261 | // (strtolower($this->entities[$i]->header->disposition->name) |
| 1262 | // != 'attachment') && |
| 1263 | // ($this->entities[$i]->type0 != 'message' && |
| 1264 | // $this->entities[$i]->type1 != 'rfc822') |
| 1265 | // ) { |
| 1266 | // $entity = $this->entities[$i]->findDisplayEntity($entity, $alt_order); |
| 1267 | // ++$i; |
| 1268 | //} |
| 1269 | |
| 1270 | return ($entity); |
| 1271 | } |
| 1272 | |
| 1273 | function findAlternativeEntity($alt_order) { |
| 1274 | /* If we are dealing with alternative parts then we */ |
| 1275 | /* choose the best viewable message supported by SM. */ |
| 1276 | $best_view = 0; |
| 1277 | $entity = array(); |
| 1278 | $altcount = count($alt_order); |
| 1279 | |
| 1280 | foreach($this->entities as $ent) { |
| 1281 | $type = $ent->header->type0 . '/' . $ent->header->type1; |
| 1282 | if ($type == 'multipart/related') { |
| 1283 | $type = $ent->header->getParameter('type'); |
| 1284 | } |
| 1285 | for ($j = $best_view; $j < $altcount; ++$j) { |
| 1286 | if (($alt_order[$j] == $type) && ($j >= $best_view)) { |
| 1287 | $best_view = $j; |
| 1288 | $entity = $ent; |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | return $entity; |
| 1294 | } |
| 1295 | |
| 1296 | function findRelatedEntity() { |
| 1297 | $msgs = array(); |
| 1298 | $entcount = count($this->entities); |
| 1299 | |
| 1300 | for ($i = 0; $i < $entcount; ++$i) { |
| 1301 | $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1; |
| 1302 | if ($this->header->getParameter('type') == $type) { |
| 1303 | $msgs[] = $this->entities[$i]; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | return $msgs; |
| 1308 | } |
| 1309 | |
| 1310 | function getAttachments($exclude_id=array(), $result = array()) { |
| 1311 | if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) { |
| 1312 | $this = $this->entities[0]; |
| 1313 | } |
| 1314 | |
| 1315 | if (count($this->entities)) { |
| 1316 | foreach ($this->entities as $entity) { |
| 1317 | $exclude = false; |
| 1318 | |
| 1319 | foreach ($exclude_id as $excl) { |
| 1320 | if ($entity->entity_id === $excl) { |
| 1321 | $exclude = true; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | if (!$exclude) { |
| 1326 | if (($entity->type0 == 'multipart') && |
| 1327 | ($entity->type1 != 'related')) { |
| 1328 | $result = $entity->getAttachments($exclude_id, $result); |
| 1329 | } else if ($entity->type0 != 'multipart') { |
| 1330 | $result[] = $entity; |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | } else { |
| 1335 | $exclude = false; |
| 1336 | foreach ($exclude_id as $excl) { |
| 1337 | $exclude = $exclude || ($this->entity_id == $excl); |
| 1338 | } |
| 1339 | |
| 1340 | if (!$exclude) { |
| 1341 | $result[] = $this; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | return $result; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | class smime_message { |
| 1350 | |
| 1351 | } |
| 1352 | |
| 1353 | class disposition { |
| 1354 | function disposition($name) { |
| 1355 | $this->name = $name; |
| 1356 | $this->properties = array(); |
| 1357 | } |
| 1358 | |
| 1359 | function getProperty($par) { |
| 1360 | $value = strtolower($par); |
| 1361 | if (isset($this->properties[$par])) { |
| 1362 | return $this->properties[$par]; |
| 1363 | } |
| 1364 | return ''; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | class language { |
| 1369 | function language($name) { |
| 1370 | $this->name = $name; |
| 1371 | $this->properties = array(); |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | class content_type { |
| 1376 | var $type0 = 'text', |
| 1377 | $type1 = 'plain', |
| 1378 | $properties = ''; |
| 1379 | |
| 1380 | function content_type($type) { |
| 1381 | $pos = strpos($type, '/'); |
| 1382 | if ($pos > 0) { |
| 1383 | $this->type0 = substr($type, 0, $pos); |
| 1384 | $this->type1 = substr($type, $pos+1); |
| 1385 | } else { |
| 1386 | $this->type0 = $type; |
| 1387 | } |
| 1388 | $this->properties = array(); |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | ?> |