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