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 '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);
155 $value = $this->stripComments($value);
156 $value = str_replace(' ', '', $value);
157 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
160 $value = $this->stripComments($value);
161 $this->parseContentType($value);
163 case 'content-disposition':
164 $value = $this->stripComments($value);
165 $this->parseDisposition($value);
169 $this->xmailer
= $value;
172 $this->priority
= $value;
175 $value = $this->stripComments($value);
176 $this->mlist('post', $value);
179 $value = $this->stripComments($value);
180 $this->mlist('reply', $value);
182 case 'list-subscribe':
183 $value = $this->stripComments($value);
184 $this->mlist('subscribe', $value);
186 case 'list-unsubscribe':
187 $value = $this->stripComments($value);
188 $this->mlist('unsubscribe', $value);
191 $value = $this->stripComments($value);
192 $this->mlist('archive', $value);
195 $value = $this->stripComments($value);
196 $this->mlist('owner', $value);
199 $value = $this->stripComments($value);
200 $this->mlist('help', $value);
203 $value = $this->stripComments($value);
204 $this->mlist('id', $value);
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
216 * $addr_ar = array with parsed addresses
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
223 * output: array with addressstructure objects or only one
224 * address_structure object.
226 function parseAddress
227 ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) {
229 $j = strlen($address);
234 switch ($address{$pos}) {
235 case '"': /* get the personal name */
236 if ($address{++
$pos} == '"') {
239 while ($pos < $j && $address{$pos} != '"') {
240 if ((substr($address, $pos, 2) == '\\"') ||
241 (substr($address, $pos, 2) == '\\\\')) {
242 $name .= $address{$pos++
};
244 $name .= $address{$pos++
};
249 case '<': /* get email address */
250 $addr_start = $pos++
;
251 while ($pos < $j && $address{$pos} != '>') {
252 $addr .= $address{$pos++
};
256 case '(': /* rip off comments */
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;
265 $j = strlen($address);
266 $pos = $addr_start +
1;
268 case ',': /* we reached a delimiter */
270 $addr = substr($address, 0, $pos);
271 } else if ($name == '') {
272 $name = trim(substr($address, 0, $addr_start));
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;
281 $addr_structure->mailbox
= substr($addr, 0, $at);
282 $addr_structure->host
= substr($addr, $at+
1);
284 /* if lookup function */
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'];
296 if (!$addr_structure->mailbox
) {
297 $addr_structure->mailbox
= trim($addr);
299 $addr_structure->host
= $host;
303 $address = trim(substr($address, $pos+
1));
304 $j = strlen($address);
308 $addr_ar[] = $addr_structure;
310 case ':': /* process the group addresses */
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];
317 $address = substr($address, $pos++
);
318 $j = strlen($address);
323 $address = substr($address, 0, $pos - 1);
333 $addr = substr($address, 0, $pos);
334 } else if ($name == '') {
335 $name = trim(substr($address, 0, $addr_start));
337 if (!$name && $comment) $name = $comment;
338 $at = strpos($addr, '@');
339 $addr_structure = new AddressStructure();
340 $addr_structure->group
= $group;
342 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
343 $addr_structure->host
= trim(substr($addr, $at+
1));
345 /* if lookup function */
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'];
357 if (!$addr_structure->mailbox
) {
358 $addr_structure->mailbox
= trim($addr);
360 $addr_structure->host
= $host;
364 if ($group && $addr == '') { /* no addresses found in group */
366 $addr_structure->personal
= $name;
367 $addr_ar[] = $addr_structure;
368 return (array($addr_ar,$pos+
1 ));
370 $addr_structure->personal
= $name;
371 $addr_ar[] = $addr_structure;
372 return (array($addr_ar,$pos+
1 ));
374 $addr_structure->personal
= $name;
375 if ($name ||
$addr) {
376 $addr_ar[] = $addr_structure;
382 return ($addr_ar[0]);
385 function parseContentType($value) {
386 $pos = strpos($value, ';');
389 $type = trim(substr($value, 0, $pos));
390 $props = trim(substr($type, $pos+
1));
394 $content_type = new ContentType($type);
396 $properties = $this->parseProperties($props);
397 if (!isset($properties['charset'])) {
398 $properties['charset'] = 'us-ascii';
400 $content_type->properties
= $this->parseProperties($props);
402 $this->content_type
= $content_type;
405 function parseProperties($value) {
406 $propArray = explode(';', $value);
407 $propResultArray = array();
408 foreach ($propArray as $prop) {
410 $pos = strpos($prop, '=');
412 $key = trim(substr($prop, 0, $pos));
413 $val = trim(substr($prop, $pos+
1));
414 if ($val{0} == '"') {
415 $val = substr($val, 1, -1);
417 $propResultArray[$key] = $val;
420 return $propResultArray;
423 function parseDisposition($value) {
424 $pos = strpos($value, ';');
427 $name = trim(substr($value, 0, $pos));
428 $props = trim(substr($value, $pos+
1));
432 $props_a = $this->parseProperties($props);
433 $disp = new Disposition($name);
434 $disp->properties
= $props_a;
435 $this->disposition
= $disp;
438 function mlist($field, $value) {
440 $value_a = explode(',', $value);
441 foreach ($value_a as $val) {
443 if ($val{0} == '<') {
444 $val = substr($val, 1, -1);
446 if (substr($val, 0, 7) == 'mailto:') {
447 $res_a['mailto'] = substr($val, 7);
449 $res_a['href'] = $val;
452 $this->mlist
[$field] = $res_a;
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'))
461 function getAddr_s($arr, $separator = ',') {
464 if (is_array($arr)) {
465 foreach($arr as $arg) {
466 if ($this->getAddr_s($arg)) {
467 $s .= $separator . $result;
470 $s = ($s ?
substr($s, 2) : $s);
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;
479 $s = substr($s, 0, -strlen($separator));
481 if (is_object($addr)) {
482 $s .= $addr->getAddress();
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);
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
;
502 $email = $next_addr->mailbox
;
504 $email = strtolower($email);
505 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
506 $arr[$email] = $next_addr->personal
;
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
;
524 function findAddress($address, $recurs = false) {
526 if (is_array($address)) {
528 foreach($address as $argument) {
529 $match = $this->findAddress($argument, true);
534 if (count($match[0]) && !$result) {
541 if (!is_array($this->cc
)) $this->cc
= array();
542 $srch_addr = $this->parseAddress($address);
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
) {
550 return array($results, true);
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
) {
564 return array($results, true);
573 return array($results, false);
574 } elseif (count($result)) {
584 function getContentType($type0, $type1) {
585 $type0 = $this->content_type
->type0
;
586 $type1 = $this->content_type
->type1
;
587 return $this->content_type
->properties
;