4 * Rfc822Header.class.php
6 * Copyright (c) 2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions needed to handle mime messages.
16 * input: header_string or array
37 $more_headers = array(); /* only needed for constructing headers
39 function parseHeader($hdr) {
41 $hdr = implode('', $hdr);
44 /* First we unfold the header */
45 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr));
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, ':');
53 $field = substr($line, 0, $pos);
54 if (!strstr($field,' ')) { /* valid field */
55 $value = trim(substr($line, $pos+
1));
56 $this->parseField($field, $value);
60 if ($this->content_type
== '') {
61 $this->parseContentType('text/plain; charset=us-ascii');
65 function stripComments($value) {
67 $cnt = strlen($value);
68 for ($i = 0; $i < $cnt; ++
$i) {
72 while ((++
$i < $cnt) && ($value{$i} != '"')) {
73 if ($value{$i} == '\\') {
77 $result .= $value{$i};
79 $result .= $value{$i};
83 while (($depth > 0) && (++
$i < $cnt)) {
100 $result .= $value{$i};
107 function parseField($field, $value) {
108 $field = strtolower($field);
111 $value = $this->stripComments($value);
112 $d = strtr($value, array(' ' => ' '));
113 $d = explode(' ', $d);
114 $this->date
= getTimeStamp($d);
117 $this->subject
= $value;
120 $this->from
= $this->parseAddress($value,true);
123 $this->sender
= $this->parseAddress($value);
126 $this->reply_to
= $this->parseAddress($value, true);
129 $this->to
= $this->parseAddress($value, true);
132 $this->cc
= $this->parseAddress($value, true);
135 $this->bcc
= $this->parseAddress($value, true);
138 $this->in_reply_to
= $value;
141 $value = $this->stripComments($value);
142 $this->message_id
= $value;
145 $value = $this->stripComments($value);
146 $this->references
= $value;
148 case 'disposition-notification-to':
149 $value = $this->stripComments($value);
150 $this->dnt
= $this->parseAddress($value);
153 $value = $this->stripComments($value);
154 $value = str_replace(' ', '', $value);
155 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
158 $value = $this->stripComments($value);
159 $this->parseContentType($value);
161 case 'content-disposition':
162 $value = $this->stripComments($value);
163 $this->parseDisposition($value);
167 $this->xmailer
= $value;
170 $this->priority
= $value;
173 $value = $this->stripComments($value);
174 $this->mlist('post', $value);
177 $value = $this->stripComments($value);
178 $this->mlist('reply', $value);
180 case 'list-subscribe':
181 $value = $this->stripComments($value);
182 $this->mlist('subscribe', $value);
184 case 'list-unsubscribe':
185 $value = $this->stripComments($value);
186 $this->mlist('unsubscribe', $value);
189 $value = $this->stripComments($value);
190 $this->mlist('archive', $value);
193 $value = $this->stripComments($value);
194 $this->mlist('owner', $value);
197 $value = $this->stripComments($value);
198 $this->mlist('help', $value);
201 $value = $this->stripComments($value);
202 $this->mlist('id', $value);
209 * parseAddress: recursive function for parsing address strings and store
210 * them in an address stucture object.
211 * input: $address = string
212 * $ar = boolean (return array instead of only the
214 * $addr_ar = array with parsed addresses
216 * $host = string (default domainname in case of
217 * addresses without a domainname)
218 * $lookup = callback function (for lookup address
219 * strings which are probably nicks
221 * output: array with addressstructure objects or only one
222 * address_structure object.
224 function parseAddress
225 ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) {
227 $j = strlen($address);
232 switch ($address{$pos}) {
233 case '"': /* get the personal name */
234 if ($address{++
$pos} == '"') {
237 while ($pos < $j && $address{$pos} != '"') {
238 if ((substr($address, $pos, 2) == '\\"') ||
239 (substr($address, $pos, 2) == '\\\\')) {
240 $name .= $address{$pos++
};
242 $name .= $address{$pos++
};
247 case '<': /* get email address */
248 $addr_start = $pos++
;
249 while ($pos < $j && $address{$pos} != '>') {
250 $addr .= $address{$pos++
};
254 case '(': /* rip off comments */
256 $pos = strpos($address,')');
257 if ($pos !== false) {
258 $comment = substr($address, $addr_start+
1,($pos-$addr_start-1));
259 $address_start = substr($address, 0, $addr_start);
260 $address_end = substr($address, $pos +
1);
261 $address = $address_start . $address_end;
263 $j = strlen($address);
264 $pos = $addr_start +
1;
266 case ',': /* we reached a delimiter */
268 $addr = substr($address, 0, $pos);
269 } else if ($name == '') {
270 $name = trim(substr($address, 0, $addr_start));
273 $at = strpos($addr, '@');
274 $addr_structure = new AddressStructure();
275 if (!$name && $comment) $name = $comment;
276 $addr_structure->personal
= $name;
277 $addr_structure->group
= $group;
279 $addr_structure->mailbox
= substr($addr, 0, $at);
280 $addr_structure->host
= substr($addr, $at+
1);
282 /* if lookup function */
284 $aAddr = call_user_func_array($lookup,array($addr));
285 if (isset($aAddr['email'])) {
286 $at = strpos($aAddr['email'], '@');
287 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
288 $addr_structure->host
= substr($aAddr['email'], $at+
1);
289 if (isset($aAddr['name'])) {
290 $addr_structure->personal
= $aAddr['name'];
294 if (!$addr_structure->mailbox
) {
295 $addr_structure->mailbox
= trim($addr);
297 $addr_structure->host
= $host;
301 $address = trim(substr($address, $pos+
1));
302 $j = strlen($address);
306 $addr_ar[] = $addr_structure;
308 case ':': /* process the group addresses */
310 $group = substr($address, 0, $pos);
311 $address = substr($address, $pos+
1);
312 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
313 $addr_ar = $result[0];
315 $address = substr($address, $pos++
);
316 $j = strlen($address);
321 $address = substr($address, 0, $pos - 1);
331 $addr = substr($address, 0, $pos);
332 } else if ($name == '') {
333 $name = trim(substr($address, 0, $addr_start));
335 if (!$name && $comment) $name = $comment;
336 $at = strpos($addr, '@');
337 $addr_structure = new AddressStructure();
338 $addr_structure->group
= $group;
340 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
341 $addr_structure->host
= trim(substr($addr, $at+
1));
343 /* if lookup function */
345 $aAddr = call_user_func_array($lookup,array($addr));
346 if (isset($aAddr['email'])) {
347 $at = strpos($aAddr['email'], '@');
348 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
349 $addr_structure->host
= substr($aAddr['email'], $at+
1);
350 if (isset($aAddr['name'])) {
351 $addr_structure->personal
= $aAddr['name'];
355 if (!$addr_structure->mailbox
) {
356 $addr_structure->mailbox
= trim($addr);
358 $addr_structure->host
= $host;
362 if ($group && $addr == '') { /* no addresses found in group */
364 $addr_structure->personal
= $name;
365 $addr_ar[] = $addr_structure;
366 return (array($addr_ar,$pos+
1 ));
368 $addr_structure->personal
= $name;
369 $addr_ar[] = $addr_structure;
370 return (array($addr_ar,$pos+
1 ));
372 $addr_structure->personal
= $name;
373 if ($name ||
$addr) {
374 $addr_ar[] = $addr_structure;
380 return ($addr_ar[0]);
383 function parseContentType($value) {
384 $pos = strpos($value, ';');
387 $type = trim(substr($value, 0, $pos));
388 $props = trim(substr($type, $pos+
1));
392 $content_type = new ContentType($type);
394 $properties = $this->parseProperties($props);
395 if (!isset($properties['charset'])) {
396 $properties['charset'] = 'us-ascii';
398 $content_type->properties
= $this->parseProperties($props);
400 $this->content_type
= $content_type;
403 function parseProperties($value) {
404 $propArray = explode(';', $value);
405 $propResultArray = array();
406 foreach ($propArray as $prop) {
408 $pos = strpos($prop, '=');
410 $key = trim(substr($prop, 0, $pos));
411 $val = trim(substr($prop, $pos+
1));
412 if ($val{0} == '"') {
413 $val = substr($val, 1, -1);
415 $propResultArray[$key] = $val;
418 return $propResultArray;
421 function parseDisposition($value) {
422 $pos = strpos($value, ';');
425 $name = trim(substr($value, 0, $pos));
426 $props = trim(substr($value, $pos+
1));
430 $props_a = $this->parseProperties($props);
431 $disp = new Disposition($name);
432 $disp->properties
= $props_a;
433 $this->disposition
= $disp;
436 function mlist($field, $value) {
438 $value_a = explode(',', $value);
439 foreach ($value_a as $val) {
441 if ($val{0} == '<') {
442 $val = substr($val, 1, -1);
444 if (substr($val, 0, 7) == 'mailto:') {
445 $res_a['mailto'] = substr($val, 7);
447 $res_a['href'] = $val;
450 $this->mlist
[$field] = $res_a;
454 * function to get the addres strings out of the header.
455 * Arguments: string or array of strings !
456 * example1: header->getAddr_s('to').
457 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
459 function getAddr_s($arr, $separator = ',') {
462 if (is_array($arr)) {
463 foreach($arr as $arg) {
464 if ($this->getAddr_s($arg)) {
465 $s .= $separator . $result;
468 $s = ($s ?
substr($s, 2) : $s);
470 eval('$addr = $this->' . $arr . ';') ;
471 if (is_array($addr)) {
472 foreach ($addr as $addr_o) {
473 if (is_object($addr_o)) {
474 $s .= $addr_o->getAddress() . $separator;
477 $s = substr($s, 0, -strlen($separator));
479 if (is_object($addr)) {
480 $s .= $addr->getAddress();
487 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
488 if (is_array($arg)) {
489 foreach($arg as $argument) {
490 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
493 $addr = $this->{$arg};
494 if (is_array($addr)) {
495 foreach ($addr as $next_addr) {
496 if (is_object($next_addr)) {
497 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
498 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
500 $email = $next_addr->mailbox
;
502 $email = strtolower($email);
503 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
504 $arr[$email] = $next_addr->personal
;
509 if (is_object($addr)) {
510 $email = $addr->mailbox
;
511 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
512 $email = strtolower($email);
513 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
514 $arr[$email] = $addr->personal
;
522 function findAddress($address, $recurs = false) {
524 if (is_array($address)) {
526 foreach($address as $argument) {
527 $match = $this->findAddress($argument, true);
532 if (count($match[0]) && !$result) {
539 if (!is_array($this->cc
)) $this->cc
= array();
540 $srch_addr = $this->parseAddress($address);
542 foreach ($this->to
as $to) {
543 if ($to->host
== $srch_addr->host
) {
544 if ($to->mailbox
== $srch_addr->mailbox
) {
545 $results[] = $srch_addr;
546 if ($to->personal
== $srch_addr->personal
) {
548 return array($results, true);
556 foreach ($this->cc
as $cc) {
557 if ($cc->host
== $srch_addr->host
) {
558 if ($cc->mailbox
== $srch_addr->mailbox
) {
559 $results[] = $srch_addr;
560 if ($cc->personal
== $srch_addr->personal
) {
562 return array($results, true);
571 return array($results, false);
572 } elseif (count($result)) {
582 function getContentType($type0, $type1) {
583 $type0 = $this->content_type
->type0
;
584 $type1 = $this->content_type
->type1
;
585 return $this->content_type
->properties
;