| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * mime.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This contains the functions necessary to detect and decode MIME |
| 10 | * messages. |
| 11 | * |
| 12 | * @version $Id$ |
| 13 | * @package squirrelmail |
| 14 | */ |
| 15 | |
| 16 | /** The typical includes... */ |
| 17 | require_once(SM_PATH . 'functions/imap.php'); |
| 18 | require_once(SM_PATH . 'functions/attachment_common.php'); |
| 19 | |
| 20 | /* -------------------------------------------------------------------------- */ |
| 21 | /* MIME DECODING */ |
| 22 | /* -------------------------------------------------------------------------- */ |
| 23 | |
| 24 | /** |
| 25 | * Get the MIME structure |
| 26 | * |
| 27 | * This function gets the structure of a message and stores it in the "message" class. |
| 28 | * It will return this object for use with all relevant header information and |
| 29 | * fully parsed into the standard "message" object format. |
| 30 | */ |
| 31 | function mime_structure ($bodystructure, $flags=array()) { |
| 32 | |
| 33 | /* Isolate the body structure and remove beginning and end parenthesis. */ |
| 34 | $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') + 13)); |
| 35 | $read = trim(substr ($read, 0, -1)); |
| 36 | $i = 0; |
| 37 | $msg = Message::parseStructure($read,$i); |
| 38 | if (!is_object($msg)) { |
| 39 | include_once(SM_PATH . 'functions/display_messages.php'); |
| 40 | global $color, $mailbox; |
| 41 | /* removed urldecode because $_GET is auto urldecoded ??? */ |
| 42 | displayPageHeader( $color, $mailbox ); |
| 43 | $errormessage = _("SquirrelMail could not decode the bodystructure of the message"); |
| 44 | $errormessage .= '<BR>'._("the provided bodystructure by your imap-server").':<BR><BR>'; |
| 45 | $errormessage .= '<pre>' . htmlspecialchars($read) . '</pre>'; |
| 46 | plain_error_message( $errormessage, $color ); |
| 47 | echo '</body></html>'; |
| 48 | exit; |
| 49 | } |
| 50 | if (count($flags)) { |
| 51 | foreach ($flags as $flag) { |
| 52 | $char = strtoupper($flag{1}); |
| 53 | switch ($char) { |
| 54 | case 'S': |
| 55 | if (strtolower($flag) == '\\seen') { |
| 56 | $msg->is_seen = true; |
| 57 | } |
| 58 | break; |
| 59 | case 'A': |
| 60 | if (strtolower($flag) == '\\answered') { |
| 61 | $msg->is_answered = true; |
| 62 | } |
| 63 | break; |
| 64 | case 'D': |
| 65 | if (strtolower($flag) == '\\deleted') { |
| 66 | $msg->is_deleted = true; |
| 67 | } |
| 68 | break; |
| 69 | case 'F': |
| 70 | if (strtolower($flag) == '\\flagged') { |
| 71 | $msg->is_flagged = true; |
| 72 | } |
| 73 | break; |
| 74 | case 'M': |
| 75 | if (strtolower($flag) == '$mdnsent') { |
| 76 | $msg->is_mdnsent = true; |
| 77 | } |
| 78 | break; |
| 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | // listEntities($msg); |
| 85 | return $msg; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | /* This starts the parsing of a particular structure. It is called recursively, |
| 91 | * so it can be passed different structures. It returns an object of type |
| 92 | * $message. |
| 93 | * First, it checks to see if it is a multipart message. If it is, then it |
| 94 | * handles that as it sees is necessary. If it is just a regular entity, |
| 95 | * then it parses it and adds the necessary header information (by calling out |
| 96 | * to mime_get_elements() |
| 97 | */ |
| 98 | |
| 99 | function mime_fetch_body($imap_stream, $id, $ent_id=1, $fetch_size=0) { |
| 100 | /* Do a bit of error correction. If we couldn't find the entity id, just guess |
| 101 | * that it is the first one. That is usually the case anyway. |
| 102 | */ |
| 103 | |
| 104 | if (!$ent_id) { |
| 105 | $cmd = "FETCH $id BODY[]"; |
| 106 | } else { |
| 107 | $cmd = "FETCH $id BODY[$ent_id]"; |
| 108 | } |
| 109 | |
| 110 | if ($fetch_size!=0) $cmd .= "<0.$fetch_size>"; |
| 111 | |
| 112 | $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, TRUE); |
| 113 | do { |
| 114 | $topline = trim(array_shift($data)); |
| 115 | } while($topline && ($topline[0] == '*') && !preg_match('/\* [0-9]+ FETCH.*/i', $topline)) ; |
| 116 | |
| 117 | $wholemessage = implode('', $data); |
| 118 | if (ereg('\\{([^\\}]*)\\}', $topline, $regs)) { |
| 119 | $ret = substr($wholemessage, 0, $regs[1]); |
| 120 | /* There is some information in the content info header that could be important |
| 121 | * in order to parse html messages. Let's get them here. |
| 122 | */ |
| 123 | // if ($ret{0} == '<') { |
| 124 | // $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, TRUE); |
| 125 | // } |
| 126 | } else if (ereg('"([^"]*)"', $topline, $regs)) { |
| 127 | $ret = $regs[1]; |
| 128 | } else { |
| 129 | global $where, $what, $mailbox, $passed_id, $startMessage; |
| 130 | $par = 'mailbox=' . urlencode($mailbox) . '&passed_id=' . $passed_id; |
| 131 | if (isset($where) && isset($what)) { |
| 132 | $par .= '&where=' . urlencode($where) . '&what=' . urlencode($what); |
| 133 | } else { |
| 134 | $par .= '&startMessage=' . $startMessage . '&show_more=0'; |
| 135 | } |
| 136 | $par .= '&response=' . urlencode($response) . |
| 137 | '&message=' . urlencode($message) . |
| 138 | '&topline=' . urlencode($topline); |
| 139 | |
| 140 | echo '<tt><br>' . |
| 141 | '<table width="80%"><tr>' . |
| 142 | '<tr><td colspan=2>' . |
| 143 | _("Body retrieval error. The reason for this is most probably that the message is malformed.") . |
| 144 | '</td></tr>' . |
| 145 | '<tr><td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" . |
| 146 | '<tr><td><b>' . _("Response:") . "</td><td>$response</td></tr>" . |
| 147 | '<tr><td><b>' . _("Message:") . "</td><td>$message</td></tr>" . |
| 148 | '<tr><td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" . |
| 149 | "</table><BR></tt></font><hr>"; |
| 150 | |
| 151 | $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, TRUE); |
| 152 | array_shift($data); |
| 153 | $wholemessage = implode('', $data); |
| 154 | |
| 155 | $ret = $wholemessage; |
| 156 | } |
| 157 | return $ret; |
| 158 | } |
| 159 | |
| 160 | function mime_print_body_lines ($imap_stream, $id, $ent_id=1, $encoding) { |
| 161 | |
| 162 | /* Don't kill the connection if the browser is over a dialup |
| 163 | * and it would take over 30 seconds to download it. |
| 164 | * Dont call set_time_limit in safe mode. |
| 165 | */ |
| 166 | |
| 167 | if (!ini_get('safe_mode')) { |
| 168 | set_time_limit(0); |
| 169 | } |
| 170 | /* in case of base64 encoded attachments, do not buffer them. |
| 171 | Instead, echo the decoded attachment directly to screen */ |
| 172 | if (strtolower($encoding) == 'base64') { |
| 173 | if (!$ent_id) { |
| 174 | $query = "FETCH $id BODY[]"; |
| 175 | } else { |
| 176 | $query = "FETCH $id BODY[$ent_id]"; |
| 177 | } |
| 178 | sqimap_run_command($imap_stream,$query,true,$response,$message,TRUE,'sqimap_base64_decode','php://stdout',true); |
| 179 | } else { |
| 180 | $body = mime_fetch_body ($imap_stream, $id, $ent_id); |
| 181 | echo decodeBody($body, $encoding); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | TODO, use the same method for quoted printable. |
| 186 | However, I assume that quoted printable attachments aren't that large |
| 187 | so the performancegain / memory usage drop will be minimal. |
| 188 | If we decide to add that then we need to adapt sqimap_fread because |
| 189 | we need to split te result on \n and fread doesn't stop at \n. That |
| 190 | means we also should provide $results from sqimap_fread (by ref) to |
| 191 | te function and set $no_return to false. The $filter function for |
| 192 | quoted printable should handle unsetting of $results. |
| 193 | */ |
| 194 | /* |
| 195 | TODO 2: find out how we write to the output stream php://stdout. fwrite |
| 196 | doesn't work because 'php://stdout isn't a stream. |
| 197 | */ |
| 198 | |
| 199 | return; |
| 200 | /* |
| 201 | fputs ($imap_stream, "$sid FETCH $id BODY[$ent_id]\r\n"); |
| 202 | $cnt = 0; |
| 203 | $continue = true; |
| 204 | $read = fgets ($imap_stream,8192); |
| 205 | |
| 206 | |
| 207 | // This could be bad -- if the section has sqimap_session_id() . ' OK' |
| 208 | // or similar, it will kill the download. |
| 209 | while (!ereg("^".$sid_s." (OK|BAD|NO)(.*)$", $read, $regs)) { |
| 210 | if (trim($read) == ')==') { |
| 211 | $read1 = $read; |
| 212 | $read = fgets ($imap_stream,4096); |
| 213 | if (ereg("^".$sid." (OK|BAD|NO)(.*)$", $read, $regs)) { |
| 214 | return; |
| 215 | } else { |
| 216 | echo decodeBody($read1, $encoding) . |
| 217 | decodeBody($read, $encoding); |
| 218 | } |
| 219 | } else if ($cnt) { |
| 220 | echo decodeBody($read, $encoding); |
| 221 | } |
| 222 | $read = fgets ($imap_stream,4096); |
| 223 | $cnt++; |
| 224 | // break; |
| 225 | } |
| 226 | */ |
| 227 | } |
| 228 | |
| 229 | /* -[ END MIME DECODING ]----------------------------------------------------------- */ |
| 230 | |
| 231 | /* This is here for debugging purposes. It will print out a list |
| 232 | * of all the entity IDs that are in the $message object. |
| 233 | */ |
| 234 | function listEntities ($message) { |
| 235 | if ($message) { |
| 236 | echo "<tt>" . $message->entity_id . ' : ' . $message->type0 . '/' . $message->type1 . ' parent = '. $message->parent->entity_id. '<br>'; |
| 237 | for ($i = 0; isset($message->entities[$i]); $i++) { |
| 238 | echo "$i : "; |
| 239 | $msg = listEntities($message->entities[$i]); |
| 240 | |
| 241 | if ($msg) { |
| 242 | echo "return: "; |
| 243 | return $msg; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | function getPriorityStr($priority) { |
| 250 | $priority_level = substr($priority,0,1); |
| 251 | |
| 252 | switch($priority_level) { |
| 253 | /* Check for a higher then normal priority. */ |
| 254 | case '1': |
| 255 | case '2': |
| 256 | $priority_string = _("High"); |
| 257 | break; |
| 258 | |
| 259 | /* Check for a lower then normal priority. */ |
| 260 | case '4': |
| 261 | case '5': |
| 262 | $priority_string = _("Low"); |
| 263 | break; |
| 264 | |
| 265 | /* Check for a normal priority. */ |
| 266 | case '3': |
| 267 | default: |
| 268 | $priority_level = '3'; |
| 269 | $priority_string = _("Normal"); |
| 270 | break; |
| 271 | |
| 272 | } |
| 273 | return $priority_string; |
| 274 | } |
| 275 | |
| 276 | /* returns a $message object for a particular entity id */ |
| 277 | function getEntity ($message, $ent_id) { |
| 278 | return $message->getEntity($ent_id); |
| 279 | } |
| 280 | |
| 281 | /* translateText |
| 282 | * Extracted from strings.php 23/03/2002 |
| 283 | */ |
| 284 | |
| 285 | function translateText(&$body, $wrap_at, $charset) { |
| 286 | global $where, $what; /* from searching */ |
| 287 | global $color; /* color theme */ |
| 288 | |
| 289 | require_once(SM_PATH . 'functions/url_parser.php'); |
| 290 | |
| 291 | $body_ary = explode("\n", $body); |
| 292 | for ($i=0; $i < count($body_ary); $i++) { |
| 293 | $line = $body_ary[$i]; |
| 294 | if (strlen($line) - 2 >= $wrap_at) { |
| 295 | sqWordWrap($line, $wrap_at); |
| 296 | } |
| 297 | $line = charset_decode($charset, $line); |
| 298 | $line = str_replace("\t", ' ', $line); |
| 299 | |
| 300 | parseUrl ($line); |
| 301 | |
| 302 | $quotes = 0; |
| 303 | $pos = 0; |
| 304 | $j = strlen($line); |
| 305 | |
| 306 | while ($pos < $j) { |
| 307 | if ($line[$pos] == ' ') { |
| 308 | $pos++; |
| 309 | } else if (strpos($line, '>', $pos) === $pos) { |
| 310 | $pos += 4; |
| 311 | $quotes++; |
| 312 | } else { |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if ($quotes % 2) { |
| 318 | if (!isset($color[13])) { |
| 319 | $color[13] = '#800000'; |
| 320 | } |
| 321 | $line = '<font color="' . $color[13] . '">' . $line . '</font>'; |
| 322 | } elseif ($quotes) { |
| 323 | if (!isset($color[14])) { |
| 324 | $color[14] = '#FF0000'; |
| 325 | } |
| 326 | $line = '<font color="' . $color[14] . '">' . $line . '</font>'; |
| 327 | } |
| 328 | |
| 329 | $body_ary[$i] = $line; |
| 330 | } |
| 331 | $body = '<pre>' . implode("\n", $body_ary) . '</pre>'; |
| 332 | } |
| 333 | |
| 334 | /* This returns a parsed string called $body. That string can then |
| 335 | * be displayed as the actual message in the HTML. It contains |
| 336 | * everything needed, including HTML Tags, Attachments at the |
| 337 | * bottom, etc. |
| 338 | */ |
| 339 | function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $mailbox='INBOX') { |
| 340 | /* This if statement checks for the entity to show as the |
| 341 | * primary message. To add more of them, just put them in the |
| 342 | * order that is their priority. |
| 343 | */ |
| 344 | global $startMessage, $username, $key, $imapServerAddress, $imapPort, |
| 345 | $show_html_default, $sort, $has_unsafe_images, $passed_ent_id; |
| 346 | global $languages, $squirrelmail_language; |
| 347 | |
| 348 | if( !sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET) ) { |
| 349 | $view_unsafe_images = false; |
| 350 | } |
| 351 | |
| 352 | $body = ''; |
| 353 | $urlmailbox = urlencode($mailbox); |
| 354 | $body_message = getEntity($message, $ent_num); |
| 355 | if (($body_message->header->type0 == 'text') || |
| 356 | ($body_message->header->type0 == 'rfc822')) { |
| 357 | $body = mime_fetch_body ($imap_stream, $id, $ent_num); |
| 358 | $body = decodeBody($body, $body_message->header->encoding); |
| 359 | |
| 360 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 361 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
| 362 | if (mb_detect_encoding($body) != 'ASCII') { |
| 363 | $body = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $body); |
| 364 | } |
| 365 | } |
| 366 | $hookResults = do_hook("message_body", $body); |
| 367 | $body = $hookResults[1]; |
| 368 | |
| 369 | /* If there are other types that shouldn't be formatted, add |
| 370 | * them here. |
| 371 | */ |
| 372 | |
| 373 | if ($body_message->header->type1 == 'html') { |
| 374 | if ($show_html_default <> 1) { |
| 375 | $entity_conv = array(' ' => ' ', |
| 376 | '<p>' => "\n", |
| 377 | '<P>' => "\n", |
| 378 | '<br>' => "\n", |
| 379 | '<BR>' => "\n", |
| 380 | '<br />' => "\n", |
| 381 | '<BR />' => "\n", |
| 382 | '>' => '>', |
| 383 | '<' => '<'); |
| 384 | $body = strtr($body, $entity_conv); |
| 385 | $body = strip_tags($body); |
| 386 | $body = trim($body); |
| 387 | translateText($body, $wrap_at, |
| 388 | $body_message->header->getParameter('charset')); |
| 389 | } else { |
| 390 | $body = magicHTML($body, $id, $message, $mailbox); |
| 391 | } |
| 392 | } else { |
| 393 | translateText($body, $wrap_at, |
| 394 | $body_message->header->getParameter('charset')); |
| 395 | } |
| 396 | $link = 'passed_id=' . $id . '&ent_id='.$ent_num. |
| 397 | '&mailbox=' . $urlmailbox .'&sort=' . $sort . |
| 398 | '&startMessage=' . $startMessage . '&show_more=0'; |
| 399 | if (isset($passed_ent_id)) { |
| 400 | $link .= '&passed_ent_id='.$passed_ent_id; |
| 401 | } |
| 402 | $body .= '<center><small><a href="download.php?absolute_dl=true&' . |
| 403 | $link . '">' . _("Download this as a file") . '</a>'; |
| 404 | if ($view_unsafe_images) { |
| 405 | $text = _("Hide Unsafe Images"); |
| 406 | } else { |
| 407 | if (isset($has_unsafe_images) && $has_unsafe_images) { |
| 408 | $link .= '&view_unsafe_images=1'; |
| 409 | $text = _("View Unsafe Images"); |
| 410 | } else { |
| 411 | $text = ''; |
| 412 | } |
| 413 | } |
| 414 | if($text != '') { |
| 415 | $body .= ' | <a href="read_body.php?' . $link . '">' . $text . '</a>'; |
| 416 | } |
| 417 | $body .= '</small></center><br>' . "\n"; |
| 418 | } |
| 419 | return $body; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | function formatAttachments($message, $exclude_id, $mailbox, $id) { |
| 424 | global $where, $what, $startMessage, $color; |
| 425 | static $ShownHTML = 0; |
| 426 | |
| 427 | $att_ar = $message->getAttachments($exclude_id); |
| 428 | |
| 429 | if (!count($att_ar)) return ''; |
| 430 | |
| 431 | $attachments = ''; |
| 432 | |
| 433 | $urlMailbox = urlencode($mailbox); |
| 434 | |
| 435 | foreach ($att_ar as $att) { |
| 436 | $ent = $att->entity_id; |
| 437 | $header = $att->header; |
| 438 | $type0 = strtolower($header->type0); |
| 439 | $type1 = strtolower($header->type1); |
| 440 | $name = ''; |
| 441 | $links['download link']['text'] = _("download"); |
| 442 | $links['download link']['href'] = SM_PATH . |
| 443 | "src/download.php?absolute_dl=true&passed_id=$id&mailbox=$urlMailbox&ent_id=$ent"; |
| 444 | $ImageURL = ''; |
| 445 | if ($type0 =='message' && $type1 == 'rfc822') { |
| 446 | $default_page = SM_PATH . 'src/read_body.php'; |
| 447 | $rfc822_header = $att->rfc822_header; |
| 448 | $filename = $rfc822_header->subject; |
| 449 | if (trim( $filename ) == '') { |
| 450 | $filename = 'untitled-[' . $ent . ']' ; |
| 451 | } |
| 452 | $from_o = $rfc822_header->from; |
| 453 | if (is_object($from_o)) { |
| 454 | $from_name = decodeHeader($from_o->getAddress(false)); |
| 455 | } else { |
| 456 | $from_name = _("Unknown sender"); |
| 457 | } |
| 458 | $description = $from_name; |
| 459 | } else { |
| 460 | $default_page = SM_PATH . 'src/download.php'; |
| 461 | if (is_object($header->disposition)) { |
| 462 | $filename = $header->disposition->getProperty('filename'); |
| 463 | if (trim($filename) == '') { |
| 464 | $name = decodeHeader($header->disposition->getProperty('name')); |
| 465 | if (trim($name) == '') { |
| 466 | $name = $header->getParameter('name'); |
| 467 | if(trim($name) == '') { |
| 468 | if (trim( $header->id ) == '') { |
| 469 | $filename = 'untitled-[' . $ent . ']' ; |
| 470 | } else { |
| 471 | $filename = 'cid: ' . $header->id; |
| 472 | } |
| 473 | } else { |
| 474 | $filename = $name; |
| 475 | } |
| 476 | } else { |
| 477 | $filename = $name; |
| 478 | } |
| 479 | } |
| 480 | } else { |
| 481 | $filename = $header->getParameter('name'); |
| 482 | if (!trim($filename)) { |
| 483 | if (trim( $header->id ) == '') { |
| 484 | $filename = 'untitled-[' . $ent . ']' ; |
| 485 | } else { |
| 486 | $filename = 'cid: ' . $header->id; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | if ($header->description) { |
| 491 | $description = decodeHeader($header->description); |
| 492 | } else { |
| 493 | $description = ''; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | $display_filename = $filename; |
| 498 | if (isset($passed_ent_id)) { |
| 499 | $passed_ent_id_link = '&passed_ent_id='.$passed_ent_id; |
| 500 | } else { |
| 501 | $passed_ent_id_link = ''; |
| 502 | } |
| 503 | $defaultlink = $default_page . "?startMessage=$startMessage" |
| 504 | . "&passed_id=$id&mailbox=$urlMailbox" |
| 505 | . '&ent_id='.$ent.$passed_ent_id_link; |
| 506 | if ($where && $what) { |
| 507 | $defaultlink .= '&where='. urlencode($where).'&what='.urlencode($what); |
| 508 | } |
| 509 | /* This executes the attachment hook with a specific MIME-type. |
| 510 | * If that doesn't have results, it tries if there's a rule |
| 511 | * for a more generic type. |
| 512 | */ |
| 513 | $hookresults = do_hook("attachment $type0/$type1", $links, |
| 514 | $startMessage, $id, $urlMailbox, $ent, $defaultlink, |
| 515 | $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); |
| 520 | } |
| 521 | |
| 522 | $links = $hookresults[1]; |
| 523 | $defaultlink = $hookresults[6]; |
| 524 | |
| 525 | $attachments .= '<TR><TD>' . |
| 526 | '<A HREF="'.$defaultlink.'">'.decodeHeader($display_filename).'</A> </TD>' . |
| 527 | '<TD><SMALL><b>' . show_readable_size($header->size) . |
| 528 | '</b> </small></TD>' . |
| 529 | '<TD><SMALL>[ '.htmlspecialchars($type0).'/'.htmlspecialchars($type1).' ] </SMALL></TD>' . |
| 530 | '<TD><SMALL>'; |
| 531 | $attachments .= '<b>' . $description . '</b>'; |
| 532 | $attachments .= '</SMALL></TD><TD><SMALL> '; |
| 533 | |
| 534 | $skipspaces = 1; |
| 535 | foreach ($links as $val) { |
| 536 | if ($skipspaces) { |
| 537 | $skipspaces = 0; |
| 538 | } else { |
| 539 | $attachments .= ' | '; |
| 540 | } |
| 541 | $attachments .= '<a href="' . $val['href'] . '">' . $val['text'] . '</a>'; |
| 542 | } |
| 543 | unset($links); |
| 544 | $attachments .= "</TD></TR>\n"; |
| 545 | } |
| 546 | $attachmentadd = do_hook_function('attachments_bottom',$attachments); |
| 547 | if ($attachmentadd != '') |
| 548 | $attachments = $attachmentadd; |
| 549 | return $attachments; |
| 550 | } |
| 551 | |
| 552 | function sqimap_base64_decode(&$string) { |
| 553 | |
| 554 | // base64 enoded data goes in pairs of 4 bytes. To achieve on the |
| 555 | // fly decoding (to reduce memory usage) you have to check if the |
| 556 | // data has incomplete pairs |
| 557 | |
| 558 | // remove the noise in order to check if the 4 bytes pairs are complete |
| 559 | $string = str_replace(array("\r\n","\n", "\r", " "),array('','','',''),$string); |
| 560 | |
| 561 | $sStringRem = ''; |
| 562 | $iMod = strlen($string) % 4; |
| 563 | if ($iMod) { |
| 564 | $sStringRem = substr($string,-$iMod); |
| 565 | // check if $sStringRem contains padding characters |
| 566 | if (substr($sStringRem,-1) != '=') { |
| 567 | $string = substr($string,0,-$iMod); |
| 568 | } else { |
| 569 | $sStringRem = ''; |
| 570 | } |
| 571 | } |
| 572 | $string = base64_decode($string); |
| 573 | return $sStringRem; |
| 574 | } |
| 575 | |
| 576 | |
| 577 | /* This function decodes the body depending on the encoding type. */ |
| 578 | function decodeBody($body, $encoding) { |
| 579 | global $show_html_default; |
| 580 | |
| 581 | $body = str_replace("\r\n", "\n", $body); |
| 582 | $encoding = strtolower($encoding); |
| 583 | |
| 584 | $encoding_handler = do_hook_function('decode_body', $encoding); |
| 585 | |
| 586 | |
| 587 | // plugins get first shot at decoding the body |
| 588 | // |
| 589 | if (!empty($encoding_handler) && function_exists($encoding_handler)) { |
| 590 | $body = $encoding_handler('decode', $body); |
| 591 | |
| 592 | } else if ($encoding == 'quoted-printable' || |
| 593 | $encoding == 'quoted_printable') { |
| 594 | $body = quoted_printable_decode($body); |
| 595 | |
| 596 | while (ereg("=\n", $body)) { |
| 597 | $body = ereg_replace ("=\n", '', $body); |
| 598 | } |
| 599 | |
| 600 | } else if ($encoding == 'base64') { |
| 601 | $body = base64_decode($body); |
| 602 | } |
| 603 | |
| 604 | // All other encodings are returned raw. |
| 605 | return $body; |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Decodes headers |
| 610 | * |
| 611 | * This functions decode strings that is encoded according to |
| 612 | * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text). |
| 613 | * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002 |
| 614 | * |
| 615 | * @param string $string header string that has to be made readable |
| 616 | * @param boolean $utfencode change message in order to be readable on user's charset. defaults to true |
| 617 | * @param boolean $htmlsave preserve spaces and sanitize html special characters. defaults to true |
| 618 | * @param boolean $decide decide if string can be utfencoded. defaults to false |
| 619 | * @return string decoded header string |
| 620 | */ |
| 621 | function decodeHeader ($string, $utfencode=true,$htmlsave=true,$decide=false) { |
| 622 | global $languages, $squirrelmail_language,$default_charset; |
| 623 | if (is_array($string)) { |
| 624 | $string = implode("\n", $string); |
| 625 | } |
| 626 | |
| 627 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 628 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
| 629 | $string = $languages[$squirrelmail_language]['XTRA_CODE']('decodeheader', $string); |
| 630 | // Do we need to return at this point? |
| 631 | // return $string; |
| 632 | } |
| 633 | $i = 0; |
| 634 | $iLastMatch = -2; |
| 635 | $encoded = true; |
| 636 | |
| 637 | $aString = explode(' ',$string); |
| 638 | $ret = ''; |
| 639 | foreach ($aString as $chunk) { |
| 640 | if ($encoded && $chunk === '') { |
| 641 | continue; |
| 642 | } elseif ($chunk === '') { |
| 643 | $ret .= ' '; |
| 644 | continue; |
| 645 | } |
| 646 | $encoded = false; |
| 647 | /* if encoded words are not separated by a linear-space-white we still catch them */ |
| 648 | $j = $i-1; |
| 649 | |
| 650 | while ($match = preg_match('/^(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=(.*)$/Ui',$chunk,$res)) { |
| 651 | /* if the last chunk isn't an encoded string then put back the space, otherwise don't */ |
| 652 | if ($iLastMatch !== $j) { |
| 653 | if ($htmlsave) { |
| 654 | $ret .= ' '; |
| 655 | } else { |
| 656 | $ret .= ' '; |
| 657 | } |
| 658 | } |
| 659 | $iLastMatch = $i; |
| 660 | $j = $i; |
| 661 | $ret .= $res[1]; |
| 662 | $encoding = ucfirst($res[3]); |
| 663 | |
| 664 | /* decide about valid decoding */ |
| 665 | if ($decide && is_conversion_safe($res[2])) { |
| 666 | $utfencode=true; |
| 667 | $can_be_encoded=true; |
| 668 | } else { |
| 669 | $can_be_encoded=false; |
| 670 | } |
| 671 | switch ($encoding) |
| 672 | { |
| 673 | case 'B': |
| 674 | $replace = base64_decode($res[4]); |
| 675 | if ($can_be_encoded) { |
| 676 | /* convert string to different charset, |
| 677 | * if functions asks for it (usually in compose) |
| 678 | */ |
| 679 | $ret .= charset_convert($res[2],$replace,$default_charset); |
| 680 | } else { |
| 681 | // convert string to html codes in order to display it |
| 682 | $ret .= charset_decode($res[2],$replace); |
| 683 | } |
| 684 | break; |
| 685 | case 'Q': |
| 686 | $replace = str_replace('_', ' ', $res[4]); |
| 687 | $replace = preg_replace('/=([0-9a-f]{2})/ie', 'chr(hexdec("\1"))', |
| 688 | $replace); |
| 689 | if ($utfencode) { |
| 690 | if ($can_be_encoded) { |
| 691 | /* convert string to different charset, |
| 692 | * if functions asks for it (usually in compose) |
| 693 | */ |
| 694 | $replace = charset_convert($res[2], $replace,$default_charset); |
| 695 | } else { |
| 696 | // convert string to html codes in order to display it |
| 697 | $replace = charset_decode($res[2], $replace); |
| 698 | } |
| 699 | } else { |
| 700 | if ($htmlsave) { |
| 701 | $replace = htmlspecialchars($replace); |
| 702 | } |
| 703 | } |
| 704 | $ret .= $replace; |
| 705 | break; |
| 706 | default: |
| 707 | break; |
| 708 | } |
| 709 | $chunk = $res[5]; |
| 710 | $encoded = true; |
| 711 | } |
| 712 | if (!$encoded) { |
| 713 | if ($htmlsave) { |
| 714 | $ret .= ' '; |
| 715 | } else { |
| 716 | $ret .= ' '; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (!$encoded && $htmlsave) { |
| 721 | $ret .= htmlspecialchars($chunk); |
| 722 | } else { |
| 723 | $ret .= $chunk; |
| 724 | } |
| 725 | ++$i; |
| 726 | } |
| 727 | /* remove the first added space */ |
| 728 | if ($ret) { |
| 729 | if ($htmlsave) { |
| 730 | $ret = substr($ret,5); |
| 731 | } else { |
| 732 | $ret = substr($ret,1); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | return $ret; |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Encodes header as quoted-printable |
| 741 | * |
| 742 | * Encode a string according to RFC 1522 for use in headers if it |
| 743 | * contains 8-bit characters or anything that looks like it should |
| 744 | * be encoded. |
| 745 | * |
| 746 | * @param string $string header string, that has to be encoded |
| 747 | * @return string quoted-printable encoded string |
| 748 | */ |
| 749 | function encodeHeader ($string) { |
| 750 | global $default_charset, $languages, $squirrelmail_language; |
| 751 | |
| 752 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 753 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
| 754 | return $languages[$squirrelmail_language]['XTRA_CODE']('encodeheader', $string); |
| 755 | } |
| 756 | |
| 757 | // Encode only if the string contains 8-bit characters or =? |
| 758 | $j = strlen($string); |
| 759 | $max_l = 75 - strlen($default_charset) - 7; |
| 760 | $aRet = array(); |
| 761 | $ret = ''; |
| 762 | $iEncStart = $enc_init = false; |
| 763 | $cur_l = $iOffset = 0; |
| 764 | for($i = 0; $i < $j; ++$i) { |
| 765 | switch($string{$i}) |
| 766 | { |
| 767 | case '=': |
| 768 | case '<': |
| 769 | case '>': |
| 770 | case ',': |
| 771 | case '?': |
| 772 | case '_': |
| 773 | if ($iEncStart === false) { |
| 774 | $iEncStart = $i; |
| 775 | } |
| 776 | $cur_l+=3; |
| 777 | if ($cur_l > ($max_l-2)) { |
| 778 | /* if there is an stringpart that doesn't need encoding, add it */ |
| 779 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
| 780 | $aRet[] = "=?$default_charset?Q?$ret?="; |
| 781 | $iOffset = $i; |
| 782 | $cur_l = 0; |
| 783 | $ret = ''; |
| 784 | $iEncStart = false; |
| 785 | } else { |
| 786 | $ret .= sprintf("=%02X",ord($string{$i})); |
| 787 | } |
| 788 | break; |
| 789 | case '(': |
| 790 | case ')': |
| 791 | if ($iEncStart !== false) { |
| 792 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
| 793 | $aRet[] = "=?$default_charset?Q?$ret?="; |
| 794 | $iOffset = $i; |
| 795 | $cur_l = 0; |
| 796 | $ret = ''; |
| 797 | $iEncStart = false; |
| 798 | } |
| 799 | break; |
| 800 | case ' ': |
| 801 | if ($iEncStart !== false) { |
| 802 | $cur_l++; |
| 803 | if ($cur_l > $max_l) { |
| 804 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
| 805 | $aRet[] = "=?$default_charset?Q?$ret?="; |
| 806 | $iOffset = $i; |
| 807 | $cur_l = 0; |
| 808 | $ret = ''; |
| 809 | $iEncStart = false; |
| 810 | } else { |
| 811 | $ret .= '_'; |
| 812 | } |
| 813 | } |
| 814 | break; |
| 815 | default: |
| 816 | $k = ord($string{$i}); |
| 817 | if ($k > 126) { |
| 818 | if ($iEncStart === false) { |
| 819 | // do not start encoding in the middle of a string, also take the rest of the word. |
| 820 | $sLeadString = substr($string,0,$i); |
| 821 | $aLeadString = explode(' ',$sLeadString); |
| 822 | $sToBeEncoded = array_pop($aLeadString); |
| 823 | $iEncStart = $i - strlen($sToBeEncoded); |
| 824 | $ret .= $sToBeEncoded; |
| 825 | $cur_l += strlen($sToBeEncoded); |
| 826 | } |
| 827 | $cur_l += 3; |
| 828 | /* first we add the encoded string that reached it's max size */ |
| 829 | if ($cur_l > ($max_l-2)) { |
| 830 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
| 831 | $aRet[] = "=?$default_charset?Q?$ret?= "; /* the next part is also encoded => separate by space */ |
| 832 | $cur_l = 3; |
| 833 | $ret = ''; |
| 834 | $iOffset = $i; |
| 835 | $iEncStart = $i; |
| 836 | } |
| 837 | $enc_init = true; |
| 838 | $ret .= sprintf("=%02X", $k); |
| 839 | } else { |
| 840 | if ($iEncStart !== false) { |
| 841 | $cur_l++; |
| 842 | if ($cur_l > $max_l) { |
| 843 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
| 844 | $aRet[] = "=?$default_charset?Q?$ret?="; |
| 845 | $iEncStart = false; |
| 846 | $iOffset = $i; |
| 847 | $cur_l = 0; |
| 848 | $ret = ''; |
| 849 | } else { |
| 850 | $ret .= $string{$i}; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | break; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | if ($enc_init) { |
| 859 | if ($iEncStart !== false) { |
| 860 | $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset); |
| 861 | $aRet[] = "=?$default_charset?Q?$ret?="; |
| 862 | } else { |
| 863 | $aRet[] = substr($string,$iOffset); |
| 864 | } |
| 865 | $string = implode('',$aRet); |
| 866 | } |
| 867 | return $string; |
| 868 | } |
| 869 | |
| 870 | /* This function trys to locate the entity_id of a specific mime element */ |
| 871 | function find_ent_id($id, $message) { |
| 872 | for ($i = 0, $ret = ''; $ret == '' && $i < count($message->entities); $i++) { |
| 873 | if ($message->entities[$i]->header->type0 == 'multipart') { |
| 874 | $ret = find_ent_id($id, $message->entities[$i]); |
| 875 | } else { |
| 876 | if (strcasecmp($message->entities[$i]->header->id, $id) == 0) { |
| 877 | // if (sq_check_save_extension($message->entities[$i])) { |
| 878 | return $message->entities[$i]->entity_id; |
| 879 | // } |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | return $ret; |
| 884 | } |
| 885 | |
| 886 | function sq_check_save_extension($message) { |
| 887 | $filename = $message->getFilename(); |
| 888 | $ext = substr($filename, strrpos($filename,'.')+1); |
| 889 | $save_extensions = array('jpg','jpeg','gif','png','bmp'); |
| 890 | return in_array($ext, $save_extensions); |
| 891 | } |
| 892 | |
| 893 | |
| 894 | /** |
| 895 | ** HTMLFILTER ROUTINES |
| 896 | */ |
| 897 | |
| 898 | /** |
| 899 | * This function is more or less a wrapper around stripslashes. Apparently |
| 900 | * Explorer is stupid enough to just remove the backslashes and then |
| 901 | * execute the content of the attribute as if nothing happened. |
| 902 | * Who does that? |
| 903 | * |
| 904 | * @param attvalue The value of the attribute |
| 905 | * @return attvalue The value of the attribute stripslashed. |
| 906 | */ |
| 907 | function sq_unbackslash($attvalue){ |
| 908 | /** |
| 909 | * Remove any backslashes. See if there are any first. |
| 910 | */ |
| 911 | |
| 912 | if (strstr($attvalue, '\\') !== false){ |
| 913 | $attvalue = stripslashes($attvalue); |
| 914 | } |
| 915 | return $attvalue; |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * Kill any tabs, newlines, or carriage returns. Our friends the |
| 920 | * makers of the browser with 95% market value decided that it'd |
| 921 | * be funny to make "java[tab]script" be just as good as "javascript". |
| 922 | * |
| 923 | * @param attvalue The attribute value before extraneous spaces removed. |
| 924 | * @return attvalue The attribute value after extraneous spaces removed. |
| 925 | */ |
| 926 | function sq_unspace($attvalue){ |
| 927 | if (strcspn($attvalue, "\t\r\n") != strlen($attvalue)){ |
| 928 | $attvalue = str_replace(Array("\t", "\r", "\n"), Array('', '', ''), |
| 929 | $attvalue); |
| 930 | } |
| 931 | return $attvalue; |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * This function returns the final tag out of the tag name, an array |
| 936 | * of attributes, and the type of the tag. This function is called by |
| 937 | * sq_sanitize internally. |
| 938 | * |
| 939 | * @param $tagname the name of the tag. |
| 940 | * @param $attary the array of attributes and their values |
| 941 | * @param $tagtype The type of the tag (see in comments). |
| 942 | * @return a string with the final tag representation. |
| 943 | */ |
| 944 | function sq_tagprint($tagname, $attary, $tagtype){ |
| 945 | $me = 'sq_tagprint'; |
| 946 | |
| 947 | if ($tagtype == 2){ |
| 948 | $fulltag = '</' . $tagname . '>'; |
| 949 | } else { |
| 950 | $fulltag = '<' . $tagname; |
| 951 | if (is_array($attary) && sizeof($attary)){ |
| 952 | $atts = Array(); |
| 953 | while (list($attname, $attvalue) = each($attary)){ |
| 954 | array_push($atts, "$attname=$attvalue"); |
| 955 | } |
| 956 | $fulltag .= ' ' . join(" ", $atts); |
| 957 | } |
| 958 | if ($tagtype == 3){ |
| 959 | $fulltag .= ' /'; |
| 960 | } |
| 961 | $fulltag .= '>'; |
| 962 | } |
| 963 | return $fulltag; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * A small helper function to use with array_walk. Modifies a by-ref |
| 968 | * value and makes it lowercase. |
| 969 | * |
| 970 | * @param $val a value passed by-ref. |
| 971 | * @return void since it modifies a by-ref value. |
| 972 | */ |
| 973 | function sq_casenormalize(&$val){ |
| 974 | $val = strtolower($val); |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * This function skips any whitespace from the current position within |
| 979 | * a string and to the next non-whitespace value. |
| 980 | * |
| 981 | * @param $body the string |
| 982 | * @param $offset the offset within the string where we should start |
| 983 | * looking for the next non-whitespace character. |
| 984 | * @return the location within the $body where the next |
| 985 | * non-whitespace char is located. |
| 986 | */ |
| 987 | function sq_skipspace($body, $offset){ |
| 988 | $me = 'sq_skipspace'; |
| 989 | preg_match('/^(\s*)/s', substr($body, $offset), $matches); |
| 990 | if (sizeof($matches{1})){ |
| 991 | $count = strlen($matches{1}); |
| 992 | $offset += $count; |
| 993 | } |
| 994 | return $offset; |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * This function looks for the next character within a string. It's |
| 999 | * really just a glorified "strpos", except it catches if failures |
| 1000 | * nicely. |
| 1001 | * |
| 1002 | * @param $body The string to look for needle in. |
| 1003 | * @param $offset Start looking from this position. |
| 1004 | * @param $needle The character/string to look for. |
| 1005 | * @return location of the next occurance of the needle, or |
| 1006 | * strlen($body) if needle wasn't found. |
| 1007 | */ |
| 1008 | function sq_findnxstr($body, $offset, $needle){ |
| 1009 | $me = 'sq_findnxstr'; |
| 1010 | $pos = strpos($body, $needle, $offset); |
| 1011 | if ($pos === FALSE){ |
| 1012 | $pos = strlen($body); |
| 1013 | } |
| 1014 | return $pos; |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * This function takes a PCRE-style regexp and tries to match it |
| 1019 | * within the string. |
| 1020 | * |
| 1021 | * @param $body The string to look for needle in. |
| 1022 | * @param $offset Start looking from here. |
| 1023 | * @param $reg A PCRE-style regex to match. |
| 1024 | * @return Returns a false if no matches found, or an array |
| 1025 | * with the following members: |
| 1026 | * - integer with the location of the match within $body |
| 1027 | * - string with whatever content between offset and the match |
| 1028 | * - string with whatever it is we matched |
| 1029 | */ |
| 1030 | function sq_findnxreg($body, $offset, $reg){ |
| 1031 | $me = 'sq_findnxreg'; |
| 1032 | $matches = Array(); |
| 1033 | $retarr = Array(); |
| 1034 | preg_match("%^(.*?)($reg)%si", substr($body, $offset), $matches); |
| 1035 | if (!isset($matches{0}) || !$matches{0}){ |
| 1036 | $retarr = false; |
| 1037 | } else { |
| 1038 | $retarr{0} = $offset + strlen($matches{1}); |
| 1039 | $retarr{1} = $matches{1}; |
| 1040 | $retarr{2} = $matches{2}; |
| 1041 | } |
| 1042 | return $retarr; |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * This function looks for the next tag. |
| 1047 | * |
| 1048 | * @param $body String where to look for the next tag. |
| 1049 | * @param $offset Start looking from here. |
| 1050 | * @return false if no more tags exist in the body, or |
| 1051 | * an array with the following members: |
| 1052 | * - string with the name of the tag |
| 1053 | * - array with attributes and their values |
| 1054 | * - integer with tag type (1, 2, or 3) |
| 1055 | * - integer where the tag starts (starting "<") |
| 1056 | * - integer where the tag ends (ending ">") |
| 1057 | * first three members will be false, if the tag is invalid. |
| 1058 | */ |
| 1059 | function sq_getnxtag($body, $offset){ |
| 1060 | $me = 'sq_getnxtag'; |
| 1061 | if ($offset > strlen($body)){ |
| 1062 | return false; |
| 1063 | } |
| 1064 | $lt = sq_findnxstr($body, $offset, "<"); |
| 1065 | if ($lt == strlen($body)){ |
| 1066 | return false; |
| 1067 | } |
| 1068 | /** |
| 1069 | * We are here: |
| 1070 | * blah blah <tag attribute="value"> |
| 1071 | * \---------^ |
| 1072 | */ |
| 1073 | $pos = sq_skipspace($body, $lt+1); |
| 1074 | if ($pos >= strlen($body)){ |
| 1075 | return Array(false, false, false, $lt, strlen($body)); |
| 1076 | } |
| 1077 | /** |
| 1078 | * There are 3 kinds of tags: |
| 1079 | * 1. Opening tag, e.g.: |
| 1080 | * <a href="blah"> |
| 1081 | * 2. Closing tag, e.g.: |
| 1082 | * </a> |
| 1083 | * 3. XHTML-style content-less tag, e.g.: |
| 1084 | * <img src="blah"/> |
| 1085 | */ |
| 1086 | $tagtype = false; |
| 1087 | switch (substr($body, $pos, 1)){ |
| 1088 | case '/': |
| 1089 | $tagtype = 2; |
| 1090 | $pos++; |
| 1091 | break; |
| 1092 | case '!': |
| 1093 | /** |
| 1094 | * A comment or an SGML declaration. |
| 1095 | */ |
| 1096 | if (substr($body, $pos+1, 2) == "--"){ |
| 1097 | $gt = strpos($body, "-->", $pos); |
| 1098 | if ($gt === false){ |
| 1099 | $gt = strlen($body); |
| 1100 | } else { |
| 1101 | $gt += 2; |
| 1102 | } |
| 1103 | return Array(false, false, false, $lt, $gt); |
| 1104 | } else { |
| 1105 | $gt = sq_findnxstr($body, $pos, ">"); |
| 1106 | return Array(false, false, false, $lt, $gt); |
| 1107 | } |
| 1108 | break; |
| 1109 | default: |
| 1110 | /** |
| 1111 | * Assume tagtype 1 for now. If it's type 3, we'll switch values |
| 1112 | * later. |
| 1113 | */ |
| 1114 | $tagtype = 1; |
| 1115 | break; |
| 1116 | } |
| 1117 | |
| 1118 | $tag_start = $pos; |
| 1119 | $tagname = ''; |
| 1120 | /** |
| 1121 | * Look for next [\W-_], which will indicate the end of the tag name. |
| 1122 | */ |
| 1123 | $regary = sq_findnxreg($body, $pos, "[^\w\-_]"); |
| 1124 | if ($regary == false){ |
| 1125 | return Array(false, false, false, $lt, strlen($body)); |
| 1126 | } |
| 1127 | list($pos, $tagname, $match) = $regary; |
| 1128 | $tagname = strtolower($tagname); |
| 1129 | |
| 1130 | /** |
| 1131 | * $match can be either of these: |
| 1132 | * '>' indicating the end of the tag entirely. |
| 1133 | * '\s' indicating the end of the tag name. |
| 1134 | * '/' indicating that this is type-3 xhtml tag. |
| 1135 | * |
| 1136 | * Whatever else we find there indicates an invalid tag. |
| 1137 | */ |
| 1138 | switch ($match){ |
| 1139 | case '/': |
| 1140 | /** |
| 1141 | * This is an xhtml-style tag with a closing / at the |
| 1142 | * end, like so: <img src="blah"/>. Check if it's followed |
| 1143 | * by the closing bracket. If not, then this tag is invalid |
| 1144 | */ |
| 1145 | if (substr($body, $pos, 2) == "/>"){ |
| 1146 | $pos++; |
| 1147 | $tagtype = 3; |
| 1148 | } else { |
| 1149 | $gt = sq_findnxstr($body, $pos, ">"); |
| 1150 | $retary = Array(false, false, false, $lt, $gt); |
| 1151 | return $retary; |
| 1152 | } |
| 1153 | case '>': |
| 1154 | return Array($tagname, false, $tagtype, $lt, $pos); |
| 1155 | break; |
| 1156 | default: |
| 1157 | /** |
| 1158 | * Check if it's whitespace |
| 1159 | */ |
| 1160 | if (!preg_match('/\s/', $match)){ |
| 1161 | /** |
| 1162 | * This is an invalid tag! Look for the next closing ">". |
| 1163 | */ |
| 1164 | $gt = sq_findnxstr($body, $lt, ">"); |
| 1165 | return Array(false, false, false, $lt, $gt); |
| 1166 | } |
| 1167 | break; |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * At this point we're here: |
| 1172 | * <tagname attribute='blah'> |
| 1173 | * \-------^ |
| 1174 | * |
| 1175 | * At this point we loop in order to find all attributes. |
| 1176 | */ |
| 1177 | $attname = ''; |
| 1178 | $atttype = false; |
| 1179 | $attary = Array(); |
| 1180 | |
| 1181 | while ($pos <= strlen($body)){ |
| 1182 | $pos = sq_skipspace($body, $pos); |
| 1183 | if ($pos == strlen($body)){ |
| 1184 | /** |
| 1185 | * Non-closed tag. |
| 1186 | */ |
| 1187 | return Array(false, false, false, $lt, $pos); |
| 1188 | } |
| 1189 | /** |
| 1190 | * See if we arrived at a ">" or "/>", which means that we reached |
| 1191 | * the end of the tag. |
| 1192 | */ |
| 1193 | $matches = Array(); |
| 1194 | if (preg_match("%^(\s*)(>|/>)%s", substr($body, $pos), $matches)) { |
| 1195 | /** |
| 1196 | * Yep. So we did. |
| 1197 | */ |
| 1198 | $pos += strlen($matches{1}); |
| 1199 | if ($matches{2} == "/>"){ |
| 1200 | $tagtype = 3; |
| 1201 | $pos++; |
| 1202 | } |
| 1203 | return Array($tagname, $attary, $tagtype, $lt, $pos); |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * There are several types of attributes, with optional |
| 1208 | * [:space:] between members. |
| 1209 | * Type 1: |
| 1210 | * attrname[:space:]=[:space:]'CDATA' |
| 1211 | * Type 2: |
| 1212 | * attrname[:space:]=[:space:]"CDATA" |
| 1213 | * Type 3: |
| 1214 | * attr[:space:]=[:space:]CDATA |
| 1215 | * Type 4: |
| 1216 | * attrname |
| 1217 | * |
| 1218 | * We leave types 1 and 2 the same, type 3 we check for |
| 1219 | * '"' and convert to """ if needed, then wrap in |
| 1220 | * double quotes. Type 4 we convert into: |
| 1221 | * attrname="yes". |
| 1222 | */ |
| 1223 | $regary = sq_findnxreg($body, $pos, "[^:\w\-_]"); |
| 1224 | if ($regary == false){ |
| 1225 | /** |
| 1226 | * Looks like body ended before the end of tag. |
| 1227 | */ |
| 1228 | return Array(false, false, false, $lt, strlen($body)); |
| 1229 | } |
| 1230 | list($pos, $attname, $match) = $regary; |
| 1231 | $attname = strtolower($attname); |
| 1232 | /** |
| 1233 | * We arrived at the end of attribute name. Several things possible |
| 1234 | * here: |
| 1235 | * '>' means the end of the tag and this is attribute type 4 |
| 1236 | * '/' if followed by '>' means the same thing as above |
| 1237 | * '\s' means a lot of things -- look what it's followed by. |
| 1238 | * anything else means the attribute is invalid. |
| 1239 | */ |
| 1240 | switch($match){ |
| 1241 | case '/': |
| 1242 | /** |
| 1243 | * This is an xhtml-style tag with a closing / at the |
| 1244 | * end, like so: <img src="blah"/>. Check if it's followed |
| 1245 | * by the closing bracket. If not, then this tag is invalid |
| 1246 | */ |
| 1247 | if (substr($body, $pos, 2) == "/>"){ |
| 1248 | $pos++; |
| 1249 | $tagtype = 3; |
| 1250 | } else { |
| 1251 | $gt = sq_findnxstr($body, $pos, ">"); |
| 1252 | $retary = Array(false, false, false, $lt, $gt); |
| 1253 | return $retary; |
| 1254 | } |
| 1255 | case '>': |
| 1256 | $attary{$attname} = '"yes"'; |
| 1257 | return Array($tagname, $attary, $tagtype, $lt, $pos); |
| 1258 | break; |
| 1259 | default: |
| 1260 | /** |
| 1261 | * Skip whitespace and see what we arrive at. |
| 1262 | */ |
| 1263 | $pos = sq_skipspace($body, $pos); |
| 1264 | $char = substr($body, $pos, 1); |
| 1265 | /** |
| 1266 | * Two things are valid here: |
| 1267 | * '=' means this is attribute type 1 2 or 3. |
| 1268 | * \w means this was attribute type 4. |
| 1269 | * anything else we ignore and re-loop. End of tag and |
| 1270 | * invalid stuff will be caught by our checks at the beginning |
| 1271 | * of the loop. |
| 1272 | */ |
| 1273 | if ($char == "="){ |
| 1274 | $pos++; |
| 1275 | $pos = sq_skipspace($body, $pos); |
| 1276 | /** |
| 1277 | * Here are 3 possibilities: |
| 1278 | * "'" attribute type 1 |
| 1279 | * '"' attribute type 2 |
| 1280 | * everything else is the content of tag type 3 |
| 1281 | */ |
| 1282 | $quot = substr($body, $pos, 1); |
| 1283 | if ($quot == "'"){ |
| 1284 | $regary = sq_findnxreg($body, $pos+1, "\'"); |
| 1285 | if ($regary == false){ |
| 1286 | return Array(false, false, false, $lt, strlen($body)); |
| 1287 | } |
| 1288 | list($pos, $attval, $match) = $regary; |
| 1289 | $pos++; |
| 1290 | $attary{$attname} = "'" . $attval . "'"; |
| 1291 | } else if ($quot == '"'){ |
| 1292 | $regary = sq_findnxreg($body, $pos+1, '\"'); |
| 1293 | if ($regary == false){ |
| 1294 | return Array(false, false, false, $lt, strlen($body)); |
| 1295 | } |
| 1296 | list($pos, $attval, $match) = $regary; |
| 1297 | $pos++; |
| 1298 | $attary{$attname} = '"' . $attval . '"'; |
| 1299 | } else { |
| 1300 | /** |
| 1301 | * These are hateful. Look for \s, or >. |
| 1302 | */ |
| 1303 | $regary = sq_findnxreg($body, $pos, "[\s>]"); |
| 1304 | if ($regary == false){ |
| 1305 | return Array(false, false, false, $lt, strlen($body)); |
| 1306 | } |
| 1307 | list($pos, $attval, $match) = $regary; |
| 1308 | /** |
| 1309 | * If it's ">" it will be caught at the top. |
| 1310 | */ |
| 1311 | $attval = preg_replace("/\"/s", """, $attval); |
| 1312 | $attary{$attname} = '"' . $attval . '"'; |
| 1313 | } |
| 1314 | } else if (preg_match("|[\w/>]|", $char)) { |
| 1315 | /** |
| 1316 | * That was attribute type 4. |
| 1317 | */ |
| 1318 | $attary{$attname} = '"yes"'; |
| 1319 | } else { |
| 1320 | /** |
| 1321 | * An illegal character. Find next '>' and return. |
| 1322 | */ |
| 1323 | $gt = sq_findnxstr($body, $pos, ">"); |
| 1324 | return Array(false, false, false, $lt, $gt); |
| 1325 | } |
| 1326 | break; |
| 1327 | } |
| 1328 | } |
| 1329 | /** |
| 1330 | * The fact that we got here indicates that the tag end was never |
| 1331 | * found. Return invalid tag indication so it gets stripped. |
| 1332 | */ |
| 1333 | return Array(false, false, false, $lt, strlen($body)); |
| 1334 | } |
| 1335 | |
| 1336 | /** |
| 1337 | * This function checks attribute values for entity-encoded values |
| 1338 | * and returns them translated into 8-bit strings so we can run |
| 1339 | * checks on them. |
| 1340 | * |
| 1341 | * @param $attvalue A string to run entity check against. |
| 1342 | * @return Translated value. |
| 1343 | */ |
| 1344 | |
| 1345 | function sq_deent($attvalue){ |
| 1346 | $me = 'sq_deent'; |
| 1347 | /** |
| 1348 | * See if we have to run the checks first. All entities must start |
| 1349 | * with "&". |
| 1350 | */ |
| 1351 | if (strpos($attvalue, '&') === false){ |
| 1352 | return $attvalue; |
| 1353 | } |
| 1354 | /** |
| 1355 | * Check named entities first. |
| 1356 | */ |
| 1357 | $trans = get_html_translation_table(HTML_ENTITIES); |
| 1358 | /** |
| 1359 | * Leave " in, as it can mess us up. |
| 1360 | */ |
| 1361 | $trans = array_flip($trans); |
| 1362 | unset($trans{'"'}); |
| 1363 | while (list($ent, $val) = each($trans)){ |
| 1364 | $attvalue = preg_replace('/' . $ent . '*/si', $val, $attvalue); |
| 1365 | } |
| 1366 | /** |
| 1367 | * Now translate numbered entities from 1 to 255 if needed. |
| 1368 | */ |
| 1369 | if (strpos($attvalue, '#') !== false){ |
| 1370 | $omit = Array(34, 39); |
| 1371 | for ($asc = 256; $asc >= 0; $asc--){ |
| 1372 | if (!in_array($asc, $omit)){ |
| 1373 | $chr = chr($asc); |
| 1374 | $octrule = '/\�*' . $asc . ';*/si'; |
| 1375 | $hexrule = '/\�*' . dechex($asc) . ';*/si'; |
| 1376 | $attvalue = preg_replace($octrule, $chr, $attvalue); |
| 1377 | $attvalue = preg_replace($hexrule, $chr, $attvalue); |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | return $attvalue; |
| 1382 | } |
| 1383 | |
| 1384 | /** |
| 1385 | * This function runs various checks against the attributes. |
| 1386 | * |
| 1387 | * @param $tagname String with the name of the tag. |
| 1388 | * @param $attary Array with all tag attributes. |
| 1389 | * @param $rm_attnames See description for sq_sanitize |
| 1390 | * @param $bad_attvals See description for sq_sanitize |
| 1391 | * @param $add_attr_to_tag See description for sq_sanitize |
| 1392 | * @param $message message object |
| 1393 | * @param $id message id |
| 1394 | * @return Array with modified attributes. |
| 1395 | */ |
| 1396 | function sq_fixatts($tagname, |
| 1397 | $attary, |
| 1398 | $rm_attnames, |
| 1399 | $bad_attvals, |
| 1400 | $add_attr_to_tag, |
| 1401 | $message, |
| 1402 | $id, |
| 1403 | $mailbox |
| 1404 | ){ |
| 1405 | $me = 'sq_fixatts'; |
| 1406 | while (list($attname, $attvalue) = each($attary)){ |
| 1407 | /** |
| 1408 | * See if this attribute should be removed. |
| 1409 | */ |
| 1410 | foreach ($rm_attnames as $matchtag=>$matchattrs){ |
| 1411 | if (preg_match($matchtag, $tagname)){ |
| 1412 | foreach ($matchattrs as $matchattr){ |
| 1413 | if (preg_match($matchattr, $attname)){ |
| 1414 | unset($attary{$attname}); |
| 1415 | continue; |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | /** |
| 1421 | * Remove any backslashes, entities, and extraneous whitespace. |
| 1422 | */ |
| 1423 | $attvalue = sq_unbackslash($attvalue); |
| 1424 | $attvalue = sq_deent($attvalue); |
| 1425 | $attvalue = sq_unspace($attvalue); |
| 1426 | |
| 1427 | /** |
| 1428 | * Remove \r \n \t \0 " " "\\" |
| 1429 | */ |
| 1430 | $attvalue = str_replace(Array("\r", "\n", "\t", "\0", " ", "\\"), |
| 1431 | Array('', '','','','',''), $attvalue); |
| 1432 | |
| 1433 | /** |
| 1434 | * Now let's run checks on the attvalues. |
| 1435 | * I don't expect anyone to comprehend this. If you do, |
| 1436 | * get in touch with me so I can drive to where you live and |
| 1437 | * shake your hand personally. :) |
| 1438 | */ |
| 1439 | foreach ($bad_attvals as $matchtag=>$matchattrs){ |
| 1440 | if (preg_match($matchtag, $tagname)){ |
| 1441 | foreach ($matchattrs as $matchattr=>$valary){ |
| 1442 | if (preg_match($matchattr, $attname)){ |
| 1443 | /** |
| 1444 | * There are two arrays in valary. |
| 1445 | * First is matches. |
| 1446 | * Second one is replacements |
| 1447 | */ |
| 1448 | list($valmatch, $valrepl) = $valary; |
| 1449 | $newvalue = |
| 1450 | preg_replace($valmatch, $valrepl, $attvalue); |
| 1451 | if ($newvalue != $attvalue){ |
| 1452 | $attary{$attname} = $newvalue; |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | } |
| 1458 | /** |
| 1459 | * Turn cid: urls into http-friendly ones. |
| 1460 | */ |
| 1461 | if (preg_match("/^[\'\"]\s*cid:/si", $attvalue)){ |
| 1462 | $attary{$attname} = sq_cid2http($message, $id, $attvalue, $mailbox); |
| 1463 | } |
| 1464 | } |
| 1465 | /** |
| 1466 | * See if we need to append any attributes to this tag. |
| 1467 | */ |
| 1468 | foreach ($add_attr_to_tag as $matchtag=>$addattary){ |
| 1469 | if (preg_match($matchtag, $tagname)){ |
| 1470 | $attary = array_merge($attary, $addattary); |
| 1471 | } |
| 1472 | } |
| 1473 | return $attary; |
| 1474 | } |
| 1475 | |
| 1476 | /** |
| 1477 | * This function edits the style definition to make them friendly and |
| 1478 | * usable in squirrelmail. |
| 1479 | * |
| 1480 | * @param $message the message object |
| 1481 | * @param $id the message id |
| 1482 | * @param $content a string with whatever is between <style> and </style> |
| 1483 | * @param $mailbox the message mailbox |
| 1484 | * @return a string with edited content. |
| 1485 | */ |
| 1486 | function sq_fixstyle($body, $pos, $message, $id, $mailbox){ |
| 1487 | global $view_unsafe_images; |
| 1488 | $me = 'sq_fixstyle'; |
| 1489 | $ret = sq_findnxreg($body, $pos, '</\s*style\s*>'); |
| 1490 | if ($ret == FALSE){ |
| 1491 | return array(FALSE, strlen($body)); |
| 1492 | } |
| 1493 | $newpos = $ret[0] + strlen($ret[2]); |
| 1494 | $content = $ret[1]; |
| 1495 | /** |
| 1496 | * First look for general BODY style declaration, which would be |
| 1497 | * like so: |
| 1498 | * body {background: blah-blah} |
| 1499 | * and change it to .bodyclass so we can just assign it to a <div> |
| 1500 | */ |
| 1501 | $content = preg_replace("|body(\s*\{.*?\})|si", ".bodyclass\\1", $content); |
| 1502 | $secremoveimg = '../images/' . _("sec_remove_eng.png"); |
| 1503 | /** |
| 1504 | * Fix url('blah') declarations. |
| 1505 | */ |
| 1506 | $content = preg_replace("|url\s*\(\s*([\'\"])\s*\S+script\s*:.*?([\'\"])\s*\)|si", |
| 1507 | "url(\\1$secremoveimg\\2)", $content); |
| 1508 | /** |
| 1509 | * Fix url('https*://.*) declarations but only if $view_unsafe_images |
| 1510 | * is false. |
| 1511 | */ |
| 1512 | if (!$view_unsafe_images){ |
| 1513 | $content = preg_replace("|url\s*\(\s*([\'\"])\s*https*:.*?([\'\"])\s*\)|si", |
| 1514 | "url(\\1$secremoveimg\\2)", $content); |
| 1515 | } |
| 1516 | |
| 1517 | /** |
| 1518 | * Fix urls that refer to cid: |
| 1519 | */ |
| 1520 | while (preg_match("|url\s*\(\s*([\'\"]\s*cid:.*?[\'\"])\s*\)|si", |
| 1521 | $content, $matches)){ |
| 1522 | $cidurl = $matches{1}; |
| 1523 | $httpurl = sq_cid2http($message, $id, $cidurl, $mailbox); |
| 1524 | $content = preg_replace("|url\s*\(\s*$cidurl\s*\)|si", |
| 1525 | "url($httpurl)", $content); |
| 1526 | } |
| 1527 | |
| 1528 | /** |
| 1529 | * Fix stupid css declarations which lead to vulnerabilities |
| 1530 | * in IE. |
| 1531 | */ |
| 1532 | $match = Array('/expression/i', |
| 1533 | '/behaviou*r/i', |
| 1534 | '/binding/i', |
| 1535 | '/include-source/i'); |
| 1536 | $replace = Array('idiocy', 'idiocy', 'idiocy', 'idiocy'); |
| 1537 | $content = preg_replace($match, $replace, $content); |
| 1538 | return array($content, $newpos); |
| 1539 | } |
| 1540 | |
| 1541 | /** |
| 1542 | * This function converts cid: url's into the ones that can be viewed in |
| 1543 | * the browser. |
| 1544 | * |
| 1545 | * @param $message the message object |
| 1546 | * @param $id the message id |
| 1547 | * @param $cidurl the cid: url. |
| 1548 | * @param $mailbox the message mailbox |
| 1549 | * @return a string with a http-friendly url |
| 1550 | */ |
| 1551 | function sq_cid2http($message, $id, $cidurl, $mailbox){ |
| 1552 | /** |
| 1553 | * Get rid of quotes. |
| 1554 | */ |
| 1555 | $quotchar = substr($cidurl, 0, 1); |
| 1556 | if ($quotchar == '"' || $quotchar == "'"){ |
| 1557 | $cidurl = str_replace($quotchar, "", $cidurl); |
| 1558 | } else { |
| 1559 | $quotchar = ''; |
| 1560 | } |
| 1561 | $cidurl = substr(trim($cidurl), 4); |
| 1562 | $linkurl = find_ent_id($cidurl, $message); |
| 1563 | /* in case of non-save cid links $httpurl should be replaced by a sort of |
| 1564 | unsave link image */ |
| 1565 | $httpurl = ''; |
| 1566 | if ($linkurl) { |
| 1567 | $httpurl = $quotchar . SM_PATH . 'src/download.php?absolute_dl=true&' . |
| 1568 | "passed_id=$id&mailbox=" . urlencode($mailbox) . |
| 1569 | '&ent_id=' . $linkurl . $quotchar; |
| 1570 | } |
| 1571 | return $httpurl; |
| 1572 | } |
| 1573 | |
| 1574 | /** |
| 1575 | * This function changes the <body> tag into a <div> tag since we |
| 1576 | * can't really have a body-within-body. |
| 1577 | * |
| 1578 | * @param $attary an array of attributes and values of <body> |
| 1579 | * @param $mailbox mailbox we're currently reading (for cid2http) |
| 1580 | * @param $message current message (for cid2http) |
| 1581 | * @param $id current message id (for cid2http) |
| 1582 | * @return a modified array of attributes to be set for <div> |
| 1583 | */ |
| 1584 | function sq_body2div($attary, $mailbox, $message, $id){ |
| 1585 | $me = 'sq_body2div'; |
| 1586 | $divattary = Array('class' => "'bodyclass'"); |
| 1587 | $bgcolor = '#ffffff'; |
| 1588 | $text = '#000000'; |
| 1589 | $has_bgc_stl = $has_txt_stl = false; |
| 1590 | $styledef = ''; |
| 1591 | if (is_array($attary) && sizeof($attary) > 0){ |
| 1592 | foreach ($attary as $attname=>$attvalue){ |
| 1593 | $quotchar = substr($attvalue, 0, 1); |
| 1594 | $attvalue = str_replace($quotchar, "", $attvalue); |
| 1595 | switch ($attname){ |
| 1596 | case 'background': |
| 1597 | $attvalue = sq_cid2http($message, $id, |
| 1598 | $attvalue, $mailbox); |
| 1599 | $styledef .= "background-image: url('$attvalue'); "; |
| 1600 | break; |
| 1601 | case 'bgcolor': |
| 1602 | $has_bgc_stl = true; |
| 1603 | $styledef .= "background-color: $attvalue; "; |
| 1604 | break; |
| 1605 | case 'text': |
| 1606 | $has_txt_stl = true; |
| 1607 | $styledef .= "color: $attvalue; "; |
| 1608 | break; |
| 1609 | } |
| 1610 | } |
| 1611 | // Outlook defines a white bgcolor and no text color. This can lead to |
| 1612 | // white text on a white bg with certain themes. |
| 1613 | if ($has_bgc_stl && !$has_txt_stl) { |
| 1614 | $styledef .= "color: $text; "; |
| 1615 | } |
| 1616 | if (strlen($styledef) > 0){ |
| 1617 | $divattary{"style"} = "\"$styledef\""; |
| 1618 | } |
| 1619 | } |
| 1620 | return $divattary; |
| 1621 | } |
| 1622 | |
| 1623 | /** |
| 1624 | * This is the main function and the one you should actually be calling. |
| 1625 | * There are several variables you should be aware of an which need |
| 1626 | * special description. |
| 1627 | * |
| 1628 | * Since the description is quite lengthy, see it here: |
| 1629 | * http://linux.duke.edu/projects/mini/htmlfilter/ |
| 1630 | * |
| 1631 | * @param $body the string with HTML you wish to filter |
| 1632 | * @param $tag_list see description above |
| 1633 | * @param $rm_tags_with_content see description above |
| 1634 | * @param $self_closing_tags see description above |
| 1635 | * @param $force_tag_closing see description above |
| 1636 | * @param $rm_attnames see description above |
| 1637 | * @param $bad_attvals see description above |
| 1638 | * @param $add_attr_to_tag see description above |
| 1639 | * @param $message message object |
| 1640 | * @param $id message id |
| 1641 | * @return sanitized html safe to show on your pages. |
| 1642 | */ |
| 1643 | function sq_sanitize($body, |
| 1644 | $tag_list, |
| 1645 | $rm_tags_with_content, |
| 1646 | $self_closing_tags, |
| 1647 | $force_tag_closing, |
| 1648 | $rm_attnames, |
| 1649 | $bad_attvals, |
| 1650 | $add_attr_to_tag, |
| 1651 | $message, |
| 1652 | $id, |
| 1653 | $mailbox |
| 1654 | ){ |
| 1655 | $me = 'sq_sanitize'; |
| 1656 | $rm_tags = array_shift($tag_list); |
| 1657 | /** |
| 1658 | * Normalize rm_tags and rm_tags_with_content. |
| 1659 | */ |
| 1660 | @array_walk($tag_list, 'sq_casenormalize'); |
| 1661 | @array_walk($rm_tags_with_content, 'sq_casenormalize'); |
| 1662 | @array_walk($self_closing_tags, 'sq_casenormalize'); |
| 1663 | /** |
| 1664 | * See if tag_list is of tags to remove or tags to allow. |
| 1665 | * false means remove these tags |
| 1666 | * true means allow these tags |
| 1667 | */ |
| 1668 | $curpos = 0; |
| 1669 | $open_tags = Array(); |
| 1670 | $trusted = "\n<!-- begin sanitized html -->\n"; |
| 1671 | $skip_content = false; |
| 1672 | /** |
| 1673 | * Take care of netscape's stupid javascript entities like |
| 1674 | * &{alert('boo')}; |
| 1675 | */ |
| 1676 | $body = preg_replace("/&(\{.*?\};)/si", "&\\1", $body); |
| 1677 | |
| 1678 | while (($curtag = sq_getnxtag($body, $curpos)) != FALSE){ |
| 1679 | list($tagname, $attary, $tagtype, $lt, $gt) = $curtag; |
| 1680 | $free_content = substr($body, $curpos, $lt-$curpos); |
| 1681 | /** |
| 1682 | * Take care of <style> |
| 1683 | */ |
| 1684 | if ($tagname == "style" && $tagtype == 1){ |
| 1685 | list($free_content, $curpos) = |
| 1686 | sq_fixstyle($body, $gt+1, $message, $id, $mailbox); |
| 1687 | if ($free_content != FALSE){ |
| 1688 | $trusted .= sq_tagprint($tagname, $attary, $tagtype); |
| 1689 | $trusted .= $free_content; |
| 1690 | $trusted .= sq_tagprint($tagname, false, 2); |
| 1691 | } |
| 1692 | continue; |
| 1693 | } |
| 1694 | if ($skip_content == false){ |
| 1695 | $trusted .= $free_content; |
| 1696 | } |
| 1697 | if ($tagname != FALSE){ |
| 1698 | if ($tagtype == 2){ |
| 1699 | if ($skip_content == $tagname){ |
| 1700 | /** |
| 1701 | * Got to the end of tag we needed to remove. |
| 1702 | */ |
| 1703 | $tagname = false; |
| 1704 | $skip_content = false; |
| 1705 | } else { |
| 1706 | if ($skip_content == false){ |
| 1707 | if ($tagname == "body"){ |
| 1708 | $tagname = "div"; |
| 1709 | } |
| 1710 | if (isset($open_tags{$tagname}) && |
| 1711 | $open_tags{$tagname} > 0){ |
| 1712 | $open_tags{$tagname}--; |
| 1713 | } else { |
| 1714 | $tagname = false; |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | } else { |
| 1719 | /** |
| 1720 | * $rm_tags_with_content |
| 1721 | */ |
| 1722 | if ($skip_content == false){ |
| 1723 | /** |
| 1724 | * See if this is a self-closing type and change |
| 1725 | * tagtype appropriately. |
| 1726 | */ |
| 1727 | if ($tagtype == 1 |
| 1728 | && in_array($tagname, $self_closing_tags)){ |
| 1729 | $tagtype = 3; |
| 1730 | } |
| 1731 | /** |
| 1732 | * See if we should skip this tag and any content |
| 1733 | * inside it. |
| 1734 | */ |
| 1735 | if ($tagtype == 1 && |
| 1736 | in_array($tagname, $rm_tags_with_content)){ |
| 1737 | $skip_content = $tagname; |
| 1738 | } else { |
| 1739 | if (($rm_tags == false |
| 1740 | && in_array($tagname, $tag_list)) || |
| 1741 | ($rm_tags == true && |
| 1742 | !in_array($tagname, $tag_list))){ |
| 1743 | $tagname = false; |
| 1744 | } else { |
| 1745 | /** |
| 1746 | * Convert body into div. |
| 1747 | */ |
| 1748 | if ($tagname == "body"){ |
| 1749 | $tagname = "div"; |
| 1750 | $attary = sq_body2div($attary, $mailbox, |
| 1751 | $message, $id); |
| 1752 | } |
| 1753 | if ($tagtype == 1){ |
| 1754 | if (isset($open_tags{$tagname})){ |
| 1755 | $open_tags{$tagname}++; |
| 1756 | } else { |
| 1757 | $open_tags{$tagname}=1; |
| 1758 | } |
| 1759 | } |
| 1760 | /** |
| 1761 | * This is where we run other checks. |
| 1762 | */ |
| 1763 | if (is_array($attary) && sizeof($attary) > 0){ |
| 1764 | $attary = sq_fixatts($tagname, |
| 1765 | $attary, |
| 1766 | $rm_attnames, |
| 1767 | $bad_attvals, |
| 1768 | $add_attr_to_tag, |
| 1769 | $message, |
| 1770 | $id, |
| 1771 | $mailbox |
| 1772 | ); |
| 1773 | } |
| 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | } |
| 1778 | if ($tagname != false && $skip_content == false){ |
| 1779 | $trusted .= sq_tagprint($tagname, $attary, $tagtype); |
| 1780 | } |
| 1781 | } |
| 1782 | $curpos = $gt+1; |
| 1783 | } |
| 1784 | $trusted .= substr($body, $curpos, strlen($body)-$curpos); |
| 1785 | if ($force_tag_closing == true){ |
| 1786 | foreach ($open_tags as $tagname=>$opentimes){ |
| 1787 | while ($opentimes > 0){ |
| 1788 | $trusted .= '</' . $tagname . '>'; |
| 1789 | $opentimes--; |
| 1790 | } |
| 1791 | } |
| 1792 | $trusted .= "\n"; |
| 1793 | } |
| 1794 | $trusted .= "<!-- end sanitized html -->\n"; |
| 1795 | return $trusted; |
| 1796 | } |
| 1797 | |
| 1798 | /** |
| 1799 | * This is a wrapper function to call html sanitizing routines. |
| 1800 | * |
| 1801 | * @param $body the body of the message |
| 1802 | * @param $id the id of the message |
| 1803 | * @return a string with html safe to display in the browser. |
| 1804 | */ |
| 1805 | function magicHTML($body, $id, $message, $mailbox = 'INBOX') { |
| 1806 | global $attachment_common_show_images, $view_unsafe_images, |
| 1807 | $has_unsafe_images; |
| 1808 | /** |
| 1809 | * Don't display attached images in HTML mode. |
| 1810 | */ |
| 1811 | $attachment_common_show_images = false; |
| 1812 | $tag_list = Array( |
| 1813 | false, |
| 1814 | "object", |
| 1815 | "meta", |
| 1816 | "html", |
| 1817 | "head", |
| 1818 | "base", |
| 1819 | "link", |
| 1820 | "frame", |
| 1821 | "iframe", |
| 1822 | "plaintext", |
| 1823 | "marquee" |
| 1824 | ); |
| 1825 | |
| 1826 | $rm_tags_with_content = Array( |
| 1827 | "script", |
| 1828 | "applet", |
| 1829 | "embed", |
| 1830 | "title", |
| 1831 | "frameset", |
| 1832 | "xml" |
| 1833 | ); |
| 1834 | |
| 1835 | $self_closing_tags = Array( |
| 1836 | "img", |
| 1837 | "br", |
| 1838 | "hr", |
| 1839 | "input" |
| 1840 | ); |
| 1841 | |
| 1842 | $force_tag_closing = true; |
| 1843 | |
| 1844 | $rm_attnames = Array( |
| 1845 | "/.*/" => |
| 1846 | Array( |
| 1847 | "/target/i", |
| 1848 | "/^on.*/i", |
| 1849 | "/^dynsrc/i", |
| 1850 | "/^data.*/i", |
| 1851 | "/^lowsrc.*/i" |
| 1852 | ) |
| 1853 | ); |
| 1854 | |
| 1855 | $secremoveimg = "../images/" . _("sec_remove_eng.png"); |
| 1856 | $bad_attvals = Array( |
| 1857 | "/.*/" => |
| 1858 | Array( |
| 1859 | "/^src|background/i" => |
| 1860 | Array( |
| 1861 | Array( |
| 1862 | "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si", |
| 1863 | "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si", |
| 1864 | "/^([\'\"])\s*about\s*:.*([\'\"])/si" |
| 1865 | ), |
| 1866 | Array( |
| 1867 | "\\1$secremoveimg\\2", |
| 1868 | "\\1$secremoveimg\\2", |
| 1869 | "\\1$secremoveimg\\2", |
| 1870 | "\\1$secremoveimg\\2" |
| 1871 | ) |
| 1872 | ), |
| 1873 | "/^href|action/i" => |
| 1874 | Array( |
| 1875 | Array( |
| 1876 | "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si", |
| 1877 | "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si", |
| 1878 | "/^([\'\"])\s*about\s*:.*([\'\"])/si" |
| 1879 | ), |
| 1880 | Array( |
| 1881 | "\\1#\\1", |
| 1882 | "\\1#\\1", |
| 1883 | "\\1#\\1", |
| 1884 | "\\1#\\1" |
| 1885 | ) |
| 1886 | ), |
| 1887 | "/^style/i" => |
| 1888 | Array( |
| 1889 | Array( |
| 1890 | "/expression/i", |
| 1891 | "/binding/i", |
| 1892 | "/behaviou*r/i", |
| 1893 | "/include-source/i", |
| 1894 | "/url\s*\(\s*([\'\"])\s*\S+script\s*:.*([\'\"])\s*\)/si", |
| 1895 | "/url\s*\(\s*([\'\"])\s*mocha\s*:.*([\'\"])\s*\)/si", |
| 1896 | "/url\s*\(\s*([\'\"])\s*about\s*:.*([\'\"])\s*\)/si", |
| 1897 | "/(.*)\s*:\s*url\s*\(\s*([\'\"]*)\s*\S+script\s*:.*([\'\"]*)\s*\)/si" |
| 1898 | ), |
| 1899 | Array( |
| 1900 | "idiocy", |
| 1901 | "idiocy", |
| 1902 | "idiocy", |
| 1903 | "idiocy", |
| 1904 | "url(\\1#\\1)", |
| 1905 | "url(\\1#\\1)", |
| 1906 | "url(\\1#\\1)", |
| 1907 | "url(\\1#\\1)", |
| 1908 | "\\1:url(\\2#\\3)" |
| 1909 | ) |
| 1910 | ) |
| 1911 | ) |
| 1912 | ); |
| 1913 | if( !sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET) ) { |
| 1914 | $view_unsafe_images = false; |
| 1915 | } |
| 1916 | if (!$view_unsafe_images){ |
| 1917 | /** |
| 1918 | * Remove any references to http/https if view_unsafe_images set |
| 1919 | * to false. |
| 1920 | */ |
| 1921 | array_push($bad_attvals{'/.*/'}{'/^src|background/i'}[0], |
| 1922 | '/^([\'\"])\s*https*:.*([\'\"])/si'); |
| 1923 | array_push($bad_attvals{'/.*/'}{'/^src|background/i'}[1], |
| 1924 | "\\1$secremoveimg\\1"); |
| 1925 | array_push($bad_attvals{'/.*/'}{'/^style/i'}[0], |
| 1926 | '/url\(([\'\"])\s*https*:.*([\'\"])\)/si'); |
| 1927 | array_push($bad_attvals{'/.*/'}{'/^style/i'}[1], |
| 1928 | "url(\\1$secremoveimg\\1)"); |
| 1929 | } |
| 1930 | |
| 1931 | $add_attr_to_tag = Array( |
| 1932 | "/^a$/i" => |
| 1933 | Array('target'=>'"_new"', |
| 1934 | 'title'=>'"'._("This external link will open in a new window").'"' |
| 1935 | ) |
| 1936 | ); |
| 1937 | $trusted = sq_sanitize($body, |
| 1938 | $tag_list, |
| 1939 | $rm_tags_with_content, |
| 1940 | $self_closing_tags, |
| 1941 | $force_tag_closing, |
| 1942 | $rm_attnames, |
| 1943 | $bad_attvals, |
| 1944 | $add_attr_to_tag, |
| 1945 | $message, |
| 1946 | $id, |
| 1947 | $mailbox |
| 1948 | ); |
| 1949 | if (preg_match("|$secremoveimg|i", $trusted)){ |
| 1950 | $has_unsafe_images = true; |
| 1951 | } |
| 1952 | return $trusted; |
| 1953 | } |
| 1954 | |
| 1955 | /** |
| 1956 | * function SendDownloadHeaders - send file to the browser |
| 1957 | * |
| 1958 | * Original Source: SM core src/download.php |
| 1959 | * moved here to make it available to other code, and separate |
| 1960 | * front end from back end functionality. |
| 1961 | * |
| 1962 | * @param string $type0 first half of mime type |
| 1963 | * @param string $type1 second half of mime type |
| 1964 | * @param string $filename filename to tell the browser for downloaded file |
| 1965 | * @param boolean $force whether to force the download dialog to pop |
| 1966 | * @param optional integer $filesize send the Content-Header and length to the browser |
| 1967 | * @return void |
| 1968 | */ |
| 1969 | function SendDownloadHeaders($type0, $type1, $filename, $force, $filesize=0) { |
| 1970 | global $languages, $squirrelmail_language; |
| 1971 | $isIE = $isIE6 = 0; |
| 1972 | |
| 1973 | sqgetGlobalVar('HTTP_USER_AGENT', $HTTP_USER_AGENT, SQ_SERVER); |
| 1974 | |
| 1975 | if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false && |
| 1976 | strstr($HTTP_USER_AGENT, 'Opera') === false) { |
| 1977 | $isIE = 1; |
| 1978 | } |
| 1979 | |
| 1980 | if (strstr($HTTP_USER_AGENT, 'compatible; MSIE 6') !== false && |
| 1981 | strstr($HTTP_USER_AGENT, 'Opera') === false) { |
| 1982 | $isIE6 = 1; |
| 1983 | } |
| 1984 | |
| 1985 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 1986 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
| 1987 | $filename = |
| 1988 | $languages[$squirrelmail_language]['XTRA_CODE']('downloadfilename', $filename, $HTTP_USER_AGENT); |
| 1989 | } else { |
| 1990 | $filename = ereg_replace('[\\/:\*\?"<>\|;]', '_', str_replace(' ', ' ', $filename)); |
| 1991 | } |
| 1992 | |
| 1993 | // A Pox on Microsoft and it's Internet Explorer! |
| 1994 | // |
| 1995 | // IE has lots of bugs with file downloads. |
| 1996 | // It also has problems with SSL. Both of these cause problems |
| 1997 | // for us in this function. |
| 1998 | // |
| 1999 | // See this article on Cache Control headers and SSL |
| 2000 | // http://support.microsoft.com/default.aspx?scid=kb;en-us;323308 |
| 2001 | // |
| 2002 | // The best thing you can do for IE is to upgrade to the latest |
| 2003 | // version |
| 2004 | //set all the Cache Control Headers for IE |
| 2005 | if ($isIE) { |
| 2006 | header ("Pragma: public"); |
| 2007 | header ("Cache-Control: no-store, max-age=0, no-cache, must-revalidate"); # HTTP/1.1 |
| 2008 | header ("Cache-Control: post-check=0, pre-check=0", false); |
| 2009 | header ("Cache-control: private"); |
| 2010 | |
| 2011 | //set the inline header for IE, we'll add the attachment header later if we need it |
| 2012 | header ("Content-Disposition: inline; filename=$filename"); |
| 2013 | } |
| 2014 | |
| 2015 | if (!$force) { |
| 2016 | // Try to show in browser window |
| 2017 | header ("Content-Disposition: inline; filename=\"$filename\""); |
| 2018 | header ("Content-Type: $type0/$type1; name=\"$filename\""); |
| 2019 | } else { |
| 2020 | // Try to pop up the "save as" box |
| 2021 | |
| 2022 | // IE makes this hard. It pops up 2 save boxes, or none. |
| 2023 | // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP |
| 2024 | // http://support.microsoft.com/default.aspx?scid=kb;EN-US;260519 |
| 2025 | // But, according to Microsoft, it is "RFC compliant but doesn't |
| 2026 | // take into account some deviations that allowed within the |
| 2027 | // specification." Doesn't that mean RFC non-compliant? |
| 2028 | // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP |
| 2029 | |
| 2030 | // all browsers need the application/octet-stream header for this |
| 2031 | header ("Content-Type: application/octet-stream; name=\"$filename\""); |
| 2032 | |
| 2033 | // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp |
| 2034 | // Do not have quotes around filename, but that applied to |
| 2035 | // "attachment"... does it apply to inline too? |
| 2036 | header ("Content-Disposition: attachment; filename=\"$filename\""); |
| 2037 | |
| 2038 | if ($isIE && !$isIE6) { |
| 2039 | // This combination seems to work mostly. IE 5.5 SP 1 has |
| 2040 | // known issues (see the Microsoft Knowledge Base) |
| 2041 | |
| 2042 | // This works for most types, but doesn't work with Word files |
| 2043 | header ("Content-Type: application/download; name=\"$filename\""); |
| 2044 | |
| 2045 | // These are spares, just in case. :-) |
| 2046 | //header("Content-Type: $type0/$type1; name=\"$filename\""); |
| 2047 | //header("Content-Type: application/x-msdownload; name=\"$filename\""); |
| 2048 | //header("Content-Type: application/octet-stream; name=\"$filename\""); |
| 2049 | } else { |
| 2050 | // another application/octet-stream forces download for Netscape |
| 2051 | header ("Content-Type: application/octet-stream; name=\"$filename\""); |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | //send the content-length header if the calling function provides it |
| 2056 | if ($filesize > 0) { |
| 2057 | header("Content-Length: $filesize"); |
| 2058 | } |
| 2059 | |
| 2060 | } // end fn SendDownloadHeaders |
| 2061 | |
| 2062 | ?> |