4 ** This contains the functions necessary to detect and decode MIME
10 if (defined('mime_php'))
12 define('mime_php', true);
14 require_once('../functions/imap.php');
15 require_once('../functions/attachment_common.php');
17 /** 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 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 = "";
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.
38 var $entities = array();
40 function addEntity ($msg) {
41 $this->entities
[] = $msg;
45 /* --------------------------------------------------------------------------------- */
47 /* --------------------------------------------------------------------------------- */
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 function mime_structure ($imap_stream, $header) {
54 sqimap_messages_flag ($imap_stream, $header->id
, $header->id
, 'Seen');
55 $ssid = sqimap_session_id();
56 $lsid = strlen( $ssid );
58 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
60 // This should use sqimap_read_data instead of reading it itself
62 $read = fgets ($imap_stream, 10000);
64 while( substr($read, 0, $lsid) <> $ssid &&
65 !feof( $imap_stream ) ) {
66 $bodystructure .= $read;
67 $read = fgets ($imap_stream, 10000);
69 $read = $bodystructure;
71 // isolate the body structure and remove beginning and end parenthesis
72 $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') +
13));
73 $read = trim(substr ($read, 0, -1));
74 $end = mime_match_parenthesis(0, $read);
75 while ($end == strlen($read)-1) {
76 $read = trim(substr ($read, 0, -1));
77 $read = trim(substr ($read, 1));
78 $end = mime_match_parenthesis(0, $read);
81 $msg = mime_parse_structure ($read, 0);
82 $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()
93 function mime_parse_structure ($structure, $ent_id) {
96 if ($structure{0} == '(') {
97 $ent_id = mime_new_element_level($ent_id);
101 $end = mime_match_parenthesis ($start, $structure);
103 $element = substr($structure, $start+
1, ($end - $start)-1);
104 $ent_id = mime_increment_id ($ent_id);
105 $newmsg = mime_parse_structure ($element, $ent_id);
106 $msg->addEntity ($newmsg);
107 } while ($structure{$end+
1} == '(');
109 // parse the elements
110 $msg = mime_get_element ($structure, $msg, $ent_id);
115 // Increments the element ID. An element id can look like any of
116 // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
117 // the last number of the element id, changing 1.2 to 1.3.
118 function mime_increment_id ($id) {
120 if (strpos($id, ".")) {
121 $first = substr($id, 0, strrpos($id, "."));
122 $last = substr($id, strrpos($id, ".")+
1);
124 $new = $first . "." .$last;
132 // See comment for mime_increment_id().
133 // This adds another level on to the entity_id changing 1.3 to 1.3.0
134 // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
135 // before it can be used. I left it this way so as not to have
136 // to make a special case if it is the first entity_id. It
137 // always increments it, and that works fine.
138 function mime_new_element_level ($id) {
149 function mime_get_element (&$structure, $msg, $ent_id) {
152 $msg->header
= new msg_header();
153 $msg->header
->entity_id
= $ent_id;
154 $properties = array();
156 while (strlen($structure) > 0) {
157 $structure = trim($structure);
158 $char = $structure{0};
160 if (strtolower(substr($structure, 0, 3)) == 'nil') {
162 $structure = substr($structure, 3);
163 } else if ($char == '"') {
164 // loop through until we find the matching quote, and return that as a string
167 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
171 $structure = substr($structure, strlen($text) +
2);
172 } else if ($char == '(') {
174 $end = mime_match_parenthesis (0, $structure);
175 $sub = substr($structure, 1, $end-1);
176 $properties = mime_get_props($properties, $sub);
177 $structure = substr($structure, strlen($sub) +
2);
179 // loop through until we find a space or an end parenthesis
181 $char = $structure{$pos};
183 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
186 $char = $structure{$pos};
188 $structure = substr($structure, strlen($text));
191 // This is where all the text parts get put into the header
194 $msg->header
->type0
= strtolower($text);
197 $msg->header
->type1
= strtolower($text);
200 // Invisimail enclose images with <>
201 $msg->header
->id
= str_replace( '<', '', str_replace( '>', '', $text ) );
204 $msg->header
->description
= $text;
207 $msg->header
->encoding
= strtolower($text);
210 $msg->header
->size
= $text;
213 if ($msg->header
->type0
== 'text' && $elem_num == 8) {
214 // This is a plain text message, so lets get the number of lines
216 $msg->header
->num_lines
= $text;
218 } else if ($msg->header
->type0
== 'message' && $msg->header
->type1
== 'rfc822' && $elem_num == 8) {
219 // This is an encapsulated message, so lets start all over again and
220 // parse this message adding it on to the existing one.
221 $structure = trim($structure);
222 if ( $structure{0} == '(' ) {
223 $e = mime_match_parenthesis (0, $structure);
224 $structure = substr($structure, 0, $e);
225 $structure = substr($structure, 1);
226 $m = mime_parse_structure($structure, $msg->header
->entity_id
);
228 // the following conditional is there to correct a bug that wasn't
229 // incrementing the entity IDs correctly because of the special case
230 // that message/rfc822 is. This fixes it fine.
231 if (substr($structure, 1, 1) != '(')
232 $m->header
->entity_id
= mime_increment_id(mime_new_element_level($ent_id));
234 // Now we'll go through and reformat the results.
236 for ($i=0; $i < count($m->entities
); $i++
) {
237 $msg->addEntity($m->entities
[$i]);
250 // loop through the additional properties and put those in the various headers
251 if ($msg->header
->type0
!= 'message') {
252 for ($i=0; $i < count($properties); $i++
) {
253 $msg->header
->{$properties[$i]['name']} = $properties[$i]['value'];
260 // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
261 // figure out how to do this part, so I decided to go to bed. I woke up
262 // in the morning and had a flash of insight. I went to the white-board
263 // and scribbled it out, then spent a bit programming it, and this is the
264 // result. Nothing complicated, but I think my brain was fried yesterday.
265 // Funny how that happens some times.
267 // This gets properties in a nested parenthesisized list. For example,
268 // this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
269 // This returns an array called $props with all paired up properties.
270 // It ignores the "attachment" for now, maybe that should change later
271 // down the road. In this case, what is returned is:
272 // $props[0]["name"] = "filename";
273 // $props[0]["value"] = "luke.tar.gz";
274 function mime_get_props ($props, $structure) {
276 while (strlen($structure) > 0) {
277 $structure = trim($structure);
278 $char = $structure{0};
283 while ( ( $char = $structure{$pos} ) != '"' &&
284 $pos < strlen($structure)) {
288 $structure = trim(substr($structure, strlen($tmp) +
2));
289 $char = $structure{0};
294 while ( ( $char = $structure{$pos} ) != '"' &&
295 $pos < strlen($structure) ) {
299 $structure = trim(substr($structure, strlen($tmp) +
2));
302 $props[$k]['name'] = strtolower($tmp);
303 $props[$k]['value'] = $value;
304 } else if ($char == '(') {
305 $end = mime_match_parenthesis (0, $structure);
306 $sub = substr($structure, 1, $end-1);
309 $props = mime_get_props($props, $sub);
310 $structure = substr($structure, strlen($sub) +
2);
313 } else if ($char == '(') {
314 $end = mime_match_parenthesis (0, $structure);
315 $sub = substr($structure, 1, $end-1);
316 $props = mime_get_props($props, $sub);
317 $structure = substr($structure, strlen($sub) +
2);
325 // Matches parenthesis. It will return the position of the matching
326 // parenthesis in $structure. For instance, if $structure was:
327 // ("text" "plain" ("val1name", "1") nil ... )
329 // then this would return 42 to match up those two.
330 function mime_match_parenthesis ($pos, $structure) {
332 $j = strlen( $structure );
334 // ignore all extra characters
335 // If inside of a string, skip string -- Boundary IDs and other
336 // things can have ) in them.
337 if( $structure{$pos} != '(' )
342 if ($structure{$pos} == ')') {
344 } elseif ($structure{$pos} == '"') {
346 while( $structure{$pos} != '"' &&
348 if (substr($structure, $pos, 2) == '\\"')
350 elseif (substr($structure, $pos, 2) == '\\\\')
354 } elseif ( $structure{$pos} == '(' ) {
355 $pos = mime_match_parenthesis ($pos, $structure);
358 echo "Error decoding mime structure. Report this as a bug!<br>\n";
362 function mime_fetch_body ($imap_stream, $id, $ent_id ) {
363 // do a bit of error correction. If we couldn't find the entity id, just guess
364 // that it is the first one. That is usually the case anyway.
367 $sid = sqimap_session_id();
368 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
369 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
370 $topline = array_shift($data);
371 while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
372 $topline = array_shift($data);
373 $wholemessage = implode('', $data);
374 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
375 $ret = substr( $wholemessage, 0, $regs[1] );
377 There is some information in the content info header that could be important
378 in order to parse html messages. Let's get them here.
380 if( $ret{0} == '<' ) {
381 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id.MIME]\r\n");
382 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
385 foreach( $data as $d ) {
386 if( substr( $d, 0, 13 ) == 'Content-Base:' ) {
391 ( !isNoSep( $d{$i} ) ||
$d{$i} == '"' ) )
394 if( isNoSep( $d{$i} ) )
399 } elseif( $k == 1 && !isnosep( $d{0} ) ) {
400 $base .= substr( $d, 1 );
405 $ret = "<base href=\"$base\">" . $ret;
407 } else if (ereg('"([^"]*)"', $topline, $regs)) {
410 global $where, $what, $mailbox, $passed_id, $startMessage;
411 $par = "mailbox=".urlencode($mailbox)."&passed_id=$passed_id";
412 if (isset($where) && isset($what)) {
413 $par .= "&where=".urlencode($where)."&what=".urlencode($what);
415 $par .= "&startMessage=$startMessage&show_more=0";
417 $par .= '&response='.urlencode($response).'&message='.urlencode($message).
418 '&topline='.urlencode($topline);
420 echo '<b><font color=$color[2]>Body retrieval error. The reason for this is most probably that<BR> ' .
421 'the message is malformed. Please help us making future versions<BR> ' .
422 "better by submitting this message to the developers knowledgebase!<BR>\n" .
423 "<A HREF=\"../src/retrievalerror.php?$par\">Submit message</A><BR>" .
425 "<tt>Response: $response<BR>" .
426 "Message: $message<BR>" .
427 "FETCH line: $topline<BR></tt></font></b>";
429 fputs ($imap_stream, "$sid FETCH $passed_id BODY[]\r\n");
430 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
432 $wholemessage = implode('', $data);
434 $ret = "---------------\n$wholemessage";
440 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
441 // do a bit of error correction. If we couldn't find the entity id, just guess
442 // that it is the first one. That is usually the case anyway.
443 if (!$ent_id) $ent_id = 1;
444 $sid = sqimap_session_id();
445 // Don't kill the connection if the browser is over a dialup
446 // and it would take over 30 seconds to download it.
449 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
452 $read = fgets ($imap_stream,4096);
453 // This could be bad -- if the section has sqimap_session_id() . ' OK'
454 // or similar, it will kill the download.
455 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
456 if (trim($read) == ')==') {
458 $read = fgets ($imap_stream,4096);
459 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
462 echo decodeBody($read1, $encoding) .
463 decodeBody($read, $encoding);
466 echo decodeBody($read, $encoding);
468 $read = fgets ($imap_stream,4096);
473 /* -[ END MIME DECODING ]----------------------------------------------------------- */
477 /** This is the first function called. It decides if this is a multipart
478 message or if it should be handled as a single entity
480 function decodeMime ($imap_stream, &$header) {
481 global $username, $key, $imapServerAddress, $imapPort;
482 return mime_structure ($imap_stream, $header);
485 // This is here for debugging purposese. It will print out a list
486 // of all the entity IDs that are in the $message object.
488 function listEntities ($message) {
490 if ($message->header->entity_id)
491 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
492 for ($i = 0; $message->entities[$i]; $i++) {
493 $msg = listEntities($message->entities[$i], $ent_id);
501 // returns a $message object for a particular entity id
502 function getEntity ($message, $ent_id) {
504 if ($message->header
->entity_id
== $ent_id && strlen($ent_id) == strlen($message->header
->entity_id
)) {
507 for ($i = 0; isset($message->entities
[$i]); $i++
) {
508 $msg = getEntity ($message->entities
[$i], $ent_id);
516 // figures out what entity to display and returns the $message object
518 function findDisplayEntity ($message, $textOnly = 1) {
519 global $show_html_default;
524 if ( $message->header
->type0
== 'multipart' &&
525 ( $message->header
->type1
== 'alternative' ||
526 $message->header
->type1
== 'related' ) &&
527 $show_html_default && ! $textOnly ) {
528 $entity = findDisplayEntityHTML($message);
531 // Show text/plain or text/html -- the first one we find.
533 $message->header
->type0
== 'text' &&
534 ( $message->header
->type1
== 'plain' ||
535 $message->header
->type1
== 'html' ) &&
536 isset($message->header
->entity_id
) ) {
537 $entity = $message->header
->entity_id
;
541 while ($entity == 0 && isset($message->entities
[$i]) ) {
542 $entity = findDisplayEntity($message->entities
[$i], $textOnly);
550 // Shows the HTML version
551 function findDisplayEntityHTML ($message) {
552 if ($message->header
->type0
== 'text' &&
553 $message->header
->type1
== 'html' &&
554 isset($message->header
->entity_id
))
555 return $message->header
->entity_id
;
556 for ($i = 0; isset($message->entities
[$i]); $i ++
) {
557 $entity = findDisplayEntityHTML($message->entities
[$i]);
564 /** This returns a parsed string called $body. That string can then
565 be displayed as the actual message in the HTML. It contains
566 everything needed, including HTML Tags, Attachments at the
569 function formatBody($imap_stream, $message, $color, $wrap_at) {
570 // this if statement checks for the entity to show as the
571 // primary message. To add more of them, just put them in the
572 // order that is their priority.
573 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
576 $id = $message->header
->id
;
577 $urlmailbox = urlencode($message->header
->mailbox
);
579 // Get the right entity and redefine message to be this entity
580 // Pass the 0 to mean that we want the 'best' viewable one
581 $ent_num = findDisplayEntity ($message, 0);
582 $body_message = getEntity($message, $ent_num);
583 if (($body_message->header
->type0
== 'text') ||
584 ($body_message->header
->type0
== 'rfc822')) {
586 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
587 $body = decodeBody($body, $body_message->header
->encoding
);
588 $hookResults = do_hook("message_body", $body);
589 $body = $hookResults[1];
591 // If there are other types that shouldn't be formatted, add
593 if ($body_message->header
->type1
== 'html') {
594 if( $show_html_default <> 1 ) {
595 $body = strip_tags( $body );
596 translateText($body, $wrap_at, $body_message->header
->charset
);
598 $body = MagicHTML( $body, $id );
601 translateText($body, $wrap_at, $body_message->header
->charset
);
604 $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>";
606 /** Display the ATTACHMENTS: message if there's more than one part **/
607 $body .= "</TD></TR></TABLE>";
608 if (isset($message->entities
[0])) {
609 $body .= formatAttachments ($message, $ent_num, $message->header
->mailbox
, $id);
611 $body .= "</TD></TR></TABLE>";
613 $body = formatAttachments ($message, -1, $message->header
->mailbox
, $id);
618 // A recursive function that returns a list of attachments with links
619 // to where to download these attachments
620 function formatAttachments ($message, $ent_id, $mailbox, $id) {
621 global $where, $what;
622 global $startMessage, $color;
623 static $ShownHTML = 0;
626 if ($ShownHTML == 0) {
629 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
630 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
631 _("Attachments") . ':' .
632 "</B></TH></TR><TR><TD>\n" .
633 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
634 formatAttachments ($message, $ent_id, $mailbox, $id) .
635 "</TABLE></TD></TR></TABLE>";
641 if (!$message->entities
) {
642 $type0 = strtolower($message->header
->type0
);
643 $type1 = strtolower($message->header
->type1
);
644 $name = decodeHeader($message->header
->name
);
646 if ($message->header
->entity_id
!= $ent_id) {
647 $filename = decodeHeader($message->header
->filename
);
648 if (trim($filename) == '') {
649 if (trim($name) == '') {
650 if( trim( $message->header
->id
) == '' )
651 $display_filename = 'untitled-[' . $message->header
->entity_id
. ']' ;
653 $display_filename = 'cid: ' . $message->header
->id
;
654 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
656 $display_filename = $name;
660 $display_filename = $filename;
663 $urlMailbox = urlencode($mailbox);
664 $ent = urlencode($message->header
->entity_id
);
667 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
669 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
670 $Links['download link']['text'] = _("download");
671 $Links['download link']['href'] =
672 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
675 $HookResults = do_hook("attachment $type0/$type1", $Links,
676 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
677 $display_filename, $where, $what);
679 $Links = $HookResults[1];
680 $DefaultLink = $HookResults[6];
682 $body .= '<TR><TD> </TD><TD>' .
683 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
684 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
685 '</b> </small></TD>' .
686 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
688 if ($message->header
->description
)
689 $body .= '<b>' . htmlspecialchars($message->header
->description
) . '</b>';
690 $body .= '</SMALL></TD><TD><SMALL> ';
694 foreach ($Links as $Val) {
698 $body .= ' | ';
700 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
705 $body .= "</SMALL></TD></TR>\n";
708 for ($i = 0; $i < count($message->entities
); $i++
) {
709 $body .= formatAttachments ($message->entities
[$i], $ent_id, $mailbox, $id);
717 /** this function decodes the body depending on the encoding type. **/
718 function decodeBody($body, $encoding) {
719 $body = str_replace("\r\n", "\n", $body);
720 $encoding = strtolower($encoding);
722 global $show_html_default;
724 if ($encoding == 'quoted-printable') {
725 $body = quoted_printable_decode($body);
729 Following code has been comented as I see no reason for it.
730 If there is any please tell me a mingo@rotedic.com
732 while (ereg("=\n", $body))
733 $body = ereg_replace ("=\n", "", $body);
735 } else if ($encoding == 'base64') {
736 $body = base64_decode($body);
739 // All other encodings are returned raw.
744 // This functions decode strings that is encoded according to
745 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
746 function decodeHeader ($string) {
747 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
749 if (ucfirst($res[2]) == "B") {
750 $replace = base64_decode($res[3]);
752 $replace = ereg_replace("_", " ", $res[3]);
753 // Convert lowercase Quoted Printable to uppercase for
754 // quoted_printable_decode to understand it.
755 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", $replace, $res)) {
756 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
758 $replace = quoted_printable_decode($replace);
761 $replace = charset_decode ($res[1], $replace);
763 // Remove the name of the character set.
764 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
767 // In case there should be more encoding in the string: recurse
768 return (decodeHeader($string));
773 // Encode a string according to RFC 1522 for use in headers if it
774 // contains 8-bit characters or anything that looks like it should
776 function encodeHeader ($string) {
777 global $default_charset;
779 // Encode only if the string contains 8-bit characters or =?
780 $j = strlen( $string );
781 $l = FALSE; // Must be encoded ?
783 for( $i=0; $i < $j; ++
$i) {
784 switch( $string{$i} ) {
799 $k = ord( $string{$i} );
801 $ret .= sprintf("=%02X", $k);
809 $string = "=?$default_charset?Q?$ret?=";
815 Strips dangerous tags from html messages.
818 function MagicHTML( $body, $id ) {
820 global $message, $PHP_SELF, $HTTP_SERVER_VARS;
822 $j = strlen( $body ); // Legnth of the HTML
823 $ret = ''; // Returned string
824 $bgcolor = '#ffffff'; // Background style color (defaults to white)
825 $leftmargin = ''; // Left margin style
826 $title = ''; // HTML title if any
830 if( $body{$i} == '<' ) {
831 $tag = $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4};
832 switch( strtoupper( $tag ) ) {
833 // Strips the entire tag and contents
840 while( $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4}.$body{$i+
5} <> $etg &&
842 while( $i < $j && $body{++
$i} <> '>' );
843 // $ret .= "<!-- $tag removed -->";
848 while( $body{$i} <> '>' && // </title>
853 while( $body{$i} <> '<' && // </title>
860 // Destroy these tags
871 while( $body{$i} <> '>' &&
878 while( $body{$i} <> '>' && // </title>
882 // We parse the style to look for interesting stuff
884 while( $body{$i} <> '>' &&
886 // First we get the name of the style
888 while( $body{$i} <> '>' &&
892 if( isnoSep( $body{$i} ) )
896 stripComments( $i, $j, $body );
897 $style = strtoupper( trim( $style ) );
898 if( $style == 'BODY' ) {
899 // Next we look into the definitions of the body style
900 while( $body{$i} <> '>' &&
903 // We look for the background color if any.
904 if( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
906 $bgcolor = getStyleData( $i, $j, $body );
907 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
909 $leftmargin = getStyleData( $i, $j, $body );
914 // Other style are mantained
915 $styleblk .= "$style ";
916 while( $body{$i} <> '>' &&
920 $styleblk .= $body{$i};
923 $styleblk .= $body{$i};
925 stripComments( $i, $j, $body );
926 if( $body{$i} <> '>' )
929 if( $styleblk <> '' )
930 $ret .= "<style>$styleblk";
934 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
939 $ret .= stripEvent( $i, $j, $body, $id, $base );
940 //if( $bgcolor <> '' )
941 $ret .= " bgcolor=$bgcolor";
942 $ret .= ' width=100%><tr>';
943 if( $leftmargin <> '' )
944 $ret .= "<td width=$leftmargin> </td>";
950 while( !isNoSep( $body{$i} ) &&
953 if( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
955 while( !isNoSep( $body{$i} ) &&
958 while( $body{$i} <> '>' &&
960 if( $body{$i} <> '"' )
964 // Debuging $ret .= "<!-- base == $base -->";
965 if( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 )
966 $ret .= "\n<BASE HREF=\"$base\">\n";
970 $ret .= '</td></tr></TABLE>';
974 // Following tags can contain some event handler, lets search it
975 stripComments( $i, $j, $body );
978 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
979 // $ret .= "<!-- $tag detected -->";
987 return( "\n\n<!-- HTML Output ahead -->\n" .
989 "\n<!-- END of HTML Output --><base href=\"".
990 $HTTP_SERVER_VARS["SERVER_NAME"] . substr( $PHP_SELF, 0, strlen( $PHP_SELF ) - 13 ) .
994 function isNoSep( $char ) {
1012 The following function is usefull to remove extra data that can cause
1013 html not to display properly. Especialy with MS stuff.
1016 function stripComments( &$i, $j, &$body ) {
1018 while( $body{$i}.$body{$i+
1}.$body{$i+
2}.$body{$i+
3} == '<!--' &&
1021 while( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1031 /* Gets the style data of a specific style */
1033 function getStyleData( &$i, $j, &$body ) {
1036 while( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1040 // And get the color
1042 while( isNoSep( $body{$i} ) &&
1052 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1053 change on with no (onload -> noload)
1056 function stripEvent( &$i, $j, &$body, $id, $base ) {
1062 while( $body{$i} <> '>' &&
1064 $etg = strtolower($body{$i}.$body{$i+
1}.$body{$i+
2});
1067 // Retrolinks are not allowed without a base because they mess with SM security
1077 if( $body{$i+
3} == ':') {
1080 while( isNoSep( $body{$i} ) &&
1082 $name .= $body{$i++
};
1084 $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1085 urlencode( $message->header
->mailbox
) .
1086 "&passed_ent_id=" . find_ent_id( $name, $message );
1087 if( $body{$k} == '"' )
1092 if( $body{$i} == '>' )
1104 if( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+
1}.$body{$i+
2}, 'script:') == 0 ) {
1120 /* This function trys to locate the entity_id of a specific mime element */
1122 function find_ent_id( $id, $message ) {
1125 for ($i=0; $ret == '' && $i < count($message->entities
); $i++
) {
1127 if( $message->entities
[$i]->header
->entity_id
== '' ) {
1128 $ret = find_ent_id( $id, $message->entities
[$i] );
1130 if( strcasecmp( $message->entities
[$i]->header
->id
, $id ) == 0 )
1131 $ret = $message->entities
[$i]->header
->entity_id
;