05a340ab39466dbe8871edfefd3b66736e67b6f4
6 * Copyright (c) 1999-2001 The Squirrelmail Development 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 **/
21 /** msg_header contains generic variables for values that **/
22 /** could be in a header. **/
24 var $type0 = '', $type1 = '', $boundary = '', $charset = '';
25 var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
26 var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
27 var $id = 0, $mailbox = '', $description = '', $filename = '';
28 var $entity_id = 0, $message_id = 0, $name = '';
29 // var $priority = "";
33 /** message is the object that contains messages. It is a recursive
34 object in that through the $entities variable, it can contain
35 more objects of type message. See documentation in mime.txt for
36 a better description of how this works.
39 var $entities = array();
41 function addEntity ($msg) {
42 $this->entities
[] = $msg;
46 /* --------------------------------------------------------------------------------- */
48 /* --------------------------------------------------------------------------------- */
50 // This function gets the structure of a message and stores it in the "message" class.
51 // It will return this object for use with all relevant header information and
52 // fully parsed into the standard "message" object format.
53 function mime_structure ($imap_stream, $header) {
55 sqimap_messages_flag ($imap_stream, $header->id
, $header->id
, 'Seen');
56 $ssid = sqimap_session_id();
57 $lsid = strlen( $ssid );
59 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
61 // This should use sqimap_read_data instead of reading it itself
63 $read = fgets ($imap_stream, 10000);
65 while( substr($read, 0, $lsid) <> $ssid &&
66 !feof( $imap_stream ) ) {
67 $bodystructure .= $read;
68 $read = fgets ($imap_stream, 10000);
70 $read = $bodystructure;
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);
82 $msg = mime_parse_structure ($read, 0);
83 $msg->header
= $header;
87 // this starts the parsing of a particular structure. It is called recursively,
88 // so it can be passed different structures. It returns an object of type
90 // First, it checks to see if it is a multipart message. If it is, then it
91 // handles that as it sees is necessary. If it is just a regular entity,
92 // then it parses it and adds the necessary header information (by calling out
93 // to mime_get_elements()
94 function mime_parse_structure ($structure, $ent_id) {
97 if ($structure{0} == '(') {
98 $ent_id = mime_new_element_level($ent_id);
102 $end = mime_match_parenthesis ($start, $structure);
104 $element = substr($structure, $start+
1, ($end - $start)-1);
105 $ent_id = mime_increment_id ($ent_id);
106 $newmsg = mime_parse_structure ($element, $ent_id);
107 $msg->addEntity ($newmsg);
108 } while ($structure{$end+
1} == '(');
110 // parse the elements
111 $msg = mime_get_element ($structure, $msg, $ent_id);
116 // Increments the element ID. An element id can look like any of
117 // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
118 // the last number of the element id, changing 1.2 to 1.3.
119 function mime_increment_id ($id) {
121 if (strpos($id, ".")) {
122 $first = substr($id, 0, strrpos($id, "."));
123 $last = substr($id, strrpos($id, ".")+
1);
125 $new = $first . "." .$last;
133 // See comment for mime_increment_id().
134 // This adds another level on to the entity_id changing 1.3 to 1.3.0
135 // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
136 // before it can be used. I left it this way so as not to have
137 // to make a special case if it is the first entity_id. It
138 // always increments it, and that works fine.
139 function mime_new_element_level ($id) {
150 function mime_get_element (&$structure, $msg, $ent_id) {
153 $msg->header
= new msg_header();
154 $msg->header
->entity_id
= $ent_id;
155 $properties = array();
157 while (strlen($structure) > 0) {
158 $structure = trim($structure);
159 $char = $structure{0};
161 if (strtolower(substr($structure, 0, 3)) == 'nil') {
163 $structure = substr($structure, 3);
164 } else if ($char == '"') {
165 // loop through until we find the matching quote, and return that as a string
168 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
172 $structure = substr($structure, strlen($text) +
2);
173 } else if ($char == '(') {
175 $end = mime_match_parenthesis (0, $structure);
176 $sub = substr($structure, 1, $end-1);
177 $properties = mime_get_props($properties, $sub);
178 $structure = substr($structure, strlen($sub) +
2);
180 // loop through until we find a space or an end parenthesis
182 $char = $structure{$pos};
184 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
187 $char = $structure{$pos};
189 $structure = substr($structure, strlen($text));
192 // This is where all the text parts get put into the header
195 $msg->header
->type0
= strtolower($text);
198 $msg->header
->type1
= strtolower($text);
201 // Invisimail enclose images with <>
202 $msg->header
->id
= str_replace( '<', '', str_replace( '>', '', $text ) );
205 $msg->header
->description
= $text;
208 $msg->header
->encoding
= strtolower($text);
211 $msg->header
->size
= $text;
214 if ($msg->header
->type0
== 'text' && $elem_num == 8) {
215 // This is a plain text message, so lets get the number of lines
217 $msg->header
->num_lines
= $text;
219 } else if ($msg->header
->type0
== 'message' && $msg->header
->type1
== 'rfc822' && $elem_num == 8) {
220 // This is an encapsulated message, so lets start all over again and
221 // parse this message adding it on to the existing one.
222 $structure = trim($structure);
223 if ( $structure{0} == '(' ) {
224 $e = mime_match_parenthesis (0, $structure);
225 $structure = substr($structure, 0, $e);
226 $structure = substr($structure, 1);
227 $m = mime_parse_structure($structure, $msg->header
->entity_id
);
229 // the following conditional is there to correct a bug that wasn't
230 // incrementing the entity IDs correctly because of the special case
231 // that message/rfc822 is. This fixes it fine.
232 if (substr($structure, 1, 1) != '(')
233 $m->header
->entity_id
= mime_increment_id(mime_new_element_level($ent_id));
235 // Now we'll go through and reformat the results.
237 for ($i=0; $i < count($m->entities
); $i++
) {
238 $msg->addEntity($m->entities
[$i]);
251 // loop through the additional properties and put those in the various headers
252 if ($msg->header
->type0
!= 'message') {
253 for ($i=0; $i < count($properties); $i++
) {
254 $msg->header
->{$properties[$i]['name']} = $properties[$i]['value'];
261 // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
262 // figure out how to do this part, so I decided to go to bed. I woke up
263 // in the morning and had a flash of insight. I went to the white-board
264 // and scribbled it out, then spent a bit programming it, and this is the
265 // result. Nothing complicated, but I think my brain was fried yesterday.
266 // Funny how that happens some times.
268 // This gets properties in a nested parenthesisized list. For example,
269 // this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
270 // This returns an array called $props with all paired up properties.
271 // It ignores the "attachment" for now, maybe that should change later
272 // down the road. In this case, what is returned is:
273 // $props[0]["name"] = "filename";
274 // $props[0]["value"] = "luke.tar.gz";
275 function mime_get_props ($props, $structure) {
277 while (strlen($structure) > 0) {
278 $structure = trim($structure);
279 $char = $structure{0};
284 while ( ( $char = $structure{$pos} ) != '"' &&
285 $pos < strlen($structure)) {
289 $structure = trim(substr($structure, strlen($tmp) +
2));
290 $char = $structure{0};
295 while ( ( $char = $structure{$pos} ) != '"' &&
296 $pos < strlen($structure) ) {
300 $structure = trim(substr($structure, strlen($tmp) +
2));
303 $props[$k]['name'] = strtolower($tmp);
304 $props[$k]['value'] = $value;
305 } else if ($char == '(') {
306 $end = mime_match_parenthesis (0, $structure);
307 $sub = substr($structure, 1, $end-1);
310 $props = mime_get_props($props, $sub);
311 $structure = substr($structure, strlen($sub) +
2);
314 } else if ($char == '(') {
315 $end = mime_match_parenthesis (0, $structure);
316 $sub = substr($structure, 1, $end-1);
317 $props = mime_get_props($props, $sub);
318 $structure = substr($structure, strlen($sub) +
2);
326 // Matches parenthesis. It will return the position of the matching
327 // parenthesis in $structure. For instance, if $structure was:
328 // ("text" "plain" ("val1name", "1") nil ... )
330 // then this would return 42 to match up those two.
331 function mime_match_parenthesis ($pos, $structure) {
333 $j = strlen( $structure );
335 // ignore all extra characters
336 // If inside of a string, skip string -- Boundary IDs and other
337 // things can have ) in them.
338 if( $structure{$pos} != '(' )
343 if ($structure{$pos} == ')') {
345 } elseif ($structure{$pos} == '"') {
347 while( $structure{$pos} != '"' &&
349 if (substr($structure, $pos, 2) == '\\"')
351 elseif (substr($structure, $pos, 2) == '\\\\')
355 } elseif ( $structure{$pos} == '(' ) {
356 $pos = mime_match_parenthesis ($pos, $structure);
359 echo "Error decoding mime structure. Report this as a bug!<br>\n";
363 function mime_fetch_body ($imap_stream, $id, $ent_id ) {
364 // do a bit of error correction. If we couldn't find the entity id, just guess
365 // that it is the first one. That is usually the case anyway.
368 $sid = sqimap_session_id();
369 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
370 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
371 $topline = array_shift($data);
372 while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
373 $topline = array_shift($data);
374 $wholemessage = implode('', $data);
375 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
376 $ret = substr( $wholemessage, 0, $regs[1] );
378 There is some information in the content info header that could be important
379 in order to parse html messages. Let's get them here.
381 if( $ret{0} == '<' ) {
382 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id.MIME]\r\n");
383 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
386 foreach( $data as $d ) {
387 if( substr( $d, 0, 13 ) == 'Content-Base:' ) {
392 ( !isNoSep( $d{$i} ) ||
$d{$i} == '"' ) )
395 if( isNoSep( $d{$i} ) )
400 } elseif( $k == 1 && !isnosep( $d{0} ) ) {
401 $base .= substr( $d, 1 );
406 $ret = "<base href=\"$base\">" . $ret;
408 } else if (ereg('"([^"]*)"', $topline, $regs)) {
411 global $where, $what, $mailbox, $passed_id, $startMessage;
412 $par = "mailbox=".urlencode($mailbox)."&passed_id=$passed_id";
413 if (isset($where) && isset($what)) {
414 $par .= "&where=".urlencode($where)."&what=".urlencode($what);
416 $par .= "&startMessage=$startMessage&show_more=0";
418 $par .= '&response='.urlencode($response).'&message='.urlencode($message).
419 '&topline='.urlencode($topline);
421 echo '<b><font color=$color[2]>Body retrieval error. The reason for this is most probably that<BR> ' .
422 'the message is malformed. Please help us making future versions<BR> ' .
423 "better by submitting this message to the developers knowledgebase!<BR>\n" .
424 "<A HREF=\"../src/retrievalerror.php?$par\">Submit message</A><BR>" .
426 "<tt>Response: $response<BR>" .
427 "Message: $message<BR>" .
428 "FETCH line: $topline<BR></tt></font></b>";
430 fputs ($imap_stream, "$sid FETCH $passed_id BODY[]\r\n");
431 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
433 $wholemessage = implode('', $data);
435 $ret = "---------------\n$wholemessage";
441 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
442 // do a bit of error correction. If we couldn't find the entity id, just guess
443 // that it is the first one. That is usually the case anyway.
444 if (!$ent_id) $ent_id = 1;
445 $sid = sqimap_session_id();
446 // Don't kill the connection if the browser is over a dialup
447 // and it would take over 30 seconds to download it.
450 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
453 $read = fgets ($imap_stream,4096);
454 // This could be bad -- if the section has sqimap_session_id() . ' OK'
455 // or similar, it will kill the download.
456 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
457 if (trim($read) == ')==') {
459 $read = fgets ($imap_stream,4096);
460 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
463 echo decodeBody($read1, $encoding) .
464 decodeBody($read, $encoding);
467 echo decodeBody($read, $encoding);
469 $read = fgets ($imap_stream,4096);
474 /* -[ END MIME DECODING ]----------------------------------------------------------- */
478 /** This is the first function called. It decides if this is a multipart
479 message or if it should be handled as a single entity
481 function decodeMime ($imap_stream, &$header) {
482 global $username, $key, $imapServerAddress, $imapPort;
483 return mime_structure ($imap_stream, $header);
486 // This is here for debugging purposese. It will print out a list
487 // of all the entity IDs that are in the $message object.
489 function listEntities ($message) {
491 if ($message->header->entity_id)
492 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
493 for ($i = 0; $message->entities[$i]; $i++) {
494 $msg = listEntities($message->entities[$i], $ent_id);
502 // returns a $message object for a particular entity id
503 function getEntity ($message, $ent_id) {
505 if ($message->header
->entity_id
== $ent_id && strlen($ent_id) == strlen($message->header
->entity_id
)) {
508 for ($i = 0; isset($message->entities
[$i]); $i++
) {
509 $msg = getEntity ($message->entities
[$i], $ent_id);
517 // figures out what entity to display and returns the $message object
519 function findDisplayEntity ($message, $textOnly = 1) {
520 global $show_html_default;
525 if ( $message->header
->type0
== 'multipart' &&
526 ( $message->header
->type1
== 'alternative' ||
527 $message->header
->type1
== 'related' ) &&
528 $show_html_default && ! $textOnly ) {
529 $entity = findDisplayEntityHTML($message);
532 // Show text/plain or text/html -- the first one we find.
534 $message->header
->type0
== 'text' &&
535 ( $message->header
->type1
== 'plain' ||
536 $message->header
->type1
== 'html' ) &&
537 isset($message->header
->entity_id
) ) {
538 $entity = $message->header
->entity_id
;
542 while ($entity == 0 && isset($message->entities
[$i]) ) {
543 $entity = findDisplayEntity($message->entities
[$i], $textOnly);
551 // Shows the HTML version
552 function findDisplayEntityHTML ($message) {
553 if ($message->header
->type0
== 'text' &&
554 $message->header
->type1
== 'html' &&
555 isset($message->header
->entity_id
))
556 return $message->header
->entity_id
;
557 for ($i = 0; isset($message->entities
[$i]); $i ++
) {
558 $entity = findDisplayEntityHTML($message->entities
[$i]);
565 /** This returns a parsed string called $body. That string can then
566 be displayed as the actual message in the HTML. It contains
567 everything needed, including HTML Tags, Attachments at the
570 function formatBody($imap_stream, $message, $color, $wrap_at) {
571 // this if statement checks for the entity to show as the
572 // primary message. To add more of them, just put them in the
573 // order that is their priority.
574 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
577 $id = $message->header
->id
;
578 $urlmailbox = urlencode($message->header
->mailbox
);
580 // Get the right entity and redefine message to be this entity
581 // Pass the 0 to mean that we want the 'best' viewable one
582 $ent_num = findDisplayEntity ($message, 0);
583 $body_message = getEntity($message, $ent_num);
584 if (($body_message->header
->type0
== 'text') ||
585 ($body_message->header
->type0
== 'rfc822')) {
587 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
588 $body = decodeBody($body, $body_message->header
->encoding
);
589 $hookResults = do_hook("message_body", $body);
590 $body = $hookResults[1];
592 // If there are other types that shouldn't be formatted, add
594 if ($body_message->header
->type1
== 'html') {
595 if( $show_html_default <> 1 ) {
596 $body = strip_tags( $body );
597 translateText($body, $wrap_at, $body_message->header
->charset
);
599 $body = MagicHTML( $body, $id );
602 translateText($body, $wrap_at, $body_message->header
->charset
);
605 $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>";
607 /** Display the ATTACHMENTS: message if there's more than one part **/
608 $body .= "</TD></TR></TABLE>";
609 if (isset($message->entities
[0])) {
610 $body .= formatAttachments ($message, $ent_num, $message->header
->mailbox
, $id);
612 $body .= "</TD></TR></TABLE>";
614 $body = formatAttachments ($message, -1, $message->header
->mailbox
, $id);
619 // A recursive function that returns a list of attachments with links
620 // to where to download these attachments
621 function formatAttachments ($message, $ent_id, $mailbox, $id) {
622 global $where, $what;
623 global $startMessage, $color;
624 static $ShownHTML = 0;
627 if ($ShownHTML == 0) {
630 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
631 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
632 _("Attachments") . ':' .
633 "</B></TH></TR><TR><TD>\n" .
634 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
635 formatAttachments ($message, $ent_id, $mailbox, $id) .
636 "</TABLE></TD></TR></TABLE>";
642 if (!$message->entities
) {
643 $type0 = strtolower($message->header
->type0
);
644 $type1 = strtolower($message->header
->type1
);
645 $name = decodeHeader($message->header
->name
);
647 if ($message->header
->entity_id
!= $ent_id) {
648 $filename = decodeHeader($message->header
->filename
);
649 if (trim($filename) == '') {
650 if (trim($name) == '') {
651 if( trim( $message->header
->id
) == '' )
652 $display_filename = 'untitled-[' . $message->header
->entity_id
. ']' ;
654 $display_filename = 'cid: ' . $message->header
->id
;
655 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
657 $display_filename = $name;
661 $display_filename = $filename;
664 $urlMailbox = urlencode($mailbox);
665 $ent = urlencode($message->header
->entity_id
);
668 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
670 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
671 $Links['download link']['text'] = _("download");
672 $Links['download link']['href'] =
673 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
676 $HookResults = do_hook("attachment $type0/$type1", $Links,
677 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
678 $display_filename, $where, $what);
680 $Links = $HookResults[1];
681 $DefaultLink = $HookResults[6];
683 $body .= '<TR><TD> </TD><TD>' .
684 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
685 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
686 '</b> </small></TD>' .
687 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
689 if ($message->header
->description
)
690 $body .= '<b>' . htmlspecialchars($message->header
->description
) . '</b>';
691 $body .= '</SMALL></TD><TD><SMALL> ';
695 foreach ($Links as $Val) {
699 $body .= ' | ';
701 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
706 $body .= "</SMALL></TD></TR>\n";
709 for ($i = 0; $i < count($message->entities
); $i++
) {
710 $body .= formatAttachments ($message->entities
[$i], $ent_id, $mailbox, $id);
718 /** this function decodes the body depending on the encoding type. **/
719 function decodeBody($body, $encoding) {
720 $body = str_replace("\r\n", "\n", $body);
721 $encoding = strtolower($encoding);
723 global $show_html_default;
725 if ($encoding == 'quoted-printable') {
726 $body = quoted_printable_decode($body);
730 Following code has been comented as I see no reason for it.
731 If there is any please tell me a mingo@rotedic.com
733 while (ereg("=\n", $body))
734 $body = ereg_replace ("=\n", "", $body);
736 } else if ($encoding == 'base64') {
737 $body = base64_decode($body);
740 // All other encodings are returned raw.
745 // This functions decode strings that is encoded according to
746 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
747 function decodeHeader ($string) {
748 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
750 if (ucfirst($res[2]) == "B") {
751 $replace = base64_decode($res[3]);
753 $replace = ereg_replace("_", " ", $res[3]);
754 // Convert lowercase Quoted Printable to uppercase for
755 // quoted_printable_decode to understand it.
756 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", $replace, $res)) {
757 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
759 $replace = quoted_printable_decode($replace);
762 $replace = charset_decode ($res[1], $replace);
764 // Remove the name of the character set.
765 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
768 // In case there should be more encoding in the string: recurse
769 return (decodeHeader($string));
774 // Encode a string according to RFC 1522 for use in headers if it
775 // contains 8-bit characters or anything that looks like it should
777 function encodeHeader ($string) {
778 global $default_charset;
780 // Encode only if the string contains 8-bit characters or =?
781 $j = strlen( $string );
782 $l = FALSE; // Must be encoded ?
784 for( $i=0; $i < $j; ++
$i) {
785 switch( $string{$i} ) {
800 $k = ord( $string{$i} );
802 $ret .= sprintf("=%02X", $k);
810 $string = "=?$default_charset?Q?$ret?=";
816 Strips dangerous tags from html messages.
819 function MagicHTML( $body, $id ) {
821 global $message, $PHP_SELF, $HTTP_SERVER_VARS;
823 $j = strlen( $body ); // Legnth of the HTML
824 $ret = ''; // Returned string
825 $bgcolor = '#ffffff'; // Background style color (defaults to white)
826 $textcolor = '#000000'; // Foreground style color (defaults to black)
827 $leftmargin = ''; // Left margin style
828 $title = ''; // HTML title if any
832 if( $body{$i} == '<' ) {
835 while ($body{$pos} == ' ' ||
$body{$pos} == "\t" ||
838 while (strlen($tag) < 4 && $body{$pos} != ' ' &&
839 $body{$pos} != "\t" && $body{$pos} != "\n") {
843 switch( strtoupper( $tag ) ) {
844 // Strips the entire tag and contents
851 while( $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4}.$body{$i+
5} <> $etg &&
853 while( $i < $j && $body{++
$i} <> '>' );
854 // $ret .= "<!-- $tag removed -->";
859 while( $body{$i} <> '>' && // </title>
864 while( $body{$i} <> '<' && // </title>
871 // Destroy these tags
882 while( $body{$i} <> '>' &&
889 while( $body{$i} <> '>' && // </title>
893 // We parse the style to look for interesting stuff
895 while( $body{$i} <> '>' &&
897 // First we get the name of the style
899 while( $body{$i} <> '>' &&
903 if( isnoSep( $body{$i} ) )
907 stripComments( $i, $j, $body );
908 $style = strtoupper( trim( $style ) );
909 if( $style == 'BODY' ) {
910 // Next we look into the definitions of the body style
911 while( $body{$i} <> '>' &&
914 // We look for the background color if any.
915 if( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
917 $bgcolor = getStyleData( $i, $j, $body );
918 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
920 $leftmargin = getStyleData( $i, $j, $body );
925 // Other style are mantained
926 $styleblk .= "$style ";
927 while( $body{$i} <> '>' &&
931 $styleblk .= $body{$i};
934 $styleblk .= $body{$i};
936 stripComments( $i, $j, $body );
937 if( $body{$i} <> '>' )
940 if( $styleblk <> '' )
941 $ret .= "<style>$styleblk";
945 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
950 $ret .= stripEvent( $i, $j, $body, $id, $base );
951 $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
952 if( $leftmargin <> '' )
953 $ret .= "<td width=$leftmargin> </td>";
955 if (strtolower($bgcolor) == 'ffffff' ||
956 strtolower($bgcolor) == '#ffffff')
957 $ret .= '<font color=#000000>';
962 while( !isNoSep( $body{$i} ) &&
965 if( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
967 while( !isNoSep( $body{$i} ) &&
970 while( $body{$i} <> '>' &&
972 if( $body{$i} <> '"' )
976 // Debuging $ret .= "<!-- base == $base -->";
977 if( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 )
978 $ret .= "\n<BASE HREF=\"$base\">\n";
982 $ret .= '</font></td></tr></TABLE>';
986 // Following tags can contain some event handler, lets search it
987 stripComments( $i, $j, $body );
990 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
991 // $ret .= "<!-- $tag detected -->";
999 return( "\n\n<!-- HTML Output ahead -->\n" .
1001 "\n<!-- END of HTML Output --><base href=\"".
1002 $HTTP_SERVER_VARS["SERVER_NAME"] . substr( $PHP_SELF, 0, strlen( $PHP_SELF ) - 13 ) .
1006 function isNoSep( $char ) {
1024 The following function is usefull to remove extra data that can cause
1025 html not to display properly. Especialy with MS stuff.
1028 function stripComments( &$i, $j, &$body ) {
1030 while( $body{$i}.$body{$i+
1}.$body{$i+
2}.$body{$i+
3} == '<!--' &&
1033 while( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1043 /* Gets the style data of a specific style */
1045 function getStyleData( &$i, $j, &$body ) {
1048 while( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1052 // And get the color
1054 while( isNoSep( $body{$i} ) &&
1064 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1065 change on with no (onload -> noload)
1068 function stripEvent( &$i, $j, &$body, $id, $base ) {
1074 while( $body{$i} <> '>' &&
1076 $etg = strtolower($body{$i}.$body{$i+
1}.$body{$i+
2});
1079 // Retrolinks are not allowed without a base because they mess with SM security
1089 if( $body{$i+
3} == ':') {
1092 while( isNoSep( $body{$i} ) &&
1094 $name .= $body{$i++
};
1096 $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1097 urlencode( $message->header
->mailbox
) .
1098 "&passed_ent_id=" . find_ent_id( $name, $message );
1099 if( $body{$k} == '"' )
1104 if( $body{$i} == '>' )
1116 if( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+
1}.$body{$i+
2}, 'script:') == 0 ) {
1132 /* This function trys to locate the entity_id of a specific mime element */
1134 function find_ent_id( $id, $message ) {
1137 for ($i=0; $ret == '' && $i < count($message->entities
); $i++
) {
1139 if( $message->entities
[$i]->header
->entity_id
== '' ) {
1140 $ret = find_ent_id( $id, $message->entities
[$i] );
1142 if( strcasecmp( $message->entities
[$i]->header
->id
, $id ) == 0 )
1143 $ret = $message->entities
[$i]->header
->entity_id
;