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 | } |
08c18186 |
195 | if ($msg->type0 == 'message' && $msg->type1 == 'rfc822') { |
319cf0d2 |
196 | /*this is a header for a message/rfc822 entity */ |
197 | $msg = $msg->entities[0]; |
198 | } |
199 | } |
08c18186 |
200 | |
201 | if ($msg->type0 == 'message' && $msg->type1 == 'rfc822') { |
319cf0d2 |
202 | /*this is a header for a message/rfc822 entity */ |
203 | $msg = $msg->entities[0]; |
204 | } |
8f6b86cc |
205 | |
319cf0d2 |
206 | if (isset($msg->entities[($ent_a[$cnt-1])-1])) { |
207 | $msg = $msg->entities[($ent_a[$cnt-1]-1)]; |
8f6b86cc |
208 | } |
08c18186 |
209 | |
319cf0d2 |
210 | return $msg; |
8f6b86cc |
211 | } |
319cf0d2 |
212 | |
213 | function getMailbox() { |
214 | $msg = $this; |
215 | while (is_object($msg->parent)) { |
216 | $msg = $msg->parent; |
217 | } |
218 | return $msg->mailbox; |
219 | } |
220 | |
8f6b86cc |
221 | /* |
222 | * Bodystructure parser, a recursive function for generating the |
223 | * entity-tree with all the mime-parts. |
224 | * |
225 | * It follows RFC2060 and stores all the described fields in the |
226 | * message object. |
227 | * |
228 | * Question/Bugs: |
229 | * |
230 | * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net. |
231 | * |
232 | */ |
08c18186 |
233 | function &parseStructure($read, $i=0, $message = false) { |
319cf0d2 |
234 | $arg_no = 0; |
235 | $arg_a = array(); |
236 | $cnt = strlen($read); |
237 | while ($i < $cnt) { |
238 | $char = strtoupper($read{$i}); |
239 | switch ($char) { |
240 | case '(': |
241 | if ($arg_no == 0 ) { |
242 | if (!isset($msg)) { |
243 | $msg = new message(); |
244 | $hdr = new msg_header(); |
245 | $hdr->type0 = 'text'; |
246 | $hdr->type1 = 'plain'; |
247 | $hdr->encoding = 'us-ascii'; |
248 | |
249 | if ($this->type0 == 'message' && $this->type1 == 'rfc822') { |
250 | $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */ |
251 | } else if (isset($this->entity_id) && $this->entity_id !='') { |
252 | $ent_no = count($this->entities)+1; |
253 | $par_ent = substr($this->entity_id,-2); |
254 | if ($par_ent{0} == '.') { |
255 | $par_ent = $par_ent{1}; |
256 | } |
257 | if ($par_ent == '0') { |
258 | $ent_no = count($this->entities)+1; |
259 | if ($ent_no > 0) { |
260 | $ent = substr($this->entity_id,0,strrpos($this->entity_id,'.')); |
261 | if ($ent) { |
262 | $ent = $ent . ".$ent_no"; |
263 | } else { |
264 | $ent = $ent_no; |
265 | } |
266 | $msg->entity_id = $ent; |
267 | } else { |
268 | $msg->entity_id = $ent_no; |
269 | } |
270 | } else { |
271 | $ent = $this->entity_id . ".$ent_no"; |
272 | $msg->entity_id = $ent; |
273 | } |
274 | } else { |
275 | $msg->entity_id = '0'; |
276 | } |
277 | } else { |
278 | $msg->header->type0 = 'multipart'; |
279 | $msg->type0 = 'multipart'; |
280 | while ($read{$i} == '(') { |
281 | $msg->addEntity($msg->parseStructure($read,&$i)); |
282 | } |
283 | } |
284 | } else { |
285 | switch ($arg_no) { |
286 | case 1: |
287 | /* multipart properties */ |
288 | $i++; |
289 | $arg_a[] = $this->parseProperties($read,&$i); |
290 | $arg_no++; |
291 | break; |
292 | case 2: |
293 | if (isset($msg->type0) && $msg->type0 == 'multipart') { |
294 | $i++; |
295 | $arg_a[]= $msg->parseDisposition($read,&$i); |
296 | } else { /* properties */ |
297 | /* properties */ |
298 | $arg_a[] = $msg->parseProperties($read,&$i); |
299 | } |
300 | $arg_no++; |
301 | break; |
08c18186 |
302 | case 3: |
303 | if (isset($msg->type0) && $msg->type0 == 'multipart') { |
304 | $i++; |
305 | $arg_a[]= $msg->parseLanguage($read,&$i); |
306 | } |
319cf0d2 |
307 | case 7: |
308 | if ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822') { |
2c761b52 |
309 | |
319cf0d2 |
310 | $msg->header->type0 = $arg_a[0]; |
311 | $msg->type0 = $arg_a[0]; |
8f6b86cc |
312 | |
319cf0d2 |
313 | $msg->header->type1 = $arg_a[1]; |
314 | $msg->type1 = $arg_a[1]; |
315 | |
316 | $msg->parseEnvelope($read,&$i,&$hdr); |
317 | $i++; |
318 | while ($i < $cnt && $read{$i} != '(') { |
319 | $i++; |
320 | } |
321 | $msg->addEntity($msg->parseStructure($read,&$i)); |
322 | } |
323 | break; |
324 | case 8: |
325 | $i++; |
326 | $arg_a[] = $msg->parseDisposition($read,&$i); |
327 | $arg_no++; |
328 | break; |
329 | case 9: |
330 | if ($arg_a[0] == 'text' || |
331 | ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) { |
332 | $i++; |
333 | $arg_a[] = $msg->parseDisposition($read,&$i); |
334 | } else { |
335 | $arg_a[] = $msg->parseLanguage($read,&$i); |
336 | } |
337 | $arg_no++; |
338 | break; |
339 | case 10: |
340 | if ($arg_a[0] == 'text' || |
341 | ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) { |
342 | $arg_a[] = $msg->parseLanguage($read,&$i); |
343 | } else { |
344 | $arg_a[] = ''; /* not yet desribed in rfc2060 */ |
345 | } |
346 | $arg_no++; |
347 | break; |
348 | default: |
349 | /* unknown argument, skip this part */ |
350 | $msg->parseParenthesis($read,&$i); |
351 | $arg_a[] = ''; |
352 | $arg_no++; |
353 | break; |
354 | } /* switch */ |
355 | } |
356 | break; |
357 | case '"': |
358 | /* inside an entity -> start processing */ |
359 | $debug = substr($read,$i,20); |
360 | $arg_s = $msg->parseQuote($read,&$i); |
361 | $arg_no++; |
362 | if ($arg_no < 3) $arg_s = strtolower($arg_s); /* type0 and type1 */ |
363 | $arg_a[] = $arg_s; |
364 | break; |
365 | case 'N': |
366 | /* probably NIL argument */ |
08c18186 |
367 | if (strtoupper(substr($read,$i,4)) == 'NIL ' || |
368 | strtoupper(substr($read,$i,4)) == 'NIL)') { |
319cf0d2 |
369 | $arg_a[] = ''; |
370 | $arg_no++; |
371 | $i = $i+2; |
372 | } |
373 | break; |
374 | case '{': |
375 | /* process the literal value */ |
376 | $arg_a[] = $msg->parseLiteral($read,&$i); |
377 | $arg_no++; |
378 | break; |
379 | case (is_numeric($read{$i}) ): |
380 | /* process integers */ |
381 | if ($read{$i} == ' ') break; |
382 | $arg_s = $read{$i};; |
383 | $i++; |
384 | while (preg_match('/\d+/',$read{$i})) { // != ' ') { |
385 | $arg_s .= $read{$i}; |
386 | $i++; |
387 | } |
388 | $arg_no++; |
389 | $arg_a[] = $arg_s; |
390 | break; |
391 | case ')': |
392 | if (isset($msg->type0) && $msg->type0 == 'multipart') { |
393 | $multipart = true; |
394 | } else { |
395 | $multipart = false; |
396 | } |
397 | if (!$multipart) { |
398 | if ($arg_a[0] == 'text' || |
399 | ($arg_a[0] == 'message' && $arg_a[1] == 'rfc822')) { |
400 | $shifted_args = true; |
401 | } else { |
402 | $shifted_args = false; |
403 | } |
404 | $hdr->type0 = $arg_a[0]; |
405 | $hdr->type1 = $arg_a[1]; |
8f6b86cc |
406 | |
319cf0d2 |
407 | $msg->type0 = $arg_a[0]; |
408 | $msg->type1 = $arg_a[1]; |
8f6b86cc |
409 | |
319cf0d2 |
410 | $arr = $arg_a[2]; |
411 | if (is_array($arr)) { |
412 | foreach($arr as $name => $value) { |
413 | $hdr->{$name} = $value; |
414 | } |
415 | } |
416 | $hdr->id = str_replace( '<', '', str_replace( '>', '', $arg_a[3] ) ); |
417 | $hdr->description = $arg_a[4]; |
418 | $hdr->encoding = strtolower($arg_a[5]); |
419 | $hdr->entity_id = $msg->entity_id; |
420 | $hdr->size = $arg_a[6]; |
421 | if ($shifted_args) { |
422 | $hdr->lines = $arg_a[7]; |
423 | if (isset($arg_a[8])) { |
424 | $hdr->md5 = $arg_a[8]; |
425 | } |
426 | if (isset($arg_a[9])) { |
427 | $hdr->disposition = $arg_a[9]; |
428 | } |
429 | if (isset($arg_a[10])) { |
430 | $hdr->language = $arg_a[10]; |
431 | } |
432 | } else { |
433 | if (isset($arg_a[7])) { |
434 | $hdr->md5 = $arg_a[7]; |
435 | } |
436 | if (isset($arg_a[8])) { |
437 | $hdr->disposition = $arg_a[8]; |
438 | } |
439 | if (isset($arg_a[9])) { |
440 | $hdr->language = $arg_a[9]; |
441 | } |
442 | } |
443 | $msg->header = $hdr; |
444 | $arg_no = 0; |
445 | $i++; |
446 | if (substr($msg->entity_id,-2) == '.0' && $msg->type0 !='multipart') { |
447 | $msg->entity_id++; |
448 | } |
449 | return $msg; |
450 | } else { |
451 | $hdr->type0 = 'multipart'; |
452 | $hdr->type1 = $arg_a[0]; |
0fee68d1 |
453 | |
319cf0d2 |
454 | $msg->type0 = 'multipart'; |
455 | $msg->type1 = $arg_a[0]; |
456 | if (isset($arg_a[1])) { |
457 | $arr = $arg_a[1]; |
458 | if (is_array($arr)) { |
459 | foreach($arr as $name => $value) { |
460 | $hdr->{$name} = $value; |
461 | } |
462 | } |
463 | } |
464 | if (isset($arg_a[2])) { |
465 | $hdr->disposition = $arg_a[2]; |
466 | } |
467 | if (isset($arg_a[3])) { |
468 | $hdr->language = $arg_a[3]; |
469 | } |
470 | $msg->header = $hdr; |
471 | return $msg; |
472 | } |
473 | default: |
474 | break; |
475 | } /* switch */ |
476 | $i++; |
477 | } /* while */ |
478 | } /* parsestructure */ |
479 | |
8f6b86cc |
480 | function parseProperties($read, $i) { |
319cf0d2 |
481 | $properties = array(); |
482 | $arg_s = ''; |
483 | $prop_name = ''; |
484 | while ($read{$i} != ')') { |
485 | if ($read{$i} == '"') { |
486 | $arg_s = $this->parseQuote($read,&$i); |
487 | } else if ($read{$i} == '{') { |
488 | $arg_s = $this->parseLiteral($read,&$i); |
489 | } |
490 | if ($prop_name == '' && $arg_s) { |
491 | $prop_name = strtolower($arg_s); |
492 | $properties[$prop_name] = ''; |
493 | $arg_s = ''; |
494 | } elseif ($prop_name != '' && $arg_s != '') { |
495 | $properties[$prop_name] = $arg_s; |
496 | $prop_name = ''; |
497 | $arg_s = ''; |
498 | } |
499 | $i++; |
500 | } |
501 | return $properties; |
8f6b86cc |
502 | } |
503 | |
504 | function parseEnvelope($read, $i, $hdr) { |
505 | $arg_no = 0; |
319cf0d2 |
506 | $arg_a = array(); |
507 | $cnt = strlen($read); |
508 | while ($i< $cnt && $read{$i} != ')') { |
509 | $i++; |
510 | $char = strtoupper($read{$i}); |
511 | switch ($char) { |
512 | case '"': |
513 | $arg_a[] = $this->parseQuote($read,&$i); |
514 | $arg_no++; |
515 | break; |
516 | case '{': |
517 | $arg_a[] = $this->parseLiteral($read,&$i); |
518 | $arg_no++; |
519 | break; |
520 | case 'N': |
521 | /* probably NIL argument */ |
08c18186 |
522 | if (strtoupper(substr($read,$i,3)) == 'NIL') { |
319cf0d2 |
523 | $arg_a[] = ''; |
524 | $arg_no++; |
525 | $i = $i+2; |
526 | } |
527 | break; |
528 | case '(': |
529 | /* Address structure |
530 | * With group support. |
531 | * Note: Group support is useless on SMTP connections |
532 | * because the protocol doesn't support it |
533 | */ |
534 | $addr_a = array(); |
535 | $group = ''; |
536 | $a=0; |
537 | while ($i < $cnt && $read{$i} != ')') { |
538 | if ($read{$i} == '(') { |
539 | $addr = $this->parseAddress($read,&$i); |
540 | if ($addr->host == '' && $addr->mailbox != '') { |
541 | /* start of group */ |
542 | $group = $addr->mailbox; |
543 | $group_addr = $addr; |
544 | $j = $a; |
545 | } elseif ($group && $addr->host == '' && $addr->mailbox == '') { |
546 | /* end group */ |
547 | if ($a == $j+1) { /* no group members */ |
548 | $group_addr->group = $group; |
549 | $group_addr->mailbox = ''; |
550 | $group_addr->personal = "$group: Undisclosed recipients;"; |
551 | $addr_a[] = $group_addr; |
552 | $group =''; |
553 | } |
554 | } else { |
555 | $addr->group = $group; |
556 | $addr_a[] = $addr; |
557 | } |
558 | $a++; |
559 | } |
560 | $i++; |
561 | } |
562 | $arg_a[] = $addr_a; |
563 | break; |
564 | default: |
565 | break; |
566 | } |
567 | $i++; |
568 | } |
569 | if (count($arg_a) > 9) { |
570 | /* argument 1: date */ |
8f6b86cc |
571 | $d = strtr($arg_a[0], array(' ' => ' ')); |
572 | $d = explode(' ', $d); |
573 | $hdr->date = getTimeStamp($d); |
319cf0d2 |
574 | /* argument 2: subject */ |
575 | if (!trim($arg_a[1])) { |
576 | $arg_a[1]= _("(no subject)"); |
577 | } |
578 | $hdr->subject = $arg_a[1]; |
579 | /* argument 3: from */ |
580 | $hdr->from = $arg_a[2][0]; |
581 | /* argument 4: sender */ |
582 | $hdr->sender = $arg_a[3][0]; |
583 | /* argument 5: reply-to */ |
584 | $hdr->replyto = $arg_a[4][0]; |
585 | /* argument 6: to */ |
586 | $hdr->to = $arg_a[5]; |
587 | /* argument 7: cc */ |
588 | $hdr->cc = $arg_a[6]; |
589 | /* argument 8: bcc */ |
590 | $hdr->bcc = $arg_a[7]; |
591 | /* argument 9: in-reply-to */ |
592 | $hdr->inreplyto = $arg_a[8]; |
593 | /* argument 10: message-id */ |
594 | $hdr->message_id = $arg_a[9]; |
08c18186 |
595 | } |
8f6b86cc |
596 | } |
319cf0d2 |
597 | |
8f6b86cc |
598 | function parseLiteral($read, $i) { |
319cf0d2 |
599 | $lit_cnt = ''; |
600 | $i++; |
601 | while ($read{$i} != '}') { |
602 | $lit_cnt .= $read{$i}; |
603 | $i++; |
604 | } |
605 | $lit_cnt +=2; /* add the { and } characters */ |
606 | $s = ''; |
607 | for ($j = 0; $j < $lit_cnt; $j++) { |
608 | $i++; |
609 | $s .= $read{$i}; |
610 | } |
611 | return $s; |
8f6b86cc |
612 | } |
319cf0d2 |
613 | |
8f6b86cc |
614 | function parseQuote($read, $i) { |
319cf0d2 |
615 | $i++; |
616 | $s = ''; |
08c18186 |
617 | while ($read{$i} != '"') { |
319cf0d2 |
618 | if ($read{$i} == '\\') { |
619 | $i++; |
620 | } |
621 | $s .= $read{$i}; |
622 | $i++; |
623 | } |
8f6b86cc |
624 | return $s; |
625 | } |
319cf0d2 |
626 | |
8f6b86cc |
627 | function parseAddress($read, $i) { |
319cf0d2 |
628 | $arg_a = array(); |
08c18186 |
629 | while ($read{$i} != ')' ) { //&& $i < count($read)) { |
319cf0d2 |
630 | $char = strtoupper($read{$i}); |
631 | switch ($char) { |
632 | case '"': |
633 | $arg_a[] = $this->parseQuote($read,&$i); |
634 | break; |
635 | case '{': |
636 | $arg_a[] = $this->parseLiteral($read,&$i); |
637 | break; |
638 | case 'N': |
639 | if (strtolower(substr($read,$i,3)) == 'nil') { |
640 | $arg_a[] = ''; |
641 | $i = $i+2; |
642 | } |
08c18186 |
643 | break; |
319cf0d2 |
644 | default: |
645 | break; |
646 | } |
647 | $i++; |
648 | } |
649 | if (count($arg_a) == 4) { |
650 | $adr = new address_structure(); |
651 | $adr->personal = $arg_a[0]; |
652 | $adr->adl = $arg_a[1]; |
653 | $adr->mailbox = $arg_a[2]; |
654 | $adr->host = $arg_a[3]; |
08c18186 |
655 | } else { |
656 | $adr = ''; |
657 | } |
319cf0d2 |
658 | return $adr; |
8f6b86cc |
659 | } |
319cf0d2 |
660 | |
08c18186 |
661 | function parseDisposition($read,&$i) { |
8f6b86cc |
662 | $arg_a = array(); |
663 | while ($read{$i} != ')') { |
319cf0d2 |
664 | switch ($read{$i}) { |
665 | case '"': |
666 | $arg_a[] = $this->parseQuote($read,&$i); |
667 | break; |
668 | case '{': |
669 | $arg_a[] = $this->parseLiteral($read,&$i); |
670 | break; |
671 | case '(': |
672 | $arg_a[] = $this->parseProperties($read,&$i); |
673 | break; |
674 | default: |
675 | break; |
676 | } |
677 | $i++; |
e02d0fa7 |
678 | } |
319cf0d2 |
679 | if (isset($arg_a[0])) { |
680 | $disp = new disposition($arg_a[0]); |
681 | if (isset($arg_a[1])) { |
682 | $disp->properties = $arg_a[1]; |
683 | } |
684 | } |
08c18186 |
685 | if (is_object($disp)) { |
686 | return $disp; |
687 | } |
8f6b86cc |
688 | } |
319cf0d2 |
689 | |
8f6b86cc |
690 | function parseLanguage($read,&$i) { |
08c18186 |
691 | /* no idea how to process this one without examples */ |
692 | $arg = ''; |
693 | while ($read{$i} != ')') { |
694 | switch ($read{$i}) { |
695 | case '"': |
696 | $arg = $this->parseQuote($read,&$i); |
697 | break; |
698 | case '{': |
699 | $arg = $this->parseLiteral($read,&$i); |
700 | break; |
701 | case '(': |
702 | $arg = $this->parseProperties($read,&$i); |
703 | break; |
704 | default: |
705 | break; |
706 | } |
707 | $i++; |
708 | } |
709 | return $arg;; |
8f6b86cc |
710 | } |
319cf0d2 |
711 | |
8f6b86cc |
712 | function parseParenthesis($read,&$i) { |
713 | while ($read{$i} != ')') { |
319cf0d2 |
714 | switch ($read{$i}) { |
715 | case '"': |
716 | $this->parseQuote($read,&$i); |
717 | break; |
718 | case '{': |
719 | $this->parseLiteral($read,&$i); |
720 | break; |
721 | case '(': |
722 | $this->parseParenthesis($read,&$i); |
723 | break; |
724 | default: |
725 | break; |
726 | } |
727 | $i++; |
728 | } |
729 | } |
730 | |
731 | function findDisplayEntity ($entity = array(), $alt_order = array('text/plain','text/html')) { |
319cf0d2 |
732 | $found = false; |
733 | $type = $this->type0.'/'.$this->type1; |
734 | if ( $type == 'multipart/alternative') { |
735 | $msg = $this->findAlternativeEntity($alt_order); |
736 | if (count($msg->entities) == 0) { |
737 | $entity[] = $msg->entity_id; |
738 | } else { |
739 | $msg->findDisplayEntity(&$entity, $alt_order); |
740 | } |
741 | $found = true; |
742 | } else if ( $type == 'multipart/related') { |
743 | $msgs = $this->findRelatedEntity(); |
744 | for ($i = 0; $i < count($msgs); $i++) { |
745 | $msg = $msgs[$i]; |
746 | if (count($msg->entities) == 0) { |
747 | $entity[] = $msg->entity_id; |
748 | } else { |
749 | $msg->findDisplayEntity(&$entity,$alt_order); |
750 | } |
751 | $found = true; |
752 | } |
753 | } else if ( $this->type0 == 'text' && |
754 | ( $this->type1 == 'plain' || |
08c18186 |
755 | $this->type1 == 'html' || |
756 | $this->type1 == 'message') && |
319cf0d2 |
757 | isset($this->entity_id) ) { |
758 | if (count($this->entities) == 0) { |
759 | if (!$this->header->disposition->name == 'attachment') { |
760 | $entity[] = $this->entity_id; |
761 | } |
762 | } |
763 | } |
764 | $i = 0; |
765 | while ( isset($this->entities[$i]) && !$found && |
766 | !($this->entities[$i]->header->disposition->name |
767 | == 'attachment') && |
768 | !($this->entities[$i]->type0 == 'message' && |
769 | $this->entities[$i]->type1 == 'rfc822' ) |
770 | ) |
771 | { |
772 | $this->entities[$i]->findDisplayEntity(&$entity, $alt_order); |
773 | $i++; |
774 | } |
775 | |
776 | if ( !isset($entity[0]) ) { |
777 | $entity[]=""; |
0fee68d1 |
778 | } |
319cf0d2 |
779 | return( $entity ); |
8f6b86cc |
780 | } |
319cf0d2 |
781 | |
782 | function findAlternativeEntity ($alt_order) { |
783 | /* if we are dealing with alternative parts then we choose the best |
784 | * viewable message supported by SM. |
785 | */ |
786 | $best_view = 0; |
787 | $ent_id = 0; |
788 | $k = 0; |
789 | for ($i = 0; $i < count($this->entities); $i ++) { |
790 | $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1; |
791 | if ($type == 'multipart/related') { |
792 | $type = $this->entities[$i]->header->type; |
793 | } |
794 | for ($j = $k; $j < count($alt_order); $j++) { |
795 | if ($alt_order[$j] == $type && $j > $best_view) { |
796 | $best_view = $j; |
797 | $ent_id = $i; |
798 | $k = $j; |
799 | } |
800 | } |
801 | } |
802 | return $this->entities[$ent_id]; |
803 | } |
804 | |
805 | function findRelatedEntity () { |
806 | $msgs = array(); |
807 | for ($i = 0; $i < count($this->entities); $i ++) { |
808 | $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1; |
809 | if ($this->header->type == $type) { |
810 | $msgs[] = $this->entities[$i]; |
811 | } |
812 | } |
813 | return $msgs; |
814 | } |
815 | |
816 | function getAttachments($exclude_id=array(), $result = array()) { |
817 | if ($this->type0 == 'message' && $this->type1 == 'rfc822') { |
818 | $this = $this->entities[0]; |
819 | } |
820 | if (count($this->entities)) { |
821 | foreach ($this->entities as $entity) { |
822 | $exclude = false; |
823 | foreach ($exclude_id as $excl) { |
824 | if ($entity->entity_id == $excl) { |
825 | $exclude = true; |
826 | } |
827 | } |
828 | if (!$exclude) { |
829 | if ($entity->type0 == 'multipart' && $entity->type1 == 'digest') { |
830 | $result = $entity->getAttachments($exclude_id, $result); |
831 | } else if ($entity->type0 != 'multipart') { |
832 | $result[] = $entity; |
833 | } |
834 | } |
835 | } |
836 | } else { |
837 | $exclude = false; |
838 | foreach ($exclude_id as $excl) { |
839 | if ($this->entity_id == $excl) { |
840 | $exclude = true; |
841 | } |
842 | } |
843 | if (!$exclude) { |
844 | $result[] = $this; |
845 | } |
846 | } |
847 | return $result; |
848 | } |
849 | |
8f6b86cc |
850 | } |
851 | |
852 | class disposition { |
853 | function disposition($name) { |
854 | $this->name = $name; |
855 | $this->properties = array(); |
856 | } |
857 | } |
858 | |
859 | ?> |