4 * Rfc822Header.class.php
6 * Copyright (c) 2002 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 if(!preg_match('/^X.*/i', $field) &&
57 !preg_match('/^Subject/i', $field)) {
58 $value = $this->stripComments($value);
60 $this->parseField($field, $value);
64 if ($this->content_type
== '') {
65 $this->parseContentType('text/plain; charset=us-ascii');
69 function stripComments($value) {
72 $cnt = strlen($value);
73 for ($i = 0; $i < $cnt; ++
$i) {
77 while ((++
$i < $cnt) && ($value{$i} != '"')) {
78 if ($value{$i} == '\\') {
82 $result .= $value{$i};
84 $result .= $value{$i};
88 while (($depth > 0) && (++
$i < $cnt)) {
105 $result .= $value{$i};
112 function parseField($field, $value) {
113 $field = strtolower($field);
116 $d = strtr($value, array(' ' => ' '));
117 $d = explode(' ', $d);
118 $this->date
= getTimeStamp($d);
121 $this->subject
= $value;
124 $this->from
= $this->parseAddress($value,true);
127 $this->sender
= $this->parseAddress($value);
130 $this->reply_to
= $this->parseAddress($value, true);
133 $this->to
= $this->parseAddress($value, true);
136 $this->cc
= $this->parseAddress($value, true);
139 $this->bcc
= $this->parseAddress($value, true);
142 $this->in_reply_to
= $value;
145 $this->message_id
= $value;
148 $this->references
= $value;
150 case 'disposition-notification-to':
151 $this->dnt
= $this->parseAddress($value);
154 $value = str_replace(' ', '', $value);
155 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
158 $this->parseContentType($value);
160 case 'content-disposition':
161 $this->parseDisposition($value);
165 $this->xmailer
= $value;
168 $this->priority
= $value;
171 $this->mlist('post', $value);
174 $this->mlist('reply', $value);
176 case 'list-subscribe':
177 $this->mlist('subscribe', $value);
179 case 'list-unsubscribe':
180 $this->mlist('unsubscribe', $value);
183 $this->mlist('archive', $value);
186 $this->mlist('owner', $value);
189 $this->mlist('help', $value);
192 $this->mlist('id', $value);
199 function parseAddress
200 ($address, $ar=false, $addr_ar = array(), $group = '', $host='') {
202 $j = strlen($address);
206 switch ($address{$pos}) {
207 case '"': /* get the personal name */
208 if ($address{++
$pos} == '"') {
211 while ($pos < $j && $address{$pos} != '"') {
212 if ((substr($address, $pos, 2) == '\\"') ||
213 (substr($address, $pos, 2) == '\\\\')) {
214 $name .= $address{$pos++
};
216 $name .= $address{$pos++
};
221 case '<': /* get email address */
222 $addr_start = $pos++
;
223 while ($pos < $j && $address{$pos} != '>') {
224 $addr .= $address{$pos++
};
228 case '(': /* rip off comments */
230 for (++
$pos; ($pos < $j) && ($address{$pos} != ')'); ++
$pos) {
231 $addr .= $address{$pos};
233 $address_start = substr($address, 0, $addr_start);
234 $address_end = substr($address, $pos +
1);
235 $address = $address_start . $address_end;
236 $j = strlen($address);
237 $pos = $addr_start +
1;
239 case ',': /* we reached a delimiter */
242 $addr = substr($address, 0, $pos);
243 } else if ($name == '') {
244 $name = trim(substr($address, 0, $addr_start));
247 $at = strpos($addr, '@');
248 $addr_structure = new AddressStructure();
249 $addr_structure->personal
= $name;
250 $addr_structure->group
= $group;
252 $addr_structure->mailbox
= substr($addr, 0, $at);
253 $addr_structure->host
= substr($addr, $at+
1);
255 $addr_structure->mailbox
= $addr;
257 $addr_structure->host
= $host;
260 $address = trim(substr($address, $pos+
1));
261 $j = strlen($address);
265 $addr_ar[] = $addr_structure;
267 case ':': /* process the group addresses */
269 $group = substr($address, 0, $pos);
270 $address = substr($address, $pos+
1);
271 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
272 $addr_ar = $result[0];
274 $address = substr($address, $pos++
);
275 $j = strlen($address);
280 $address = substr($address, 0, $pos - 1);
290 $addr = substr($address, 0, $pos);
291 } else if ($name == '') {
292 $name = trim(substr($address, 0, $addr_start));
294 $at = strpos($addr, '@');
295 $addr_structure = new AddressStructure();
296 $addr_structure->group
= $group;
298 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
299 $addr_structure->host
= trim(substr($addr, $at+
1));
301 $addr_structure->mailbox
= trim($addr);
303 $addr_structure->host
= $host;
306 if ($group && $addr == '') { /* no addresses found in group */
308 $addr_structure->personal
= $name;
309 $addr_ar[] = $addr_structure;
310 return (array($addr_ar,$pos+
1 ));
312 $addr_structure->personal
= $name;
313 $addr_ar[] = $addr_structure;
314 return (array($addr_ar,$pos+
1 ));
316 $addr_structure->personal
= $name;
317 if ($name ||
$addr) {
318 $addr_ar[] = $addr_structure;
324 return ($addr_ar[0]);
327 function parseContentType($value) {
328 $pos = strpos($value, ';');
331 $type = trim(substr($value, 0, $pos));
332 $props = trim(substr($type, $pos+
1));
336 $content_type = new ContentType($type);
338 $properties = $this->parseProperties($props);
339 if (!isset($properties['charset'])) {
340 $properties['charset'] = 'us-ascii';
342 $content_type->properties
= $this->parseProperties($props);
344 $this->content_type
= $content_type;
347 function parseProperties($value) {
348 $propArray = explode(';', $value);
349 $propResultArray = array();
350 foreach ($propArray as $prop) {
352 $pos = strpos($prop, '=');
354 $key = trim(substr($prop, 0, $pos));
355 $val = trim(substr($prop, $pos+
1));
356 if ($val{0} == '"') {
357 $val = substr($val, 1, -1);
359 $propResultArray[$key] = $val;
362 return $propResultArray;
365 function parseDisposition($value) {
366 $pos = strpos($value, ';');
369 $name = trim(substr($value, 0, $pos));
370 $props = trim(substr($value, $pos+
1));
374 $props_a = $this->parseProperties($props);
375 $disp = new Disposition($name);
376 $disp->properties
= $props_a;
377 $this->disposition
= $disp;
380 function mlist($field, $value) {
382 $value_a = explode(',', $value);
383 foreach ($value_a as $val) {
385 if ($val{0} == '<') {
386 $val = substr($val, 1, -1);
388 if (substr($val, 0, 7) == 'mailto:') {
389 $res_a['mailto'] = substr($val, 7);
391 $res_a['href'] = $val;
394 $this->mlist
[$field] = $res_a;
398 * function to get the addres strings out of the header.
399 * Arguments: string or array of strings !
400 * example1: header->getAddr_s('to').
401 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
403 function getAddr_s($arr, $separator = ',') {
406 if (is_array($arr)) {
407 foreach($arr as $arg) {
408 if ($this->getAddr_s($arg)) {
409 $s .= $separator . $result;
412 $s = ($s ?
substr($s, 2) : $s);
414 eval('$addr = $this->' . $arr . ';') ;
415 if (is_array($addr)) {
416 foreach ($addr as $addr_o) {
417 if (is_object($addr_o)) {
418 $s .= $addr_o->getAddress() . $separator;
421 $s = substr($s, 0, -strlen($separator));
423 if (is_object($addr)) {
424 $s .= $addr->getAddress();
431 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
432 if (is_array($arg)) {
433 foreach($arg as $argument) {
434 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
437 eval('$addr = $this->' . $arg . ';') ;
438 if (is_array($addr)) {
439 foreach ($addr as $next_addr) {
440 if (is_object($next_addr)) {
441 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
442 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
444 $email = $next_addr->mailbox
;
446 $email = strtolower($email);
447 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
448 $arr[$email] = $next_addr->personal
;
453 if (is_object($addr)) {
454 $email = $addr->mailbox
;
455 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
456 $email = strtolower($email);
457 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
458 $arr[$email] = $addr->personal
;
466 function findAddress($address, $recurs = false) {
468 if (is_array($address)) {
470 foreach($address as $argument) {
471 $match = $this->findAddress($argument, true);
476 if (count($match[0]) && !$result) {
483 $srch_addr = $this->parseAddress($address);
485 foreach ($this->to
as $to) {
486 if ($to->host
== $srch_addr->host
) {
487 if ($to->mailbox
== $srch_addr->mailbox
) {
488 $results[] = $srch_addr;
489 if ($to->personal
== $srch_addr->personal
) {
491 return array($results, true);
499 foreach ($this->cc
as $cc) {
500 if ($cc->host
== $srch_addr->host
) {
501 if ($cc->mailbox
== $srch_addr->mailbox
) {
502 $results[] = $srch_addr;
503 if ($cc->personal
== $srch_addr->personal
) {
505 return array($results, true);
514 return array($results, false);
515 } elseif (count($result)) {
524 function getContentType($type0, $type1) {
525 $type0 = $this->content_type
->type0
;
526 $type1 = $this->content_type
->type1
;
527 return $this->content_type
->properties
;