check for empty string before we add it to the token array
[squirrelmail.git] / class / mime / Rfc822Header.class.php
1 <?php
2
3 /**
4 * Rfc822Header.class.php
5 *
6 * Copyright (c) 2003 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 $encoding = '',
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 }
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 $this->parseField($field, $value);
57 }
58 }
59 }
60 if ($this->content_type == '') {
61 $this->parseContentType('text/plain; charset=us-ascii');
62 }
63 }
64
65 function stripComments($value) {
66 $result = '';
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':
111 $value = $this->stripComments($value);
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':
141 $value = $this->stripComments($value);
142 $this->message_id = $value;
143 break;
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':
150 case 'disposition-notification-to':
151 $value = $this->stripComments($value);
152 $this->dnt = $this->parseAddress($value);
153 break;
154 case 'mime-version':
155 $value = $this->stripComments($value);
156 $value = str_replace(' ', '', $value);
157 $this->mime = ($value == '1.0' ? true : $this->mime);
158 break;
159 case 'content-type':
160 $value = $this->stripComments($value);
161 $this->parseContentType($value);
162 break;
163 case 'content-disposition':
164 $value = $this->stripComments($value);
165 $this->parseDisposition($value);
166 break;
167 case 'user-agent':
168 case 'x-mailer':
169 $this->xmailer = $value;
170 break;
171 case 'x-priority':
172 $this->priority = $value;
173 break;
174 case 'list-post':
175 $value = $this->stripComments($value);
176 $this->mlist('post', $value);
177 break;
178 case 'list-reply':
179 $value = $this->stripComments($value);
180 $this->mlist('reply', $value);
181 break;
182 case 'list-subscribe':
183 $value = $this->stripComments($value);
184 $this->mlist('subscribe', $value);
185 break;
186 case 'list-unsubscribe':
187 $value = $this->stripComments($value);
188 $this->mlist('unsubscribe', $value);
189 break;
190 case 'list-archive':
191 $value = $this->stripComments($value);
192 $this->mlist('archive', $value);
193 break;
194 case 'list-owner':
195 $value = $this->stripComments($value);
196 $this->mlist('owner', $value);
197 break;
198 case 'list-help':
199 $value = $this->stripComments($value);
200 $this->mlist('help', $value);
201 break;
202 case 'list-id':
203 $value = $this->stripComments($value);
204 $this->mlist('id', $value);
205 break;
206 default:
207 break;
208 }
209 }
210
211 function getAddressTokens($address) {
212 $aTokens = array();
213 $aAddress = array();
214 $iCnt = strlen($address);
215 $aSpecials = array('(' ,'<' ,',' ,';' ,':');
216 $aReplace = array(' (',' <',' ,',' ;',' :');
217 $address = str_replace($aSpecials,$aReplace,$address);
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);
246 if ($sToken) $aTokens[] = $sToken;
247 break;
248 case '(':
249 $iEnd = strpos($address,')',$i);
250 if (!$iEnd) {
251 $sToken = substr($address,$i);
252 $i = $iCnt;
253 } else {
254 $sToken = substr($address,$i,$iEnd - $i + 1);
255 $i = $iEnd;
256 }
257 $sToken = str_replace($aReplace, $aSpecials,$sToken);
258 $aTokens[] = $sToken;
259 break;
260 case ',':
261 case ';':
262 case ';':
263 case ' ':
264 $aTokens[] = $cChar;
265 break;
266 default:
267 $iEnd = strpos($address,' ',$i+1);
268 if ($iEnd) {
269 $sToken = trim(substr($address,$i,$iEnd - $i));
270 $i = $iEnd-1;
271 } else {
272 $sToken = trim(substr($address,$i));
273 $i = $iCnt;
274 }
275 if ($sToken) $aTokens[] = $sToken;
276 }
277 ++$i;
278 }
279 return $aTokens;
280 }
281 function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') {
282 if (!$sEmail) {
283 while (count($aStack) && !$sEmail) {
284 $sEmail = trim(array_pop($aStack));
285 }
286 }
287 if (count($aStack)) {
288 $sPersonal = trim(implode('',$aStack));
289 } else {
290 $sPersonal = '';
291 }
292 if (!$sPersonal && count($aComment)) {
293 $sComment = trim(implode(' ',$aComment));
294 $sPersonal .= $sComment;
295 }
296 $oAddr =& new AddressStructure();
297 if ($sPersonal && substr($sPersonal,0,2) == '=?') {
298 $oAddr->personal = encodeHeader($sPersonal);
299 } else {
300 $oAddr->personal = $sPersonal;
301 }
302 $oAddr->group = $sGroup;
303 $iPosAt = strpos($sEmail,'@');
304 if ($iPosAt) {
305 $oAddr->mailbox = substr($sEmail, 0, $iPosAt);
306 $oAddr->host = substr($sEmail, $iPosAt+1);
307 } else {
308 $oAddr->mailbox = $sEmail;
309 $oAddr->host = false;
310 }
311 $oAddr->group = $sGroup;
312 $sEmail = '';
313 $aStack = $aComment = array();
314 return $oAddr;
315 }
316
317 /*
318 * parseAddress: recursive function for parsing address strings and store
319 * them in an address stucture object.
320 * input: $address = string
321 * $ar = boolean (return array instead of only the
322 * first element)
323 * $addr_ar = array with parsed addresses // obsolete
324 * $group = string // obsolete
325 * $host = string (default domainname in case of
326 * addresses without a domainname)
327 * $lookup = callback function (for lookup address
328 * strings which are probably nicks
329 * (without @ ) )
330 * output: array with addressstructure objects or only one
331 * address_structure object.
332 * personal name: encoded: =?charset?Q|B?string?=
333 * quoted: "string"
334 * normal: string
335 * email : <mailbox@host>
336 * : mailbox@host
337 * This function is also used for validating addresses returned from compose
338 * That's also the reason that the function became a little bit huge
339 */
340
341 function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) {
342 $aTokens = $this->getAddressTokens($address);
343 $sPersonal = $sEmail = $sComment = $sGroup = '';
344 $aStack = $aComment = array();
345 foreach ($aTokens as $sToken) {
346 $cChar = $sToken{0};
347 switch ($cChar)
348 {
349 case '=':
350 case '"':
351 case ' ':
352 $aStack[] = $sToken;
353 break;
354 case '(':
355 $aComment[] = substr($sToken,1,-1);
356 break;
357 case ';':
358 if ($sGroup) {
359 $oAddr = end($aAddress);
360 if ($oAddr && $oAddr->group == $sGroup) {
361 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
362 } else {
363 /* group is empty */
364 $aAddress[] = $this->createAddressObject(array(),array(),$sGroup,'');
365 }
366 $sGroup = '';
367 $aStack = $aComment = array();
368 break;
369 }
370 case ',':
371 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup);
372 break;
373 case ':':
374 $sGroup = implode(' ',$aStack); break;
375 $aStack = array();
376 break;
377 case '<':
378 $sEmail = trim(substr($sToken,1,-1));
379 break;
380 case '>':
381 /* skip */
382 break;
383 default: $aStack[] = $sToken; break;
384 }
385 }
386 /* now do the action again for the last address */
387 $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail);
388 /* try to lookup the addresses in case of invalid email addresses */
389 $aProcessedAddress = array();
390 foreach ($aAddress as $oAddr) {
391 $aAddrBookAddress = array();
392 if (!$oAddr->host) {
393 $grouplookup = false;
394 if ($lookup) {
395 $aAddr = call_user_func_array($lookup,array($oAddr->mailbox));
396 if (isset($aAddr['email'])) {
397 if (strpos($aAddr['email'],',')) {
398 $grouplookup = true;
399 $aAddrBookAddress = $this->parseAddress($aAddr['email'],true);
400 } else {
401 $iPosAt = strpos($aAddr['email'], '@');
402 $oAddr->mailbox = substr($aAddr['email'], 0, $iPosAt);
403 $oAddr->host = substr($aAddr['email'], $iPosAt+1);
404 if (isset($aAddr['name'])) {
405 $oAddr->personal = $aAddr['name'];
406 } else {
407 $oAddr->personal = encodeHeader($sPersonal);
408 }
409 }
410 }
411 }
412 if (!$grouplookup && !$oAddr->mailbox) {
413 $oAddr->mailbox = trim($sEmail);
414 if ($sHost && $oAddr->mailbox) {
415 $oAddr->host = $sHost;
416 }
417 }
418 }
419 if (!$aAddrBookAddress && $oAddr->mailbox) {
420 $aProcessedAddress[] = $oAddr;
421 } else {
422 $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress);
423 }
424 }
425 if ($ar) {
426 return $aProcessedAddress;
427 } else {
428 return $aProcessedAddress[0];
429 }
430 }
431
432 function parseContentType($value) {
433 $pos = strpos($value, ';');
434 $props = '';
435 if ($pos > 0) {
436 $type = trim(substr($value, 0, $pos));
437 $props = trim(substr($value, $pos+1));
438 } else {
439 $type = $value;
440 }
441 $content_type = new ContentType($type);
442 if ($props) {
443 $properties = $this->parseProperties($props);
444 if (!isset($properties['charset'])) {
445 $properties['charset'] = 'us-ascii';
446 }
447 $content_type->properties = $this->parseProperties($props);
448 }
449 $this->content_type = $content_type;
450 }
451
452 function parseProperties($value) {
453 $propArray = explode(';', $value);
454 $propResultArray = array();
455 foreach ($propArray as $prop) {
456 $prop = trim($prop);
457 $pos = strpos($prop, '=');
458 if ($pos > 0) {
459 $key = trim(substr($prop, 0, $pos));
460 $val = trim(substr($prop, $pos+1));
461 if ($val{0} == '"') {
462 $val = substr($val, 1, -1);
463 }
464 $propResultArray[$key] = $val;
465 }
466 }
467 return $propResultArray;
468 }
469
470 function parseDisposition($value) {
471 $pos = strpos($value, ';');
472 $props = '';
473 if ($pos > 0) {
474 $name = trim(substr($value, 0, $pos));
475 $props = trim(substr($value, $pos+1));
476 } else {
477 $name = $value;
478 }
479 $props_a = $this->parseProperties($props);
480 $disp = new Disposition($name);
481 $disp->properties = $props_a;
482 $this->disposition = $disp;
483 }
484
485 function mlist($field, $value) {
486 $res_a = array();
487 $value_a = explode(',', $value);
488 foreach ($value_a as $val) {
489 $val = trim($val);
490 if ($val{0} == '<') {
491 $val = substr($val, 1, -1);
492 }
493 if (substr($val, 0, 7) == 'mailto:') {
494 $res_a['mailto'] = substr($val, 7);
495 } else {
496 $res_a['href'] = $val;
497 }
498 }
499 $this->mlist[$field] = $res_a;
500 }
501
502 /*
503 * function to get the addres strings out of the header.
504 * Arguments: string or array of strings !
505 * example1: header->getAddr_s('to').
506 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
507 */
508 function getAddr_s($arr, $separator = ',',$encoded=false) {
509 $s = '';
510
511 if (is_array($arr)) {
512 foreach($arr as $arg) {
513 if ($this->getAddr_s($arg, $separator, $encoded)) {
514 $s .= $separator . $result;
515 }
516 }
517 $s = ($s ? substr($s, 2) : $s);
518 } else {
519 $addr = $this->{$arr};
520 if (is_array($addr)) {
521 foreach ($addr as $addr_o) {
522 if (is_object($addr_o)) {
523 if ($encoded) {
524 $s .= $addr_o->getEncodedAddress() . $separator;
525 } else {
526 $s .= $addr_o->getAddress() . $separator;
527 }
528 }
529 }
530 $s = substr($s, 0, -strlen($separator));
531 } else {
532 if (is_object($addr)) {
533 if ($encoded) {
534 $s .= $addr->getEncodedAddress();
535 } else {
536 $s .= $addr->getAddress();
537 }
538 }
539 }
540 }
541 return $s;
542 }
543
544 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
545 if (is_array($arg)) {
546 foreach($arg as $argument) {
547 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
548 }
549 } else {
550 $addr = $this->{$arg};
551 if (is_array($addr)) {
552 foreach ($addr as $next_addr) {
553 if (is_object($next_addr)) {
554 if (isset($next_addr->host) && ($next_addr->host != '')) {
555 $email = $next_addr->mailbox . '@' . $next_addr->host;
556 } else {
557 $email = $next_addr->mailbox;
558 }
559 $email = strtolower($email);
560 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
561 $arr[$email] = $next_addr->personal;
562 }
563 }
564 }
565 } else {
566 if (is_object($addr)) {
567 $email = $addr->mailbox;
568 $email .= (isset($addr->host) ? '@' . $addr->host : '');
569 $email = strtolower($email);
570 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
571 $arr[$email] = $addr->personal;
572 }
573 }
574 }
575 }
576 return $arr;
577 }
578
579 function findAddress($address, $recurs = false) {
580 $result = false;
581 if (is_array($address)) {
582 $i=0;
583 foreach($address as $argument) {
584 $match = $this->findAddress($argument, true);
585 $last = end($match);
586 if ($match[1]) {
587 return $i;
588 } else {
589 if (count($match[0]) && !$result) {
590 $result = $i;
591 }
592 }
593 ++$i;
594 }
595 } else {
596 if (!is_array($this->cc)) $this->cc = array();
597 $srch_addr = $this->parseAddress($address);
598 $results = array();
599 foreach ($this->to as $to) {
600 if ($to->host == $srch_addr->host) {
601 if ($to->mailbox == $srch_addr->mailbox) {
602 $results[] = $srch_addr;
603 if ($to->personal == $srch_addr->personal) {
604 if ($recurs) {
605 return array($results, true);
606 } else {
607 return true;
608 }
609 }
610 }
611 }
612 }
613 foreach ($this->cc as $cc) {
614 if ($cc->host == $srch_addr->host) {
615 if ($cc->mailbox == $srch_addr->mailbox) {
616 $results[] = $srch_addr;
617 if ($cc->personal == $srch_addr->personal) {
618 if ($recurs) {
619 return array($results, true);
620 } else {
621 return true;
622 }
623 }
624 }
625 }
626 }
627 if ($recurs) {
628 return array($results, false);
629 } elseif (count($result)) {
630 return true;
631 } else {
632 return false;
633 }
634 }
635 //exit;
636 return $result;
637 }
638
639 function getContentType($type0, $type1) {
640 $type0 = $this->content_type->type0;
641 $type1 = $this->content_type->type1;
642 return $this->content_type->properties;
643 }
644 }
645
646 ?>