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));
33 $msg = $msg->parseStructure($read,0);
36 foreach ($flags as $flag) {
37 $char = strtoupper($flag{1});
40 if (strtolower($flag) == '\\seen') {
45 if (strtolower($flag) == '\\answered') {
46 $msg->is_answered
= true;
50 if (strtolower($flag) == '\\deleted') {
51 $msg->is_deleted
= true;
55 if (strtolower($flag) == '\\flagged') {
56 $msg->is_flagged
= true;
60 if (strtolower($flag) == '\$mdnsent') {
69 // listEntities($msg);
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 $sid_s = substr($sid,0,strpos($sid, ' '));
166 fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n");
169 $read = fgets ($imap_stream,8192);
172 // if (preg_match('/.*\{(\d+)\}.*/',$read,$regs)) {
175 $size_div = (int) ($size / 4096);
176 $size_mod = $size % 4096;
177 if (!$size_mod) $size_div++;
179 for ($i=0;$i<$size_div;$i++) {
180 $read .= fread ($imap_stream,4096);
183 $read .= fread ($imap_stream, $size_mod);
185 echo decodeBody($read, $encoding);
188 // This could be bad -- if the section has sqimap_session_id() . ' OK'
189 // or similar, it will kill the download.
190 while (!ereg("^".$sid_s." (OK|BAD|NO)(.*)$", $read, $regs)) {
191 if (trim($read) == ')==') {
193 $read = fgets ($imap_stream,4096);
194 if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) {
197 echo decodeBody($read1, $encoding) .
198 decodeBody($read, $encoding);
201 echo decodeBody($read, $encoding);
203 $read = fgets ($imap_stream,4096);
209 /* -[ END MIME DECODING ]----------------------------------------------------------- */
212 // This is here for debugging purposese. It will print out a list
213 // of all the entity IDs that are in the $message object.
215 function listEntities ($message) {
217 echo "<tt>" . $message->entity_id
. ' : ' . $message->type0
. '/' . $message->type1
. ' parent = '. $message->parent
->entity_id
. '<br>';
218 for ($i = 0;isset($message->entities
[$i]); $i++
) {
220 $msg = listEntities($message->entities
[$i]);
231 /* returns a $message object for a particular entity id */
232 function getEntity ($message, $ent_id) {
233 return $message->getEntity($ent_id);
237 * figures out what entity to display and returns the $message object
240 function findDisplayEntity ($msg, $textOnly = true, $entity = array() ) {
241 global $show_html_default;
245 $type = $msg->type0
.'/'.$msg->type1
;
246 if ( $type == 'multipart/alternative') {
247 $msg = findAlternativeEntity($msg, $textOnly);
248 if (count($msg->entities
) == 0) {
249 $entity[] = $msg->entity_id
;
252 $entity =findDisplayEntity($msg,$textOnly, $entity);
254 } else if ( $type == 'multipart/related') {
255 $msgs = findRelatedEntity($msg);
256 for ($i = 0; $i < count($msgs); $i++
) {
258 if (count($msg->entities
) == 0) {
259 $entity[] = $msg->entity_id
;
262 $entity =findDisplayEntity($msg,$textOnly, $entity);
265 } else if ( count($entity) == 0 &&
266 $msg->type0
== 'text' &&
267 ( $msg->type1
== 'plain' ||
268 $msg->type1
== 'html' ) &&
269 isset($msg->entity_id
) ) {
270 if (count($msg->entities
) == 0) {
271 $entity[] = $msg->entity_id
;
275 while ( isset($msg->entities
[$i]) && count($entity) == 0 && !$found ) {
276 $entity = findDisplayEntity($msg->entities
[$i], $textOnly, $entity);
280 if ( !isset($entity[0]) ) {
286 /* Shows the HTML version */
287 function findDisplayEntityHTML ($message) {
289 if ( $message->header
->type0
== 'text' &&
290 $message->header
->type1
== 'html' &&
291 isset($message->header
->entity_id
)) {
292 return $message->header
->entity_id
;
294 for ($i = 0; isset($message->entities
[$i]); $i ++
) {
295 if ( $message->header
->type0
== 'message' &&
296 $message->header
->type1
== 'rfc822' &&
297 isset($message->header
->entity_id
)) {
301 $entity = findDisplayEntityHTML($message->entities
[$i]);
310 function findAlternativeEntity ($message, $textOnly) {
311 global $show_html_default;
312 /* if we are dealing with alternative parts then we choose the best
313 * viewable message supported by SM.
315 if ($show_html_default && !$textOnly) {
316 $alt_order = array ('text/plain','text/html');
318 $alt_order = array ('text/plain');
323 for ($i = 0; $i < count($message->entities
); $i ++
) {
324 $type = $message->entities
[$i]->header
->type0
.'/'.$message->entities
[$i]->header
->type1
;
325 if ($type == 'multipart/related') {
326 $type = $message->entities
[$i]->header
->type
;
328 for ($j = $k; $j < count($alt_order); $j++
) {
329 if ($alt_order[$j] == $type && $j > $best_view) {
336 return $message->entities
[$ent_id];
339 function findRelatedEntity ($message) {
341 for ($i = 0; $i < count($message->entities
); $i ++
) {
342 $type = $message->entities
[$i]->header
->type0
.'/'.$message->entities
[$i]->header
->type1
;
343 if ($message->header
->type
== $type) {
344 $msgs[] = $message->entities
[$i];
352 * Extracted from strings.php 23/03/2002
355 function translateText(&$body, $wrap_at, $charset) {
356 global $where, $what; /* from searching */
357 global $color; /* color theme */
359 require_once('../functions/url_parser.php');
361 $body_ary = explode("\n", $body);
363 for ($i=0; $i < count($body_ary); $i++
) {
364 $line = $body_ary[$i];
365 if (strlen($line) - 2 >= $wrap_at) {
366 sqWordWrap($line, $wrap_at);
368 $line = charset_decode($charset, $line);
369 $line = str_replace("\t", ' ', $line);
375 $j = strlen( $line );
377 while ( $pos < $j ) {
378 if ($line[$pos] == ' ') {
380 } else if (strpos($line, '>', $pos) === $pos) {
389 if (! isset($color[14])) {
390 $color[14] = '#FF0000';
392 $line = '<FONT COLOR="' . $color[14] . '">' . $line . '</FONT>';
394 if (! isset($color[13])) {
395 $color[13] = '#800000';
397 $line = '<FONT COLOR="' . $color[13] . '">' . $line . '</FONT>';
400 $body_ary[$i] = $line;
402 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
406 /* This returns a parsed string called $body. That string can then
407 be displayed as the actual message in the HTML. It contains
408 everything needed, including HTML Tags, Attachments at the
411 function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num) {
412 // this if statement checks for the entity to show as the
413 // primary message. To add more of them, just put them in the
414 // order that is their priority.
415 global $startMessage, $username, $key, $imapServerAddress, $imapPort,
416 $show_html_default, $has_unsafe_images, $view_unsafe_images, $sort;
418 $has_unsafe_images = 0;
422 if ($message->type0
== 'message' && $message->type1
== 'rfc822') {
423 $message = $message->entities
[0];
425 $urlmailbox = urlencode($message->mailbox
);
426 $body_message = getEntity($message, $ent_num);
427 if (($body_message->header
->type0
== 'text') ||
428 ($body_message->header
->type0
== 'rfc822')) {
429 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
431 $body = decodeBody($body, $body_message->header
->encoding
);
432 $hookResults = do_hook("message_body", $body);
433 $body = $hookResults[1];
434 // If there are other types that shouldn't be formatted, add
436 if ($body_message->header
->type1
== 'html') {
437 if ( $show_html_default <> 1 ) {
438 $body = strip_tags( $body );
439 translateText($body, $wrap_at, $body_message->header
->charset
);
441 $body = magicHTML( $body, $id, $message );
444 translateText($body, $wrap_at, $body_message->header
->charset
);
446 $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>";
447 if ($has_unsafe_images) {
448 if ($view_unsafe_images) {
449 $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";
451 $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";
455 /** Display the ATTACHMENTS: message if there's more than one part **/
456 if ($message->type0
== 'message') {
458 $mailbox = $message->mailbox
;
459 // $message->header->setVar('message_id',$id);
460 // $message->header->setVar('mailbox',$mailbox);
462 if (isset($message->entities
[1])) {
463 /* Header-type alternative means we choose the best one to display
464 so don't show the alternatives as attachment. Header-type related
465 means that the attachments are already part of the related message.
467 if ($message->header
->type1
!='related' && $message->header
->type1
!='alternative') {
468 $body .= formatAttachments ($message, $ent_num, $message->mailbox
, $id);
472 $body = formatAttachments ($message, -1, $message->mailbox
, $id);
478 * A recursive function that returns a list of attachments with links
479 * to where to download these attachments
481 function formatAttachments($message, $ent_id, $mailbox, $id) {
482 global $where, $what;
483 global $startMessage, $color;
484 static $ShownHTML = 0;
487 if ($ShownHTML == 0) {
490 $body .= "<TABLE WIDTH=\"100%\" CELLSPACING=0 CELLPADDING=2 BORDER=0 BGCOLOR=\"$color[0]\"><TR>\n" .
491 "<TH ALIGN=\"left\" BGCOLOR=\"$color[9]\"><B>\n" .
492 _("Attachments") . ':' .
493 "</B></TH></TR><TR><TD>\n" .
494 "<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=0>\n" .
495 formatAttachments($message, $ent_id, $mailbox, $id) .
496 "</TABLE></TD></TR></TABLE>";
498 } else if ($message) {
499 $header = $message->header
;
500 $type0 = strtolower($header->type0
);
501 $type1 = strtolower($header->type1
);
503 if (isset($header->name
)) {
504 $name = decodeHeader($header->name
);
506 if ($type0 =='message' && $type1 == 'rfc822') {
508 $filename = decodeHeader($message->header
->subject
);
509 $display_filename = $filename;
511 $urlMailbox = urlencode($mailbox);
512 $ent = urlencode($message->entity_id
);
515 "../src/read_body.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
516 if ($where && $what) {
517 $DefaultLink .= '&where=' . urlencode($where) . '&what=' . urlencode($what);
519 $Links['download link']['text'] = _("download");
520 $Links['download link']['href'] =
521 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
524 /* this executes the attachment hook with a specific MIME-type.
525 * if that doens't have results, it tries if there's a rule
526 * for a more generic type. */
527 $HookResults = do_hook("attachment $type0/$type1", $Links,
528 $startMessage, $id, $urlMailbox, $ent, $DefaultLink, $display_filename, $where, $what);
529 if(count($HookResults[1]) <= 1) {
530 $HookResults = do_hook("attachment $type0/*", $Links,
531 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
532 $display_filename, $where, $what);
535 $Links = $HookResults[1];
536 $DefaultLink = $HookResults[6];
538 $body .= '<TR><TD> </TD><TD>' .
539 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
540 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
541 '</b> </small></TD>' .
542 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
544 $from_o = $message->header
->from
;
545 if (isset($from_o)) {
546 $from_name = $from_o->getAddress(false);
548 $from_name = _("Unknown sender");
550 $from_name = decodeHeader(htmlspecialchars($from_name));
551 $body .= '<b>' . $from_name . '</b>';
552 $body .= '</SMALL></TD><TD><SMALL> ';
555 foreach ($Links as $Val) {
559 $body .= ' | ';
561 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
566 $body .= "</SMALL></TD></TR>\n";
570 } elseif (!$message->entities
) {
572 $type0 = strtolower($message->header
->type0
);
573 $type1 = strtolower($message->header
->type1
);
574 $name = decodeHeader($message->header
->name
);
576 if ($message->entity_id
!= $ent_id) {
577 $filename = decodeHeader($message->header
->filename
);
578 if (trim($filename) == '') {
579 if (trim($name) == '') {
580 if ( trim( $message->header
->id
) == '' )
581 $display_filename = 'untitled-[' . $message->entity_id
. ']' ;
583 $display_filename = 'cid: ' . $message->header
->id
;
584 // $display_filename = 'untitled-[' . $message->entity_id . ']' ;
586 $display_filename = $name;
590 $display_filename = $filename;
593 $urlMailbox = urlencode($mailbox);
594 $ent = urlencode($message->entity_id
);
597 "../src/download.php?startMessage=$startMessage&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
598 if ($where && $what) {
599 $DefaultLink = '&where='. urlencode($where).'&what='.urlencode($what);
601 $Links['download link']['text'] = _("download");
602 $Links['download link']['href'] =
603 "../src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&passed_ent_id=$ent";
606 /* this executes the attachment hook with a specific MIME-type.
607 * if that doens't have results, it tries if there's a rule
608 * for a more generic type. */
609 $HookResults = do_hook("attachment $type0/$type1", $Links,
610 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
611 $display_filename, $where, $what);
612 if(count($HookResults[1]) <= 1) {
613 $HookResults = do_hook("attachment $type0/*", $Links,
614 $startMessage, $id, $urlMailbox, $ent, $DefaultLink,
615 $display_filename, $where, $what);
618 $Links = $HookResults[1];
619 $DefaultLink = $HookResults[6];
621 $body .= '<TR><TD> </TD><TD>' .
622 "<A HREF=\"$DefaultLink\">$display_filename</A> </TD>" .
623 '<TD><SMALL><b>' . show_readable_size($message->header
->size
) .
624 '</b> </small></TD>' .
625 "<TD><SMALL>[ $type0/$type1 ] </SMALL></TD>" .
627 if ($message->header
->description
) {
628 $body .= '<b>' . htmlspecialchars(_($message->header
->description
)) . '</b>';
630 $body .= '</SMALL></TD><TD><SMALL> ';
634 foreach ($Links as $Val) {
638 $body .= ' | ';
640 $body .= '<a href="' . $Val['href'] . '">' . $Val['text'] . '</a>';
645 $body .= "</SMALL></TD></TR>\n";
648 for ($i = 0; $i < count($message->entities
); $i++
) {
649 $body .= formatAttachments($message->entities
[$i], $ent_id, $mailbox, $id);
657 /** this function decodes the body depending on the encoding type. **/
658 function decodeBody($body, $encoding) {
659 $body = str_replace("\r\n", "\n", $body);
660 $encoding = strtolower($encoding);
662 global $show_html_default;
664 if ($encoding == 'quoted-printable' ||
665 $encoding == 'quoted_printable') {
666 $body = quoted_printable_decode($body);
668 while (ereg("=\n", $body))
669 $body = ereg_replace ("=\n", "", $body);
671 } else if ($encoding == 'base64') {
672 $body = base64_decode($body);
675 // All other encodings are returned raw.
680 * This functions decode strings that is encoded according to
681 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
682 * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002
684 function decodeHeader ($string, $utfencode=true) {
685 if (is_array($string)) {
686 $string = implode("\n", $string);
689 while (preg_match('/^(.{' . $i . '})(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=/Ui',
692 // Ignore white-space between consecutive encoded-words
693 if (strspn($res[2], " \t") != strlen($res[2])) {
697 if (ucfirst($res[4]) == 'B') {
698 $replace = base64_decode($res[5]);
700 $replace = str_replace('_', ' ', $res[5]);
701 $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))',
703 /* Only encode into entities by default. Some places
704 don't need the encoding, like the compose form. */
706 $replace = charset_decode($res[3], $replace);
709 $string = $prefix . $replace . substr($string, strlen($res[0]));
710 $i = strlen($prefix) +
strlen($replace);
716 * Encode a string according to RFC 1522 for use in headers if it
717 * contains 8-bit characters or anything that looks like it should
720 function encodeHeader ($string) {
721 global $default_charset;
723 // Encode only if the string contains 8-bit characters or =?
724 $j = strlen( $string );
725 $l = strstr($string, '=?'); // Must be encoded ?
727 for( $i=0; $i < $j; ++
$i) {
728 switch( $string{$i} ) {
742 $k = ord( $string{$i} );
744 $ret .= sprintf("=%02X", $k);
752 $string = "=?$default_charset?Q?$ret?=";
758 /* This function trys to locate the entity_id of a specific mime element */
760 function find_ent_id( $id, $message ) {
762 for ($i=0; $ret == '' && $i < count($message->entities
); $i++
) {
763 if ( $message->entities
[$i]->header
->type0
== 'multipart') {
764 $ret = find_ent_id( $id, $message->entities
[$i] );
766 if ( strcasecmp( $message->entities
[$i]->header
->id
, $id ) == 0 )
767 $ret = $message->entities
[$i]->entity_id
;
774 ** HTMLFILTER ROUTINES
778 * This function returns the final tag out of the tag name, an array
779 * of attributes, and the type of the tag. This function is called by
780 * sq_sanitize internally.
782 * @param $tagname the name of the tag.
783 * @param $attary the array of attributes and their values
784 * @param $tagtype The type of the tag (see in comments).
785 * @return a string with the final tag representation.
787 function sq_tagprint($tagname, $attary, $tagtype){
790 $fulltag = '</' . $tagname . '>';
792 $fulltag = '<' . $tagname;
793 if (is_array($attary) && sizeof($attary)){
795 while (list($attname, $attvalue) = each($attary)){
796 array_push($atts, "$attname=$attvalue");
798 $fulltag .= ' ' . join(" ", $atts);
809 * A small helper function to use with array_walk. Modifies a by-ref
810 * value and makes it lowercase.
812 * @param $val a value passed by-ref.
813 * @return void since it modifies a by-ref value.
815 function sq_casenormalize(&$val){
816 $val = strtolower($val);
820 * This function skips any whitespace from the current position within
821 * a string and to the next non-whitespace value.
823 * @param $body the string
824 * @param $offset the offset within the string where we should start
825 * looking for the next non-whitespace character.
826 * @return the location within the $body where the next
827 * non-whitespace char is located.
829 function sq_skipspace($body, $offset){
830 $me = "sq_skipspace";
831 preg_match("/^(\s*)/s", substr($body, $offset), $matches);
832 if (sizeof($matches{1})){
833 $count = strlen($matches{1});
840 * This function looks for the next character within a string. It's
841 * really just a glorified "strpos", except it catches if failures
844 * @param $body The string to look for needle in.
845 * @param $offset Start looking from this position.
846 * @param $needle The character/string to look for.
847 * @return location of the next occurance of the needle, or
848 * strlen($body) if needle wasn't found.
850 function sq_findnxstr($body, $offset, $needle){
851 $me = "sq_findnxstr";
852 $pos = strpos($body, $needle, $offset);
854 $pos = strlen($body);
860 * This function takes a PCRE-style regexp and tries to match it
863 * @param $body The string to look for needle in.
864 * @param $offset Start looking from here.
865 * @param $reg A PCRE-style regex to match.
866 * @return Returns a false if no matches found, or an array
867 * with the following members:
868 * - integer with the location of the match within $body
869 * - string with whatever content between offset and the match
870 * - string with whatever it is we matched
872 function sq_findnxreg($body, $offset, $reg){
873 $me = "sq_findnxreg";
876 preg_match("%^(.*?)($reg)%s", substr($body, $offset), $matches);
880 $retarr{0} = $offset +
strlen($matches{1});
881 $retarr{1} = $matches{1};
882 $retarr{2} = $matches{2};
888 * This function looks for the next tag.
890 * @param $body String where to look for the next tag.
891 * @param $offset Start looking from here.
892 * @return false if no more tags exist in the body, or
893 * an array with the following members:
894 * - string with the name of the tag
895 * - array with attributes and their values
896 * - integer with tag type (1, 2, or 3)
897 * - integer where the tag starts (starting "<")
898 * - integer where the tag ends (ending ">")
899 * first three members will be false, if the tag is invalid.
901 function sq_getnxtag($body, $offset){
903 if ($offset > strlen($body)){
906 $lt = sq_findnxstr($body, $offset, "<");
907 if ($lt == strlen($body)){
912 * blah blah <tag attribute="value">
915 $pos = sq_skipspace($body, $lt+
1);
916 if ($pos >= strlen($body)){
917 return Array(false, false, false, $lt, strlen($body));
920 * There are 3 kinds of tags:
921 * 1. Opening tag, e.g.:
923 * 2. Closing tag, e.g.:
925 * 3. XHTML-style content-less tag, e.g.:
929 switch (substr($body, $pos, 1)){
936 * A comment or an SGML declaration.
938 if (substr($body, $pos+
1, 2) == "--"){
939 $gt = strpos($body, "-->", $pos);
945 return Array(false, false, false, $lt, $gt);
947 $gt = sq_findnxstr($body, $pos, ">");
948 return Array(false, false, false, $lt, $gt);
953 * Assume tagtype 1 for now. If it's type 3, we'll switch values
963 * Look for next [\W-_], which will indicate the end of the tag name.
965 $regary = sq_findnxreg($body, $pos, "[^\w\-_]");
966 if ($regary == false){
967 return Array(false, false, false, $lt, strlen($body));
969 list($pos, $tagname, $match) = $regary;
970 $tagname = strtolower($tagname);
973 * $match can be either of these:
974 * '>' indicating the end of the tag entirely.
975 * '\s' indicating the end of the tag name.
976 * '/' indicating that this is type-3 xhtml tag.
978 * Whatever else we find there indicates an invalid tag.
983 * This is an xhtml-style tag with a closing / at the
984 * end, like so: <img src="blah"/>. Check if it's followed
985 * by the closing bracket. If not, then this tag is invalid
987 if (substr($body, $pos, 2) == "/>"){
991 $gt = sq_findnxstr($body, $pos, ">");
992 $retary = Array(false, false, false, $lt, $gt);
996 return Array($tagname, false, $tagtype, $lt, $pos);
1000 * Check if it's whitespace
1002 if (preg_match("/\s/", $match)){
1005 * This is an invalid tag! Look for the next closing ">".
1007 $gt = sq_findnxstr($body, $offset, ">");
1008 return Array(false, false, false, $lt, $gt);
1013 * At this point we're here:
1014 * <tagname attribute='blah'>
1017 * At this point we loop in order to find all attributes.
1023 while ($pos <= strlen($body)){
1024 $pos = sq_skipspace($body, $pos);
1025 if ($pos == strlen($body)){
1029 return Array(false, false, false, $lt, $pos);
1032 * See if we arrived at a ">" or "/>", which means that we reached
1033 * the end of the tag.
1036 if (preg_match("%^(\s*)(>|/>)%s", substr($body, $pos), $matches)) {
1040 $pos +
= strlen($matches{1});
1041 if ($matches{2} == "/>"){
1045 return Array($tagname, $attary, $tagtype, $lt, $pos);
1049 * There are several types of attributes, with optional
1050 * [:space:] between members.
1052 * attrname[:space:]=[:space:]'CDATA'
1054 * attrname[:space:]=[:space:]"CDATA"
1056 * attr[:space:]=[:space:]CDATA
1060 * We leave types 1 and 2 the same, type 3 we check for
1061 * '"' and convert to """ if needed, then wrap in
1062 * double quotes. Type 4 we convert into:
1065 $regary = sq_findnxreg($body, $pos, "[^\w\-_]");
1066 if ($regary == false){
1068 * Looks like body ended before the end of tag.
1070 return Array(false, false, false, $lt, strlen($body));
1072 list($pos, $attname, $match) = $regary;
1073 $attname = strtolower($attname);
1075 * We arrived at the end of attribute name. Several things possible
1077 * '>' means the end of the tag and this is attribute type 4
1078 * '/' if followed by '>' means the same thing as above
1079 * '\s' means a lot of things -- look what it's followed by.
1080 * anything else means the attribute is invalid.
1085 * This is an xhtml-style tag with a closing / at the
1086 * end, like so: <img src="blah"/>. Check if it's followed
1087 * by the closing bracket. If not, then this tag is invalid
1089 if (substr($body, $pos, 2) == "/>"){
1093 $gt = sq_findnxstr($body, $pos, ">");
1094 $retary = Array(false, false, false, $lt, $gt);
1098 $attary{$attname} = '"yes"';
1099 return Array($tagname, $attary, $tagtype, $lt, $pos);
1103 * Skip whitespace and see what we arrive at.
1105 $pos = sq_skipspace($body, $pos);
1106 $char = substr($body, $pos, 1);
1108 * Two things are valid here:
1109 * '=' means this is attribute type 1 2 or 3.
1110 * \w means this was attribute type 4.
1111 * anything else we ignore and re-loop. End of tag and
1112 * invalid stuff will be caught by our checks at the beginning
1117 $pos = sq_skipspace($body, $pos);
1119 * Here are 3 possibilities:
1120 * "'" attribute type 1
1121 * '"' attribute type 2
1122 * everything else is the content of tag type 3
1124 $quot = substr($body, $pos, 1);
1126 $regary = sq_findnxreg($body, $pos+
1, "\'");
1127 if ($regary == false){
1128 return Array(false, false, false, $lt, strlen($body));
1130 list($pos, $attval, $match) = $regary;
1132 $attary{$attname} = "'" . $attval . "'";
1133 } else if ($quot == '"'){
1134 $regary = sq_findnxreg($body, $pos+
1, '\"');
1135 if ($regary == false){
1136 return Array(false, false, false, $lt, strlen($body));
1138 list($pos, $attval, $match) = $regary;
1140 $attary{$attname} = '"' . $attval . '"';
1143 * These are hateful. Look for \s, or >.
1145 $regary = sq_findnxreg($body, $pos, "[\s>]");
1146 if ($regary == false){
1147 return Array(false, false, false, $lt, strlen($body));
1149 list($pos, $attval, $match) = $regary;
1151 * If it's ">" it will be caught at the top.
1153 $attval = preg_replace("/\"/s", """, $attval);
1154 $attary{$attname} = '"' . $attval . '"';
1156 } else if (preg_match("|[\w/>]|", $char)) {
1158 * That was attribute type 4.
1160 $attary{$attname} = '"yes"';
1163 * An illegal character. Find next '>' and return.
1165 $gt = sq_findnxstr($body, $pos, ">");
1166 return Array(false, false, false, $lt, $gt);
1171 * The fact that we got here indicates that the tag end was never
1172 * found. Return invalid tag indication so it gets stripped.
1174 return Array(false, false, false, $lt, strlen($body));
1178 * This function checks attribute values for entity-encoded values
1179 * and returns them translated into 8-bit strings so we can run
1182 * @param $attvalue A string to run entity check against.
1183 * @return Translated value.
1185 function sq_deent($attvalue){
1188 * See if we have to run the checks first. All entities must start
1191 if (strpos($attvalue, "&") === false){
1195 * Check named entities first.
1197 $trans = get_html_translation_table(HTML_ENTITIES
);
1199 * Leave " in, as it can mess us up.
1201 $trans = array_flip($trans);
1202 unset($trans{"""});
1203 while (list($ent, $val) = each($trans)){
1204 $attvalue = preg_replace("/$ent*(\W)/si", "$val\\1", $attvalue);
1207 * Now translate numbered entities from 1 to 255 if needed.
1209 if (strpos($attvalue, "#") !== false){
1210 $omit = Array(34, 39);
1211 for ($asc=1; $asc<256; $asc++
){
1212 if (!in_array($asc, $omit)){
1214 $attvalue = preg_replace("/\�*$asc;*(\D)/si", "$chr\\1",
1216 $attvalue = preg_replace("/\�*".dechex($asc).";*(\W)/si",
1217 "$chr\\1", $attvalue);
1225 * This function runs various checks against the attributes.
1227 * @param $tagname String with the name of the tag.
1228 * @param $attary Array with all tag attributes.
1229 * @param $rm_attnames See description for sq_sanitize
1230 * @param $bad_attvals See description for sq_sanitize
1231 * @param $add_attr_to_tag See description for sq_sanitize
1232 * @param $message message object
1233 * @param $id message id
1234 * @return Array with modified attributes.
1236 function sq_fixatts($tagname,
1245 while (list($attname, $attvalue) = each($attary)){
1247 * See if this attribute should be removed.
1249 foreach ($rm_attnames as $matchtag=>$matchattrs){
1250 if (preg_match($matchtag, $tagname)){
1251 foreach ($matchattrs as $matchattr){
1252 if (preg_match($matchattr, $attname)){
1253 unset($attary{$attname});
1260 * Remove any entities.
1262 $attvalue = sq_deent($attvalue);
1265 * Now let's run checks on the attvalues.
1266 * I don't expect anyone to comprehend this. If you do,
1267 * get in touch with me so I can drive to where you live and
1268 * shake your hand personally. :)
1270 foreach ($bad_attvals as $matchtag=>$matchattrs){
1271 if (preg_match($matchtag, $tagname)){
1272 foreach ($matchattrs as $matchattr=>$valary){
1273 if (preg_match($matchattr, $attname)){
1275 * There are two arrays in valary.
1277 * Second one is replacements
1279 list($valmatch, $valrepl) = $valary;
1281 preg_replace($valmatch, $valrepl, $attvalue);
1282 if ($newvalue != $attvalue){
1283 $attary{$attname} = $newvalue;
1290 * Turn cid: urls into http-friendly ones.
1292 if (preg_match("/^[\'\"]\s*cid:/si", $attvalue)){
1293 $attary{$attname} = sq_cid2http($message, $id, $attvalue);
1297 * See if we need to append any attributes to this tag.
1299 foreach ($add_attr_to_tag as $matchtag=>$addattary){
1300 if (preg_match($matchtag, $tagname)){
1301 $attary = array_merge($attary, $addattary);
1308 * This function edits the style definition to make them friendly and
1309 * usable in squirrelmail.
1311 * @param $message the message object
1312 * @param $id the message id
1313 * @param $content a string with whatever is between <style> and </style>
1314 * @return a string with edited content.
1316 function sq_fixstyle($message, $id, $content){
1317 global $view_unsafe_images;
1318 $me = "sq_fixstyle";
1320 * First look for general BODY style declaration, which would be
1322 * body {background: blah-blah}
1323 * and change it to .bodyclass so we can just assign it to a <div>
1325 $content = preg_replace("|body(\s*\{.*?\})|si", ".bodyclass\\1", $content);
1326 $secremoveimg = "../images/" . _("sec_remove_eng.png");
1328 * Fix url('blah') declarations.
1330 $content = preg_replace("|url\(([\'\"])\s*\S+script\s*:.*?([\'\"])\)|si",
1331 "url(\\1$secremoveimg\\2)", $content);
1333 * Fix url('https*://.*) declarations but only if $view_unsafe_images
1336 if (!$view_unsafe_images){
1337 $content = preg_replace("|url\(([\'\"])\s*https*:.*?([\'\"])\)|si",
1338 "url(\\1$secremoveimg\\2)", $content);
1342 * Fix urls that refer to cid:
1344 while (preg_match("|url\(([\'\"]\s*cid:.*?[\'\"])\)|si", $content,
1346 $cidurl = $matches{1};
1347 $httpurl = sq_cid2http($message, $id, $cidurl);
1348 $content = preg_replace("|url\($cidurl\)|si",
1349 "url($httpurl)", $content);
1353 * Fix stupid css declarations which lead to vulnerabilities
1356 $match = Array('/expression/si',
1359 $replace = Array('idiocy', 'idiocy', 'idiocy');
1360 $content = preg_replace($match, $replace, $content);
1365 * This function converts cid: url's into the ones that can be viewed in
1368 * @param $message the message object
1369 * @param $id the message id
1370 * @param $cidurl the cid: url.
1371 * @return a string with a http-friendly url
1373 function sq_cid2http($message, $id, $cidurl){
1375 * Get rid of quotes.
1377 $quotchar = substr($cidurl, 0, 1);
1378 $cidurl = str_replace($quotchar, "", $cidurl);
1379 $cidurl = substr(trim($cidurl), 4);
1380 $httpurl = $quotchar . "../src/download.php?absolute_dl=true&" .
1381 "passed_id=$id&mailbox=" . urlencode($message->mailbox
) .
1382 "&passed_ent_id=" . find_ent_id($cidurl, $message) . $quotchar;
1387 * This function changes the <body> tag into a <div> tag since we
1388 * can't really have a body-within-body.
1390 * @param $attary an array of attributes and values of <body>
1391 * @return a modified array of attributes to be set for <div>
1393 function sq_body2div($attary){
1394 $me = "sq_body2div";
1395 $divattary = Array("class"=>"'bodyclass'");
1399 if (is_array($attary) && sizeof($attary) > 0){
1400 foreach ($attary as $attname=>$attvalue){
1401 $quotchar = substr($attvalue, 0, 1);
1402 $attvalue = str_replace($quotchar, "", $attvalue);
1405 $styledef .= "background-image: url('$attvalue'); ";
1408 $styledef .= "background-color: $attvalue; ";
1411 $styledef .= "color: $attvalue; ";
1414 if (strlen($styledef) > 0){
1415 $divattary{"style"} = "\"$styledef\"";
1422 * This is the main function and the one you should actually be calling.
1423 * There are several variables you should be aware of an which need
1424 * special description.
1426 * Since the description is quite lengthy, see it here:
1427 * http://www.mricon.com/html/phpfilter.html
1429 * @param $body the string with HTML you wish to filter
1430 * @param $tag_list see description above
1431 * @param $rm_tags_with_content see description above
1432 * @param $self_closing_tags see description above
1433 * @param $force_tag_closing see description above
1434 * @param $rm_attnames see description above
1435 * @param $bad_attvals see description above
1436 * @param $add_attr_to_tag see description above
1437 * @param $message message object
1438 * @param $id message id
1439 * @return sanitized html safe to show on your pages.
1441 function sq_sanitize($body,
1443 $rm_tags_with_content,
1452 $me = "sq_sanitize";
1454 * Normalize rm_tags and rm_tags_with_content.
1456 @array_walk
($rm_tags, 'sq_casenormalize');
1457 @array_walk
($rm_tags_with_content, 'sq_casenormalize');
1458 @array_walk
($self_closing_tags, 'sq_casenormalize');
1460 * See if tag_list is of tags to remove or tags to allow.
1461 * false means remove these tags
1462 * true means allow these tags
1464 $rm_tags = array_shift($tag_list);
1466 $open_tags = Array();
1467 $trusted = "<!-- begin sanitized html -->\n";
1468 $skip_content = false;
1470 * Take care of netscape's stupid javascript entities like
1473 $body = preg_replace("/&(\{.*?\};)/si", "&\\1", $body);
1475 while (($curtag=sq_getnxtag($body, $curpos)) != FALSE){
1476 list($tagname, $attary, $tagtype, $lt, $gt) = $curtag;
1477 $free_content = substr($body, $curpos, $lt-$curpos);
1479 * Take care of <style>
1481 if ($tagname == "style" && $tagtype == 2){
1483 * This is a closing </style>. Edit the
1484 * content before we apply it.
1486 $free_content = sq_fixstyle($message, $id, $free_content);
1488 if ($skip_content == false){
1489 $trusted .= $free_content;
1492 if ($tagname != FALSE){
1494 if ($skip_content == $tagname){
1496 * Got to the end of tag we needed to remove.
1499 $skip_content = false;
1501 if ($skip_content == false){
1502 if ($tagname == "body"){
1505 if (isset($open_tags{$tagname}) &&
1506 $open_tags{$tagname} > 0){
1507 $open_tags{$tagname}--;
1517 * $rm_tags_with_content
1519 if ($skip_content == false){
1521 * See if this is a self-closing type and change
1522 * tagtype appropriately.
1525 && in_array($tagname, $self_closing_tags)){
1529 * See if we should skip this tag and any content
1532 if ($tagtype == 1 &&
1533 in_array($tagname, $rm_tags_with_content)){
1534 $skip_content = $tagname;
1536 if (($rm_tags == false
1537 && in_array($tagname, $tag_list)) ||
1538 ($rm_tags == true &&
1539 !in_array($tagname, $tag_list))){
1543 if (isset($open_tags{$tagname})){
1544 $open_tags{$tagname}++
;
1546 $open_tags{$tagname}=1;
1550 * This is where we run other checks.
1552 if (is_array($attary) && sizeof($attary) > 0){
1553 $attary = sq_fixatts($tagname,
1563 * Convert body into div.
1565 if ($tagname == "body"){
1567 $attary = sq_body2div($attary, $message, $id);
1574 if ($tagname != false && $skip_content == false){
1575 $trusted .= sq_tagprint($tagname, $attary, $tagtype);
1581 $trusted .= substr($body, $curpos, strlen($body)-$curpos);
1582 if ($force_tag_closing == true){
1583 foreach ($open_tags as $tagname=>$opentimes){
1584 while ($opentimes > 0){
1585 $trusted .= '</' . $tagname . '>';
1591 $trusted .= "<!-- end sanitized html -->\n";
1596 * This is a wrapper function to call html sanitizing routines.
1598 * @param $body the body of the message
1599 * @param $id the id of the message
1600 * @return a string with html safe to display in the browser.
1602 function magicHTML($body, $id, $message){
1603 global $attachment_common_show_images, $view_unsafe_images,
1606 * Don't display attached images in HTML mode.
1608 $attachment_common_show_images = false;
1618 $rm_tags_with_content = Array(
1625 $self_closing_tags = Array(
1632 $force_tag_closing = false;
1634 $rm_attnames = Array(
1644 $secremoveimg = "../images/" . _("sec_remove_eng.png");
1645 $bad_attvals = Array(
1648 "/^src|background/i" =>
1651 "|^([\'\"])\s*\.\./.*([\'\"])|si",
1652 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si",
1653 "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si",
1654 "/^([\'\"])\s*about\s*:.*([\'\"])/si"
1657 "\\1$secremoveimg\\2",
1658 "\\1$secremoveimg\\2",
1659 "\\1$secremoveimg\\2",
1660 "\\1$secremoveimg\\2"
1663 "/^href|action/i" =>
1666 "|^([\'\"])\s*\.\./.*([\'\"])|si",
1667 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si",
1668 "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si",
1669 "/^([\'\"])\s*about\s*:.*([\'\"])/si"
1684 "|url\(([\'\"])\s*\.\./.*([\'\"])\)|si",
1685 "/url\(([\'\"])\s*\S+script\s*:.*([\'\"])\)/si",
1686 "/url\(([\'\"])\s*mocha\s*:.*([\'\"])\)/si",
1687 "/url\(([\'\"])\s*about\s*:.*([\'\"])\)/si"
1701 if (!$view_unsafe_images){
1703 * Remove any references to http/https if view_unsafe_images set
1706 array_push($bad_attvals{'/.*/'}{'/^src|background/i'}[0],
1707 '/^([\'\"])\s*https*:.*([\'\"])/si');
1708 array_push($bad_attvals{'/.*/'}{'/^src|background/i'}[1],
1709 "\\1$secremoveimg\\2");
1710 array_push($bad_attvals{'/.*/'}{'/^style/si'}[0],
1711 '/url\(([\'\"])\s*https*:.*([\'\"])\)/si');
1712 array_push($bad_attvals{'/.*/'}{'/^style/si'}[1],
1713 "url(\\1$secremoveimg\\2)");
1716 $add_attr_to_tag = Array(
1717 "/^a$/si" => Array('target'=>'"_new"')
1719 $trusted = sq_sanitize($body,
1721 $rm_tags_with_content,
1730 if (preg_match("|$secremoveimg|si", $trusted)){
1731 $has_unsafe_images = true;