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