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; |
2bf8f74a |
212 | $arg_a[]= $msg->parseLanguage($read, $i); |
19d470aa |
213 | } |
214 | case 7: |
215 | if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) { |
216 | $msg->header->type0 = $arg_a[0]; |
217 | $msg->header->type1 = $arg_a[1]; |
218 | $msg->type0 = $arg_a[0]; |
219 | $msg->type1 = $arg_a[1]; |
220 | $rfc822_hdr = new Rfc822Header(); |
2bf8f74a |
221 | $msg->rfc822_header = $msg->parseEnvelope($read, $i, $rfc822_hdr); |
19d470aa |
222 | while (($i < $cnt) && ($read{$i} != '(')) { |
223 | ++$i; |
224 | } |
225 | $res = $msg->parseStructure($read, $i); |
226 | $i = $res[1]; |
227 | $msg->addEntity($res[0]); |
228 | } |
229 | break; |
230 | case 8: |
231 | ++$i; |
21a50099 |
232 | $arg_a[] = $msg->parseDisposition($read, $i); |
19d470aa |
233 | ++$arg_no; |
234 | break; |
235 | case 9: |
236 | ++$i; |
237 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
21a50099 |
238 | $arg_a[] = $msg->parseDisposition($read, $i); |
19d470aa |
239 | } else { |
21a50099 |
240 | $arg_a[] = $msg->parseLanguage($read, $i); |
19d470aa |
241 | } |
19d470aa |
242 | ++$arg_no; |
243 | break; |
244 | case 10: |
245 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
246 | ++$i; |
21a50099 |
247 | $arg_a[] = $msg->parseLanguage($read, $i); |
19d470aa |
248 | } else { |
249 | $i = $msg->parseParenthesis($read, $i); |
250 | $arg_a[] = ''; /* not yet described in rfc2060 */ |
251 | } |
252 | ++$arg_no; |
253 | break; |
254 | default: |
255 | /* unknown argument, skip this part */ |
256 | $i = $msg->parseParenthesis($read, $i); |
257 | $arg_a[] = ''; |
258 | ++$arg_no; |
259 | break; |
260 | } /* switch */ |
261 | break; |
262 | case '"': |
263 | /* inside an entity -> start processing */ |
264 | $debug = substr($read, $i, 20); |
21a50099 |
265 | $arg_s = $msg->parseQuote($read, $i); |
19d470aa |
266 | ++$arg_no; |
267 | if ($arg_no < 3) { |
268 | $arg_s = strtolower($arg_s); /* type0 and type1 */ |
269 | } |
270 | $arg_a[] = $arg_s; |
271 | break; |
272 | case 'n': |
273 | case 'N': |
274 | /* probably NIL argument */ |
275 | if (strtoupper(substr($read, $i, 4)) == 'NIL ') { |
276 | $arg_a[] = ''; |
277 | ++$arg_no; |
278 | $i += 2; |
279 | } |
280 | break; |
281 | case '{': |
282 | /* process the literal value */ |
21a50099 |
283 | $arg_s = $msg->parseLiteral($read, $i); |
19d470aa |
284 | ++$arg_no; |
285 | break; |
286 | case is_numeric($read{$i}): |
287 | /* process integers */ |
288 | if ($read{$i} == ' ') { break; } |
289 | $arg_s = $read{$i};; |
290 | for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) { |
291 | $arg_s .= $read{$i}; |
292 | } |
293 | ++$arg_no; |
294 | $arg_a[] = $arg_s; |
295 | break; |
296 | case ')': |
297 | $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart')); |
298 | if (!$multipart) { |
299 | $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))); |
300 | $hdr->type0 = $arg_a[0]; |
301 | $hdr->type1 = $arg_a[1]; |
302 | |
303 | $msg->type0 = $arg_a[0]; |
304 | $msg->type1 = $arg_a[1]; |
305 | $arr = $arg_a[2]; |
306 | if (is_array($arr)) { |
307 | $hdr->parameters = $arg_a[2]; |
308 | } |
309 | $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3])); |
310 | $hdr->description = $arg_a[4]; |
311 | $hdr->encoding = strtolower($arg_a[5]); |
312 | $hdr->entity_id = $msg->entity_id; |
313 | $hdr->size = $arg_a[6]; |
314 | if ($shifted_args) { |
315 | $hdr->lines = $arg_a[7]; |
316 | $s = 1; |
317 | } else { |
318 | $s = 0; |
319 | } |
320 | $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5); |
321 | $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition); |
322 | $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language); |
323 | $msg->header = $hdr; |
324 | if ((strrchr($msg->entity_id, '.') == '.0') && ($msg->type0 !='multipart')) { |
325 | $msg->entity_id = $this->entity_id . '.1'; |
326 | } |
327 | } else { |
328 | $hdr->type0 = 'multipart'; |
329 | $hdr->type1 = $arg_a[0]; |
330 | $msg->type0 = 'multipart'; |
331 | $msg->type1 = $arg_a[0]; |
332 | $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters); |
333 | $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition); |
334 | $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language); |
335 | $msg->header = $hdr; |
336 | } |
337 | ++$i; |
338 | return (array($msg, $i)); |
339 | default: break; |
340 | } /* switch */ |
341 | |
342 | } /* for */ |
343 | } /* parsestructure */ |
344 | |
21a50099 |
345 | function parseProperties($read, &$i) { |
19d470aa |
346 | $properties = array(); |
347 | $prop_name = ''; |
348 | |
349 | for (; $read{$i} != ')'; ++$i) { |
350 | $arg_s = ''; |
351 | if ($read{$i} == '"') { |
21a50099 |
352 | $arg_s = $this->parseQuote($read, $i); |
19d470aa |
353 | } else if ($read{$i} == '{') { |
21a50099 |
354 | $arg_s = $this->parseLiteral($read, $i); |
19d470aa |
355 | } |
356 | |
357 | if ($arg_s != '') { |
358 | if ($prop_name == '') { |
359 | $prop_name = strtolower($arg_s); |
360 | $properties[$prop_name] = ''; |
361 | } else if ($prop_name != '') { |
362 | $properties[$prop_name] = $arg_s; |
363 | $prop_name = ''; |
364 | } |
365 | } |
366 | } |
21a50099 |
367 | return $properties; |
19d470aa |
368 | } |
369 | |
2bf8f74a |
370 | function parseEnvelope($read, &$i, $hdr) { |
19d470aa |
371 | $arg_no = 0; |
372 | $arg_a = array(); |
373 | |
374 | for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) { |
375 | ++$i; |
376 | $char = strtoupper($read{$i}); |
377 | switch ($char) { |
378 | case '"': |
21a50099 |
379 | $arg_a[] = $this->parseQuote($read, $i); |
19d470aa |
380 | ++$arg_no; |
381 | break; |
382 | case '{': |
21a50099 |
383 | $arg_a[] = $this->parseLiteral($read, $i); |
19d470aa |
384 | ++$arg_no; |
385 | break; |
386 | case 'N': |
387 | /* probably NIL argument */ |
388 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
389 | $arg_a[] = ''; |
390 | ++$arg_no; |
391 | $i += 2; |
392 | } |
393 | break; |
394 | case '(': |
395 | /* Address structure (with group support) |
396 | * Note: Group support is useless on SMTP connections |
397 | * because the protocol doesn't support it |
398 | */ |
399 | $addr_a = array(); |
400 | $group = ''; |
401 | $a=0; |
402 | for (; $i < $cnt && $read{$i} != ')'; ++$i) { |
403 | if ($read{$i} == '(') { |
2bf8f74a |
404 | $addr = $this->parseAddress($read, $i); |
19d470aa |
405 | if (($addr->host == '') && ($addr->mailbox != '')) { |
406 | /* start of group */ |
407 | $group = $addr->mailbox; |
408 | $group_addr = $addr; |
409 | $j = $a; |
410 | } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) { |
411 | /* end group */ |
412 | if ($a == ($j+1)) { /* no group members */ |
413 | $group_addr->group = $group; |
414 | $group_addr->mailbox = ''; |
415 | $group_addr->personal = "$group: Undisclosed recipients;"; |
416 | $addr_a[] = $group_addr; |
417 | $group =''; |
418 | } |
419 | } else { |
420 | $addr->group = $group; |
421 | $addr_a[] = $addr; |
422 | } |
423 | ++$a; |
424 | } |
425 | } |
426 | $arg_a[] = $addr_a; |
427 | break; |
428 | default: break; |
429 | } |
430 | } |
431 | |
432 | if (count($arg_a) > 9) { |
433 | /* argument 1: date */ |
434 | $d = strtr($arg_a[0], array(' ' => ' ')); |
435 | $d = explode(' ', $d); |
436 | $hdr->date = getTimeStamp($d); |
437 | |
438 | /* argument 2: subject */ |
439 | $arg_a[1] = (!trim($arg_a[1]) ? _("(no subject)") : $arg_a[1]); |
440 | $hdr->subject = $arg_a[1]; |
441 | |
442 | $hdr->from = $arg_a[2][0]; /* argument 3: from */ |
443 | $hdr->sender = $arg_a[3][0]; /* argument 4: sender */ |
444 | $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */ |
445 | $hdr->to = $arg_a[5]; /* argument 6: to */ |
446 | $hdr->cc = $arg_a[6]; /* argument 7: cc */ |
447 | $hdr->bcc = $arg_a[7]; /* argument 8: bcc */ |
448 | $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */ |
449 | $hdr->message_id = $arg_a[9]; /* argument 10: message-id */ |
450 | } |
2bf8f74a |
451 | return $hdr; |
19d470aa |
452 | } |
453 | |
21a50099 |
454 | function parseLiteral($read, &$i) { |
19d470aa |
455 | $lit_cnt = ''; |
456 | for (++$i; $read{$i} != '}'; ++$i) { |
457 | $lit_cnt .= $read{$i}; |
458 | } |
459 | |
460 | $lit_cnt +=2; /* add the { and } characters */ |
461 | $s = ''; |
462 | for ($j = 0; $j < $lit_cnt; ++$j) { |
463 | $s .= $read{++$i}; |
464 | } |
465 | return (array($s, $i)); |
466 | } |
467 | |
21a50099 |
468 | function parseQuote($read, &$i) { |
19d470aa |
469 | $s = ''; |
470 | for (++$i; $read{$i} != '"'; ++$i) { |
471 | if ($read{$i} == '\\') { |
472 | ++$i; |
473 | } |
474 | $s .= $read{$i}; |
475 | } |
21a50099 |
476 | return $s; |
19d470aa |
477 | } |
478 | |
2bf8f74a |
479 | function parseAddress($read, &$i) { |
19d470aa |
480 | $arg_a = array(); |
481 | |
482 | for (; $read{$i} != ')'; ++$i) { |
483 | $char = strtoupper($read{$i}); |
484 | switch ($char) { |
485 | case '"': |
486 | case '{': |
21a50099 |
487 | $arg_a[] = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i)); |
19d470aa |
488 | break; |
489 | case 'n': |
490 | case 'N': |
491 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
492 | $arg_a[] = ''; |
493 | $i += 2; |
494 | } |
495 | break; |
496 | default: break; |
497 | } |
498 | } |
499 | |
500 | if (count($arg_a) == 4) { |
501 | $adr = new AddressStructure(); |
502 | $adr->personal = $arg_a[0]; |
503 | $adr->adl = $arg_a[1]; |
504 | $adr->mailbox = $arg_a[2]; |
505 | $adr->host = $arg_a[3]; |
506 | } else { |
507 | $adr = ''; |
508 | } |
2bf8f74a |
509 | return $adr; |
19d470aa |
510 | } |
511 | |
21a50099 |
512 | function parseDisposition($read, &$i) { |
19d470aa |
513 | $arg_a = array(); |
19d470aa |
514 | for (; $read{$i} != ')'; ++$i) { |
515 | switch ($read{$i}) { |
21a50099 |
516 | case '"': $arg_a[] = $this->parseQuote($read, $i); break; |
517 | case '{': $arg_a[] = $this->parseLiteral($read, $i); break; |
518 | case '(': $arg_a[] = $this->parseProperties($read, $i); break; |
19d470aa |
519 | default: break; |
520 | } |
521 | } |
522 | |
523 | if (isset($arg_a[0])) { |
524 | $disp = new Disposition($arg_a[0]); |
525 | if (isset($arg_a[1])) { |
526 | $disp->properties = $arg_a[1]; |
527 | } |
528 | } |
529 | |
21a50099 |
530 | return (is_object($disp) ? $disp : ''); |
19d470aa |
531 | } |
532 | |
21a50099 |
533 | function parseLanguage($read, &$i) { |
19d470aa |
534 | /* no idea how to process this one without examples */ |
535 | $arg_a = array(); |
536 | |
537 | for (; $read{$i} != ')'; ++$i) { |
538 | switch ($read{$i}) { |
21a50099 |
539 | case '"': $arg_a[] = $this->parseQuote($read, $i); break; |
540 | case '{': $arg_a[] = $this->parseLiteral($read, $i); break; |
541 | case '(': $arg_a[] = $this->parseProperties($read, $i); break; |
19d470aa |
542 | default: break; |
543 | } |
544 | } |
545 | |
546 | if (isset($arg_a[0])) { |
547 | $lang = new Language($arg_a[0]); |
548 | if (isset($arg_a[1])) { |
549 | $lang->properties = $arg_a[1]; |
550 | } |
551 | } |
552 | |
21a50099 |
553 | return (is_object($lang) ? $lang : ''); |
19d470aa |
554 | } |
555 | |
556 | function parseParenthesis($read, $i) { |
557 | for (; $read{$i} != ')'; ++$i) { |
558 | switch ($read{$i}) { |
21a50099 |
559 | case '"': $this->parseQuote($read, $i); break; |
560 | case '{': $this->parseLiteral($read, $i); break; |
561 | case '(': $this->parseProperties($read, $i); break; |
19d470aa |
562 | default: break; |
563 | } |
564 | } |
565 | return $i; |
566 | } |
567 | |
568 | /* Function to fill the message structure in case the */ |
569 | /* bodystructure is not available NOT FINISHED YET */ |
570 | function parseMessage($read, $type0, $type1) { |
571 | switch ($type0) { |
572 | case 'message': |
573 | $rfc822_header = true; |
574 | $mime_header = false; |
575 | break; |
576 | case 'multipart': |
577 | $rfc822_header = false; |
578 | $mime_header = true; |
579 | break; |
580 | default: return $read; |
581 | } |
582 | |
583 | for ($i = 1; $i < $count; ++$i) { |
584 | $line = trim($body[$i]); |
585 | if (($mime_header || $rfc822_header) && |
586 | (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) { |
587 | $bnd = $reg[1]; |
588 | $bndreg = $bnd; |
589 | $bndreg = str_replace("\\", "\\\\", $bndreg); |
590 | $bndreg = str_replace("?", "\\?", $bndreg); |
591 | $bndreg = str_replace("+", "\\+", $bndreg); |
592 | $bndreg = str_replace(".", "\\.", $bndreg); |
593 | $bndreg = str_replace("/", "\\/", $bndreg); |
594 | $bndreg = str_replace("-", "\\-", $bndreg); |
595 | $bndreg = str_replace("(", "\\(", $bndreg); |
596 | $bndreg = str_replace(")", "\\)", $bndreg); |
597 | } else if ($rfc822_header && $line == '') { |
598 | $rfc822_header = false; |
599 | if ($msg->type0 == 'multipart') { |
600 | $mime_header = true; |
601 | } |
602 | } |
603 | |
604 | if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) { |
605 | $cnt = count($boundaries)-1; |
606 | $bnd = $boundaries[$cnt]['bnd']; |
607 | $bndreg = $boundaries[$cnt]['bndreg']; |
608 | |
609 | $regstr = '/^--'."($bndreg)".".*".'/'; |
610 | if (preg_match($regstr, $line, $reg)) { |
611 | $bndlen = strlen($reg[1]); |
612 | $bndend = false; |
613 | if (strlen($line) > ($bndlen + 3)) { |
614 | if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) { |
615 | $bndend = true; |
616 | } |
617 | } |
618 | if ($bndend) { |
619 | /* calc offset and return $msg */ |
620 | //$entStr = CalcEntity("$entStr", -1); |
621 | array_pop($boundaries); |
622 | $mime_header = true; |
623 | $bnd_end = true; |
624 | } else { |
625 | $mime_header = true; |
626 | $bnd_end = false; |
627 | //$entStr = CalcEntity("$entStr", 0); |
628 | ++$content_indx; |
629 | } |
630 | } else { |
631 | if ($header) { } |
632 | } |
633 | } |
634 | } |
635 | } |
636 | |
637 | function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html'), $strict=false) { |
638 | $found = false; |
639 | if ($this->type0 == 'multipart') { |
640 | if($this->type1 == 'alternative') { |
641 | $msg = $this->findAlternativeEntity($alt_order); |
642 | if (count($msg->entities) == 0) { |
643 | $entity[] = $msg->entity_id; |
644 | } else { |
645 | $entity = $msg->findDisplayEntity($entity, $alt_order, $strict); |
646 | } |
647 | $found = true; |
648 | } else if ($this->type1 == 'related') { /* RFC 2387 */ |
649 | $msgs = $this->findRelatedEntity(); |
650 | foreach ($msgs as $msg) { |
651 | if (count($msg->entities) == 0) { |
652 | $entity[] = $msg->entity_id; |
653 | } else { |
654 | $entity = $msg->findDisplayEntity($entity, $alt_order, $strict); |
655 | } |
656 | } |
657 | if (count($msgs) > 0) { |
658 | $found = true; |
659 | } |
660 | } else { /* Treat as multipart/mixed */ |
661 | foreach ($this->entities as $ent) { |
662 | if((strtolower($ent->header->disposition->name) != 'attachment') && |
663 | (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) { |
664 | $entity = $ent->findDisplayEntity($entity, $alt_order, $strict); |
665 | $found = true; |
666 | } |
667 | } |
668 | } |
669 | } else { /* If not multipart, then just compare with each entry from $alt_order */ |
670 | $type = $this->type0.'/'.$this->type1; |
671 | foreach ($alt_order as $alt) { |
672 | if( ($alt == $type) && isset($this->entity_id) ) { |
673 | if ((count($this->entities) == 0) && |
674 | (strtolower($this->header->disposition->name) != 'attachment')) { |
675 | $entity[] = $this->entity_id; |
676 | $found = true; |
677 | } |
678 | } |
679 | } |
680 | } |
681 | if(!$found) { |
682 | foreach ($this->entities as $ent) { |
683 | if((strtolower($ent->header->disposition->name) != 'attachment') && |
684 | (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) { |
685 | $entity = $ent->findDisplayEntity($entity, $alt_order, $strict); |
686 | $found = true; |
687 | } |
688 | } |
689 | } |
690 | if(!$strict && !$found) { |
691 | if (($this->type0 == 'text') && |
692 | in_array($this->type1, array('plain', 'html', 'message')) && |
693 | isset($this->entity_id)) { |
694 | if (count($this->entities) == 0) { |
695 | if (strtolower($this->header->disposition->name) != 'attachment') { |
696 | $entity[] = $this->entity_id; |
697 | } |
698 | } |
699 | } |
700 | } |
701 | |
702 | return $entity; |
703 | } |
704 | |
705 | function findAlternativeEntity($alt_order) { |
706 | /* If we are dealing with alternative parts then we */ |
707 | /* choose the best viewable message supported by SM. */ |
708 | $best_view = 0; |
709 | $entity = array(); |
710 | foreach($this->entities as $ent) { |
711 | $type = $ent->header->type0 . '/' . $ent->header->type1; |
712 | if ($type == 'multipart/related') { |
713 | $type = $ent->header->getParameter('type'); |
714 | } |
715 | $altCount = count($alt_order); |
716 | for ($j = $best_view; $j < $altCount; ++$j) { |
717 | if (($alt_order[$j] == $type) && ($j >= $best_view)) { |
718 | $best_view = $j; |
719 | $entity = $ent; |
720 | } |
721 | } |
722 | } |
723 | |
724 | return $entity; |
725 | } |
726 | |
727 | function findRelatedEntity() { |
728 | $msgs = array(); |
729 | |
730 | $entCount = count($this->entities); |
731 | for ($i = 0; $i < $entCount; ++$i) { |
732 | $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1; |
733 | if ($this->header->getParameter('type') == $type) { |
734 | $msgs[] = $this->entities[$i]; |
735 | } |
736 | } |
737 | |
738 | return $msgs; |
739 | } |
740 | |
741 | function getAttachments($exclude_id=array(), $result = array()) { |
742 | if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) { |
743 | $this = $this->entities[0]; |
744 | } |
745 | |
746 | if (count($this->entities)) { |
747 | foreach ($this->entities as $entity) { |
748 | $exclude = false; |
749 | |
750 | foreach ($exclude_id as $excl) { |
751 | if ($entity->entity_id === $excl) { |
752 | $exclude = true; |
753 | } |
754 | } |
755 | |
756 | if (!$exclude) { |
757 | if (($entity->type0 == 'multipart') && |
758 | ($entity->type1 != 'related')) { |
759 | $result = $entity->getAttachments($exclude_id, $result); |
760 | } else if ($entity->type0 != 'multipart') { |
761 | $result[] = $entity; |
762 | } |
763 | } |
764 | } |
765 | } else { |
766 | $exclude = false; |
767 | foreach ($exclude_id as $excl) { |
768 | $exclude = $exclude || ($this->entity_id == $excl); |
769 | } |
770 | |
771 | if (!$exclude) { |
772 | $result[] = $this; |
773 | } |
774 | } |
775 | |
776 | return $result; |
777 | } |
778 | } |
779 | |
780 | ?> |