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 | */ |
18 | class 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)); |
d0719411 |
56 | $this->parseField($field, $value); |
57 | } |
19d470aa |
58 | } |
59 | } |
60 | if ($this->content_type == '') { |
61 | $this->parseContentType('text/plain; charset=us-ascii'); |
62 | } |
63 | } |
64 | |
65 | function stripComments($value) { |
66 | $result = ''; |
19d470aa |
67 | $cnt = strlen($value); |
68 | for ($i = 0; $i < $cnt; ++$i) { |
69 | switch ($value{$i}) { |
70 | case '"': |
71 | $result .= '"'; |
72 | while ((++$i < $cnt) && ($value{$i} != '"')) { |
73 | if ($value{$i} == '\\') { |
74 | $result .= '\\'; |
75 | ++$i; |
76 | } |
77 | $result .= $value{$i}; |
78 | } |
79 | $result .= $value{$i}; |
80 | break; |
81 | case '(': |
82 | $depth = 1; |
83 | while (($depth > 0) && (++$i < $cnt)) { |
84 | switch($value{$i}) { |
85 | case '\\': |
86 | ++$i; |
87 | break; |
88 | case '(': |
89 | ++$depth; |
90 | break; |
91 | case ')': |
92 | --$depth; |
93 | break; |
94 | default: |
95 | break; |
96 | } |
97 | } |
98 | break; |
99 | default: |
100 | $result .= $value{$i}; |
101 | break; |
102 | } |
103 | } |
104 | return $result; |
105 | } |
106 | |
107 | function parseField($field, $value) { |
108 | $field = strtolower($field); |
109 | switch($field) { |
110 | case 'date': |
1465f80c |
111 | $value = $this->stripComments($value); |
19d470aa |
112 | $d = strtr($value, array(' ' => ' ')); |
113 | $d = explode(' ', $d); |
114 | $this->date = getTimeStamp($d); |
115 | break; |
116 | case 'subject': |
117 | $this->subject = $value; |
118 | break; |
119 | case 'from': |
120 | $this->from = $this->parseAddress($value,true); |
121 | break; |
122 | case 'sender': |
123 | $this->sender = $this->parseAddress($value); |
124 | break; |
125 | case 'reply-to': |
126 | $this->reply_to = $this->parseAddress($value, true); |
127 | break; |
128 | case 'to': |
129 | $this->to = $this->parseAddress($value, true); |
130 | break; |
131 | case 'cc': |
132 | $this->cc = $this->parseAddress($value, true); |
133 | break; |
134 | case 'bcc': |
135 | $this->bcc = $this->parseAddress($value, true); |
136 | break; |
137 | case 'in-reply-to': |
138 | $this->in_reply_to = $value; |
139 | break; |
140 | case 'message-id': |
1465f80c |
141 | $value = $this->stripComments($value); |
19d470aa |
142 | $this->message_id = $value; |
143 | break; |
238c278c |
144 | case 'references': |
1465f80c |
145 | $value = $this->stripComments($value); |
238c278c |
146 | $this->references = $value; |
147 | break; |
19d470aa |
148 | case 'disposition-notification-to': |
1465f80c |
149 | $value = $this->stripComments($value); |
19d470aa |
150 | $this->dnt = $this->parseAddress($value); |
151 | break; |
152 | case 'mime-version': |
1465f80c |
153 | $value = $this->stripComments($value); |
19d470aa |
154 | $value = str_replace(' ', '', $value); |
155 | $this->mime = ($value == '1.0' ? true : $this->mime); |
156 | break; |
157 | case 'content-type': |
1465f80c |
158 | $value = $this->stripComments($value); |
19d470aa |
159 | $this->parseContentType($value); |
160 | break; |
161 | case 'content-disposition': |
1465f80c |
162 | $value = $this->stripComments($value); |
19d470aa |
163 | $this->parseDisposition($value); |
164 | break; |
165 | case 'user-agent': |
166 | case 'x-mailer': |
1465f80c |
167 | $this->xmailer = $value; |
19d470aa |
168 | break; |
169 | case 'x-priority': |
170 | $this->priority = $value; |
171 | break; |
172 | case 'list-post': |
1465f80c |
173 | $value = $this->stripComments($value); |
19d470aa |
174 | $this->mlist('post', $value); |
175 | break; |
176 | case 'list-reply': |
1465f80c |
177 | $value = $this->stripComments($value); |
19d470aa |
178 | $this->mlist('reply', $value); |
179 | break; |
180 | case 'list-subscribe': |
1465f80c |
181 | $value = $this->stripComments($value); |
19d470aa |
182 | $this->mlist('subscribe', $value); |
183 | break; |
184 | case 'list-unsubscribe': |
1465f80c |
185 | $value = $this->stripComments($value); |
19d470aa |
186 | $this->mlist('unsubscribe', $value); |
187 | break; |
188 | case 'list-archive': |
1465f80c |
189 | $value = $this->stripComments($value); |
19d470aa |
190 | $this->mlist('archive', $value); |
191 | break; |
192 | case 'list-owner': |
1465f80c |
193 | $value = $this->stripComments($value); |
19d470aa |
194 | $this->mlist('owner', $value); |
195 | break; |
196 | case 'list-help': |
1465f80c |
197 | $value = $this->stripComments($value); |
19d470aa |
198 | $this->mlist('help', $value); |
199 | break; |
200 | case 'list-id': |
1465f80c |
201 | $value = $this->stripComments($value); |
19d470aa |
202 | $this->mlist('id', $value); |
203 | break; |
204 | default: |
205 | break; |
206 | } |
207 | } |
e74ba378 |
208 | /* |
209 | * parseAddress: recursive function for parsing address strings and store |
210 | * them in an address stucture object. |
211 | * input: $address = string |
212 | * $ar = boolean (return array instead of only the |
213 | * first element) |
214 | * $addr_ar = array with parsed addresses |
215 | * $group = string |
216 | * $host = string (default domainname in case of |
217 | * addresses without a domainname) |
218 | * $lookup = callback function (for lookup address |
219 | * strings which are probably nicks |
220 | * (without @ ) ) |
221 | * output: array with addressstructure objects or only one |
222 | * address_structure object. |
223 | */ |
19d470aa |
224 | function parseAddress |
e74ba378 |
225 | ($address, $ar=false, $addr_ar = array(), $group = '', $host='',$lookup=false) { |
19d470aa |
226 | $pos = 0; |
227 | $j = strlen($address); |
228 | $name = ''; |
229 | $addr = ''; |
1465f80c |
230 | $comment = ''; |
19d470aa |
231 | while ($pos < $j) { |
232 | switch ($address{$pos}) { |
233 | case '"': /* get the personal name */ |
234 | if ($address{++$pos} == '"') { |
235 | ++$pos; |
236 | } else { |
237 | while ($pos < $j && $address{$pos} != '"') { |
238 | if ((substr($address, $pos, 2) == '\\"') || |
239 | (substr($address, $pos, 2) == '\\\\')) { |
240 | $name .= $address{$pos++}; |
241 | } |
242 | $name .= $address{$pos++}; |
243 | } |
244 | } |
245 | ++$pos; |
246 | break; |
247 | case '<': /* get email address */ |
248 | $addr_start = $pos++; |
249 | while ($pos < $j && $address{$pos} != '>') { |
250 | $addr .= $address{$pos++}; |
251 | } |
252 | ++$pos; |
253 | break; |
254 | case '(': /* rip off comments */ |
255 | $addr_start = $pos; |
1465f80c |
256 | $pos = strpos($address,')'); |
257 | if ($pos !== false) { |
258 | $comment = substr($address, $addr_start+1,($pos-$addr_start-1)); |
259 | $address_start = substr($address, 0, $addr_start); |
260 | $address_end = substr($address, $pos + 1); |
261 | $address = $address_start . $address_end; |
262 | } |
19d470aa |
263 | $j = strlen($address); |
264 | $pos = $addr_start + 1; |
265 | break; |
266 | case ',': /* we reached a delimiter */ |
267 | if ($addr == '') { |
268 | $addr = substr($address, 0, $pos); |
269 | } else if ($name == '') { |
270 | $name = trim(substr($address, 0, $addr_start)); |
271 | } |
272 | |
273 | $at = strpos($addr, '@'); |
274 | $addr_structure = new AddressStructure(); |
1465f80c |
275 | if (!$name && $comment) $name = $comment; |
19d470aa |
276 | $addr_structure->personal = $name; |
277 | $addr_structure->group = $group; |
278 | if ($at) { |
279 | $addr_structure->mailbox = substr($addr, 0, $at); |
280 | $addr_structure->host = substr($addr, $at+1); |
281 | } else { |
e74ba378 |
282 | /* if lookup function */ |
283 | if ($lookup) { |
284 | $aAddr = call_user_func_array($lookup,array($addr)); |
285 | if (isset($aAddr['email'])) { |
286 | $at = strpos($aAddr['email'], '@'); |
287 | $addr_structure->mailbox = substr($aAddr['email'], 0, $at); |
288 | $addr_structure->host = substr($aAddr['email'], $at+1); |
289 | if (isset($aAddr['name'])) { |
290 | $addr_structure->personal = $aAddr['name']; |
291 | } |
292 | } |
293 | } |
ec0e3bf0 |
294 | if (!$addr_structure->mailbox) { |
e74ba378 |
295 | $addr_structure->mailbox = trim($addr); |
296 | if ($host) { |
297 | $addr_structure->host = $host; |
298 | } |
238c278c |
299 | } |
19d470aa |
300 | } |
301 | $address = trim(substr($address, $pos+1)); |
302 | $j = strlen($address); |
303 | $pos = 0; |
304 | $name = ''; |
305 | $addr = ''; |
306 | $addr_ar[] = $addr_structure; |
307 | break; |
308 | case ':': /* process the group addresses */ |
309 | /* group marker */ |
310 | $group = substr($address, 0, $pos); |
311 | $address = substr($address, $pos+1); |
312 | $result = $this->parseAddress($address, $ar, $addr_ar, $group); |
313 | $addr_ar = $result[0]; |
314 | $pos = $result[1]; |
315 | $address = substr($address, $pos++); |
316 | $j = strlen($address); |
317 | $group = ''; |
318 | break; |
319 | case ';': |
320 | if ($group) { |
321 | $address = substr($address, 0, $pos - 1); |
322 | } |
323 | ++$pos; |
324 | break; |
325 | default: |
326 | ++$pos; |
327 | break; |
328 | } |
329 | } |
330 | if ($addr == '') { |
331 | $addr = substr($address, 0, $pos); |
332 | } else if ($name == '') { |
333 | $name = trim(substr($address, 0, $addr_start)); |
334 | } |
1465f80c |
335 | if (!$name && $comment) $name = $comment; |
19d470aa |
336 | $at = strpos($addr, '@'); |
337 | $addr_structure = new AddressStructure(); |
338 | $addr_structure->group = $group; |
339 | if ($at) { |
340 | $addr_structure->mailbox = trim(substr($addr, 0, $at)); |
341 | $addr_structure->host = trim(substr($addr, $at+1)); |
342 | } else { |
e74ba378 |
343 | /* if lookup function */ |
344 | if ($lookup) { |
345 | $aAddr = call_user_func_array($lookup,array($addr)); |
346 | if (isset($aAddr['email'])) { |
347 | $at = strpos($aAddr['email'], '@'); |
348 | $addr_structure->mailbox = substr($aAddr['email'], 0, $at); |
349 | $addr_structure->host = substr($aAddr['email'], $at+1); |
350 | if (isset($aAddr['name'])) { |
351 | $addr_structure->personal = $aAddr['name']; |
352 | } |
353 | } |
354 | } |
ec0e3bf0 |
355 | if (!$addr_structure->mailbox) { |
e74ba378 |
356 | $addr_structure->mailbox = trim($addr); |
357 | if ($host) { |
358 | $addr_structure->host = $host; |
359 | } |
238c278c |
360 | } |
19d470aa |
361 | } |
362 | if ($group && $addr == '') { /* no addresses found in group */ |
238c278c |
363 | $name = "$group"; |
19d470aa |
364 | $addr_structure->personal = $name; |
365 | $addr_ar[] = $addr_structure; |
085103f0 |
366 | return (array($addr_ar,$pos+1 )); |
367 | } elseif ($group) { |
368 | $addr_structure->personal = $name; |
369 | $addr_ar[] = $addr_structure; |
370 | return (array($addr_ar,$pos+1 )); |
19d470aa |
371 | } else { |
372 | $addr_structure->personal = $name; |
373 | if ($name || $addr) { |
374 | $addr_ar[] = $addr_structure; |
375 | } |
376 | } |
377 | if ($ar) { |
378 | return ($addr_ar); |
379 | } |
380 | return ($addr_ar[0]); |
381 | } |
382 | |
383 | function parseContentType($value) { |
384 | $pos = strpos($value, ';'); |
385 | $props = ''; |
386 | if ($pos > 0) { |
387 | $type = trim(substr($value, 0, $pos)); |
388 | $props = trim(substr($type, $pos+1)); |
389 | } else { |
390 | $type = $value; |
391 | } |
392 | $content_type = new ContentType($type); |
393 | if ($props) { |
394 | $properties = $this->parseProperties($props); |
395 | if (!isset($properties['charset'])) { |
396 | $properties['charset'] = 'us-ascii'; |
397 | } |
398 | $content_type->properties = $this->parseProperties($props); |
399 | } |
400 | $this->content_type = $content_type; |
401 | } |
402 | |
403 | function parseProperties($value) { |
404 | $propArray = explode(';', $value); |
405 | $propResultArray = array(); |
406 | foreach ($propArray as $prop) { |
407 | $prop = trim($prop); |
408 | $pos = strpos($prop, '='); |
409 | if ($pos > 0) { |
410 | $key = trim(substr($prop, 0, $pos)); |
411 | $val = trim(substr($prop, $pos+1)); |
412 | if ($val{0} == '"') { |
413 | $val = substr($val, 1, -1); |
414 | } |
415 | $propResultArray[$key] = $val; |
416 | } |
417 | } |
418 | return $propResultArray; |
419 | } |
420 | |
421 | function parseDisposition($value) { |
422 | $pos = strpos($value, ';'); |
423 | $props = ''; |
424 | if ($pos > 0) { |
425 | $name = trim(substr($value, 0, $pos)); |
fc9269ec |
426 | $props = trim(substr($value, $pos+1)); |
19d470aa |
427 | } else { |
428 | $name = $value; |
429 | } |
430 | $props_a = $this->parseProperties($props); |
431 | $disp = new Disposition($name); |
432 | $disp->properties = $props_a; |
433 | $this->disposition = $disp; |
434 | } |
435 | |
436 | function mlist($field, $value) { |
437 | $res_a = array(); |
438 | $value_a = explode(',', $value); |
439 | foreach ($value_a as $val) { |
440 | $val = trim($val); |
441 | if ($val{0} == '<') { |
442 | $val = substr($val, 1, -1); |
443 | } |
444 | if (substr($val, 0, 7) == 'mailto:') { |
445 | $res_a['mailto'] = substr($val, 7); |
446 | } else { |
447 | $res_a['href'] = $val; |
448 | } |
449 | } |
450 | $this->mlist[$field] = $res_a; |
451 | } |
452 | |
453 | /* |
454 | * function to get the addres strings out of the header. |
455 | * Arguments: string or array of strings ! |
456 | * example1: header->getAddr_s('to'). |
457 | * example2: header->getAddr_s(array('to', 'cc', 'bcc')) |
458 | */ |
459 | function getAddr_s($arr, $separator = ',') { |
460 | $s = ''; |
461 | |
462 | if (is_array($arr)) { |
463 | foreach($arr as $arg) { |
464 | if ($this->getAddr_s($arg)) { |
465 | $s .= $separator . $result; |
466 | } |
467 | } |
468 | $s = ($s ? substr($s, 2) : $s); |
469 | } else { |
470 | eval('$addr = $this->' . $arr . ';') ; |
471 | if (is_array($addr)) { |
472 | foreach ($addr as $addr_o) { |
473 | if (is_object($addr_o)) { |
474 | $s .= $addr_o->getAddress() . $separator; |
475 | } |
476 | } |
477 | $s = substr($s, 0, -strlen($separator)); |
478 | } else { |
479 | if (is_object($addr)) { |
480 | $s .= $addr->getAddress(); |
481 | } |
482 | } |
483 | } |
484 | return $s; |
485 | } |
486 | |
487 | function getAddr_a($arg, $excl_arr = array(), $arr = array()) { |
488 | if (is_array($arg)) { |
489 | foreach($arg as $argument) { |
490 | $arr = $this->getAddr_a($argument, $excl_arr, $arr); |
491 | } |
492 | } else { |
1465f80c |
493 | $addr = $this->{$arg}; |
19d470aa |
494 | if (is_array($addr)) { |
495 | foreach ($addr as $next_addr) { |
496 | if (is_object($next_addr)) { |
497 | if (isset($next_addr->host) && ($next_addr->host != '')) { |
498 | $email = $next_addr->mailbox . '@' . $next_addr->host; |
499 | } else { |
500 | $email = $next_addr->mailbox; |
501 | } |
502 | $email = strtolower($email); |
503 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
504 | $arr[$email] = $next_addr->personal; |
505 | } |
506 | } |
507 | } |
508 | } else { |
509 | if (is_object($addr)) { |
510 | $email = $addr->mailbox; |
511 | $email .= (isset($addr->host) ? '@' . $addr->host : ''); |
512 | $email = strtolower($email); |
513 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
514 | $arr[$email] = $addr->personal; |
515 | } |
516 | } |
517 | } |
518 | } |
519 | return $arr; |
520 | } |
d0719411 |
521 | |
522 | function findAddress($address, $recurs = false) { |
523 | $result = false; |
524 | if (is_array($address)) { |
525 | $i=0; |
526 | foreach($address as $argument) { |
527 | $match = $this->findAddress($argument, true); |
528 | $last = end($match); |
529 | if ($match[1]) { |
530 | return $i; |
531 | } else { |
532 | if (count($match[0]) && !$result) { |
533 | $result = $i; |
534 | } |
535 | } |
536 | ++$i; |
537 | } |
538 | } else { |
03d73e64 |
539 | if (!is_array($this->cc)) $this->cc = array(); |
d0719411 |
540 | $srch_addr = $this->parseAddress($address); |
541 | $results = array(); |
542 | foreach ($this->to as $to) { |
543 | if ($to->host == $srch_addr->host) { |
544 | if ($to->mailbox == $srch_addr->mailbox) { |
545 | $results[] = $srch_addr; |
546 | if ($to->personal == $srch_addr->personal) { |
547 | if ($recurs) { |
548 | return array($results, true); |
549 | } else { |
550 | return true; |
551 | } |
552 | } |
553 | } |
554 | } |
555 | } |
556 | foreach ($this->cc as $cc) { |
557 | if ($cc->host == $srch_addr->host) { |
558 | if ($cc->mailbox == $srch_addr->mailbox) { |
559 | $results[] = $srch_addr; |
560 | if ($cc->personal == $srch_addr->personal) { |
561 | if ($recurs) { |
562 | return array($results, true); |
563 | } else { |
564 | return true; |
565 | } |
566 | } |
567 | } |
568 | } |
569 | } |
570 | if ($recurs) { |
571 | return array($results, false); |
572 | } elseif (count($result)) { |
573 | return true; |
574 | } else { |
575 | return false; |
576 | } |
577 | } |
1465f80c |
578 | //exit; |
d0719411 |
579 | return $result; |
580 | } |
19d470aa |
581 | |
582 | function getContentType($type0, $type1) { |
583 | $type0 = $this->content_type->type0; |
584 | $type1 = $this->content_type->type1; |
585 | return $this->content_type->properties; |
586 | } |
587 | } |
588 | |
589 | ?> |