8f6b86cc |
1 | <?php |
2 | |
3 | /** |
4 | * mime.class |
5 | * |
6 | * Copyright (c) 2002 The SquirrelMail Project Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * |
10 | * This contains functions needed to handle mime messages. |
11 | * |
12 | * $Id$ |
13 | */ |
14 | |
15 | class msg_header { |
16 | /** msg_header contains generic variables for values that **/ |
17 | /** could be in a header. **/ |
18 | |
19 | var $type0 = '', $type1 = '', $boundary = '', $charset = '', |
20 | $encoding='', $size = 0, $to = array(), $from = '', $date = '', |
21 | $cc = array(), $bcc = array(), $reply_to = '', $subject = '', |
22 | $id = 0, $mailbox = '', $description = '', $filename = '', |
23 | $entity_id = 0, $message_id = 0, $name = '', $priority = 3, $type = '', |
319cf0d2 |
24 | $disposition = '', $md5='', $language='',$dnt = '', $xmailer = ''; |
25 | |
8f6b86cc |
26 | /* |
27 | * returns addres_list of supplied argument |
28 | * arguments: array('to', 'from', ...) or just a string like 'to'. |
29 | * result: string: address1, addres2, .... |
319cf0d2 |
30 | */ |
31 | |
8f6b86cc |
32 | function setVar($var, $value) { |
33 | $this->{$var} = $value; |
34 | } |
319cf0d2 |
35 | /* |
36 | * function to get the addres strings out of the header. |
37 | * Arguments: string or array of strings ! |
38 | * example1: header->getAddr_s('to'). |
39 | * example2: header->getAddr_s(array('to','cc','bcc')) |
40 | */ |
8f6b86cc |
41 | function getAddr_s($arr) { |
319cf0d2 |
42 | if (is_array($arr)) { |
43 | $s = ''; |
44 | foreach($arr as $arg ) { |
45 | $result = $this->getAddr_s($arg); |
46 | if ($result) { |
47 | $s .= ', ' . $result; |
48 | } |
49 | } |
50 | if ($s) $s = substr($s,2); |
51 | return $s; |
52 | } else { |
53 | $s = ''; |
54 | eval('$addr = $this->'.$arr.';') ; |
55 | if (is_array($addr)) { |
56 | foreach ($addr as $addr_o) { |
57 | if (is_object($addr_o)) { |
58 | $s .= $addr_o->getAddress() . ', '; |
59 | } |
60 | } |
61 | $s = substr($s,0,-2); |
62 | } else { |
63 | if (is_object($addr)) { |
64 | $s .= $addr->getAddress(); |
65 | } |
66 | } |
67 | return $s; |
68 | } |
8f6b86cc |
69 | } |
319cf0d2 |
70 | |
8f6b86cc |
71 | function getAddr_a($arg, $excl_arr=array(), $arr = array()) { |
319cf0d2 |
72 | if (is_array($arg)) { |
73 | foreach($arg as $argument ) { |
74 | $arr = $this->getAddr_a($argument, $excl_arr, $arr); |
75 | } |
76 | return $arr; |
77 | } else { |
78 | eval('$addr = $this->'.$arg.';') ; |
79 | if (is_array($addr)) { |
80 | foreach ($addr as $addr_o) { |
81 | if (is_object($addr_o)) { |
82 | if (isset($addr_o->host) && $addr_o->host !='') { |
83 | $email = $addr_o->mailbox.'@'.$addr_o->host; |
84 | } else { |
85 | $email = $addr_o->mailbox; |
86 | } |
87 | $email = strtolower($email); |
88 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
89 | $arr[$email] = $addr_o->personal; |
90 | } |
91 | } |
92 | } |
93 | } else { |
94 | if (is_object($addr)) { |
95 | if (isset($addr->host)) { |
96 | $email = $addr->mailbox.'@'.$addr->host; |
97 | } else { |
98 | $email = $addr->mailbox; |
99 | } |
100 | $email = strtolower($email); |
101 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
102 | $arr[$email] = $addr->personal; |
103 | } |
104 | } |
105 | } |
106 | return $arr; |
107 | } |
8f6b86cc |
108 | } |
109 | } |
110 | |
111 | class address_structure { |
319cf0d2 |
112 | var $personal = '', $adl = '', $mailbox = '', $host = '', $group = ''; |
8f6b86cc |
113 | |
319cf0d2 |
114 | function getAddress($full=true) { |
115 | if (is_object($this)) { |
116 | if (isset($this->host) && $this->host !='') { |
117 | $email = '<'.$this->mailbox.'@'.$this->host.'>'; |
e02d0fa7 |
118 | } else { |
319cf0d2 |
119 | $email = $this->mailbox; |
e02d0fa7 |
120 | } |
319cf0d2 |
121 | if (trim($this->personal) !='') { |
122 | if ($email) { |
123 | $addr = '"' . $this->personal . '" ' .$email; |
124 | } else { |
125 | $addr = $this->personal; |
126 | } |
127 | $best_dpl = $this->personal; |
128 | } else { |
129 | $addr = $email; |
130 | $best_dpl = $email; |
131 | } |
132 | if ($full) { |
133 | return $addr; |
134 | } else { |
135 | return $best_dpl; |
136 | } |
137 | } else return ''; |
138 | } |
8f6b86cc |
139 | } |
140 | |
141 | class message { |
142 | /** message is the object that contains messages. It is a recursive |
143 | object in that through the $entities variable, it can contain |
144 | more objects of type message. See documentation in mime.txt for |
145 | a better description of how this works. |
146 | **/ |
147 | var $header = '', $entities = array(), $mailbox = 'INBOX', $id = 0, |
148 | $envelope = '', $parent_ent, $entity, $type0='', $type1='', |
319cf0d2 |
149 | $parent = '', $decoded_body='', |
150 | $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0, |
151 | $is_mdnsent = 0; |
8f6b86cc |
152 | |
153 | function setEnt($ent) { |
154 | $this->entity_id= $ent; |
155 | } |
319cf0d2 |
156 | |
8f6b86cc |
157 | function setBody($body) { |
158 | $this->decoded_body = $body; |
159 | } |
160 | function addEntity ($msg) { |
161 | $msg->parent = &$this; |
162 | $this->entities[] = $msg; |
163 | } |
164 | |
165 | function addRFC822Header($read) { |
319cf0d2 |
166 | $header = new msg_header(); |
167 | $this->header = sqimap_parse_RFC822Header($read,$header); |
8f6b86cc |
168 | } |
169 | |
170 | function getEntity($ent) { |
319cf0d2 |
171 | |
8f6b86cc |
172 | $cur_ent = $this->entity_id; |
319cf0d2 |
173 | $msg = $this; |
174 | if ($cur_ent == '' || $cur_ent == '0') { |
175 | $cur_ent_a = array(); |
176 | } else { |
177 | $cur_ent_a = explode('.',$this->entity_id); |
178 | } |
179 | $ent_a = explode('.',$ent); |
180 | |
181 | $cnt = count($ent_a); |
8f6b86cc |
182 | |
319cf0d2 |
183 | for ($i=0;$i<$cnt -1;$i++) { |
184 | if (isset($cur_ent_a[$i]) && $cur_ent_a[$i] != $ent_a[$i]) { |
185 | $msg = $msg->parent; |
186 | $cur_ent_a = explode('.',$msg->entity_id); |
187 | $i--; |
188 | } else if (!isset($cur_ent_a[$i])) { |
189 | if (isset($msg->entities[($ent_a[$i]-1)])) { |
190 | $msg = $msg->entities[($ent_a[$i]-1)]; |
191 | } else { |
192 | $msg = $msg->entities[0]; |
193 | } |
194 | } |
195 | if ($msg->type0 == 'message') { |
196 | /*this is a header for a message/rfc822 entity */ |
197 | $msg = $msg->entities[0]; |
198 | } |
199 | } |
200 | if ($msg->type0 == 'message') { |
201 | /*this is a header for a message/rfc822 entity */ |
202 | $msg = $msg->entities[0]; |
203 | } |
8f6b86cc |
204 | |
319cf0d2 |
205 | if (isset($msg->entities[($ent_a[$cnt-1])-1])) { |
206 | $msg = $msg->entities[($ent_a[$cnt-1]-1)]; |
8f6b86cc |
207 | } |
319cf0d2 |
208 | return $msg; |
8f6b86cc |
209 | } |
319cf0d2 |
210 | |
211 | function getMailbox() { |
212 | $msg = $this; |
213 | while (is_object($msg->parent)) { |
214 | $msg = $msg->parent; |
215 | } |
216 | return $msg->mailbox; |
217 | } |
218 | |
8f6b86cc |
219 | /* |
220 | * Bodystructure parser, a recursive function for generating the |
221 | * entity-tree with all the mime-parts. |
222 | * |
223 | * It follows RFC2060 and stores all the described fields in the |
224 | * message object. |
225 | * |
226 | * Question/Bugs: |
227 | * |
228 | * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net. |
229 | * |
230 | */ |
231 | function parseStructure($read, $i=0, $message = false) { |
319cf0d2 |
232 | $arg_no = 0; |
233 | $arg_a = array(); |
234 | $cnt = strlen($read); |
235 | while ($i < $cnt) { |
236 | $char = strtoupper($read{$i}); |
237 | switch ($char) { |
238 | case '(': |
239 | if ($arg_no == 0 ) { |
240 | if (!isset($msg)) { |
241 | $msg = new message(); |
242 | $hdr = new msg_header(); |
243 | $hdr->type0 = 'text'; |
244 | $hdr->type1 = 'plain'; |
245 | $hdr->encoding = 'us-ascii'; |
246 | |
247 | if ($this->type0 == 'message' && $this->type1 == 'rfc822') { |
248 | $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */ |
249 | } else if (isset($this->entity_id) && $this->entity_id !='') { |
250 | $ent_no = count($this->entities)+1; |
251 | $par_ent = substr($this->entity_id,-2); |
252 | if ($par_ent{0} == '.') { |
253 | $par_ent = $par_ent{1}; |
254 | } |
255 | if ($par_ent == '0') { |
256 | $ent_no = count($this->entities)+1; |
257 | if ($ent_no > 0) { |
258 | $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.')); |
259 | if ($ent) { |
260 | $ent = $ent . ".$ent_no"; |
261 | } else { |
262 | $ent = $ent_no; |
263 | } |
264 | $msg->entity_id = $ent; |
265 | } else { |
266 | $msg->entity_id = $ent_no; |
267 | } |
268 | } else { |
269 | $ent = $this->entity_id . ".$ent_no"; |
270 | $msg->entity_id = $ent; |
271 | } |
272 | } else { |
273 | $msg->entity_id = '0'; |
274 | } |
275 | } else { |
276 | $msg->header->type0 = 'multipart'; |
277 | $msg->type0 = 'multipart'; |
278 | while ($read{$i} == '(') { |
279 | $msg->addEntity($msg->parseStructure($read,&$i)); |
280 | } |
281 | } |
282 | } else { |
283 | switch ($arg_no) { |
284 | case 1: |
285 | /* multipart properties */ |
286 | $i++; |
287 | $arg_a[] = $this->parseProperties($read,&$i); |
288 | $arg_no++; |
289 | break; |
290 | case 2: |
291 | if (isset($msg->type0) && $msg->type0 == 'multipart') { |
292 | $i++; |
293 | $arg_a[]= $msg->parseDisposition($read,&$i); |
294 | } else { /* properties */ |
295 | /* properties */ |
296 | $arg_a[] = $msg->parseProperties($read,&$i); |
297 | } |
298 | $arg_no++; |
299 | break; |
300 | case 7: |
301 | if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822') { |
2c761b52 |
302 | |
319cf0d2 |
303 | $msg->header->type0 = $arg_a[0]; |
304 | $msg->type0 = $arg_a[0]; |
8f6b86cc |
305 | |
319cf0d2 |
306 | $msg->header->type1 = $arg_a[1]; |
307 | $msg->type1 = $arg_a[1]; |
308 | |
309 | $msg->parseEnvelope($read,&$i,&$hdr); |
310 | $i++; |
311 | while ($i < $cnt && $read{$i} != '(') { |
312 | $i++; |
313 | } |
314 | $msg->addEntity($msg->parseStructure($read,&$i)); |
315 | } |
316 | break; |
317 | case 8: |
318 | $i++; |
319 | $arg_a[] = $msg->parseDisposition($read,&$i); |
320 | $arg_no++; |
321 | break; |
322 | case 9: |
323 | if ($arg_a[0] == 'text' || |
324 | ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) { |
325 | $i++; |
326 | $arg_a[] = $msg->parseDisposition($read,&$i); |
327 | } else { |
328 | $arg_a[] = $msg->parseLanguage($read,&$i); |
329 | } |
330 | $arg_no++; |
331 | break; |
332 | case 10: |
333 | if ($arg_a[0] == 'text' || |
334 | ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) { |
335 | $arg_a[] = $msg->parseLanguage($read,&$i); |
336 | } else { |
337 | $arg_a[] = ''; /* not yet desribed in rfc2060 */ |
338 | } |
339 | $arg_no++; |
340 | break; |
341 | default: |
342 | /* unknown argument, skip this part */ |
343 | $msg->parseParenthesis($read,&$i); |
344 | $arg_a[] = ''; |
345 | $arg_no++; |
346 | break; |
347 | } /* switch */ |
348 | } |
349 | break; |
350 | case '"': |
351 | /* inside an entity -> start processing */ |
352 | $debug = substr($read,$i,20); |
353 | $arg_s = $msg->parseQuote($read,&$i); |
354 | $arg_no++; |
355 | if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */ |
356 | $arg_a[] = $arg_s; |
357 | break; |
358 | case 'N': |
359 | /* probably NIL argument */ |
360 | if (strtolower(substr($read,$i,3)) == 'nil') { |
361 | $arg_a[] = ''; |
362 | $arg_no++; |
363 | $i = $i+2; |
364 | } |
365 | break; |
366 | case '{': |
367 | /* process the literal value */ |
368 | $arg_a[] = $msg->parseLiteral($read,&$i); |
369 | $arg_no++; |
370 | break; |
371 | case (is_numeric($read{$i}) ): |
372 | /* process integers */ |
373 | if ($read{$i} == ' ') break; |
374 | $arg_s = $read{$i};; |
375 | $i++; |
376 | while (preg_match('/\d+/',$read{$i})) { // != ' ') { |
377 | $arg_s .= $read{$i}; |
378 | $i++; |
379 | } |
380 | $arg_no++; |
381 | $arg_a[] = $arg_s; |
382 | break; |
383 | case ')': |
384 | if (isset($msg->type0) && $msg->type0 == 'multipart') { |
385 | $multipart = true; |
386 | } else { |
387 | $multipart = false; |
388 | } |
389 | if (!$multipart) { |
390 | if ($arg_a[0] == 'text' || |
391 | ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) { |
392 | $shifted_args = true; |
393 | } else { |
394 | $shifted_args = false; |
395 | } |
396 | $hdr->type0 = $arg_a[0]; |
397 | $hdr->type1 = $arg_a[1]; |
8f6b86cc |
398 | |
319cf0d2 |
399 | $msg->type0 = $arg_a[0]; |
400 | $msg->type1 = $arg_a[1]; |
8f6b86cc |
401 | |
319cf0d2 |
402 | $arr = $arg_a[2]; |
403 | if (is_array($arr)) { |
404 | foreach($arr as $name => $value) { |
405 | $hdr->{$name} = $value; |
406 | } |
407 | } |
408 | $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) ); |
409 | $hdr->description = $arg_a[4]; |
410 | $hdr->encoding = strtolower($arg_a[5]); |
411 | $hdr->entity_id = $msg->entity_id; |
412 | $hdr->size = $arg_a[6]; |
413 | if ($shifted_args) { |
414 | $hdr->lines = $arg_a[7]; |
415 | if (isset($arg_a[8])) { |
416 | $hdr->md5 = $arg_a[8]; |
417 | } |
418 | if (isset($arg_a[9])) { |
419 | $hdr->disposition = $arg_a[9]; |
420 | } |
421 | if (isset($arg_a[10])) { |
422 | $hdr->language = $arg_a[10]; |
423 | } |
424 | } else { |
425 | if (isset($arg_a[7])) { |
426 | $hdr->md5 = $arg_a[7]; |
427 | } |
428 | if (isset($arg_a[8])) { |
429 | $hdr->disposition = $arg_a[8]; |
430 | } |
431 | if (isset($arg_a[9])) { |
432 | $hdr->language = $arg_a[9]; |
433 | } |
434 | } |
435 | $msg->header = $hdr; |
436 | $arg_no = 0; |
437 | $i++; |
438 | if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') { |
439 | $msg->entity_id++; |
440 | } |
441 | return $msg; |
442 | } else { |
443 | $hdr->type0 = 'multipart'; |
444 | $hdr->type1 = $arg_a[0]; |
0fee68d1 |
445 | |
319cf0d2 |
446 | $msg->type0 = 'multipart'; |
447 | $msg->type1 = $arg_a[0]; |
448 | if (isset($arg_a[1])) { |
449 | $arr = $arg_a[1]; |
450 | if (is_array($arr)) { |
451 | foreach($arr as $name => $value) { |
452 | $hdr->{$name} = $value; |
453 | } |
454 | } |
455 | } |
456 | if (isset($arg_a[2])) { |
457 | $hdr->disposition = $arg_a[2]; |
458 | } |
459 | if (isset($arg_a[3])) { |
460 | $hdr->language = $arg_a[3]; |
461 | } |
462 | $msg->header = $hdr; |
463 | return $msg; |
464 | } |
465 | default: |
466 | break; |
467 | } /* switch */ |
468 | $i++; |
469 | } /* while */ |
470 | } /* parsestructure */ |
471 | |
8f6b86cc |
472 | function parseProperties($read, $i) { |
319cf0d2 |
473 | $properties = array(); |
474 | $arg_s = ''; |
475 | $prop_name = ''; |
476 | while ($read{$i} != ')') { |
477 | if ($read{$i} == '"') { |
478 | $arg_s = $this->parseQuote($read,&$i); |
479 | } else if ($read{$i} == '{') { |
480 | $arg_s = $this->parseLiteral($read,&$i); |
481 | } |
482 | if ($prop_name == '' && $arg_s) { |
483 | $prop_name = strtolower($arg_s); |
484 | $properties[$prop_name] = ''; |
485 | $arg_s = ''; |
486 | } elseif ($prop_name != '' && $arg_s != '') { |
487 | $properties[$prop_name] = $arg_s; |
488 | $prop_name = ''; |
489 | $arg_s = ''; |
490 | } |
491 | $i++; |
492 | } |
493 | return $properties; |
8f6b86cc |
494 | } |
495 | |
496 | function parseEnvelope($read, $i, $hdr) { |
497 | $arg_no = 0; |
319cf0d2 |
498 | $arg_a = array(); |
499 | $cnt = strlen($read); |
500 | while ($i< $cnt && $read{$i} != ')') { |
501 | $i++; |
502 | $char = strtoupper($read{$i}); |
503 | switch ($char) { |
504 | case '"': |
505 | $arg_a[] = $this->parseQuote($read,&$i); |
506 | $arg_no++; |
507 | break; |
508 | case '{': |
509 | $arg_a[] = $this->parseLiteral($read,&$i); |
510 | $arg_no++; |
511 | break; |
512 | case 'N': |
513 | /* probably NIL argument */ |
514 | if (strtolower(substr($read,$i,3)) == 'nil') { |
515 | $arg_a[] = ''; |
516 | $arg_no++; |
517 | $i = $i+2; |
518 | } |
519 | break; |
520 | case '(': |
521 | /* Address structure |
522 | * With group support. |
523 | * Note: Group support is useless on SMTP connections |
524 | * because the protocol doesn't support it |
525 | */ |
526 | $addr_a = array(); |
527 | $group = ''; |
528 | $a=0; |
529 | while ($i < $cnt && $read{$i} != ')') { |
530 | if ($read{$i} == '(') { |
531 | $addr = $this->parseAddress($read,&$i); |
532 | if ($addr->host == '' && $addr->mailbox != '') { |
533 | /* start of group */ |
534 | $group = $addr->mailbox; |
535 | $group_addr = $addr; |
536 | $j = $a; |
537 | } elseif ($group && $addr->host == '' && $addr->mailbox == '') { |
538 | /* end group */ |
539 | if ($a == $j+1) { /* no group members */ |
540 | $group_addr->group = $group; |
541 | $group_addr->mailbox = ''; |
542 | $group_addr->personal = "$group: Undisclosed recipients;"; |
543 | $addr_a[] = $group_addr; |
544 | $group =''; |
545 | } |
546 | } else { |
547 | $addr->group = $group; |
548 | $addr_a[] = $addr; |
549 | } |
550 | $a++; |
551 | } |
552 | $i++; |
553 | } |
554 | $arg_a[] = $addr_a; |
555 | break; |
556 | default: |
557 | break; |
558 | } |
559 | $i++; |
560 | } |
561 | if (count($arg_a) > 9) { |
562 | /* argument 1: date */ |
8f6b86cc |
563 | $d = strtr($arg_a[0], array(' ' => ' ')); |
564 | $d = explode(' ', $d); |
565 | $hdr->date = getTimeStamp($d); |
319cf0d2 |
566 | /* argument 2: subject */ |
567 | if (!trim($arg_a[1])) { |
568 | $arg_a[1]= _("(no subject)"); |
569 | } |
570 | $hdr->subject = $arg_a[1]; |
571 | /* argument 3: from */ |
572 | $hdr->from = $arg_a[2][0]; |
573 | /* argument 4: sender */ |
574 | $hdr->sender = $arg_a[3][0]; |
575 | /* argument 5: reply-to */ |
576 | $hdr->replyto = $arg_a[4][0]; |
577 | /* argument 6: to */ |
578 | $hdr->to = $arg_a[5]; |
579 | /* argument 7: cc */ |
580 | $hdr->cc = $arg_a[6]; |
581 | /* argument 8: bcc */ |
582 | $hdr->bcc = $arg_a[7]; |
583 | /* argument 9: in-reply-to */ |
584 | $hdr->inreplyto = $arg_a[8]; |
585 | /* argument 10: message-id */ |
586 | $hdr->message_id = $arg_a[9]; |
587 | } |
8f6b86cc |
588 | } |
319cf0d2 |
589 | |
8f6b86cc |
590 | function parseLiteral($read, $i) { |
319cf0d2 |
591 | $lit_cnt = ''; |
592 | $i++; |
593 | while ($read{$i} != '}') { |
594 | $lit_cnt .= $read{$i}; |
595 | $i++; |
596 | } |
597 | $lit_cnt +=2; /* add the { and } characters */ |
598 | $s = ''; |
599 | for ($j = 0; $j < $lit_cnt; $j++) { |
600 | $i++; |
601 | $s .= $read{$i}; |
602 | } |
603 | return $s; |
8f6b86cc |
604 | } |
319cf0d2 |
605 | |
8f6b86cc |
606 | function parseQuote($read, $i) { |
319cf0d2 |
607 | $i++; |
608 | $s = ''; |
609 | $cnt = strlen($read); |
610 | while ($i < $cnt && $read{$i} != '"') { |
611 | if ($read{$i} == '\\') { |
612 | $i++; |
613 | } |
614 | $s .= $read{$i}; |
615 | $i++; |
616 | } |
8f6b86cc |
617 | return $s; |
618 | } |
319cf0d2 |
619 | |
8f6b86cc |
620 | function parseAddress($read, $i) { |
319cf0d2 |
621 | $arg_a = array(); |
8f6b86cc |
622 | while ($read{$i} != ')') { |
319cf0d2 |
623 | $char = strtoupper($read{$i}); |
624 | switch ($char) { |
625 | case '"': |
626 | $arg_a[] = $this->parseQuote($read,&$i); |
627 | break; |
628 | case '{': |
629 | $arg_a[] = $this->parseLiteral($read,&$i); |
630 | break; |
631 | case 'N': |
632 | if (strtolower(substr($read,$i,3)) == 'nil') { |
633 | $arg_a[] = ''; |
634 | $i = $i+2; |
635 | } |
636 | default: |
637 | break; |
638 | } |
639 | $i++; |
640 | } |
641 | if (count($arg_a) == 4) { |
642 | $adr = new address_structure(); |
643 | $adr->personal = $arg_a[0]; |
644 | $adr->adl = $arg_a[1]; |
645 | $adr->mailbox = $arg_a[2]; |
646 | $adr->host = $arg_a[3]; |
647 | } else $adr = ''; |
648 | return $adr; |
8f6b86cc |
649 | } |
319cf0d2 |
650 | |
8f6b86cc |
651 | function parseDisposition($read,$i) { |
652 | $arg_a = array(); |
653 | while ($read{$i} != ')') { |
319cf0d2 |
654 | switch ($read{$i}) { |
655 | case '"': |
656 | $arg_a[] = $this->parseQuote($read,&$i); |
657 | break; |
658 | case '{': |
659 | $arg_a[] = $this->parseLiteral($read,&$i); |
660 | break; |
661 | case '(': |
662 | $arg_a[] = $this->parseProperties($read,&$i); |
663 | break; |
664 | default: |
665 | break; |
666 | } |
667 | $i++; |
e02d0fa7 |
668 | } |
319cf0d2 |
669 | if (isset($arg_a[0])) { |
670 | $disp = new disposition($arg_a[0]); |
671 | if (isset($arg_a[1])) { |
672 | $disp->properties = $arg_a[1]; |
673 | } |
674 | } |
675 | if (is_object($disp)) return $disp; |
8f6b86cc |
676 | } |
319cf0d2 |
677 | |
8f6b86cc |
678 | function parseLanguage($read,&$i) { |
319cf0d2 |
679 | /* no idea how to process this one without examples */ |
680 | return ''; |
8f6b86cc |
681 | } |
319cf0d2 |
682 | |
8f6b86cc |
683 | function parseParenthesis($read,&$i) { |
684 | while ($read{$i} != ')') { |
319cf0d2 |
685 | switch ($read{$i}) { |
686 | case '"': |
687 | $this->parseQuote($read,&$i); |
688 | break; |
689 | case '{': |
690 | $this->parseLiteral($read,&$i); |
691 | break; |
692 | case '(': |
693 | $this->parseParenthesis($read,&$i); |
694 | break; |
695 | default: |
696 | break; |
697 | } |
698 | $i++; |
699 | } |
700 | } |
701 | |
702 | function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html')) { |
703 | |
704 | $found = false; |
705 | $type = $this->type0.'/'.$this->type1; |
706 | if ( $type == 'multipart/alternative') { |
707 | $msg = $this->findAlternativeEntity($alt_order); |
708 | if (count($msg->entities) == 0) { |
709 | $entity[] = $msg->entity_id; |
710 | } else { |
711 | $msg->findDisplayEntity(&$entity, $alt_order); |
712 | } |
713 | $found = true; |
714 | } else if ( $type == 'multipart/related') { |
715 | $msgs = $this->findRelatedEntity(); |
716 | for ($i = 0; $i < count($msgs); $i++) { |
717 | $msg = $msgs[$i]; |
718 | if (count($msg->entities) == 0) { |
719 | $entity[] = $msg->entity_id; |
720 | } else { |
721 | $msg->findDisplayEntity(&$entity,$alt_order); |
722 | } |
723 | $found = true; |
724 | } |
725 | } else if ( $this->type0 == 'text' && |
726 | ( $this->type1 == 'plain' || |
727 | $this->type1 == 'html' ) && |
728 | isset($this->entity_id) ) { |
729 | if (count($this->entities) == 0) { |
730 | if (!$this->header->disposition->name == 'attachment') { |
731 | $entity[] = $this->entity_id; |
732 | } |
733 | } |
734 | } |
735 | $i = 0; |
736 | while ( isset($this->entities[$i]) && !$found && |
737 | !($this->entities[$i]->header->disposition->name |
738 | == 'attachment') && |
739 | !($this->entities[$i]->type0 == 'message' && |
740 | $this->entities[$i]->type1 == 'rfc822' ) |
741 | ) |
742 | { |
743 | $this->entities[$i]->findDisplayEntity(&$entity, $alt_order); |
744 | $i++; |
745 | } |
746 | |
747 | if ( !isset($entity[0]) ) { |
748 | $entity[]=""; |
0fee68d1 |
749 | } |
319cf0d2 |
750 | return( $entity ); |
8f6b86cc |
751 | } |
319cf0d2 |
752 | |
753 | function findAlternativeEntity ($alt_order) { |
754 | /* if we are dealing with alternative parts then we choose the best |
755 | * viewable message supported by SM. |
756 | */ |
757 | $best_view = 0; |
758 | $ent_id = 0; |
759 | $k = 0; |
760 | for ($i = 0; $i < count($this->entities); $i ++) { |
761 | $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1; |
762 | if ($type == 'multipart/related') { |
763 | $type = $this->entities[$i]->header->type; |
764 | } |
765 | for ($j = $k; $j < count($alt_order); $j++) { |
766 | if ($alt_order[$j] == $type && $j > $best_view) { |
767 | $best_view = $j; |
768 | $ent_id = $i; |
769 | $k = $j; |
770 | } |
771 | } |
772 | } |
773 | return $this->entities[$ent_id]; |
774 | } |
775 | |
776 | function findRelatedEntity () { |
777 | $msgs = array(); |
778 | for ($i = 0; $i < count($this->entities); $i ++) { |
779 | $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1; |
780 | if ($this->header->type == $type) { |
781 | $msgs[] = $this->entities[$i]; |
782 | } |
783 | } |
784 | return $msgs; |
785 | } |
786 | |
787 | function getAttachments($exclude_id=array(), $result = array()) { |
788 | if ($this->type0 == 'message' && $this->type1 == 'rfc822') { |
789 | $this = $this->entities[0]; |
790 | } |
791 | if (count($this->entities)) { |
792 | foreach ($this->entities as $entity) { |
793 | $exclude = false; |
794 | foreach ($exclude_id as $excl) { |
795 | if ($entity->entity_id == $excl) { |
796 | $exclude = true; |
797 | } |
798 | } |
799 | if (!$exclude) { |
800 | if ($entity->type0 == 'multipart' && $entity->type1 == 'digest') { |
801 | $result = $entity->getAttachments($exclude_id, $result); |
802 | } else if ($entity->type0 != 'multipart') { |
803 | $result[] = $entity; |
804 | } |
805 | } |
806 | } |
807 | } else { |
808 | $exclude = false; |
809 | foreach ($exclude_id as $excl) { |
810 | if ($this->entity_id == $excl) { |
811 | $exclude = true; |
812 | } |
813 | } |
814 | if (!$exclude) { |
815 | $result[] = $this; |
816 | } |
817 | } |
818 | return $result; |
819 | } |
820 | |
8f6b86cc |
821 | } |
822 | |
823 | class disposition { |
824 | function disposition($name) { |
825 | $this->name = $name; |
826 | $this->properties = array(); |
827 | } |
828 | } |
829 | |
830 | ?> |