c8f4f2cfb94f930b998b1e0c45518cce4dbd3c45
[squirrelmail.git] / functions / mime.php
1 <?php
2
3 /**
4 * mime.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains the functions necessary to detect and decode MIME
10 * messages.
11 *
12 * $Id$
13 */
14
15 require_once('../functions/imap.php');
16 require_once('../functions/attachment_common.php');
17
18 /** Setting up the objects that have the structure for the message **/
19 class msg_header {
20 /** msg_header contains generic variables for values that **/
21 /** could be in a header. **/
22
23 var $type0 = '', $type1 = '', $boundary = '', $charset = '';
24 var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
25 var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
26 var $id = 0, $mailbox = '', $description = '', $filename = '';
27 var $entity_id = 0, $message_id = 0, $name = '';
28 // var $priority = "";
29 }
30
31 class message {
32 /** message is the object that contains messages. It is a recursive
33 object in that through the $entities variable, it can contain
34 more objects of type message. See documentation in mime.txt for
35 a better description of how this works.
36 **/
37 var $header = '';
38 var $entities = array();
39
40 function addEntity ($msg) {
41 $this->entities[] = $msg;
42 }
43 }
44
45 /* --------------------------------------------------------------------------------- */
46 /* MIME DECODING */
47 /* --------------------------------------------------------------------------------- */
48
49 /* This function gets the structure of a message and stores it in the "message" class.
50 * It will return this object for use with all relevant header information and
51 * fully parsed into the standard "message" object format.
52 */
53 function mime_structure ($imap_stream, $header) {
54
55 sqimap_messages_flag ($imap_stream, $header->id, $header->id, 'Seen');
56 $ssid = sqimap_session_id();
57 $lsid = strlen( $ssid );
58 $id = $header->id;
59 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
60 //
61 // This should use sqimap_read_data instead of reading it itself
62 //
63 $read = fgets ($imap_stream, 10000);
64 $bodystructure = '';
65 while ( substr($read, 0, $lsid) <> $ssid &&
66 !feof( $imap_stream ) ) {
67 $bodystructure .= $read;
68 $read = fgets ($imap_stream, 10000);
69 }
70 $read = $bodystructure;
71
72 // isolate the body structure and remove beginning and end parenthesis
73 $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') + 13));
74 $read = trim(substr ($read, 0, -1));
75 $end = mime_match_parenthesis(0, $read);
76 while ($end == strlen($read)-1) {
77 $read = trim(substr ($read, 0, -1));
78 $read = trim(substr ($read, 1));
79 $end = mime_match_parenthesis(0, $read);
80 }
81
82 $msg = mime_parse_structure ($read, 0);
83 $msg->header = $header;
84
85 return( $msg );
86 }
87
88 /* this starts the parsing of a particular structure. It is called recursively,
89 * so it can be passed different structures. It returns an object of type
90 * $message.
91 * First, it checks to see if it is a multipart message. If it is, then it
92 * handles that as it sees is necessary. If it is just a regular entity,
93 * then it parses it and adds the necessary header information (by calling out
94 * to mime_get_elements()
95 */
96 function mime_parse_structure ($structure, $ent_id) {
97
98 $msg = new message();
99 if ($structure{0} == '(') {
100 $ent_id = mime_new_element_level($ent_id);
101 $start = $end = -1;
102 do {
103 $start = $end+1;
104 $end = mime_match_parenthesis ($start, $structure);
105
106 $element = substr($structure, $start+1, ($end - $start)-1);
107 $ent_id = mime_increment_id ($ent_id);
108 $newmsg = mime_parse_structure ($element, $ent_id);
109 $msg->addEntity ($newmsg);
110 } while ($structure{$end+1} == '(');
111 } else {
112 // parse the elements
113 $msg = mime_get_element ($structure, $msg, $ent_id);
114 }
115 return $msg;
116 }
117
118 /* Increments the element ID. An element id can look like any of
119 * the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
120 * the last number of the element id, changing 1.2 to 1.3.
121 */
122 function mime_increment_id ($id) {
123
124 if (strpos($id, ".")) {
125 $first = substr($id, 0, strrpos($id, "."));
126 $last = substr($id, strrpos($id, ".")+1);
127 $last++;
128 $new = $first . "." .$last;
129 } else {
130 $new = $id + 1;
131 }
132
133 return $new;
134 }
135
136 /*
137 * See comment for mime_increment_id().
138 * This adds another level on to the entity_id changing 1.3 to 1.3.0
139 * NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
140 * before it can be used. I left it this way so as not to have
141 * to make a special case if it is the first entity_id. It
142 * always increments it, and that works fine.
143 */
144 function mime_new_element_level ($id) {
145
146 if (!$id) {
147 $id = 0;
148 } else {
149 $id = $id . '.0';
150 }
151
152 return( $id );
153 }
154
155 function mime_get_element (&$structure, $msg, $ent_id) {
156
157 $elem_num = 1;
158 $msg->header = new msg_header();
159 $msg->header->entity_id = $ent_id;
160 $properties = array();
161
162 while (strlen($structure) > 0) {
163 $structure = trim($structure);
164 $char = $structure{0};
165
166 if (strtolower(substr($structure, 0, 3)) == 'nil') {
167 $text = '';
168 $structure = substr($structure, 3);
169 } else if ($char == '"') {
170 // loop through until we find the matching quote, and return that as a string
171 $pos = 1;
172 $text = '';
173 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
174 $text .= $char;
175 $pos++;
176 }
177 $structure = substr($structure, strlen($text) + 2);
178 } else if ($char == '(') {
179 // comment me
180 $end = mime_match_parenthesis (0, $structure);
181 $sub = substr($structure, 1, $end-1);
182 $properties = mime_get_props($properties, $sub);
183 $structure = substr($structure, strlen($sub) + 2);
184 } else {
185 // loop through until we find a space or an end parenthesis
186 $pos = 0;
187 $char = $structure{$pos};
188 $text = '';
189 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
190 $text .= $char;
191 $pos++;
192 $char = $structure{$pos};
193 }
194 $structure = substr($structure, strlen($text));
195 }
196
197 // This is where all the text parts get put into the header
198 switch ($elem_num) {
199 case 1:
200 $msg->header->type0 = strtolower($text);
201 break;
202 case 2:
203 $msg->header->type1 = strtolower($text);
204 break;
205 case 4: // Id
206 // Invisimail enclose images with <>
207 $msg->header->id = str_replace( '<', '', str_replace( '>', '', $text ) );
208 break;
209 case 5:
210 $msg->header->description = $text;
211 break;
212 case 6:
213 $msg->header->encoding = strtolower($text);
214 break;
215 case 7:
216 $msg->header->size = $text;
217 break;
218 default:
219 if ($msg->header->type0 == 'text' && $elem_num == 8) {
220 // This is a plain text message, so lets get the number of lines
221 // that it contains.
222 $msg->header->num_lines = $text;
223
224 } else if ($msg->header->type0 == 'message' && $msg->header->type1 == 'rfc822' && $elem_num == 8) {
225 // This is an encapsulated message, so lets start all over again and
226 // parse this message adding it on to the existing one.
227 $structure = trim($structure);
228 if ( $structure{0} == '(' ) {
229 $e = mime_match_parenthesis (0, $structure);
230 $structure = substr($structure, 0, $e);
231 $structure = substr($structure, 1);
232 $m = mime_parse_structure($structure, $msg->header->entity_id);
233
234 // the following conditional is there to correct a bug that wasn't
235 // incrementing the entity IDs correctly because of the special case
236 // that message/rfc822 is. This fixes it fine.
237 if (substr($structure, 1, 1) != '(')
238 $m->header->entity_id = mime_increment_id(mime_new_element_level($ent_id));
239
240 // Now we'll go through and reformat the results.
241 if ($m->entities) {
242 for ($i=0; $i < count($m->entities); $i++) {
243 $msg->addEntity($m->entities[$i]);
244 }
245 } else {
246 $msg->addEntity($m);
247 }
248 $structure = "";
249 }
250 }
251 break;
252 }
253 $elem_num++;
254 $text = "";
255 }
256 // loop through the additional properties and put those in the various headers
257 if ($msg->header->type0 != 'message') {
258 for ($i=0; $i < count($properties); $i++) {
259 $msg->header->{$properties[$i]['name']} = $properties[$i]['value'];
260 }
261 }
262
263 return $msg;
264 }
265
266 /*
267 * I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
268 * figure out how to do this part, so I decided to go to bed. I woke up
269 * in the morning and had a flash of insight. I went to the white-board
270 * and scribbled it out, then spent a bit programming it, and this is the
271 * result. Nothing complicated, but I think my brain was fried yesterday.
272 * Funny how that happens some times.
273 *
274 * This gets properties in a nested parenthesisized list. For example,
275 * this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
276 * This returns an array called $props with all paired up properties.
277 * It ignores the "attachment" for now, maybe that should change later
278 * down the road. In this case, what is returned is:
279 * $props[0]["name"] = "filename";
280 * $props[0]["value"] = "luke.tar.gz";
281 */
282 function mime_get_props ($props, $structure) {
283
284 while (strlen($structure) > 0) {
285 $structure = trim($structure);
286 $char = $structure{0};
287
288 if ($char == '"') {
289 $pos = 1;
290 $tmp = '';
291 while ( ( $char = $structure{$pos} ) != '"' &&
292 $pos < strlen($structure)) {
293 $tmp .= $char;
294 $pos++;
295 }
296 $structure = trim(substr($structure, strlen($tmp) + 2));
297 $char = $structure{0};
298
299 if ($char == '"') {
300 $pos = 1;
301 $value = '';
302 while ( ( $char = $structure{$pos} ) != '"' &&
303 $pos < strlen($structure) ) {
304 $value .= $char;
305 $pos++;
306 }
307 $structure = trim(substr($structure, strlen($tmp) + 2));
308
309 $k = count($props);
310 $props[$k]['name'] = strtolower($tmp);
311 $props[$k]['value'] = $value;
312 } else if ($char == '(') {
313 $end = mime_match_parenthesis (0, $structure);
314 $sub = substr($structure, 1, $end-1);
315 if (! isset($props))
316 $props = array();
317 $props = mime_get_props($props, $sub);
318 $structure = substr($structure, strlen($sub) + 2);
319 }
320 return $props;
321 } else if ($char == '(') {
322 $end = mime_match_parenthesis (0, $structure);
323 $sub = substr($structure, 1, $end-1);
324 $props = mime_get_props($props, $sub);
325 $structure = substr($structure, strlen($sub) + 2);
326 return $props;
327 } else {
328 return $props;
329 }
330 }
331 }
332
333 /*
334 * Matches parenthesis. It will return the position of the matching
335 * parenthesis in $structure. For instance, if $structure was:
336 * ("text" "plain" ("val1name", "1") nil ... )
337 * x x
338 * then this would return 42 to match up those two.
339 */
340 function mime_match_parenthesis ($pos, $structure) {
341
342 $j = strlen( $structure );
343
344 // ignore all extra characters
345 // If inside of a string, skip string -- Boundary IDs and other
346 // things can have ) in them.
347 if ( $structure{$pos} != '(' ) {
348 return( $j );
349 }
350
351 while ( $pos < $j ) {
352 $pos++;
353 if ($structure{$pos} == ')') {
354 return $pos;
355 } elseif ($structure{$pos} == '"') {
356 $pos++;
357 while ( $structure{$pos} != '"' &&
358 $pos < $j ) {
359 if (substr($structure, $pos, 2) == '\\"') {
360 $pos++;
361 } elseif (substr($structure, $pos, 2) == '\\\\') {
362 $pos++;
363 }
364 $pos++;
365 }
366 } elseif ( $structure{$pos} == '(' ) {
367 $pos = mime_match_parenthesis ($pos, $structure);
368 }
369 }
370 echo _("Error decoding mime structure. Report this as a bug!") . '<br>';
371 return( $pos );
372 }
373
374 function mime_fetch_body ($imap_stream, $id, $ent_id ) {
375 // do a bit of error correction. If we couldn't find the entity id, just guess
376 // that it is the first one. That is usually the case anyway.
377 if (!$ent_id)
378 $ent_id = 1;
379 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id]", true, $response, $message);
380 $topline = array_shift($data);
381 while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
382 $topline = array_shift($data);
383 $wholemessage = implode('', $data);
384 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
385 $ret = substr( $wholemessage, 0, $regs[1] );
386 /*
387 There is some information in the content info header that could be important
388 in order to parse html messages. Let's get them here.
389 */
390 if ( $ret{0} == '<' ) {
391 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message);
392 $base = '';
393 $k = 10;
394 foreach( $data as $d ) {
395 if ( substr( $d, 0, 13 ) == 'Content-Base:' ) {
396 $j = strlen( $d );
397 $i = 13;
398 $base = '';
399 while ( $i < $j &&
400 ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) )
401 $i++;
402 while ( $i < $j ) {
403 if ( isNoSep( $d{$i} ) )
404 $base .= $d{$i};
405 $i++;
406 }
407 $k = 0;
408 } elseif ( $k == 1 && !isnosep( $d{0} ) ) {
409 $base .= substr( $d, 1 );
410 }
411 $k++;
412 }
413 if ( $base <> '' )
414
415 $ret = "<base href=\"$base\">" . $ret;
416 }
417 } else if (ereg('"([^"]*)"', $topline, $regs)) {
418 $ret = $regs[1];
419 } else {
420 global $where, $what, $mailbox, $passed_id, $startMessage;
421 $par = "mailbox=".urlencode($mailbox)."&passed_id=$passed_id";
422 if (isset($where) && isset($what)) {
423 $par .= "&where=".urlencode($where)."&what=".urlencode($what);
424 } else {
425 $par .= "&startMessage=$startMessage&show_more=0";
426 }
427 $par .= '&response='.urlencode($response).'&message='.urlencode($message).
428 '&topline='.urlencode($topline);
429
430 echo '<b><font color=$color[2]>' .
431 _("Body retrieval error. The reason for this is most probably that the message is malformed. Please help us making future versions better by submitting this message to the developers knowledgebase!") .
432 "<A HREF=\"../src/retrievalerror.php?$par\">Submit message</A><BR>" .
433 '<tt>' . _("Response:") . "$response<BR>" .
434 _("Message:") . " $message<BR>" .
435 _("FETCH line:") . " $topline<BR></tt></font></b>";
436
437 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message);
438 array_shift($data);
439 $wholemessage = implode('', $data);
440
441 $ret = "---------------\n$wholemessage";
442
443 }
444 return( $ret );
445 }
446
447 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
448 // do a bit of error correction. If we couldn't find the entity id, just guess
449 // that it is the first one. That is usually the case anyway.
450 if (!$ent_id) {
451 $ent_id = 1;
452 }
453 $sid = sqimap_session_id();
454 // Don't kill the connection if the browser is over a dialup
455 // and it would take over 30 seconds to download it.
456
457 // donĀ“t call set_time_limit in safe mode.
458 if (!ini_get("safe_mode")) {
459 set_time_limit(0);
460 }
461
462 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
463 $cnt = 0;
464 $continue = true;
465 $read = fgets ($imap_stream,4096);
466 // This could be bad -- if the section has sqimap_session_id() . ' OK'
467 // or similar, it will kill the download.
468 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
469 if (trim($read) == ')==') {
470 $read1 = $read;
471 $read = fgets ($imap_stream,4096);
472 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
473 return;
474 } else {
475 echo decodeBody($read1, $encoding) .
476 decodeBody($read, $encoding);
477 }
478 } else if ($cnt) {
479 echo decodeBody($read, $encoding);
480 }
481 $read = fgets ($imap_stream,4096);
482 $cnt++;
483 }
484 }
485
486 /* -[ END MIME DECODING ]----------------------------------------------------------- */
487
488
489
490 /* This is the first function called. It decides if this is a multipart
491 message or if it should be handled as a single entity
492 */
493 function decodeMime ($imap_stream, &$header) {
494 global $username, $key, $imapServerAddress, $imapPort;
495 return mime_structure ($imap_stream, $header);
496 }
497
498 // This is here for debugging purposese. It will print out a list
499 // of all the entity IDs that are in the $message object.
500 /*
501 function listEntities ($message) {
502 if ($message) {
503 if ($message->header->entity_id)
504 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
505 for ($i = 0; $message->entities[$i]; $i++) {
506 $msg = listEntities($message->entities[$i], $ent_id);
507 if ($msg)
508 return $msg;
509 }
510 }
511 }
512 */
513
514 /* returns a $message object for a particular entity id */
515 function getEntity ($message, $ent_id) {
516 if ($message) {
517 if ($message->header->entity_id == $ent_id && strlen($ent_id) == strlen($message->header->entity_id)) {
518 return $message;
519 } else {
520 for ($i = 0; isset($message->entities[$i]); $i++) {
521 $msg = getEntity ($message->entities[$i], $ent_id);
522 if ($msg) {
523 return $msg;
524 }
525 }
526 }
527 }
528 }
529
530 /*
531 * figures out what entity to display and returns the $message object
532 * for that entity.
533 */
534 function findDisplayEntity ($message, $textOnly = 1) {
535 global $show_html_default;
536
537 $entity = 0;
538
539 if ($message) {
540 if ( $message->header->type0 == 'multipart' &&
541 ( $message->header->type1 == 'alternative' ||
542 $message->header->type1 == 'related' ) &&
543 $show_html_default && ! $textOnly ) {
544 $entity = findDisplayEntityHTML($message);
545 }
546
547 // Show text/plain or text/html -- the first one we find.
548 if ( $entity == 0 &&
549 $message->header->type0 == 'text' &&
550 ( $message->header->type1 == 'plain' ||
551 $message->header->type1 == 'html' ) &&
552 isset($message->header->entity_id) ) {
553 $entity = $message->header->entity_id;
554 }
555
556 $i = 0;
557 while ($entity == 0 && isset($message->entities[$i]) ) {
558 $entity = findDisplayEntity($message->entities[$i], $textOnly);
559 $i++;
560 }
561 }
562
563 return( $entity );
564 }
565
566 /* Shows the HTML version */
567 function findDisplayEntityHTML ($message) {
568
569 if ( $message->header->type0 == 'text' &&
570 $message->header->type1 == 'html' &&
571 isset($message->header->entity_id)) {
572 return $message->header->entity_id;
573 }
574 for ($i = 0; isset($message->entities[$i]); $i ++) {
575 $entity = findDisplayEntityHTML($message->entities[$i]);
576 if ($entity != 0) {
577 return $entity;
578 }
579 }
580
581 return 0;
582 }
583
584 /* This returns a parsed string called $body. That string can then
585 be displayed as the actual message in the HTML. It contains
586 everything needed, including HTML Tags, Attachments at the
587 bottom, etc.
588 */
589 function formatBody($imap_stream, $message, $color, $wrap_at) {
590 // this if statement checks for the entity to show as the
591 // primary message. To add more of them, just put them in the
592 // order that is their priority.
593 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
594 $show_html_default;
595
596 $id = $message->header->id;
597 $urlmailbox = urlencode($message->header->mailbox);
598
599 // Get the right entity and redefine message to be this entity
600 // Pass the 0 to mean that we want the 'best' viewable one
601 $ent_num = findDisplayEntity ($message, 0);
602 $body_message = getEntity($message, $ent_num);
603 if (($body_message->header->type0 == 'text') ||
604 ($body_message->header->type0 == 'rfc822')) {
605
606 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
607 $body = decodeBody($body, $body_message->header->encoding);
608 $hookResults = do_hook("message_body", $body);
609 $body = $hookResults[1];
610
611 // If there are other types that shouldn't be formatted, add
612 // them here
613 if ($body_message->header->type1 == 'html') {
614 if ( $show_html_default <> 1 ) {
615 $body = strip_tags( $body );
616 translateText($body, $wrap_at, $body_message->header->charset);
617 } else {
618 $body = MagicHTML( $body, $id );
619 }
620 } else {
621 translateText($body, $wrap_at, $body_message->header->charset);
622 }
623
624 $body .= "<SMALL><CENTER><A HREF=\"../src/download.php?absolute_dl=true&passed_id=$id&passed_ent_id=$ent_num&mailbox=$urlmailbox&showHeaders=1\">". _("Download this as a file") ."</A></CENTER><BR></SMALL>";
625
626 /** Display the ATTACHMENTS: message if there's more than one part **/
627 $body .= "</TD></TR></TABLE>";
628 if (isset($message->entities[0])) {
629 $body .= formatAttachments ($message, $ent_num, $message->header->mailbox, $id);
630 }
631 $body .= "</TD></TR></TABLE>";
632 } else {
633 $body = formatAttachments ($message, -1, $message->header->mailbox, $id);
634 }
635 return ($body);
636 }
637
638 /*
639 * A recursive function that returns a list of attachments with links
640 * to where to download these attachments
641 */
642 function formatAttachments ($message, $ent_id, $mailbox, $id) {
643 global $where, $what;
644 global $startMessage, $color;
645 static $ShownHTML = 0;
646
647 $body = "";
648 if ($ShownHTML == 0) {
649 $ShownHTML = 1;
650
651 $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
652 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
653 _("Attachments") . ':' .
654 "</B></TH></TR><TR><TD>\n" .
655 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
656 formatAttachments ($message, $ent_id, $mailbox, $id) .
657 "</TABLE></TD></TR></TABLE>";
658
659 return( $body );
660 }
661
662 if ($message) {
663 if (!$message->entities) {
664 $type0 = strtolower($message->header->type0);
665 $type1 = strtolower($message->header->type1);
666 $name = decodeHeader($message->header->name);
667
668 if ($message->header->entity_id != $ent_id) {
669 $filename = decodeHeader($message->header->filename);
670 if (trim($filename) == '') {
671 if (trim($name) == '') {
672 if ( trim( $message->header->id ) == '' )
673 $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
674 else
675 $display_filename = 'cid: ' . $message->header->id;
676 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
677 } else {
678 $display_filename = $name;
679 $filename = $name;
680 }
681 } else {
682 $display_filename = $filename;
683 }
684
685 $urlMailbox = urlencode($mailbox);
686 $ent = urlencode($message->header->entity_id);
687
688 $DefaultLink =
689 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
690 if ($where && $what)
691 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
692 $Links['download link']['text'] = _("download");
693 $Links['download link']['href'] =
694 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
695 $ImageURL = '';
696
697 / * this executes the attachment hook with a specific MIME-type.
698 * if that doens't have results, it tries if there's a rule
699 * for a more generic type. */
700 $HookResults = do_hook("attachment $type0/$type1", $Links,
701 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
702 $display_filename, $where, $what);
703 if(count($HookResults[1]) <= 1) {
704 $HookResults = do_hook("attachment $type0/*", $Links,
705 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
706 $display_filename, $where, $what);
707 }
708
709 $Links = $HookResults[1];
710 $DefaultLink = $HookResults[6];
711
712 $body .= '<TR><TD>&nbsp;&nbsp;</TD><TD>' .
713 "<A HREF=\"$DefaultLink\">$display_filename</A>&nbsp;</TD>" .
714 '<TD><SMALL><b>' . show_readable_size($message->header->size) .
715 '</b>&nbsp;&nbsp;</small></TD>' .
716 "<TD><SMALL>[ $type0/$type1 ]&nbsp;</SMALL></TD>" .
717 '<TD><SMALL>';
718 if ($message->header->description)
719 $body .= '<b>' . htmlspecialchars($message->header->description) . '</b>';
720 $body .= '</SMALL></TD><TD><SMALL>&nbsp;';
721
722
723 $SkipSpaces = 1;
724 foreach ($Links as $Val) {
725 if ($SkipSpaces) {
726 $SkipSpaces = 0;
727 } else {
728 $body .= '&nbsp;&nbsp;|&nbsp;&nbsp;';
729 }
730 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
731 }
732
733 unset($Links);
734
735 $body .= "</SMALL></TD></TR>\n";
736 }
737 } else {
738 for ($i = 0; $i < count($message->entities); $i++) {
739 $body .= formatAttachments ($message->entities[$i], $ent_id, $mailbox, $id);
740 }
741 }
742 return( $body );
743 }
744 }
745
746
747 /** this function decodes the body depending on the encoding type. **/
748 function decodeBody($body, $encoding) {
749 $body = str_replace("\r\n", "\n", $body);
750 $encoding = strtolower($encoding);
751
752 global $show_html_default;
753
754 if ($encoding == 'quoted-printable') {
755 $body = quoted_printable_decode($body);
756
757
758 while (ereg("=\n", $body))
759 $body = ereg_replace ("=\n", "", $body);
760
761 } else if ($encoding == 'base64') {
762 $body = base64_decode($body);
763 }
764
765 // All other encodings are returned raw.
766 return $body;
767 }
768
769 /*
770 * This functions decode strings that is encoded according to
771 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
772 */
773 function decodeHeader ($string, $utfencode=true) {
774 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
775 $string, $res)) {
776 if (ucfirst($res[2]) == 'B') {
777 $replace = base64_decode($res[3]);
778 } else {
779 $replace = str_replace('_', ' ', $res[3]);
780 // Convert lowercase Quoted Printable to uppercase for
781 // quoted_printable_decode to understand it.
782 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", $replace, $res)) {
783 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
784 }
785 $replace = quoted_printable_decode($replace);
786 }
787 /* Only encode into entities by default. Some places
788 don't need the encoding, like the compose form. */
789 if ($utfencode){
790 $replace = charset_decode ($res[1], $replace);
791 }
792
793 // Remove the name of the character set.
794 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
795 $replace, $string);
796
797 // In case there should be more encoding in the string: recurse
798 return (decodeHeader($string));
799 } else
800 return ($string);
801 }
802
803 /*
804 * Encode a string according to RFC 1522 for use in headers if it
805 * contains 8-bit characters or anything that looks like it should
806 * be encoded.
807 */
808 function encodeHeader ($string) {
809 global $default_charset;
810
811 // Encode only if the string contains 8-bit characters or =?
812 $j = strlen( $string );
813 $l = strstr($string, '=?'); // Must be encoded ?
814 $ret = '';
815 for( $i=0; $i < $j; ++$i) {
816 switch( $string{$i} ) {
817 case '=':
818 $ret .= '=3D';
819 break;
820 case '?':
821 $ret .= '=3F';
822 break;
823 case '_':
824 $ret .= '=5F';
825 break;
826 case ' ':
827 $ret .= '_';
828 break;
829 default:
830 $k = ord( $string{$i} );
831 if ( $k > 126 ) {
832 $ret .= sprintf("=%02X", $k);
833 $l = TRUE;
834 } else
835 $ret .= $string{$i};
836 }
837 }
838
839 if ( $l ) {
840 $string = "=?$default_charset?Q?$ret?=";
841 }
842
843 return( $string );
844 }
845
846 /*
847 Strips dangerous tags from html messages.
848 */
849 function MagicHTML( $body, $id ) {
850
851 global $message, $HTTP_SERVER_VARS,
852 $attachment_common_show_images;
853
854 $attachment_common_show_images =
855 FALSE; // Don't display attached images in HTML mode
856 $j = strlen( $body ); // Legnth of the HTML
857 $ret = ''; // Returned string
858 $bgcolor = '#ffffff'; // Background style color (defaults to white)
859 $textcolor = '#000000'; // Foreground style color (defaults to black)
860 $leftmargin = ''; // Left margin style
861 $title = ''; // HTML title if any
862
863 $i = 0;
864 while ( $i < $j ) {
865 if ( $body{$i} == '<' ) {
866 $pos = $i + 1;
867 $tag = '';
868 while ($body{$pos} == ' ' || $body{$pos} == "\t" ||
869 $body{$pos} == "\n") {
870 $pos ++;
871 }
872 while (strlen($tag) < 4 && $body{$pos} != ' ' &&
873 $body{$pos} != "\t" && $body{$pos} != "\n") {
874 $tag .= $body{$pos};
875 $pos ++;
876 }
877 switch( strtoupper( $tag ) ) {
878 // Strips the entire tag and contents
879 case 'APPL':
880 case 'EMBB':
881 case 'FRAM':
882 case 'SCRI':
883 case 'OBJE':
884 $etg = '/' . $tag;
885 while ( $body{$i+1}.$body{$i+2}.$body{$i+3}.$body{$i+4}.$body{$i+5} <> $etg &&
886 $i < $j ) $i++;
887 while ( $i < $j && $body{++$i} <> '>' );
888 // $ret .= "<!-- $tag removed -->";
889 break;
890 // Substitute Title
891 case 'TITL':
892 $i += 5;
893 while ( $body{$i} <> '>' && // </title>
894 $i < $j )
895 $i++;
896 $i++;
897 $title = '';
898 while ( $body{$i} <> '<' && // </title>
899 $i < $j ) {
900 $title .= $body{$i};
901 $i++;
902 }
903 $i += 7;
904 break;
905 // Destroy these tags
906 case 'HTML':
907 case 'HEAD':
908 case '/HTM':
909 case '/HEA':
910 case '!DOC':
911 case 'META':
912 case 'DIV ':
913 case '/DIV':
914 case '!-- ':
915 $i += 4;
916 while ( $body{$i} <> '>' &&
917 $i < $j )
918 $i++;
919 // $i++;
920 break;
921 case 'STYL':
922 $i += 5;
923 while ( $body{$i} <> '>' && // </title>
924 $i < $j )
925 $i++;
926 $i++;
927 // We parse the style to look for interesting stuff
928 $styleblk = '';
929 while ( $body{$i} <> '>' &&
930 $i < $j ) {
931 // First we get the name of the style
932 $style = '';
933 while ( $body{$i} <> '>' &&
934 $body{$i} <> '<' &&
935 $body{$i} <> '{' &&
936 $i < $j ) {
937 if ( isnoSep( $body{$i} ) )
938 $style .= $body{$i};
939 $i++;
940 }
941 stripComments( $i, $j, $body );
942 $style = strtoupper( trim( $style ) );
943 if ( $style == 'BODY' ) {
944 // Next we look into the definitions of the body style
945 while ( $body{$i} <> '>' &&
946 $body{$i} <> '}' &&
947 $i < $j ) {
948 // We look for the background color if any.
949 if ( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
950 $i += 17;
951 $bgcolor = getStyleData( $i, $j, $body );
952 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
953 $i += 12;
954 $leftmargin = getStyleData( $i, $j, $body );
955 }
956 $i++;
957 }
958 } else {
959 // Other style are mantained
960 $styleblk .= "$style ";
961 while ( $body{$i} <> '>' &&
962 $body{$i} <> '<' &&
963 $body{$i} <> '}' &&
964 $i < $j ) {
965 $styleblk .= $body{$i};
966 $i++;
967 }
968 $styleblk .= $body{$i};
969 }
970 stripComments( $i, $j, $body );
971 if ( $body{$i} <> '>' )
972 $i++;
973 }
974 if ( $styleblk <> '' )
975 $ret .= "<style>$styleblk";
976 break;
977 case 'BODY':
978 if ( $title <> '' )
979 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
980 $ret .= "<TABLE";
981 $i += 5;
982 if (! isset($base)) {
983 $base = '';
984 }
985 $ret .= stripEvent( $i, $j, $body, $id, $base );
986 $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
987 if ( $leftmargin <> '' )
988 $ret .= "<td width=$leftmargin>&nbsp;</td>";
989 $ret .= '<td>';
990 if (strtolower($bgcolor) == 'ffffff' ||
991 strtolower($bgcolor) == '#ffffff')
992 $ret .= '<font color=#000000>';
993 break;
994 case 'BASE':
995 $i += 5;
996 $base = '';
997 while ( !isNoSep( $body{$i} ) &&
998 $i < $j ) {
999 $i++;
1000 }
1001 if ( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
1002 $i += 5;
1003 while ( !isNoSep( $body{$i} ) &&
1004 $i < $j ) {
1005 $i++;
1006 }
1007 while ( $body{$i} <> '>' &&
1008 $i < $j ) {
1009 if ( $body{$i} <> '"' ) {
1010 $base .= $body{$i};
1011 }
1012 $i++;
1013 }
1014 // Debuging $ret .= "<!-- base == $base -->";
1015 if ( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 ) {
1016 $ret .= "\n<BASE HREF=\"$base\">\n";
1017 }
1018 }
1019 break;
1020 case '/BOD':
1021 $ret .= '</font></td></tr></TABLE>';
1022 $i += 6;
1023 break;
1024 default:
1025 // Following tags can contain some event handler, lets search it
1026 stripComments( $i, $j, $body );
1027 if (! isset($base)) {
1028 $base = '';
1029 }
1030 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
1031 // $ret .= "<!-- $tag detected -->";
1032 }
1033 } else {
1034 $ret .= $body{$i};
1035 }
1036 $i++;
1037 }
1038
1039 return( "\n\n<!-- HTML Output ahead -->\n" .
1040 $ret .
1041 "\n<!-- END of HTML Output --><base href=\"".
1042 get_location() . '/'.
1043 "\">\n\n" );
1044 }
1045
1046 function isNoSep( $char ) {
1047
1048 switch( $char ) {
1049 case ' ':
1050 case "\n":
1051 case "\t":
1052 case "\r":
1053 case '>':
1054 case '"':
1055 return( FALSE );
1056 break;
1057 default:
1058 return( TRUE );
1059 }
1060
1061 }
1062
1063 /*
1064 The following function is usefull to remove extra data that can cause
1065 html not to display properly. Especialy with MS stuff.
1066 */
1067
1068 function stripComments( &$i, $j, &$body ) {
1069
1070 while ( $body{$i}.$body{$i+1}.$body{$i+2}.$body{$i+3} == '<!--' &&
1071 $i < $j ) {
1072 $i += 5;
1073 while ( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1074 $i < $j )
1075 $i++;
1076 $i++;
1077 }
1078
1079 return;
1080
1081 }
1082
1083 /* Gets the style data of a specific style */
1084
1085 function getStyleData( &$i, $j, &$body ) {
1086
1087 // We skip spaces
1088 while ( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1089 $i < $j ) {
1090 $i++;
1091 }
1092 // And get the color
1093 $ret = '';
1094 while ( isNoSep( $body{$i} ) &&
1095 $i < $j ) {
1096 $ret .= $body{$i};
1097 $i++;
1098 }
1099
1100 return( $ret );
1101 }
1102
1103 /*
1104 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1105 change on with no (onload -> noload)
1106 */
1107
1108 function stripEvent( &$i, $j, &$body, $id, $base ) {
1109
1110 global $message, $base_uri;
1111
1112 $ret = '';
1113
1114 while ( $body{$i} <> '>' &&
1115 $i < $j ) {
1116 $etg = strtolower($body{$i}.$body{$i+1}.$body{$i+2});
1117 switch( $etg ) {
1118 case 'src':
1119 // This is probably a src specification
1120 $k = $i + 3;
1121 while( !isNoSep( $body{$k} )) {
1122 $k++;
1123 }
1124 if ( $body{$k} == '=' ) {
1125 /* It is indeed */
1126 $k++;
1127 while( !isNoSep( $body{$k} ) &&
1128 $k < $j ) {
1129 $k++;
1130 }
1131 $src = '';
1132 while ( $body{$k} <> '>' && isNoSep( $body{$k} ) &&
1133 $k < $j ) {
1134 $src .= $body{$k};
1135 $k++;
1136 }
1137 while( !isNoSep( $body{$k} ) &&
1138 $k < $j ) {
1139 $k++;
1140 }
1141 if ( strtolower( substr( $src, 0, 4 ) ) == 'cid:' ) {
1142 $src = substr( $src, 4 );
1143 $src = "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1144 urlencode( $message->header->mailbox ) .
1145 "&passed_ent_id=" . find_ent_id( $src, $message );
1146 } else if ( strtolower( substr( $src, 0, 4 ) ) <> 'http' ||
1147 stristr( $src, $base_uri ) ) {
1148 /* Javascript and local urls goes out */
1149 $src = '../images/' . _("sec_remove_eng.png");
1150 }
1151 $ret .= 'src="' . $src . '" ';
1152 $i = $k - 3;
1153 } else {
1154 $ret .= 'src';
1155 $i = $i + 3;
1156 }
1157
1158 break;
1159 case '../':
1160 // Retrolinks are not allowed without a base because they mess with SM security
1161 if ( $base == '' ) {
1162 $i += 2;
1163 } else {
1164 $ret .= '.';
1165 }
1166 break;
1167 case 'cid':
1168 // Internal link
1169 $k = $i-1;
1170 if ( $body{$i+3} == ':') {
1171 $i +=4;
1172 $name = '';
1173 while ( isNoSep( $body{$i} ) &&
1174 $i < $j ) {
1175 $name .= $body{$i++};
1176 }
1177 if ( $name <> '' ) {
1178 $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1179 urlencode( $message->header->mailbox ) .
1180 "&passed_ent_id=" . find_ent_id( $name, $message );
1181 if ( $body{$k} == '"' )
1182 $ret .= '" ';
1183 else
1184 $ret .= ' ';
1185 }
1186 if ( $body{$i} == '>' )
1187 $i -= 1;
1188 }
1189 break;
1190 case ' on':
1191 case "\non":
1192 case "\ron":
1193 case "\ton":
1194 $ret .= ' no';
1195 $i += 2;
1196 break;
1197 case 'pt:':
1198 if ( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+1}.$body{$i+2}, 'script:') == 0 ) {
1199 $ret .= '_no/';
1200 } else {
1201 $ret .= $etg;
1202 }
1203 $i += 2;
1204 break;
1205 default:
1206 $ret .= $body{$i};
1207 }
1208 $i++;
1209 }
1210 return( $ret );
1211 }
1212
1213
1214 /* This function trys to locate the entity_id of a specific mime element */
1215
1216 function find_ent_id( $id, $message ) {
1217
1218 $ret = '';
1219 for ($i=0; $ret == '' && $i < count($message->entities); $i++) {
1220
1221 if ( $message->entities[$i]->header->entity_id == '' ) {
1222 $ret = find_ent_id( $id, $message->entities[$i] );
1223 } else {
1224 if ( strcasecmp( $message->entities[$i]->header->id, $id ) == 0 )
1225 $ret = $message->entities[$i]->header->entity_id;
1226 }
1227
1228 }
1229
1230 return( $ret );
1231
1232 }
1233 ?>