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 /* --------------------------------------------------------------------------------- */
20 /* --------------------------------------------------------------------------------- */
22 /* This function gets the structure of a message and stores it in the "message" class.
23 * It will return this object for use with all relevant header information and
24 * fully parsed into the standard "message" object format.
27 function mime_structure ($bodystructure, $flags=array()) {
29 // isolate the body structure and remove beginning and end parenthesis
30 $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') +
13));
31 $msg = &new message();
32 $read = trim(substr ($read, 0, -1));
34 $msg = $msg->parseStructure($read,0);
37 foreach ($flags as $flag) {
38 $char = strtoupper($flag{1});
41 if (strtolower($flag) == '\\seen') {
46 if (strtolower($flag) == '\\answered') {
47 $msg->is_answered
= true;
51 if (strtolower($flag) == '\\deleted') {
52 $msg->is_deleted
= true;
56 if (strtolower($flag) == '\\flagged') {
57 $msg->is_flagged
= true;
61 if (strtolower($flag) == '\$mdnsent') {
73 /* this starts the parsing of a particular structure. It is called recursively,
74 * so it can be passed different structures. It returns an object of type
76 * First, it checks to see if it is a multipart message. If it is, then it
77 * handles that as it sees is necessary. If it is just a regular entity,
78 * then it parses it and adds the necessary header information (by calling out
79 * to mime_get_elements()
82 function mime_fetch_body($imap_stream, $id, $ent_id) {
85 * do a bit of error correction. If we couldn't find the entity id, just guess
86 * that it is the first one. That is usually the case anyway.
91 $cmd = "FETCH $id BODY[$ent_id]";
93 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, $uid_support);
95 $topline = trim(array_shift( $data ));
96 } while( $topline && $topline[0] == '*' && !preg_match( '/\* [0-9]+ FETCH.*/i', $topline )) ;
98 $wholemessage = implode('', $data);
99 if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) {
101 $ret = substr( $wholemessage, 0, $regs[1] );
103 There is some information in the content info header that could be important
104 in order to parse html messages. Let's get them here.
106 if ( $ret{0} == '<' ) {
107 $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, $uid_support);
109 } else if (ereg('"([^"]*)"', $topline, $regs)) {
112 global $where, $what, $mailbox, $passed_id, $startMessage;
113 $par = 'mailbox=' . urlencode($mailbox) . "&passed_id=$passed_id";
114 if (isset($where) && isset($what)) {
115 $par .= '&where='. urlencode($where) . "&what=" . urlencode($what);
117 $par .= "&startMessage=$startMessage&show_more=0";
119 $par .= '&response=' . urlencode($response) .
120 '&message=' . urlencode($message).
121 '&topline=' . urlencode($topline);
124 '<table width="80%"><tr>' .
125 '<tr><td colspan=2>' .
126 _("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!") .
127 " <A HREF=\"../src/retrievalerror.php?$par\"><br>" .
128 _("Submit message") . '</A><BR> ' .
130 '<td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
131 '<td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
132 '<td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
133 '<td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
134 "</table><BR></tt></font><hr>";
136 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, $uid_support);
138 $wholemessage = implode('', $data);
140 $ret = $wholemessage;
145 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding) {
147 // do a bit of error correction. If we couldn't find the entity id, just guess
148 // that it is the first one. That is usually the case anyway.
152 $sid = sqimap_session_id($uid_support);
153 // Don't kill the connection if the browser is over a dialup
154 // and it would take over 30 seconds to download it.
156 // donĀ“t call set_time_limit in safe mode.
157 if (!ini_get("safe_mode")) {
161 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
164 $read = fgets ($imap_stream,4096);
165 if (preg_match('/.*\{(\d+)\}.*/',$read,$regs)) {
167 $size_div = (int) ($size / 4096);
168 $size_mod = $size %
4096;
169 if (!$size_mod) $size_div++
;
171 for ($i=0;$i<$size_div;$i++
) {
172 $read .= fread ($imap_stream,4096);
175 $read .= fread ($imap_stream, $size_mod);
177 return decodeBody($read, $encoding);
179 // This could be bad -- if the section has sqimap_session_id() . ' OK'
180 // or similar, it will kill the download.
181 while (!ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
182 if (trim($read) == ')==') {
184 $read = fgets ($imap_stream,4096);
185 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
188 echo decodeBody($read1, $encoding) .
189 decodeBody($read, $encoding);
192 echo decodeBody($read, $encoding);
194 $read = fgets ($imap_stream,4096);
199 /* -[ END MIME DECODING ]----------------------------------------------------------- */
202 // This is here for debugging purposese. It will print out a list
203 // of all the entity IDs that are in the $message object.
205 function listEntities ($message) {
207 echo "<tt>" . $message->entity_id
. ' : ' . $message->type0
. '/' . $message->type1
. ' parent = '. $message->parent
->entity_id
. '<br>';
208 for ($i = 0;isset($message->entities
[$i]); $i++
) {
210 $msg = listEntities($message->entities
[$i]);
221 /* returns a $message object for a particular entity id */
222 function getEntity ($message, $ent_id) {
223 return $message->getEntity($ent_id);
227 * figures out what entity to display and returns the $message object
230 function findDisplayEntity ($msg, $textOnly = true, $entity = array() ) {
231 global $show_html_default;
235 $type = $msg->type0
.'/'.$msg->type1
;
236 if ( $type == 'multipart/alternative') {
237 $msg = findAlternativeEntity($msg, $textOnly);
238 if (count($msg->entities
) == 0) {
239 $entity[] = $msg->entity_id
;
242 $entity =findDisplayEntity($msg,$textOnly, $entity);
244 } else if ( $type == 'multipart/related') {
245 $msgs = findRelatedEntity($msg);
246 for ($i = 0; $i < count($msgs); $i++
) {
248 if (count($msg->entities
) == 0) {
249 $entity[] = $msg->entity_id
;
252 $entity =findDisplayEntity($msg,$textOnly, $entity);
255 } else if ( count($entity) == 0 &&
256 $msg->type0
== 'text' &&
257 ( $msg->type1
== 'plain' ||
258 $msg->type1
== 'html' ) &&
259 isset($msg->entity_id
) ) {
260 if (count($msg->entities
) == 0) {
261 $entity[] = $msg->entity_id
;
265 while ( isset($msg->entities
[$i]) && count($entity) == 0 && !$found ) {
266 $entity = findDisplayEntity($msg->entities
[$i], $textOnly, $entity);
270 if ( !isset($entity[0]) ) {
276 /* Shows the HTML version */
277 function findDisplayEntityHTML ($message) {
279 if ( $message->header
->type0
== 'text' &&
280 $message->header
->type1
== 'html' &&
281 isset($message->header
->entity_id
)) {
282 return $message->header
->entity_id
;
284 for ($i = 0; isset($message->entities
[$i]); $i ++
) {
285 if ( $message->header
->type0
== 'message' &&
286 $message->header
->type1
== 'rfc822' &&
287 isset($message->header
->entity_id
)) {
291 $entity = findDisplayEntityHTML($message->entities
[$i]);
300 function findAlternativeEntity ($message, $textOnly) {
301 global $show_html_default;
302 /* if we are dealing with alternative parts then we choose the best
303 * viewable message supported by SM.
305 if ($show_html_default && !$textOnly) {
306 $alt_order = array ('text/plain','text/html');
308 $alt_order = array ('text/plain');
313 for ($i = 0; $i < count($message->entities
); $i ++
) {
314 $type = $message->entities
[$i]->header
->type0
.'/'.$message->entities
[$i]->header
->type1
;
315 if ($type == 'multipart/related') {
316 $type = $message->entities
[$i]->header
->type
;
318 for ($j = $k; $j < count($alt_order); $j++
) {
319 if ($alt_order[$j] == $type && $j > $best_view) {
326 return $message->entities
[$ent_id];
329 function findRelatedEntity ($message) {
331 for ($i = 0; $i < count($message->entities
); $i ++
) {
332 $type = $message->entities
[$i]->header
->type0
.'/'.$message->entities
[$i]->header
->type1
;
333 if ($message->header
->type
== $type) {
334 $msgs[] = $message->entities
[$i];
342 * Extracted from strings.php 23/03/2002
345 function translateText(&$body, $wrap_at, $charset) {
346 global $where, $what; /* from searching */
347 global $color; /* color theme */
349 require_once('../functions/url_parser.php');
351 $body_ary = explode("\n", $body);
353 for ($i=0; $i < count($body_ary); $i++
) {
354 $line = $body_ary[$i];
355 if (strlen($line) - 2 >= $wrap_at) {
356 sqWordWrap($line, $wrap_at);
358 $line = charset_decode($charset, $line);
359 $line = str_replace("\t", ' ', $line);
365 $j = strlen( $line );
367 while ( $pos < $j ) {
368 if ($line[$pos] == ' ') {
370 } else if (strpos($line, '>', $pos) === $pos) {
379 if (! isset($color[14])) {
380 $color[14] = '#FF0000';
382 $line = '<FONT COLOR="' . $color[14] . '">' . $line . '</FONT>';
384 if (! isset($color[13])) {
385 $color[13] = '#800000';
387 $line = '<FONT COLOR="' . $color[13] . '">' . $line . '</FONT>';
390 $body_ary[$i] = $line;
392 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
396 /* This returns a parsed string called $body. That string can then
397 be displayed as the actual message in the HTML. It contains
398 everything needed, including HTML Tags, Attachments at the
401 function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num) {
402 // this if statement checks for the entity to show as the
403 // primary message. To add more of them, just put them in the
404 // order that is their priority.
405 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
406 $show_html_default, $has_unsafe_images, $view_unsafe_images, $sort;
408 $has_unsafe_images = 0;
412 $urlmailbox = urlencode($message->mailbox
);
413 $body_message = getEntity($message, $ent_num);
414 if (($body_message->header
->type0
== 'text') ||
415 ($body_message->header
->type0
== 'rfc822')) {
416 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
418 $body = decodeBody($body, $body_message->header
->encoding
);
419 $hookResults = do_hook("message_body", $body);
420 $body = $hookResults[1];
421 // If there are other types that shouldn't be formatted, add
423 if ($body_message->header
->type1
== 'html') {
424 if ( $show_html_default <> 1 ) {
425 $body = strip_tags( $body );
426 translateText($body, $wrap_at, $body_message->header
->charset
);
428 $body = magicHTML( $body, $id, $message );
431 translateText($body, $wrap_at, $body_message->header
->charset
);
433 $body .= "<CENTER><SMALL><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></SMALL></CENTER><BR>";
434 if ($has_unsafe_images) {
435 if ($view_unsafe_images) {
436 $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&mailbox=$urlmailbox&sort=$sort&startMessage=$startMessage&show_more=0\">". _("Hide Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
438 $body .= "<CENTER><SMALL><A HREF=\"read_body.php?passed_id=$id&mailbox=$urlmailbox&sort=$sort&startMessage=$startMessage&show_more=0&view_unsafe_images=1\">". _("View Unsafe Images") ."</A></SMALL></CENTER><BR>\n";
442 /** Display the ATTACHMENTS: message if there's more than one part **/
443 if ($message->type0
== 'message') {
445 $mailbox = $message->mailbox
;
446 // $message->header->setVar('message_id',$id);
447 // $message->header->setVar('mailbox',$mailbox);
449 if (isset($message->entities
[1])) {
450 /* Header-type alternative means we choose the best one to display
451 so don't show the alternatives as attachment. Header-type related
452 means that the attachments are already part of the related message.
454 if ($message->header
->type1
!='related' && $message->header
->type1
!='alternative') {
455 $body .= formatAttachments ($message, $ent_num, $message->mailbox
, $id);
459 $body = formatAttachments ($message, -1, $message->mailbox
, $id);
465 * A recursive function that returns a list of attachments with links
466 * to where to download these attachments
468 function formatAttachments($message, $ent_id, $mailbox, $id) {
469 global $where, $what;
470 global $startMessage, $color;
471 static $ShownHTML = 0;
474 if ($ShownHTML == 0) {
477 $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
478 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
479 _("Attachments") . ':' .
480 "</B></TH></TR><TR><TD>\n" .
481 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
482 formatAttachments($message, $ent_id, $mailbox, $id) .
483 "</TABLE></TD></TR></TABLE>";
485 } else if ($message) {
486 $header = $message->header
;
487 $type0 = strtolower($header->type0
);
488 $type1 = strtolower($header->type1
);
490 if (isset($header->name
)) {
491 $name = decodeHeader($header->name
);
493 if ($type0 =='message' && $type1 == 'rfc822') {
495 $filename = decodeHeader($message->header
->subject
);
496 $display_filename = $filename;
498 $urlMailbox = urlencode($mailbox);
499 $ent = urlencode($message->entity_id
);
502 "../src/read_body.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
503 if ($where && $what) {
504 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
506 $Links['download link']['text'] = _("download");
507 $Links['download link']['href'] =
508 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
511 /* this executes the attachment hook with a specific MIME-type.
512 * if that doens't have results, it tries if there's a rule
513 * for a more generic type. */
514 $HookResults = do_hook("attachment $type0/$type1", $Links,
515 $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what);
516 if(count($HookResults[1]) <= 1) {
517 $HookResults = do_hook("attachment $type0/*", $Links,
518 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
519 $display_filename, $where, $what);
522 $Links = $HookResults[1];
523 $DefaultLink = $HookResults[6];
525 $body .= '<TR><TD> </TD><TD>' .
526 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
527 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
528 '</b> </small></TD>' .
529 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
531 $from_o = $message->header
->from
;
532 if (isset($from_o)) {
533 $from_name = $from_o->getAddress(false);
535 $from_name = _("Unknown sender");
537 $from_name = decodeHeader(htmlspecialchars($from_name));
538 $body .= '<b>' . $from_name . '</b>';
539 $body .= '</SMALL></TD><TD><SMALL> ';
542 foreach ($Links as $Val) {
546 $body .= ' | ';
548 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
553 $body .= "</SMALL></TD></TR>\n";
557 } elseif (!$message->entities
) {
559 $type0 = strtolower($message->header
->type0
);
560 $type1 = strtolower($message->header
->type1
);
561 $name = decodeHeader($message->header
->name
);
563 if ($message->entity_id
!= $ent_id) {
564 $filename = decodeHeader($message->header
->filename
);
565 if (trim($filename) == '') {
566 if (trim($name) == '') {
567 if ( trim( $message->header
->id
) == '' )
568 $display_filename = 'untitled-[' . $message->entity_id
. ']' ;
570 $display_filename = 'cid: ' . $message->header
->id
;
571 // $display_filename = 'untitled-[' . $message->entity_id . ']' ;
573 $display_filename = $name;
577 $display_filename = $filename;
580 $urlMailbox = urlencode($mailbox);
581 $ent = urlencode($message->entity_id
);
584 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
585 if ($where && $what) {
586 $DefaultLink = '&where='. urlencode($where).'&what='.urlencode($what);
588 $Links['download link']['text'] = _("download");
589 $Links['download link']['href'] =
590 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
593 /* this executes the attachment hook with a specific MIME-type.
594 * if that doens't have results, it tries if there's a rule
595 * for a more generic type. */
596 $HookResults = do_hook("attachment $type0/$type1", $Links,
597 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
598 $display_filename, $where, $what);
599 if(count($HookResults[1]) <= 1) {
600 $HookResults = do_hook("attachment $type0/*", $Links,
601 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
602 $display_filename, $where, $what);
605 $Links = $HookResults[1];
606 $DefaultLink = $HookResults[6];
608 $body .= '<TR><TD> </TD><TD>' .
609 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
610 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
611 '</b> </small></TD>' .
612 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
614 if ($message->header
->description
) {
615 $body .= '<b>' . htmlspecialchars(_($message->header
->description
)) . '</b>';
617 $body .= '</SMALL></TD><TD><SMALL> ';
621 foreach ($Links as $Val) {
625 $body .= ' | ';
627 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
632 $body .= "</SMALL></TD></TR>\n";
635 for ($i = 0; $i < count($message->entities
); $i++
) {
636 $body .= formatAttachments($message->entities
[$i], $ent_id, $mailbox, $id);
644 /** this function decodes the body depending on the encoding type. **/
645 function decodeBody($body, $encoding) {
646 $body = str_replace("\r\n", "\n", $body);
647 $encoding = strtolower($encoding);
649 global $show_html_default;
651 if ($encoding == 'quoted-printable' ||
652 $encoding == 'quoted_printable') {
653 $body = quoted_printable_decode($body);
655 while (ereg("=\n", $body))
656 $body = ereg_replace ("=\n", "", $body);
658 } else if ($encoding == 'base64') {
659 $body = base64_decode($body);
662 // All other encodings are returned raw.
667 * This functions decode strings that is encoded according to
668 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
669 * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002
671 function decodeHeader ($string, $utfencode=true) {
672 if (is_array($string)) {
673 $string = implode("\n", $string);
676 while (preg_match('/^(.{' . $i . '})(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=/Ui',
679 // Ignore white-space between consecutive encoded-words
680 if (strspn($res[2], " \t") != strlen($res[2])) {
684 if (ucfirst($res[4]) == 'B') {
685 $replace = base64_decode($res[5]);
687 $replace = str_replace('_', ' ', $res[5]);
688 $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))',
690 /* Only encode into entities by default. Some places
691 don't need the encoding, like the compose form. */
693 $replace = charset_decode($res[3], $replace);
696 $string = $prefix . $replace . substr($string, strlen($res[0]));
697 $i = strlen($prefix) +
strlen($replace);
703 * Encode a string according to RFC 1522 for use in headers if it
704 * contains 8-bit characters or anything that looks like it should
707 function encodeHeader ($string) {
708 global $default_charset;
710 // Encode only if the string contains 8-bit characters or =?
711 $j = strlen( $string );
712 $l = strstr($string, '=?'); // Must be encoded ?
714 for( $i=0; $i < $j; ++
$i) {
715 switch( $string{$i} ) {
729 $k = ord( $string{$i} );
731 $ret .= sprintf("=%02X", $k);
739 $string = "=?$default_charset?Q?$ret?=";
745 /* This function trys to locate the entity_id of a specific mime element */
747 function find_ent_id( $id, $message ) {
749 for ($i=0; $ret == '' && $i < count($message->entities
); $i++
) {
750 if ( $message->entities
[$i]->header
->type0
== 'multipart') {
751 $ret = find_ent_id( $id, $message->entities
[$i] );
753 if ( strcasecmp( $message->entities
[$i]->header
->id
, $id ) == 0 )
754 $ret = $message->entities
[$i]->entity_id
;
761 ** HTMLFILTER ROUTINES
765 * This function returns the final tag out of the tag name, an array
766 * of attributes, and the type of the tag. This function is called by
767 * sq_sanitize internally.
769 * @param $tagname the name of the tag.
770 * @param $attary the array of attributes and their values
771 * @param $tagtype The type of the tag (see in comments).
772 * @return a string with the final tag representation.
774 function sq_tagprint($tagname, $attary, $tagtype){
777 $fulltag = '</' . $tagname . '>';
779 $fulltag = '<' . $tagname;
780 if (is_array($attary) && sizeof($attary)){
782 while (list($attname, $attvalue) = each($attary)){
783 array_push($atts, "$attname=$attvalue");
785 $fulltag .= ' ' . join(" ", $atts);
796 * A small helper function to use with array_walk. Modifies a by-ref
797 * value and makes it lowercase.
799 * @param $val a value passed by-ref.
800 * @return void since it modifies a by-ref value.
802 function sq_casenormalize(&$val){
803 $val = strtolower($val);
807 * This function skips any whitespace from the current position within
808 * a string and to the next non-whitespace value.
810 * @param $body the string
811 * @param $offset the offset within the string where we should start
812 * looking for the next non-whitespace character.
813 * @return the location within the $body where the next
814 * non-whitespace char is located.
816 function sq_skipspace($body, $offset){
817 $me = "sq_skipspace";
818 preg_match("/^(\s*)/s", substr($body, $offset), $matches);
819 if (sizeof($matches{1})){
820 $count = strlen($matches{1});
827 * This function looks for the next character within a string. It's
828 * really just a glorified "strpos", except it catches if failures
831 * @param $body The string to look for needle in.
832 * @param $offset Start looking from this position.
833 * @param $needle The character/string to look for.
834 * @return location of the next occurance of the needle, or
835 * strlen($body) if needle wasn't found.
837 function sq_findnxstr($body, $offset, $needle){
838 $me = "sq_findnxstr";
839 $pos = strpos($body, $needle, $offset);
841 $pos = strlen($body);
847 * This function takes a PCRE-style regexp and tries to match it
850 * @param $body The string to look for needle in.
851 * @param $offset Start looking from here.
852 * @param $reg A PCRE-style regex to match.
853 * @return Returns a false if no matches found, or an array
854 * with the following members:
855 * - integer with the location of the match within $body
856 * - string with whatever content between offset and the match
857 * - string with whatever it is we matched
859 function sq_findnxreg($body, $offset, $reg){
860 $me = "sq_findnxreg";
863 preg_match("%^(.*?)($reg)%s", substr($body, $offset), $matches);
867 $retarr{0} = $offset +
strlen($matches{1});
868 $retarr{1} = $matches{1};
869 $retarr{2} = $matches{2};
875 * This function looks for the next tag.
877 * @param $body String where to look for the next tag.
878 * @param $offset Start looking from here.
879 * @return false if no more tags exist in the body, or
880 * an array with the following members:
881 * - string with the name of the tag
882 * - array with attributes and their values
883 * - integer with tag type (1, 2, or 3)
884 * - integer where the tag starts (starting "<")
885 * - integer where the tag ends (ending ">")
886 * first three members will be false, if the tag is invalid.
888 function sq_getnxtag($body, $offset){
890 if ($offset > strlen($body)){
893 $lt = sq_findnxstr($body, $offset, "<");
894 if ($lt == strlen($body)){
899 * blah blah <tag attribute="value">
902 $pos = sq_skipspace($body, $lt+
1);
903 if ($pos >= strlen($body)){
904 return Array(false, false, false, $lt, strlen($body));
907 * There are 3 kinds of tags:
908 * 1. Opening tag, e.g.:
910 * 2. Closing tag, e.g.:
912 * 3. XHTML-style content-less tag, e.g.:
916 switch (substr($body, $pos, 1)){
923 * A comment or an SGML declaration.
925 if (substr($body, $pos+
1, 2) == "--"){
926 $gt = strpos($body, "-->", $pos);
932 return Array(false, false, false, $lt, $gt);
934 $gt = sq_findnxstr($body, $pos, ">");
935 return Array(false, false, false, $lt, $gt);
940 * Assume tagtype 1 for now. If it's type 3, we'll switch values
950 * Look for next [\W-_], which will indicate the end of the tag name.
952 $regary = sq_findnxreg($body, $pos, "[^\w\-_]");
953 if ($regary == false){
954 return Array(false, false, false, $lt, strlen($body));
956 list($pos, $tagname, $match) = $regary;
957 $tagname = strtolower($tagname);
960 * $match can be either of these:
961 * '>' indicating the end of the tag entirely.
962 * '\s' indicating the end of the tag name.
963 * '/' indicating that this is type-3 xhtml tag.
965 * Whatever else we find there indicates an invalid tag.
970 * This is an xhtml-style tag with a closing / at the
971 * end, like so: <img src="blah"/>. Check if it's followed
972 * by the closing bracket. If not, then this tag is invalid
974 if (substr($body, $pos, 2) == "/>"){
978 $gt = sq_findnxstr($body, $pos, ">");
979 $retary = Array(false, false, false, $lt, $gt);
983 return Array($tagname, false, $tagtype, $lt, $pos);
987 * Check if it's whitespace
989 if (preg_match("/\s/", $match)){
992 * This is an invalid tag! Look for the next closing ">".
994 $gt = sq_findnxstr($body, $offset, ">");
995 return Array(false, false, false, $lt, $gt);
1000 * At this point we're here:
1001 * <tagname attribute='blah'>
1004 * At this point we loop in order to find all attributes.
1010 while ($pos <= strlen($body)){
1011 $pos = sq_skipspace($body, $pos);
1012 if ($pos == strlen($body)){
1016 return Array(false, false, false, $lt, $pos);
1019 * See if we arrived at a ">" or "/>", which means that we reached
1020 * the end of the tag.
1023 if (preg_match("%^(\s*)(>|/>)%s", substr($body, $pos), $matches)) {
1027 $pos +
= strlen($matches{1});
1028 if ($matches{2} == "/>"){
1032 return Array($tagname, $attary, $tagtype, $lt, $pos);
1036 * There are several types of attributes, with optional
1037 * [:space:] between members.
1039 * attrname[:space:]=[:space:]'CDATA'
1041 * attrname[:space:]=[:space:]"CDATA"
1043 * attr[:space:]=[:space:]CDATA
1047 * We leave types 1 and 2 the same, type 3 we check for
1048 * '"' and convert to """ if needed, then wrap in
1049 * double quotes. Type 4 we convert into:
1052 $regary = sq_findnxreg($body, $pos, "[^\w\-_]");
1053 if ($regary == false){
1055 * Looks like body ended before the end of tag.
1057 return Array(false, false, false, $lt, strlen($body));
1059 list($pos, $attname, $match) = $regary;
1060 $attname = strtolower($attname);
1062 * We arrived at the end of attribute name. Several things possible
1064 * '>' means the end of the tag and this is attribute type 4
1065 * '/' if followed by '>' means the same thing as above
1066 * '\s' means a lot of things -- look what it's followed by.
1067 * anything else means the attribute is invalid.
1072 * This is an xhtml-style tag with a closing / at the
1073 * end, like so: <img src="blah"/>. Check if it's followed
1074 * by the closing bracket. If not, then this tag is invalid
1076 if (substr($body, $pos, 2) == "/>"){
1080 $gt = sq_findnxstr($body, $pos, ">");
1081 $retary = Array(false, false, false, $lt, $gt);
1085 $attary{$attname} = '"yes"';
1086 return Array($tagname, $attary, $tagtype, $lt, $pos);
1090 * Skip whitespace and see what we arrive at.
1092 $pos = sq_skipspace($body, $pos);
1093 $char = substr($body, $pos, 1);
1095 * Two things are valid here:
1096 * '=' means this is attribute type 1 2 or 3.
1097 * \w means this was attribute type 4.
1098 * anything else we ignore and re-loop. End of tag and
1099 * invalid stuff will be caught by our checks at the beginning
1104 $pos = sq_skipspace($body, $pos);
1106 * Here are 3 possibilities:
1107 * "'" attribute type 1
1108 * '"' attribute type 2
1109 * everything else is the content of tag type 3
1111 $quot = substr($body, $pos, 1);
1113 $regary = sq_findnxreg($body, $pos+
1, "\'");
1114 if ($regary == false){
1115 return Array(false, false, false, $lt, strlen($body));
1117 list($pos, $attval, $match) = $regary;
1119 $attary{$attname} = "'" . $attval . "'";
1120 } else if ($quot == '"'){
1121 $regary = sq_findnxreg($body, $pos+
1, '\"');
1122 if ($regary == false){
1123 return Array(false, false, false, $lt, strlen($body));
1125 list($pos, $attval, $match) = $regary;
1127 $attary{$attname} = '"' . $attval . '"';
1130 * These are hateful. Look for \s, or >.
1132 $regary = sq_findnxreg($body, $pos, "[\s>]");
1133 if ($regary == false){
1134 return Array(false, false, false, $lt, strlen($body));
1136 list($pos, $attval, $match) = $regary;
1138 * If it's ">" it will be caught at the top.
1140 $attval = preg_replace("/\"/s", """, $attval);
1141 $attary{$attname} = '"' . $attval . '"';
1143 } else if (preg_match("|[\w/>]|", $char)) {
1145 * That was attribute type 4.
1147 $attary{$attname} = '"yes"';
1150 * An illegal character. Find next '>' and return.
1152 $gt = sq_findnxstr($body, $pos, ">");
1153 return Array(false, false, false, $lt, $gt);
1158 * The fact that we got here indicates that the tag end was never
1159 * found. Return invalid tag indication so it gets stripped.
1161 return Array(false, false, false, $lt, strlen($body));
1165 * This function checks attribute values for entity-encoded values
1166 * and returns them translated into 8-bit strings so we can run
1169 * @param $attvalue A string to run entity check against.
1170 * @return Translated value.
1172 function sq_deent($attvalue){
1175 * See if we have to run the checks first. All entities must start
1178 if (strpos($attvalue, "&") === false){
1182 * Check named entities first.
1184 $trans = get_html_translation_table(HTML_ENTITIES
);
1186 * Leave " in, as it can mess us up.
1188 $trans = array_flip($trans);
1189 unset($trans{"""});
1190 while (list($ent, $val) = each($trans)){
1191 $attvalue = preg_replace("/$ent*(\W)/si", "$val\\1", $attvalue);
1194 * Now translate numbered entities from 1 to 255 if needed.
1196 if (strpos($attvalue, "#") !== false){
1197 $omit = Array(34, 39);
1198 for ($asc=1; $asc<256; $asc++
){
1199 if (!in_array($asc, $omit)){
1201 $attvalue = preg_replace("/\�*$asc;*(\D)/si", "$chr\\1",
1203 $attvalue = preg_replace("/\�*".dechex($asc).";*(\W)/si",
1204 "$chr\\1", $attvalue);
1212 * This function runs various checks against the attributes.
1214 * @param $tagname String with the name of the tag.
1215 * @param $attary Array with all tag attributes.
1216 * @param $rm_attnames See description for sq_sanitize
1217 * @param $bad_attvals See description for sq_sanitize
1218 * @param $add_attr_to_tag See description for sq_sanitize
1219 * @param $message message object
1220 * @param $id message id
1221 * @return Array with modified attributes.
1223 function sq_fixatts($tagname,
1232 while (list($attname, $attvalue) = each($attary)){
1234 * See if this attribute should be removed.
1236 foreach ($rm_attnames as $matchtag=>$matchattrs){
1237 if (preg_match($matchtag, $tagname)){
1238 foreach ($matchattrs as $matchattr){
1239 if (preg_match($matchattr, $attname)){
1240 unset($attary{$attname});
1247 * Remove any entities.
1249 $attvalue = sq_deent($attvalue);
1252 * Now let's run checks on the attvalues.
1253 * I don't expect anyone to comprehend this. If you do,
1254 * get in touch with me so I can drive to where you live and
1255 * shake your hand personally. :)
1257 foreach ($bad_attvals as $matchtag=>$matchattrs){
1258 if (preg_match($matchtag, $tagname)){
1259 foreach ($matchattrs as $matchattr=>$valary){
1260 if (preg_match($matchattr, $attname)){
1262 * There are two arrays in valary.
1264 * Second one is replacements
1266 list($valmatch, $valrepl) = $valary;
1268 preg_replace($valmatch, $valrepl, $attvalue);
1269 if ($newvalue != $attvalue){
1270 $attary{$attname} = $newvalue;
1277 * Turn cid: urls into http-friendly ones.
1279 if (preg_match("/^[\'\"]\s*cid:/si", $attvalue)){
1280 $attary{$attname} = sq_cid2http($message, $id, $attvalue);
1284 * See if we need to append any attributes to this tag.
1286 foreach ($add_attr_to_tag as $matchtag=>$addattary){
1287 if (preg_match($matchtag, $tagname)){
1288 $attary = array_merge($attary, $addattary);
1295 * This function edits the style definition to make them friendly and
1296 * usable in squirrelmail.
1298 * @param $message the message object
1299 * @param $id the message id
1300 * @param $content a string with whatever is between <style> and </style>
1301 * @return a string with edited content.
1303 function sq_fixstyle($message, $id, $content){
1304 global $view_unsafe_images;
1305 $me = "sq_fixstyle";
1307 * First look for general BODY style declaration, which would be
1309 * body {background: blah-blah}
1310 * and change it to .bodyclass so we can just assign it to a <div>
1312 $content = preg_replace("|body(\s*\{.*?\})|si", ".bodyclass\\1", $content);
1313 $secremoveimg = "../images/" . _("sec_remove_eng.png");
1315 * Fix url('blah') declarations.
1317 $content = preg_replace("|url\(([\'\"])\s*\S+script\s*:.*?([\'\"])\)|si",
1318 "url(\\1$secremoveimg\\2)", $content);
1320 * Fix url('https*://.*) declarations but only if $view_unsafe_images
1323 if (!$view_unsafe_images){
1324 $content = preg_replace("|url\(([\'\"])\s*https*:.*?([\'\"])\)|si",
1325 "url(\\1$secremoveimg\\2)", $content);
1329 * Fix urls that refer to cid:
1331 while (preg_match("|url\(([\'\"]\s*cid:.*?[\'\"])\)|si", $content,
1333 $cidurl = $matches{1};
1334 $httpurl = sq_cid2http($message, $id, $cidurl);
1335 $content = preg_replace("|url\($cidurl\)|si",
1336 "url($httpurl)", $content);
1340 * Fix stupid css declarations which lead to vulnerabilities
1343 $match = Array('/expression/si',
1346 $replace = Array('idiocy', 'idiocy', 'idiocy');
1347 $content = preg_replace($match, $replace, $content);
1352 * This function converts cid: url's into the ones that can be viewed in
1355 * @param $message the message object
1356 * @param $id the message id
1357 * @param $cidurl the cid: url.
1358 * @return a string with a http-friendly url
1360 function sq_cid2http($message, $id, $cidurl){
1362 * Get rid of quotes.
1364 $quotchar = substr($cidurl, 0, 1);
1365 $cidurl = str_replace($quotchar, "", $cidurl);
1366 $cidurl = substr(trim($cidurl), 4);
1367 $httpurl = $quotchar . "../src/download.php?absolute_dl=true&" .
1368 "passed_id=$id&mailbox=" . urlencode($message->header
->mailbox
) .
1369 "&passed_ent_id=" . find_ent_id($cidurl, $message) . $quotchar;
1374 * This function changes the <body> tag into a <div> tag since we
1375 * can't really have a body-within-body.
1377 * @param $attary an array of attributes and values of <body>
1378 * @return a modified array of attributes to be set for <div>
1380 function sq_body2div($attary){
1381 $me = "sq_body2div";
1382 $divattary = Array("class"=>"'bodyclass'");
1386 if (is_array($attary) && sizeof($attary) > 0){
1387 foreach ($attary as $attname=>$attvalue){
1388 $quotchar = substr($attvalue, 0, 1);
1389 $attvalue = str_replace($quotchar, "", $attvalue);
1392 $styledef .= "background-image: url('$attvalue'); ";
1395 $styledef .= "background-color: $attvalue; ";
1398 $styledef .= "color: $attvalue; ";
1401 if (strlen($styledef) > 0){
1402 $divattary{"style"} = "\"$styledef\"";
1409 * This is the main function and the one you should actually be calling.
1410 * There are several variables you should be aware of an which need
1411 * special description.
1413 * Since the description is quite lengthy, see it here:
1414 * http://www.mricon.com/html/phpfilter.html
1416 * @param $body the string with HTML you wish to filter
1417 * @param $tag_list see description above
1418 * @param $rm_tags_with_content see description above
1419 * @param $self_closing_tags see description above
1420 * @param $force_tag_closing see description above
1421 * @param $rm_attnames see description above
1422 * @param $bad_attvals see description above
1423 * @param $add_attr_to_tag see description above
1424 * @param $message message object
1425 * @param $id message id
1426 * @return sanitized html safe to show on your pages.
1428 function sq_sanitize($body,
1430 $rm_tags_with_content,
1439 $me = "sq_sanitize";
1441 * Normalize rm_tags and rm_tags_with_content.
1443 @array_walk
($rm_tags, 'sq_casenormalize');
1444 @array_walk
($rm_tags_with_content, 'sq_casenormalize');
1445 @array_walk
($self_closing_tags, 'sq_casenormalize');
1447 * See if tag_list is of tags to remove or tags to allow.
1448 * false means remove these tags
1449 * true means allow these tags
1451 $rm_tags = array_shift($tag_list);
1453 $open_tags = Array();
1454 $trusted = "<!-- begin sanitized html -->\n";
1455 $skip_content = false;
1457 * Take care of netscape's stupid javascript entities like
1460 $body = preg_replace("/&(\{.*?\};)/si", "&\\1", $body);
1462 while (($curtag=sq_getnxtag($body, $curpos)) != FALSE){
1463 list($tagname, $attary, $tagtype, $lt, $gt) = $curtag;
1464 $free_content = substr($body, $curpos, $lt-$curpos);
1466 * Take care of <style>
1468 if ($tagname == "style" && $tagtype == 2){
1470 * This is a closing </style>. Edit the
1471 * content before we apply it.
1473 $free_content = sq_fixstyle($message, $id, $free_content);
1475 if ($skip_content == false){
1476 $trusted .= $free_content;
1479 if ($tagname != FALSE){
1481 if ($skip_content == $tagname){
1483 * Got to the end of tag we needed to remove.
1486 $skip_content = false;
1488 if ($skip_content == false){
1489 if ($tagname == "body"){
1492 if (isset($open_tags{$tagname}) &&
1493 $open_tags{$tagname} > 0){
1494 $open_tags{$tagname}--;
1504 * $rm_tags_with_content
1506 if ($skip_content == false){
1508 * See if this is a self-closing type and change
1509 * tagtype appropriately.
1512 && in_array($tagname, $self_closing_tags)){
1516 * See if we should skip this tag and any content
1519 if ($tagtype == 1 &&
1520 in_array($tagname, $rm_tags_with_content)){
1521 $skip_content = $tagname;
1523 if (($rm_tags == false
1524 && in_array($tagname, $tag_list)) ||
1525 ($rm_tags == true &&
1526 !in_array($tagname, $tag_list))){
1530 if (isset($open_tags{$tagname})){
1531 $open_tags{$tagname}++
;
1533 $open_tags{$tagname}=1;
1537 * This is where we run other checks.
1539 if (is_array($attary) && sizeof($attary) > 0){
1540 $attary = sq_fixatts($tagname,
1550 * Convert body into div.
1552 if ($tagname == "body"){
1554 $attary = sq_body2div($attary, $message, $id);
1561 if ($tagname != false && $skip_content == false){
1562 $trusted .= sq_tagprint($tagname, $attary, $tagtype);
1568 $trusted .= substr($body, $curpos, strlen($body)-$curpos);
1569 if ($force_tag_closing == true){
1570 foreach ($open_tags as $tagname=>$opentimes){
1571 while ($opentimes > 0){
1572 $trusted .= '</' . $tagname . '>';
1578 $trusted .= "<!-- end sanitized html -->\n";
1583 * This is a wrapper function to call html sanitizing routines.
1585 * @param $body the body of the message
1586 * @param $id the id of the message
1587 * @return a string with html safe to display in the browser.
1589 function magicHTML($body, $id, $message){
1590 global $attachment_common_show_images, $view_unsafe_images,
1593 * Don't display attached images in HTML mode.
1595 $attachment_common_show_images = false;
1605 $rm_tags_with_content = Array(
1612 $self_closing_tags = Array(
1619 $force_tag_closing = false;
1621 $rm_attnames = Array(
1631 $secremoveimg = "../images/" . _("sec_remove_eng.png");
1632 $bad_attvals = Array(
1635 "/^src|background/i" =>
1638 "|^([\'\"])\s*\.\./.*([\'\"])|si",
1639 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si",
1640 "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si",
1641 "/^([\'\"])\s*about\s*:.*([\'\"])/si"
1644 "\\1$secremoveimg\\2",
1645 "\\1$secremoveimg\\2",
1646 "\\1$secremoveimg\\2",
1647 "\\1$secremoveimg\\2"
1650 "/^href|action/i" =>
1653 "|^([\'\"])\s*\.\./.*([\'\"])|si",
1654 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si",
1655 "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si",
1656 "/^([\'\"])\s*about\s*:.*([\'\"])/si"
1671 "|url\(([\'\"])\s*\.\./.*([\'\"])\)|si",
1672 "/url\(([\'\"])\s*\S+script\s*:.*([\'\"])\)/si",
1673 "/url\(([\'\"])\s*mocha\s*:.*([\'\"])\)/si",
1674 "/url\(([\'\"])\s*about\s*:.*([\'\"])\)/si"
1688 if (!$view_unsafe_images){
1690 * Remove any references to http/https if view_unsafe_images set
1693 array_push($bad_attvals{'/.*/'}{'/^src|background/i'}[0],
1694 '/^([\'\"])\s*https*:.*([\'\"])/si');
1695 array_push($bad_attvals{'/.*/'}{'/^src|background/i'}[1],
1696 "\\1$secremoveimg\\2");
1697 array_push($bad_attvals{'/.*/'}{'/^style/si'}[0],
1698 '/url\(([\'\"])\s*https*:.*([\'\"])\)/si');
1699 array_push($bad_attvals{'/.*/'}{'/^style/si'}[1],
1700 "url(\\1$secremoveimg\\2)");
1703 $add_attr_to_tag = Array(
1704 "/^a$/si" => Array('target'=>'"_new"')
1706 $trusted = sq_sanitize($body,
1708 $rm_tags_with_content,
1717 if (preg_match("|$secremoveimg|si", $trusted)){
1718 $has_unsafe_images = true;