add checks for Messge Disposition Notifications.
[squirrelmail.git] / class / mime / Rfc822Header.class.php
... / ...
CommitLineData
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 */
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 = '',
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 $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 * parseAddress: recursive function for parsing address strings and store
212 * them in an address stucture object.
213 * input: $address = string
214 * $ar = boolean (return array instead of only the
215 * first element)
216 * $addr_ar = array with parsed addresses
217 * $group = string
218 * $host = string (default domainname in case of
219 * addresses without a domainname)
220 * $lookup = callback function (for lookup address
221 * strings which are probably nicks
222 * (without @ ) )
223 * output: array with addressstructure objects or only one
224 * address_structure object.
225 */
226 function parseAddress
227 ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) {
228 $pos = 0;
229 $j = strlen($address);
230 $name = '';
231 $addr = '';
232 $comment = '';
233 while ($pos < $j) {
234 switch ($address{$pos}) {
235 case '"': /* get the personal name */
236 if ($address{++$pos} == '"') {
237 ++$pos;
238 } else {
239 while ($pos < $j && $address{$pos} != '"') {
240 if ((substr($address, $pos, 2) == '\\"') ||
241 (substr($address, $pos, 2) == '\\\\')) {
242 $name .= $address{$pos++};
243 }
244 $name .= $address{$pos++};
245 }
246 }
247 ++$pos;
248 break;
249 case '<': /* get email address */
250 $addr_start = $pos++;
251 while ($pos < $j && $address{$pos} != '>') {
252 $addr .= $address{$pos++};
253 }
254 ++$pos;
255 break;
256 case '(': /* rip off comments */
257 $addr_start = $pos;
258 $pos = strpos($address,')');
259 if ($pos !== false) {
260 $comment = substr($address, $addr_start+1,($pos-$addr_start-1));
261 $address_start = substr($address, 0, $addr_start);
262 $address_end = substr($address, $pos + 1);
263 $address = $address_start . $address_end;
264 }
265 $j = strlen($address);
266 $pos = $addr_start + 1;
267 break;
268 case ',': /* we reached a delimiter */
269 if ($addr == '') {
270 $addr = substr($address, 0, $pos);
271 } else if ($name == '') {
272 $name = trim(substr($address, 0, $addr_start));
273 }
274
275 $at = strpos($addr, '@');
276 $addr_structure = new AddressStructure();
277 if (!$name && $comment) $name = $comment;
278 $addr_structure->personal = $name;
279 $addr_structure->group = $group;
280 if ($at) {
281 $addr_structure->mailbox = substr($addr, 0, $at);
282 $addr_structure->host = substr($addr, $at+1);
283 } else {
284 /* if lookup function */
285 if ($lookup) {
286 $aAddr = call_user_func_array($lookup,array($addr));
287 if (isset($aAddr['email'])) {
288 $at = strpos($aAddr['email'], '@');
289 $addr_structure->mailbox = substr($aAddr['email'], 0, $at);
290 $addr_structure->host = substr($aAddr['email'], $at+1);
291 if (isset($aAddr['name'])) {
292 $addr_structure->personal = $aAddr['name'];
293 }
294 }
295 }
296 if (!$addr_structure->mailbox) {
297 $addr_structure->mailbox = trim($addr);
298 if ($host) {
299 $addr_structure->host = $host;
300 }
301 }
302 }
303 $address = trim(substr($address, $pos+1));
304 $j = strlen($address);
305 $pos = 0;
306 $name = '';
307 $addr = '';
308 $addr_ar[] = $addr_structure;
309 break;
310 case ':': /* process the group addresses */
311 /* group marker */
312 $group = substr($address, 0, $pos);
313 $address = substr($address, $pos+1);
314 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
315 $addr_ar = $result[0];
316 $pos = $result[1];
317 $address = substr($address, $pos++);
318 $j = strlen($address);
319 $group = '';
320 break;
321 case ';':
322 if ($group) {
323 $address = substr($address, 0, $pos - 1);
324 }
325 ++$pos;
326 break;
327 default:
328 ++$pos;
329 break;
330 }
331 }
332 if ($addr == '') {
333 $addr = substr($address, 0, $pos);
334 } else if ($name == '') {
335 $name = trim(substr($address, 0, $addr_start));
336 }
337 if (!$name && $comment) $name = $comment;
338 $at = strpos($addr, '@');
339 $addr_structure = new AddressStructure();
340 $addr_structure->group = $group;
341 if ($at) {
342 $addr_structure->mailbox = trim(substr($addr, 0, $at));
343 $addr_structure->host = trim(substr($addr, $at+1));
344 } else {
345 /* if lookup function */
346 if ($lookup) {
347 $aAddr = call_user_func_array($lookup,array($addr));
348 if (isset($aAddr['email'])) {
349 $at = strpos($aAddr['email'], '@');
350 $addr_structure->mailbox = substr($aAddr['email'], 0, $at);
351 $addr_structure->host = substr($aAddr['email'], $at+1);
352 if (isset($aAddr['name'])) {
353 $addr_structure->personal = $aAddr['name'];
354 }
355 }
356 }
357 if (!$addr_structure->mailbox) {
358 $addr_structure->mailbox = trim($addr);
359 if ($host) {
360 $addr_structure->host = $host;
361 }
362 }
363 }
364 if ($group && $addr == '') { /* no addresses found in group */
365 $name = "$group";
366 $addr_structure->personal = $name;
367 $addr_ar[] = $addr_structure;
368 return (array($addr_ar,$pos+1 ));
369 } elseif ($group) {
370 $addr_structure->personal = $name;
371 $addr_ar[] = $addr_structure;
372 return (array($addr_ar,$pos+1 ));
373 } else {
374 $addr_structure->personal = $name;
375 if ($name || $addr) {
376 $addr_ar[] = $addr_structure;
377 }
378 }
379 if ($ar) {
380 return ($addr_ar);
381 }
382 return ($addr_ar[0]);
383 }
384
385 function parseContentType($value) {
386 $pos = strpos($value, ';');
387 $props = '';
388 if ($pos > 0) {
389 $type = trim(substr($value, 0, $pos));
390 $props = trim(substr($type, $pos+1));
391 } else {
392 $type = $value;
393 }
394 $content_type = new ContentType($type);
395 if ($props) {
396 $properties = $this->parseProperties($props);
397 if (!isset($properties['charset'])) {
398 $properties['charset'] = 'us-ascii';
399 }
400 $content_type->properties = $this->parseProperties($props);
401 }
402 $this->content_type = $content_type;
403 }
404
405 function parseProperties($value) {
406 $propArray = explode(';', $value);
407 $propResultArray = array();
408 foreach ($propArray as $prop) {
409 $prop = trim($prop);
410 $pos = strpos($prop, '=');
411 if ($pos > 0) {
412 $key = trim(substr($prop, 0, $pos));
413 $val = trim(substr($prop, $pos+1));
414 if ($val{0} == '"') {
415 $val = substr($val, 1, -1);
416 }
417 $propResultArray[$key] = $val;
418 }
419 }
420 return $propResultArray;
421 }
422
423 function parseDisposition($value) {
424 $pos = strpos($value, ';');
425 $props = '';
426 if ($pos > 0) {
427 $name = trim(substr($value, 0, $pos));
428 $props = trim(substr($value, $pos+1));
429 } else {
430 $name = $value;
431 }
432 $props_a = $this->parseProperties($props);
433 $disp = new Disposition($name);
434 $disp->properties = $props_a;
435 $this->disposition = $disp;
436 }
437
438 function mlist($field, $value) {
439 $res_a = array();
440 $value_a = explode(',', $value);
441 foreach ($value_a as $val) {
442 $val = trim($val);
443 if ($val{0} == '<') {
444 $val = substr($val, 1, -1);
445 }
446 if (substr($val, 0, 7) == 'mailto:') {
447 $res_a['mailto'] = substr($val, 7);
448 } else {
449 $res_a['href'] = $val;
450 }
451 }
452 $this->mlist[$field] = $res_a;
453 }
454
455 /*
456 * function to get the addres strings out of the header.
457 * Arguments: string or array of strings !
458 * example1: header->getAddr_s('to').
459 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
460 */
461 function getAddr_s($arr, $separator = ',') {
462 $s = '';
463
464 if (is_array($arr)) {
465 foreach($arr as $arg) {
466 if ($this->getAddr_s($arg)) {
467 $s .= $separator . $result;
468 }
469 }
470 $s = ($s ? substr($s, 2) : $s);
471 } else {
472 eval('$addr = $this->' . $arr . ';') ;
473 if (is_array($addr)) {
474 foreach ($addr as $addr_o) {
475 if (is_object($addr_o)) {
476 $s .= $addr_o->getAddress() . $separator;
477 }
478 }
479 $s = substr($s, 0, -strlen($separator));
480 } else {
481 if (is_object($addr)) {
482 $s .= $addr->getAddress();
483 }
484 }
485 }
486 return $s;
487 }
488
489 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
490 if (is_array($arg)) {
491 foreach($arg as $argument) {
492 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
493 }
494 } else {
495 $addr = $this->{$arg};
496 if (is_array($addr)) {
497 foreach ($addr as $next_addr) {
498 if (is_object($next_addr)) {
499 if (isset($next_addr->host) && ($next_addr->host != '')) {
500 $email = $next_addr->mailbox . '@' . $next_addr->host;
501 } else {
502 $email = $next_addr->mailbox;
503 }
504 $email = strtolower($email);
505 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
506 $arr[$email] = $next_addr->personal;
507 }
508 }
509 }
510 } else {
511 if (is_object($addr)) {
512 $email = $addr->mailbox;
513 $email .= (isset($addr->host) ? '@' . $addr->host : '');
514 $email = strtolower($email);
515 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
516 $arr[$email] = $addr->personal;
517 }
518 }
519 }
520 }
521 return $arr;
522 }
523
524 function findAddress($address, $recurs = false) {
525 $result = false;
526 if (is_array($address)) {
527 $i=0;
528 foreach($address as $argument) {
529 $match = $this->findAddress($argument, true);
530 $last = end($match);
531 if ($match[1]) {
532 return $i;
533 } else {
534 if (count($match[0]) && !$result) {
535 $result = $i;
536 }
537 }
538 ++$i;
539 }
540 } else {
541 if (!is_array($this->cc)) $this->cc = array();
542 $srch_addr = $this->parseAddress($address);
543 $results = array();
544 foreach ($this->to as $to) {
545 if ($to->host == $srch_addr->host) {
546 if ($to->mailbox == $srch_addr->mailbox) {
547 $results[] = $srch_addr;
548 if ($to->personal == $srch_addr->personal) {
549 if ($recurs) {
550 return array($results, true);
551 } else {
552 return true;
553 }
554 }
555 }
556 }
557 }
558 foreach ($this->cc as $cc) {
559 if ($cc->host == $srch_addr->host) {
560 if ($cc->mailbox == $srch_addr->mailbox) {
561 $results[] = $srch_addr;
562 if ($cc->personal == $srch_addr->personal) {
563 if ($recurs) {
564 return array($results, true);
565 } else {
566 return true;
567 }
568 }
569 }
570 }
571 }
572 if ($recurs) {
573 return array($results, false);
574 } elseif (count($result)) {
575 return true;
576 } else {
577 return false;
578 }
579 }
580 //exit;
581 return $result;
582 }
583
584 function getContentType($type0, $type1) {
585 $type0 = $this->content_type->type0;
586 $type1 = $this->content_type->type1;
587 return $this->content_type->properties;
588 }
589}
590
591?>