00cda0e383ff7cb26dc3cbf0d89cbdc3e36f6b0d
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 * parseAddress: recursive function for parsing address strings and store
200 * them in an address stucture object.
201 * input: $address = string
202 * $ar = boolean (return array instead of only the
204 * $addr_ar = array with parsed addresses
206 * $host = string (default domainname in case of
207 * addresses without a domainname)
208 * $lookup = callback function (for lookup address
209 * strings which are probably nicks
211 * output: array with addressstructure objects or only one
212 * address_structure object.
214 function parseAddress
215 ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) {
217 $j = strlen($address);
221 switch ($address{$pos}) {
222 case '"': /* get the personal name */
223 if ($address{++
$pos} == '"') {
226 while ($pos < $j && $address{$pos} != '"') {
227 if ((substr($address, $pos, 2) == '\\"') ||
228 (substr($address, $pos, 2) == '\\\\')) {
229 $name .= $address{$pos++
};
231 $name .= $address{$pos++
};
236 case '<': /* get email address */
237 $addr_start = $pos++
;
238 while ($pos < $j && $address{$pos} != '>') {
239 $addr .= $address{$pos++
};
243 case '(': /* rip off comments */
245 for (++
$pos; ($pos < $j) && ($address{$pos} != ')'); ++
$pos) {
246 $addr .= $address{$pos};
248 $address_start = substr($address, 0, $addr_start);
249 $address_end = substr($address, $pos +
1);
250 $address = $address_start . $address_end;
251 $j = strlen($address);
252 $pos = $addr_start +
1;
254 case ',': /* we reached a delimiter */
256 $addr = substr($address, 0, $pos);
257 } else if ($name == '') {
258 $name = trim(substr($address, 0, $addr_start));
261 $at = strpos($addr, '@');
262 $addr_structure = new AddressStructure();
263 $addr_structure->personal
= $name;
264 $addr_structure->group
= $group;
266 $addr_structure->mailbox
= substr($addr, 0, $at);
267 $addr_structure->host
= substr($addr, $at+
1);
269 /* if lookup function */
271 $aAddr = call_user_func_array($lookup,array($addr));
272 if (isset($aAddr['email'])) {
273 $at = strpos($aAddr['email'], '@');
274 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
275 $addr_structure->host
= substr($aAddr['email'], $at+
1);
276 if (isset($aAddr['name'])) {
277 $addr_structure->personal
= $aAddr['name'];
281 if (!$addr_structure->mailbox
) {
282 $addr_structure->mailbox
= trim($addr);
284 $addr_structure->host
= $host;
288 $address = trim(substr($address, $pos+
1));
289 $j = strlen($address);
293 $addr_ar[] = $addr_structure;
295 case ':': /* process the group addresses */
297 $group = substr($address, 0, $pos);
298 $address = substr($address, $pos+
1);
299 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
300 $addr_ar = $result[0];
302 $address = substr($address, $pos++
);
303 $j = strlen($address);
308 $address = substr($address, 0, $pos - 1);
318 $addr = substr($address, 0, $pos);
319 } else if ($name == '') {
320 $name = trim(substr($address, 0, $addr_start));
322 $at = strpos($addr, '@');
323 $addr_structure = new AddressStructure();
324 $addr_structure->group
= $group;
326 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
327 $addr_structure->host
= trim(substr($addr, $at+
1));
329 /* if lookup function */
331 $aAddr = call_user_func_array($lookup,array($addr));
332 if (isset($aAddr['email'])) {
333 $at = strpos($aAddr['email'], '@');
334 $addr_structure->mailbox
= substr($aAddr['email'], 0, $at);
335 $addr_structure->host
= substr($aAddr['email'], $at+
1);
336 if (isset($aAddr['name'])) {
337 $addr_structure->personal
= $aAddr['name'];
341 if (!$addr_structure->mailbox
) {
342 $addr_structure->mailbox
= trim($addr);
344 $addr_structure->host
= $host;
348 if ($group && $addr == '') { /* no addresses found in group */
350 $addr_structure->personal
= $name;
351 $addr_ar[] = $addr_structure;
352 return (array($addr_ar,$pos+
1 ));
354 $addr_structure->personal
= $name;
355 $addr_ar[] = $addr_structure;
356 return (array($addr_ar,$pos+
1 ));
358 $addr_structure->personal
= $name;
359 if ($name ||
$addr) {
360 $addr_ar[] = $addr_structure;
366 return ($addr_ar[0]);
369 function parseContentType($value) {
370 $pos = strpos($value, ';');
373 $type = trim(substr($value, 0, $pos));
374 $props = trim(substr($type, $pos+
1));
378 $content_type = new ContentType($type);
380 $properties = $this->parseProperties($props);
381 if (!isset($properties['charset'])) {
382 $properties['charset'] = 'us-ascii';
384 $content_type->properties
= $this->parseProperties($props);
386 $this->content_type
= $content_type;
389 function parseProperties($value) {
390 $propArray = explode(';', $value);
391 $propResultArray = array();
392 foreach ($propArray as $prop) {
394 $pos = strpos($prop, '=');
396 $key = trim(substr($prop, 0, $pos));
397 $val = trim(substr($prop, $pos+
1));
398 if ($val{0} == '"') {
399 $val = substr($val, 1, -1);
401 $propResultArray[$key] = $val;
404 return $propResultArray;
407 function parseDisposition($value) {
408 $pos = strpos($value, ';');
411 $name = trim(substr($value, 0, $pos));
412 $props = trim(substr($value, $pos+
1));
416 $props_a = $this->parseProperties($props);
417 $disp = new Disposition($name);
418 $disp->properties
= $props_a;
419 $this->disposition
= $disp;
422 function mlist($field, $value) {
424 $value_a = explode(',', $value);
425 foreach ($value_a as $val) {
427 if ($val{0} == '<') {
428 $val = substr($val, 1, -1);
430 if (substr($val, 0, 7) == 'mailto:') {
431 $res_a['mailto'] = substr($val, 7);
433 $res_a['href'] = $val;
436 $this->mlist
[$field] = $res_a;
440 * function to get the addres strings out of the header.
441 * Arguments: string or array of strings !
442 * example1: header->getAddr_s('to').
443 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
445 function getAddr_s($arr, $separator = ',') {
448 if (is_array($arr)) {
449 foreach($arr as $arg) {
450 if ($this->getAddr_s($arg)) {
451 $s .= $separator . $result;
454 $s = ($s ?
substr($s, 2) : $s);
456 eval('$addr = $this->' . $arr . ';') ;
457 if (is_array($addr)) {
458 foreach ($addr as $addr_o) {
459 if (is_object($addr_o)) {
460 $s .= $addr_o->getAddress() . $separator;
463 $s = substr($s, 0, -strlen($separator));
465 if (is_object($addr)) {
466 $s .= $addr->getAddress();
473 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
474 if (is_array($arg)) {
475 foreach($arg as $argument) {
476 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
479 eval('$addr = $this->' . $arg . ';') ;
480 if (is_array($addr)) {
481 foreach ($addr as $next_addr) {
482 if (is_object($next_addr)) {
483 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
484 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
486 $email = $next_addr->mailbox
;
488 $email = strtolower($email);
489 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
490 $arr[$email] = $next_addr->personal
;
495 if (is_object($addr)) {
496 $email = $addr->mailbox
;
497 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
498 $email = strtolower($email);
499 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
500 $arr[$email] = $addr->personal
;
508 function findAddress($address, $recurs = false) {
510 if (is_array($address)) {
512 foreach($address as $argument) {
513 $match = $this->findAddress($argument, true);
518 if (count($match[0]) && !$result) {
525 if (!is_array($this->cc
)) $this->cc
= array();
526 $srch_addr = $this->parseAddress($address);
528 foreach ($this->to
as $to) {
529 if ($to->host
== $srch_addr->host
) {
530 if ($to->mailbox
== $srch_addr->mailbox
) {
531 $results[] = $srch_addr;
532 if ($to->personal
== $srch_addr->personal
) {
534 return array($results, true);
542 foreach ($this->cc
as $cc) {
543 if ($cc->host
== $srch_addr->host
) {
544 if ($cc->mailbox
== $srch_addr->mailbox
) {
545 $results[] = $srch_addr;
546 if ($cc->personal
== $srch_addr->personal
) {
548 return array($results, true);
557 return array($results, false);
558 } elseif (count($result)) {
567 function getContentType($type0, $type1) {
568 $type0 = $this->content_type
->type0
;
569 $type1 = $this->content_type
->type1
;
570 return $this->content_type
->properties
;