a744b9144f12acaa8f1171b8fd3e8096c97449c7
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 $value = trim(substr($line, $pos+
1));
55 if(!preg_match('/^X.*/i', $field) &&
56 !preg_match('/^Subject/i', $field)) {
57 $value = $this->stripComments($value);
59 $this->parseField($field, $value);
62 if ($this->content_type
== '') {
63 $this->parseContentType('text/plain; charset=us-ascii');
67 function stripComments($value) {
70 $cnt = strlen($value);
71 for ($i = 0; $i < $cnt; ++
$i) {
75 while ((++
$i < $cnt) && ($value{$i} != '"')) {
76 if ($value{$i} == '\\') {
80 $result .= $value{$i};
82 $result .= $value{$i};
86 while (($depth > 0) && (++
$i < $cnt)) {
103 $result .= $value{$i};
110 function parseField($field, $value) {
111 $field = strtolower($field);
114 $d = strtr($value, array(' ' => ' '));
115 $d = explode(' ', $d);
116 $this->date
= getTimeStamp($d);
119 $this->subject
= $value;
122 $this->from
= $this->parseAddress($value,true);
125 $this->sender
= $this->parseAddress($value);
128 $this->reply_to
= $this->parseAddress($value, true);
131 $this->to
= $this->parseAddress($value, true);
134 $this->cc
= $this->parseAddress($value, true);
137 $this->bcc
= $this->parseAddress($value, true);
140 $this->in_reply_to
= $value;
143 $this->message_id
= $value;
146 $this->references
= $value;
148 case 'disposition-notification-to':
149 $this->dnt
= $this->parseAddress($value);
152 $value = str_replace(' ', '', $value);
153 $this->mime
= ($value == '1.0' ?
true : $this->mime
);
156 $this->parseContentType($value);
158 case 'content-disposition':
159 $this->parseDisposition($value);
163 $this->xmailer
= $value;
166 $this->priority
= $value;
169 $this->mlist('post', $value);
172 $this->mlist('reply', $value);
174 case 'list-subscribe':
175 $this->mlist('subscribe', $value);
177 case 'list-unsubscribe':
178 $this->mlist('unsubscribe', $value);
181 $this->mlist('archive', $value);
184 $this->mlist('owner', $value);
187 $this->mlist('help', $value);
190 $this->mlist('id', $value);
197 function parseAddress
198 ($address, $ar=false, $addr_ar = array(), $group = '', $host='') {
200 $j = strlen($address);
204 switch ($address{$pos}) {
205 case '"': /* get the personal name */
206 if ($address{++
$pos} == '"') {
209 while ($pos < $j && $address{$pos} != '"') {
210 if ((substr($address, $pos, 2) == '\\"') ||
211 (substr($address, $pos, 2) == '\\\\')) {
212 $name .= $address{$pos++
};
214 $name .= $address{$pos++
};
219 case '<': /* get email address */
220 $addr_start = $pos++
;
221 while ($pos < $j && $address{$pos} != '>') {
222 $addr .= $address{$pos++
};
226 case '(': /* rip off comments */
228 for (++
$pos; ($pos < $j) && ($address{$pos} != ')'); ++
$pos) {
229 $addr .= $address{$pos};
231 $address_start = substr($address, 0, $addr_start);
232 $address_end = substr($address, $pos +
1);
233 $address = $address_start . $address_end;
234 $j = strlen($address);
235 $pos = $addr_start +
1;
237 case ',': /* we reached a delimiter */
240 $addr = substr($address, 0, $pos);
241 } else if ($name == '') {
242 $name = trim(substr($address, 0, $addr_start));
245 $at = strpos($addr, '@');
246 $addr_structure = new AddressStructure();
247 $addr_structure->personal
= $name;
248 $addr_structure->group
= $group;
250 $addr_structure->mailbox
= substr($addr, 0, $at);
251 $addr_structure->host
= substr($addr, $at+
1);
253 $addr_structure->mailbox
= $addr;
255 $addr_structure->host
= $host;
258 $address = trim(substr($address, $pos+
1));
259 $j = strlen($address);
263 $addr_ar[] = $addr_structure;
265 case ':': /* process the group addresses */
267 $group = substr($address, 0, $pos);
268 $address = substr($address, $pos+
1);
269 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
270 $addr_ar = $result[0];
272 $address = substr($address, $pos++
);
273 $j = strlen($address);
278 $address = substr($address, 0, $pos - 1);
288 $addr = substr($address, 0, $pos);
289 } else if ($name == '') {
290 $name = trim(substr($address, 0, $addr_start));
292 $at = strpos($addr, '@');
293 $addr_structure = new AddressStructure();
294 $addr_structure->group
= $group;
296 $addr_structure->mailbox
= trim(substr($addr, 0, $at));
297 $addr_structure->host
= trim(substr($addr, $at+
1));
299 $addr_structure->mailbox
= trim($addr);
301 $addr_structure->host
= $host;
304 if ($group && $addr == '') { /* no addresses found in group */
306 $addr_structure->personal
= $name;
307 $addr_ar[] = $addr_structure;
308 return (array($addr_ar,$pos+
1 ));
310 $addr_structure->personal
= $name;
311 $addr_ar[] = $addr_structure;
312 return (array($addr_ar,$pos+
1 ));
314 $addr_structure->personal
= $name;
315 if ($name ||
$addr) {
316 $addr_ar[] = $addr_structure;
322 return ($addr_ar[0]);
325 function parseContentType($value) {
326 $pos = strpos($value, ';');
329 $type = trim(substr($value, 0, $pos));
330 $props = trim(substr($type, $pos+
1));
334 $content_type = new ContentType($type);
336 $properties = $this->parseProperties($props);
337 if (!isset($properties['charset'])) {
338 $properties['charset'] = 'us-ascii';
340 $content_type->properties
= $this->parseProperties($props);
342 $this->content_type
= $content_type;
345 function parseProperties($value) {
346 $propArray = explode(';', $value);
347 $propResultArray = array();
348 foreach ($propArray as $prop) {
350 $pos = strpos($prop, '=');
352 $key = trim(substr($prop, 0, $pos));
353 $val = trim(substr($prop, $pos+
1));
354 if ($val{0} == '"') {
355 $val = substr($val, 1, -1);
357 $propResultArray[$key] = $val;
360 return $propResultArray;
363 function parseDisposition($value) {
364 $pos = strpos($value, ';');
367 $name = trim(substr($value, 0, $pos));
368 $props = trim(substr($value, $pos+
1));
372 $props_a = $this->parseProperties($props);
373 $disp = new Disposition($name);
374 $disp->properties
= $props_a;
375 $this->disposition
= $disp;
378 function mlist($field, $value) {
380 $value_a = explode(',', $value);
381 foreach ($value_a as $val) {
383 if ($val{0} == '<') {
384 $val = substr($val, 1, -1);
386 if (substr($val, 0, 7) == 'mailto:') {
387 $res_a['mailto'] = substr($val, 7);
389 $res_a['href'] = $val;
392 $this->mlist
[$field] = $res_a;
396 * function to get the addres strings out of the header.
397 * Arguments: string or array of strings !
398 * example1: header->getAddr_s('to').
399 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
401 function getAddr_s($arr, $separator = ',') {
404 if (is_array($arr)) {
405 foreach($arr as $arg) {
406 if ($this->getAddr_s($arg)) {
407 $s .= $separator . $result;
410 $s = ($s ?
substr($s, 2) : $s);
412 eval('$addr = $this->' . $arr . ';') ;
413 if (is_array($addr)) {
414 foreach ($addr as $addr_o) {
415 if (is_object($addr_o)) {
416 $s .= $addr_o->getAddress() . $separator;
419 $s = substr($s, 0, -strlen($separator));
421 if (is_object($addr)) {
422 $s .= $addr->getAddress();
429 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
430 if (is_array($arg)) {
431 foreach($arg as $argument) {
432 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
435 eval('$addr = $this->' . $arg . ';') ;
436 if (is_array($addr)) {
437 foreach ($addr as $next_addr) {
438 if (is_object($next_addr)) {
439 if (isset($next_addr->host
) && ($next_addr->host
!= '')) {
440 $email = $next_addr->mailbox
. '@' . $next_addr->host
;
442 $email = $next_addr->mailbox
;
444 $email = strtolower($email);
445 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
446 $arr[$email] = $next_addr->personal
;
451 if (is_object($addr)) {
452 $email = $addr->mailbox
;
453 $email .= (isset($addr->host
) ?
'@' . $addr->host
: '');
454 $email = strtolower($email);
455 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
456 $arr[$email] = $addr->personal
;
464 function getContentType($type0, $type1) {
465 $type0 = $this->content_type
->type0
;
466 $type1 = $this->content_type
->type1
;
467 return $this->content_type
->properties
;