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
36 $more_headers = array(); /* only needed for constructing headers
38 function parseHeader($hdr) {
40 $hdr = implode('', $hdr);
43 /* First we unfold the header */
44 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr));
46 /* Now we can make a new header array with */
47 /* each element representing a headerline */
48 $hdr = explode("\r\n" , $hdr);
49 foreach ($hdr as $line) {
50 $pos = strpos($line, ':');
52 $field = substr($line, 0, $pos);
53 $value = trim(substr($line, $pos+
1));
54 if(!preg_match('/^X.*/i', $field) &&
55 !preg_match('/^Subject/i', $field)) {
56 $value = $this->stripComments($value);
58 $this->parseField($field, $value);
61 if ($this->content_type
== '') {
62 $this->parseContentType('text/plain; charset=us-ascii');
66 function stripComments($value) {
69 $cnt = strlen($value);
70 for ($i = 0; $i < $cnt; ++
$i) {
74 while ((++
$i < $cnt) && ($value{$i} != '"')) {
75 if ($value{$i} == '\\') {
79 $result .= $value{$i};
81 $result .= $value{$i};
85 while (($depth > 0) && (++
$i < $cnt)) {
102 $result .= $value{$i};
109 function parseField($field, $value) {
110 $field = strtolower($field);
113 $d = strtr($value, array(' ' => ' '));
114 $d = explode(' ', $d);
115 $this->date
= getTimeStamp($d);
118 $this->subject
= $value;
121 $this->from
= $this->parseAddress($value,true);
124 $this->sender
= $this->parseAddress($value);
127 $this->reply_to
= $this->parseAddress($value, true);
130 $this->to
= $this->parseAddress($value, true);
133 $this->cc
= $this->parseAddress($value, true);
136 $this->bcc
= $this->parseAddress($value, true);
139 $this->in_reply_to
= $value;
142 $this->message_id
= $value;
144 case 'disposition-notification-to':
145 $this->dnt
= $this->parseAddress($value);
148 $value = str_replace(' ', '', $value);
149 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
152 $this->parseContentType($value);
154 case 'content-disposition':
155 $this->parseDisposition($value);
159 $this->xmailer
= $value;
162 $this->priority
= $value;
165 $this->mlist('post', $value);
168 $this->mlist('reply', $value);
170 case 'list-subscribe':
171 $this->mlist('subscribe', $value);
173 case 'list-unsubscribe':
174 $this->mlist('unsubscribe', $value);
177 $this->mlist('archive', $value);
180 $this->mlist('owner', $value);
183 $this->mlist('help', $value);
186 $this->mlist('id', $value);
193 function parseAddress
194 ($address, $ar=false, $addr_ar = array(), $group = '') {
196 $j = strlen($address);
200 switch ($address{$pos}) {
201 case '"': /* get the personal name */
202 if ($address{++
$pos} == '"') {
205 while ($pos < $j && $address{$pos} != '"') {
206 if ((substr($address, $pos, 2) == '\\"') ||
207 (substr($address, $pos, 2) == '\\\\')) {
208 $name .= $address{$pos++
};
210 $name .= $address{$pos++
};
215 case '<': /* get email address */
216 $addr_start = $pos++
;
217 while ($pos < $j && $address{$pos} != '>') {
218 $addr .= $address{$pos++
};
222 case '(': /* rip off comments */
224 for (++
$pos; ($pos < $j) && ($address{$pos} != ')'); ++
$pos) {
225 $addr .= $address{$pos};
227 $address_start = substr($address, 0, $addr_start);
228 $address_end = substr($address, $pos +
1);
229 $address = $address_start . $address_end;
230 $j = strlen($address);
231 $pos = $addr_start +
1;
233 case ',': /* we reached a delimiter */
235 $addr = substr($address, 0, $pos);
236 } else if ($name == '') {
237 $name = trim(substr($address, 0, $addr_start));
240 $at = strpos($addr, '@');
241 $addr_structure = new AddressStructure();
242 $addr_structure->personal
= $name;
243 $addr_structure->group
= $group;
245 $addr_structure->mailbox
= substr($addr, 0, $at);
246 $addr_structure->host
= substr($addr, $at+
1);
248 $addr_structure->mailbox
= $addr;
250 $address = trim(substr($address, $pos+
1));
251 $j = strlen($address);
255 $addr_ar[] = $addr_structure;
257 case ':': /* process the group addresses */
259 $group = substr($address, 0, $pos);
260 $address = substr($address, $pos+
1);
261 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
262 $addr_ar = $result[0];
264 $address = substr($address, $pos++
);
265 $j = strlen($address);
270 $address = substr($address, 0, $pos - 1);
280 $addr = substr($address, 0, $pos);
281 } else if ($name == '') {
282 $name = trim(substr($address, 0, $addr_start));
284 $at = strpos($addr, '@');
285 $addr_structure = new AddressStructure();
286 $addr_structure->group
= $group;
288 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
289 $addr_structure->host
= trim(substr($addr, $at+
1));
291 $addr_structure->mailbox
= trim($addr);
293 if ($group && $addr == '') { /* no addresses found in group */
294 $name = "$group: Undisclosed recipients;";
295 $addr_structure->personal
= $name;
296 $addr_ar[] = $addr_structure;
297 return (array($addr_ar, $pos+
1));
299 $addr_structure->personal
= $name;
300 if ($name ||
$addr) {
301 $addr_ar[] = $addr_structure;
307 return ($addr_ar[0]);
310 function parseContentType($value) {
311 $pos = strpos($value, ';');
314 $type = trim(substr($value, 0, $pos));
315 $props = trim(substr($type, $pos+
1));
319 $content_type = new ContentType($type);
321 $properties = $this->parseProperties($props);
322 if (!isset($properties['charset'])) {
323 $properties['charset'] = 'us-ascii';
325 $content_type->properties
= $this->parseProperties($props);
327 $this->content_type
= $content_type;
330 function parseProperties($value) {
331 $propArray = explode(';', $value);
332 $propResultArray = array();
333 foreach ($propArray as $prop) {
335 $pos = strpos($prop, '=');
337 $key = trim(substr($prop, 0, $pos));
338 $val = trim(substr($prop, $pos+
1));
339 if ($val{0} == '"') {
340 $val = substr($val, 1, -1);
342 $propResultArray[$key] = $val;
345 return $propResultArray;
348 function parseDisposition($value) {
349 $pos = strpos($value, ';');
352 $name = trim(substr($value, 0, $pos));
353 $props = trim(substr($type, $pos+
1));
357 $props_a = $this->parseProperties($props);
358 $disp = new Disposition($name);
359 $disp->properties
= $props_a;
360 $this->disposition
= $disp;
363 function mlist($field, $value) {
365 $value_a = explode(',', $value);
366 foreach ($value_a as $val) {
368 if ($val{0} == '<') {
369 $val = substr($val, 1, -1);
371 if (substr($val, 0, 7) == 'mailto:') {
372 $res_a['mailto'] = substr($val, 7);
374 $res_a['href'] = $val;
377 $this->mlist
[$field] = $res_a;
381 * function to get the addres strings out of the header.
382 * Arguments: string or array of strings !
383 * example1: header->getAddr_s('to').
384 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
386 function getAddr_s($arr, $separator = ',') {
389 if (is_array($arr)) {
390 foreach($arr as $arg) {
391 if ($this->getAddr_s($arg)) {
392 $s .= $separator . $result;
395 $s = ($s ?
substr($s, 2) : $s);
397 eval('$addr = $this->' . $arr . ';') ;
398 if (is_array($addr)) {
399 foreach ($addr as $addr_o) {
400 if (is_object($addr_o)) {
401 $s .= $addr_o->getAddress() . $separator;
404 $s = substr($s, 0, -strlen($separator));
406 if (is_object($addr)) {
407 $s .= $addr->getAddress();
414 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
415 if (is_array($arg)) {
416 foreach($arg as $argument) {
417 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
420 eval('$addr = $this->' . $arg . ';') ;
421 if (is_array($addr)) {
422 foreach ($addr as $next_addr) {
423 if (is_object($next_addr)) {
424 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
425 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
427 $email = $next_addr->mailbox
;
429 $email = strtolower($email);
430 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
431 $arr[$email] = $next_addr->personal
;
436 if (is_object($addr)) {
437 $email = $addr->mailbox
;
438 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
439 $email = strtolower($email);
440 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
441 $arr[$email] = $addr->personal
;
449 function getContentType($type0, $type1) {
450 $type0 = $this->content_type
->type0
;
451 $type1 = $this->content_type
->type1
;
452 return $this->content_type
->properties
;