e9a6b4a970b51b862c19199fa97e43bfcfb091c2
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains the functions necessary to detect and decode MIME
15 require_once('../functions/imap.php');
16 require_once('../functions/attachment_common.php');
18 /** Setting up the objects that have the structure for the message **/
20 /** msg_header contains generic variables for values that **/
21 /** could be in a header. **/
23 var $type0 = '', $type1 = '', $boundary = '', $charset = '',
24 $encoding = '', $size = 0, $to = array(), $from = '', $date = '',
25 $cc = array(), $bcc = array(), $reply_to = '', $subject = '',
26 $id = 0, $mailbox = '', $description = '', $filename = '',
27 $entity_id = 0, $message_id = 0, $name = '', $priority = 3;
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.
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;
87 /* this starts the parsing of a particular structure. It is called recursively,
88 * so it can be passed different structures. It returns an object of type
90 * First, it checks to see if it is a multipart message. If it is, then it
91 * handles that as it sees is necessary. If it is just a regular entity,
92 * then it parses it and adds the necessary header information (by calling out
93 * to mime_get_elements()
95 function mime_parse_structure ($structure, $ent_id) {
98 if ($structure{0} == '(') {
99 $ent_id = mime_new_element_level($ent_id);
103 $end = mime_match_parenthesis ($start, $structure);
105 $element = substr($structure, $start+
1, ($end - $start)-1);
106 $ent_id = mime_increment_id ($ent_id);
107 $newmsg = mime_parse_structure ($element, $ent_id);
108 $msg->addEntity ($newmsg);
109 } while ($structure{$end+
1} == '(');
111 // parse the elements
112 $msg = mime_get_element ($structure, $msg, $ent_id);
117 /* Increments the element ID. An element id can look like any of
118 * the following: 1, 1.2, 4.3.2.4.1, etc. This function increments
119 * the last number of the element id, changing 1.2 to 1.3.
121 function mime_increment_id ($id) {
123 if (strpos($id, ".")) {
124 $first = substr($id, 0, strrpos($id, "."));
125 $last = substr($id, strrpos($id, ".")+
1);
127 $new = $first . "." .$last;
136 * See comment for mime_increment_id().
137 * This adds another level on to the entity_id changing 1.3 to 1.3.0
138 * NOTE: 1.3.0 is not a valid element ID. It MUST be incremented
139 * before it can be used. I left it this way so as not to have
140 * to make a special case if it is the first entity_id. It
141 * always increments it, and that works fine.
143 function mime_new_element_level ($id) {
154 function mime_get_element (&$structure, $msg, $ent_id) {
157 $msg->header
= new msg_header();
158 $msg->header
->entity_id
= $ent_id;
159 $properties = array();
161 while (strlen($structure) > 0) {
162 $structure = trim($structure);
163 $char = $structure{0};
165 if (strtolower(substr($structure, 0, 3)) == 'nil') {
167 $structure = substr($structure, 3);
168 } else if ($char == '"') {
169 // loop through until we find the matching quote, and return that as a string
172 while ( ($char = $structure{$pos} ) <> '"' && $pos < strlen($structure)) {
176 $structure = substr($structure, strlen($text) +
2);
177 } else if ($char == '(') {
179 $end = mime_match_parenthesis (0, $structure);
180 $sub = substr($structure, 1, $end-1);
181 $properties = mime_get_props($properties, $sub);
182 $structure = substr($structure, strlen($sub) +
2);
184 // loop through until we find a space or an end parenthesis
186 $char = $structure{$pos};
188 while ($char != ' ' && $char != ')' && $pos < strlen($structure)) {
191 $char = $structure{$pos};
193 $structure = substr($structure, strlen($text));
196 // This is where all the text parts get put into the header
199 $msg->header
->type0
= strtolower($text);
202 $msg->header
->type1
= strtolower($text);
205 // Invisimail enclose images with <>
206 $msg->header
->id
= str_replace( '<', '', str_replace( '>', '', $text ) );
209 $msg->header
->description
= $text;
212 $msg->header
->encoding
= strtolower($text);
215 $msg->header
->size
= $text;
218 if ($msg->header
->type0
== 'text' && $elem_num == 8) {
219 // This is a plain text message, so lets get the number of lines
221 $msg->header
->num_lines
= $text;
223 } else if ($msg->header
->type0
== 'message' && $msg->header
->type1
== 'rfc822' && $elem_num == 8) {
224 // This is an encapsulated message, so lets start all over again and
225 // parse this message adding it on to the existing one.
226 $structure = trim($structure);
227 if ( $structure{0} == '(' ) {
228 $e = mime_match_parenthesis (0, $structure);
229 $structure = substr($structure, 0, $e);
230 $structure = substr($structure, 1);
231 $m = mime_parse_structure($structure, $msg->header
->entity_id
);
233 // the following conditional is there to correct a bug that wasn't
234 // incrementing the entity IDs correctly because of the special case
235 // that message/rfc822 is. This fixes it fine.
236 if (substr($structure, 1, 1) != '(')
237 $m->header
->entity_id
= mime_increment_id(mime_new_element_level($ent_id));
239 // Now we'll go through and reformat the results.
241 for ($i=0; $i < count($m->entities
); $i++
) {
242 $msg->addEntity($m->entities
[$i]);
255 // loop through the additional properties and put those in the various headers
256 if ($msg->header
->type0
!= 'message') {
257 for ($i=0; $i < count($properties); $i++
) {
258 $msg->header
->{$properties[$i]['name']} = $properties[$i]['value'];
266 * I did most of the MIME stuff yesterday (June 20, 2000), but I couldn't
267 * figure out how to do this part, so I decided to go to bed. I woke up
268 * in the morning and had a flash of insight. I went to the white-board
269 * and scribbled it out, then spent a bit programming it, and this is the
270 * result. Nothing complicated, but I think my brain was fried yesterday.
271 * Funny how that happens some times.
273 * This gets properties in a nested parenthesisized list. For example,
274 * this would get passed something like: ("attachment" ("filename" "luke.tar.gz"))
275 * This returns an array called $props with all paired up properties.
276 * It ignores the "attachment" for now, maybe that should change later
277 * down the road. In this case, what is returned is:
278 * $props[0]["name"] = "filename";
279 * $props[0]["value"] = "luke.tar.gz";
281 function mime_get_props ($props, $structure) {
283 while (strlen($structure) > 0) {
284 $structure = trim($structure);
285 $char = $structure{0};
290 while ( ( $char = $structure{$pos} ) != '"' &&
291 $pos < strlen($structure)) {
295 $structure = trim(substr($structure, strlen($tmp) +
2));
296 $char = $structure{0};
301 while ( ( $char = $structure{$pos} ) != '"' &&
302 $pos < strlen($structure) ) {
306 $structure = trim(substr($structure, strlen($tmp) +
2));
309 $props[$k]['name'] = strtolower($tmp);
310 $props[$k]['value'] = $value;
311 } else if ($char == '(') {
312 $end = mime_match_parenthesis (0, $structure);
313 $sub = substr($structure, 1, $end-1);
316 $props = mime_get_props($props, $sub);
317 $structure = substr($structure, strlen($sub) +
2);
320 } else if ($char == '(') {
321 $end = mime_match_parenthesis (0, $structure);
322 $sub = substr($structure, 1, $end-1);
323 $props = mime_get_props($props, $sub);
324 $structure = substr($structure, strlen($sub) +
2);
333 * Matches parenthesis. It will return the position of the matching
334 * parenthesis in $structure. For instance, if $structure was:
335 * ("text" "plain" ("val1name", "1") nil ... )
337 * then this would return 42 to match up those two.
339 function mime_match_parenthesis ($pos, $structure) {
341 $j = strlen( $structure );
343 // ignore all extra characters
344 // If inside of a string, skip string -- Boundary IDs and other
345 // things can have ) in them.
346 if ( $structure{$pos} != '(' ) {
350 while ( $pos < $j ) {
352 if ($structure{$pos} == ')') {
354 } elseif ($structure{$pos} == '"') {
356 while ( $structure{$pos} != '"' &&
358 if (substr($structure, $pos, 2) == '\\"') {
360 } elseif (substr($structure, $pos, 2) == '\\\\') {
365 } elseif ( $structure{$pos} == '(' ) {
366 $pos = mime_match_parenthesis ($pos, $structure);
369 echo _("Error decoding mime structure. Report this as a bug!") . '<br>';
373 function mime_fetch_body ($imap_stream, $id, $ent_id ) {
374 // do a bit of error correction. If we couldn't find the entity id, just guess
375 // that it is the first one. That is usually the case anyway.
378 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id]", true, $response, $message);
379 $topline = array_shift($data);
380 while (! ereg('\\* [0-9]+ FETCH ', $topline) && $data)
381 $topline = array_shift($data);
382 $wholemessage = implode('', $data);
383 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
384 $ret = substr( $wholemessage, 0, $regs[1] );
386 There is some information in the content info header that could be important
387 in order to parse html messages. Let's get them here.
389 if ( $ret{0} == '<' ) {
390 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message);
393 foreach( $data as $d ) {
394 if ( substr( $d, 0, 13 ) == 'Content-Base:' ) {
399 ( !isNoSep( $d{$i} ) ||
$d{$i} == '"' ) )
402 if ( isNoSep( $d{$i} ) )
407 } elseif ( $k == 1 && !isnosep( $d{0} ) ) {
408 $base .= substr( $d, 1 );
414 $ret = "<base href=\"$base\">" . $ret;
416 } else if (ereg('"([^"]*)"', $topline, $regs)) {
419 global $where, $what, $mailbox, $passed_id, $startMessage;
420 $par = "mailbox=".urlencode($mailbox)."&passed_id=$passed_id";
421 if (isset($where) && isset($what)) {
422 $par .= "&where=".urlencode($where)."&what=".urlencode($what);
424 $par .= "&startMessage=$startMessage&show_more=0";
426 $par .= '&response='.urlencode($response).'&message='.urlencode($message).
427 '&topline='.urlencode($topline);
429 echo '<b><font color=$color[2]>' .
430 _("Body retrieval error. The reason for this is most probably that the message is malformed. Please help us making future versions better by submitting this message to the developers knowledgebase!") .
431 "<A HREF=\"../src/retrievalerror.php?$par\">Submit message</A><BR>" .
432 '<tt>' . _("Response:") . "$response<BR>" .
433 _("Message:") . " $message<BR>" .
434 _("FETCH line:") . " $topline<BR></tt></font></b>";
436 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message);
438 $wholemessage = implode('', $data);
440 $ret = "---------------\n$wholemessage";
446 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
447 // do a bit of error correction. If we couldn't find the entity id, just guess
448 // that it is the first one. That is usually the case anyway.
452 $sid = sqimap_session_id();
453 // Don't kill the connection if the browser is over a dialup
454 // and it would take over 30 seconds to download it.
456 // don´t call set_time_limit in safe mode.
457 if (!ini_get("safe_mode")) {
461 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
464 $read = fgets ($imap_stream,4096);
465 // This could be bad -- if the section has sqimap_session_id() . ' OK'
466 // or similar, it will kill the download.
467 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
468 if (trim($read) == ')==') {
470 $read = fgets ($imap_stream,4096);
471 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
474 echo decodeBody($read1, $encoding) .
475 decodeBody($read, $encoding);
478 echo decodeBody($read, $encoding);
480 $read = fgets ($imap_stream,4096);
485 /* -[ END MIME DECODING ]----------------------------------------------------------- */
489 /* This is the first function called. It decides if this is a multipart
490 message or if it should be handled as a single entity
492 function decodeMime ($imap_stream, &$header) {
493 global $username, $key, $imapServerAddress, $imapPort;
494 return mime_structure ($imap_stream, $header);
497 // This is here for debugging purposese. It will print out a list
498 // of all the entity IDs that are in the $message object.
500 function listEntities ($message) {
502 if ($message->header->entity_id)
503 echo "<tt>" . $message->header->entity_id . ' : ' . $message->header->type0 . '/' . $message->header->type1 . '<br>';
504 for ($i = 0; $message->entities[$i]; $i++) {
505 $msg = listEntities($message->entities[$i], $ent_id);
513 /* returns a $message object for a particular entity id */
514 function getEntity ($message, $ent_id) {
516 if ($message->header
->entity_id
== $ent_id && strlen($ent_id) == strlen($message->header
->entity_id
)) {
519 for ($i = 0; isset($message->entities
[$i]); $i++
) {
520 $msg = getEntity ($message->entities
[$i], $ent_id);
530 * figures out what entity to display and returns the $message object
533 function findDisplayEntity ($message, $textOnly = 1) {
534 global $show_html_default;
539 if ( $message->header
->type0
== 'multipart' &&
540 ( $message->header
->type1
== 'alternative' ||
541 $message->header
->type1
== 'related' ) &&
542 $show_html_default && ! $textOnly ) {
543 $entity = findDisplayEntityHTML($message);
546 // Show text/plain or text/html -- the first one we find.
548 $message->header
->type0
== 'text' &&
549 ( $message->header
->type1
== 'plain' ||
550 $message->header
->type1
== 'html' ) &&
551 isset($message->header
->entity_id
) ) {
552 $entity = $message->header
->entity_id
;
556 while ($entity == 0 && isset($message->entities
[$i]) ) {
557 $entity = findDisplayEntity($message->entities
[$i], $textOnly);
565 /* Shows the HTML version */
566 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
;
573 for ($i = 0; isset($message->entities
[$i]); $i ++
) {
574 $entity = findDisplayEntityHTML($message->entities
[$i]);
583 /* This returns a parsed string called $body. That string can then
584 be displayed as the actual message in the HTML. It contains
585 everything needed, including HTML Tags, Attachments at the
588 function formatBody($imap_stream, $message, $color, $wrap_at) {
589 // this if statement checks for the entity to show as the
590 // primary message. To add more of them, just put them in the
591 // order that is their priority.
592 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
595 $id = $message->header
->id
;
596 $urlmailbox = urlencode($message->header
->mailbox
);
598 // Get the right entity and redefine message to be this entity
599 // Pass the 0 to mean that we want the 'best' viewable one
600 $ent_num = findDisplayEntity ($message, 0);
601 $body_message = getEntity($message, $ent_num);
602 if (($body_message->header
->type0
== 'text') ||
603 ($body_message->header
->type0
== 'rfc822')) {
605 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
606 $body = decodeBody($body, $body_message->header
->encoding
);
607 $hookResults = do_hook("message_body", $body);
608 $body = $hookResults[1];
610 // If there are other types that shouldn't be formatted, add
612 if ($body_message->header
->type1
== 'html') {
613 if ( $show_html_default <> 1 ) {
614 $body = strip_tags( $body );
615 translateText($body, $wrap_at, $body_message->header
->charset
);
617 $body = MagicHTML( $body, $id );
620 translateText($body, $wrap_at, $body_message->header
->charset
);
623 $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>";
625 /** Display the ATTACHMENTS: message if there's more than one part **/
626 $body .= "</TD></TR></TABLE>";
627 if (isset($message->entities
[0])) {
628 $body .= formatAttachments ($message, $ent_num, $message->header
->mailbox
, $id);
630 $body .= "</TD></TR></TABLE>";
632 $body = formatAttachments ($message, -1, $message->header
->mailbox
, $id);
638 * A recursive function that returns a list of attachments with links
639 * to where to download these attachments
641 function formatAttachments ($message, $ent_id, $mailbox, $id) {
642 global $where, $what;
643 global $startMessage, $color;
644 static $ShownHTML = 0;
647 if ($ShownHTML == 0) {
650 $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
651 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
652 _("Attachments") . ':' .
653 "</B></TH></TR><TR><TD>\n" .
654 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
655 formatAttachments ($message, $ent_id, $mailbox, $id) .
656 "</TABLE></TD></TR></TABLE>";
662 if (!$message->entities
) {
663 $type0 = strtolower($message->header
->type0
);
664 $type1 = strtolower($message->header
->type1
);
665 $name = decodeHeader($message->header
->name
);
667 if ($message->header
->entity_id
!= $ent_id) {
668 $filename = decodeHeader($message->header
->filename
);
669 if (trim($filename) == '') {
670 if (trim($name) == '') {
671 if ( trim( $message->header
->id
) == '' )
672 $display_filename = 'untitled-[' . $message->header
->entity_id
. ']' ;
674 $display_filename = 'cid: ' . $message->header
->id
;
675 // $display_filename = 'untitled-[' . $message->header->entity_id . ']' ;
677 $display_filename = $name;
681 $display_filename = $filename;
684 $urlMailbox = urlencode($mailbox);
685 $ent = urlencode($message->header
->entity_id
);
688 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
690 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
691 $Links['download link']['text'] = _("download");
692 $Links['download link']['href'] =
693 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
696 /* this executes the attachment hook with a specific MIME-type.
697 * if that doens't have results, it tries if there's a rule
698 * for a more generic type. */
699 $HookResults = do_hook("attachment $type0/$type1", $Links,
700 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
701 $display_filename, $where, $what);
702 if(count($HookResults[1]) <= 1) {
703 $HookResults = do_hook("attachment $type0/*", $Links,
704 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
705 $display_filename, $where, $what);
708 $Links = $HookResults[1];
709 $DefaultLink = $HookResults[6];
711 $body .= '<TR><TD> </TD><TD>' .
712 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
713 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
714 '</b> </small></TD>' .
715 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
717 if ($message->header
->description
)
718 $body .= '<b>' . htmlspecialchars($message->header
->description
) . '</b>';
719 $body .= '</SMALL></TD><TD><SMALL> ';
723 foreach ($Links as $Val) {
727 $body .= ' | ';
729 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
734 $body .= "</SMALL></TD></TR>\n";
737 for ($i = 0; $i < count($message->entities
); $i++
) {
738 $body .= formatAttachments ($message->entities
[$i], $ent_id, $mailbox, $id);
746 /** this function decodes the body depending on the encoding type. **/
747 function decodeBody($body, $encoding) {
748 $body = str_replace("\r\n", "\n", $body);
749 $encoding = strtolower($encoding);
751 global $show_html_default;
753 if ($encoding == 'quoted-printable') {
754 $body = quoted_printable_decode($body);
757 while (ereg("=\n", $body))
758 $body = ereg_replace ("=\n", "", $body);
760 } else if ($encoding == 'base64') {
761 $body = base64_decode($body);
764 // All other encodings are returned raw.
769 * This functions decode strings that is encoded according to
770 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
772 function decodeHeader ($string, $utfencode=true) {
774 if ( is_array( $string ) ) {
775 $string = implode("\n", $string );
778 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
780 if (ucfirst($res[2]) == 'B') {
781 $replace = base64_decode($res[3]);
783 $replace = str_replace('_', ' ', $res[3]);
784 // Convert lowercase Quoted Printable to uppercase for
785 // quoted_printable_decode to understand it.
786 while (ereg("(=(([0-9][abcdef])|([abcdef][0-9])|([abcdef][abcdef])))",
788 $replace = str_replace($res[1], strtoupper($res[1]), $replace);
790 $replace = quoted_printable_decode($replace);
792 /* Only encode into entities by default. Some places
793 don't need the encoding, like the compose form. */
795 $replace = charset_decode ($res[1], $replace);
798 // Remove the name of the character set.
799 $string = eregi_replace ('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=',
802 // In case there should be more encoding in the string: recurse
803 $string = decodeHeader($string);
810 * Encode a string according to RFC 1522 for use in headers if it
811 * contains 8-bit characters or anything that looks like it should
814 function encodeHeader ($string) {
815 global $default_charset;
817 // Encode only if the string contains 8-bit characters or =?
818 $j = strlen( $string );
819 $l = strstr($string, '=?'); // Must be encoded ?
821 for( $i=0; $i < $j; ++
$i) {
822 switch( $string{$i} ) {
836 $k = ord( $string{$i} );
838 $ret .= sprintf("=%02X", $k);
846 $string = "=?$default_charset?Q?$ret?=";
853 Strips dangerous tags from html messages.
855 function MagicHTML( $body, $id ) {
857 global $message, $HTTP_SERVER_VARS,
858 $attachment_common_show_images;
860 $attachment_common_show_images =
861 FALSE; // Don't display attached images in HTML mode
862 $j = strlen( $body ); // Legnth of the HTML
863 $ret = ''; // Returned string
864 $bgcolor = '#ffffff'; // Background style color (defaults to white)
865 $textcolor = '#000000'; // Foreground style color (defaults to black)
866 $leftmargin = ''; // Left margin style
867 $title = ''; // HTML title if any
871 if ( $body{$i} == '<' ) {
874 while ($body{$pos} == ' ' ||
$body{$pos} == "\t" ||
875 $body{$pos} == "\n") {
878 while (strlen($tag) < 4 && $body{$pos} != ' ' &&
879 $body{$pos} != "\t" && $body{$pos} != "\n") {
883 switch( strtoupper( $tag ) ) {
884 // Strips the entire tag and contents
891 while ( $body{$i+
1}.$body{$i+
2}.$body{$i+
3}.$body{$i+
4}.$body{$i+
5} <> $etg &&
893 while ( $i < $j && $body{++
$i} <> '>' );
894 // $ret .= "<!-- $tag removed -->";
899 while ( $body{$i} <> '>' && // </title>
904 while ( $body{$i} <> '<' && // </title>
911 // Destroy these tags
922 while ( $body{$i} <> '>' &&
929 while ( $body{$i} <> '>' && // </title>
933 // We parse the style to look for interesting stuff
935 while ( $body{$i} <> '>' &&
937 // First we get the name of the style
939 while ( $body{$i} <> '>' &&
943 if ( isnoSep( $body{$i} ) )
947 stripComments( $i, $j, $body );
948 $style = strtoupper( trim( $style ) );
949 if ( $style == 'BODY' ) {
950 // Next we look into the definitions of the body style
951 while ( $body{$i} <> '>' &&
954 // We look for the background color if any.
955 if ( substr( $body, $i, 17 ) == 'BACKGROUND-COLOR:' ) {
957 $bgcolor = getStyleData( $i, $j, $body );
958 } elseif ( substr( $body, $i, 12 ) == 'MARGIN-LEFT:' ) {
960 $leftmargin = getStyleData( $i, $j, $body );
965 // Other style are mantained
966 $styleblk .= "$style ";
967 while ( $body{$i} <> '>' &&
971 $styleblk .= $body{$i};
974 $styleblk .= $body{$i};
976 stripComments( $i, $j, $body );
977 if ( $body{$i} <> '>' )
980 if ( $styleblk <> '' )
981 $ret .= "<style>$styleblk";
985 $ret .= '<b>' . _("Title:") . " </b>$title<br>\n";
988 if (! isset($base)) {
991 $ret .= stripEvent( $i, $j, $body, $id, $base );
992 $ret .= " bgcolor=$bgcolor width=\"100%\"><tr>";
993 if ( $leftmargin <> '' )
994 $ret .= "<td width=$leftmargin> </td>";
996 if (strtolower($bgcolor) == 'ffffff' ||
997 strtolower($bgcolor) == '#ffffff')
998 $ret .= '<font color=#000000>';
1003 while ( !isNoSep( $body{$i} ) &&
1007 if ( strcasecmp( substr( $base, 0, 4 ), 'href' ) ) {
1009 while ( !isNoSep( $body{$i} ) &&
1013 while ( $body{$i} <> '>' &&
1015 if ( $body{$i} <> '"' ) {
1020 // Debuging $ret .= "<!-- base == $base -->";
1021 if ( strcasecmp( substr( $base, 0, 4 ), 'file' ) <> 0 ) {
1022 $ret .= "\n<BASE HREF=\"$base\">\n";
1027 $ret .= '</font></td></tr></TABLE>';
1031 // Following tags can contain some event handler, lets search it
1032 stripComments( $i, $j, $body );
1033 if (! isset($base)) {
1036 $ret .= stripEvent( $i, $j, $body, $id, $base ) . '>';
1037 // $ret .= "<!-- $tag detected -->";
1045 return( "\n\n<!-- HTML Output ahead -->\n" .
1047 "\n<!-- END of HTML Output --><base href=\"".
1048 get_location() . '/'.
1052 function isNoSep( $char ) {
1070 The following function is usefull to remove extra data that can cause
1071 html not to display properly. Especialy with MS stuff.
1074 function stripComments( &$i, $j, &$body ) {
1076 while ( $body{$i}.$body{$i+
1}.$body{$i+
2}.$body{$i+
3} == '<!--' &&
1079 while ( $body{$i-2}.$body{$i-1}.$body{$i} <> '-->' &&
1089 /* Gets the style data of a specific style */
1091 function getStyleData( &$i, $j, &$body ) {
1094 while ( $body{$i} <> '>' && !isNoSep( $body{$i} ) &&
1098 // And get the color
1100 while ( isNoSep( $body{$i} ) &&
1110 Private function for strip_dangerous_tag. Look for event based coded and "remove" it
1111 change on with no (onload -> noload)
1114 function stripEvent( &$i, $j, &$body, $id, $base ) {
1116 global $message, $base_uri;
1120 while ( $body{$i} <> '>' &&
1122 $etg = strtolower($body{$i}.$body{$i+
1}.$body{$i+
2});
1125 // This is probably a src specification
1127 while( !isNoSep( $body{$k} )) {
1130 if ( $body{$k} == '=' ) {
1133 while( !isNoSep( $body{$k} ) &&
1138 while ( $body{$k} <> '>' && isNoSep( $body{$k} ) &&
1143 while( !isNoSep( $body{$k} ) &&
1147 if ( strtolower( substr( $src, 0, 4 ) ) == 'cid:' ) {
1148 $src = substr( $src, 4 );
1149 $src = "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1150 urlencode( $message->header
->mailbox
) .
1151 "&passed_ent_id=" . find_ent_id( $src, $message );
1152 } else if ( strtolower( substr( $src, 0, 4 ) ) <> 'http' ||
1153 stristr( $src, $base_uri ) ) {
1154 /* Javascript and local urls goes out */
1155 $src = '../images/' . _("sec_remove_eng.png");
1157 $ret .= 'src="' . $src . '" ';
1166 // Retrolinks are not allowed without a base because they mess with SM security
1167 if ( $base == '' ) {
1176 if ( $body{$i+
3} == ':') {
1179 while ( isNoSep( $body{$i} ) &&
1181 $name .= $body{$i++
};
1183 if ( $name <> '' ) {
1184 $ret .= "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=" .
1185 urlencode( $message->header
->mailbox
) .
1186 "&passed_ent_id=" . find_ent_id( $name, $message );
1187 if ( $body{$k} == '"' )
1192 if ( $body{$i} == '>' )
1204 if ( strcasecmp( $body{$i-4}.$body{$i-3}.$body{$i-2}.$body{$i-1}.$body{$i}.$body{$i+
1}.$body{$i+
2}, 'script:') == 0 ) {
1220 /* This function trys to locate the entity_id of a specific mime element */
1222 function find_ent_id( $id, $message ) {
1225 for ($i=0; $ret == '' && $i < count($message->entities
); $i++
) {
1227 if ( $message->entities
[$i]->header
->entity_id
== '' ) {
1228 $ret = find_ent_id( $id, $message->entities
[$i] );
1230 if ( strcasecmp( $message->entities
[$i]->header
->id
, $id ) == 0 )
1231 $ret = $message->entities
[$i]->header
->entity_id
;