39839600442eeb60c934e96922e48c617b6c6e82
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains the functions necessary to detect and decode MIME
15 require_once('../functions/imap.php');
16 require_once('../functions/attachment_common.php');
18 /** Setting up the objects that have the structure for the message **/
20 /** msg_header contains generic variables for values that **/
21 /** could be in a header. **/
23 var $type0 = '', $type1 = '', $boundary = '', $charset = '',
24 $encoding = '', $size = 0, $to = array(), $from = '', $date = '',
25 $cc = array(), $bcc = array(), $reply_to = '', $subject = '',
26 $id = 0, $mailbox = '', $description = '', $filename = '',
27 $entity_id = 0, $message_id = 0, $name = '', $priority = 3, $type = '';
31 /** message is the object that contains messages. It is a recursive
32 object in that through the $entities variable, it can contain
33 more objects of type message. See documentation in mime.txt for
34 a better description of how this works.
36 var $header = '', $entities = array();
38 function addEntity ($msg) {
39 $this->entities
[] = $msg;
43 /* --------------------------------------------------------------------------------- */
45 /* --------------------------------------------------------------------------------- */
47 /* This function gets the structure of a message and stores it in the "message" class.
48 * It will return this object for use with all relevant header information and
49 * fully parsed into the standard "message" object format.
51 function mime_structure ($imap_stream, $header) {
53 $ssid = sqimap_session_id();
54 $lsid = strlen( $ssid );
56 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
58 // This should use sqimap_read_data instead of reading it itself
60 $read = fgets ($imap_stream, 9216);
62 while ( substr($read, 0, $lsid) <> $ssid &&
63 !feof( $imap_stream ) ) {
64 $bodystructure .= $read;
65 $read = fgets ($imap_stream, 9216);
67 $read = $bodystructure;
69 // isolate the body structure and remove beginning and end parenthesis
70 $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') +
13));
72 $read = trim(substr ($read, 0, -1));
73 $end = mime_match_parenthesis(0, $read);
74 while ($end == strlen($read)-1) {
75 $read = trim(substr ($read, 0, -1));
76 $read = trim(substr ($read, 1));
77 $end = mime_match_parenthesis(0, $read);
80 $msg = mime_parse_structure ($read, 0);
81 $msg->header
= $header;
86 /* this starts the parsing of a particular structure. It is called recursively,
87 * so it can be passed different structures. It returns an object of type
89 * First, it checks to see if it is a multipart message. If it is, then it
90 * handles that as it sees is necessary. If it is just a regular entity,
91 * then it parses it and adds the necessary header information (by calling out
92 * to mime_get_elements()
94 function mime_parse_structure ($structure, $ent_id) {
95 $properties = array();
97 if ($structure{0} == '(') {
98 $old_ent_id = $ent_id;
99 $ent_id = mime_new_element_level($ent_id);
103 $end = mime_match_parenthesis ($start, $structure);
104 /* add "forgotten" parent entities (alternative and relative) */
105 if (strpos($ent_id, '0') ||
strpos($ent_id, '0') == 0) {
106 $str = substr($structure, $end+
1 );
107 $startprop = strrpos($str,'(');
108 $endprop = strrpos($str,')');
109 $propstr = substr($str, $startprop +
1, ($endprop - $startprop)-1);
111 $type1 = trim(substr($str,0, $startprop));
112 $pos = strrpos($type1,' ');
113 $type1 = strtolower(trim(substr($type1,$pos +
1)));
114 $cnt = strlen($type1);
115 $type1 = substr($type1,0,$cnt-1);
117 $properties = mime_get_props($properties, $propstr);
118 if (count($properties)>0) {
119 $msg->header
->entity_id
= $old_ent_id;
120 $msg->header
->type0
= 'multipart';
121 $msg->header
->type1
= $type1;
123 for ($i=0; $i < count($properties); $i++
) {
124 $msg->header
->{$properties[$i]['name']} = $properties[$i]['value'];
125 $name = $properties[$i]['name'];
126 $value = $properties[$i]['value'];
130 $element = substr($structure, $start+
1, ($end - $start)-1);
132 $ent_id = mime_increment_id ($ent_id);
133 $newmsg = mime_parse_structure ($element, $ent_id);
134 $msg->addEntity ($newmsg);
136 } while ($structure{$end+
1} == '(');
138 // parse the elements
139 $msg = mime_get_element ($structure, $msg, $ent_id);
144 /* Increments the element ID. An element id can look like any of
145 * the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
146 * the last number of the element id, changing 1.2 to 1.3.
148 function mime_increment_id ($id) {
150 if (strpos($id, '.')) {
151 $first = substr($id, 0, strrpos($id, '.'));
152 $last = substr($id, strrpos($id, '.')+
1);
154 $new = $first . '.' .$last;
163 * See comment for mime_increment_id().
164 * This adds another level on to the entity_id changing 1.3 to 1.3.0
165 * NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
166 * before it can be used. I left it this way so as not to have
167 * to make a special case if it is the first entity_id. It
168 * always increments it, and that works fine.
170 function mime_new_element_level ($id) {
181 function mime_get_element (&$structure, $msg, $ent_id) {
184 $msg->header
= new msg_header();
185 $msg->header
->entity_id
= $ent_id;
186 $properties = array();
187 while (strlen($structure) > 0) {
188 $structure = trim($structure);
189 $char = $structure{0};
191 if (strtolower(substr($structure, 0, 3)) == 'nil') {
193 $structure = substr($structure, 3);
194 } else if ($char == '"') {
195 // loop through until we find the matching quote, and return that as a string
198 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
202 $structure = substr($structure, strlen($text) +
2);
203 } else if ($char == '(') {
205 $end = mime_match_parenthesis (0, $structure);
206 $sub = substr($structure, 1, $end-1);
207 $properties = mime_get_props($properties, $sub);
208 $structure = substr($structure, strlen($sub) +
2);
210 // loop through until we find a space or an end parenthesis
212 $char = $structure{$pos};
214 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
217 $char = $structure{$pos};
219 $structure = substr($structure, strlen($text));
222 // This is where all the text parts get put into the header
225 $msg->header
->type0
= strtolower($text);
228 $msg->header
->type1
= strtolower($text);
231 // Invisimail enclose images with <>
232 $msg->header
->id
= str_replace( '<', '', str_replace( '>', '', $text ) );
235 $msg->header
->description
= $text;
238 $msg->header
->encoding
= strtolower($text);
241 $msg->header
->size
= $text;
244 if ($msg->header
->type0
== 'text' && $elem_num == 8) {
245 // This is a plain text message, so lets get the number of lines
247 $msg->header
->num_lines
= $text;
249 } else if ($msg->header
->type0
== 'message' && $msg->header
->type1
== 'rfc822' && $elem_num == 8) {
250 // This is an encapsulated message, so lets start all over again and
251 // parse this message adding it on to the existing one.
252 $structure = trim($structure);
253 if ( $structure{0} == '(' ) {
254 $e = mime_match_parenthesis (0, $structure);
255 $structure = substr($structure, 0, $e);
256 $structure = substr($structure, 1);
257 $m = mime_parse_structure($structure, $msg->header
->entity_id
);
259 // the following conditional is there to correct a bug that wasn't
260 // incrementing the entity IDs correctly because of the special case
261 // that message/rfc822 is. This fixes it fine.
262 if (substr($structure, 1, 1) != '(')
263 $m->header
->entity_id
= mime_increment_id(mime_new_element_level($ent_id));
265 // Now we'll go through and reformat the results.
267 for ($i=0; $i < count($m->entities
); $i++
) {
268 $msg->addEntity($m->entities
[$i]);
281 // loop through the additional properties and put those in the various headers
282 for ($i=0; $i < count($properties); $i++
) {
283 $msg->header
->{$properties[$i]['name']} = $properties[$i]['value'];
290 * I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
291 * figure out how to do this part, so I decided to go to bed. I woke up
292 * in the morning and had a flash of insight. I went to the white-board
293 * and scribbled it out, then spent a bit programming it, and this is the
294 * result. Nothing complicated, but I think my brain was fried yesterday.
295 * Funny how that happens some times.
297 * This gets properties in a nested parenthesisized list. For example,
298 * this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
299 * This returns an array called $props with all paired up properties.
300 * It ignores the "attachment" for now, maybe that should change later
301 * down the road. In this case, what is returned is:
302 * $props[0]["name"] = "filename";
303 * $props[0]["value"] = "luke.tar.gz";
305 function mime_get_props ($props, $structure) {
307 while (strlen($structure) > 0) {
308 $structure = trim($structure);
309 $char = $structure{0};
313 while ( ( $char = $structure{$pos} ) != '"' &&
314 $pos < strlen($structure)) {
318 $structure = trim(substr($structure, strlen($tmp) +
2));
319 $char = $structure{0};
324 while ( ( $char = $structure{$pos} ) != '"' &&
325 $pos < strlen($structure) ) {
329 $structure = trim(substr($structure, strlen($value) +
2));
331 $props[$k]['name'] = strtolower($tmp);
332 $props[$k]['value'] = $value;
333 if ($structure != '') {
334 mime_get_props($props, $structure);
338 } else if ($char == '(') {
339 $end = mime_match_parenthesis (0, $structure);
340 $sub = substr($structure, 1, $end-1);
343 $props = mime_get_props($props, $sub);
344 $structure = substr($structure, strlen($sub) +
2);
347 } else if ($char == '(') {
348 $end = mime_match_parenthesis (0, $structure);
349 $sub = substr($structure, 1, $end-1);
350 $props = mime_get_props($props, $sub);
351 $structure = substr($structure, strlen($sub) +
2);
360 * Matches parenthesis. It will return the position of the matching
361 * parenthesis in $structure. For instance, if $structure was:
362 * ("text" "plain" ("val1name", "1") nil ... )
364 * then this would return 42 to match up those two.
366 function mime_match_parenthesis ($pos, $structure) {
368 $j = strlen( $structure );
370 // ignore all extra characters
371 // If inside of a string, skip string -- Boundary IDs and other
372 // things can have ) in them.
373 if ( $structure{$pos} != '(' ) {
377 while ( $pos < $j ) {
379 if ($structure{$pos} == ')') {
381 } elseif ($structure{$pos} == '"') {
383 while ( $structure{$pos} != '"' &&
385 if (substr($structure, $pos, 2) == '\\"') {
387 } elseif (substr($structure, $pos, 2) == '\\\\') {
392 } elseif ( $structure{$pos} == '(' ) {
393 $pos = mime_match_parenthesis ($pos, $structure);
396 echo _("Error decoding mime structure. Report this as a bug!") . '<br>';
400 function mime_fetch_body($imap_stream, $id, $ent_id ) {
403 * do a bit of error correction. If we couldn't find the entity id, just guess
404 * that it is the first one. That is usually the case anyway.
410 $cmd = "FETCH $id BODY[$ent_id]";
411 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message);
414 $topline = trim(array_shift( $data ));
415 } while( $topline && $topline[0] == '*' && !preg_match( '/\* [0-9]+ FETCH.*/i', $topline )) ;
416 $wholemessage = implode('', $data);
417 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
419 $ret = substr( $wholemessage, 0, $regs[1] );
421 There is some information in the content info header that could be important
422 in order to parse html messages. Let's get them here.
424 if ( $ret{0} == '<' ) {
425 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message);
426 /* BASE within HTML documents is illegal (see w3 spec)
429 * foreach( $data as $d ) {
430 * if ( substr( $d, 0, 13 ) == 'Content-Base:' ) {
435 * ( !isNoSep( $d{$i} ) || $d{$i} == '"' ) )
437 * while ( $i < $j ) {
438 * if ( isNoSep( $d{$i} ) )
443 * } elseif ( $k == 1 && !isnosep( $d{0} ) ) {
444 * $base .= substr( $d, 1 );
448 * if ( $base <> '' ) {
449 * $ret = "<base href=\"$base\">" . $ret;
453 } else if (ereg('"([^"]*)"', $topline, $regs)) {
456 global $where, $what, $mailbox, $passed_id, $startMessage;
457 $par = 'mailbox=' . urlencode($mailbox) . "&passed_id=$passed_id";
458 if (isset($where) && isset($what)) {
459 $par .= '&where='. urlencode($where) . "&what=" . urlencode($what);
461 $par .= "&startMessage=$startMessage&show_more=0";
463 $par .= '&response=' . urlencode($response) .
464 '&message=' . urlencode($message).
465 '&topline=' . urlencode($topline);
468 '<table width="80%"><tr>' .
469 '<tr><td colspan=2>' .
470 _("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!") .
471 " <A HREF=\"../src/retrievalerror.php?$par\"><br>" .
472 _("Submit message") . '</A><BR> ' .
474 '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
475 '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
476 '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
477 '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
478 "</table><BR></tt></font><hr>";
480 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message);
482 $wholemessage = implode('', $data);
484 $ret = $wholemessage;
489 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
490 // do a bit of error correction. If we couldn't find the entity id, just guess
491 // that it is the first one. That is usually the case anyway.
495 $sid = sqimap_session_id();
496 // Don't kill the connection if the browser is over a dialup
497 // and it would take over 30 seconds to download it.
499 // don´t call set_time_limit in safe mode.
500 if (!ini_get("safe_mode")) {
504 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
507 $read = fgets ($imap_stream,4096);
508 // This could be bad -- if the section has sqimap_session_id() . ' OK'
509 // or similar, it will kill the download.
510 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
511 if (trim($read) == ')==') {
513 $read = fgets ($imap_stream,4096);
514 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
517 echo decodeBody($read1, $encoding) .
518 decodeBody($read, $encoding);
521 echo decodeBody($read, $encoding);
523 $read = fgets ($imap_stream,4096);
528 /* -[ END MIME DECODING ]----------------------------------------------------------- */
532 /* This is the first function called. It decides if this is a multipart
533 message or if it should be handled as a single entity
535 function decodeMime ($imap_stream, &$header) {
536 global $username, $key, $imapServerAddress, $imapPort;
537 return mime_structure ($imap_stream, $header);
540 // This is here for debugging purposese. It will print out a list
541 // of all the entity IDs that are in the $message object.
543 function listEntities ($message) {
545 if ($message->header
->entity_id
)
546 echo "<tt>" . $message->header
->entity_id
. ' : ' . $message->header
->type0
. '/' . $message->header
->type1
. '<br>';
547 for ($i = 0; $message->entities
[$i]; $i++
) {
548 $msg = listEntities($message->entities
[$i], $ent_id);
556 /* returns a $message object for a particular entity id */
557 function getEntity ($message, $ent_id) {
559 if ($message->header
->entity_id
== $ent_id && strlen($ent_id) == strlen($message->header
->entity_id
))
563 for ($i = 0; isset($message->entities
[$i]); $i++
) {
564 $msg = getEntity ($message->entities
[$i], $ent_id);
574 * figures out what entity to display and returns the $message object
577 function findDisplayEntity ($msg, $textOnly = 1) {
578 global $show_html_default;
583 if ( $msg->header
->type0
== 'multipart' &&
584 ( $msg->header
->type1
== 'alternative' ||
585 $msg->header
->type1
== 'mixed' ||
586 $msg->header
->type1
== 'related' ) &&
587 $show_html_default && ! $textOnly ) {
588 $entity = findDisplayEntityHTML($msg);
591 // Show text/plain or text/html -- the first one we find.
593 $msg->header
->type0
== 'text' &&
594 ( $msg->header
->type1
== 'plain' ||
595 $msg->header
->type1
== 'html' ) &&
596 isset($msg->header
->entity_id
) ) {
597 $entity = $msg->header
->entity_id
;
601 while ($entity == 0 && isset($msg->entities
[$i]) ) {
602 $entity = findDisplayEntity($msg->entities
[$i], $textOnly);
610 /* Shows the HTML version */
611 function findDisplayEntityHTML ($message) {
613 if ( $message->header
->type0
== 'text' &&
614 $message->header
->type1
== 'html' &&
615 isset($message->header
->entity_id
)) {
616 return $message->header
->entity_id
;
618 for ($i = 0; isset($message->entities
[$i]); $i ++
) {
619 if ( $message->header
->type0
== 'message' &&
620 $message->header
->type1
== 'rfc822' &&
621 isset($message->header
->entity_id
)) {
624 $entity = findDisplayEntityHTML($message->entities
[$i]);
635 * Extracted from strings.php 23/03/2002
638 function translateText(&$body, $wrap_at, $charset) {
639 global $where, $what; /* from searching */
640 global $color; /* color theme */
642 require_once('../functions/url_parser.php');
644 $body_ary = explode("\n", $body);
646 for ($i=0; $i < count($body_ary); $i++
) {
647 $line = $body_ary[$i];
648 if (strlen($line) - 2 >= $wrap_at) {
649 sqWordWrap($line, $wrap_at);
651 $line = charset_decode($charset, $line);
652 $line = str_replace("\t", ' ', $line);
658 $j = strlen( $line );
660 while ( $pos < $j ) {
661 if ($line[$pos] == ' ') {
663 } else if (strpos($line, '>', $pos) === $pos) {
672 if (! isset($color[14])) {
673 $color[14] = '#FF0000';
675 $line = '<FONT COLOR="' . $color[14] . '">' . $line . '</FONT>';
677 if (! isset($color[13])) {
678 $color[13] = '#800000';
680 $line = '<FONT COLOR="' . $color[13] . '">' . $line . '</FONT>';
683 $body_ary[$i] = $line;
685 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
688 /* debugfunction for looping through entities and displaying correct entities */
689 function listMyEntities ($message) {
692 if ($message->header
->entity_id
) {
693 echo "<tt>" . $message->header
->entity_id
. ' : ' . $message->header
->type0
. '/' . $message->header
->type1
. '<br>';
695 if (!($message->header
->type0
== 'message' && $message->header
->type1
== 'rfc822')) {
696 if (isset($message->header
->boundary
) ) {
697 $ent_id = $message->header
->entity_id
;
698 $var = $message->header
->boundary
;
700 echo "<b>$ent_id boundary = $var</b><br>";
702 if (isset($message->header
->type
) ) {
703 $var = $message->header
->type
;
705 echo "<b>$ent_id type = $var</b><br>";
707 for ($i = 0; $message->entities
[$i]; $i++
) {
708 $msg = listMyEntities($message->entities
[$i]);
711 if ($msg ) return $msg;
719 /* This returns a parsed string called $body. That string can then
720 be displayed as the actual message in the HTML. It contains
721 everything needed, including HTML Tags, Attachments at the
724 function formatBody($imap_stream, $message, $color, $wrap_at) {
725 // this if statement checks for the entity to show as the
726 // primary message. To add more of them, just put them in the
727 // order that is their priority.
728 global $startMessage, $username, $key, $imapServerAddress, $imapPort, $body,
729 $show_html_default, $has_unsafe_images, $view_unsafe_images, $sort;
731 $has_unsafe_images = 0;
733 $id = $message->header
->id
;
735 $urlmailbox = urlencode($message->header
->mailbox
);
736 // ListMyEntities($message);
737 // Get the right entity and redefine message to be this entity
738 // Pass the 0 to mean that we want the 'best' viewable one
739 $ent_num = findDisplayEntity ($message, 0);
740 $body_message = getEntity($message, $ent_num);
741 if (($body_message->header
->type0
== 'text') ||
742 ($body_message->header
->type0
== 'rfc822')) {
743 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
744 $body = decodeBody($body, $body_message->header
->encoding
);
745 $hookResults = do_hook("message_body", $body);
746 $body = $hookResults[1];
747 // If there are other types that shouldn't be formatted, add
749 if ($body_message->header
->type1
== 'html') {
750 if ( $show_html_default <> 1 ) {
751 $body = strip_tags( $body );
752 translateText($body, $wrap_at, $body_message->header
->charset
);
754 $body = MagicHTML( $body, $id );
757 translateText($body, $wrap_at, $body_message->header
->charset
);
760 $body .= "<CENTER><SMALL><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></SMALL></CENTER><BR>";
761 if ($has_unsafe_images) {
762 if ($view_unsafe_images) {
763 $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&mailbox=$urlmailbox&sort=$sort&startMessage=$startMessage&show_more=0\">". _("Hide Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
765 $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&mailbox=$urlmailbox&sort=$sort&startMessage=$startMessage&show_more=0&view_unsafe_images=1\">". _("View Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
769 /** Display the ATTACHMENTS: message if there's more than one part **/
770 if (isset($message->entities
[0])) {
771 $body .= formatAttachments ($message, $ent_num, $message->header
->mailbox
, $id);
774 $body = formatAttachments ($message, -1, $message->header
->mailbox
, $id);
780 * A recursive function that returns a list of attachments with links
781 * to where to download these attachments
783 function formatAttachments($message, $ent_id, $mailbox, $id) {
784 global $where, $what;
785 global $startMessage, $color;
786 static $ShownHTML = 0;
789 if ($ShownHTML == 0) {
792 $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
793 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
794 _("Attachments") . ':' .
795 "</B></TH></TR><TR><TD>\n" .
796 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
797 formatAttachments($message, $ent_id, $mailbox, $id) .
798 "</TABLE></TD></TR></TABLE>";
800 } else if ($message) {
801 $header = $message->header
;
802 $type0 = strtolower($header->type0
);
803 $type1 = strtolower($header->type1
);
804 $name = decodeHeader($header->name
);
806 if ($type0 =='message' && $type1 == 'rfc822') {
808 $filename = decodeHeader($message->header
->filename
);
809 if (trim($filename) == '') {
810 if (trim($name) == '') {
811 $display_filename = 'untitled-[' . $message->header
->entity_id
. ']' ;
813 $display_filename = $name;
817 $display_filename = $filename;
820 $urlMailbox = urlencode($mailbox);
821 $ent = urlencode($message->header
->entity_id
);
824 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
825 if ($where && $what) {
826 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
828 $Links['download link']['text'] = _("download");
829 $Links['download link']['href'] =
830 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
833 /* this executes the attachment hook with a specific MIME-type.
834 * if that doens't have results, it tries if there's a rule
835 * for a more generic type. */
836 $HookResults = do_hook("attachment $type0/$type1", $Links,
837 $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what);
838 if(count($HookResults[1]) <= 1) {
839 $HookResults = do_hook("attachment $type0/*", $Links,
840 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
841 $display_filename, $where, $what);
844 $Links = $HookResults[1];
845 $DefaultLink = $HookResults[6];
847 $body .= '<TR><TD> </TD><TD>' .
848 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
849 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
850 '</b> </small></TD>' .
851 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
853 if ($message->header
->description
) {
854 $body .= '<b>' . htmlspecialchars(_($message->header
->description
)) . '</b>';
856 $body .= '</SMALL></TD><TD><SMALL> ';
860 foreach ($Links as $Val) {
864 $body .= ' | ';
866 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
871 $body .= "</SMALL></TD></TR>\n";
875 } elseif (!$message->entities
) {
877 $type0 = strtolower($message->header
->type0
);
878 $type1 = strtolower($message->header
->type1
);
879 $name = decodeHeader($message->header
->name
);
881 if ($message->header
->entity_id
!= $ent_id) {
882 $filename = decodeHeader($message->header
->filename
);
883 if (trim($filename) == '') {
884 if (trim($name) == '') {
885 if ( trim( $message->header
->id
) == '' )
886 $display_filename = 'untitled-[' . $message->header
->entity_id
. ']' ;
888 $display_filename = 'cid: ' . $message->header
->id
;
889 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
891 $display_filename = $name;
895 $display_filename = $filename;
898 $urlMailbox = urlencode($mailbox);
899 $ent = urlencode($message->header
->entity_id
);
902 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
903 if ($where && $what) {
904 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
906 $Links['download link']['text'] = _("download");
907 $Links['download link']['href'] =
908 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
911 /* this executes the attachment hook with a specific MIME-type.
912 * if that doens't have results, it tries if there's a rule
913 * for a more generic type. */
914 $HookResults = do_hook("attachment $type0/$type1", $Links,
915 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
916 $display_filename, $where, $what);
917 if(count($HookResults[1]) <= 1) {
918 $HookResults = do_hook("attachment $type0/*", $Links,
919 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
920 $display_filename, $where, $what);
923 $Links = $HookResults[1];
924 $DefaultLink = $HookResults[6];
926 $body .= '<TR><TD> </TD><TD>' .
927 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
928 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
929 '</b> </small></TD>' .
930 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
932 if ($message->header
->description
) {
933 $body .= '<b>' . htmlspecialchars(_($message->header
->description
)) . '</b>';
935 $body .= '</SMALL></TD><TD><SMALL> ';
939 foreach ($Links as $Val) {
943 $body .= ' | ';
945 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
950 $body .= "</SMALL></TD></TR>\n";
953 for ($i = 0; $i < count($message->entities
); $i++
) {
954 $body .= formatAttachments($message->entities
[$i], $ent_id, $mailbox, $id);
962 /** this function decodes the body depending on the encoding type. **/
963 function decodeBody($body, $encoding) {
964 $body = str_replace("\r\n", "\n", $body);
965 $encoding = strtolower($encoding);
967 global $show_html_default;
969 if ($encoding == 'quoted-printable') {
970 $body = quoted_printable_decode($body);
973 while (ereg("=\n", $body))
974 $body = ereg_replace ("=\n", "", $body);
976 } else if ($encoding == 'base64') {
977 $body = base64_decode($body);
980 // All other encodings are returned raw.
985 * This functions decode strings that is encoded according to
986 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
987 * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002
989 function decodeHeader ($string, $utfencode=true) {
990 if (is_array($string)) {
991 $string = implode("\n", $string);
994 while (preg_match('/^(.{' . $i . '})(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=/Ui',
997 // Ignore white-space between consecutive encoded-words
998 if (strspn($res[2], " \t") != strlen($res[2])) {
1002 if (ucfirst($res[4]) == 'B') {
1003 $replace = base64_decode($res[5]);
1005 $replace = str_replace('_', ' ', $res[5]);
1006 $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))',
1008 /* Only encode into entities by default. Some places
1009 don't need the encoding, like the compose form. */
1011 $replace = charset_decode($res[3], $replace);
1014 $string = $prefix . $replace . substr($string, strlen($res[0]));
1015 $i = strlen($prefix) +
strlen($replace);
1021 * Encode a string according to RFC 1522 for use in headers if it
1022 * contains 8-bit characters or anything that looks like it should
1025 function encodeHeader ($string) {
1026 global $default_charset;
1028 // Encode only if the string contains 8-bit characters or =?
1029 $j = strlen( $string );
1030 $l = strstr($string, '=?'); // Must be encoded ?
1032 for( $i=0; $i < $j; ++
$i) {
1033 switch( $string{$i} ) {
1047 $k = ord( $string{$i} );
1049 $ret .= sprintf("=%02X", $k);
1052 $ret .= $string{$i};
1057 $string = "=?$default_charset?Q?$ret?=";
1064 Strips dangerous tags from html messages.
1066 function MagicHTML( $body, $id ) {
1068 global $message, $HTTP_SERVER_VARS,
1069 $attachment_common_show_images;
1071 $attachment_common_show_images =
1072 FALSE; // Don't display attached images in HTML mode
1073 $j = strlen( $body ); // Legnth of the HTML
1074 $ret = ''; // Returned string
1075 $bgcolor = '#ffffff'; // Background style color (defaults to white)
1076 $textcolor = '#000000'; // Foreground style color (defaults to black)
1077 $leftmargin = ''; // Left margin style
1078 $title = ''; // HTML title if any
1082 if ( $body{$i} == '<' ) {
1085 while ($body{$pos} == ' ' ||
$body{$pos} == "\t" ||
1086 $body{$pos} == "\n" ) {
1089 while (strlen($tag) < 4 && $body{$pos} != ' ' &&
1090 $body{$pos} != "\t" && $body{$pos} != "\n" &&
1092 $tag .= $body{$pos};
1096 A comment in HTML is only three characters and isn't
1097 guaranteed to have a space after it. This fudges so
1098 it will be caught by the switch statement.
1100 if (ereg("!--", $tag)) {
1103 switch( strtoupper( $tag ) ) {
1104 // Strips the entire tag and contents
1111 while ( $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4}.$body{$i+
5} <> $etg &&
1113 while ( $i < $j && $body{++
$i} <> '>' );
1114 // $ret .= "<!-- $tag removed -->";
1119 while ( $body{$i} <> '>' && // </title>
1124 while ( $body{$i} <> '<' && // </title>
1126 $title .= $body{$i};
1131 // Destroy these tags
1142 while ( $body{$i} <> '>' &&
1149 while ( $body{$i} <> '>' && // </title>
1153 // We parse the style to look for interesting stuff
1155 while ( $body{$i} <> '>' &&
1157 // First we get the name of the style
1159 while ( $body{$i} <> '>' &&
1163 if ( isnoSep( $body{$i} ) )
1164 $style .= $body{$i};
1167 stripComments( $i, $j, $body );
1168 $style = strtoupper( trim( $style ) );
1169 if ( $style == 'BODY' ) {
1170 // Next we look into the definitions of the body style
1171 while ( $body{$i} <> '>' &&
1174 // We look for the background color if any.
1175 if ( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
1177 $bgcolor = getStyleData( $i, $j, $body );
1178 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
1180 $leftmargin = getStyleData( $i, $j, $body );
1185 // Other style are mantained
1186 $styleblk .= "$style ";
1187 while ( $body{$i} <> '>' &&
1191 $styleblk .= $body{$i};
1194 $styleblk .= $body{$i};
1196 stripComments( $i, $j, $body );
1197 if ( $body{$i} <> '>' )
1200 if ( $styleblk <> '' )
1201 $ret .= "<style>$styleblk";
1205 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
1208 if (! isset($base)) {
1211 $ret .= stripEvent( $i, $j, $body, $id, $base );
1212 $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
1213 if ( $leftmargin <> '' )
1214 $ret .= "<td width=$leftmargin> </td>";
1216 if (strtolower($bgcolor) == 'ffffff' ||
1217 strtolower($bgcolor) == '#ffffff')
1218 $ret .= '<font color=#000000>';
1223 if ( strncasecmp($body{$i}, 'font', 4) ) {
1225 while ( !isNoSep( $body{$i} ) && $i < $j ) {
1228 while ( $body{$i} <> '>' && $i < $j ) {
1232 $ret .= "<BASEFONT $base>\n";
1236 while ( !isNoSep( $body{$i} ) &&
1240 if ( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
1242 while ( !isNoSep( $body{$i} ) &&
1246 while ( $body{$i} <> '>' &&
1248 if ( $body{$i} <> '"' ) {
1253 // Debuging $ret .= "<!-- base == $base -->";
1254 if ( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 ) {
1255 $ret .= "\n<BASE HREF=\"$base\">\n";
1260 $ret .= '</font></td></tr></TABLE>';
1264 // Following tags can contain some event handler, lets search it
1265 stripComments( $i, $j, $body );
1266 if (! isset($base)) {
1269 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
1270 // $ret .= "<!-- $tag detected -->";
1278 return( "\n\n<!-- HTML Output ahead -->\n" .
1280 /* Base is illegal within HTML
1281 "\n<!-- END of HTML Output --><base href=\"".
1282 get_location() . '/'.
1285 "\n<!-- END of HTML Output -->\n\n" );
1288 function isNoSep( $char ) {
1306 The following function is usefull to remove extra data that can cause
1307 html not to display properly. Especialy with MS stuff.
1310 function stripComments( &$i, $j, &$body ) {
1312 while ( $body{$i}.$body{$i+
1}.$body{$i+
2}.$body{$i+
3} == '<!--' &&
1315 while ( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1325 /* Gets the style data of a specific style */
1327 function getStyleData( &$i, $j, &$body ) {
1330 while ( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1334 // And get the color
1336 while ( isNoSep( $body{$i} ) &&
1346 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1347 change on with no (onload -> noload)
1350 function stripEvent( &$i, $j, &$body, $id, $base ) {
1352 global $message, $base_uri, $has_unsafe_images, $view_unsafe_images;
1356 while ( $body{$i} <> '>' &&
1358 $etg = strtolower($body{$i}.$body{$i+
1}.$body{$i+
2});
1361 // This is probably a src specification
1363 while( !isNoSep( $body{$k} )) {
1366 if ( $body{$k} == '=' ) {
1369 while( !isNoSep( $body{$k} ) &&
1374 while ( $body{$k} <> '>' && isNoSep( $body{$k} ) &&
1380 while( !isNoSep( $body{$k} ) &&
1385 if ( strtolower( substr( $src, 0, 4 ) ) == 'cid:' ) {
1386 $src = substr( $src, 4 );
1387 $src = "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1388 urlencode( $message->header
->mailbox
) .
1389 "&passed_ent_id=" . find_ent_id( $src, $message );
1390 } else if ( strtolower( substr( $src, 0, 4 ) ) <> 'http' ||
1391 stristr( $src, $base_uri ) ) {
1392 /* Javascript and local urls goes out */
1393 if (!$view_unsafe_images) {
1394 $src = '../images/' . _("sec_remove_eng.png");
1396 $has_unsafe_images = 1;
1398 $ret .= 'src="' . $src . '" ';
1407 // Retrolinks are not allowed without a base because they mess with SM security
1408 if ( $base == '' ) {
1417 if ( $body{$i+
3} == ':') {
1420 while ( isNoSep( $body{$i} ) &&
1422 $name .= $body{$i++
};
1424 if ( $name <> '' ) {
1425 $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1426 urlencode( $message->header
->mailbox
) .
1427 "&passed_ent_id=" . find_ent_id( $name, $message );
1428 if ( $body{$k} == '"' )
1433 if ( $body{$i} == '>' )
1445 if ( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+
1}.$body{$i+
2}, 'script:') == 0 ) {
1461 /* This function trys to locate the entity_id of a specific mime element */
1463 function find_ent_id( $id, $message ) {
1465 for ($i=0; $ret == '' && $i < count($message->entities
); $i++
) {
1466 if ( $message->entities
[$i]->header
->entity_id
== '' ||
$message->entities
[$i]->header
->type
) {
1467 $ret = find_ent_id( $id, $message->entities
[$i] );
1469 if ( strcasecmp( $message->entities
[$i]->header
->id
, $id ) == 0 )
1470 $ret = $message->entities
[$i]->header
->entity_id
;