4 ** This contains the functions necessary to detect and decode MIME
10 require_once('../functions/imap.php');
11 require_once('../functions/attachment_common.php');
13 /** Setting up the objects that have the structure for the message **/
16 /** msg_header contains generic variables for values that **/
17 /** could be in a header. **/
19 var $type0 = '', $type1 = '', $boundary = '', $charset = '';
20 var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
21 var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
22 var $id = 0, $mailbox = '', $description = '', $filename = '';
23 var $entity_id = 0, $message_id = 0, $name = '';
24 // var $priority = "";
28 /** message is the object that contains messages. It is a recursive
29 object in that through the $entities variable, it can contain
30 more objects of type message. See documentation in mime.txt for
31 a better description of how this works.
34 var $entities = array();
36 function addEntity ($msg) {
37 $this->entities
[] = $msg;
41 /* --------------------------------------------------------------------------------- */
43 /* --------------------------------------------------------------------------------- */
45 // This function gets the structure of a message and stores it in the "message" class.
46 // It will return this object for use with all relevant header information and
47 // fully parsed into the standard "message" object format.
48 function mime_structure ($imap_stream, $header) {
50 sqimap_messages_flag ($imap_stream, $header->id
, $header->id
, 'Seen');
51 $ssid = sqimap_session_id();
52 $lsid = strlen( $ssid );
54 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
56 // This should use sqimap_read_data instead of reading it itself
58 $read = fgets ($imap_stream, 10000);
60 while( substr($read, 0, $lsid) <> $ssid &&
61 !feof( $imap_stream ) ) {
62 $bodystructure .= $read;
63 $read = fgets ($imap_stream, 10000);
65 $read = $bodystructure;
67 // isolate the body structure and remove beginning and end parenthesis
68 $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') +
13));
69 $read = trim(substr ($read, 0, -1));
70 $end = mime_match_parenthesis(0, $read);
71 while ($end == strlen($read)-1) {
72 $read = trim(substr ($read, 0, -1));
73 $read = trim(substr ($read, 1));
74 $end = mime_match_parenthesis(0, $read);
77 $msg = mime_parse_structure ($read, 0);
78 $msg->header
= $header;
82 // this starts the parsing of a particular structure. It is called recursively,
83 // so it can be passed different structures. It returns an object of type
85 // First, it checks to see if it is a multipart message. If it is, then it
86 // handles that as it sees is necessary. If it is just a regular entity,
87 // then it parses it and adds the necessary header information (by calling out
88 // to mime_get_elements()
89 function mime_parse_structure ($structure, $ent_id) {
92 if ($structure{0} == '(') {
93 $ent_id = mime_new_element_level($ent_id);
97 $end = mime_match_parenthesis ($start, $structure);
99 $element = substr($structure, $start+
1, ($end - $start)-1);
100 $ent_id = mime_increment_id ($ent_id);
101 $newmsg = mime_parse_structure ($element, $ent_id);
102 $msg->addEntity ($newmsg);
103 } while ($structure{$end+
1} == '(');
105 // parse the elements
106 $msg = mime_get_element ($structure, $msg, $ent_id);
111 // Increments the element ID. An element id can look like any of
112 // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
113 // the last number of the element id, changing 1.2 to 1.3.
114 function mime_increment_id ($id) {
116 if (strpos($id, ".")) {
117 $first = substr($id, 0, strrpos($id, "."));
118 $last = substr($id, strrpos($id, ".")+
1);
120 $new = $first . "." .$last;
128 // See comment for mime_increment_id().
129 // This adds another level on to the entity_id changing 1.3 to 1.3.0
130 // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
131 // before it can be used. I left it this way so as not to have
132 // to make a special case if it is the first entity_id. It
133 // always increments it, and that works fine.
134 function mime_new_element_level ($id) {
145 function mime_get_element (&$structure, $msg, $ent_id) {
148 $msg->header
= new msg_header();
149 $msg->header
->entity_id
= $ent_id;
150 $properties = array();
152 while (strlen($structure) > 0) {
153 $structure = trim($structure);
154 $char = $structure{0};
156 if (strtolower(substr($structure, 0, 3)) == 'nil') {
158 $structure = substr($structure, 3);
159 } else if ($char == '"') {
160 // loop through until we find the matching quote, and return that as a string
163 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
167 $structure = substr($structure, strlen($text) +
2);
168 } else if ($char == '(') {
170 $end = mime_match_parenthesis (0, $structure);
171 $sub = substr($structure, 1, $end-1);
172 $properties = mime_get_props($properties, $sub);
173 $structure = substr($structure, strlen($sub) +
2);
175 // loop through until we find a space or an end parenthesis
177 $char = $structure{$pos};
179 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
182 $char = $structure{$pos};
184 $structure = substr($structure, strlen($text));
187 // This is where all the text parts get put into the header
190 $msg->header
->type0
= strtolower($text);
193 $msg->header
->type1
= strtolower($text);
196 // Invisimail enclose images with <>
197 $msg->header
->id
= str_replace( '<', '', str_replace( '>', '', $text ) );
200 $msg->header
->description
= $text;
203 $msg->header
->encoding
= strtolower($text);
206 $msg->header
->size
= $text;
209 if ($msg->header
->type0
== 'text' && $elem_num == 8) {
210 // This is a plain text message, so lets get the number of lines
212 $msg->header
->num_lines
= $text;
214 } else if ($msg->header
->type0
== 'message' && $msg->header
->type1
== 'rfc822' && $elem_num == 8) {
215 // This is an encapsulated message, so lets start all over again and
216 // parse this message adding it on to the existing one.
217 $structure = trim($structure);
218 if ( $structure{0} == '(' ) {
219 $e = mime_match_parenthesis (0, $structure);
220 $structure = substr($structure, 0, $e);
221 $structure = substr($structure, 1);
222 $m = mime_parse_structure($structure, $msg->header
->entity_id
);
224 // the following conditional is there to correct a bug that wasn't
225 // incrementing the entity IDs correctly because of the special case
226 // that message/rfc822 is. This fixes it fine.
227 if (substr($structure, 1, 1) != '(')
228 $m->header
->entity_id
= mime_increment_id(mime_new_element_level($ent_id));
230 // Now we'll go through and reformat the results.
232 for ($i=0; $i < count($m->entities
); $i++
) {
233 $msg->addEntity($m->entities
[$i]);
246 // loop through the additional properties and put those in the various headers
247 if ($msg->header
->type0
!= 'message') {
248 for ($i=0; $i < count($properties); $i++
) {
249 $msg->header
->{$properties[$i]['name']} = $properties[$i]['value'];
256 // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
257 // figure out how to do this part, so I decided to go to bed. I woke up
258 // in the morning and had a flash of insight. I went to the white-board
259 // and scribbled it out, then spent a bit programming it, and this is the
260 // result. Nothing complicated, but I think my brain was fried yesterday.
261 // Funny how that happens some times.
263 // This gets properties in a nested parenthesisized list. For example,
264 // this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
265 // This returns an array called $props with all paired up properties.
266 // It ignores the "attachment" for now, maybe that should change later
267 // down the road. In this case, what is returned is:
268 // $props[0]["name"] = "filename";
269 // $props[0]["value"] = "luke.tar.gz";
270 function mime_get_props ($props, $structure) {
272 while (strlen($structure) > 0) {
273 $structure = trim($structure);
274 $char = $structure{0};
279 while ( ( $char = $structure{$pos} ) != '"' &&
280 $pos < strlen($structure)) {
284 $structure = trim(substr($structure, strlen($tmp) +
2));
285 $char = $structure{0};
290 while ( ( $char = $structure{$pos} ) != '"' &&
291 $pos < strlen($structure) ) {
295 $structure = trim(substr($structure, strlen($tmp) +
2));
298 $props[$k]['name'] = strtolower($tmp);
299 $props[$k]['value'] = $value;
300 } else if ($char == '(') {
301 $end = mime_match_parenthesis (0, $structure);
302 $sub = substr($structure, 1, $end-1);
305 $props = mime_get_props($props, $sub);
306 $structure = substr($structure, strlen($sub) +
2);
309 } else if ($char == '(') {
310 $end = mime_match_parenthesis (0, $structure);
311 $sub = substr($structure, 1, $end-1);
312 $props = mime_get_props($props, $sub);
313 $structure = substr($structure, strlen($sub) +
2);
321 // Matches parenthesis. It will return the position of the matching
322 // parenthesis in $structure. For instance, if $structure was:
323 // ("text" "plain" ("val1name", "1") nil ... )
325 // then this would return 42 to match up those two.
326 function mime_match_parenthesis ($pos, $structure) {
328 $j = strlen( $structure );
330 // ignore all extra characters
331 // If inside of a string, skip string -- Boundary IDs and other
332 // things can have ) in them.
333 if( $structure{$pos} != '(' )
338 if ($structure{$pos} == ')') {
340 } elseif ($structure{$pos} == '"') {
342 while( $structure{$pos} != '"' &&
344 if (substr($structure, $pos, 2) == '\\"')
346 elseif (substr($structure, $pos, 2) == '\\\\')
350 } elseif ( $structure{$pos} == '(' ) {
351 $pos = mime_match_parenthesis ($pos, $structure);
354 echo "Error decoding mime structure. Report this as a bug!<br>\n";
358 function mime_fetch_body ($imap_stream, $id, $ent_id ) {
359 // do a bit of error correction. If we couldn't find the entity id, just guess
360 // that it is the first one. That is usually the case anyway.
363 $sid = sqimap_session_id();
364 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
365 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
366 $topline = array_shift($data);
367 while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
368 $topline = array_shift($data);
369 $wholemessage = implode('', $data);
370 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
371 $ret = substr( $wholemessage, 0, $regs[1] );
373 There is some information in the content info header that could be important
374 in order to parse html messages. Let's get them here.
376 if( $ret{0} == '<' ) {
377 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id.MIME]\r\n");
378 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
381 foreach( $data as $d ) {
382 if( substr( $d, 0, 13 ) == 'Content-Base:' ) {
387 ( !isNoSep( $d{$i} ) ||
$d{$i} == '"' ) )
390 if( isNoSep( $d{$i} ) )
395 } elseif( $k == 1 && !isnosep( $d{0} ) ) {
396 $base .= substr( $d, 1 );
401 $ret = "<base href=\"$base\">" . $ret;
403 } else if (ereg('"([^"]*)"', $topline, $regs)) {
406 global $where, $what, $mailbox, $passed_id, $startMessage;
407 $par = "mailbox=".urlencode($mailbox)."&passed_id=$passed_id";
408 if (isset($where) && isset($what)) {
409 $par .= "&where=".urlencode($where)."&what=".urlencode($what);
411 $par .= "&startMessage=$startMessage&show_more=0";
413 $par .= '&response='.urlencode($response).'&message='.urlencode($message).
414 '&topline='.urlencode($topline);
416 echo '<b><font color=$color[2]>Body retrieval error. The reason for this is most probably that<BR> ' .
417 'the message is malformed. Please help us making future versions<BR> ' .
418 "better by submitting this message to the developers knowledgebase!<BR>\n" .
419 "<A HREF=\"../src/retrievalerror.php?$par\">Submit message</A><BR>" .
421 "<tt>Response: $response<BR>" .
422 "Message: $message<BR>" .
423 "FETCH line: $topline<BR></tt></font></b>";
425 fputs ($imap_stream, "$sid FETCH $passed_id BODY[]\r\n");
426 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
428 $wholemessage = implode('', $data);
430 $ret = "---------------\n$wholemessage";
436 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
437 // do a bit of error correction. If we couldn't find the entity id, just guess
438 // that it is the first one. That is usually the case anyway.
439 if (!$ent_id) $ent_id = 1;
440 $sid = sqimap_session_id();
441 // Don't kill the connection if the browser is over a dialup
442 // and it would take over 30 seconds to download it.
445 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
448 $read = fgets ($imap_stream,4096);
449 // This could be bad -- if the section has sqimap_session_id() . ' OK'
450 // or similar, it will kill the download.
451 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
452 if (trim($read) == ')==') {
454 $read = fgets ($imap_stream,4096);
455 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
458 echo decodeBody($read1, $encoding) .
459 decodeBody($read, $encoding);
462 echo decodeBody($read, $encoding);
464 $read = fgets ($imap_stream,4096);
469 /* -[ END MIME DECODING ]----------------------------------------------------------- */
473 /** This is the first function called. It decides if this is a multipart
474 message or if it should be handled as a single entity
476 function decodeMime ($imap_stream, &$header) {
477 global $username, $key, $imapServerAddress, $imapPort;
478 return mime_structure ($imap_stream, $header);
481 // This is here for debugging purposese. It will print out a list
482 // of all the entity IDs that are in the $message object.
484 function listEntities ($message) {
486 if ($message->header->entity_id)
487 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
488 for ($i = 0; $message->entities[$i]; $i++) {
489 $msg = listEntities($message->entities[$i], $ent_id);
497 // returns a $message object for a particular entity id
498 function getEntity ($message, $ent_id) {
500 if ($message->header
->entity_id
== $ent_id && strlen($ent_id) == strlen($message->header
->entity_id
)) {
503 for ($i = 0; isset($message->entities
[$i]); $i++
) {
504 $msg = getEntity ($message->entities
[$i], $ent_id);
512 // figures out what entity to display and returns the $message object
514 function findDisplayEntity ($message, $textOnly = 1) {
515 global $show_html_default;
520 if ( $message->header
->type0
== 'multipart' &&
521 ( $message->header
->type1
== 'alternative' ||
522 $message->header
->type1
== 'related' ) &&
523 $show_html_default && ! $textOnly ) {
524 $entity = findDisplayEntityHTML($message);
527 // Show text/plain or text/html -- the first one we find.
529 $message->header
->type0
== 'text' &&
530 ( $message->header
->type1
== 'plain' ||
531 $message->header
->type1
== 'html' ) &&
532 isset($message->header
->entity_id
) ) {
533 $entity = $message->header
->entity_id
;
537 while ($entity == 0 && isset($message->entities
[$i]) ) {
538 $entity = findDisplayEntity($message->entities
[$i], $textOnly);
546 // Shows the HTML version
547 function findDisplayEntityHTML ($message) {
548 if ($message->header
->type0
== 'text' &&
549 $message->header
->type1
== 'html' &&
550 isset($message->header
->entity_id
))
551 return $message->header
->entity_id
;
552 for ($i = 0; isset($message->entities
[$i]); $i ++
) {
553 $entity = findDisplayEntityHTML($message->entities
[$i]);
560 /** This returns a parsed string called $body. That string can then
561 be displayed as the actual message in the HTML. It contains
562 everything needed, including HTML Tags, Attachments at the
565 function formatBody($imap_stream, $message, $color, $wrap_at) {
566 // this if statement checks for the entity to show as the
567 // primary message. To add more of them, just put them in the
568 // order that is their priority.
569 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
572 $id = $message->header
->id
;
573 $urlmailbox = urlencode($message->header
->mailbox
);
575 // Get the right entity and redefine message to be this entity
576 // Pass the 0 to mean that we want the 'best' viewable one
577 $ent_num = findDisplayEntity ($message, 0);
578 $body_message = getEntity($message, $ent_num);
579 if (($body_message->header
->type0
== 'text') ||
580 ($body_message->header
->type0
== 'rfc822')) {
582 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
583 $body = decodeBody($body, $body_message->header
->encoding
);
584 $hookResults = do_hook("message_body", $body);
585 $body = $hookResults[1];
587 // If there are other types that shouldn't be formatted, add
589 if ($body_message->header
->type1
== 'html') {
590 if( $show_html_default <> 1 ) {
591 $body = strip_tags( $body );
592 translateText($body, $wrap_at, $body_message->header
->charset
);
594 $body = MagicHTML( $body, $id );
597 translateText($body, $wrap_at, $body_message->header
->charset
);
600 $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>";
602 /** Display the ATTACHMENTS: message if there's more than one part **/
603 $body .= "</TD></TR></TABLE>";
604 if (isset($message->entities
[0])) {
605 $body .= formatAttachments ($message, $ent_num, $message->header
->mailbox
, $id);
607 $body .= "</TD></TR></TABLE>";
609 $body = formatAttachments ($message, -1, $message->header
->mailbox
, $id);
614 // A recursive function that returns a list of attachments with links
615 // to where to download these attachments
616 function formatAttachments ($message, $ent_id, $mailbox, $id) {
617 global $where, $what;
618 global $startMessage, $color;
619 static $ShownHTML = 0;
622 if ($ShownHTML == 0) {
625 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
626 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
627 _("Attachments") . ':' .
628 "</B></TH></TR><TR><TD>\n" .
629 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
630 formatAttachments ($message, $ent_id, $mailbox, $id) .
631 "</TABLE></TD></TR></TABLE>";
637 if (!$message->entities
) {
638 $type0 = strtolower($message->header
->type0
);
639 $type1 = strtolower($message->header
->type1
);
640 $name = decodeHeader($message->header
->name
);
642 if ($message->header
->entity_id
!= $ent_id) {
643 $filename = decodeHeader($message->header
->filename
);
644 if (trim($filename) == '') {
645 if (trim($name) == '') {
646 if( trim( $message->header
->id
) == '' )
647 $display_filename = 'untitled-[' . $message->header
->entity_id
. ']' ;
649 $display_filename = 'cid: ' . $message->header
->id
;
650 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
652 $display_filename = $name;
656 $display_filename = $filename;
659 $urlMailbox = urlencode($mailbox);
660 $ent = urlencode($message->header
->entity_id
);
663 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
665 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
666 $Links['download link']['text'] = _("download");
667 $Links['download link']['href'] =
668 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
671 $HookResults = do_hook("attachment $type0/$type1", $Links,
672 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
673 $display_filename, $where, $what);
675 $Links = $HookResults[1];
676 $DefaultLink = $HookResults[6];
678 $body .= '<TR><TD> </TD><TD>' .
679 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
680 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
681 '</b> </small></TD>' .
682 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
684 if ($message->header
->description
)
685 $body .= '<b>' . htmlspecialchars($message->header
->description
) . '</b>';
686 $body .= '</SMALL></TD><TD><SMALL> ';
690 foreach ($Links as $Val) {
694 $body .= ' | ';
696 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
701 $body .= "</SMALL></TD></TR>\n";
704 for ($i = 0; $i < count($message->entities
); $i++
) {
705 $body .= formatAttachments ($message->entities
[$i], $ent_id, $mailbox, $id);
713 /** this function decodes the body depending on the encoding type. **/
714 function decodeBody($body, $encoding) {
715 $body = str_replace("\r\n", "\n", $body);
716 $encoding = strtolower($encoding);
718 global $show_html_default;
720 if ($encoding == 'quoted-printable') {
721 $body = quoted_printable_decode($body);
725 Following code has been comented as I see no reason for it.
726 If there is any please tell me a mingo@rotedic.com
728 while (ereg("=\n", $body))
729 $body = ereg_replace ("=\n", "", $body);
731 } else if ($encoding == 'base64') {
732 $body = base64_decode($body);
735 // All other encodings are returned raw.
740 // This functions decode strings that is encoded according to
741 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
742 function decodeHeader ($string) {
743 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
745 if (ucfirst($res[2]) == "B") {
746 $replace = base64_decode($res[3]);
748 $replace = ereg_replace("_", " ", $res[3]);
749 // Convert lowercase Quoted Printable to uppercase for
750 // quoted_printable_decode to understand it.
751 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", $replace, $res)) {
752 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
754 $replace = quoted_printable_decode($replace);
757 $replace = charset_decode ($res[1], $replace);
759 // Remove the name of the character set.
760 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
763 // In case there should be more encoding in the string: recurse
764 return (decodeHeader($string));
769 // Encode a string according to RFC 1522 for use in headers if it
770 // contains 8-bit characters or anything that looks like it should
772 function encodeHeader ($string) {
773 global $default_charset;
775 // Encode only if the string contains 8-bit characters or =?
776 $j = strlen( $string );
777 $l = FALSE; // Must be encoded ?
779 for( $i=0; $i < $j; ++
$i) {
780 switch( $string{$i} ) {
795 $k = ord( $string{$i} );
797 $ret .= sprintf("=%02X", $k);
805 $string = "=?$default_charset?Q?$ret?=";
811 Strips dangerous tags from html messages.
814 function MagicHTML( $body, $id ) {
816 global $message, $PHP_SELF, $HTTP_SERVER_VARS;
818 $j = strlen( $body ); // Legnth of the HTML
819 $ret = ''; // Returned string
820 $bgcolor = '#ffffff'; // Background style color (defaults to white)
821 $leftmargin = ''; // Left margin style
822 $title = ''; // HTML title if any
826 if( $body{$i} == '<' ) {
827 $tag = $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4};
828 switch( strtoupper( $tag ) ) {
829 // Strips the entire tag and contents
836 while( $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4}.$body{$i+
5} <> $etg &&
838 while( $i < $j && $body{++
$i} <> '>' );
839 // $ret .= "<!-- $tag removed -->";
844 while( $body{$i} <> '>' && // </title>
849 while( $body{$i} <> '<' && // </title>
856 // Destroy these tags
867 while( $body{$i} <> '>' &&
874 while( $body{$i} <> '>' && // </title>
878 // We parse the style to look for interesting stuff
880 while( $body{$i} <> '>' &&
882 // First we get the name of the style
884 while( $body{$i} <> '>' &&
888 if( isnoSep( $body{$i} ) )
892 stripComments( $i, $j, $body );
893 $style = strtoupper( trim( $style ) );
894 if( $style == 'BODY' ) {
895 // Next we look into the definitions of the body style
896 while( $body{$i} <> '>' &&
899 // We look for the background color if any.
900 if( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
902 $bgcolor = getStyleData( $i, $j, $body );
903 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
905 $leftmargin = getStyleData( $i, $j, $body );
910 // Other style are mantained
911 $styleblk .= "$style ";
912 while( $body{$i} <> '>' &&
916 $styleblk .= $body{$i};
919 $styleblk .= $body{$i};
921 stripComments( $i, $j, $body );
922 if( $body{$i} <> '>' )
925 if( $styleblk <> '' )
926 $ret .= "<style>$styleblk";
930 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
935 $ret .= stripEvent( $i, $j, $body, $id, $base );
936 //if( $bgcolor <> '' )
937 $ret .= " bgcolor=$bgcolor";
938 $ret .= ' width=100%><tr>';
939 if( $leftmargin <> '' )
940 $ret .= "<td width=$leftmargin> </td>";
946 while( !isNoSep( $body{$i} ) &&
949 if( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
951 while( !isNoSep( $body{$i} ) &&
954 while( $body{$i} <> '>' &&
956 if( $body{$i} <> '"' )
960 // Debuging $ret .= "<!-- base == $base -->";
961 if( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 )
962 $ret .= "\n<BASE HREF=\"$base\">\n";
966 $ret .= '</td></tr></TABLE>';
970 // Following tags can contain some event handler, lets search it
971 stripComments( $i, $j, $body );
974 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
975 // $ret .= "<!-- $tag detected -->";
983 return( "\n\n<!-- HTML Output ahead -->\n" .
985 "\n<!-- END of HTML Output --><base href=\"".
986 $HTTP_SERVER_VARS["SERVER_NAME"] . substr( $PHP_SELF, 0, strlen( $PHP_SELF ) - 13 ) .
990 function isNoSep( $char ) {
1008 The following function is usefull to remove extra data that can cause
1009 html not to display properly. Especialy with MS stuff.
1012 function stripComments( &$i, $j, &$body ) {
1014 while( $body{$i}.$body{$i+
1}.$body{$i+
2}.$body{$i+
3} == '<!--' &&
1017 while( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1027 /* Gets the style data of a specific style */
1029 function getStyleData( &$i, $j, &$body ) {
1032 while( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1036 // And get the color
1038 while( isNoSep( $body{$i} ) &&
1048 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1049 change on with no (onload -> noload)
1052 function stripEvent( &$i, $j, &$body, $id, $base ) {
1058 while( $body{$i} <> '>' &&
1060 $etg = strtolower($body{$i}.$body{$i+
1}.$body{$i+
2});
1063 // Retrolinks are not allowed without a base because they mess with SM security
1073 if( $body{$i+
3} == ':') {
1076 while( isNoSep( $body{$i} ) &&
1078 $name .= $body{$i++
};
1080 $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1081 urlencode( $message->header
->mailbox
) .
1082 "&passed_ent_id=" . find_ent_id( $name, $message );
1083 if( $body{$k} == '"' )
1088 if( $body{$i} == '>' )
1100 if( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+
1}.$body{$i+
2}, 'script:') == 0 ) {
1116 /* This function trys to locate the entity_id of a specific mime element */
1118 function find_ent_id( $id, $message ) {
1121 for ($i=0; $ret == '' && $i < count($message->entities
); $i++
) {
1123 if( $message->entities
[$i]->header
->entity_id
== '' ) {
1124 $ret = find_ent_id( $id, $message->entities
[$i] );
1126 if( strcasecmp( $message->entities
[$i]->header
->id
, $id ) == 0 )
1127 $ret = $message->entities
[$i]->header
->entity_id
;