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