Obsolete code because crc32() does exist in our minimum php requirements.
[squirrelmail.git] / class / mime / Rfc822Header.class.php
CommitLineData
19d470aa 1<?php
2
3/**
4 * Rfc822Header.class.php
5 *
76911253 6 * Copyright (c) 2003 The SquirrelMail Project Team
19d470aa 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions needed to handle mime messages.
10 *
11 * $Id$
12 */
13
14/*
15 * rdc822_header class
16 * input: header_string or array
17 */
18class Rfc822Header {
19 var $date = '',
20 $subject = '',
21 $from = array(),
22 $sender = '',
23 $reply_to = array(),
24 $to = array(),
25 $cc = array(),
26 $bcc = array(),
27 $in_reply_to = '',
28 $message_id = '',
238c278c 29 $references = '',
19d470aa 30 $mime = false,
31 $content_type = '',
32 $disposition = '',
33 $xmailer = '',
34 $priority = 3,
35 $dnt = '',
36 $mlist = array(),
37 $more_headers = array(); /* only needed for constructing headers
38 in smtp.php */
39 function parseHeader($hdr) {
40 if (is_array($hdr)) {
41 $hdr = implode('', $hdr);
42 }
43
44 /* First we unfold the header */
45 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr));
46
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, ':');
52 if ($pos > 0) {
53 $field = substr($line, 0, $pos);
d0719411 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);
59 }
60 $this->parseField($field, $value);
61 }
19d470aa 62 }
63 }
64 if ($this->content_type == '') {
65 $this->parseContentType('text/plain; charset=us-ascii');
66 }
67 }
68
69 function stripComments($value) {
70 $result = '';
71
72 $cnt = strlen($value);
73 for ($i = 0; $i < $cnt; ++$i) {
74 switch ($value{$i}) {
75 case '"':
76 $result .= '"';
77 while ((++$i < $cnt) && ($value{$i} != '"')) {
78 if ($value{$i} == '\\') {
79 $result .= '\\';
80 ++$i;
81 }
82 $result .= $value{$i};
83 }
84 $result .= $value{$i};
85 break;
86 case '(':
87 $depth = 1;
88 while (($depth > 0) && (++$i < $cnt)) {
89 switch($value{$i}) {
90 case '\\':
91 ++$i;
92 break;
93 case '(':
94 ++$depth;
95 break;
96 case ')':
97 --$depth;
98 break;
99 default:
100 break;
101 }
102 }
103 break;
104 default:
105 $result .= $value{$i};
106 break;
107 }
108 }
109 return $result;
110 }
111
112 function parseField($field, $value) {
113 $field = strtolower($field);
114 switch($field) {
115 case 'date':
116 $d = strtr($value, array(' ' => ' '));
117 $d = explode(' ', $d);
118 $this->date = getTimeStamp($d);
119 break;
120 case 'subject':
121 $this->subject = $value;
122 break;
123 case 'from':
124 $this->from = $this->parseAddress($value,true);
125 break;
126 case 'sender':
127 $this->sender = $this->parseAddress($value);
128 break;
129 case 'reply-to':
130 $this->reply_to = $this->parseAddress($value, true);
131 break;
132 case 'to':
133 $this->to = $this->parseAddress($value, true);
134 break;
135 case 'cc':
136 $this->cc = $this->parseAddress($value, true);
137 break;
138 case 'bcc':
139 $this->bcc = $this->parseAddress($value, true);
140 break;
141 case 'in-reply-to':
142 $this->in_reply_to = $value;
143 break;
144 case 'message-id':
145 $this->message_id = $value;
146 break;
238c278c 147 case 'references':
148 $this->references = $value;
149 break;
19d470aa 150 case 'disposition-notification-to':
151 $this->dnt = $this->parseAddress($value);
152 break;
153 case 'mime-version':
154 $value = str_replace(' ', '', $value);
155 $this->mime = ($value == '1.0' ? true : $this->mime);
156 break;
157 case 'content-type':
158 $this->parseContentType($value);
159 break;
160 case 'content-disposition':
161 $this->parseDisposition($value);
162 break;
163 case 'user-agent':
164 case 'x-mailer':
165 $this->xmailer = $value;
166 break;
167 case 'x-priority':
168 $this->priority = $value;
169 break;
170 case 'list-post':
171 $this->mlist('post', $value);
172 break;
173 case 'list-reply':
174 $this->mlist('reply', $value);
175 break;
176 case 'list-subscribe':
177 $this->mlist('subscribe', $value);
178 break;
179 case 'list-unsubscribe':
180 $this->mlist('unsubscribe', $value);
181 break;
182 case 'list-archive':
183 $this->mlist('archive', $value);
184 break;
185 case 'list-owner':
186 $this->mlist('owner', $value);
187 break;
188 case 'list-help':
189 $this->mlist('help', $value);
190 break;
191 case 'list-id':
192 $this->mlist('id', $value);
193 break;
194 default:
195 break;
196 }
197 }
e74ba378 198 /*
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
203 * first element)
204 * $addr_ar = array with parsed addresses
205 * $group = string
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
210 * (without @ ) )
211 * output: array with addressstructure objects or only one
212 * address_structure object.
213 */
19d470aa 214 function parseAddress
e74ba378 215 ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) {
19d470aa 216 $pos = 0;
217 $j = strlen($address);
218 $name = '';
219 $addr = '';
220 while ($pos < $j) {
221 switch ($address{$pos}) {
222 case '"': /* get the personal name */
223 if ($address{++$pos} == '"') {
224 ++$pos;
225 } else {
226 while ($pos < $j && $address{$pos} != '"') {
227 if ((substr($address, $pos, 2) == '\\"') ||
228 (substr($address, $pos, 2) == '\\\\')) {
229 $name .= $address{$pos++};
230 }
231 $name .= $address{$pos++};
232 }
233 }
234 ++$pos;
235 break;
236 case '<': /* get email address */
237 $addr_start = $pos++;
238 while ($pos < $j && $address{$pos} != '>') {
239 $addr .= $address{$pos++};
240 }
241 ++$pos;
242 break;
243 case '(': /* rip off comments */
244 $addr_start = $pos;
245 for (++$pos; ($pos < $j) && ($address{$pos} != ')'); ++$pos) {
246 $addr .= $address{$pos};
247 }
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;
253 break;
254 case ',': /* we reached a delimiter */
255 if ($addr == '') {
256 $addr = substr($address, 0, $pos);
257 } else if ($name == '') {
258 $name = trim(substr($address, 0, $addr_start));
259 }
260
261 $at = strpos($addr, '@');
262 $addr_structure = new AddressStructure();
263 $addr_structure->personal = $name;
264 $addr_structure->group = $group;
265 if ($at) {
266 $addr_structure->mailbox = substr($addr, 0, $at);
267 $addr_structure->host = substr($addr, $at+1);
268 } else {
e74ba378 269 /* if lookup function */
270 if ($lookup) {
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'];
278 }
279 }
280 }
ec0e3bf0 281 if (!$addr_structure->mailbox) {
e74ba378 282 $addr_structure->mailbox = trim($addr);
283 if ($host) {
284 $addr_structure->host = $host;
285 }
238c278c 286 }
19d470aa 287 }
288 $address = trim(substr($address, $pos+1));
289 $j = strlen($address);
290 $pos = 0;
291 $name = '';
292 $addr = '';
293 $addr_ar[] = $addr_structure;
294 break;
295 case ':': /* process the group addresses */
296 /* group marker */
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];
301 $pos = $result[1];
302 $address = substr($address, $pos++);
303 $j = strlen($address);
304 $group = '';
305 break;
306 case ';':
307 if ($group) {
308 $address = substr($address, 0, $pos - 1);
309 }
310 ++$pos;
311 break;
312 default:
313 ++$pos;
314 break;
315 }
316 }
317 if ($addr == '') {
318 $addr = substr($address, 0, $pos);
319 } else if ($name == '') {
320 $name = trim(substr($address, 0, $addr_start));
321 }
322 $at = strpos($addr, '@');
323 $addr_structure = new AddressStructure();
324 $addr_structure->group = $group;
325 if ($at) {
326 $addr_structure->mailbox = trim(substr($addr, 0, $at));
327 $addr_structure->host = trim(substr($addr, $at+1));
328 } else {
e74ba378 329 /* if lookup function */
330 if ($lookup) {
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'];
338 }
339 }
340 }
ec0e3bf0 341 if (!$addr_structure->mailbox) {
e74ba378 342 $addr_structure->mailbox = trim($addr);
343 if ($host) {
344 $addr_structure->host = $host;
345 }
238c278c 346 }
19d470aa 347 }
348 if ($group && $addr == '') { /* no addresses found in group */
238c278c 349 $name = "$group";
19d470aa 350 $addr_structure->personal = $name;
351 $addr_ar[] = $addr_structure;
085103f0 352 return (array($addr_ar,$pos+1 ));
353 } elseif ($group) {
354 $addr_structure->personal = $name;
355 $addr_ar[] = $addr_structure;
356 return (array($addr_ar,$pos+1 ));
19d470aa 357 } else {
358 $addr_structure->personal = $name;
359 if ($name || $addr) {
360 $addr_ar[] = $addr_structure;
361 }
362 }
363 if ($ar) {
364 return ($addr_ar);
365 }
366 return ($addr_ar[0]);
367 }
368
369 function parseContentType($value) {
370 $pos = strpos($value, ';');
371 $props = '';
372 if ($pos > 0) {
373 $type = trim(substr($value, 0, $pos));
374 $props = trim(substr($type, $pos+1));
375 } else {
376 $type = $value;
377 }
378 $content_type = new ContentType($type);
379 if ($props) {
380 $properties = $this->parseProperties($props);
381 if (!isset($properties['charset'])) {
382 $properties['charset'] = 'us-ascii';
383 }
384 $content_type->properties = $this->parseProperties($props);
385 }
386 $this->content_type = $content_type;
387 }
388
389 function parseProperties($value) {
390 $propArray = explode(';', $value);
391 $propResultArray = array();
392 foreach ($propArray as $prop) {
393 $prop = trim($prop);
394 $pos = strpos($prop, '=');
395 if ($pos > 0) {
396 $key = trim(substr($prop, 0, $pos));
397 $val = trim(substr($prop, $pos+1));
398 if ($val{0} == '"') {
399 $val = substr($val, 1, -1);
400 }
401 $propResultArray[$key] = $val;
402 }
403 }
404 return $propResultArray;
405 }
406
407 function parseDisposition($value) {
408 $pos = strpos($value, ';');
409 $props = '';
410 if ($pos > 0) {
411 $name = trim(substr($value, 0, $pos));
fc9269ec 412 $props = trim(substr($value, $pos+1));
19d470aa 413 } else {
414 $name = $value;
415 }
416 $props_a = $this->parseProperties($props);
417 $disp = new Disposition($name);
418 $disp->properties = $props_a;
419 $this->disposition = $disp;
420 }
421
422 function mlist($field, $value) {
423 $res_a = array();
424 $value_a = explode(',', $value);
425 foreach ($value_a as $val) {
426 $val = trim($val);
427 if ($val{0} == '<') {
428 $val = substr($val, 1, -1);
429 }
430 if (substr($val, 0, 7) == 'mailto:') {
431 $res_a['mailto'] = substr($val, 7);
432 } else {
433 $res_a['href'] = $val;
434 }
435 }
436 $this->mlist[$field] = $res_a;
437 }
438
439 /*
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'))
444 */
445 function getAddr_s($arr, $separator = ',') {
446 $s = '';
447
448 if (is_array($arr)) {
449 foreach($arr as $arg) {
450 if ($this->getAddr_s($arg)) {
451 $s .= $separator . $result;
452 }
453 }
454 $s = ($s ? substr($s, 2) : $s);
455 } else {
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;
461 }
462 }
463 $s = substr($s, 0, -strlen($separator));
464 } else {
465 if (is_object($addr)) {
466 $s .= $addr->getAddress();
467 }
468 }
469 }
470 return $s;
471 }
472
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);
477 }
478 } else {
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;
485 } else {
486 $email = $next_addr->mailbox;
487 }
488 $email = strtolower($email);
489 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
490 $arr[$email] = $next_addr->personal;
491 }
492 }
493 }
494 } else {
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;
501 }
502 }
503 }
504 }
505 return $arr;
506 }
d0719411 507
508 function findAddress($address, $recurs = false) {
509 $result = false;
510 if (is_array($address)) {
511 $i=0;
512 foreach($address as $argument) {
513 $match = $this->findAddress($argument, true);
514 $last = end($match);
515 if ($match[1]) {
516 return $i;
517 } else {
518 if (count($match[0]) && !$result) {
519 $result = $i;
520 }
521 }
522 ++$i;
523 }
524 } else {
03d73e64 525 if (!is_array($this->cc)) $this->cc = array();
d0719411 526 $srch_addr = $this->parseAddress($address);
527 $results = array();
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) {
533 if ($recurs) {
534 return array($results, true);
535 } else {
536 return true;
537 }
538 }
539 }
540 }
541 }
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) {
547 if ($recurs) {
548 return array($results, true);
549 } else {
550 return true;
551 }
552 }
553 }
554 }
555 }
556 if ($recurs) {
557 return array($results, false);
558 } elseif (count($result)) {
559 return true;
560 } else {
561 return false;
562 }
563 }
564 return $result;
565 }
19d470aa 566
567 function getContentType($type0, $type1) {
568 $type0 = $this->content_type->type0;
569 $type1 = $this->content_type->type1;
570 return $this->content_type->properties;
571 }
572}
573
574?>