| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Rfc822Header.class.php |
| 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 Rfc822Header { |
| 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 | $references = '', |
| 30 | $mime = false, |
| 31 | $content_type = '', |
| 32 | $disposition = '', |
| 33 | $xmailer = '', |
| 34 | $priority = 3, |
| 35 | $dnt = '', |
| 36 | $mlist = array(), |
| 37 | $more_headers = array(); /* only needed for constructing headers |
| 38 | in smtp.php */ |
| 39 | function parseHeader($hdr) { |
| 40 | if (is_array($hdr)) { |
| 41 | $hdr = implode('', $hdr); |
| 42 | } |
| 43 | |
| 44 | /* First we unfold the header */ |
| 45 | $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr)); |
| 46 | |
| 47 | /* Now we can make a new header array with */ |
| 48 | /* each element representing a headerline */ |
| 49 | $hdr = explode("\r\n" , $hdr); |
| 50 | foreach ($hdr as $line) { |
| 51 | $pos = strpos($line, ':'); |
| 52 | if ($pos > 0) { |
| 53 | $field = substr($line, 0, $pos); |
| 54 | if (!strstr($field,' ')) { /* valid field */ |
| 55 | $value = trim(substr($line, $pos+1)); |
| 56 | if(!preg_match('/^X.*/i', $field) && |
| 57 | !preg_match('/^Subject/i', $field)) { |
| 58 | $value = $this->stripComments($value); |
| 59 | } |
| 60 | $this->parseField($field, $value); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | if ($this->content_type == '') { |
| 65 | $this->parseContentType('text/plain; charset=us-ascii'); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | function stripComments($value) { |
| 70 | $result = ''; |
| 71 | |
| 72 | $cnt = strlen($value); |
| 73 | for ($i = 0; $i < $cnt; ++$i) { |
| 74 | switch ($value{$i}) { |
| 75 | case '"': |
| 76 | $result .= '"'; |
| 77 | while ((++$i < $cnt) && ($value{$i} != '"')) { |
| 78 | if ($value{$i} == '\\') { |
| 79 | $result .= '\\'; |
| 80 | ++$i; |
| 81 | } |
| 82 | $result .= $value{$i}; |
| 83 | } |
| 84 | $result .= $value{$i}; |
| 85 | break; |
| 86 | case '(': |
| 87 | $depth = 1; |
| 88 | while (($depth > 0) && (++$i < $cnt)) { |
| 89 | switch($value{$i}) { |
| 90 | case '\\': |
| 91 | ++$i; |
| 92 | break; |
| 93 | case '(': |
| 94 | ++$depth; |
| 95 | break; |
| 96 | case ')': |
| 97 | --$depth; |
| 98 | break; |
| 99 | default: |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | break; |
| 104 | default: |
| 105 | $result .= $value{$i}; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | return $result; |
| 110 | } |
| 111 | |
| 112 | function parseField($field, $value) { |
| 113 | $field = strtolower($field); |
| 114 | switch($field) { |
| 115 | case 'date': |
| 116 | $d = strtr($value, array(' ' => ' ')); |
| 117 | $d = explode(' ', $d); |
| 118 | $this->date = getTimeStamp($d); |
| 119 | break; |
| 120 | case 'subject': |
| 121 | $this->subject = $value; |
| 122 | break; |
| 123 | case 'from': |
| 124 | $this->from = $this->parseAddress($value,true); |
| 125 | break; |
| 126 | case 'sender': |
| 127 | $this->sender = $this->parseAddress($value); |
| 128 | break; |
| 129 | case 'reply-to': |
| 130 | $this->reply_to = $this->parseAddress($value, true); |
| 131 | break; |
| 132 | case 'to': |
| 133 | $this->to = $this->parseAddress($value, true); |
| 134 | break; |
| 135 | case 'cc': |
| 136 | $this->cc = $this->parseAddress($value, true); |
| 137 | break; |
| 138 | case 'bcc': |
| 139 | $this->bcc = $this->parseAddress($value, true); |
| 140 | break; |
| 141 | case 'in-reply-to': |
| 142 | $this->in_reply_to = $value; |
| 143 | break; |
| 144 | case 'message-id': |
| 145 | $this->message_id = $value; |
| 146 | break; |
| 147 | case 'references': |
| 148 | $this->references = $value; |
| 149 | break; |
| 150 | case 'disposition-notification-to': |
| 151 | $this->dnt = $this->parseAddress($value); |
| 152 | break; |
| 153 | case 'mime-version': |
| 154 | $value = str_replace(' ', '', $value); |
| 155 | $this->mime = ($value == '1.0' ? true : $this->mime); |
| 156 | break; |
| 157 | case 'content-type': |
| 158 | $this->parseContentType($value); |
| 159 | break; |
| 160 | case 'content-disposition': |
| 161 | $this->parseDisposition($value); |
| 162 | break; |
| 163 | case 'user-agent': |
| 164 | case 'x-mailer': |
| 165 | $this->xmailer = $value; |
| 166 | break; |
| 167 | case 'x-priority': |
| 168 | $this->priority = $value; |
| 169 | break; |
| 170 | case 'list-post': |
| 171 | $this->mlist('post', $value); |
| 172 | break; |
| 173 | case 'list-reply': |
| 174 | $this->mlist('reply', $value); |
| 175 | break; |
| 176 | case 'list-subscribe': |
| 177 | $this->mlist('subscribe', $value); |
| 178 | break; |
| 179 | case 'list-unsubscribe': |
| 180 | $this->mlist('unsubscribe', $value); |
| 181 | break; |
| 182 | case 'list-archive': |
| 183 | $this->mlist('archive', $value); |
| 184 | break; |
| 185 | case 'list-owner': |
| 186 | $this->mlist('owner', $value); |
| 187 | break; |
| 188 | case 'list-help': |
| 189 | $this->mlist('help', $value); |
| 190 | break; |
| 191 | case 'list-id': |
| 192 | $this->mlist('id', $value); |
| 193 | break; |
| 194 | default: |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | function parseAddress |
| 200 | ($address, $ar=false, $addr_ar = array(), $group = '', $host='') { |
| 201 | $pos = 0; |
| 202 | $j = strlen($address); |
| 203 | $name = ''; |
| 204 | $addr = ''; |
| 205 | while ($pos < $j) { |
| 206 | switch ($address{$pos}) { |
| 207 | case '"': /* get the personal name */ |
| 208 | if ($address{++$pos} == '"') { |
| 209 | ++$pos; |
| 210 | } else { |
| 211 | while ($pos < $j && $address{$pos} != '"') { |
| 212 | if ((substr($address, $pos, 2) == '\\"') || |
| 213 | (substr($address, $pos, 2) == '\\\\')) { |
| 214 | $name .= $address{$pos++}; |
| 215 | } |
| 216 | $name .= $address{$pos++}; |
| 217 | } |
| 218 | } |
| 219 | ++$pos; |
| 220 | break; |
| 221 | case '<': /* get email address */ |
| 222 | $addr_start = $pos++; |
| 223 | while ($pos < $j && $address{$pos} != '>') { |
| 224 | $addr .= $address{$pos++}; |
| 225 | } |
| 226 | ++$pos; |
| 227 | break; |
| 228 | case '(': /* rip off comments */ |
| 229 | $addr_start = $pos; |
| 230 | for (++$pos; ($pos < $j) && ($address{$pos} != ')'); ++$pos) { |
| 231 | $addr .= $address{$pos}; |
| 232 | } |
| 233 | $address_start = substr($address, 0, $addr_start); |
| 234 | $address_end = substr($address, $pos + 1); |
| 235 | $address = $address_start . $address_end; |
| 236 | $j = strlen($address); |
| 237 | $pos = $addr_start + 1; |
| 238 | break; |
| 239 | case ',': /* we reached a delimiter */ |
| 240 | //case ';': |
| 241 | if ($addr == '') { |
| 242 | $addr = substr($address, 0, $pos); |
| 243 | } else if ($name == '') { |
| 244 | $name = trim(substr($address, 0, $addr_start)); |
| 245 | } |
| 246 | |
| 247 | $at = strpos($addr, '@'); |
| 248 | $addr_structure = new AddressStructure(); |
| 249 | $addr_structure->personal = $name; |
| 250 | $addr_structure->group = $group; |
| 251 | if ($at) { |
| 252 | $addr_structure->mailbox = substr($addr, 0, $at); |
| 253 | $addr_structure->host = substr($addr, $at+1); |
| 254 | } else { |
| 255 | $addr_structure->mailbox = $addr; |
| 256 | if ($host) { |
| 257 | $addr_structure->host = $host; |
| 258 | } |
| 259 | } |
| 260 | $address = trim(substr($address, $pos+1)); |
| 261 | $j = strlen($address); |
| 262 | $pos = 0; |
| 263 | $name = ''; |
| 264 | $addr = ''; |
| 265 | $addr_ar[] = $addr_structure; |
| 266 | break; |
| 267 | case ':': /* process the group addresses */ |
| 268 | /* group marker */ |
| 269 | $group = substr($address, 0, $pos); |
| 270 | $address = substr($address, $pos+1); |
| 271 | $result = $this->parseAddress($address, $ar, $addr_ar, $group); |
| 272 | $addr_ar = $result[0]; |
| 273 | $pos = $result[1]; |
| 274 | $address = substr($address, $pos++); |
| 275 | $j = strlen($address); |
| 276 | $group = ''; |
| 277 | break; |
| 278 | case ';': |
| 279 | if ($group) { |
| 280 | $address = substr($address, 0, $pos - 1); |
| 281 | } |
| 282 | ++$pos; |
| 283 | break; |
| 284 | default: |
| 285 | ++$pos; |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | if ($addr == '') { |
| 290 | $addr = substr($address, 0, $pos); |
| 291 | } else if ($name == '') { |
| 292 | $name = trim(substr($address, 0, $addr_start)); |
| 293 | } |
| 294 | $at = strpos($addr, '@'); |
| 295 | $addr_structure = new AddressStructure(); |
| 296 | $addr_structure->group = $group; |
| 297 | if ($at) { |
| 298 | $addr_structure->mailbox = trim(substr($addr, 0, $at)); |
| 299 | $addr_structure->host = trim(substr($addr, $at+1)); |
| 300 | } else { |
| 301 | $addr_structure->mailbox = trim($addr); |
| 302 | if ($host) { |
| 303 | $addr_structure->host = $host; |
| 304 | } |
| 305 | } |
| 306 | if ($group && $addr == '') { /* no addresses found in group */ |
| 307 | $name = "$group"; |
| 308 | $addr_structure->personal = $name; |
| 309 | $addr_ar[] = $addr_structure; |
| 310 | return (array($addr_ar,$pos+1 )); |
| 311 | } elseif ($group) { |
| 312 | $addr_structure->personal = $name; |
| 313 | $addr_ar[] = $addr_structure; |
| 314 | return (array($addr_ar,$pos+1 )); |
| 315 | } else { |
| 316 | $addr_structure->personal = $name; |
| 317 | if ($name || $addr) { |
| 318 | $addr_ar[] = $addr_structure; |
| 319 | } |
| 320 | } |
| 321 | if ($ar) { |
| 322 | return ($addr_ar); |
| 323 | } |
| 324 | return ($addr_ar[0]); |
| 325 | } |
| 326 | |
| 327 | function parseContentType($value) { |
| 328 | $pos = strpos($value, ';'); |
| 329 | $props = ''; |
| 330 | if ($pos > 0) { |
| 331 | $type = trim(substr($value, 0, $pos)); |
| 332 | $props = trim(substr($type, $pos+1)); |
| 333 | } else { |
| 334 | $type = $value; |
| 335 | } |
| 336 | $content_type = new ContentType($type); |
| 337 | if ($props) { |
| 338 | $properties = $this->parseProperties($props); |
| 339 | if (!isset($properties['charset'])) { |
| 340 | $properties['charset'] = 'us-ascii'; |
| 341 | } |
| 342 | $content_type->properties = $this->parseProperties($props); |
| 343 | } |
| 344 | $this->content_type = $content_type; |
| 345 | } |
| 346 | |
| 347 | function parseProperties($value) { |
| 348 | $propArray = explode(';', $value); |
| 349 | $propResultArray = array(); |
| 350 | foreach ($propArray as $prop) { |
| 351 | $prop = trim($prop); |
| 352 | $pos = strpos($prop, '='); |
| 353 | if ($pos > 0) { |
| 354 | $key = trim(substr($prop, 0, $pos)); |
| 355 | $val = trim(substr($prop, $pos+1)); |
| 356 | if ($val{0} == '"') { |
| 357 | $val = substr($val, 1, -1); |
| 358 | } |
| 359 | $propResultArray[$key] = $val; |
| 360 | } |
| 361 | } |
| 362 | return $propResultArray; |
| 363 | } |
| 364 | |
| 365 | function parseDisposition($value) { |
| 366 | $pos = strpos($value, ';'); |
| 367 | $props = ''; |
| 368 | if ($pos > 0) { |
| 369 | $name = trim(substr($value, 0, $pos)); |
| 370 | $props = trim(substr($value, $pos+1)); |
| 371 | } else { |
| 372 | $name = $value; |
| 373 | } |
| 374 | $props_a = $this->parseProperties($props); |
| 375 | $disp = new Disposition($name); |
| 376 | $disp->properties = $props_a; |
| 377 | $this->disposition = $disp; |
| 378 | } |
| 379 | |
| 380 | function mlist($field, $value) { |
| 381 | $res_a = array(); |
| 382 | $value_a = explode(',', $value); |
| 383 | foreach ($value_a as $val) { |
| 384 | $val = trim($val); |
| 385 | if ($val{0} == '<') { |
| 386 | $val = substr($val, 1, -1); |
| 387 | } |
| 388 | if (substr($val, 0, 7) == 'mailto:') { |
| 389 | $res_a['mailto'] = substr($val, 7); |
| 390 | } else { |
| 391 | $res_a['href'] = $val; |
| 392 | } |
| 393 | } |
| 394 | $this->mlist[$field] = $res_a; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * function to get the addres strings out of the header. |
| 399 | * Arguments: string or array of strings ! |
| 400 | * example1: header->getAddr_s('to'). |
| 401 | * example2: header->getAddr_s(array('to', 'cc', 'bcc')) |
| 402 | */ |
| 403 | function getAddr_s($arr, $separator = ',') { |
| 404 | $s = ''; |
| 405 | |
| 406 | if (is_array($arr)) { |
| 407 | foreach($arr as $arg) { |
| 408 | if ($this->getAddr_s($arg)) { |
| 409 | $s .= $separator . $result; |
| 410 | } |
| 411 | } |
| 412 | $s = ($s ? substr($s, 2) : $s); |
| 413 | } else { |
| 414 | eval('$addr = $this->' . $arr . ';') ; |
| 415 | if (is_array($addr)) { |
| 416 | foreach ($addr as $addr_o) { |
| 417 | if (is_object($addr_o)) { |
| 418 | $s .= $addr_o->getAddress() . $separator; |
| 419 | } |
| 420 | } |
| 421 | $s = substr($s, 0, -strlen($separator)); |
| 422 | } else { |
| 423 | if (is_object($addr)) { |
| 424 | $s .= $addr->getAddress(); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | return $s; |
| 429 | } |
| 430 | |
| 431 | function getAddr_a($arg, $excl_arr = array(), $arr = array()) { |
| 432 | if (is_array($arg)) { |
| 433 | foreach($arg as $argument) { |
| 434 | $arr = $this->getAddr_a($argument, $excl_arr, $arr); |
| 435 | } |
| 436 | } else { |
| 437 | eval('$addr = $this->' . $arg . ';') ; |
| 438 | if (is_array($addr)) { |
| 439 | foreach ($addr as $next_addr) { |
| 440 | if (is_object($next_addr)) { |
| 441 | if (isset($next_addr->host) && ($next_addr->host != '')) { |
| 442 | $email = $next_addr->mailbox . '@' . $next_addr->host; |
| 443 | } else { |
| 444 | $email = $next_addr->mailbox; |
| 445 | } |
| 446 | $email = strtolower($email); |
| 447 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
| 448 | $arr[$email] = $next_addr->personal; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } else { |
| 453 | if (is_object($addr)) { |
| 454 | $email = $addr->mailbox; |
| 455 | $email .= (isset($addr->host) ? '@' . $addr->host : ''); |
| 456 | $email = strtolower($email); |
| 457 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
| 458 | $arr[$email] = $addr->personal; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | return $arr; |
| 464 | } |
| 465 | |
| 466 | function findAddress($address, $recurs = false) { |
| 467 | $result = false; |
| 468 | if (is_array($address)) { |
| 469 | $i=0; |
| 470 | foreach($address as $argument) { |
| 471 | $match = $this->findAddress($argument, true); |
| 472 | $last = end($match); |
| 473 | if ($match[1]) { |
| 474 | return $i; |
| 475 | } else { |
| 476 | if (count($match[0]) && !$result) { |
| 477 | $result = $i; |
| 478 | } |
| 479 | } |
| 480 | ++$i; |
| 481 | } |
| 482 | } else { |
| 483 | $srch_addr = $this->parseAddress($address); |
| 484 | $results = array(); |
| 485 | foreach ($this->to as $to) { |
| 486 | if ($to->host == $srch_addr->host) { |
| 487 | if ($to->mailbox == $srch_addr->mailbox) { |
| 488 | $results[] = $srch_addr; |
| 489 | if ($to->personal == $srch_addr->personal) { |
| 490 | if ($recurs) { |
| 491 | return array($results, true); |
| 492 | } else { |
| 493 | return true; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | foreach ($this->cc as $cc) { |
| 500 | if ($cc->host == $srch_addr->host) { |
| 501 | if ($cc->mailbox == $srch_addr->mailbox) { |
| 502 | $results[] = $srch_addr; |
| 503 | if ($cc->personal == $srch_addr->personal) { |
| 504 | if ($recurs) { |
| 505 | return array($results, true); |
| 506 | } else { |
| 507 | return true; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | if ($recurs) { |
| 514 | return array($results, false); |
| 515 | } elseif (count($result)) { |
| 516 | return true; |
| 517 | } else { |
| 518 | return false; |
| 519 | } |
| 520 | } |
| 521 | return $result; |
| 522 | } |
| 523 | |
| 524 | function getContentType($type0, $type1) { |
| 525 | $type0 = $this->content_type->type0; |
| 526 | $type1 = $this->content_type->type1; |
| 527 | return $this->content_type->properties; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | ?> |