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 /*****************************************************************/
16 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
17 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
18 /*** + Base level indent should begin at left margin, as ***/
19 /*** the require_once below. ***/
20 /*** + All identation should consist of four space blocks ***/
21 /*** + Tab characters are evil. ***/
22 /*** + all comments should use "slash-star ... star-slash" ***/
23 /*** style -- no pound characters, no slash-slash style ***/
24 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
25 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
26 /*** + Please use ' instead of ", when possible. Note " ***/
27 /*** should always be used in _( ) function calls. ***/
28 /*** Thank you for your help making the SM code more readable. ***/
29 /*****************************************************************/
31 require_once('../functions/imap.php');
32 require_once('../functions/attachment_common.php');
34 /** Setting up the objects that have the structure for the message **/
36 /** msg_header contains generic variables for values that **/
37 /** could be in a header. **/
39 var $type0 = '', $type1 = '', $boundary = '', $charset = '';
40 var $encoding = '', $size = 0, $to = array(), $from = '', $date = '';
41 var $cc = array(), $bcc = array(), $reply_to = '', $subject = '';
42 var $id = 0, $mailbox = '', $description = '', $filename = '';
43 var $entity_id = 0, $message_id = 0, $name = '';
44 // var $priority = "";
48 /** message is the object that contains messages. It is a recursive
49 object in that through the $entities variable, it can contain
50 more objects of type message. See documentation in mime.txt for
51 a better description of how this works.
54 var $entities = array();
56 function addEntity ($msg) {
57 $this->entities
[] = $msg;
61 /* --------------------------------------------------------------------------------- */
63 /* --------------------------------------------------------------------------------- */
65 // This function gets the structure of a message and stores it in the "message" class.
66 // It will return this object for use with all relevant header information and
67 // fully parsed into the standard "message" object format.
68 function mime_structure ($imap_stream, $header) {
70 sqimap_messages_flag ($imap_stream, $header->id
, $header->id
, 'Seen');
71 $ssid = sqimap_session_id();
72 $lsid = strlen( $ssid );
74 fputs ($imap_stream, "$ssid FETCH $id BODYSTRUCTURE\r\n");
76 // This should use sqimap_read_data instead of reading it itself
78 $read = fgets ($imap_stream, 10000);
80 while( substr($read, 0, $lsid) <> $ssid &&
81 !feof( $imap_stream ) ) {
82 $bodystructure .= $read;
83 $read = fgets ($imap_stream, 10000);
85 $read = $bodystructure;
87 // isolate the body structure and remove beginning and end parenthesis
88 $read = trim(substr ($read, strpos(strtolower($read), 'bodystructure') +
13));
89 $read = trim(substr ($read, 0, -1));
90 $end = mime_match_parenthesis(0, $read);
91 while ($end == strlen($read)-1) {
92 $read = trim(substr ($read, 0, -1));
93 $read = trim(substr ($read, 1));
94 $end = mime_match_parenthesis(0, $read);
97 $msg = mime_parse_structure ($read, 0);
98 $msg->header
= $header;
102 // this starts the parsing of a particular structure. It is called recursively,
103 // so it can be passed different structures. It returns an object of type
105 // First, it checks to see if it is a multipart message. If it is, then it
106 // handles that as it sees is necessary. If it is just a regular entity,
107 // then it parses it and adds the necessary header information (by calling out
108 // to mime_get_elements()
109 function mime_parse_structure ($structure, $ent_id) {
111 $msg = new message();
112 if ($structure{0} == '(') {
113 $ent_id = mime_new_element_level($ent_id);
117 $end = mime_match_parenthesis ($start, $structure);
119 $element = substr($structure, $start+
1, ($end - $start)-1);
120 $ent_id = mime_increment_id ($ent_id);
121 $newmsg = mime_parse_structure ($element, $ent_id);
122 $msg->addEntity ($newmsg);
123 } while ($structure{$end+
1} == '(');
125 // parse the elements
126 $msg = mime_get_element ($structure, $msg, $ent_id);
131 // Increments the element ID. An element id can look like any of
132 // the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
133 // the last number of the element id, changing 1.2 to 1.3.
134 function mime_increment_id ($id) {
136 if (strpos($id, ".")) {
137 $first = substr($id, 0, strrpos($id, "."));
138 $last = substr($id, strrpos($id, ".")+
1);
140 $new = $first . "." .$last;
148 // See comment for mime_increment_id().
149 // This adds another level on to the entity_id changing 1.3 to 1.3.0
150 // NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
151 // before it can be used. I left it this way so as not to have
152 // to make a special case if it is the first entity_id. It
153 // always increments it, and that works fine.
154 function mime_new_element_level ($id) {
165 function mime_get_element (&$structure, $msg, $ent_id) {
168 $msg->header
= new msg_header();
169 $msg->header
->entity_id
= $ent_id;
170 $properties = array();
172 while (strlen($structure) > 0) {
173 $structure = trim($structure);
174 $char = $structure{0};
176 if (strtolower(substr($structure, 0, 3)) == 'nil') {
178 $structure = substr($structure, 3);
179 } else if ($char == '"') {
180 // loop through until we find the matching quote, and return that as a string
183 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
187 $structure = substr($structure, strlen($text) +
2);
188 } else if ($char == '(') {
190 $end = mime_match_parenthesis (0, $structure);
191 $sub = substr($structure, 1, $end-1);
192 $properties = mime_get_props($properties, $sub);
193 $structure = substr($structure, strlen($sub) +
2);
195 // loop through until we find a space or an end parenthesis
197 $char = $structure{$pos};
199 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
202 $char = $structure{$pos};
204 $structure = substr($structure, strlen($text));
207 // This is where all the text parts get put into the header
210 $msg->header
->type0
= strtolower($text);
213 $msg->header
->type1
= strtolower($text);
216 // Invisimail enclose images with <>
217 $msg->header
->id
= str_replace( '<', '', str_replace( '>', '', $text ) );
220 $msg->header
->description
= $text;
223 $msg->header
->encoding
= strtolower($text);
226 $msg->header
->size
= $text;
229 if ($msg->header
->type0
== 'text' && $elem_num == 8) {
230 // This is a plain text message, so lets get the number of lines
232 $msg->header
->num_lines
= $text;
234 } else if ($msg->header
->type0
== 'message' && $msg->header
->type1
== 'rfc822' && $elem_num == 8) {
235 // This is an encapsulated message, so lets start all over again and
236 // parse this message adding it on to the existing one.
237 $structure = trim($structure);
238 if ( $structure{0} == '(' ) {
239 $e = mime_match_parenthesis (0, $structure);
240 $structure = substr($structure, 0, $e);
241 $structure = substr($structure, 1);
242 $m = mime_parse_structure($structure, $msg->header
->entity_id
);
244 // the following conditional is there to correct a bug that wasn't
245 // incrementing the entity IDs correctly because of the special case
246 // that message/rfc822 is. This fixes it fine.
247 if (substr($structure, 1, 1) != '(')
248 $m->header
->entity_id
= mime_increment_id(mime_new_element_level($ent_id));
250 // Now we'll go through and reformat the results.
252 for ($i=0; $i < count($m->entities
); $i++
) {
253 $msg->addEntity($m->entities
[$i]);
266 // loop through the additional properties and put those in the various headers
267 if ($msg->header
->type0
!= 'message') {
268 for ($i=0; $i < count($properties); $i++
) {
269 $msg->header
->{$properties[$i]['name']} = $properties[$i]['value'];
276 // I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
277 // figure out how to do this part, so I decided to go to bed. I woke up
278 // in the morning and had a flash of insight. I went to the white-board
279 // and scribbled it out, then spent a bit programming it, and this is the
280 // result. Nothing complicated, but I think my brain was fried yesterday.
281 // Funny how that happens some times.
283 // This gets properties in a nested parenthesisized list. For example,
284 // this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
285 // This returns an array called $props with all paired up properties.
286 // It ignores the "attachment" for now, maybe that should change later
287 // down the road. In this case, what is returned is:
288 // $props[0]["name"] = "filename";
289 // $props[0]["value"] = "luke.tar.gz";
290 function mime_get_props ($props, $structure) {
292 while (strlen($structure) > 0) {
293 $structure = trim($structure);
294 $char = $structure{0};
299 while ( ( $char = $structure{$pos} ) != '"' &&
300 $pos < strlen($structure)) {
304 $structure = trim(substr($structure, strlen($tmp) +
2));
305 $char = $structure{0};
310 while ( ( $char = $structure{$pos} ) != '"' &&
311 $pos < strlen($structure) ) {
315 $structure = trim(substr($structure, strlen($tmp) +
2));
318 $props[$k]['name'] = strtolower($tmp);
319 $props[$k]['value'] = $value;
320 } else if ($char == '(') {
321 $end = mime_match_parenthesis (0, $structure);
322 $sub = substr($structure, 1, $end-1);
325 $props = mime_get_props($props, $sub);
326 $structure = substr($structure, strlen($sub) +
2);
329 } else if ($char == '(') {
330 $end = mime_match_parenthesis (0, $structure);
331 $sub = substr($structure, 1, $end-1);
332 $props = mime_get_props($props, $sub);
333 $structure = substr($structure, strlen($sub) +
2);
341 // Matches parenthesis. It will return the position of the matching
342 // parenthesis in $structure. For instance, if $structure was:
343 // ("text" "plain" ("val1name", "1") nil ... )
345 // then this would return 42 to match up those two.
346 function mime_match_parenthesis ($pos, $structure) {
348 $j = strlen( $structure );
350 // ignore all extra characters
351 // If inside of a string, skip string -- Boundary IDs and other
352 // things can have ) in them.
353 if( $structure{$pos} != '(' )
358 if ($structure{$pos} == ')') {
360 } elseif ($structure{$pos} == '"') {
362 while( $structure{$pos} != '"' &&
364 if (substr($structure, $pos, 2) == '\\"')
366 elseif (substr($structure, $pos, 2) == '\\\\')
370 } elseif ( $structure{$pos} == '(' ) {
371 $pos = mime_match_parenthesis ($pos, $structure);
374 echo "Error decoding mime structure. Report this as a bug!<br>\n";
378 function mime_fetch_body ($imap_stream, $id, $ent_id ) {
379 // do a bit of error correction. If we couldn't find the entity id, just guess
380 // that it is the first one. That is usually the case anyway.
383 $sid = sqimap_session_id();
384 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
385 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
386 $topline = array_shift($data);
387 while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
388 $topline = array_shift($data);
389 $wholemessage = implode('', $data);
390 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
391 $ret = substr( $wholemessage, 0, $regs[1] );
393 There is some information in the content info header that could be important
394 in order to parse html messages. Let's get them here.
396 if( $ret{0} == '<' ) {
397 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id.MIME]\r\n");
398 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
401 foreach( $data as $d ) {
402 if( substr( $d, 0, 13 ) == 'Content-Base:' ) {
407 ( !isNoSep( $d{$i} ) ||
$d{$i} == '"' ) )
410 if( isNoSep( $d{$i} ) )
415 } elseif( $k == 1 && !isnosep( $d{0} ) ) {
416 $base .= substr( $d, 1 );
421 $ret = "<base href=\"$base\">" . $ret;
423 } else if (ereg('"([^"]*)"', $topline, $regs)) {
426 global $where, $what, $mailbox, $passed_id, $startMessage;
427 $par = "mailbox=".urlencode($mailbox)."&passed_id=$passed_id";
428 if (isset($where) && isset($what)) {
429 $par .= "&where=".urlencode($where)."&what=".urlencode($what);
431 $par .= "&startMessage=$startMessage&show_more=0";
433 $par .= '&response='.urlencode($response).'&message='.urlencode($message).
434 '&topline='.urlencode($topline);
436 echo '<b><font color=$color[2]>Body retrieval error. The reason for this is most probably that<BR> ' .
437 'the message is malformed. Please help us making future versions<BR> ' .
438 "better by submitting this message to the developers knowledgebase!<BR>\n" .
439 "<A HREF=\"../src/retrievalerror.php?$par\">Submit message</A><BR>" .
441 "<tt>Response: $response<BR>" .
442 "Message: $message<BR>" .
443 "FETCH line: $topline<BR></tt></font></b>";
445 fputs ($imap_stream, "$sid FETCH $passed_id BODY[]\r\n");
446 $data = sqimap_read_data ($imap_stream, $sid, true, $response, $message);
448 $wholemessage = implode('', $data);
450 $ret = "---------------\n$wholemessage";
456 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
457 // do a bit of error correction. If we couldn't find the entity id, just guess
458 // that it is the first one. That is usually the case anyway.
459 if (!$ent_id) $ent_id = 1;
460 $sid = sqimap_session_id();
461 // Don't kill the connection if the browser is over a dialup
462 // and it would take over 30 seconds to download it.
465 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
468 $read = fgets ($imap_stream,4096);
469 // This could be bad -- if the section has sqimap_session_id() . ' OK'
470 // or similar, it will kill the download.
471 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
472 if (trim($read) == ')==') {
474 $read = fgets ($imap_stream,4096);
475 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
478 echo decodeBody($read1, $encoding) .
479 decodeBody($read, $encoding);
482 echo decodeBody($read, $encoding);
484 $read = fgets ($imap_stream,4096);
489 /* -[ END MIME DECODING ]----------------------------------------------------------- */
493 /** This is the first function called. It decides if this is a multipart
494 message or if it should be handled as a single entity
496 function decodeMime ($imap_stream, &$header) {
497 global $username, $key, $imapServerAddress, $imapPort;
498 return mime_structure ($imap_stream, $header);
501 // This is here for debugging purposese. It will print out a list
502 // of all the entity IDs that are in the $message object.
504 function listEntities ($message) {
506 if ($message->header->entity_id)
507 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
508 for ($i = 0; $message->entities[$i]; $i++) {
509 $msg = listEntities($message->entities[$i], $ent_id);
517 // returns a $message object for a particular entity id
518 function getEntity ($message, $ent_id) {
520 if ($message->header
->entity_id
== $ent_id && strlen($ent_id) == strlen($message->header
->entity_id
)) {
523 for ($i = 0; isset($message->entities
[$i]); $i++
) {
524 $msg = getEntity ($message->entities
[$i], $ent_id);
532 // figures out what entity to display and returns the $message object
534 function findDisplayEntity ($message, $textOnly = 1) {
535 global $show_html_default;
540 if ( $message->header
->type0
== 'multipart' &&
541 ( $message->header
->type1
== 'alternative' ||
542 $message->header
->type1
== 'related' ) &&
543 $show_html_default && ! $textOnly ) {
544 $entity = findDisplayEntityHTML($message);
547 // Show text/plain or text/html -- the first one we find.
549 $message->header
->type0
== 'text' &&
550 ( $message->header
->type1
== 'plain' ||
551 $message->header
->type1
== 'html' ) &&
552 isset($message->header
->entity_id
) ) {
553 $entity = $message->header
->entity_id
;
557 while ($entity == 0 && isset($message->entities
[$i]) ) {
558 $entity = findDisplayEntity($message->entities
[$i], $textOnly);
566 // Shows the HTML version
567 function findDisplayEntityHTML ($message) {
568 if ($message->header
->type0
== 'text' &&
569 $message->header
->type1
== 'html' &&
570 isset($message->header
->entity_id
))
571 return $message->header
->entity_id
;
572 for ($i = 0; isset($message->entities
[$i]); $i ++
) {
573 $entity = findDisplayEntityHTML($message->entities
[$i]);
580 /** This returns a parsed string called $body. That string can then
581 be displayed as the actual message in the HTML. It contains
582 everything needed, including HTML Tags, Attachments at the
585 function formatBody($imap_stream, $message, $color, $wrap_at) {
586 // this if statement checks for the entity to show as the
587 // primary message. To add more of them, just put them in the
588 // order that is their priority.
589 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
592 $id = $message->header
->id
;
593 $urlmailbox = urlencode($message->header
->mailbox
);
595 // Get the right entity and redefine message to be this entity
596 // Pass the 0 to mean that we want the 'best' viewable one
597 $ent_num = findDisplayEntity ($message, 0);
598 $body_message = getEntity($message, $ent_num);
599 if (($body_message->header
->type0
== 'text') ||
600 ($body_message->header
->type0
== 'rfc822')) {
602 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
603 $body = decodeBody($body, $body_message->header
->encoding
);
604 $hookResults = do_hook("message_body", $body);
605 $body = $hookResults[1];
607 // If there are other types that shouldn't be formatted, add
609 if ($body_message->header
->type1
== 'html') {
610 if( $show_html_default <> 1 ) {
611 $body = strip_tags( $body );
612 translateText($body, $wrap_at, $body_message->header
->charset
);
614 $body = MagicHTML( $body, $id );
617 translateText($body, $wrap_at, $body_message->header
->charset
);
620 $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>";
622 /** Display the ATTACHMENTS: message if there's more than one part **/
623 $body .= "</TD></TR></TABLE>";
624 if (isset($message->entities
[0])) {
625 $body .= formatAttachments ($message, $ent_num, $message->header
->mailbox
, $id);
627 $body .= "</TD></TR></TABLE>";
629 $body = formatAttachments ($message, -1, $message->header
->mailbox
, $id);
634 // A recursive function that returns a list of attachments with links
635 // to where to download these attachments
636 function formatAttachments ($message, $ent_id, $mailbox, $id) {
637 global $where, $what;
638 global $startMessage, $color;
639 static $ShownHTML = 0;
642 if ($ShownHTML == 0) {
645 $body .= "<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
646 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
647 _("Attachments") . ':' .
648 "</B></TH></TR><TR><TD>\n" .
649 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
650 formatAttachments ($message, $ent_id, $mailbox, $id) .
651 "</TABLE></TD></TR></TABLE>";
657 if (!$message->entities
) {
658 $type0 = strtolower($message->header
->type0
);
659 $type1 = strtolower($message->header
->type1
);
660 $name = decodeHeader($message->header
->name
);
662 if ($message->header
->entity_id
!= $ent_id) {
663 $filename = decodeHeader($message->header
->filename
);
664 if (trim($filename) == '') {
665 if (trim($name) == '') {
666 if( trim( $message->header
->id
) == '' )
667 $display_filename = 'untitled-[' . $message->header
->entity_id
. ']' ;
669 $display_filename = 'cid: ' . $message->header
->id
;
670 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
672 $display_filename = $name;
676 $display_filename = $filename;
679 $urlMailbox = urlencode($mailbox);
680 $ent = urlencode($message->header
->entity_id
);
683 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
685 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
686 $Links['download link']['text'] = _("download");
687 $Links['download link']['href'] =
688 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
691 $HookResults = do_hook("attachment $type0/$type1", $Links,
692 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
693 $display_filename, $where, $what);
695 $Links = $HookResults[1];
696 $DefaultLink = $HookResults[6];
698 $body .= '<TR><TD> </TD><TD>' .
699 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
700 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
701 '</b> </small></TD>' .
702 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
704 if ($message->header
->description
)
705 $body .= '<b>' . htmlspecialchars($message->header
->description
) . '</b>';
706 $body .= '</SMALL></TD><TD><SMALL> ';
710 foreach ($Links as $Val) {
714 $body .= ' | ';
716 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
721 $body .= "</SMALL></TD></TR>\n";
724 for ($i = 0; $i < count($message->entities
); $i++
) {
725 $body .= formatAttachments ($message->entities
[$i], $ent_id, $mailbox, $id);
733 /** this function decodes the body depending on the encoding type. **/
734 function decodeBody($body, $encoding) {
735 $body = str_replace("\r\n", "\n", $body);
736 $encoding = strtolower($encoding);
738 global $show_html_default;
740 if ($encoding == 'quoted-printable') {
741 $body = quoted_printable_decode($body);
745 Following code has been comented as I see no reason for it.
746 If there is any please tell me a mingo@rotedic.com
748 while (ereg("=\n", $body))
749 $body = ereg_replace ("=\n", "", $body);
751 } else if ($encoding == 'base64') {
752 $body = base64_decode($body);
755 // All other encodings are returned raw.
760 // This functions decode strings that is encoded according to
761 // RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
762 function decodeHeader ($string) {
763 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
765 if (ucfirst($res[2]) == "B") {
766 $replace = base64_decode($res[3]);
768 $replace = ereg_replace("_", " ", $res[3]);
769 // Convert lowercase Quoted Printable to uppercase for
770 // quoted_printable_decode to understand it.
771 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))", $replace, $res)) {
772 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
774 $replace = quoted_printable_decode($replace);
777 $replace = charset_decode ($res[1], $replace);
779 // Remove the name of the character set.
780 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
783 // In case there should be more encoding in the string: recurse
784 return (decodeHeader($string));
789 // Encode a string according to RFC 1522 for use in headers if it
790 // contains 8-bit characters or anything that looks like it should
792 function encodeHeader ($string) {
793 global $default_charset;
795 // Encode only if the string contains 8-bit characters or =?
796 $j = strlen( $string );
797 $l = FALSE; // Must be encoded ?
799 for( $i=0; $i < $j; ++
$i) {
800 switch( $string{$i} ) {
815 $k = ord( $string{$i} );
817 $ret .= sprintf("=%02X", $k);
825 $string = "=?$default_charset?Q?$ret?=";
831 Strips dangerous tags from html messages.
834 function MagicHTML( $body, $id ) {
836 global $message, $PHP_SELF, $HTTP_SERVER_VARS;
838 $j = strlen( $body ); // Legnth of the HTML
839 $ret = ''; // Returned string
840 $bgcolor = '#ffffff'; // Background style color (defaults to white)
841 $textcolor = '#000000'; // Foreground style color (defaults to black)
842 $leftmargin = ''; // Left margin style
843 $title = ''; // HTML title if any
847 if( $body{$i} == '<' ) {
850 while ($body{$pos} == ' ' ||
$body{$pos} == "\t" ||
853 while (strlen($tag) < 4 && $body{$pos} != ' ' &&
854 $body{$pos} != "\t" && $body{$pos} != "\n") {
858 switch( strtoupper( $tag ) ) {
859 // Strips the entire tag and contents
866 while( $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4}.$body{$i+
5} <> $etg &&
868 while( $i < $j && $body{++
$i} <> '>' );
869 // $ret .= "<!-- $tag removed -->";
874 while( $body{$i} <> '>' && // </title>
879 while( $body{$i} <> '<' && // </title>
886 // Destroy these tags
897 while( $body{$i} <> '>' &&
904 while( $body{$i} <> '>' && // </title>
908 // We parse the style to look for interesting stuff
910 while( $body{$i} <> '>' &&
912 // First we get the name of the style
914 while( $body{$i} <> '>' &&
918 if( isnoSep( $body{$i} ) )
922 stripComments( $i, $j, $body );
923 $style = strtoupper( trim( $style ) );
924 if( $style == 'BODY' ) {
925 // Next we look into the definitions of the body style
926 while( $body{$i} <> '>' &&
929 // We look for the background color if any.
930 if( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
932 $bgcolor = getStyleData( $i, $j, $body );
933 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
935 $leftmargin = getStyleData( $i, $j, $body );
940 // Other style are mantained
941 $styleblk .= "$style ";
942 while( $body{$i} <> '>' &&
946 $styleblk .= $body{$i};
949 $styleblk .= $body{$i};
951 stripComments( $i, $j, $body );
952 if( $body{$i} <> '>' )
955 if( $styleblk <> '' )
956 $ret .= "<style>$styleblk";
960 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
965 $ret .= stripEvent( $i, $j, $body, $id, $base );
966 $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
967 if( $leftmargin <> '' )
968 $ret .= "<td width=$leftmargin> </td>";
970 if (strtolower($bgcolor) == 'ffffff' ||
971 strtolower($bgcolor) == '#ffffff')
972 $ret .= '<font color=#000000>';
977 while( !isNoSep( $body{$i} ) &&
980 if( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
982 while( !isNoSep( $body{$i} ) &&
985 while( $body{$i} <> '>' &&
987 if( $body{$i} <> '"' )
991 // Debuging $ret .= "<!-- base == $base -->";
992 if( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 )
993 $ret .= "\n<BASE HREF=\"$base\">\n";
997 $ret .= '</font></td></tr></TABLE>';
1001 // Following tags can contain some event handler, lets search it
1002 stripComments( $i, $j, $body );
1005 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
1006 // $ret .= "<!-- $tag detected -->";
1014 return( "\n\n<!-- HTML Output ahead -->\n" .
1016 "\n<!-- END of HTML Output --><base href=\"".
1017 $HTTP_SERVER_VARS["SERVER_NAME"] . substr( $PHP_SELF, 0, strlen( $PHP_SELF ) - 13 ) .
1021 function isNoSep( $char ) {
1039 The following function is usefull to remove extra data that can cause
1040 html not to display properly. Especialy with MS stuff.
1043 function stripComments( &$i, $j, &$body ) {
1045 while( $body{$i}.$body{$i+
1}.$body{$i+
2}.$body{$i+
3} == '<!--' &&
1048 while( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1058 /* Gets the style data of a specific style */
1060 function getStyleData( &$i, $j, &$body ) {
1063 while( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1067 // And get the color
1069 while( isNoSep( $body{$i} ) &&
1079 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1080 change on with no (onload -> noload)
1083 function stripEvent( &$i, $j, &$body, $id, $base ) {
1089 while( $body{$i} <> '>' &&
1091 $etg = strtolower($body{$i}.$body{$i+
1}.$body{$i+
2});
1094 // Retrolinks are not allowed without a base because they mess with SM security
1104 if( $body{$i+
3} == ':') {
1107 while( isNoSep( $body{$i} ) &&
1109 $name .= $body{$i++
};
1111 $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1112 urlencode( $message->header
->mailbox
) .
1113 "&passed_ent_id=" . find_ent_id( $name, $message );
1114 if( $body{$k} == '"' )
1119 if( $body{$i} == '>' )
1131 if( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+
1}.$body{$i+
2}, 'script:') == 0 ) {
1147 /* This function trys to locate the entity_id of a specific mime element */
1149 function find_ent_id( $id, $message ) {
1152 for ($i=0; $ret == '' && $i < count($message->entities
); $i++
) {
1154 if( $message->entities
[$i]->header
->entity_id
== '' ) {
1155 $ret = find_ent_id( $id, $message->entities
[$i] );
1157 if( strcasecmp( $message->entities
[$i]->header
->id
, $id ) == 0 )
1158 $ret = $message->entities
[$i]->header
->entity_id
;