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');
16 /** Setting up the objects that have the structure for the message **/
19 /** msg_header contains generic variables for values that **/
20 /** could be in a header. **/
22 var $type0 = '', $type1 = '', $boundary = '', $charset = '';
23 var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
24 var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
25 var $id = 0, $mailbox = '', $description = '', $filename = '';
26 var $entity_id = 0, $message_id = 0, $name = '';
27 // var $priority = "";
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.
37 var $entities = array();
39 function addEntity ($msg) {
40 $this->entities
[] = $msg;
44 /* --------------------------------------------------------------------------------- */
46 /* --------------------------------------------------------------------------------- */
48 // This function gets the structure of a message and stores it in the "message" class.
49 // It will return this object for use with all relevant header information and
50 // fully parsed into the standard "message" object format.
51 function mime_structure ($imap_stream, $header) {
53 sqimap_messages_flag ($imap_stream, $header->id
, $header->id
, 'Seen');
54 $ssid = sqimap_session_id();
55 $lsid = strlen( $ssid );
57 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
59 // This should use sqimap_read_data instead of reading it itself
61 $read = fgets ($imap_stream, 10000);
63 while( substr($read, 0, $lsid) <> $ssid &&
64 !feof( $imap_stream ) ) {
65 $bodystructure .= $read;
66 $read = fgets ($imap_stream, 10000);
68 $read = $bodystructure;
70 // isolate the body structure and remove beginning and end parenthesis
71 $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;
85 // this starts the parsing of a particular structure. It is called recursively,
86 // so it can be passed different structures. It returns an object of type
88 // First, it checks to see if it is a multipart message. If it is, then it
89 // handles that as it sees is necessary. If it is just a regular entity,
90 // then it parses it and adds the necessary header information (by calling out
91 // to mime_get_elements()
92 function mime_parse_structure ($structure, $ent_id) {
95 if ($structure{0} == '(') {
96 $ent_id = mime_new_element_level($ent_id);
100 $end = mime_match_parenthesis ($start, $structure);
102 $element = substr($structure, $start+
1, ($end - $start)-1);
103 $ent_id = mime_increment_id ($ent_id);
104 $newmsg = mime_parse_structure ($element, $ent_id);
105 $msg->addEntity ($newmsg);
106 } while ($structure{$end+
1} == '(');
108 // parse the elements
109 $msg = mime_get_element ($structure, $msg, $ent_id);
114 // Increments the element ID. An element id can look like any of
115 // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
116 // the last number of the element id, changing 1.2 to 1.3.
117 function mime_increment_id ($id) {
119 if (strpos($id, ".")) {
120 $first = substr($id, 0, strrpos($id, "."));
121 $last = substr($id, strrpos($id, ".")+
1);
123 $new = $first . "." .$last;
131 // See comment for mime_increment_id().
132 // This adds another level on to the entity_id changing 1.3 to 1.3.0
133 // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
134 // before it can be used. I left it this way so as not to have
135 // to make a special case if it is the first entity_id. It
136 // always increments it, and that works fine.
137 function mime_new_element_level ($id) {
148 function mime_get_element (&$structure, $msg, $ent_id) {
151 $msg->header
= new msg_header();
152 $msg->header
->entity_id
= $ent_id;
153 $properties = array();
155 while (strlen($structure) > 0) {
156 $structure = trim($structure);
157 $char = $structure{0};
159 if (strtolower(substr($structure, 0, 3)) == 'nil') {
161 $structure = substr($structure, 3);
162 } else if ($char == '"') {
163 // loop through until we find the matching quote, and return that as a string
166 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
170 $structure = substr($structure, strlen($text) +
2);
171 } else if ($char == '(') {
173 $end = mime_match_parenthesis (0, $structure);
174 $sub = substr($structure, 1, $end-1);
175 $properties = mime_get_props($properties, $sub);
176 $structure = substr($structure, strlen($sub) +
2);
178 // loop through until we find a space or an end parenthesis
180 $char = $structure{$pos};
182 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
185 $char = $structure{$pos};
187 $structure = substr($structure, strlen($text));
190 // This is where all the text parts get put into the header
193 $msg->header
->type0
= strtolower($text);
196 $msg->header
->type1
= strtolower($text);
199 // Invisimail enclose images with <>
200 $msg->header
->id
= str_replace( '<', '', str_replace( '>', '', $text ) );
203 $msg->header
->description
= $text;
206 $msg->header
->encoding
= strtolower($text);
209 $msg->header
->size
= $text;
212 if ($msg->header
->type0
== 'text' && $elem_num == 8) {
213 // This is a plain text message, so lets get the number of lines
215 $msg->header
->num_lines
= $text;
217 } else if ($msg->header
->type0
== 'message' && $msg->header
->type1
== 'rfc822' && $elem_num == 8) {
218 // This is an encapsulated message, so lets start all over again and
219 // parse this message adding it on to the existing one.
220 $structure = trim($structure);
221 if ( $structure{0} == '(' ) {
222 $e = mime_match_parenthesis (0, $structure);
223 $structure = substr($structure, 0, $e);
224 $structure = substr($structure, 1);
225 $m = mime_parse_structure($structure, $msg->header
->entity_id
);
227 // the following conditional is there to correct a bug that wasn't
228 // incrementing the entity IDs correctly because of the special case
229 // that message/rfc822 is. This fixes it fine.
230 if (substr($structure, 1, 1) != '(')
231 $m->header
->entity_id
= mime_increment_id(mime_new_element_level($ent_id));
233 // Now we'll go through and reformat the results.
235 for ($i=0; $i < count($m->entities
); $i++
) {
236 $msg->addEntity($m->entities
[$i]);
249 // loop through the additional properties and put those in the various headers
250 if ($msg->header
->type0
!= 'message') {
251 for ($i=0; $i < count($properties); $i++
) {
252 $msg->header
->{$properties[$i]['name']} = $properties[$i]['value'];
259 // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
260 // figure out how to do this part, so I decided to go to bed. I woke up
261 // in the morning and had a flash of insight. I went to the white-board
262 // and scribbled it out, then spent a bit programming it, and this is the
263 // result. Nothing complicated, but I think my brain was fried yesterday.
264 // Funny how that happens some times.
266 // This gets properties in a nested parenthesisized list. For example,
267 // this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
268 // This returns an array called $props with all paired up properties.
269 // It ignores the "attachment" for now, maybe that should change later
270 // down the road. In this case, what is returned is:
271 // $props[0]["name"] = "filename";
272 // $props[0]["value"] = "luke.tar.gz";
273 function mime_get_props ($props, $structure) {
275 while (strlen($structure) > 0) {
276 $structure = trim($structure);
277 $char = $structure{0};
282 while ( ( $char = $structure{$pos} ) != '"' &&
283 $pos < strlen($structure)) {
287 $structure = trim(substr($structure, strlen($tmp) +
2));
288 $char = $structure{0};
293 while ( ( $char = $structure{$pos} ) != '"' &&
294 $pos < strlen($structure) ) {
298 $structure = trim(substr($structure, strlen($tmp) +
2));
301 $props[$k]['name'] = strtolower($tmp);
302 $props[$k]['value'] = $value;
303 } else if ($char == '(') {
304 $end = mime_match_parenthesis (0, $structure);
305 $sub = substr($structure, 1, $end-1);
308 $props = mime_get_props($props, $sub);
309 $structure = substr($structure, strlen($sub) +
2);
312 } else if ($char == '(') {
313 $end = mime_match_parenthesis (0, $structure);
314 $sub = substr($structure, 1, $end-1);
315 $props = mime_get_props($props, $sub);
316 $structure = substr($structure, strlen($sub) +
2);
324 // Matches parenthesis. It will return the position of the matching
325 // parenthesis in $structure. For instance, if $structure was:
326 // ("text" "plain" ("val1name", "1") nil ... )
328 // then this would return 42 to match up those two.
329 function mime_match_parenthesis ($pos, $structure) {
331 $j = strlen( $structure );
333 // ignore all extra characters
334 // If inside of a string, skip string -- Boundary IDs and other
335 // things can have ) in them.
336 if( $structure{$pos} != '(' )
341 if ($structure{$pos} == ')') {
343 } elseif ($structure{$pos} == '"') {
345 while( $structure{$pos} != '"' &&
347 if (substr($structure, $pos, 2) == '\\"')
349 elseif (substr($structure, $pos, 2) == '\\\\')
353 } elseif ( $structure{$pos} == '(' ) {
354 $pos = mime_match_parenthesis ($pos, $structure);
357 echo "Error decoding mime structure. Report this as a bug!<br>\n";
361 function mime_fetch_body ($imap_stream, $id, $ent_id ) {
362 // do a bit of error correction. If we couldn't find the entity id, just guess
363 // that it is the first one. That is usually the case anyway.
366 $sid = sqimap_session_id();
367 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
368 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
369 $topline = array_shift($data);
370 while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
371 $topline = array_shift($data);
372 $wholemessage = implode('', $data);
373 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
374 $ret = substr( $wholemessage, 0, $regs[1] );
376 There is some information in the content info header that could be important
377 in order to parse html messages. Let's get them here.
379 if( $ret{0} == '<' ) {
380 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id.MIME]\r\n");
381 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
384 foreach( $data as $d ) {
385 if( substr( $d, 0, 13 ) == 'Content-Base:' ) {
390 ( !isNoSep( $d{$i} ) ||
$d{$i} == '"' ) )
393 if( isNoSep( $d{$i} ) )
398 } elseif( $k == 1 && !isnosep( $d{0} ) ) {
399 $base .= substr( $d, 1 );
404 $ret = "<base href=\"$base\">" . $ret;
406 } else if (ereg('"([^"]*)"', $topline, $regs)) {
409 global $where, $what, $mailbox, $passed_id, $startMessage;
410 $par = "mailbox=".urlencode($mailbox)."&passed_id=$passed_id";
411 if (isset($where) && isset($what)) {
412 $par .= "&where=".urlencode($where)."&what=".urlencode($what);
414 $par .= "&startMessage=$startMessage&show_more=0";
416 $par .= '&response='.urlencode($response).'&message='.urlencode($message).
417 '&topline='.urlencode($topline);
419 echo '<b><font color=$color[2]>Body retrieval error. The reason for this is most probably that<BR> ' .
420 'the message is malformed. Please help us making future versions<BR> ' .
421 "better by submitting this message to the developers knowledgebase!<BR>\n" .
422 "<A HREF=\"../src/retrievalerror.php?$par\">Submit message</A><BR>" .
424 "<tt>Response: $response<BR>" .
425 "Message: $message<BR>" .
426 "FETCH line: $topline<BR></tt></font></b>";
428 fputs ($imap_stream, "$sid FETCH $passed_id BODY[]\r\n");
429 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
431 $wholemessage = implode('', $data);
433 $ret = "---------------\n$wholemessage";
439 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
440 // do a bit of error correction. If we couldn't find the entity id, just guess
441 // that it is the first one. That is usually the case anyway.
442 if (!$ent_id) $ent_id = 1;
443 $sid = sqimap_session_id();
444 // Don't kill the connection if the browser is over a dialup
445 // and it would take over 30 seconds to download it.
448 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
451 $read = fgets ($imap_stream,4096);
452 // This could be bad -- if the section has sqimap_session_id() . ' OK'
453 // or similar, it will kill the download.
454 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
455 if (trim($read) == ')==') {
457 $read = fgets ($imap_stream,4096);
458 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
461 echo decodeBody($read1, $encoding) .
462 decodeBody($read, $encoding);
465 echo decodeBody($read, $encoding);
467 $read = fgets ($imap_stream,4096);
472 /* -[ END MIME DECODING ]----------------------------------------------------------- */
476 /** This is the first function called. It decides if this is a multipart
477 message or if it should be handled as a single entity
479 function decodeMime ($imap_stream, &$header) {
480 global $username, $key, $imapServerAddress, $imapPort;
481 return mime_structure ($imap_stream, $header);
484 // This is here for debugging purposese. It will print out a list
485 // of all the entity IDs that are in the $message object.
487 function listEntities ($message) {
489 if ($message->header->entity_id)
490 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
491 for ($i = 0; $message->entities[$i]; $i++) {
492 $msg = listEntities($message->entities[$i], $ent_id);
500 // returns a $message object for a particular entity id
501 function getEntity ($message, $ent_id) {
503 if ($message->header
->entity_id
== $ent_id && strlen($ent_id) == strlen($message->header
->entity_id
)) {
506 for ($i = 0; isset($message->entities
[$i]); $i++
) {
507 $msg = getEntity ($message->entities
[$i], $ent_id);
515 // figures out what entity to display and returns the $message object
517 function findDisplayEntity ($message, $textOnly = 1) {
518 global $show_html_default;
523 if ( $message->header
->type0
== 'multipart' &&
524 ( $message->header
->type1
== 'alternative' ||
525 $message->header
->type1
== 'related' ) &&
526 $show_html_default && ! $textOnly ) {
527 $entity = findDisplayEntityHTML($message);
530 // Show text/plain or text/html -- the first one we find.
532 $message->header
->type0
== 'text' &&
533 ( $message->header
->type1
== 'plain' ||
534 $message->header
->type1
== 'html' ) &&
535 isset($message->header
->entity_id
) ) {
536 $entity = $message->header
->entity_id
;
540 while ($entity == 0 && isset($message->entities
[$i]) ) {
541 $entity = findDisplayEntity($message->entities
[$i], $textOnly);
549 // Shows the HTML version
550 function findDisplayEntityHTML ($message) {
551 if ($message->header
->type0
== 'text' &&
552 $message->header
->type1
== 'html' &&
553 isset($message->header
->entity_id
))
554 return $message->header
->entity_id
;
555 for ($i = 0; isset($message->entities
[$i]); $i ++
) {
556 $entity = findDisplayEntityHTML($message->entities
[$i]);
563 /** This returns a parsed string called $body. That string can then
564 be displayed as the actual message in the HTML. It contains
565 everything needed, including HTML Tags, Attachments at the
568 function formatBody($imap_stream, $message, $color, $wrap_at) {
569 // this if statement checks for the entity to show as the
570 // primary message. To add more of them, just put them in the
571 // order that is their priority.
572 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
575 $id = $message->header
->id
;
576 $urlmailbox = urlencode($message->header
->mailbox
);
578 // Get the right entity and redefine message to be this entity
579 // Pass the 0 to mean that we want the 'best' viewable one
580 $ent_num = findDisplayEntity ($message, 0);
581 $body_message = getEntity($message, $ent_num);
582 if (($body_message->header
->type0
== 'text') ||
583 ($body_message->header
->type0
== 'rfc822')) {
585 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
586 $body = decodeBody($body, $body_message->header
->encoding
);
587 $hookResults = do_hook("message_body", $body);
588 $body = $hookResults[1];
590 // If there are other types that shouldn't be formatted, add
592 if ($body_message->header
->type1
== 'html') {
593 if( $show_html_default <> 1 ) {
594 $body = strip_tags( $body );
595 translateText($body, $wrap_at, $body_message->header
->charset
);
597 $body = MagicHTML( $body, $id );
600 translateText($body, $wrap_at, $body_message->header
->charset
);
603 $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>";
605 /** Display the ATTACHMENTS: message if there's more than one part **/
606 $body .= "</TD></TR></TABLE>";
607 if (isset($message->entities
[0])) {
608 $body .= formatAttachments ($message, $ent_num, $message->header
->mailbox
, $id);
610 $body .= "</TD></TR></TABLE>";
612 $body = formatAttachments ($message, -1, $message->header
->mailbox
, $id);
617 // A recursive function that returns a list of attachments with links
618 // to where to download these attachments
619 function formatAttachments ($message, $ent_id, $mailbox, $id) {
620 global $where, $what;
621 global $startMessage, $color;
622 static $ShownHTML = 0;
625 if ($ShownHTML == 0) {
628 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
629 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
630 _("Attachments") . ':' .
631 "</B></TH></TR><TR><TD>\n" .
632 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
633 formatAttachments ($message, $ent_id, $mailbox, $id) .
634 "</TABLE></TD></TR></TABLE>";
640 if (!$message->entities
) {
641 $type0 = strtolower($message->header
->type0
);
642 $type1 = strtolower($message->header
->type1
);
643 $name = decodeHeader($message->header
->name
);
645 if ($message->header
->entity_id
!= $ent_id) {
646 $filename = decodeHeader($message->header
->filename
);
647 if (trim($filename) == '') {
648 if (trim($name) == '') {
649 if( trim( $message->header
->id
) == '' )
650 $display_filename = 'untitled-[' . $message->header
->entity_id
. ']' ;
652 $display_filename = 'cid: ' . $message->header
->id
;
653 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
655 $display_filename = $name;
659 $display_filename = $filename;
662 $urlMailbox = urlencode($mailbox);
663 $ent = urlencode($message->header
->entity_id
);
666 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
668 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
669 $Links['download link']['text'] = _("download");
670 $Links['download link']['href'] =
671 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
674 $HookResults = do_hook("attachment $type0/$type1", $Links,
675 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
676 $display_filename, $where, $what);
678 $Links = $HookResults[1];
679 $DefaultLink = $HookResults[6];
681 $body .= '<TR><TD> </TD><TD>' .
682 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
683 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
684 '</b> </small></TD>' .
685 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
687 if ($message->header
->description
)
688 $body .= '<b>' . htmlspecialchars($message->header
->description
) . '</b>';
689 $body .= '</SMALL></TD><TD><SMALL> ';
693 foreach ($Links as $Val) {
697 $body .= ' | ';
699 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
704 $body .= "</SMALL></TD></TR>\n";
707 for ($i = 0; $i < count($message->entities
); $i++
) {
708 $body .= formatAttachments ($message->entities
[$i], $ent_id, $mailbox, $id);
716 /** this function decodes the body depending on the encoding type. **/
717 function decodeBody($body, $encoding) {
718 $body = str_replace("\r\n", "\n", $body);
719 $encoding = strtolower($encoding);
721 global $show_html_default;
723 if ($encoding == 'quoted-printable') {
724 $body = quoted_printable_decode($body);
728 Following code has been comented as I see no reason for it.
729 If there is any please tell me a mingo@rotedic.com
731 while (ereg("=\n", $body))
732 $body = ereg_replace ("=\n", "", $body);
734 } else if ($encoding == 'base64') {
735 $body = base64_decode($body);
738 // All other encodings are returned raw.
743 // This functions decode strings that is encoded according to
744 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
745 function decodeHeader ($string) {
746 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
748 if (ucfirst($res[2]) == "B") {
749 $replace = base64_decode($res[3]);
751 $replace = ereg_replace("_", " ", $res[3]);
752 // Convert lowercase Quoted Printable to uppercase for
753 // quoted_printable_decode to understand it.
754 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", $replace, $res)) {
755 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
757 $replace = quoted_printable_decode($replace);
760 $replace = charset_decode ($res[1], $replace);
762 // Remove the name of the character set.
763 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
766 // In case there should be more encoding in the string: recurse
767 return (decodeHeader($string));
772 // Encode a string according to RFC 1522 for use in headers if it
773 // contains 8-bit characters or anything that looks like it should
775 function encodeHeader ($string) {
776 global $default_charset;
778 // Encode only if the string contains 8-bit characters or =?
779 $j = strlen( $string );
780 $l = FALSE; // Must be encoded ?
782 for( $i=0; $i < $j; ++
$i) {
783 switch( $string{$i} ) {
798 $k = ord( $string{$i} );
800 $ret .= sprintf("=%02X", $k);
808 $string = "=?$default_charset?Q?$ret?=";
814 Strips dangerous tags from html messages.
817 function MagicHTML( $body, $id ) {
819 global $message, $PHP_SELF, $HTTP_SERVER_VARS;
821 $j = strlen( $body ); // Legnth of the HTML
822 $ret = ''; // Returned string
823 $bgcolor = '#ffffff'; // Background style color (defaults to white)
824 $leftmargin = ''; // Left margin style
825 $title = ''; // HTML title if any
829 if( $body{$i} == '<' ) {
830 $tag = $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4};
831 switch( strtoupper( $tag ) ) {
832 // Strips the entire tag and contents
839 while( $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4}.$body{$i+
5} <> $etg &&
841 while( $i < $j && $body{++
$i} <> '>' );
842 // $ret .= "<!-- $tag removed -->";
847 while( $body{$i} <> '>' && // </title>
852 while( $body{$i} <> '<' && // </title>
859 // Destroy these tags
870 while( $body{$i} <> '>' &&
877 while( $body{$i} <> '>' && // </title>
881 // We parse the style to look for interesting stuff
883 while( $body{$i} <> '>' &&
885 // First we get the name of the style
887 while( $body{$i} <> '>' &&
891 if( isnoSep( $body{$i} ) )
895 stripComments( $i, $j, $body );
896 $style = strtoupper( trim( $style ) );
897 if( $style == 'BODY' ) {
898 // Next we look into the definitions of the body style
899 while( $body{$i} <> '>' &&
902 // We look for the background color if any.
903 if( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
905 $bgcolor = getStyleData( $i, $j, $body );
906 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
908 $leftmargin = getStyleData( $i, $j, $body );
913 // Other style are mantained
914 $styleblk .= "$style ";
915 while( $body{$i} <> '>' &&
919 $styleblk .= $body{$i};
922 $styleblk .= $body{$i};
924 stripComments( $i, $j, $body );
925 if( $body{$i} <> '>' )
928 if( $styleblk <> '' )
929 $ret .= "<style>$styleblk";
933 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
938 $ret .= stripEvent( $i, $j, $body, $id, $base );
939 //if( $bgcolor <> '' )
940 $ret .= " bgcolor=$bgcolor";
941 $ret .= ' width=100%><tr>';
942 if( $leftmargin <> '' )
943 $ret .= "<td width=$leftmargin> </td>";
949 while( !isNoSep( $body{$i} ) &&
952 if( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
954 while( !isNoSep( $body{$i} ) &&
957 while( $body{$i} <> '>' &&
959 if( $body{$i} <> '"' )
963 // Debuging $ret .= "<!-- base == $base -->";
964 if( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 )
965 $ret .= "\n<BASE HREF=\"$base\">\n";
969 $ret .= '</td></tr></TABLE>';
973 // Following tags can contain some event handler, lets search it
974 stripComments( $i, $j, $body );
977 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
978 // $ret .= "<!-- $tag detected -->";
986 return( "\n\n<!-- HTML Output ahead -->\n" .
988 "\n<!-- END of HTML Output --><base href=\"".
989 $HTTP_SERVER_VARS["SERVER_NAME"] . substr( $PHP_SELF, 0, strlen( $PHP_SELF ) - 13 ) .
993 function isNoSep( $char ) {
1011 The following function is usefull to remove extra data that can cause
1012 html not to display properly. Especialy with MS stuff.
1015 function stripComments( &$i, $j, &$body ) {
1017 while( $body{$i}.$body{$i+
1}.$body{$i+
2}.$body{$i+
3} == '<!--' &&
1020 while( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1030 /* Gets the style data of a specific style */
1032 function getStyleData( &$i, $j, &$body ) {
1035 while( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1039 // And get the color
1041 while( isNoSep( $body{$i} ) &&
1051 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1052 change on with no (onload -> noload)
1055 function stripEvent( &$i, $j, &$body, $id, $base ) {
1061 while( $body{$i} <> '>' &&
1063 $etg = strtolower($body{$i}.$body{$i+
1}.$body{$i+
2});
1066 // Retrolinks are not allowed without a base because they mess with SM security
1076 if( $body{$i+
3} == ':') {
1079 while( isNoSep( $body{$i} ) &&
1081 $name .= $body{$i++
};
1083 $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1084 urlencode( $message->header
->mailbox
) .
1085 "&passed_ent_id=" . find_ent_id( $name, $message );
1086 if( $body{$k} == '"' )
1091 if( $body{$i} == '>' )
1103 if( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+
1}.$body{$i+
2}, 'script:') == 0 ) {
1119 /* This function trys to locate the entity_id of a specific mime element */
1121 function find_ent_id( $id, $message ) {
1124 for ($i=0; $ret == '' && $i < count($message->entities
); $i++
) {
1126 if( $message->entities
[$i]->header
->entity_id
== '' ) {
1127 $ret = find_ent_id( $id, $message->entities
[$i] );
1129 if( strcasecmp( $message->entities
[$i]->header
->id
, $id ) == 0 )
1130 $ret = $message->entities
[$i]->header
->entity_id
;