8f6b86cc |
1 | <?php |
2 | |
3 | /** |
1a09384c |
4 | * mime.class |
8f6b86cc |
5 | * |
6 | * Copyright (c) 2002 The SquirrelMail Project Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
8f6b86cc |
9 | * This contains functions needed to handle mime messages. |
10 | * |
11 | * $Id$ |
12 | */ |
13 | |
e219fb97 |
14 | /* |
e8bedb2d |
15 | * rdc822_header class |
16 | * input: header_string or array |
17 | */ |
b65ca362 |
18 | class rfc822_header { |
e8bedb2d |
19 | var $date = '', |
20 | $subject = '', |
b65ca362 |
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 = '', |
29 | $mime = false, |
30 | $content_type = '', |
31 | $disposition = '', |
32 | $xmailer = '', |
33 | $priority = 3, |
34 | $dnt = '', |
35 | $mlist = array(), |
e219fb97 |
36 | $more_headers = array(); /* only needed for constructing headers |
b65ca362 |
37 | in smtp.php */ |
38 | function parseHeader($hdr) { |
39 | if (is_array($hdr)) { |
40 | $hdr = implode('', $hdr); |
1d9392e4 |
41 | } |
b65ca362 |
42 | |
43 | /* First we unfold the header */ |
44 | $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr)); |
45 | |
46 | /* Now we can make a new header array with */ |
47 | /* each element representing a headerline */ |
e219fb97 |
48 | $hdr = explode("\r\n" , $hdr); |
b65ca362 |
49 | foreach ($hdr as $line) { |
50 | $pos = strpos($line, ':'); |
51 | if ($pos > 0) { |
52 | $field = substr($line, 0, $pos); |
53 | $value = trim(substr($line, $pos+1)); |
54 | if(!preg_match('/^X.*/i', $field) && |
4b8699db |
55 | !preg_match('/^Subject/i', $field)) { |
1d9392e4 |
56 | $value = $this->stripComments($value); |
57 | } |
b65ca362 |
58 | $this->parseField($field, $value); |
1d9392e4 |
59 | } |
60 | } |
b65ca362 |
61 | if ($this->content_type == '') { |
1d9392e4 |
62 | $this->parseContentType('text/plain; charset=us-ascii'); |
63 | } |
e8bedb2d |
64 | } |
e219fb97 |
65 | |
b65ca362 |
66 | function stripComments($value) { |
67 | $result = ''; |
68 | |
e8bedb2d |
69 | $cnt = strlen($value); |
20760c79 |
70 | for ($i = 0; $i < $cnt; $i++) { |
e219fb97 |
71 | switch ($value{$i}) { |
72 | case '"': |
73 | $result .= '"'; |
20760c79 |
74 | while ((++$i < $cnt) && ($value{$i} != '"')) { |
e219fb97 |
75 | if ($value{$i} == '\\') { |
76 | $result .= '\\'; |
20760c79 |
77 | $i++; |
e219fb97 |
78 | } |
79 | $result .= $value{$i}; |
e219fb97 |
80 | } |
81 | $result .= $value{$i}; |
82 | break; |
83 | case '(': |
20760c79 |
84 | $depth = 1; |
85 | while (($depth > 0) && (++$i < $cnt)) { |
86 | switch($value{$i}) { |
87 | case '\\': |
88 | $i++; |
89 | break; |
90 | case '(': |
91 | $depth++; |
92 | break; |
93 | case ')': |
94 | $depth--; |
95 | break; |
96 | default: |
97 | break; |
98 | } |
e219fb97 |
99 | } |
100 | break; |
101 | default: |
102 | $result .= $value{$i}; |
103 | break; |
104 | } |
105 | } |
106 | return $result; |
4b8699db |
107 | } |
e219fb97 |
108 | |
b65ca362 |
109 | function parseField($field, $value) { |
e8bedb2d |
110 | $field = strtolower($field); |
b65ca362 |
111 | switch($field) { |
112 | case 'date': |
113 | $d = strtr($value, array(' ' => ' ')); |
114 | $d = explode(' ', $d); |
115 | $this->date = getTimeStamp($d); |
116 | break; |
117 | case 'subject': |
118 | $this->subject = $value; |
119 | break; |
120 | case 'from': |
121 | $this->from = $this->parseAddress($value,true); |
122 | break; |
123 | case 'sender': |
124 | $this->sender = $this->parseAddress($value); |
125 | break; |
126 | case 'reply-to': |
127 | $this->reply_to = $this->parseAddress($value, true); |
128 | break; |
129 | case 'to': |
130 | $this->to = $this->parseAddress($value, true); |
131 | break; |
132 | case 'cc': |
133 | $this->cc = $this->parseAddress($value, true); |
134 | break; |
135 | case 'bcc': |
136 | $this->bcc = $this->parseAddress($value, true); |
137 | break; |
138 | case 'in-reply-to': |
139 | $this->in_reply_to = $value; |
140 | break; |
e219fb97 |
141 | case 'message-id': |
b65ca362 |
142 | $this->message_id = $value; |
143 | break; |
144 | case 'disposition-notification-to': |
145 | $this->dnt = $this->parseAddress($value); |
146 | break; |
e219fb97 |
147 | case 'mime-version': |
b65ca362 |
148 | $value = str_replace(' ', '', $value); |
149 | $this->mime = ($value == '1.0' ? true : $this->mime); |
150 | break; |
151 | case 'content-type': |
152 | $this->parseContentType($value); |
153 | break; |
154 | case 'content-disposition': |
155 | $this->parseDisposition($value); |
e219fb97 |
156 | break; |
b65ca362 |
157 | case 'user-agent': |
158 | case 'x-mailer': |
159 | $this->xmailer = $value; |
160 | break; |
161 | case 'x-priority': |
162 | $this->priority = $value; |
163 | break; |
164 | case 'list-post': |
165 | $this->mlist('post', $value); |
166 | break; |
167 | case 'list-reply': |
e219fb97 |
168 | $this->mlist('reply', $value); |
b65ca362 |
169 | break; |
170 | case 'list-subscribe': |
171 | $this->mlist('subscribe', $value); |
172 | break; |
173 | case 'list-unsubscribe': |
174 | $this->mlist('unsubscribe', $value); |
175 | break; |
176 | case 'list-archive': |
177 | $this->mlist('archive', $value); |
178 | break; |
179 | case 'list-owner': |
180 | $this->mlist('owner', $value); |
181 | break; |
182 | case 'list-help': |
183 | $this->mlist('help', $value); |
184 | break; |
185 | case 'list-id': |
186 | $this->mlist('id', $value); |
187 | break; |
188 | default: |
189 | break; |
190 | } |
e219fb97 |
191 | } |
e8bedb2d |
192 | |
b65ca362 |
193 | function parseAddress |
194 | ($address, $ar=false, $addr_ar = array(), $group = '') { |
e8bedb2d |
195 | $pos = 0; |
b65ca362 |
196 | $j = strlen($address); |
e8bedb2d |
197 | $name = ''; |
198 | $addr = ''; |
b65ca362 |
199 | while ($pos < $j) { |
200 | switch ($address{$pos}) { |
201 | case '"': /* get the personal name */ |
202 | if ($address{++$pos} == '"') { |
203 | ++$pos; |
204 | } else { |
205 | while ($pos < $j && $address{$pos} != '"') { |
206 | if ((substr($address, $pos, 2) == '\\"') || |
207 | (substr($address, $pos, 2) == '\\\\')) { |
208 | $name .= $address{$pos++}; |
209 | } |
210 | $name .= $address{$pos++}; |
211 | } |
212 | } |
213 | ++$pos; |
214 | break; |
215 | case '<': /* get email address */ |
216 | $addr_start = $pos++; |
217 | while ($pos < $j && $address{$pos} != '>') { |
218 | $addr .= $address{$pos++}; |
219 | } |
220 | ++$pos; |
221 | break; |
222 | case '(': /* rip off comments */ |
223 | $addr_start = $pos; |
224 | for (++$pos; $pos < $j && $address{$pos} != ')'; ++$pos) { |
e219fb97 |
225 | $addr .= $address{$pos}; |
b65ca362 |
226 | } |
227 | $address_start = substr($address, 0, $addr_start); |
228 | $address_end = substr($address, $pos + 1); |
e219fb97 |
229 | $address = $address_start . $address_end; |
b65ca362 |
230 | $j = strlen($address); |
231 | $pos = $addr_start + 1; |
232 | break; |
233 | case ',': /* we reached a delimiter */ |
234 | if ($addr == '') { |
235 | $addr = substr($address, 0, $pos); |
236 | } else if ($name == '') { |
237 | $name = trim(substr($address, 0, $addr_start)); |
238 | } |
239 | |
240 | $at = strpos($addr, '@'); |
241 | $addr_structure = new address_structure(); |
242 | $addr_structure->personal = $name; |
243 | $addr_structure->group = $group; |
244 | if ($at) { |
245 | $addr_structure->mailbox = substr($addr, 0, $at); |
246 | $addr_structure->host = substr($addr, $at+1); |
247 | } else { |
248 | $addr_structure->mailbox = $addr; |
249 | } |
250 | $address = trim(substr($address, $pos+1)); |
251 | $j = strlen($address); |
252 | $pos = 0; |
253 | $name = ''; |
254 | $addr = ''; |
255 | $addr_ar[] = $addr_structure; |
256 | break; |
257 | case ':': /* process the group addresses */ |
258 | /* group marker */ |
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]; |
263 | $pos = $result[1]; |
264 | $address = substr($address, $pos++); |
e219fb97 |
265 | $j = strlen($address); |
b65ca362 |
266 | $group = ''; |
267 | break; |
268 | case ';': |
269 | if ($group) { |
270 | $address = substr($address, 0, $pos - 1); |
271 | } |
272 | ++$pos; |
273 | break; |
274 | default: |
275 | ++$pos; |
276 | break; |
277 | } |
e8bedb2d |
278 | } |
b65ca362 |
279 | if ($addr == '') { |
280 | $addr = substr($address, 0, $pos); |
281 | } else if ($name == '') { |
282 | $name = trim(substr($address, 0, $addr_start)); |
e8bedb2d |
283 | } |
284 | $at = strpos($addr, '@'); |
285 | $addr_structure = new address_structure(); |
286 | $addr_structure->group = $group; |
b65ca362 |
287 | if ($at) { |
288 | $addr_structure->mailbox = trim(substr($addr, 0, $at)); |
289 | $addr_structure->host = trim(substr($addr, $at+1)); |
290 | } else { |
291 | $addr_structure->mailbox = trim($addr); |
e8bedb2d |
292 | } |
b65ca362 |
293 | if ($group && $addr == '') { /* no addresses found in group */ |
294 | $name = "$group: Undisclosed recipients;"; |
295 | $addr_structure->personal = $name; |
e219fb97 |
296 | $addr_ar[] = $addr_structure; |
297 | return (array($addr_ar, $pos+1)); |
b65ca362 |
298 | } else { |
299 | $addr_structure->personal = $name; |
300 | if ($name || $addr) { |
301 | $addr_ar[] = $addr_structure; |
e219fb97 |
302 | } |
e8bedb2d |
303 | } |
b65ca362 |
304 | if ($ar) { |
305 | return ($addr_ar); |
306 | } |
b65ca362 |
307 | return ($addr_ar[0]); |
8f6b86cc |
308 | } |
e219fb97 |
309 | |
b65ca362 |
310 | function parseContentType($value) { |
311 | $pos = strpos($value, ';'); |
312 | $props = ''; |
313 | if ($pos > 0) { |
314 | $type = trim(substr($value, 0, $pos)); |
315 | $props = trim(substr($type, $pos+1)); |
316 | } else { |
317 | $type = $value; |
318 | } |
319 | $content_type = new content_type($type); |
320 | if ($props) { |
321 | $properties = $this->parseProperties($props); |
322 | if (!isset($properties['charset'])) { |
323 | $properties['charset'] = 'us-ascii'; |
324 | } |
325 | $content_type->properties = $this->parseProperties($props); |
326 | } |
327 | $this->content_type = $content_type; |
328 | } |
e219fb97 |
329 | |
b65ca362 |
330 | function parseProperties($value) { |
331 | $propArray = explode(';', $value); |
332 | $propResultArray = array(); |
333 | foreach ($propArray as $prop) { |
334 | $prop = trim($prop); |
335 | $pos = strpos($prop, '='); |
336 | if ($pos > 0) { |
337 | $key = trim(substr($prop, 0, $pos)); |
338 | $val = trim(substr($prop, $pos+1)); |
339 | if ($val{0} == '"') { |
340 | $val = substr($val, 1, -1); |
341 | } |
342 | $propResultArray[$key] = $val; |
343 | } |
344 | } |
345 | return $propResultArray; |
346 | } |
e219fb97 |
347 | |
b65ca362 |
348 | function parseDisposition($value) { |
349 | $pos = strpos($value, ';'); |
350 | $props = ''; |
351 | if ($pos > 0) { |
352 | $name = trim(substr($value, 0, $pos)); |
353 | $props = trim(substr($type, $pos+1)); |
354 | } else { |
355 | $name = $value; |
356 | } |
357 | $props_a = $this->parseProperties($props); |
358 | $disp = new disposition($name); |
359 | $disp->properties = $props_a; |
360 | $this->disposition = $disp; |
361 | } |
e219fb97 |
362 | |
b65ca362 |
363 | function mlist($field, $value) { |
364 | $res_a = array(); |
365 | $value_a = explode(',', $value); |
366 | foreach ($value_a as $val) { |
367 | $val = trim($val); |
368 | if ($val{0} == '<') { |
369 | $val = substr($val, 1, -1); |
370 | } |
371 | if (substr($val, 0, 7) == 'mailto:') { |
372 | $res_a['mailto'] = substr($val, 7); |
373 | } else { |
374 | $res_a['href'] = $val; |
375 | } |
376 | } |
377 | $this->mlist[$field] = $res_a; |
378 | } |
e8bedb2d |
379 | |
e219fb97 |
380 | /* |
b65ca362 |
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')) |
385 | */ |
386 | function getAddr_s($arr, $separator = ',') { |
387 | $s = ''; |
388 | |
389 | if (is_array($arr)) { |
390 | foreach($arr as $arg) { |
391 | if ($this->getAddr_s($arg)) { |
392 | $s .= $separator . $result; |
393 | } |
394 | } |
395 | $s = ($s ? substr($s, 2) : $s); |
396 | } else { |
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; |
402 | } |
403 | } |
404 | $s = substr($s, 0, -strlen($separator)); |
405 | } else { |
406 | if (is_object($addr)) { |
407 | $s .= $addr->getAddress(); |
408 | } |
409 | } |
410 | } |
b65ca362 |
411 | return $s; |
8f6b86cc |
412 | } |
e219fb97 |
413 | |
b65ca362 |
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); |
418 | } |
419 | } else { |
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; |
426 | } else { |
427 | $email = $next_addr->mailbox; |
428 | } |
429 | $email = strtolower($email); |
430 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
431 | $arr[$email] = $next_addr->personal; |
432 | } |
433 | } |
434 | } |
435 | } else { |
436 | if (is_object($addr)) { |
437 | $email = $addr->mailbox; |
438 | $email .= (isset($addr->host) ? '@' . $addr->host : ''); |
e219fb97 |
439 | $email = strtolower($email); |
b65ca362 |
440 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
441 | $arr[$email] = $addr->personal; |
442 | } |
443 | } |
444 | } |
445 | } |
e219fb97 |
446 | return $arr; |
8f6b86cc |
447 | } |
e219fb97 |
448 | |
b65ca362 |
449 | function getContentType($type0, $type1) { |
e8bedb2d |
450 | $type0 = $this->content_type->type0; |
b65ca362 |
451 | $type1 = $this->content_type->type1; |
e219fb97 |
452 | return $this->content_type->properties; |
e8bedb2d |
453 | } |
8f6b86cc |
454 | } |
455 | |
b65ca362 |
456 | class msg_header { |
e8bedb2d |
457 | /** msg_header contains all variables available in a bodystructure **/ |
458 | /** entity like described in rfc2060 **/ |
459 | |
e219fb97 |
460 | var $type0 = '', |
461 | $type1 = '', |
b65ca362 |
462 | $parameters = array(), |
e219fb97 |
463 | $id = 0, |
464 | $description = '', |
465 | $encoding='', |
b65ca362 |
466 | $size = 0, |
e219fb97 |
467 | $md5='', |
468 | $disposition = '', |
b65ca362 |
469 | $language=''; |
e219fb97 |
470 | |
471 | /* |
e8bedb2d |
472 | * returns addres_list of supplied argument |
473 | * arguments: array('to', 'from', ...) or just a string like 'to'. |
474 | * result: string: address1, addres2, .... |
e219fb97 |
475 | */ |
476 | |
b65ca362 |
477 | function setVar($var, $value) { |
e8bedb2d |
478 | $this->{$var} = $value; |
479 | } |
e219fb97 |
480 | |
b65ca362 |
481 | function getParameter($p) { |
482 | $value = strtolower($p); |
483 | return (isset($this->parameters[$p]) ? $this->parameters[$p] : ''); |
e8bedb2d |
484 | } |
e219fb97 |
485 | |
b65ca362 |
486 | function setParameter($parameter, $value) { |
e8bedb2d |
487 | $this->parameters[strtolower($parameter)] = $value; |
488 | } |
489 | } |
490 | |
491 | |
492 | |
b65ca362 |
493 | class address_structure { |
494 | var $personal = '', |
495 | $adl = '', |
496 | $mailbox = '', |
497 | $host = '', |
498 | $group = ''; |
8f6b86cc |
499 | |
b65ca362 |
500 | function getAddress($full = true) { |
501 | $result = ''; |
502 | |
503 | if (is_object($this)) { |
20760c79 |
504 | if (isset($this->host) && ($this->host != '')) { |
b65ca362 |
505 | $email = $this->mailbox.'@'.$this->host; |
506 | } else { |
507 | $email = $this->mailbox; |
508 | } |
509 | if (trim($this->personal) != '') { |
510 | if ($email) { |
511 | $addr = '"' . $this->personal . '" <' .$email.'>'; |
512 | } else { |
513 | $addr = $this->personal; |
514 | } |
515 | $best_dpl = $this->personal; |
516 | } else { |
517 | $addr = $email; |
518 | $best_dpl = $email; |
519 | } |
520 | $result = ($full ? $addr : $best_dpl); |
319cf0d2 |
521 | } |
e219fb97 |
522 | return $result; |
b65ca362 |
523 | } |
8f6b86cc |
524 | } |
525 | |
b65ca362 |
526 | class message { |
8f6b86cc |
527 | /** message is the object that contains messages. It is a recursive |
b65ca362 |
528 | object in that through the $entities variable, it can contain |
529 | more objects of type message. See documentation in mime.txt for |
530 | a better description of how this works. |
8f6b86cc |
531 | **/ |
e219fb97 |
532 | var $rfc822_header = '', |
e8bedb2d |
533 | $mime_header = '', |
b65ca362 |
534 | $flags = '', |
e8bedb2d |
535 | $type0='', |
e219fb97 |
536 | $type1='', |
537 | $entities = array(), |
538 | $parent_ent, $entity, |
b65ca362 |
539 | $parent = '', $decoded_body='', |
540 | $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0, |
541 | $is_mdnsent = 0, |
542 | $body_part = '', |
543 | $offset = 0, /* for fetching body parts out of raw messages */ |
544 | $length = 0; /* for fetching body parts out of raw messages */ |
545 | |
546 | function setEnt($ent) { |
8f6b86cc |
547 | $this->entity_id= $ent; |
548 | } |
e219fb97 |
549 | |
b65ca362 |
550 | function addEntity ($msg) { |
8f6b86cc |
551 | $msg->parent = &$this; |
552 | $this->entities[] = $msg; |
553 | } |
554 | |
b65ca362 |
555 | function getFilename() { |
556 | $filename = ''; |
6479ed81 |
557 | $filename = $this->header->getParameter('filename'); |
558 | if (!$filename) { |
559 | $filename = $this->header->getParameter('name'); |
b65ca362 |
560 | } |
6479ed81 |
561 | |
b65ca362 |
562 | if (!$filename) { |
563 | $filename = 'untitled-'.$this->entity_id; |
564 | } |
b65ca362 |
565 | return $filename; |
812843b7 |
566 | } |
567 | |
568 | |
b65ca362 |
569 | function addRFC822Header($read) { |
570 | $header = new rfc822_header(); |
571 | $this->rfc822_header = $header->parseHeader($read); |
8f6b86cc |
572 | } |
573 | |
b65ca362 |
574 | function getEntity($ent) { |
8f6b86cc |
575 | $cur_ent = $this->entity_id; |
b65ca362 |
576 | $msg = $this; |
20760c79 |
577 | if (($cur_ent == '') || ($cur_ent == '0')) { |
b65ca362 |
578 | $cur_ent_a = array(); |
579 | } else { |
580 | $cur_ent_a = explode('.', $this->entity_id); |
581 | } |
582 | $ent_a = explode('.', $ent); |
e219fb97 |
583 | |
b65ca362 |
584 | $cnt = count($ent_a); |
585 | |
586 | for ($i = 0; $i < $cnt -1; ++$i) { |
587 | if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) { |
588 | $msg = $msg->parent; |
589 | $cur_ent_a = explode('.', $msg->entity_id); |
590 | --$i; |
591 | } else if (!isset($cur_ent_a[$i])) { |
592 | if (isset($msg->entities[($ent_a[$i]-1)])) { |
593 | $msg = $msg->entities[($ent_a[$i]-1)]; |
594 | } else { |
595 | $msg = $msg->entities[0]; |
596 | } |
597 | } |
598 | if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) { |
599 | /*this is a header for a message/rfc822 entity */ |
600 | $msg = $msg->entities[0]; |
601 | } |
602 | } |
603 | |
604 | if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) { |
605 | /*this is a header for a message/rfc822 entity */ |
606 | $msg = $msg->entities[0]; |
607 | } |
608 | |
609 | if (isset($msg->entities[($ent_a[$cnt-1])-1])) { |
610 | if (is_object($msg->entities[($ent_a[$cnt-1])-1])) { |
611 | $msg = $msg->entities[($ent_a[$cnt-1]-1)]; |
612 | } |
613 | } |
08c18186 |
614 | |
319cf0d2 |
615 | return $msg; |
8f6b86cc |
616 | } |
e8bedb2d |
617 | |
b65ca362 |
618 | function setBody($s) { |
e8bedb2d |
619 | $this->body_part = $s; |
620 | } |
e219fb97 |
621 | |
b65ca362 |
622 | function clean_up() { |
e8bedb2d |
623 | $msg = $this; |
b65ca362 |
624 | $msg->body_part = ''; |
625 | |
20760c79 |
626 | foreach ($msg->entities as $m) { |
627 | $m->clean_up(); |
b65ca362 |
628 | } |
e8bedb2d |
629 | } |
e219fb97 |
630 | |
b65ca362 |
631 | function getMailbox() { |
632 | $msg = $this; |
633 | while (is_object($msg->parent)) { |
634 | $msg = $msg->parent; |
635 | } |
636 | return $msg->mailbox; |
319cf0d2 |
637 | } |
e219fb97 |
638 | |
b65ca362 |
639 | function calcEntity($msg) { |
640 | if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) { |
641 | $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */ |
642 | } else if (isset($this->entity_id) && ($this->entity_id != '')) { |
643 | $ent_no = count($this->entities)+1; |
644 | $par_ent = substr($this->entity_id, -2); |
645 | if ($par_ent{0} == '.') { |
646 | $par_ent = $par_ent{1}; |
647 | } |
648 | if ($par_ent == '0') { |
649 | $ent_no = count($this->entities) + 1; |
650 | if ($ent_no > 0) { |
20760c79 |
651 | $ent = substr($this->entity_id, 0, strrpos($this->entity_id, '.')); |
b65ca362 |
652 | $ent = ($ent ? $ent . ".$ent_no" : $ent_no); |
653 | $msg->entity_id = $ent; |
654 | } else { |
655 | $msg->entity_id = $ent_no; |
e219fb97 |
656 | } |
b65ca362 |
657 | } else { |
658 | $ent = $this->entity_id . ".$ent_no"; |
659 | $msg->entity_id = $ent; |
660 | } |
661 | } else { |
662 | $msg->entity_id = '0'; |
663 | } |
664 | |
665 | return $msg->entity_id; |
c52ffd66 |
666 | } |
e219fb97 |
667 | |
668 | |
669 | /* |
670 | * Bodystructure parser, a recursive function for generating the |
8f6b86cc |
671 | * entity-tree with all the mime-parts. |
e219fb97 |
672 | * |
8f6b86cc |
673 | * It follows RFC2060 and stores all the described fields in the |
e219fb97 |
674 | * message object. |
8f6b86cc |
675 | * |
676 | * Question/Bugs: |
e219fb97 |
677 | * |
018d3a38 |
678 | * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net) |
8f6b86cc |
679 | * |
680 | */ |
b65ca362 |
681 | function parseStructure($read, $i = 0) { |
e60230ae |
682 | $arg_no = 0; |
683 | $arg_a = array(); |
684 | $cnt = strlen($read); |
b65ca362 |
685 | |
e60230ae |
686 | for (; $i < $cnt; ++$i) { |
687 | $char = strtoupper($read{$i}); |
b65ca362 |
688 | switch ($char) { |
689 | case '(': |
018d3a38 |
690 | switch($arg_no) { |
691 | case 0: |
692 | if (!isset($msg)) { |
693 | $msg = new message(); |
694 | $hdr = new msg_header(); |
695 | $hdr->type0 = 'text'; |
696 | $hdr->type1 = 'plain'; |
697 | $hdr->encoding = 'us-ascii'; |
698 | $msg->entity_id = $this->calcEntity($msg); |
699 | } else { |
700 | $msg->header->type0 = 'multipart'; |
701 | $msg->type0 = 'multipart'; |
702 | while ($read{$i} == '(') { |
b65ca362 |
703 | $res = $msg->parseStructure($read, $i); |
704 | $i = $res[1]; |
705 | $msg->addEntity($res[0]); |
706 | } |
018d3a38 |
707 | } |
708 | break; |
709 | case 1: |
710 | /* multipart properties */ |
711 | ++$i; |
712 | $res = $this->parseProperties($read, $i); |
713 | $arg_a[] = $res[0]; |
714 | $i = $res[1]; |
715 | ++$arg_no; |
716 | break; |
717 | case 2: |
718 | if (isset($msg->type0) && ($msg->type0 == 'multipart')) { |
b65ca362 |
719 | ++$i; |
720 | $res = $msg->parseDisposition($read, $i); |
018d3a38 |
721 | } else { /* properties */ |
722 | $res = $msg->parseProperties($read, $i); |
723 | } |
724 | $arg_a[] = $res[0]; |
725 | $i = $res[1]; |
726 | ++$arg_no; |
727 | break; |
728 | case 3: |
729 | if (isset($msg->type0) && ($msg->type0 == 'multipart')) { |
730 | ++$i; |
731 | $res= $msg->parseLanguage($read, $i); |
b65ca362 |
732 | $arg_a[] = $res[0]; |
733 | $i = $res[1]; |
018d3a38 |
734 | } |
735 | case 7: |
736 | if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) { |
737 | $msg->header->type0 = $arg_a[0]; |
738 | $msg->header->type1 = $arg_a[1]; |
739 | $msg->type0 = $arg_a[0]; |
740 | $msg->type1 = $arg_a[1]; |
741 | $rfc822_hdr = new rfc822_header(); |
742 | $res = $msg->parseEnvelope($read, $i, $rfc822_hdr); |
743 | $msg->rfc822_header = $res[0]; |
744 | $i = $res[1] + 1; |
745 | while (($i < $cnt) && ($read{$i} != '(')) { |
746 | ++$i; |
b65ca362 |
747 | } |
018d3a38 |
748 | $res = $msg->parseStructure($read, $i); |
b65ca362 |
749 | $i = $res[1]; |
018d3a38 |
750 | $msg->addEntity($res[0]); |
751 | } |
752 | break; |
753 | case 8: |
754 | ++$i; |
755 | $res = $msg->parseDisposition($read, $i); |
756 | $arg_a[] = $res[0]; |
757 | $i = $res[1]; |
758 | ++$arg_no; |
759 | break; |
760 | case 9: |
761 | ++$i; |
762 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
763 | $res = $msg->parseDisposition($read, $i); |
764 | } else { |
765 | $res = $msg->parseLanguage($read, $i); |
766 | } |
767 | $arg_a[] = $res[0]; |
768 | $i = $res[1]; |
769 | ++$arg_no; |
770 | break; |
771 | case 10: |
772 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
773 | ++$i; |
774 | $res = $msg->parseLanguage($read, $i); |
775 | $arg_a[] = $res[0]; |
776 | $i = $res[1]; |
777 | } else { |
b65ca362 |
778 | $i = $msg->parseParenthesis($read, $i); |
018d3a38 |
779 | $arg_a[] = ''; /* not yet described in rfc2060 */ |
780 | } |
781 | ++$arg_no; |
782 | break; |
783 | default: |
784 | /* unknown argument, skip this part */ |
785 | $i = $msg->parseParenthesis($read, $i); |
786 | $arg_a[] = ''; |
787 | ++$arg_no; |
788 | break; |
789 | } /* switch */ |
b65ca362 |
790 | break; |
018d3a38 |
791 | case '"': |
792 | /* inside an entity -> start processing */ |
793 | $debug = substr($read, $i, 20); |
794 | $res = $msg->parseQuote($read, $i); |
795 | $arg_s = $res[0]; |
796 | $i = $res[1]; |
797 | ++$arg_no; |
798 | if ($arg_no < 3) { |
799 | $arg_s = strtolower($arg_s); /* type0 and type1 */ |
800 | } |
801 | $arg_a[] = $arg_s; |
802 | break; |
803 | case 'n': |
804 | case 'N': |
805 | /* probably NIL argument */ |
806 | if (strtoupper(substr($read, $i, 4)) == 'NIL ') { |
807 | $arg_a[] = ''; |
808 | ++$arg_no; |
809 | $i += 2; |
810 | } |
b65ca362 |
811 | break; |
812 | case '{': |
813 | /* process the literal value */ |
814 | $res = $msg->parseLiteral($read, $i); |
815 | $arg_s = $res[0]; |
816 | $i = $res[1]; |
817 | ++$arg_no; |
818 | break; |
819 | case is_numeric($read{$i}): |
820 | /* process integers */ |
821 | if ($read{$i} == ' ') { break; } |
822 | $arg_s = $read{$i};; |
823 | for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) { |
824 | $arg_s .= $read{$i}; |
825 | } |
826 | ++$arg_no; |
827 | $arg_a[] = $arg_s; |
828 | break; |
829 | case ')': |
830 | $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart')); |
831 | if (!$multipart) { |
832 | $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))); |
833 | $hdr->type0 = $arg_a[0]; |
e219fb97 |
834 | $hdr->type1 = $arg_a[1]; |
b65ca362 |
835 | |
836 | $msg->type0 = $arg_a[0]; |
837 | $msg->type1 = $arg_a[1]; |
b65ca362 |
838 | $arr = $arg_a[2]; |
839 | if (is_array($arr)) { |
840 | $hdr->parameters = $arg_a[2]; |
841 | } |
842 | $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3])); |
843 | $hdr->description = $arg_a[4]; |
844 | $hdr->encoding = strtolower($arg_a[5]); |
845 | $hdr->entity_id = $msg->entity_id; |
e219fb97 |
846 | $hdr->size = $arg_a[6]; |
b65ca362 |
847 | if ($shifted_args) { |
848 | $hdr->lines = $arg_a[7]; |
849 | $s = 1; |
850 | } else { |
851 | $s = 0; |
852 | } |
853 | $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5); |
854 | $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition); |
855 | $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language); |
856 | $msg->header = $hdr; |
19ac8f64 |
857 | if ((strrchr($msg->entity_id, '.') == '.0') && ($msg->type0 !='multipart')) { |
858 | $msg->entity_id = $this->entity_id . '.1'; |
b65ca362 |
859 | } |
860 | } else { |
861 | $hdr->type0 = 'multipart'; |
862 | $hdr->type1 = $arg_a[0]; |
863 | $msg->type0 = 'multipart'; |
864 | $msg->type1 = $arg_a[0]; |
bc185bd2 |
865 | $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters); |
866 | $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition); |
867 | $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language); |
b65ca362 |
868 | $msg->header = $hdr; |
869 | } |
19ac8f64 |
870 | ++$i; |
b65ca362 |
871 | return (array($msg, $i)); |
872 | default: break; |
873 | } /* switch */ |
874 | |
875 | } /* for */ |
319cf0d2 |
876 | } /* parsestructure */ |
e219fb97 |
877 | |
b65ca362 |
878 | function parseProperties($read, $i) { |
879 | $properties = array(); |
880 | $prop_name = ''; |
b65ca362 |
881 | |
882 | for (; $read{$i} != ')'; ++$i) { |
e60230ae |
883 | $arg_s = ''; |
b65ca362 |
884 | if ($read{$i} == '"') { |
885 | $res = $this->parseQuote($read, $i); |
886 | $arg_s = $res[0]; |
887 | $i = $res[1]; |
888 | } else if ($read{$i} == '{') { |
889 | $res = $this->parseLiteral($read, $i); |
890 | $arg_s = $res[0]; |
891 | $i = $res[1]; |
892 | } |
893 | |
e60230ae |
894 | if ($arg_s != '') { |
895 | if ($prop_name == '') { |
896 | $prop_name = strtolower($arg_s); |
897 | $properties[$prop_name] = ''; |
898 | } else if ($prop_name != '') { |
899 | $properties[$prop_name] = $arg_s; |
900 | $prop_name = ''; |
901 | } |
b65ca362 |
902 | } |
903 | } |
e60230ae |
904 | return array($properties, $i); |
8f6b86cc |
905 | } |
e219fb97 |
906 | |
b65ca362 |
907 | function parseEnvelope($read, $i, $hdr) { |
8f6b86cc |
908 | $arg_no = 0; |
b65ca362 |
909 | $arg_a = array(); |
910 | $cnt = strlen($read); |
911 | |
912 | for (; ($i < $cnt) && ($read{$i} != ')'); ++$i) { |
913 | ++$i; |
914 | $char = strtoupper($read{$i}); |
915 | switch ($char) { |
916 | case '"': |
917 | $res = $this->parseQuote($read, $i); |
918 | $arg_a[] = $res[0]; |
919 | $i = $res[1]; |
920 | ++$arg_no; |
921 | break; |
922 | case '{': |
923 | $res = $this->parseLiteral($read, $i); |
924 | $arg_a[] = $res[0]; |
925 | $i = $res[1]; |
926 | ++$arg_no; |
927 | break; |
e219fb97 |
928 | case 'N': |
b65ca362 |
929 | /* probably NIL argument */ |
930 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
931 | $arg_a[] = ''; |
932 | ++$arg_no; |
933 | $i += 2; |
e219fb97 |
934 | } |
b65ca362 |
935 | break; |
936 | case '(': |
937 | /* Address structure (with group support) |
938 | * Note: Group support is useless on SMTP connections |
939 | * because the protocol doesn't support it |
940 | */ |
941 | $addr_a = array(); |
942 | $group = ''; |
943 | $a=0; |
944 | for (; $i < $cnt && $read{$i} != ')'; ++$i) { |
945 | if ($read{$i} == '(') { |
946 | $res = $this->parseAddress($read, $i); |
947 | $addr = $res[0]; |
948 | $i = $res[1]; |
949 | if (($addr->host == '') && ($addr->mailbox != '')) { |
950 | /* start of group */ |
951 | $group = $addr->mailbox; |
952 | $group_addr = $addr; |
953 | $j = $a; |
954 | } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) { |
955 | /* end group */ |
956 | if ($a == ($j+1)) { /* no group members */ |
957 | $group_addr->group = $group; |
958 | $group_addr->mailbox = ''; |
959 | $group_addr->personal = "$group: Undisclosed recipients;"; |
960 | $addr_a[] = $group_addr; |
961 | $group =''; |
962 | } |
963 | } else { |
964 | $addr->group = $group; |
965 | $addr_a[] = $addr; |
966 | } |
967 | ++$a; |
968 | } |
969 | } |
970 | $arg_a[] = $addr_a; |
971 | break; |
972 | default: break; |
973 | } |
974 | } |
975 | |
976 | if (count($arg_a) > 9) { |
977 | /* argument 1: date */ |
8f6b86cc |
978 | $d = strtr($arg_a[0], array(' ' => ' ')); |
979 | $d = explode(' ', $d); |
980 | $hdr->date = getTimeStamp($d); |
b65ca362 |
981 | |
982 | /* argument 2: subject */ |
983 | $arg_a[1] = (!trim($arg_a[1]) ? _("(no subject)") : $arg_a[1]); |
984 | $hdr->subject = $arg_a[1]; |
985 | |
986 | $hdr->from = $arg_a[2][0]; /* argument 3: from */ |
987 | $hdr->sender = $arg_a[3][0]; /* argument 4: sender */ |
988 | $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */ |
989 | $hdr->to = $arg_a[5]; /* argument 6: to */ |
990 | $hdr->cc = $arg_a[6]; /* argument 7: cc */ |
991 | $hdr->bcc = $arg_a[7]; /* argument 8: bcc */ |
992 | $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */ |
993 | $hdr->message_id = $arg_a[9]; /* argument 10: message-id */ |
994 | } |
995 | return (array($hdr, $i)); |
8f6b86cc |
996 | } |
e219fb97 |
997 | |
b65ca362 |
998 | function parseLiteral($read, $i) { |
999 | $lit_cnt = ''; |
1000 | for (++$i; $read{$i} != '}'; ++$i) { |
1001 | $lit_cnt .= $read{$i}; |
1002 | } |
1003 | |
1004 | $lit_cnt +=2; /* add the { and } characters */ |
1005 | $s = ''; |
1006 | for ($j = 0; $j < $lit_cnt; ++$j) { |
1007 | $s .= $read{++$i}; |
1008 | } |
1009 | return (array($s, $i)); |
8f6b86cc |
1010 | } |
e219fb97 |
1011 | |
b65ca362 |
1012 | function parseQuote($read, $i) { |
1013 | $s = ''; |
1014 | for (++$i; $read{$i} != '"'; ++$i) { |
1015 | if ($read{$i} == '\\') { |
1016 | ++$i; |
e219fb97 |
1017 | } |
b65ca362 |
1018 | $s .= $read{$i}; |
1019 | } |
e219fb97 |
1020 | return (array($s, $i)); |
8f6b86cc |
1021 | } |
e219fb97 |
1022 | |
b65ca362 |
1023 | function parseAddress($read, $i) { |
1024 | $arg_a = array(); |
1025 | |
1026 | for (; $read{$i} != ')'; ++$i) { |
1027 | $char = strtoupper($read{$i}); |
1028 | switch ($char) { |
1029 | case '"': |
e219fb97 |
1030 | case '{': |
b65ca362 |
1031 | $res = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i)); |
1032 | $arg_a[] = $res[0]; |
1033 | $i = $res[1]; |
1034 | break; |
1035 | case 'n': |
e219fb97 |
1036 | case 'N': |
b65ca362 |
1037 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
1038 | $arg_a[] = ''; |
1039 | $i += 2; |
1040 | } |
1041 | break; |
1042 | default: break; |
1043 | } |
1044 | } |
1045 | |
1046 | if (count($arg_a) == 4) { |
1047 | $adr = new address_structure(); |
1048 | $adr->personal = $arg_a[0]; |
1049 | $adr->adl = $arg_a[1]; |
1050 | $adr->mailbox = $arg_a[2]; |
1051 | $adr->host = $arg_a[3]; |
1052 | } else { |
1053 | $adr = ''; |
1054 | } |
1055 | return (array($adr, $i)); |
8f6b86cc |
1056 | } |
e219fb97 |
1057 | |
b65ca362 |
1058 | function parseDisposition($read, $i) { |
8f6b86cc |
1059 | $arg_a = array(); |
b65ca362 |
1060 | |
1061 | for (; $read{$i} != ')'; ++$i) { |
1062 | switch ($read{$i}) { |
1063 | case '"': |
e219fb97 |
1064 | case '{': |
b65ca362 |
1065 | case '(': |
1066 | switch ($read{$i}) { |
1067 | case '"': $res = $this->parseQuote($read, $i); break; |
1068 | case '{': $res = $this->parseLiteral($read, $i); break; |
1069 | case '(': $res = $this->parseProperties($read, $i); break; |
1070 | } |
1071 | $arg_a[] = $res[0]; |
1072 | $i = $res[1]; |
1073 | break; |
1074 | default: break; |
1075 | } |
e02d0fa7 |
1076 | } |
b65ca362 |
1077 | |
1078 | if (isset($arg_a[0])) { |
1079 | $disp = new disposition($arg_a[0]); |
1080 | if (isset($arg_a[1])) { |
1081 | $disp->properties = $arg_a[1]; |
1082 | } |
1083 | } |
1084 | |
1085 | return (is_object($disp) ? array($disp, $i) : array('', $i)); |
8f6b86cc |
1086 | } |
e219fb97 |
1087 | |
b65ca362 |
1088 | function parseLanguage($read, $i) { |
08c18186 |
1089 | /* no idea how to process this one without examples */ |
31e0cfba |
1090 | $arg_a = array(); |
b65ca362 |
1091 | |
1092 | for (; $read{$i} != ')'; ++$i) { |
1093 | switch ($read{$i}) { |
1094 | case '"': |
e219fb97 |
1095 | case '{': |
b65ca362 |
1096 | case '(': |
1097 | switch ($read{$i}) { |
1098 | case '"': $res = $this->parseQuote($read, $i); break; |
1099 | case '{': $res = $this->parseLiteral($read, $i); break; |
1100 | case '(': $res = $this->parseProperties($read, $i); break; |
1101 | } |
1102 | $arg_a[] = $res[0]; |
1103 | $i = $res[1]; |
1104 | break; |
1105 | default: break; |
1106 | } |
08c18186 |
1107 | } |
b65ca362 |
1108 | |
1109 | if (isset($arg_a[0])) { |
1110 | $lang = new language($arg_a[0]); |
1111 | if (isset($arg_a[1])) { |
1112 | $lang->properties = $arg_a[1]; |
1113 | } |
31e0cfba |
1114 | } |
b65ca362 |
1115 | |
1116 | return (is_object($lang) ? array($lang, $i) : array('', $i)); |
8f6b86cc |
1117 | } |
e219fb97 |
1118 | |
b65ca362 |
1119 | function parseParenthesis($read, $i) { |
1120 | for (; $read{$i} != ')'; ++$i) { |
1121 | switch ($read{$i}) { |
1122 | case '"': |
e219fb97 |
1123 | case '{': |
b65ca362 |
1124 | case '(': |
1125 | switch ($read{$i}) { |
1126 | case '"': $res = $this->parseQuote($read, $i); break; |
1127 | case '{': $res = $this->parseLiteral($read, $i); break; |
1128 | case '(': $res = $this->parseProperties($read, $i); break; |
1129 | } |
1130 | $i = $res[1]; |
1131 | break; |
1132 | default: break; |
1133 | } |
e219fb97 |
1134 | } |
b65ca362 |
1135 | return $i; |
c52ffd66 |
1136 | } |
1137 | |
b65ca362 |
1138 | /* Function to fill the message structure in case the */ |
1139 | /* bodystructure is not available NOT FINISHED YET */ |
1140 | function parseMessage($read, $type0, $type1) { |
1141 | switch ($type0) { |
1142 | case 'message': |
1143 | $rfc822_header = true; |
1144 | $mime_header = false; |
1145 | break; |
1146 | case 'multipart': |
1147 | $rfc822_header = false; |
1148 | $mime_header = true; |
1149 | break; |
1150 | default: return $read; |
1151 | } |
1152 | |
1153 | for ($i = 1; $i < $count; ++$i) { |
1154 | $line = trim($body[$i]); |
e219fb97 |
1155 | if (($mime_header || $rfc822_header) && |
b65ca362 |
1156 | (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) { |
1157 | $bnd = $reg[1]; |
e219fb97 |
1158 | $bndreg = $bnd; |
1159 | $bndreg = str_replace("\\", "\\\\", $bndreg); |
b65ca362 |
1160 | $bndreg = str_replace("?", "\\?", $bndreg); |
e219fb97 |
1161 | $bndreg = str_replace("+", "\\+", $bndreg); |
1162 | $bndreg = str_replace(".", "\\.", $bndreg); |
b65ca362 |
1163 | $bndreg = str_replace("/", "\\/", $bndreg); |
1164 | $bndreg = str_replace("-", "\\-", $bndreg); |
1165 | $bndreg = str_replace("(", "\\(", $bndreg); |
1166 | $bndreg = str_replace(")", "\\)", $bndreg); |
1167 | } else if ($rfc822_header && $line == '') { |
1168 | $rfc822_header = false; |
1169 | if ($msg->type0 == 'multipart') { |
1170 | $mime_header = true; |
1171 | } |
e219fb97 |
1172 | } |
1173 | |
b65ca362 |
1174 | if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) { |
1175 | $cnt=count($boundaries)-1; |
1176 | $bnd = $boundaries[$cnt]['bnd']; |
1177 | $bndreg = $boundaries[$cnt]['bndreg']; |
e219fb97 |
1178 | |
b65ca362 |
1179 | $regstr = '/^--'."($bndreg)".".*".'/'; |
1180 | if (preg_match($regstr, $line, $reg)) { |
1181 | $bndlen = strlen($reg[1]); |
e219fb97 |
1182 | $bndend = false; |
b65ca362 |
1183 | if (strlen($line) > ($bndlen + 3)) { |
1184 | if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) { |
1185 | $bndend = true; |
1186 | } |
1187 | } |
1188 | if ($bndend) { |
1189 | /* calc offset and return $msg */ |
1190 | //$entStr = CalcEntity("$entStr", -1); |
1191 | array_pop($boundaries); |
1192 | $mime_header = true; |
1193 | $bnd_end = true; |
1194 | } else { |
1195 | $mime_header = true; |
1196 | $bnd_end = false; |
1197 | //$entStr = CalcEntity("$entStr", 0); |
1198 | ++$content_indx; |
1199 | } |
1200 | } else { |
1201 | if ($header) { } |
1202 | } |
e219fb97 |
1203 | } |
b65ca362 |
1204 | } |
319cf0d2 |
1205 | } |
1206 | |
77b07064 |
1207 | function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) { |
8f288482 |
1208 | $found = false; |
77b07064 |
1209 | if ($this->type0 == 'multipart') { |
1210 | if($this->type1 == 'alternative') { |
1211 | $msg = $this->findAlternativeEntity($alt_order); |
b65ca362 |
1212 | if (count($msg->entities) == 0) { |
8f288482 |
1213 | $entity[] = $msg->entity_id; |
b65ca362 |
1214 | } else { |
77b07064 |
1215 | $entity = $msg->findDisplayEntity($entity, $alt_order, $strict); |
8f288482 |
1216 | } |
8f288482 |
1217 | $found = true; |
77b07064 |
1218 | } else if ($this->type1 == 'related') { /* RFC 2387 */ |
1219 | $msgs = $this->findRelatedEntity(); |
1220 | foreach ($msgs as $msg) { |
1221 | if (count($msg->entities) == 0) { |
1222 | $entity[] = $msg->entity_id; |
1223 | } else { |
1224 | $entity = $msg->findDisplayEntity($entity, $alt_order, $strict); |
1225 | } |
1226 | } |
1227 | if (count($msgs) > 0) { |
1228 | $found = true; |
1229 | } |
1230 | } else { /* Treat as multipart/mixed */ |
1231 | foreach ($this->entities as $ent) { |
1232 | if(strtolower($ent->header->disposition->name) != 'attachment' && |
1233 | ($ent->type0 != 'message' && $ent->type1 != 'rfc822')) |
1234 | { |
1235 | $entity = $ent->findDisplayEntity($entity, $alt_order, $strict); |
1236 | $found = true; |
1237 | } |
1238 | } |
8f288482 |
1239 | } |
77b07064 |
1240 | } else { /* If not multipart, then just compare with each entry from $alt_order */ |
1241 | $type = $this->type0.'/'.$this->type1; |
1242 | foreach ($alt_order as $alt) { |
1243 | if( ($alt == $type) && isset($this->entity_id) ) { |
1244 | if ( (count($this->entities) == 0) && |
1245 | (strtolower($this->header->disposition->name) != 'attachment') ) |
1246 | { |
1247 | $entity[] = $this->entity_id; |
1248 | $found = true; |
1249 | } |
8f288482 |
1250 | } |
1251 | } |
1252 | } |
77b07064 |
1253 | if(!$found) { |
8f288482 |
1254 | foreach ($this->entities as $ent) { |
b65ca362 |
1255 | if((strtolower($ent->header->disposition->name) != 'attachment') && |
1256 | (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) { |
77b07064 |
1257 | $entity = $ent->findDisplayEntity($entity, $alt_order, $strict); |
1258 | $found = true; |
1259 | } |
1260 | } |
1261 | } |
1262 | if(!$strict && !$found) { |
1263 | if ($this->type0 == 'text' && |
1264 | ($this->type1 == 'plain' || |
1265 | $this->type1 == 'html' || |
1266 | $this->type1 == 'message') && |
1267 | isset($this->entity_id) ) |
1268 | { |
1269 | if (count($this->entities) == 0) { |
1270 | if (strtolower($this->header->disposition->name) != 'attachment') { |
1271 | $entity[] = $this->entity_id; |
1272 | } |
8f288482 |
1273 | } |
1274 | } |
1275 | } |
b65ca362 |
1276 | |
77b07064 |
1277 | return $entity; |
8f6b86cc |
1278 | } |
319cf0d2 |
1279 | |
b65ca362 |
1280 | function findAlternativeEntity($alt_order) { |
1281 | /* If we are dealing with alternative parts then we */ |
1282 | /* choose the best viewable message supported by SM. */ |
319cf0d2 |
1283 | $best_view = 0; |
8f288482 |
1284 | $entity = array(); |
1285 | $altcount = count($alt_order); |
b65ca362 |
1286 | |
1287 | foreach($this->entities as $ent) { |
1288 | $type = $ent->header->type0 . '/' . $ent->header->type1; |
1289 | if ($type == 'multipart/related') { |
8f288482 |
1290 | $type = $ent->header->getParameter('type'); |
1291 | } |
b65ca362 |
1292 | for ($j = $best_view; $j < $altcount; ++$j) { |
1293 | if (($alt_order[$j] == $type) && ($j >= $best_view)) { |
8f288482 |
1294 | $best_view = $j; |
1295 | $entity = $ent; |
1296 | } |
1297 | } |
319cf0d2 |
1298 | } |
b65ca362 |
1299 | |
8f288482 |
1300 | return $entity; |
319cf0d2 |
1301 | } |
e219fb97 |
1302 | |
b65ca362 |
1303 | function findRelatedEntity() { |
e219fb97 |
1304 | $msgs = array(); |
8f288482 |
1305 | $entcount = count($this->entities); |
b65ca362 |
1306 | |
1307 | for ($i = 0; $i < $entcount; ++$i) { |
319cf0d2 |
1308 | $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1; |
b65ca362 |
1309 | if ($this->header->getParameter('type') == $type) { |
8f288482 |
1310 | $msgs[] = $this->entities[$i]; |
1311 | } |
319cf0d2 |
1312 | } |
b65ca362 |
1313 | |
319cf0d2 |
1314 | return $msgs; |
1315 | } |
e219fb97 |
1316 | |
b65ca362 |
1317 | function getAttachments($exclude_id=array(), $result = array()) { |
1318 | if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) { |
1319 | $this = $this->entities[0]; |
1320 | } |
1321 | |
1322 | if (count($this->entities)) { |
1323 | foreach ($this->entities as $entity) { |
1324 | $exclude = false; |
1325 | |
1326 | foreach ($exclude_id as $excl) { |
1327 | if ($entity->entity_id === $excl) { |
1328 | $exclude = true; |
1329 | } |
1330 | } |
1331 | |
1332 | if (!$exclude) { |
e219fb97 |
1333 | if (($entity->type0 == 'multipart') && |
b65ca362 |
1334 | ($entity->type1 != 'related')) { |
1335 | $result = $entity->getAttachments($exclude_id, $result); |
1336 | } else if ($entity->type0 != 'multipart') { |
1337 | $result[] = $entity; |
1338 | } |
1339 | } |
1340 | } |
1341 | } else { |
1342 | $exclude = false; |
1343 | foreach ($exclude_id as $excl) { |
1344 | $exclude = $exclude || ($this->entity_id == $excl); |
1345 | } |
1346 | |
1347 | if (!$exclude) { |
1348 | $result[] = $this; |
1349 | } |
1350 | } |
1351 | |
1352 | return $result; |
e219fb97 |
1353 | } |
8f6b86cc |
1354 | } |
1355 | |
b65ca362 |
1356 | class smime_message { |
1357 | |
c52ffd66 |
1358 | } |
1359 | |
b65ca362 |
1360 | class disposition { |
1361 | function disposition($name) { |
e8bedb2d |
1362 | $this->name = $name; |
1363 | $this->properties = array(); |
1364 | } |
924b0cac |
1365 | |
b65ca362 |
1366 | function getProperty($par) { |
924b0cac |
1367 | $value = strtolower($par); |
b65ca362 |
1368 | if (isset($this->properties[$par])) { |
1369 | return $this->properties[$par]; |
924b0cac |
1370 | } |
1371 | return ''; |
1372 | } |
8f6b86cc |
1373 | } |
1374 | |
b65ca362 |
1375 | class language { |
1376 | function language($name) { |
e8bedb2d |
1377 | $this->name = $name; |
1378 | $this->properties = array(); |
1379 | } |
1380 | } |
1381 | |
b65ca362 |
1382 | class content_type { |
e219fb97 |
1383 | var $type0 = 'text', |
1384 | $type1 = 'plain', |
b65ca362 |
1385 | $properties = ''; |
1386 | |
1387 | function content_type($type) { |
1388 | $pos = strpos($type, '/'); |
1389 | if ($pos > 0) { |
1390 | $this->type0 = substr($type, 0, $pos); |
1391 | $this->type1 = substr($type, $pos+1); |
1392 | } else { |
1393 | $this->type0 = $type; |
1394 | } |
1395 | $this->properties = array(); |
c52ffd66 |
1396 | } |
31e0cfba |
1397 | } |
1398 | |
4b8699db |
1399 | ?> |