Needed a little more info from subject link hook
[squirrelmail.git] / class / mime / Rfc822Header.class.php
CommitLineData
19d470aa 1<?php
2
3/**
4 * Rfc822Header.class.php
5 *
76911253 6 * Copyright (c) 2003 The SquirrelMail Project Team
19d470aa 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 */
18class 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 = '',
340d67c2 29 $references = '',
19d470aa 30 $mime = false,
31 $content_type = '',
32 $disposition = '',
33 $xmailer = '',
34 $priority = 3,
35 $dnt = '',
f1232547 36 $encoding = '',
19d470aa 37 $mlist = array(),
38 $more_headers = array(); /* only needed for constructing headers
39 in smtp.php */
40 function parseHeader($hdr) {
41 if (is_array($hdr)) {
42 $hdr = implode('', $hdr);
43 }
19d470aa 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);
340d67c2 54 if (!strstr($field,' ')) { /* valid field */
55 $value = trim(substr($line, $pos+1));
56 $this->parseField($field, $value);
57 }
19d470aa 58 }
59 }
60 if ($this->content_type == '') {
61 $this->parseContentType('text/plain; charset=us-ascii');
62 }
63 }
64
65 function stripComments($value) {
66 $result = '';
19d470aa 67 $cnt = strlen($value);
68 for ($i = 0; $i < $cnt; ++$i) {
69 switch ($value{$i}) {
70 case '"':
71 $result .= '"';
72 while ((++$i < $cnt) && ($value{$i} != '"')) {
73 if ($value{$i} == '\\') {
74 $result .= '\\';
75 ++$i;
76 }
77 $result .= $value{$i};
78 }
79 $result .= $value{$i};
80 break;
81 case '(':
82 $depth = 1;
83 while (($depth > 0) && (++$i < $cnt)) {
84 switch($value{$i}) {
85 case '\\':
86 ++$i;
87 break;
88 case '(':
89 ++$depth;
90 break;
91 case ')':
92 --$depth;
93 break;
94 default:
95 break;
96 }
97 }
98 break;
99 default:
100 $result .= $value{$i};
101 break;
102 }
103 }
104 return $result;
105 }
106
107 function parseField($field, $value) {
108 $field = strtolower($field);
109 switch($field) {
110 case 'date':
340d67c2 111 $value = $this->stripComments($value);
19d470aa 112 $d = strtr($value, array(' ' => ' '));
113 $d = explode(' ', $d);
114 $this->date = getTimeStamp($d);
115 break;
116 case 'subject':
117 $this->subject = $value;
118 break;
119 case 'from':
120 $this->from = $this->parseAddress($value,true);
121 break;
122 case 'sender':
123 $this->sender = $this->parseAddress($value);
124 break;
125 case 'reply-to':
126 $this->reply_to = $this->parseAddress($value, true);
127 break;
128 case 'to':
129 $this->to = $this->parseAddress($value, true);
130 break;
131 case 'cc':
132 $this->cc = $this->parseAddress($value, true);
133 break;
134 case 'bcc':
135 $this->bcc = $this->parseAddress($value, true);
136 break;
137 case 'in-reply-to':
138 $this->in_reply_to = $value;
139 break;
140 case 'message-id':
340d67c2 141 $value = $this->stripComments($value);
19d470aa 142 $this->message_id = $value;
143 break;
340d67c2 144 case 'references':
145 $value = $this->stripComments($value);
146 $this->references = $value;
147 break;
148 case 'x-confirm-reading-to':
149 case 'return-receipt-to':
19d470aa 150 case 'disposition-notification-to':
340d67c2 151 $value = $this->stripComments($value);
19d470aa 152 $this->dnt = $this->parseAddress($value);
153 break;
154 case 'mime-version':
340d67c2 155 $value = $this->stripComments($value);
19d470aa 156 $value = str_replace(' ', '', $value);
157 $this->mime = ($value == '1.0' ? true : $this->mime);
158 break;
159 case 'content-type':
340d67c2 160 $value = $this->stripComments($value);
19d470aa 161 $this->parseContentType($value);
162 break;
163 case 'content-disposition':
340d67c2 164 $value = $this->stripComments($value);
19d470aa 165 $this->parseDisposition($value);
166 break;
167 case 'user-agent':
168 case 'x-mailer':
340d67c2 169 $this->xmailer = $value;
19d470aa 170 break;
171 case 'x-priority':
172 $this->priority = $value;
173 break;
174 case 'list-post':
340d67c2 175 $value = $this->stripComments($value);
19d470aa 176 $this->mlist('post', $value);
177 break;
178 case 'list-reply':
340d67c2 179 $value = $this->stripComments($value);
19d470aa 180 $this->mlist('reply', $value);
181 break;
182 case 'list-subscribe':
340d67c2 183 $value = $this->stripComments($value);
19d470aa 184 $this->mlist('subscribe', $value);
185 break;
186 case 'list-unsubscribe':
340d67c2 187 $value = $this->stripComments($value);
19d470aa 188 $this->mlist('unsubscribe', $value);
189 break;
190 case 'list-archive':
340d67c2 191 $value = $this->stripComments($value);
19d470aa 192 $this->mlist('archive', $value);
193 break;
194 case 'list-owner':
340d67c2 195 $value = $this->stripComments($value);
19d470aa 196 $this->mlist('owner', $value);
197 break;
198 case 'list-help':
340d67c2 199 $value = $this->stripComments($value);
19d470aa 200 $this->mlist('help', $value);
201 break;
ba4d5a32 202 case 'list-id':
203 $value = $this->stripComments($value);
204 $this->mlist('id', $value);
205 break;
19d470aa 206 default:
207 break;
208 }
209 }
14882b16 210
211 function getAddressTokens($address) {
212 $aTokens = array();
213 $aAddress = array();
14882b16 214 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
215 $aReplace = array(' (',' <',' ,',' ;',' :');
216 $address = str_replace($aSpecials,$aReplace,$address);
55243181 217 $iCnt = strlen($address);
14882b16 218 $i = 0;
219 while ($i < $iCnt) {
220 $cChar = $address{$i};
221 switch($cChar)
222 {
223 case '<':
224 $iEnd = strpos($address,'>',$i+1);
225 if (!$iEnd) {
226 $sToken = substr($address,$i);
227 $i = $iCnt;
228 } else {
229 $sToken = substr($address,$i,$iEnd - $i +1);
230 $i = $iEnd;
231 }
232 $sToken = str_replace($aReplace, $aSpecials,$sToken);
233 $aTokens[] = $sToken;
234 break;
235 case '"':
236 $iEnd = strpos($address,$cChar,$i+1);
237 if (!$iEnd) {
238 $sToken = substr($address,$i);
239 $i = $iCnt;
240 } else {
241 // also remove the surrounding quotes
242 $sToken = substr($address,$i+1,$iEnd - $i -1);
243 $i = $iEnd;
244 }
245 $sToken = str_replace($aReplace, $aSpecials,$sToken);
72956ab6 246 if ($sToken) $aTokens[] = $sToken;
14882b16 247 break;
248 case '(':
249 $iEnd = strpos($address,')',$i);
250 if (!$iEnd) {
251 $sToken = substr($address,$i);
252 $i = $iCnt;
253 } else {
55243181 254 $iDepth = 1;
255 $iComment = $i;
256 while (($iDepth > 0) && (++$iComment < $iCnt)) {
257 $cCharComment = $address{$iComment};
258 switch($cCharComment) {
259 case '\\':
260 ++$iComment;
261 break;
262 case '(':
263 ++$iDepth;
264 break;
265 case ')':
266 --$iDepth;
267 break;
268 default:
269 break;
270 }
271 }
272 if ($iDepth == 0) {
273 $sToken = substr($address,$i,$iComment - $i +1);
274 $i = $iComment;
275 } else {
276 $sToken = substr($address,$i,$iEnd - $i + 1);
277 $i = $iEnd;
278 }
14882b16 279 }
280 $sToken = str_replace($aReplace, $aSpecials,$sToken);
281 $aTokens[] = $sToken;
282 break;
283 case ',':
284 case ';':
285 case ';':
286 case ' ':
287 $aTokens[] = $cChar;
288 break;
289 default:
290 $iEnd = strpos($address,' ',$i+1);
291 if ($iEnd) {
292 $sToken = trim(substr($address,$i,$iEnd - $i));
293 $i = $iEnd-1;
294 } else {
295 $sToken = trim(substr($address,$i));
296 $i = $iCnt;
297 }
298 if ($sToken) $aTokens[] = $sToken;
299 }
300 ++$i;
301 }
302 return $aTokens;
303 }
304 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
305 if (!$sEmail) {
306 while (count($aStack) && !$sEmail) {
307 $sEmail = trim(array_pop($aStack));
308 }
309 }
310 if (count($aStack)) {
311 $sPersonal = trim(implode('',$aStack));
312 } else {
313 $sPersonal = '';
314 }
315 if (!$sPersonal && count($aComment)) {
316 $sComment = trim(implode(' ',$aComment));
317 $sPersonal .= $sComment;
318 }
319 $oAddr =& new AddressStructure();
320 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
321 $oAddr->personal = encodeHeader($sPersonal);
322 } else {
323 $oAddr->personal = $sPersonal;
324 }
325 $oAddr->group = $sGroup;
326 $iPosAt = strpos($sEmail,'@');
327 if ($iPosAt) {
328 $oAddr->mailbox = substr($sEmail, 0, $iPosAt);
329 $oAddr->host = substr($sEmail, $iPosAt+1);
330 } else {
331 $oAddr->mailbox = $sEmail;
332 $oAddr->host = false;
333 }
334 $oAddr->group = $sGroup;
335 $sEmail = '';
336 $aStack = $aComment = array();
337 return $oAddr;
338 }
339
e74ba378 340 /*
341 * parseAddress: recursive function for parsing address strings and store
342 * them in an address stucture object.
343 * input: $address = string
344 * $ar = boolean (return array instead of only the
345 * first element)
14882b16 346 * $addr_ar = array with parsed addresses // obsolete
347 * $group = string // obsolete
e74ba378 348 * $host = string (default domainname in case of
349 * addresses without a domainname)
350 * $lookup = callback function (for lookup address
351 * strings which are probably nicks
352 * (without @ ) )
353 * output: array with addressstructure objects or only one
354 * address_structure object.
340d67c2 355 * personal name: encoded: =?charset?Q|B?string?=
356 * quoted: "string"
357 * normal: string
358 * email : <mailbox@host>
359 * : mailbox@host
360 * This function is also used for validating addresses returned from compose
14882b16 361 * That's also the reason that the function became a little bit huge
e74ba378 362 */
340d67c2 363
14882b16 364 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
365 $aTokens = $this->getAddressTokens($address);
366 $sPersonal = $sEmail = $sComment = $sGroup = '';
367 $aStack = $aComment = array();
368 foreach ($aTokens as $sToken) {
369 $cChar = $sToken{0};
370 switch ($cChar)
340d67c2 371 {
372 case '=':
14882b16 373 case '"':
374 case ' ':
375 $aStack[] = $sToken;
340d67c2 376 break;
14882b16 377 case '(':
378 $aComment[] = substr($sToken,1,-1);
340d67c2 379 break;
3fcadedb 380 case ';':
14882b16 381 if ($sGroup) {
382 $oAddr = end($aAddress);
383 if ($oAddr && $oAddr->group == $sGroup) {
384 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
340d67c2 385 } else {
14882b16 386 /* group is empty */
387 $aAddress[] = $this->createAddressObject(array(),array(),$sGroup,'');
19d470aa 388 }
14882b16 389 $sGroup = '';
390 $aStack = $aComment = array();
391 break;
340d67c2 392 }
14882b16 393 case ',':
394 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
395 break;
396 case ':':
397 $sGroup = implode(' ',$aStack); break;
398 $aStack = array();
399 break;
400 case '<':
401 $sEmail = trim(substr($sToken,1,-1));
402 break;
403 case '>':
404 /* skip */
405 break;
406 default: $aStack[] = $sToken; break;
19d470aa 407 }
408 }
14882b16 409 /* now do the action again for the last address */
410 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
411 /* try to lookup the addresses in case of invalid email addresses */
412 $aProcessedAddress = array();
413 foreach ($aAddress as $oAddr) {
414 $aAddrBookAddress = array();
415 if (!$oAddr->host) {
416 $grouplookup = false;
340d67c2 417 if ($lookup) {
14882b16 418 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox));
419 if (isset($aAddr['email'])) {
420 if (strpos($aAddr['email'],',')) {
421 $grouplookup = true;
422 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
423 } else {
424 $iPosAt = strpos($aAddr['email'], '@');
425 $oAddr->mailbox = substr($aAddr['email'], 0, $iPosAt);
426 $oAddr->host = substr($aAddr['email'], $iPosAt+1);
427 if (isset($aAddr['name'])) {
428 $oAddr->personal = $aAddr['name'];
429 } else {
430 $oAddr->personal = encodeHeader($sPersonal);
431 }
432 }
433 }
340d67c2 434 }
14882b16 435 if (!$grouplookup && !$oAddr->mailbox) {
436 $oAddr->mailbox = trim($sEmail);
437 if ($sHost && $oAddr->mailbox) {
438 $oAddr->host = $sHost;
340d67c2 439 }
440 }
14882b16 441 }
442 if (!$aAddrBookAddress && $oAddr->mailbox) {
443 $aProcessedAddress[] = $oAddr;
444 } else {
445 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
446 }
340d67c2 447 }
14882b16 448 if ($ar) {
449 return $aProcessedAddress;
19d470aa 450 } else {
14882b16 451 return $aProcessedAddress[0];
19d470aa 452 }
14882b16 453 }
19d470aa 454
455 function parseContentType($value) {
456 $pos = strpos($value, ';');
457 $props = '';
458 if ($pos > 0) {
459 $type = trim(substr($value, 0, $pos));
38d6fba7 460 $props = trim(substr($value, $pos+1));
19d470aa 461 } else {
462 $type = $value;
463 }
464 $content_type = new ContentType($type);
465 if ($props) {
466 $properties = $this->parseProperties($props);
467 if (!isset($properties['charset'])) {
468 $properties['charset'] = 'us-ascii';
469 }
470 $content_type->properties = $this->parseProperties($props);
471 }
472 $this->content_type = $content_type;
473 }
474
475 function parseProperties($value) {
476 $propArray = explode(';', $value);
477 $propResultArray = array();
478 foreach ($propArray as $prop) {
479 $prop = trim($prop);
480 $pos = strpos($prop, '=');
481 if ($pos > 0) {
482 $key = trim(substr($prop, 0, $pos));
483 $val = trim(substr($prop, $pos+1));
484 if ($val{0} == '"') {
485 $val = substr($val, 1, -1);
486 }
487 $propResultArray[$key] = $val;
488 }
489 }
490 return $propResultArray;
491 }
492
493 function parseDisposition($value) {
494 $pos = strpos($value, ';');
495 $props = '';
496 if ($pos > 0) {
497 $name = trim(substr($value, 0, $pos));
fc9269ec 498 $props = trim(substr($value, $pos+1));
19d470aa 499 } else {
500 $name = $value;
501 }
502 $props_a = $this->parseProperties($props);
503 $disp = new Disposition($name);
504 $disp->properties = $props_a;
505 $this->disposition = $disp;
506 }
507
508 function mlist($field, $value) {
509 $res_a = array();
510 $value_a = explode(',', $value);
511 foreach ($value_a as $val) {
512 $val = trim($val);
513 if ($val{0} == '<') {
514 $val = substr($val, 1, -1);
515 }
516 if (substr($val, 0, 7) == 'mailto:') {
517 $res_a['mailto'] = substr($val, 7);
518 } else {
519 $res_a['href'] = $val;
520 }
521 }
522 $this->mlist[$field] = $res_a;
523 }
524
525 /*
526 * function to get the addres strings out of the header.
527 * Arguments: string or array of strings !
528 * example1: header->getAddr_s('to').
529 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
530 */
2c9ecd11 531 function getAddr_s($arr, $separator = ',',$encoded=false) {
19d470aa 532 $s = '';
533
534 if (is_array($arr)) {
535 foreach($arr as $arg) {
2c9ecd11 536 if ($this->getAddr_s($arg, $separator, $encoded)) {
19d470aa 537 $s .= $separator . $result;
538 }
539 }
540 $s = ($s ? substr($s, 2) : $s);
541 } else {
2c9ecd11 542 $addr = $this->{$arr};
19d470aa 543 if (is_array($addr)) {
544 foreach ($addr as $addr_o) {
545 if (is_object($addr_o)) {
2c9ecd11 546 if ($encoded) {
547 $s .= $addr_o->getEncodedAddress() . $separator;
548 } else {
549 $s .= $addr_o->getAddress() . $separator;
550 }
19d470aa 551 }
552 }
553 $s = substr($s, 0, -strlen($separator));
554 } else {
555 if (is_object($addr)) {
2c9ecd11 556 if ($encoded) {
557 $s .= $addr->getEncodedAddress();
558 } else {
559 $s .= $addr->getAddress();
560 }
19d470aa 561 }
562 }
563 }
564 return $s;
565 }
566
567 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
568 if (is_array($arg)) {
569 foreach($arg as $argument) {
570 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
571 }
572 } else {
340d67c2 573 $addr = $this->{$arg};
19d470aa 574 if (is_array($addr)) {
575 foreach ($addr as $next_addr) {
576 if (is_object($next_addr)) {
577 if (isset($next_addr->host) && ($next_addr->host != '')) {
578 $email = $next_addr->mailbox . '@' . $next_addr->host;
579 } else {
580 $email = $next_addr->mailbox;
581 }
582 $email = strtolower($email);
583 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
584 $arr[$email] = $next_addr->personal;
585 }
586 }
587 }
588 } else {
589 if (is_object($addr)) {
590 $email = $addr->mailbox;
591 $email .= (isset($addr->host) ? '@' . $addr->host : '');
592 $email = strtolower($email);
593 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
594 $arr[$email] = $addr->personal;
595 }
596 }
597 }
598 }
599 return $arr;
600 }
d0719411 601
602 function findAddress($address, $recurs = false) {
340d67c2 603 $result = false;
d0719411 604 if (is_array($address)) {
340d67c2 605 $i=0;
d0719411 606 foreach($address as $argument) {
607 $match = $this->findAddress($argument, true);
340d67c2 608 $last = end($match);
609 if ($match[1]) {
610 return $i;
611 } else {
612 if (count($match[0]) && !$result) {
613 $result = $i;
614 }
615 }
616 ++$i;
617 }
618 } else {
619 if (!is_array($this->cc)) $this->cc = array();
620 $srch_addr = $this->parseAddress($address);
621 $results = array();
622 foreach ($this->to as $to) {
623 if ($to->host == $srch_addr->host) {
624 if ($to->mailbox == $srch_addr->mailbox) {
625 $results[] = $srch_addr;
626 if ($to->personal == $srch_addr->personal) {
627 if ($recurs) {
628 return array($results, true);
629 } else {
630 return true;
631 }
632 }
633 }
634 }
d0719411 635 }
340d67c2 636 foreach ($this->cc as $cc) {
637 if ($cc->host == $srch_addr->host) {
638 if ($cc->mailbox == $srch_addr->mailbox) {
639 $results[] = $srch_addr;
640 if ($cc->personal == $srch_addr->personal) {
641 if ($recurs) {
642 return array($results, true);
643 } else {
644 return true;
645 }
646 }
647 }
648 }
649 }
650 if ($recurs) {
651 return array($results, false);
652 } elseif (count($result)) {
653 return true;
654 } else {
655 return false;
656 }
657 }
1465f80c 658 //exit;
340d67c2 659 return $result;
d0719411 660 }
19d470aa 661
662 function getContentType($type0, $type1) {
663 $type0 = $this->content_type->type0;
664 $type1 = $this->content_type->type1;
665 return $this->content_type->properties;
666 }
667}
668
669?>