fsf changes, meant to be rebased on upstream
[squirrelmail.git] / functions / mime.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
8bd0068d 4 * mime.php
5 *
8bd0068d 6 * This contains the functions necessary to detect and decode MIME
7 * messages.
8 *
77a1e3d1 9 * @copyright 1999-2022 The SquirrelMail Project Team
4b4abf93 10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8bd0068d 11 * @version $Id$
12 * @package squirrelmail
13 */
b74ba498 14
202bcbcc 15/**
16 * dependency information
17 functions dependency
18 mime_structure
19 class/mime/Message.class.php
20 Message::parseStructure
21 functions/page_header.php
22 displayPageHeader
23 functions/display_messages.php
24 plain_error_message
25 mime_fetch_body
26 functions/imap_general.php
27 sqimap_run_command
28 mime_print_body_lines
29
30
31
32functions/imap.php
33functions/attachment_common.php
34functions/display_messages.php
35
36magicHtml => url_parser
37translateText => url_parser
38
39*/
40
8beafbbc 41
7c7b74b3 42/* -------------------------------------------------------------------------- */
43/* MIME DECODING */
44/* -------------------------------------------------------------------------- */
b74ba498 45
d6c32258 46/**
8bd0068d 47 * Get the MIME structure
48 *
49 * This function gets the structure of a message and stores it in the "message" class.
50 * It will return this object for use with all relevant header information and
51 * fully parsed into the standard "message" object format.
52 */
a4a70693 53function mime_structure ($bodystructure, $flags=array()) {
c9d78ab4 54
3d8371be 55 /* Isolate the body structure and remove beginning and end parenthesis. */
a4a70693 56 $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') + 13));
451f74a2 57 $read = trim(substr ($read, 0, -1));
22efa9fb 58 $i = 0;
59 $msg = Message::parseStructure($read,$i);
2b665f28 60
9de42168 61 if (!is_object($msg)) {
3d8371be 62 global $color, $mailbox;
a48eba8f 63 displayPageHeader( $color, $mailbox );
5e8de8b6 64 $errormessage = _("SquirrelMail could not decode the bodystructure of the message");
8bd0068d 65 $errormessage .= '<br />'._("The bodystructure provided by your IMAP server:").'<br /><br />';
3047e291 66 $errormessage .= '<pre>' . sm_encode_html_special_chars($read) . '</pre>';
ce8c6f42 67 plain_error_message( $errormessage );
3d8371be 68 echo '</body></html>';
9de42168 69 exit;
70 }
a4a70693 71 if (count($flags)) {
7a9e9c89 72 foreach ($flags as $flag) {
e1115979 73//FIXME: please document why it is we have to check the first char of the flag but we then go ahead and do a full string comparison anyway. Is this a speed enhancement? If not, let's keep it simple and just compare the full string and forget the switch block.
1710ad65 74 $char = strtoupper($flag[1]);
7a9e9c89 75 switch ($char) {
3d8371be 76 case 'S':
77 if (strtolower($flag) == '\\seen') {
78 $msg->is_seen = true;
79 }
80 break;
81 case 'A':
82 if (strtolower($flag) == '\\answered') {
83 $msg->is_answered = true;
84 }
85 break;
86 case 'D':
87 if (strtolower($flag) == '\\deleted') {
88 $msg->is_deleted = true;
89 }
90 break;
91 case 'F':
92 if (strtolower($flag) == '\\flagged') {
93 $msg->is_flagged = true;
94 }
1a753239 95 else if (strtolower($flag) == '$forwarded') {
96 $msg->is_forwarded = true;
97 }
3d8371be 98 break;
99 case 'M':
100 if (strtolower($flag) == '$mdnsent') {
101 $msg->is_mdnsent = true;
102 }
103 break;
104 default:
105 break;
7a9e9c89 106 }
107 }
451f74a2 108 }
7a9e9c89 109 // listEntities($msg);
3d8371be 110 return $msg;
451f74a2 111}
b74ba498 112
22efa9fb 113
114
3d8371be 115/* This starts the parsing of a particular structure. It is called recursively,
8bd0068d 116 * so it can be passed different structures. It returns an object of type
117 * $message.
118 * First, it checks to see if it is a multipart message. If it is, then it
119 * handles that as it sees is necessary. If it is just a regular entity,
120 * then it parses it and adds the necessary header information (by calling out
121 * to mime_get_elements()
122 */
451f74a2 123
4d592352 124function mime_fetch_body($imap_stream, $id, $ent_id=1, $fetch_size=0) {
3d8371be 125 /* Do a bit of error correction. If we couldn't find the entity id, just guess
8bd0068d 126 * that it is the first one. That is usually the case anyway.
127 */
7c7b74b3 128
09a4bde3 129 if (!$ent_id) {
08b7f7cc 130 $cmd = "FETCH $id BODY[]";
1035e159 131 } else {
08b7f7cc 132 $cmd = "FETCH $id BODY[$ent_id]";
09a4bde3 133 }
3d8371be 134
4d592352 135 if ($fetch_size!=0) $cmd .= "<0.$fetch_size>";
da2415c1 136
6201339c 137 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, TRUE);
77b88425 138 do {
3d8371be 139 $topline = trim(array_shift($data));
8439f61d 140 } while($topline && ($topline[0] == '*') && !preg_match('/\* [0-9]+ FETCH .*BODY.*/i', $topline)) ;
141 // Matching with "BODY" above is difficult: in most cases "FETCH \(BODY" would work
142 // but some servers may put other things in the same result, perhaps something such
143 // as "* 23 FETCH (FLAGS (\Seen) BODY[1] {174}". There is some small chance that
144 // if the character sequence "BODY" appears in a response where it isn't actually
145 // a FETCH response data item name, the current regex will break things. The better
146 // way to do this would be to parse the response correctly and not use a regex.
a4a70693 147
451f74a2 148 $wholemessage = implode('', $data);
b7910e12 149 if (preg_match('/\{([^\}]*)\}/', $topline, $regs)) {
3d8371be 150 $ret = substr($wholemessage, 0, $regs[1]);
151 /* There is some information in the content info header that could be important
8bd0068d 152 * in order to parse html messages. Let's get them here.
153 */
1710ad65 154// if ($ret[0] == '<') {
6201339c 155// $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, TRUE);
0600bdf1 156// }
b7910e12 157 } else if (preg_match('/"([^"]*)"/', $topline, $regs)) {
451f74a2 158 $ret = $regs[1];
06bcb9c3 159 } else if ((stristr($topline, 'nil') !== false) && (empty($wholemessage))) {
160 $ret = $wholemessage;
451f74a2 161 } else {
162 global $where, $what, $mailbox, $passed_id, $startMessage;
3d8371be 163 $par = 'mailbox=' . urlencode($mailbox) . '&amp;passed_id=' . $passed_id;
451f74a2 164 if (isset($where) && isset($what)) {
3d8371be 165 $par .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
a3daaaf3 166 } else {
3d8371be 167 $par .= '&amp;startMessage=' . $startMessage . '&amp;show_more=0';
451f74a2 168 }
e5ea9327 169 $par .= '&amp;response=' . urlencode($response) .
8bd0068d 170 '&amp;message=' . urlencode($message) .
171 '&amp;topline=' . urlencode($topline);
a019eeb8 172
8bd0068d 173 echo '<tt><br />' .
02474e43 174 '<table width="80%"><tr>' .
175 '<tr><td colspan="2">' .
176 _("Body retrieval error. The reason for this is most probably that the message is malformed.") .
177 '</td></tr>' .
178 '<tr><td><b>' . _("Command:") . "</td><td>$cmd</td></tr>" .
179 '<tr><td><b>' . _("Response:") . "</td><td>$response</td></tr>" .
180 '<tr><td><b>' . _("Message:") . "</td><td>$message</td></tr>" .
181 '<tr><td><b>' . _("FETCH line:") . "</td><td>$topline</td></tr>" .
182 "</table><br /></tt></font><hr />";
346817d4 183
6201339c 184 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, TRUE);
451f74a2 185 array_shift($data);
186 $wholemessage = implode('', $data);
a019eeb8 187
346817d4 188 $ret = $wholemessage;
a3daaaf3 189 }
3d8371be 190 return $ret;
451f74a2 191}
d4467150 192
c53195bb 193// TODO: Needs documentation. $ent_id default is usually 1
194function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding, $rStream='php://stdout', $force_crlf='') {
1035e159 195
3d8371be 196 /* Don't kill the connection if the browser is over a dialup
8bd0068d 197 * and it would take over 30 seconds to download it.
198 * Don't call set_time_limit in safe mode.
199 */
b7206e1d 200
3d8371be 201 if (!ini_get('safe_mode')) {
b7206e1d 202 set_time_limit(0);
203 }
7c7b74b3 204 /* in case of base64 encoded attachments, do not buffer them.
8bd0068d 205 Instead, echo the decoded attachment directly to screen */
7c7b74b3 206 if (strtolower($encoding) == 'base64') {
207 if (!$ent_id) {
7591f143 208 $query = "FETCH $id BODY[]";
7c7b74b3 209 } else {
7591f143 210 $query = "FETCH $id BODY[$ent_id]";
7c7b74b3 211 }
7591f143 212 sqimap_run_command($imap_stream,$query,true,$response,$message,TRUE,'sqimap_base64_decode',$rStream,true);
1d142b8d 213 } else {
7591f143 214 $body = mime_fetch_body ($imap_stream, $id, $ent_id);
215 if (is_resource($rStream)) {
fa26ab45 216 fputs($rStream,decodeBody($body, $encoding, $force_crlf));
7591f143 217 } else {
fa26ab45 218 echo decodeBody($body, $encoding, $force_crlf);
7591f143 219 }
1d142b8d 220 }
346817d4 221
da2415c1 222 /*
8bd0068d 223 TODO, use the same method for quoted printable.
224 However, I assume that quoted printable attachments aren't that large
225 so the performancegain / memory usage drop will be minimal.
226 If we decide to add that then we need to adapt sqimap_fread because
227 we need to split te result on \n and fread doesn't stop at \n. That
228 means we also should provide $results from sqimap_fread (by ref) to
229 te function and set $no_return to false. The $filter function for
230 quoted printable should handle unsetting of $results.
231 */
da2415c1 232 /*
8bd0068d 233 TODO 2: find out how we write to the output stream php://stdout. fwrite
234 doesn't work because 'php://stdout isn't a stream.
235 */
7c7b74b3 236
5d9c6f73 237 return;
451f74a2 238}
beb9e459 239
451f74a2 240/* -[ END MIME DECODING ]----------------------------------------------------------- */
d4467150 241
3d8371be 242/* This is here for debugging purposes. It will print out a list
8bd0068d 243 * of all the entity IDs that are in the $message object.
244 */
451f74a2 245function listEntities ($message) {
3d8371be 246 if ($message) {
3c621ba1 247 echo "<tt>" . $message->entity_id . ' : ' . $message->type0 . '/' . $message->type1 . ' parent = '. $message->parent->entity_id. '<br />';
3d8371be 248 for ($i = 0; isset($message->entities[$i]); $i++) {
249 echo "$i : ";
250 $msg = listEntities($message->entities[$i]);
251
252 if ($msg) {
253 echo "return: ";
254 return $msg;
255 }
256 }
a4a70693 257 }
451f74a2 258}
f0c4dc12 259
f792c641 260function getPriorityStr($priority) {
3d8371be 261 $priority_level = substr($priority,0,1);
262
263 switch($priority_level) {
264 /* Check for a higher then normal priority. */
265 case '1':
266 case '2':
267 $priority_string = _("High");
268 break;
269
270 /* Check for a lower then normal priority. */
271 case '4':
272 case '5':
273 $priority_string = _("Low");
274 break;
275
276 /* Check for a normal priority. */
277 case '3':
278 default:
279 $priority_level = '3';
280 $priority_string = _("Normal");
281 break;
282
283 }
284 return $priority_string;
f792c641 285}
286
451f74a2 287/* returns a $message object for a particular entity id */
288function getEntity ($message, $ent_id) {
a4a70693 289 return $message->getEntity($ent_id);
451f74a2 290}
8beafbbc 291
3d8371be 292/* translateText
8bd0068d 293 * Extracted from strings.php 23/03/2002
294 */
da4c66e8 295
296function translateText(&$body, $wrap_at, $charset) {
3d8371be 297 global $where, $what; /* from searching */
298 global $color; /* color theme */
da4c66e8 299
202bcbcc 300 // require_once(SM_PATH . 'functions/url_parser.php');
da4c66e8 301
302 $body_ary = explode("\n", $body);
da4c66e8 303 for ($i=0; $i < count($body_ary); $i++) {
4c1f4be3 304 $line = rtrim($body_ary[$i],"\r");
305
da4c66e8 306 if (strlen($line) - 2 >= $wrap_at) {
c7aff938 307 sqWordWrap($line, $wrap_at, $charset);
da4c66e8 308 }
309 $line = charset_decode($charset, $line);
310 $line = str_replace("\t", ' ', $line);
311
312 parseUrl ($line);
313
3d8371be 314 $quotes = 0;
da4c66e8 315 $pos = 0;
3d8371be 316 $j = strlen($line);
da4c66e8 317
3d8371be 318 while ($pos < $j) {
da4c66e8 319 if ($line[$pos] == ' ') {
3d8371be 320 $pos++;
da4c66e8 321 } else if (strpos($line, '&gt;', $pos) === $pos) {
322 $pos += 4;
3d8371be 323 $quotes++;
da4c66e8 324 } else {
325 break;
326 }
327 }
3d8371be 328
83c94382 329 if ($quotes % 2) {
d0814c02 330 $line = '<span class="quote1">' . $line . '</span>';
4c25967c 331 } elseif ($quotes) {
d0814c02 332 $line = '<span class="quote2">' . $line . '</span>';
da4c66e8 333 }
3d8371be 334
da4c66e8 335 $body_ary[$i] = $line;
336 }
337 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
338}
339
a2bfcbce 340/**
da1b55ad 341 * This returns a parsed string called $body. That string can then
342 * be displayed as the actual message in the HTML. It contains
343 * everything needed, including HTML Tags, Attachments at the
344 * bottom, etc.
f8a1ed5a 345 *
da1b55ad 346 * Since 1.2.0 function uses message_body hook.
347 * Till 1.3.0 function included output of formatAttachments().
348 *
349 * @param resource $imap_stream imap connection resource
350 * @param object $message squirrelmail message object
351 * @param array $color squirrelmail color theme array
352 * @param integer $wrap_at number of characters per line
353 * @param string $ent_num (since 1.3.0) message part id
354 * @param integer $id (since 1.3.0) message id
355 * @param string $mailbox (since 1.3.0) imap folder name
da1b55ad 356 * @return string html formated message text
357 */
a540f994 358function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $mailbox='INBOX') {
3d8371be 359 /* This if statement checks for the entity to show as the
8bd0068d 360 * primary message. To add more of them, just put them in the
361 * order that is their priority.
362 */
ce68b76b 363 global $startMessage, $languages, $squirrelmail_language,
40a34e57 364 $show_html_default, $sort, $has_unsafe_images, $passed_ent_id,
14c85e39 365 $use_iframe, $iframe_height, $download_and_unsafe_link,
955bfc8f 366 $download_href, $unsafe_image_toggle_href, $unsafe_image_toggle_text,
551c7b53 367 $oTemplate, $nbsp;
2c25d36a 368
369 // workaround for not updated config.php
370 if (! isset($use_iframe)) $use_iframe = false;
77bfbd2e 371
d31f73f1 372 // If there's no "view_unsafe_images" variable in the URL, turn unsafe
373 // images off by default.
afbf184c 374 sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET, FALSE);
d03c24f4 375
cc34b00d 376 $body = '';
23bcec6f 377 $urlmailbox = urlencode($mailbox);
451f74a2 378 $body_message = getEntity($message, $ent_num);
379 if (($body_message->header->type0 == 'text') ||
8bd0068d 380 ($body_message->header->type0 == 'rfc822')) {
3d8371be 381 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
451f74a2 382 $body = decodeBody($body, $body_message->header->encoding);
e842b215 383
384 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
8bd0068d 385 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
e842b215 386 if (mb_detect_encoding($body) != 'ASCII') {
33a55f5a 387 $body = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode',$body);
e842b215 388 }
389 }
d849b570 390
391 /* As of 1.5.2, $body is passed (and modified) by reference */
392 do_hook('message_body', $body);
23bcec6f 393
3d8371be 394 /* If there are other types that shouldn't be formatted, add
8bd0068d 395 * them here.
396 */
3d8371be 397
451f74a2 398 if ($body_message->header->type1 == 'html') {
c7580d55 399 // Do we need to make an HTML part viewable as non-HTML plain text?
400 if ($show_html_default != 1) {
85015544 401 $entity_conv = array('&nbsp;' => ' ',
c7580d55 402 // These are better done by regex (below)
403 // '<p>' => "\n",
404 // '<P>' => "\n",
405 // '<br>' => "\n",
406 // '<BR>' => "\n",
407 // '<br />' => "\n",
408 // '<BR />' => "\n",
409 // '<tr>' => "\n",
410 // '<div>' => "\n",
8bd0068d 411 '&gt;' => '>',
c7580d55 412 '&lt;' => '<',
413 '&amp;' => '&',
414 '&copy;' => '©');
415 // first, completely remove <style> tags as they aren't useful in this context
416 $body = preg_replace('/<style.*>.*<\/style.*>/isU', '', $body);
417 // emulate how newlines are treated as spaces in HTML
418 $body = preg_replace('/(\r|\n)+/', ' ', $body);
419 // now replace the tags listed just above
85015544 420 $body = strtr($body, $entity_conv);
c7580d55 421 // <p>, <br>, <tr> and <div> are best replaced by a newline
422 $body = preg_replace('/<(p|br|tr|div).*>/isU', "\n", $body);
423 // remove the rest of the HTML tags
3d8371be 424 $body = strip_tags($body);
c7580d55 425 // condense multiple spaces into one
426 $body = preg_replace('/[ \t]+/', ' ', $body);
427 // trim each line
428 $body = preg_replace('/ *\n */', "\n", $body);
429 // allow maximum two newlines
430 $body = preg_replace('/\n\n\n+/', "\n\n", $body);
85015544 431 $body = trim($body);
432 translateText($body, $wrap_at,
8bd0068d 433 $body_message->header->getParameter('charset'));
a540f994 434 } elseif ($use_iframe) {
84410f31 435 /**
436 * If we don't add html message between iframe tags,
437 * we must detect unsafe images and modify $has_unsafe_images.
f8a1ed5a 438 */
758a7889 439 $html_body = magicHTML($body, $id, $message, $mailbox);
b6c52e61 440 // Convert character set in order to display html mails in different character set
441 $html_body = charset_decode($body_message->header->getParameter('charset'),$html_body,false,true);
84410f31 442
2c25d36a 443 // creating iframe url
444 $iframeurl=sqm_baseuri().'src/view_html.php?'
f8a1ed5a 445 . 'mailbox=' . $urlmailbox
2c25d36a 446 . '&amp;passed_id=' . $id
447 . '&amp;ent_id=' . $ent_num
448 . '&amp;view_unsafe_images=' . (int) $view_unsafe_images;
449
79d58d4c 450 global $oTemplate;
451 $oTemplate->assign('iframe_url', $iframeurl);
51554708 452 $oTemplate->assign('iframe_height', $iframe_height);
79d58d4c 453 $oTemplate->assign('html_body', $html_body);
2b665f28 454
79d58d4c 455 $body = $oTemplate->fetch('read_html_iframe.tpl');
a3daaaf3 456 } else {
2c25d36a 457 // old way of html rendering
b6c52e61 458 /**
758a7889 459 * convert character set. charset_decode does not remove html special chars
b6c52e61 460 * applied by magicHTML functions and does not sanitize them second time if
758a7889 461 * fourth argument is true.
462 */
567dc524 463 $charset = $body_message->header->getParameter('charset');
464 if (!empty($charset)) {
465 $body = charset_decode($charset,$body,false,true);
466 }
467 $body = magicHTML($body, $id, $message, $mailbox);
a3daaaf3 468 }
451f74a2 469 } else {
3d8371be 470 translateText($body, $wrap_at,
8bd0068d 471 $body_message->header->getParameter('charset'));
451f74a2 472 }
a2bfcbce 473
9ebace19 474 /*
475 * Previously the links for downloading and unsafe images were printed
476 * under the mail. By putting the links in a global variable we can
477 * print it in the toolbar where it belongs. Since the original code was
478 * in this place it's left here. It might be possible to move it to some
479 * other place if that makes sense. The possibility to do so has not
480 * been evaluated yet.
481 */
482
483 // Initialize the global variable to an empty string.
484 // FIXME: To have $download_and_unsafe_link as a global variable might not be needed since the use of separate variables ($download_href, $unsafe_image_toggle_href, and $unsafe_image_toggle_text) for the templates was introduced.
40a34e57 485 $download_and_unsafe_link = '';
486
9ebace19 487 // Prepare and build a link for downloading the mail.
83cf04bd 488 $link = 'passed_id=' . $id . '&amp;ent_id='.$ent_num.
8bd0068d 489 '&amp;mailbox=' . $urlmailbox .'&amp;sort=' . $sort .
490 '&amp;startMessage=' . $startMessage . '&amp;show_more=0';
08b7f7cc 491 if (isset($passed_ent_id)) {
492 $link .= '&amp;passed_ent_id='.$passed_ent_id;
493 }
14c85e39 494 $download_href = SM_PATH . 'src/download.php?absolute_dl=true&amp;' . $link;
9ebace19 495
496 // Always add the link for downloading the mail as a file to the global
497 // variable.
955bfc8f 498 $download_and_unsafe_link .= "$nbsp|$nbsp"
499 . create_hyperlink($download_href, _("Download this as a file"));
9ebace19 500
501 // Find out the right text to use in the link depending on the
502 // circumstances. If the unsafe images are displayed the link should
503 // hide them, if they aren't displayed the link should only appear if
504 // the mail really contains unsafe images.
7aad7b77 505 if ($view_unsafe_images) {
23f617b8 506 $text = _("Hide Unsafe Images");
7aad7b77 507 } else {
08b7f7cc 508 if (isset($has_unsafe_images) && $has_unsafe_images) {
509 $link .= '&amp;view_unsafe_images=1';
510 $text = _("View Unsafe Images");
511 } else {
512 $text = '';
513 }
3d8371be 514 }
9ebace19 515
516 // Only create a link for unsafe images if there's need for one. If so:
517 // add it to the global variable.
83cf04bd 518 if($text != '') {
14c85e39 519 $unsafe_image_toggle_href = SM_PATH . 'src/read_body.php?'.$link;
520 $unsafe_image_toggle_text = $text;
955bfc8f 521 $download_and_unsafe_link .= "$nbsp|$nbsp"
522 . create_hyperlink($unsafe_image_toggle_href, $text);
83cf04bd 523 }
3d8371be 524 }
525 return $body;
451f74a2 526}
b74ba498 527
da1b55ad 528/**
a540f994 529 * Generate attachments array for passing to templates.
2b665f28 530 *
d67f519a 531 * @since 1.5.2
da1b55ad 532 * @param object $message SquirrelMail message object
533 * @param array $exclude_id message parts that are not attachments.
534 * @param string $mailbox mailbox name
535 * @param integer $id message id
da1b55ad 536 */
d67f519a 537function buildAttachmentArray($message, $exclude_id, $mailbox, $id) {
caf0ab1d 538 global $where, $what, $startMessage, $color, $passed_ent_id,
539 $base_uri, $block_svg_download;
451f74a2 540
23bcec6f 541 $att_ar = $message->getAttachments($exclude_id);
23bcec6f 542 $urlMailbox = urlencode($mailbox);
543
d67f519a 544 $attachments = array();
23bcec6f 545 foreach ($att_ar as $att) {
fdc9d9b5 546 $ent = $att->entity_id;
2e25760a 547 $header = $att->header;
f0c4dc12 548 $type0 = strtolower($header->type0);
549 $type1 = strtolower($header->type1);
caf0ab1d 550 if ($block_svg_download && strpos($type1, 'svg') === 0)
551 continue;
552
2e25760a 553 $name = '';
d0187bd6 554 $links = array();
21dab2dc 555 $links['download link']['text'] = _("Download");
202bcbcc 556 $links['download link']['href'] = $base_uri .
8bd0068d 557 "src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;ent_id=$ent";
2b665f28 558
2e25760a 559 if ($type0 =='message' && $type1 == 'rfc822') {
202bcbcc 560 $default_page = $base_uri . 'src/read_body.php';
2e25760a 561 $rfc822_header = $att->rfc822_header;
098ea084 562 $filename = $rfc822_header->subject;
6cc08d8b 563 if (trim( $filename ) == '') {
564 $filename = 'untitled-[' . $ent . ']' ;
08b7f7cc 565 }
2e25760a 566 $from_o = $rfc822_header->from;
567 if (is_object($from_o)) {
04ea844e 568 $from_name = decodeHeader($from_o->getAddress(false));
7e697748 569 } elseif (is_array($from_o) && count($from_o) && is_object($from_o[0])) {
570 // something weird happens when a digest message is opened and you return to the digest
571 // now the from object is part of an array. Probably the parseHeader call overwrites the info
572 // retrieved from the bodystructure in a different way. We need to fix this later.
573 // possible starting point, do not fetch header we already have and inspect how
574 // the rfc822_header object behaves.
575 $from_name = decodeHeader($from_o[0]->getAddress(false));
2e25760a 576 } else {
577 $from_name = _("Unknown sender");
f0c4dc12 578 }
d0187bd6 579 $description = _("From").': '.$from_name;
23bcec6f 580 } else {
202bcbcc 581 $default_page = $base_uri . 'src/download.php';
02474e43 582 $filename = $att->getFilename();
f810c0b2 583 if ($header->description) {
098ea084 584 $description = decodeHeader($header->description);
f810c0b2 585 } else {
3d8371be 586 $description = '';
587 }
2e25760a 588 }
589
590 $display_filename = $filename;
3d8371be 591 if (isset($passed_ent_id)) {
592 $passed_ent_id_link = '&amp;passed_ent_id='.$passed_ent_id;
593 } else {
594 $passed_ent_id_link = '';
595 }
596 $defaultlink = $default_page . "?startMessage=$startMessage"
8bd0068d 597 . "&amp;passed_id=$id&amp;mailbox=$urlMailbox"
598 . '&amp;ent_id='.$ent.$passed_ent_id_link;
2e25760a 599 if ($where && $what) {
8bd0068d 600 $defaultlink .= '&amp;where='. urlencode($where).'&amp;what='.urlencode($what);
2e25760a 601 }
7e2ff844 602 // IE does make use of mime content sniffing. Forcing a download
603 // prohibit execution of XSS inside an application/octet-stream attachment
604 if ($type0 == 'application' && $type1 == 'octet-stream') {
605 $defaultlink .= '&amp;absolute_dl=true';
606 }
2b665f28 607
3d8371be 608 /* This executes the attachment hook with a specific MIME-type.
53901c7b 609 * It also allows plugins to run if there's a rule for a more
610 * generic type. Finally, a hook for ALL attachment types is
611 * run as well.
8bd0068d 612 */
8dca4d22 613 // First remember the default link.
614 $defaultlink_orig = $defaultlink;
615
d849b570 616 /* The API for this hook has changed as of 1.5.2 so that all plugin
617 arguments are passed in an array instead of each their own plugin
618 argument, and arguments are passed by reference, so instead of
619 returning any changes, changes should simply be made to the original
620 arguments themselves. */
53901c7b 621 $temp = array(&$links, &$startMessage, &$id, &$urlMailbox, &$ent,
9f6cc07c 622 &$defaultlink, &$display_filename, &$where, &$what,
c2eacf5e 623 &$type0, &$type1);
9c3b2d22 624 do_hook("attachment $type0/$type1", $temp);
53901c7b 625 /* The API for this hook has changed as of 1.5.2 so that all plugin
626 arguments are passed in an array instead of each their own plugin
627 argument, and arguments are passed by reference, so instead of
628 returning any changes, changes should simply be made to the original
629 arguments themselves. */
630 $temp = array(&$links, &$startMessage, &$id, &$urlMailbox, &$ent,
c2eacf5e 631 &$defaultlink, &$display_filename, &$where, &$what,
632 &$type0, &$type1);
53901c7b 633 // Do not let a generic plugin change the default link if a more
634 // specialized one already did it...
635 if ($defaultlink != $defaultlink_orig) {
636 $dummy = '';
637 $temp[5] = &$dummy;
2e25760a 638 }
53901c7b 639 do_hook("attachment $type0/*", $temp);
d849b570 640 /* The API for this hook has changed as of 1.5.2 so that all plugin
641 arguments are passed in an array instead of each their own plugin
642 argument, and arguments are passed by reference, so instead of
643 returning any changes, changes should simply be made to the original
644 arguments themselves. */
9c3b2d22 645 $temp = array(&$links, &$startMessage, &$id, &$urlMailbox, &$ent,
c2eacf5e 646 &$defaultlink, &$display_filename, &$where, &$what,
647 &$type0, &$type1);
8dca4d22 648 // Do not let a generic plugin change the default link if a more
649 // specialized one already did it...
650 if ($defaultlink != $defaultlink_orig) {
651 $dummy = '';
652 $temp[5] = &$dummy;
653 }
9c3b2d22 654 do_hook("attachment */*", $temp);
77b88425 655
d67f519a 656 $this_attachment = array();
657 $this_attachment['Name'] = decodeHeader($display_filename);
658 $this_attachment['Description'] = $description;
659 $this_attachment['DefaultHREF'] = $defaultlink;
660 $this_attachment['DownloadHREF'] = $links['download link']['href'];
661 $this_attachment['ViewHREF'] = isset($links['attachment_common']) ? $links['attachment_common']['href'] : '';
24e8917e 662
663 // base64 encoded file sizes are misleading, so approximate real size
664 if (!empty($header->encoding) && strtolower($header->encoding) == 'base64')
665 $this_attachment['Size'] = $header->size / 4 * 3;
666 else
667 $this_attachment['Size'] = $header->size;
668
3047e291 669 $this_attachment['ContentType'] = sm_encode_html_special_chars($type0 .'/'. $type1);
d67f519a 670 $this_attachment['OtherLinks'] = array();
3d8371be 671 foreach ($links as $val) {
336995ea 672 if ($val['text']==_("Download")) {
673 $this_attachment['DownloadHREF'] = $val['href'];
d0187bd6 674 continue;
336995ea 675 }
676 if ($val['text']==_("View")) {
677 $this_attachment['ViewHREF'] = $val['href'];
678 continue;
679 }
4ed45b62 680
681 // This makes no sense - If 'text' and 'extra' are just concatenated,
682 // there is no point in having 'extra'.... I am going to assume this
683 // was a mistake and am changing 'extra' to be what I think it was
684 // meant to be: additional tag attributes. However, I'm not checking
685 // extensively for plugins that were using this the wrong way (but why would they?)
686 if (empty($val['text']))
d0187bd6 687 continue;
2b665f28 688
d67f519a 689 $temp = array();
690 $temp['HREF'] = $val['href'];
4ed45b62 691 $temp['Text'] = $val['text'];
692 $temp['Extra'] = (empty($val['extra']) ? '' : $val['extra']);
d67f519a 693 $this_attachment['OtherLinks'][] = $temp;
2e25760a 694 }
d67f519a 695 $attachments[] = $this_attachment;
2b665f28 696
3d8371be 697 unset($links);
2e25760a 698 }
2b665f28 699
d67f519a 700 return $attachments;
701}
702
703/**
704 * Displays attachment links and information
705 *
706 * Since 1.3.0 function is not included in formatBody() call.
707 *
708 * Since 1.0.2 uses attachment $type0/$type1 hook.
709 * Since 1.2.5 uses attachment $type0/* hook.
710 * Since 1.5.0 uses attachments_bottom hook.
711 * Since 1.5.2 uses templates and does *not* return a value.
712 *
713 * @param object $message SquirrelMail message object
714 * @param array $exclude_id message parts that are not attachments.
715 * @param string $mailbox mailbox name
716 * @param integer $id message id
717 */
718function formatAttachments($message, $exclude_id, $mailbox, $id) {
719 global $oTemplate;
2b665f28 720
d67f519a 721 $attach = buildAttachmentArray($message, $exclude_id, $mailbox, $id);
d0187bd6 722
723 $oTemplate->assign('attachments', $attach);
724 $oTemplate->display('read_attachments.tpl');
451f74a2 725}
b74ba498 726
7c7b74b3 727function sqimap_base64_decode(&$string) {
7c0ec1d8 728
b17a8968 729 // Base64 encoded data goes in pairs of 4 bytes. To achieve on the
7c0ec1d8 730 // fly decoding (to reduce memory usage) you have to check if the
731 // data has incomplete pairs
732
b17a8968 733 // Remove the noise in order to check if the 4 bytes pairs are complete
7c0ec1d8 734 $string = str_replace(array("\r\n","\n", "\r", " "),array('','','',''),$string);
735
42ce44f8 736 $sStringRem = '';
7c0ec1d8 737 $iMod = strlen($string) % 4;
738 if ($iMod) {
739 $sStringRem = substr($string,-$iMod);
b17a8968 740 // Check if $sStringRem contains padding characters
7c0ec1d8 741 if (substr($sStringRem,-1) != '=') {
742 $string = substr($string,0,-$iMod);
743 } else {
744 $sStringRem = '';
745 }
746 }
7c7b74b3 747 $string = base64_decode($string);
7c0ec1d8 748 return $sStringRem;
7c7b74b3 749}
750
fdf7cef1 751/**
fa26ab45 752 * Decodes encoded string (usually message body)
753 *
754 * This function decodes a string (usually the message body)
755 * depending on the encoding type. Currently quoted-printable
756 * and base64 encodings are supported.
757 *
758 * The decode_body hook was added to this function in 1.4.2/1.5.0.
759 * The $force_crlf parameter was added in 1.5.2.
760 *
761 * @param string $string The encoded string
762 * @param string $encoding used encoding
763 * @param string $force_crlf Whether or not to force CRLF or LF
764 * line endings (or to leave as is).
765 * If given as "LF", line endings will
766 * all be converted to LF; if "CRLF",
767 * line endings will all be converted
768 * to CRLF. If given as an empty value,
9785376e 769 * the global $force_crlf_default will
fa26ab45 770 * be consulted (it can be specified in
771 * config/config_local.php). Otherwise,
772 * any other value will cause the string
773 * to be left alone. Note that this will
774 * be overridden to "LF" if not using at
775 * least PHP version 4.3.0. (OPTIONAL;
776 * default is empty - consult global
777 * default value)
778 *
779 * @return string The decoded string
fdf7cef1 780 *
fdf7cef1 781 * @since 1.0
fa26ab45 782 *
fdf7cef1 783 */
fa26ab45 784function decodeBody($string, $encoding, $force_crlf='') {
785
786 global $force_crlf_default;
787 if (empty($force_crlf)) $force_crlf = $force_crlf_default;
788 $force_crlf = strtoupper($force_crlf);
789
790 // must force line endings to LF due to broken
791 // quoted_printable_decode() in PHP versions
792 // before 4.3.0 (see below)
793 //
794 if (!check_php_version(4, 3, 0) || $force_crlf == 'LF')
795 $string = str_replace("\r\n", "\n", $string);
796 else if ($force_crlf == 'CRLF')
797 $string = str_replace("\n", "\r\n", $string);
83be314a 798
b583c3e8 799 $encoding = strtolower($encoding);
3d8371be 800
d849b570 801 $encoding_handler = do_hook('decode_body', $encoding);
5166f86a 802
803
fa26ab45 804 // plugins get first shot at decoding the string
5166f86a 805 //
806 if (!empty($encoding_handler) && function_exists($encoding_handler)) {
fa26ab45 807 $string = $encoding_handler('decode', $string);
5166f86a 808
fdf7cef1 809 } elseif ($encoding == 'quoted-printable' ||
8bd0068d 810 $encoding == 'quoted_printable') {
fa26ab45 811
812 // quoted_printable_decode() function is broken in older
813 // php versions. Text with \r\n decoding was fixed only
814 // in php 4.3.0. Minimal code requirement is PHP 4.0.4+
815 // and the above call to: str_replace("\r\n", "\n", $string);
816 //
817 $string = quoted_printable_decode($string);
818
fdf7cef1 819 } elseif ($encoding == 'base64') {
fa26ab45 820 $string = base64_decode($string);
b583c3e8 821 }
3d8371be 822
b583c3e8 823 // All other encodings are returned raw.
fa26ab45 824 return $string;
451f74a2 825}
826
9f7f68c3 827/**
8bd0068d 828 * Decodes headers
829 *
e1115979 830 * This function decodes strings that are encoded according to
8bd0068d 831 * RFC1522 (MIME Part Two: Message Header Extensions for Non-ASCII Text).
832 * Patched by Christian Schmidt <christian@ostenfeld.dk> 23/03/2002
833 *
834 * @param string $string header string that has to be made readable
835 * @param boolean $utfencode change message in order to be readable on user's charset. defaults to true
bcae324b 836 * @param boolean $htmlsafe preserve spaces and sanitize html special characters. defaults to true
8bd0068d 837 * @param boolean $decide decide if string can be utfencoded. defaults to false
838 * @return string decoded header string
839 */
bcae324b 840function decodeHeader ($string, $utfencode=true,$htmlsafe=true,$decide=false) {
caf0ab1d 841 global $languages, $squirrelmail_language,$default_charset, $fix_broken_base64_encoded_messages;
79e07c7e 842 if (is_array($string)) {
843 $string = implode("\n", $string);
844 }
da2415c1 845
10dec454 846 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
8bd0068d 847 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decodeheader')) {
33a55f5a 848 $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decodeheader', $string);
08b7f7cc 849 // Do we need to return at this point?
850 // return $string;
83be314a 851 }
79e07c7e 852 $i = 0;
08b7f7cc 853 $iLastMatch = -2;
db65b6b0 854 $encoded = true;
0a06275a 855
821d1f14 856// FIXME: spaces are allowed inside quoted-printable encoding, but the following line will bust up any such encoded strings
098ea084 857 $aString = explode(' ',$string);
08b7f7cc 858 $ret = '';
098ea084 859 foreach ($aString as $chunk) {
358a78a1 860 if ($encoded && $chunk === '') {
08b7f7cc 861 continue;
358a78a1 862 } elseif ($chunk === '') {
08b7f7cc 863 $ret .= ' ';
864 continue;
865 }
098ea084 866 $encoded = false;
08b7f7cc 867 /* if encoded words are not separated by a linear-space-white we still catch them */
868 $j = $i-1;
7e6ca3e8 869
08b7f7cc 870 while ($match = preg_match('/^(.*)=\?([^?]*)\?(Q|B)\?([^?]*)\?=(.*)$/Ui',$chunk,$res)) {
871 /* if the last chunk isn't an encoded string then put back the space, otherwise don't */
872 if ($iLastMatch !== $j) {
bcae324b 873 if ($htmlsafe) {
9f7f68c3 874 $ret .= '&#32;';
08b7f7cc 875 } else {
876 $ret .= ' ';
877 }
878 }
879 $iLastMatch = $i;
880 $j = $i;
bcae324b 881 if ($htmlsafe) {
3047e291 882 $ret .= sm_encode_html_special_chars($res[1]);
cb718de0 883 } else {
884 $ret .= $res[1];
885 }
098ea084 886 $encoding = ucfirst($res[3]);
d6f584fc 887
888 /* decide about valid decoding */
889 if ($decide && is_conversion_safe($res[2])) {
8bd0068d 890 $utfencode=true;
891 $can_be_encoded=true;
d6f584fc 892 } else {
8bd0068d 893 $can_be_encoded=false;
d6f584fc 894 }
098ea084 895 switch ($encoding)
896 {
8bd0068d 897 case 'B':
caf0ab1d 898 // fix broken base64-encoded strings (remove end = padding,
899 // change any = to + in middle of string, add padding back
900 // to the end)
901 if ($fix_broken_base64_encoded_messages) {
902 $encoded_string_minus_padding = strtr(rtrim($res[4], '='), '=', '+');
903 $res[4] = str_pad($encoded_string_minus_padding, strlen($res[4]), '=');
904 }
8bd0068d 905 $replace = base64_decode($res[4]);
906 if ($utfencode) {
907 if ($can_be_encoded) {
908 /* convert string to different charset,
909 * if functions asks for it (usually in compose)
910 */
bcae324b 911 $ret .= charset_convert($res[2],$replace,$default_charset,$htmlsafe);
8bd0068d 912 } else {
913 // convert string to html codes in order to display it
914 $ret .= charset_decode($res[2],$replace);
915 }
fab65ca9 916 } else {
bcae324b 917 if ($htmlsafe) {
3047e291 918 $replace = sm_encode_html_special_chars($replace);
8bd0068d 919 }
920 $ret.= $replace;
fab65ca9 921 }
8bd0068d 922 break;
923 case 'Q':
924 $replace = str_replace('_', ' ', $res[4]);
6c4e2e9b 925 $replace = preg_replace_callback('/=([0-9a-f]{2})/i',
9cfb0b2e 926 (check_php_version(5, 3, 0)
927 ? function($matches) { return chr(hexdec($matches[1])); }
928 : create_function ('$matches', 'return chr(hexdec($matches[1]));')
929 ),
930 $replace);
8bd0068d 931 if ($utfencode) {
932 if ($can_be_encoded) {
933 /* convert string to different charset,
934 * if functions asks for it (usually in compose)
935 */
bcae324b 936 $replace = charset_convert($res[2], $replace,$default_charset,$htmlsafe);
8bd0068d 937 } else {
938 // convert string to html codes in order to display it
939 $replace = charset_decode($res[2], $replace);
940 }
941 } else {
bcae324b 942 if ($htmlsafe) {
3047e291 943 $replace = sm_encode_html_special_chars($replace);
8bd0068d 944 }
098ea084 945 }
8bd0068d 946 $ret .= $replace;
947 break;
948 default:
949 break;
79e07c7e 950 }
098ea084 951 $chunk = $res[5];
952 $encoded = true;
08b7f7cc 953 }
954 if (!$encoded) {
bcae324b 955 if ($htmlsafe) {
9f7f68c3 956 $ret .= '&#32;';
08b7f7cc 957 } else {
958 $ret .= ' ';
da2415c1 959 }
08b7f7cc 960 }
dc3d13a7 961
bcae324b 962 if (!$encoded && $htmlsafe) {
3047e291 963 $ret .= sm_encode_html_special_chars($chunk);
dc3d13a7 964 } else {
965 $ret .= $chunk;
966 }
098ea084 967 ++$i;
968 }
fd81e884 969 /* remove the first added space */
970 if ($ret) {
bcae324b 971 if ($htmlsafe) {
9f7f68c3 972 $ret = substr($ret,5);
fd81e884 973 } else {
974 $ret = substr($ret,1);
975 }
976 }
da2415c1 977
08b7f7cc 978 return $ret;
451f74a2 979}
980
9f7f68c3 981/**
a24cf710 982 * Encodes header
8bd0068d 983 *
a24cf710 984 * Function uses XTRA_CODE _encodeheader function, if such function exists.
a24cf710 985 *
758a7889 986 * Function uses Q encoding by default and encodes a string according to RFC
987 * 1522 for use in headers if it contains 8-bit characters or anything that
a24cf710 988 * looks like it should be encoded.
8bd0068d 989 *
758a7889 990 * Function switches to B encoding and encodeHeaderBase64() function, if
991 * string is 8bit and multibyte character set supported by mbstring extension
992 * is used. It can cause E_USER_NOTICE errors, if interface is used with
f270a6eb 993 * multibyte character set unsupported by mbstring extension.
994 *
8bd0068d 995 * @param string $string header string, that has to be encoded
996 * @return string quoted-printable encoded string
f270a6eb 997 * @todo make $mb_charsets system wide constant
8bd0068d 998 */
451f74a2 999function encodeHeader ($string) {
6fbd125b 1000 global $default_charset, $languages, $squirrelmail_language;
83be314a 1001
10dec454 1002 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
8bd0068d 1003 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encodeheader')) {
33a55f5a 1004 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encodeheader', $string);
83be314a 1005 }
793cc001 1006
a24cf710 1007 // Use B encoding for multibyte charsets
f270a6eb 1008 $mb_charsets = array('utf-8','big5','gb2313','euc-kr');
1009 if (in_array($default_charset,$mb_charsets) &&
1010 in_array($default_charset,sq_mb_list_encodings()) &&
1011 sq_is8bit($string)) {
1012 return encodeHeaderBase64($string,$default_charset);
1013 } elseif (in_array($default_charset,$mb_charsets) &&
1014 sq_is8bit($string) &&
1015 ! in_array($default_charset,sq_mb_list_encodings())) {
1016 // Add E_USER_NOTICE error here (can cause 'Cannot add header information' warning in compose.php)
1017 // trigger_error('encodeHeader: Multibyte character set unsupported by mbstring extension.',E_USER_NOTICE);
1018 }
a24cf710 1019
451f74a2 1020 // Encode only if the string contains 8-bit characters or =?
3d8371be 1021 $j = strlen($string);
098ea084 1022 $max_l = 75 - strlen($default_charset) - 7;
1023 $aRet = array();
451f74a2 1024 $ret = '';
c96c32f4 1025 $iEncStart = $enc_init = false;
0d53f0f9 1026 $cur_l = $iOffset = 0;
3d8371be 1027 for($i = 0; $i < $j; ++$i) {
1710ad65 1028 switch($string[$i])
c96c32f4 1029 {
6b76cffa 1030 case '"':
8bd0068d 1031 case '=':
1032 case '<':
1033 case '>':
1034 case ',':
1035 case '?':
1036 case '_':
1037 if ($iEncStart === false) {
1038 $iEncStart = $i;
1039 }
1040 $cur_l+=3;
1041 if ($cur_l > ($max_l-2)) {
1042 /* if there is an stringpart that doesn't need encoding, add it */
08b7f7cc 1043 $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
c96c32f4 1044 $aRet[] = "=?$default_charset?Q?$ret?=";
1045 $iOffset = $i;
1046 $cur_l = 0;
1047 $ret = '';
1048 $iEncStart = false;
08b7f7cc 1049 } else {
1710ad65 1050 $ret .= sprintf("=%02X",ord($string[$i]));
c96c32f4 1051 }
8bd0068d 1052 break;
1053 case '(':
1054 case ')':
1055 if ($iEncStart !== false) {
08b7f7cc 1056 $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
8bd0068d 1057 $aRet[] = "=?$default_charset?Q?$ret?=";
c96c32f4 1058 $iOffset = $i;
8bd0068d 1059 $cur_l = 0;
1060 $ret = '';
1061 $iEncStart = false;
c96c32f4 1062 }
8bd0068d 1063 break;
1064 case ' ':
c96c32f4 1065 if ($iEncStart !== false) {
098ea084 1066 $cur_l++;
1067 if ($cur_l > $max_l) {
08b7f7cc 1068 $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
c96c32f4 1069 $aRet[] = "=?$default_charset?Q?$ret?=";
c96c32f4 1070 $iOffset = $i;
1071 $cur_l = 0;
098ea084 1072 $ret = '';
8bd0068d 1073 $iEncStart = false;
08b7f7cc 1074 } else {
8bd0068d 1075 $ret .= '_';
c96c32f4 1076 }
3d8371be 1077 }
8bd0068d 1078 break;
1079 default:
1710ad65 1080 $k = ord($string[$i]);
8bd0068d 1081 if ($k > 126) {
1082 if ($iEncStart === false) {
1083 // do not start encoding in the middle of a string, also take the rest of the word.
1084 $sLeadString = substr($string,0,$i);
1085 $aLeadString = explode(' ',$sLeadString);
1086 $sToBeEncoded = array_pop($aLeadString);
1087 $iEncStart = $i - strlen($sToBeEncoded);
1088 $ret .= $sToBeEncoded;
1089 $cur_l += strlen($sToBeEncoded);
1090 }
1091 $cur_l += 3;
1092 /* first we add the encoded string that reached it's max size */
1093 if ($cur_l > ($max_l-2)) {
1094 $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
1095 $aRet[] = "=?$default_charset?Q?$ret?= "; /* the next part is also encoded => separate by space */
1096 $cur_l = 3;
1097 $ret = '';
1098 $iOffset = $i;
1099 $iEncStart = $i;
1100 }
1101 $enc_init = true;
1102 $ret .= sprintf("=%02X", $k);
1103 } else {
1104 if ($iEncStart !== false) {
1105 $cur_l++;
1106 if ($cur_l > $max_l) {
1107 $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
1108 $aRet[] = "=?$default_charset?Q?$ret?=";
1109 $iEncStart = false;
1110 $iOffset = $i;
1111 $cur_l = 0;
1112 $ret = '';
1113 } else {
1710ad65 1114 $ret .= $string[$i];
8bd0068d 1115 }
1116 }
1117 }
1118 break;
f7b3ba37 1119 }
451f74a2 1120 }
793cc001 1121
c96c32f4 1122 if ($enc_init) {
1123 if ($iEncStart !== false) {
1124 $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
1125 $aRet[] = "=?$default_charset?Q?$ret?=";
1126 } else {
1127 $aRet[] = substr($string,$iOffset);
1128 }
1129 $string = implode('',$aRet);
451f74a2 1130 }
3d8371be 1131 return $string;
451f74a2 1132}
b74ba498 1133
f270a6eb 1134/**
1135 * Encodes string according to rfc2047 B encoding header formating rules
1136 *
758a7889 1137 * It is recommended way to encode headers with character sets that store
f270a6eb 1138 * symbols in more than one byte.
1139 *
1140 * Function requires mbstring support. If required mbstring functions are missing,
1141 * function returns false and sets E_USER_WARNING level error message.
1142 *
758a7889 1143 * Minimal requirements - php 4.0.6 with mbstring extension. Please note,
1144 * that mbstring functions will generate E_WARNING errors, if unsupported
f270a6eb 1145 * character set is used. mb_encode_mimeheader function provided by php
1146 * mbstring extension is not used in order to get better control of header
1147 * encoding.
1148 *
758a7889 1149 * Used php code functions - function_exists(), trigger_error(), strlen()
1150 * (is used with charset names and base64 strings). Used php mbstring
f270a6eb 1151 * functions - mb_strlen and mb_substr.
1152 *
758a7889 1153 * Related documents: rfc 2045 (BASE64 encoding), rfc 2047 (mime header
f270a6eb 1154 * encoding), rfc 2822 (header folding)
1155 *
1156 * @param string $string header string that must be encoded
758a7889 1157 * @param string $charset character set. Must be supported by mbstring extension.
f270a6eb 1158 * Use sq_mb_list_encodings() to detect supported charsets.
1159 * @return string string encoded according to rfc2047 B encoding formating rules
1160 * @since 1.5.1
1161 * @todo First header line can be wrapped to $iMaxLength - $HeaderFieldLength - 1
1162 * @todo Do we want to control max length of header?
1163 * @todo Do we want to control EOL (end-of-line) marker?
1164 * @todo Do we want to translate error message?
1165 */
1166function encodeHeaderBase64($string,$charset) {
1167 /**
1168 * Check mbstring function requirements.
1169 */
1170 if (! function_exists('mb_strlen') ||
1171 ! function_exists('mb_substr')) {
1172 // set E_USER_WARNING
1173 trigger_error('encodeHeaderBase64: Required mbstring functions are missing.',E_USER_WARNING);
1174 // return false
1175 return false;
1176 }
1177
1178 // initial return array
1179 $aRet = array();
1180
1181 /**
1182 * header length = 75 symbols max (same as in encodeHeader)
1183 * remove $charset length
1184 * remove =? ? ?= (5 chars)
1185 * remove 2 more chars (\r\n ?)
1186 */
1187 $iMaxLength = 75 - strlen($charset) - 7;
1188
1189 // set first character position
1190 $iStartCharNum = 0;
1191
1192 // loop through all characters. count characters and not bytes.
1193 for ($iCharNum=1; $iCharNum<=mb_strlen($string,$charset); $iCharNum++) {
1194 // encode string from starting character to current character.
1195 $encoded_string = base64_encode(mb_substr($string,$iStartCharNum,$iCharNum-$iStartCharNum,$charset));
1196
1197 // Check encoded string length
1198 if(strlen($encoded_string)>$iMaxLength) {
1199 // if string exceeds max length, reduce number of encoded characters and add encoded string part to array
1200 $aRet[] = base64_encode(mb_substr($string,$iStartCharNum,$iCharNum-$iStartCharNum-1,$charset));
1201
1202 // set new starting character
1203 $iStartCharNum = $iCharNum-1;
1204
1205 // encode last char (in case it is last character in string)
1206 $encoded_string = base64_encode(mb_substr($string,$iStartCharNum,$iCharNum-$iStartCharNum,$charset));
1207 } // if string is shorter than max length - add next character
1208 }
1209
1210 // add last encoded string to array
1211 $aRet[] = $encoded_string;
1212
1213 // set initial return string
1214 $sRet = '';
1215
1216 // loop through encoded strings
1217 foreach($aRet as $string) {
1218 // TODO: Do we want to control EOL (end-of-line) marker
1219 if ($sRet!='') $sRet.= " ";
1220
1221 // add header tags and encoded string to return string
1222 $sRet.= '=?'.$charset.'?B?'.$string.'?=';
1223 }
1224
1225 return $sRet;
1226}
1227
691a2d25 1228/* This function trys to locate the entity_id of a specific mime element */
3d8371be 1229function find_ent_id($id, $message) {
a171b359 1230 for ($i = 0, $ret = ''; $ret == '' && $i < count($message->entities); $i++) {
1231 if ($message->entities[$i]->header->type0 == 'multipart') {
3d8371be 1232 $ret = find_ent_id($id, $message->entities[$i]);
451f74a2 1233 } else {
3d8371be 1234 if (strcasecmp($message->entities[$i]->header->id, $id) == 0) {
d8cffbab 1235// if (sq_check_save_extension($message->entities[$i])) {
8bd0068d 1236 return $message->entities[$i]->entity_id;
da2415c1 1237// }
c8f5f606 1238 } elseif (!empty($message->entities[$i]->header->parameters['name'])) {
1239 /**
1240 * This is part of a fix for Outlook Express 6.x generating
1241 * cid URLs without creating content-id headers
1242 * @@JA - 20050207
1243 */
1244 if (strcasecmp($message->entities[$i]->header->parameters['name'], $id) == 0) {
1245 return $message->entities[$i]->entity_id;
1246 }
3d8371be 1247 }
a3daaaf3 1248 }
451f74a2 1249 }
3d8371be 1250 return $ret;
451f74a2 1251}
a3daaaf3 1252
e5e9381a 1253function sq_check_save_extension($message) {
1254 $filename = $message->getFilename();
1255 $ext = substr($filename, strrpos($filename,'.')+1);
1256 $save_extensions = array('jpg','jpeg','gif','png','bmp');
3d8371be 1257 return in_array($ext, $save_extensions);
e5e9381a 1258}
1259
1260
691a2d25 1261/**
8bd0068d 1262 ** HTMLFILTER ROUTINES
1263 */
451f74a2 1264
2dd879b8 1265/**
0493ed11 1266 * This function checks attribute values for entity-encoded values
1267 * and returns them translated into 8-bit strings so we can run
1268 * checks on them.
8bd0068d 1269 *
0493ed11 1270 * @param $attvalue A string to run entity check against.
1271 * @return Nothing, modifies a reference value.
8bd0068d 1272 */
0493ed11 1273function sq_defang(&$attvalue){
1274 $me = 'sq_defang';
2dd879b8 1275 /**
0493ed11 1276 * Skip this if there aren't ampersands or backslashes.
8bd0068d 1277 */
0493ed11 1278 if (strpos($attvalue, '&') === false
1279 && strpos($attvalue, '\\') === false){
1280 return;
2dd879b8 1281 }
0493ed11 1282 $m = false;
2b665f28 1283 // before deent, translate the dangerous unicode characters and ... to safe values
1284 // otherwise the regular expressions do not match.
1285
1286
1287
0493ed11 1288 do {
1289 $m = false;
1290 $m = $m || sq_deent($attvalue, '/\&#0*(\d+);*/s');
1291 $m = $m || sq_deent($attvalue, '/\&#x0*((\d|[a-f])+);*/si', true);
1292 $m = $m || sq_deent($attvalue, '/\\\\(\d+)/s', true);
1293 } while ($m == true);
1294 $attvalue = stripslashes($attvalue);
2dd879b8 1295}
1296
1297/**
8bd0068d 1298 * Kill any tabs, newlines, or carriage returns. Our friends the
1299 * makers of the browser with 95% market value decided that it'd
1300 * be funny to make "java[tab]script" be just as good as "javascript".
1301 *
1302 * @param attvalue The attribute value before extraneous spaces removed.
0493ed11 1303 * @return attvalue Nothing, modifies a reference value.
8bd0068d 1304 */
0493ed11 1305function sq_unspace(&$attvalue){
1306 $me = 'sq_unspace';
1307 if (strcspn($attvalue, "\t\r\n\0 ") != strlen($attvalue)){
1308 $attvalue = str_replace(Array("\t", "\r", "\n", "\0", " "),
1309 Array('', '', '', '', ''), $attvalue);
2dd879b8 1310 }
2dd879b8 1311}
1312
2b665f28 1313/**
88de4926 1314 * Translate all dangerous Unicode or Shift_JIS characters which are accepted by
2b665f28 1315 * IE as regular characters.
1316 *
1317 * @param attvalue The attribute value before dangerous characters are translated.
1318 * @return attvalue Nothing, modifies a reference value.
1319 * @author Marc Groot Koerkamp.
1320 */
1321function sq_fixIE_idiocy(&$attvalue) {
1322 // remove NUL
1323 $attvalue = str_replace("\0", "", $attvalue);
1324 // remove comments
1325 $attvalue = preg_replace("/(\/\*.*?\*\/)/","",$attvalue);
1326
88de4926 1327 // IE has the evil habit of accepting every possible value for the attribute expression.
1328 // The table below contains characters which are parsed by IE if they are used in the "expression"
2b665f28 1329 // attribute value.
1330 $aDangerousCharsReplacementTable = array(
1331 array('&#x029F;', '&#0671;' ,/* L UNICODE IPA Extension */
1332 '&#x0280;', '&#0640;' ,/* R UNICODE IPA Extension */
1333 '&#x0274;', '&#0628;' ,/* N UNICODE IPA Extension */
567dc524 1334 '&#xFF25;', '&#65317;' ,/* Unicode FULLWIDTH LATIN CAPITAL LETTER E */
1335 '&#xFF45;', '&#65349;' ,/* Unicode FULLWIDTH LATIN SMALL LETTER E */
2b665f28 1336 '&#xFF38;', '&#65336;',/* Unicode FULLWIDTH LATIN CAPITAL LETTER X */
1337 '&#xFF58;', '&#65368;',/* Unicode FULLWIDTH LATIN SMALL LETTER X */
1338 '&#xFF30;', '&#65328;',/* Unicode FULLWIDTH LATIN CAPITAL LETTER P */
1339 '&#xFF50;', '&#65360;',/* Unicode FULLWIDTH LATIN SMALL LETTER P */
1340 '&#xFF32;', '&#65330;',/* Unicode FULLWIDTH LATIN CAPITAL LETTER R */
1341 '&#xFF52;', '&#65362;',/* Unicode FULLWIDTH LATIN SMALL LETTER R */
1342 '&#xFF33;', '&#65331;',/* Unicode FULLWIDTH LATIN CAPITAL LETTER S */
1343 '&#xFF53;', '&#65363;',/* Unicode FULLWIDTH LATIN SMALL LETTER S */
1344 '&#xFF29;', '&#65321;',/* Unicode FULLWIDTH LATIN CAPITAL LETTER I */
1345 '&#xFF49;', '&#65353;',/* Unicode FULLWIDTH LATIN SMALL LETTER I */
1346 '&#xFF2F;', '&#65327;',/* Unicode FULLWIDTH LATIN CAPITAL LETTER O */
1347 '&#xFF4F;', '&#65359;',/* Unicode FULLWIDTH LATIN SMALL LETTER O */
1348 '&#xFF2E;', '&#65326;',/* Unicode FULLWIDTH LATIN CAPITAL LETTER N */
1349 '&#xFF4E;', '&#65358;',/* Unicode FULLWIDTH LATIN SMALL LETTER N */
1350 '&#xFF2C;', '&#65324;',/* Unicode FULLWIDTH LATIN CAPITAL LETTER L */
1351 '&#xFF4C;', '&#65356;',/* Unicode FULLWIDTH LATIN SMALL LETTER L */
1352 '&#xFF35;', '&#65333;',/* Unicode FULLWIDTH LATIN CAPITAL LETTER U */
1353 '&#xFF55;', '&#65365;',/* Unicode FULLWIDTH LATIN SMALL LETTER U */
1354 '&#x207F;', '&#8319;' ,/* Unicode SUPERSCRIPT LATIN SMALL LETTER N */
567dc524 1355 "\xEF\xBC\xA5", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER E */ // in unicode this is some Chinese char range
1356 "\xEF\xBD\x85", /* Shift JIS FULLWIDTH LATIN SMALL LETTER E */
1357 "\xEF\xBC\xB8", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER X */
1358 "\xEF\xBD\x98", /* Shift JIS FULLWIDTH LATIN SMALL LETTER X */
1359 "\xEF\xBC\xB0", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER P */
1360 "\xEF\xBD\x90", /* Shift JIS FULLWIDTH LATIN SMALL LETTER P */
1361 "\xEF\xBC\xB2", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER R */
1362 "\xEF\xBD\x92", /* Shift JIS FULLWIDTH LATIN SMALL LETTER R */
1363 "\xEF\xBC\xB3", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER S */
1364 "\xEF\xBD\x93", /* Shift JIS FULLWIDTH LATIN SMALL LETTER S */
1365 "\xEF\xBC\xA9", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER I */
1366 "\xEF\xBD\x89", /* Shift JIS FULLWIDTH LATIN SMALL LETTER I */
1367 "\xEF\xBC\xAF", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER O */
1368 "\xEF\xBD\x8F", /* Shift JIS FULLWIDTH LATIN SMALL LETTER O */
1369 "\xEF\xBC\xAE", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER N */
1370 "\xEF\xBD\x8E", /* Shift JIS FULLWIDTH LATIN SMALL LETTER N */
1371 "\xEF\xBC\xAC", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER L */
1372 "\xEF\xBD\x8C", /* Shift JIS FULLWIDTH LATIN SMALL LETTER L */
1373 "\xEF\xBC\xB5", /* Shift JIS FULLWIDTH LATIN CAPITAL LETTER U */
1374 "\xEF\xBD\x95", /* Shift JIS FULLWIDTH LATIN SMALL LETTER U */
1375 "\xE2\x81\xBF", /* Shift JIS FULLWIDTH SUPERSCRIPT N */
1376 "\xCA\x9F", /* L UNICODE IPA Extension */
1377 "\xCA\x80", /* R UNICODE IPA Extension */
1378 "\xC9\xB4"), /* N UNICODE IPA Extension */
2b665f28 1379 array('l', 'l', 'r','r','n','n',
567dc524 1380 'E','E','e','e','X','X','x','x','P','P','p','p','R','R','r','r','S','S','s','s','I','I',
1381 'i','i','O','O','o','o','N','N','n','n','L','L','l','l','U','U','u','u','n','n',
1382 'E','e','X','x','P','p','R','r','S','s','I','i','O','o','N','n','L','l','U','u','n','l','r','n'));
2b665f28 1383 $attvalue = str_replace($aDangerousCharsReplacementTable[0],$aDangerousCharsReplacementTable[1],$attvalue);
1384
88de4926 1385 // Escapes are useful for special characters like "{}[]()'&. In other cases they are
1386 // used for XSS.
2b665f28 1387 $attvalue = preg_replace("/(\\\\)([a-zA-Z]{1})/",'$2',$attvalue);
1388}
1389
691a2d25 1390/**
8bd0068d 1391 * This function returns the final tag out of the tag name, an array
1392 * of attributes, and the type of the tag. This function is called by
1393 * sq_sanitize internally.
1394 *
1395 * @param $tagname the name of the tag.
1396 * @param $attary the array of attributes and their values
1397 * @param $tagtype The type of the tag (see in comments).
1398 * @return a string with the final tag representation.
1399 */
691a2d25 1400function sq_tagprint($tagname, $attary, $tagtype){
b583c3e8 1401 $me = 'sq_tagprint';
3d8371be 1402
691a2d25 1403 if ($tagtype == 2){
1404 $fulltag = '</' . $tagname . '>';
1405 } else {
1406 $fulltag = '<' . $tagname;
1407 if (is_array($attary) && sizeof($attary)){
1408 $atts = Array();
c71c845c 1409 foreach ($attary as $attname => $attvalue){
691a2d25 1410 array_push($atts, "$attname=$attvalue");
1411 }
1412 $fulltag .= ' ' . join(" ", $atts);
1413 }
1414 if ($tagtype == 3){
b583c3e8 1415 $fulltag .= ' /';
691a2d25 1416 }
b583c3e8 1417 $fulltag .= '>';
451f74a2 1418 }
691a2d25 1419 return $fulltag;
451f74a2 1420}
a3daaaf3 1421
691a2d25 1422/**
8bd0068d 1423 * A small helper function to use with array_walk. Modifies a by-ref
1424 * value and makes it lowercase.
1425 *
1426 * @param $val a value passed by-ref.
1427 * @return void since it modifies a by-ref value.
1428 */
691a2d25 1429function sq_casenormalize(&$val){
1430 $val = strtolower($val);
1431}
451f74a2 1432
691a2d25 1433/**
8bd0068d 1434 * This function skips any whitespace from the current position within
1435 * a string and to the next non-whitespace value.
1436 *
1437 * @param $body the string
1438 * @param $offset the offset within the string where we should start
1439 * looking for the next non-whitespace character.
1440 * @return the location within the $body where the next
1441 * non-whitespace char is located.
1442 */
691a2d25 1443function sq_skipspace($body, $offset){
b583c3e8 1444 $me = 'sq_skipspace';
3d8371be 1445 preg_match('/^(\s*)/s', substr($body, $offset), $matches);
aab65dbd 1446 if (!empty($matches[1])){
1447 $offset += strlen($matches[1]);
451f74a2 1448 }
691a2d25 1449 return $offset;
451f74a2 1450}
a3daaaf3 1451
691a2d25 1452/**
8bd0068d 1453 * This function looks for the next character within a string. It's
1454 * really just a glorified "strpos", except it catches if failures
1455 * nicely.
1456 *
1457 * @param $body The string to look for needle in.
1458 * @param $offset Start looking from this position.
1459 * @param $needle The character/string to look for.
1460 * @return location of the next occurance of the needle, or
1461 * strlen($body) if needle wasn't found.
1462 */
691a2d25 1463function sq_findnxstr($body, $offset, $needle){
3d8371be 1464 $me = 'sq_findnxstr';
691a2d25 1465 $pos = strpos($body, $needle, $offset);
1466 if ($pos === FALSE){
1467 $pos = strlen($body);
451f74a2 1468 }
691a2d25 1469 return $pos;
451f74a2 1470}
a3daaaf3 1471
691a2d25 1472/**
8bd0068d 1473 * This function takes a PCRE-style regexp and tries to match it
1474 * within the string.
1475 *
1476 * @param $body The string to look for needle in.
1477 * @param $offset Start looking from here.
1478 * @param $reg A PCRE-style regex to match.
1479 * @return Returns a false if no matches found, or an array
1480 * with the following members:
1481 * - integer with the location of the match within $body
1482 * - string with whatever content between offset and the match
1483 * - string with whatever it is we matched
1484 */
691a2d25 1485function sq_findnxreg($body, $offset, $reg){
b583c3e8 1486 $me = 'sq_findnxreg';
691a2d25 1487 $matches = Array();
1488 $retarr = Array();
7d06541f 1489 preg_match("%^(.*?)($reg)%si", substr($body, $offset), $matches);
1710ad65 1490 if (!isset($matches[0]) || !$matches[0]){
691a2d25 1491 $retarr = false;
1492 } else {
1710ad65 1493 $retarr[0] = $offset + strlen($matches[1]);
1494 $retarr[1] = $matches[1];
1495 $retarr[2] = $matches[2];
691a2d25 1496 }
1497 return $retarr;
1498}
a3daaaf3 1499
691a2d25 1500/**
8bd0068d 1501 * This function looks for the next tag.
1502 *
1503 * @param $body String where to look for the next tag.
1504 * @param $offset Start looking from here.
1505 * @return false if no more tags exist in the body, or
1506 * an array with the following members:
1507 * - string with the name of the tag
1508 * - array with attributes and their values
1509 * - integer with tag type (1, 2, or 3)
1510 * - integer where the tag starts (starting "<")
1511 * - integer where the tag ends (ending ">")
1512 * first three members will be false, if the tag is invalid.
1513 */
691a2d25 1514function sq_getnxtag($body, $offset){
b583c3e8 1515 $me = 'sq_getnxtag';
691a2d25 1516 if ($offset > strlen($body)){
1517 return false;
1518 }
1519 $lt = sq_findnxstr($body, $offset, "<");
1520 if ($lt == strlen($body)){
1521 return false;
1522 }
1523 /**
8bd0068d 1524 * We are here:
1525 * blah blah <tag attribute="value">
1526 * \---------^
1527 */
691a2d25 1528 $pos = sq_skipspace($body, $lt+1);
1529 if ($pos >= strlen($body)){
1530 return Array(false, false, false, $lt, strlen($body));
1531 }
1532 /**
8bd0068d 1533 * There are 3 kinds of tags:
1534 * 1. Opening tag, e.g.:
1535 * <a href="blah">
1536 * 2. Closing tag, e.g.:
1537 * </a>
1538 * 3. XHTML-style content-less tag, e.g.:
1539 * <img src="blah" />
1540 */
691a2d25 1541 $tagtype = false;
1542 switch (substr($body, $pos, 1)){
3d8371be 1543 case '/':
1544 $tagtype = 2;
1545 $pos++;
1546 break;
1547 case '!':
1548 /**
8bd0068d 1549 * A comment or an SGML declaration.
1550 */
3d8371be 1551 if (substr($body, $pos+1, 2) == "--"){
1552 $gt = strpos($body, "-->", $pos);
1553 if ($gt === false){
1554 $gt = strlen($body);
1555 } else {
1556 $gt += 2;
1557 }
1558 return Array(false, false, false, $lt, $gt);
bb8d0799 1559 } else {
3d8371be 1560 $gt = sq_findnxstr($body, $pos, ">");
1561 return Array(false, false, false, $lt, $gt);
1562 }
1563 break;
1564 default:
1565 /**
8bd0068d 1566 * Assume tagtype 1 for now. If it's type 3, we'll switch values
1567 * later.
1568 */
3d8371be 1569 $tagtype = 1;
1570 break;
691a2d25 1571 }
a3daaaf3 1572
0493ed11 1573 $tag_start = $pos;
691a2d25 1574 $tagname = '';
1575 /**
8bd0068d 1576 * Look for next [\W-_], which will indicate the end of the tag name.
1577 */
691a2d25 1578 $regary = sq_findnxreg($body, $pos, "[^\w\-_]");
1579 if ($regary == false){
1580 return Array(false, false, false, $lt, strlen($body));
1581 }
1582 list($pos, $tagname, $match) = $regary;
1583 $tagname = strtolower($tagname);
1584
1585 /**
8bd0068d 1586 * $match can be either of these:
1587 * '>' indicating the end of the tag entirely.
1588 * '\s' indicating the end of the tag name.
1589 * '/' indicating that this is type-3 xhtml tag.
1590 *
1591 * Whatever else we find there indicates an invalid tag.
1592 */
691a2d25 1593 switch ($match){
3d8371be 1594 case '/':
691a2d25 1595 /**
8bd0068d 1596 * This is an xhtml-style tag with a closing / at the
1597 * end, like so: <img src="blah" />. Check if it's followed
1598 * by the closing bracket. If not, then this tag is invalid
1599 */
3d8371be 1600 if (substr($body, $pos, 2) == "/>"){
1601 $pos++;
1602 $tagtype = 3;
1603 } else {
1604 $gt = sq_findnxstr($body, $pos, ">");
1605 $retary = Array(false, false, false, $lt, $gt);
1606 return $retary;
1607 }
1608 case '>':
1609 return Array($tagname, false, $tagtype, $lt, $pos);
1610 break;
1611 default:
1612 /**
8bd0068d 1613 * Check if it's whitespace
1614 */
3d8371be 1615 if (!preg_match('/\s/', $match)){
1616 /**
8bd0068d 1617 * This is an invalid tag! Look for the next closing ">".
1618 */
7d06541f 1619 $gt = sq_findnxstr($body, $lt, ">");
3d8371be 1620 return Array(false, false, false, $lt, $gt);
1621 }
1622 break;
691a2d25 1623 }
3d8371be 1624
691a2d25 1625 /**
8bd0068d 1626 * At this point we're here:
1627 * <tagname attribute='blah'>
1628 * \-------^
1629 *
1630 * At this point we loop in order to find all attributes.
1631 */
691a2d25 1632 $attname = '';
0493ed11 1633 $atttype = false;
691a2d25 1634 $attary = Array();
1635
1636 while ($pos <= strlen($body)){
1637 $pos = sq_skipspace($body, $pos);
1638 if ($pos == strlen($body)){
1639 /**
8bd0068d 1640 * Non-closed tag.
1641 */
691a2d25 1642 return Array(false, false, false, $lt, $pos);
1643 }
1644 /**
8bd0068d 1645 * See if we arrived at a ">" or "/>", which means that we reached
1646 * the end of the tag.
1647 */
691a2d25 1648 $matches = Array();
164800ad 1649 if (preg_match("%^(\s*)(>|/>)%s", substr($body, $pos), $matches)) {
c828931c 1650 /**
8bd0068d 1651 * Yep. So we did.
1652 */
1710ad65 1653 $pos += strlen($matches[1]);
1654 if ($matches[2] == "/>"){
c828931c 1655 $tagtype = 3;
1656 $pos++;
1657 }
1658 return Array($tagname, $attary, $tagtype, $lt, $pos);
1659 }
a3daaaf3 1660
cca46357 1661 /**
8bd0068d 1662 * There are several types of attributes, with optional
1663 * [:space:] between members.
1664 * Type 1:
1665 * attrname[:space:]=[:space:]'CDATA'
1666 * Type 2:
1667 * attrname[:space:]=[:space:]"CDATA"
1668 * Type 3:
1669 * attr[:space:]=[:space:]CDATA
1670 * Type 4:
1671 * attrname
1672 *
1673 * We leave types 1 and 2 the same, type 3 we check for
1674 * '"' and convert to "&quot" if needed, then wrap in
1675 * double quotes. Type 4 we convert into:
1676 * attrname="yes".
1677 */
3f7c623f 1678 $regary = sq_findnxreg($body, $pos, "[^:\w\-_]");
691a2d25 1679 if ($regary == false){
1680 /**
8bd0068d 1681 * Looks like body ended before the end of tag.
1682 */
691a2d25 1683 return Array(false, false, false, $lt, strlen($body));
cca46357 1684 }
691a2d25 1685 list($pos, $attname, $match) = $regary;
1686 $attname = strtolower($attname);
1687 /**
8bd0068d 1688 * We arrived at the end of attribute name. Several things possible
1689 * here:
1690 * '>' means the end of the tag and this is attribute type 4
1691 * '/' if followed by '>' means the same thing as above
1692 * '\s' means a lot of things -- look what it's followed by.
1693 * anything else means the attribute is invalid.
1694 */
691a2d25 1695 switch($match){
3d8371be 1696 case '/':
691a2d25 1697 /**
8bd0068d 1698 * This is an xhtml-style tag with a closing / at the
1699 * end, like so: <img src="blah" />. Check if it's followed
1700 * by the closing bracket. If not, then this tag is invalid
1701 */
3d8371be 1702 if (substr($body, $pos, 2) == "/>"){
691a2d25 1703 $pos++;
3d8371be 1704 $tagtype = 3;
691a2d25 1705 } else {
3d8371be 1706 $gt = sq_findnxstr($body, $pos, ">");
1707 $retary = Array(false, false, false, $lt, $gt);
1708 return $retary;
1709 }
1710 case '>':
1710ad65 1711 $attary[$attname] = '"yes"';
3d8371be 1712 return Array($tagname, $attary, $tagtype, $lt, $pos);
1713 break;
1714 default:
1715 /**
8bd0068d 1716 * Skip whitespace and see what we arrive at.
1717 */
3d8371be 1718 $pos = sq_skipspace($body, $pos);
1719 $char = substr($body, $pos, 1);
1720 /**
8bd0068d 1721 * Two things are valid here:
1722 * '=' means this is attribute type 1 2 or 3.
1723 * \w means this was attribute type 4.
1724 * anything else we ignore and re-loop. End of tag and
1725 * invalid stuff will be caught by our checks at the beginning
1726 * of the loop.
1727 */
3d8371be 1728 if ($char == "="){
1729 $pos++;
1730 $pos = sq_skipspace($body, $pos);
691a2d25 1731 /**
8bd0068d 1732 * Here are 3 possibilities:
1733 * "'" attribute type 1
1734 * '"' attribute type 2
1735 * everything else is the content of tag type 3
1736 */
3d8371be 1737 $quot = substr($body, $pos, 1);
1738 if ($quot == "'"){
1739 $regary = sq_findnxreg($body, $pos+1, "\'");
1740 if ($regary == false){
1741 return Array(false, false, false, $lt, strlen($body));
1742 }
1743 list($pos, $attval, $match) = $regary;
1744 $pos++;
1710ad65 1745 $attary[$attname] = "'" . $attval . "'";
3d8371be 1746 } else if ($quot == '"'){
1747 $regary = sq_findnxreg($body, $pos+1, '\"');
1748 if ($regary == false){
1749 return Array(false, false, false, $lt, strlen($body));
1750 }
1751 list($pos, $attval, $match) = $regary;
1752 $pos++;
1710ad65 1753 $attary[$attname] = '"' . $attval . '"';
3d8371be 1754 } else {
1755 /**
8bd0068d 1756 * These are hateful. Look for \s, or >.
1757 */
3d8371be 1758 $regary = sq_findnxreg($body, $pos, "[\s>]");
1759 if ($regary == false){
1760 return Array(false, false, false, $lt, strlen($body));
1761 }
1762 list($pos, $attval, $match) = $regary;
1763 /**
8bd0068d 1764 * If it's ">" it will be caught at the top.
1765 */
3d8371be 1766 $attval = preg_replace("/\"/s", "&quot;", $attval);
1710ad65 1767 $attary[$attname] = '"' . $attval . '"';
7e235a1a 1768 }
3d8371be 1769 } else if (preg_match("|[\w/>]|", $char)) {
691a2d25 1770 /**
8bd0068d 1771 * That was attribute type 4.
1772 */
1710ad65 1773 $attary[$attname] = '"yes"';
3d8371be 1774 } else {
1775 /**
8bd0068d 1776 * An illegal character. Find next '>' and return.
1777 */
3d8371be 1778 $gt = sq_findnxstr($body, $pos, ">");
1779 return Array(false, false, false, $lt, $gt);
451f74a2 1780 }
3d8371be 1781 break;
691a2d25 1782 }
1783 }
1784 /**
8bd0068d 1785 * The fact that we got here indicates that the tag end was never
1786 * found. Return invalid tag indication so it gets stripped.
1787 */
691a2d25 1788 return Array(false, false, false, $lt, strlen($body));
1789}
1790
1791/**
0493ed11 1792 * Translates entities into literal values so they can be checked.
8bd0068d 1793 *
0493ed11 1794 * @param $attvalue the by-ref value to check.
1795 * @param $regex the regular expression to check against.
1796 * @param $hex whether the entites are hexadecimal.
1797 * @return True or False depending on whether there were matches.
8bd0068d 1798 */
0493ed11 1799function sq_deent(&$attvalue, $regex, $hex=false){
b583c3e8 1800 $me = 'sq_deent';
0493ed11 1801 $ret_match = false;
2b665f28 1802 // remove comments
1803 //$attvalue = preg_replace("/(\/\*.*\*\/)/","",$attvalue);
0493ed11 1804 preg_match_all($regex, $attvalue, $matches);
1805 if (is_array($matches) && sizeof($matches[0]) > 0){
1806 $repl = Array();
1807 for ($i = 0; $i < sizeof($matches[0]); $i++){
1808 $numval = $matches[1][$i];
1809 if ($hex){
1810 $numval = hexdec($numval);
a3daaaf3 1811 }
1710ad65 1812 $repl[$matches[0][$i]] = chr($numval);
691a2d25 1813 }
0493ed11 1814 $attvalue = strtr($attvalue, $repl);
1815 return true;
1816 } else {
1817 return false;
691a2d25 1818 }
691a2d25 1819}
1820
1821/**
8bd0068d 1822 * This function runs various checks against the attributes.
1823 *
1824 * @param $tagname String with the name of the tag.
1825 * @param $attary Array with all tag attributes.
1826 * @param $rm_attnames See description for sq_sanitize
1827 * @param $bad_attvals See description for sq_sanitize
1828 * @param $add_attr_to_tag See description for sq_sanitize
1829 * @param $message message object
1830 * @param $id message id
1831 * @return Array with modified attributes.
1832 */
da2415c1 1833function sq_fixatts($tagname,
1834 $attary,
691a2d25 1835 $rm_attnames,
1836 $bad_attvals,
1837 $add_attr_to_tag,
1838 $message,
b3af12ef 1839 $id,
3d8371be 1840 $mailbox
691a2d25 1841 ){
b583c3e8 1842 $me = 'sq_fixatts';
c71c845c 1843 foreach ($attary as $attname => $attvalue){
691a2d25 1844 /**
8bd0068d 1845 * See if this attribute should be removed.
1846 */
691a2d25 1847 foreach ($rm_attnames as $matchtag=>$matchattrs){
1848 if (preg_match($matchtag, $tagname)){
1849 foreach ($matchattrs as $matchattr){
1850 if (preg_match($matchattr, $attname)){
1710ad65 1851 unset($attary[$attname]);
691a2d25 1852 continue;
1853 }
451f74a2 1854 }
451f74a2 1855 }
691a2d25 1856 }
2b665f28 1857 /**
1858 * Workaround for IE quirks
1859 */
1860 sq_fixIE_idiocy($attvalue);
1861
691a2d25 1862 /**
8bd0068d 1863 * Remove any backslashes, entities, and extraneous whitespace.
1864 */
2b665f28 1865
1866 $oldattvalue = $attvalue;
0493ed11 1867 sq_defang($attvalue);
2b665f28 1868 if ($attname == 'style' && $attvalue !== $oldattvalue) {
1869 // entities are used in the attribute value. In 99% of the cases it's there as XSS
1870 // i.e.<div style="{ left:exp&#x0280;essio&#x0274;( alert('XSS') ) }">
1871 $attvalue = "idiocy";
1710ad65 1872 $attary[$attname] = $attvalue;
2b665f28 1873 }
0493ed11 1874 sq_unspace($attvalue);
af861a34 1875
691a2d25 1876 /**
8bd0068d 1877 * Now let's run checks on the attvalues.
1878 * I don't expect anyone to comprehend this. If you do,
1879 * get in touch with me so I can drive to where you live and
1880 * shake your hand personally. :)
1881 */
691a2d25 1882 foreach ($bad_attvals as $matchtag=>$matchattrs){
1883 if (preg_match($matchtag, $tagname)){
1884 foreach ($matchattrs as $matchattr=>$valary){
1885 if (preg_match($matchattr, $attname)){
1886 /**
8bd0068d 1887 * There are two arrays in valary.
1888 * First is matches.
1889 * Second one is replacements
1890 */
691a2d25 1891 list($valmatch, $valrepl) = $valary;
da2415c1 1892 $newvalue =
691a2d25 1893 preg_replace($valmatch, $valrepl, $attvalue);
1894 if ($newvalue != $attvalue){
1710ad65 1895 $attary[$attname] = $newvalue;
567dc524 1896 $attvalue = $newvalue;
691a2d25 1897 }
1898 }
1899 }
451f74a2 1900 }
a3daaaf3 1901 }
567dc524 1902 if ($attname == 'style') {
1903 if (preg_match('/[\0-\37\200-\377]+/',$attvalue)) {
1904 // 8bit and control characters in style attribute values can be used for XSS, remove them
1710ad65 1905 $attary[$attname] = '"disallowed character"';
567dc524 1906 }
1907 preg_match_all("/url\s*\((.+)\)/si",$attvalue,$aMatch);
1908 if (count($aMatch)) {
1909 foreach($aMatch[1] as $sMatch) {
1910 // url value
1911 $urlvalue = $sMatch;
1912 sq_fix_url($attname, $urlvalue, $message, $id, $mailbox,"'");
1710ad65 1913 $attary[$attname] = str_replace($sMatch,$urlvalue,$attvalue);
567dc524 1914 }
1915 }
691a2d25 1916 }
ff940ebc 1917 /**
567dc524 1918 * Use white list based filtering on attributes which can contain url's
ff940ebc 1919 */
caf0ab1d 1920 else if ($attname == 'href' || $attname == 'xlink:href' || $attname == 'src'
1921 || $attname == 'poster' || $attname == 'formaction'
1922 || $attname == 'background' || $attname == 'action') {
567dc524 1923 sq_fix_url($attname, $attvalue, $message, $id, $mailbox);
1710ad65 1924 $attary[$attname] = $attvalue;
ff940ebc 1925 }
a3daaaf3 1926 }
691a2d25 1927 /**
8bd0068d 1928 * See if we need to append any attributes to this tag.
1929 */
691a2d25 1930 foreach ($add_attr_to_tag as $matchtag=>$addattary){
1931 if (preg_match($matchtag, $tagname)){
1932 $attary = array_merge($attary, $addattary);
1933 }
1934 }
1935 return $attary;
451f74a2 1936}
a3daaaf3 1937
567dc524 1938/**
1939 * This function filters url's
1940 *
1941 * @param $attvalue String with attribute value to filter
1942 * @param $message message object
1943 * @param $id message id
1944 * @param $mailbox mailbox
1945 * @param $sQuote quoting characters around url's
1946 */
1947function sq_fix_url($attname, &$attvalue, $message, $id, $mailbox,$sQuote = '"') {
1948 $attvalue = trim($attvalue);
1949 if ($attvalue && ($attvalue[0] =='"'|| $attvalue[0] == "'")) {
1950 // remove the double quotes
1951 $sQuote = $attvalue[0];
1952 $attvalue = trim(substr($attvalue,1,-1));
1953 }
1954
d31f73f1 1955 // If there's no "view_unsafe_images" variable in the URL, turn unsafe
1956 // images off by default.
afbf184c 1957 sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET, FALSE);
d31f73f1 1958
6ab8321f 1959 global $use_transparent_security_image;
1960 if ($use_transparent_security_image) $secremoveimg = '../images/spacer.png';
1961 else $secremoveimg = '../images/' . _("sec_remove_eng.png");
567dc524 1962
1963 /**
1964 * Replace empty src tags with the blank image. src is only used
1965 * for frames, images, and image inputs. Doing a replace should
1966 * not affect them working as should be, however it will stop
1967 * IE from being kicked off when src for img tags are not set
1968 */
1969 if ($attvalue == '') {
1970 $attvalue = '"' . SM_PATH . 'images/blank.png"';
1971 } else {
1972 // first, disallow 8 bit characters and control characters
1973 if (preg_match('/[\0-\37\200-\377]+/',$attvalue)) {
1974 switch ($attname) {
1975 case 'href':
1976 $attvalue = $sQuote . 'http://invalid-stuff-detected.example.com' . $sQuote;
1977 break;
1978 default:
1979 $attvalue = $sQuote . SM_PATH . 'images/blank.png'. $sQuote;
1980 break;
1981 }
1982 } else {
1983 $aUrl = parse_url($attvalue);
1984 if (isset($aUrl['scheme'])) {
1985 switch(strtolower($aUrl['scheme'])) {
d75e755b 1986 case 'mailto':
567dc524 1987 case 'http':
1988 case 'https':
1989 case 'ftp':
1990 if ($attname != 'href') {
1991 if ($view_unsafe_images == false) {
1992 $attvalue = $sQuote . $secremoveimg . $sQuote;
1993 } else {
1994 if (isset($aUrl['path'])) {
8bbbc757 1995
1996 // No one has been able to show that image URIs
1997 // can be exploited, so for now, no restrictions
1998 // are made at all. If this proves to be a problem,
1999 // the commented-out code below can be of help.
2000 // (One consideration is that I see nothing in this
2001 // function that specifically says that we will
2002 // only ever arrive here when inspecting an image
2003 // tag, although that does seem to be the end
2004 // result - e.g., <script src="..."> where malicious
2005 // image URIs are in fact a problem are already
2006 // filtered out elsewhere.
2007 /* ---------------------------------
567dc524 2008 // validate image extension.
2009 $ext = strtolower(substr($aUrl['path'],strrpos($aUrl['path'],'.')));
2010 if (!in_array($ext,array('.jpeg','.jpg','xjpeg','.gif','.bmp','.jpe','.png','.xbm'))) {
8bbbc757 2011 // If URI is to something other than
2012 // a regular image file, get the contents
2013 // and try to see if it is an image.
2014 // Don't use Fileinfo (finfo_file()) because
2015 // we'd need to make the admin configure the
2016 // location of the magic.mime file (FIXME: add finfo_file() support later?)
2017 //
2018 $mime_type = '';
2019 if (function_exists('mime_content_type')
2020 && ($FILE = @fopen($attvalue, 'rb', FALSE))) {
2021
2022 // fetch file
2023 //
2024 $file_contents = '';
2025 while (!feof($FILE)) {
2026 $file_contents .= fread($FILE, 8192);
2027 }
2028 fclose($FILE);
2029
2030 // store file locally
2031 //
2032 global $attachment_dir, $username;
2033 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
2034 $localfilename = GenerateRandomString(32, '', 7);
2035 $full_localfilename = "$hashed_attachment_dir/$localfilename";
2036 while (file_exists($full_localfilename)) {
2037 $localfilename = GenerateRandomString(32, '', 7);
2038 $full_localfilename = "$hashed_attachment_dir/$localfilename";
2039 }
2040 $FILE = fopen("$hashed_attachment_dir/$localfilename", 'wb');
2041 fwrite($FILE, $file_contents);
2042 fclose($FILE);
2043
2044 // get mime type and remove file
2045 //
2046 $mime_type = mime_content_type("$hashed_attachment_dir/$localfilename");
2047 unlink("$hashed_attachment_dir/$localfilename");
2048 }
2049 // debug: echo "$attvalue FILE TYPE IS $mime_type<HR>";
2050 if (substr(strtolower($mime_type), 0, 5) != 'image') {
2051 $attvalue = $sQuote . SM_PATH . 'images/blank.png'. $sQuote;
2052 }
567dc524 2053 }
8bbbc757 2054 --------------------------------- */
567dc524 2055 } else {
2056 $attvalue = $sQuote . SM_PATH . 'images/blank.png'. $sQuote;
2057 }
2058 }
cf088f55 2059 } else {
2060 $attvalue = $sQuote . $attvalue . $sQuote;
567dc524 2061 }
2062 break;
2063 case 'outbind':
2064 /**
2065 * "Hack" fix for Outlook using propriatary outbind:// protocol in img tags.
2066 * One day MS might actually make it match something useful, for now, falling
2067 * back to using cid2http, so we can grab the blank.png.
2068 */
cf088f55 2069 $attvalue = $sQuote . sq_cid2http($message, $id, $attvalue, $mailbox) . $sQuote;
567dc524 2070 break;
2071 case 'cid':
2072 /**
2073 * Turn cid: urls into http-friendly ones.
2074 */
cf088f55 2075 $attvalue = $sQuote . sq_cid2http($message, $id, $attvalue, $mailbox) . $sQuote;
567dc524 2076 break;
2077 default:
2078 $attvalue = $sQuote . SM_PATH . 'images/blank.png' . $sQuote;
2079 break;
2080 }
2081 } else {
b1fbb25f 2082 if (!isset($aUrl['path']) || $aUrl['path'] != $secremoveimg) {
567dc524 2083 // parse_url did not lead to satisfying result
2084 $attvalue = $sQuote . SM_PATH . 'images/blank.png' . $sQuote;
2085 }
2086 }
2087 }
2088 }
2089}
2090
691a2d25 2091/**
8bd0068d 2092 * This function edits the style definition to make them friendly and
2093 * usable in SquirrelMail.
2094 *
2095 * @param $message the message object
2096 * @param $id the message id
2097 * @param $content a string with whatever is between <style> and </style>
2098 * @param $mailbox the message mailbox
2099 * @return a string with edited content.
2100 */
e60a299a 2101function sq_fixstyle($body, $pos, $message, $id, $mailbox){
b583c3e8 2102 $me = 'sq_fixstyle';
7e2ff844 2103 // workaround for </style> in between comments
2104 $iCurrentPos = $pos;
2105 $content = '';
2106 $sToken = '';
2107 $bSucces = false;
2108 $bEndTag = false;
2109 for ($i=$pos,$iCount=strlen($body);$i<$iCount;++$i) {
1710ad65 2110 $char = $body[$i];
7e2ff844 2111 switch ($char) {
2112 case '<':
10c0caeb 2113 $sToken = $char;
7e2ff844 2114 break;
2115 case '/':
2116 if ($sToken == '<') {
2117 $sToken .= $char;
2118 $bEndTag = true;
2119 } else {
2120 $content .= $char;
2121 }
2122 break;
2123 case '>':
2124 if ($bEndTag) {
2125 $sToken .= $char;
2126 if (preg_match('/\<\/\s*style\s*\>/i',$sToken,$aMatch)) {
2127 $newpos = $i + 1;
2128 $bSucces = true;
2129 break 2;
2130 } else {
2131 $content .= $sToken;
2132 }
2133 $bEndTag = false;
2134 } else {
2135 $content .= $char;
2136 }
2137 break;
2138 case '!':
2139 if ($sToken == '<') {
2140 // possible comment
1710ad65 2141 if (isset($body[$i+2]) && substr($body,$i,3) == '!--') {
7e2ff844 2142 $i = strpos($body,'-->',$i+3);
2b665f28 2143 if ($i === false) { // no end comment
2144 $i = strlen($body);
2145 }
7e2ff844 2146 $sToken = '';
2147 }
2148 } else {
2149 $content .= $char;
2150 }
2151 break;
2152 default:
2153 if ($bEndTag) {
2154 $sToken .= $char;
2155 } else {
2156 $content .= $char;
2157 }
2158 break;
2159 }
2160 }
2161 if ($bSucces == FALSE){
7d06541f 2162 return array(FALSE, strlen($body));
2163 }
7e2ff844 2164
2b665f28 2165
2166
691a2d25 2167 /**
9ebace19 2168 * First look for general BODY style declaration, which would be
2169 * like so:
2170 * body {background: blah-blah}
2171 * and change it to .bodyclass so we can just assign it to a <div>
2172 */
7732acc6 2173 // $content = preg_replace("|body(\s*\{.*?\})|si", ".bodyclass\\1", $content);
d2f3a6fb 2174 // Nah, this is even better - try to preface all CSS selectors with
2175 // our <div> class ID "bodyclass" then correct generic "body" selectors
2176 // TODO: this works pretty good but breaks stuff like this:
2177 // @media print { body { font-size: 10pt; } }
2178 // but there isn't an easy way to make this regex skip @media
2179 // definitions... though lots of the ones in the wild will be
2180 // correctly handled because they tend to end with a parenthesis, like:
2181 // @media screen and (max-width:480px) { ...
2182 $content = preg_replace('/([a-z0-9._-][a-z0-9 >+~|:._-]*\s*(?:,|{.*?}))/si', '.bodyclass $1', $content);
2183 $content = str_replace('.bodyclass body', '.bodyclass', $content);
6ab8321f 2184
2185 global $use_transparent_security_image;
2186 if ($use_transparent_security_image) $secremoveimg = '../images/spacer.png';
2187 else $secremoveimg = '../images/' . _("sec_remove_eng.png");
2188
691a2d25 2189 /**
0493ed11 2190 * Fix url('blah') declarations.
2191 */
2192 // $content = preg_replace("|url\s*\(\s*([\'\"])\s*\S+script\s*:.*?([\'\"])\s*\)|si",
2193 // "url(\\1$secremoveimg\\2)", $content);
2b665f28 2194
567dc524 2195 // first check for 8bit sequences and disallowed control characters
2196 if (preg_match('/[\16-\37\200-\377]+/',$content)) {
2197 $content = '<!-- style block removed by html filter due to presence of 8bit characters -->';
2198 return array($content, $newpos);
2199 }
2200
2b665f28 2201 // IE Sucks hard. We have a special function for it.
2202 sq_fixIE_idiocy($content);
2203
2204 // remove @import line
2205 $content = preg_replace("/^\s*(@import.*)$/mi","\n<!-- @import rules forbidden -->\n",$content);
2206
5b4884be 2207 // translate ur\l and variations (IE parses that)
2b665f28 2208 // TODO check if the sq_fixIE_idiocy function already handles this.
5b4884be 2209 $content = preg_replace("/(\\\\)?u(\\\\)?r(\\\\)?l(\\\\)?/i", 'url', $content);
567dc524 2210 preg_match_all("/url\s*\((.+)\)/si",$content,$aMatch);
2211 if (count($aMatch)) {
2212 $aValue = $aReplace = array();
2213 foreach($aMatch[1] as $sMatch) {
2214 // url value
2215 $urlvalue = $sMatch;
2216 sq_fix_url('style',$urlvalue, $message, $id, $mailbox,"'");
2217 $aValue[] = $sMatch;
2218 $aReplace[] = $urlvalue;
0493ed11 2219 }
567dc524 2220 $content = str_replace($aValue,$aReplace,$content);
691a2d25 2221 }
567dc524 2222
9ebace19 2223 /**
2224 * Remove any backslashes, entities, and extraneous whitespace.
2225 */
0493ed11 2226 $contentTemp = $content;
2227 sq_defang($contentTemp);
2228 sq_unspace($contentTemp);
a3daaaf3 2229
691a2d25 2230 /**
8bd0068d 2231 * Fix stupid css declarations which lead to vulnerabilities
2232 * in IE.
39352565 2233 *
2234 * Also remove "position" attribute, as it can easily be set
2235 * to "fixed" or "absolute" with "left" and "top" attributes
2236 * of zero, taking over the whole content frame. It can also
2237 * be set to relative and move itself anywhere it wants to,
2238 * displaying content in areas it shouldn't be allowed to touch.
8bd0068d 2239 */
caf0ab1d 2240 $match = Array('/\/\*.*\*\//', // removes /* blah blah */
5db90261 2241 '/expression/i',
0493ed11 2242 '/behaviou*r/i',
2243 '/binding/i',
2b665f28 2244 '/include-source/i',
2245 '/javascript/i',
39352565 2246 '/script/i',
2247 '/position/i');
2248 $replace = Array('','idiocy', 'idiocy', 'idiocy', 'idiocy', 'idiocy', 'idiocy', '');
0493ed11 2249 $contentNew = preg_replace($match, $replace, $contentTemp);
2250 if ($contentNew !== $contentTemp) {
2251 // insecure css declarations are used. From now on we don't care
2252 // anymore if the css is destroyed by sq_deent, sq_unspace or sq_unbackslash
2253 $content = $contentNew;
2254 }
7d06541f 2255 return array($content, $newpos);
691a2d25 2256}
a3daaaf3 2257
0493ed11 2258
691a2d25 2259/**
8bd0068d 2260 * This function converts cid: url's into the ones that can be viewed in
2261 * the browser.
2262 *
2263 * @param $message the message object
2264 * @param $id the message id
2265 * @param $cidurl the cid: url.
2266 * @param $mailbox the message mailbox
2267 * @return a string with a http-friendly url
2268 */
b3af12ef 2269function sq_cid2http($message, $id, $cidurl, $mailbox){
691a2d25 2270 /**
8bd0068d 2271 * Get rid of quotes.
2272 */
691a2d25 2273 $quotchar = substr($cidurl, 0, 1);
2dd879b8 2274 if ($quotchar == '"' || $quotchar == "'"){
2275 $cidurl = str_replace($quotchar, "", $cidurl);
2276 } else {
2277 $quotchar = '';
2278 }
691a2d25 2279 $cidurl = substr(trim($cidurl), 4);
0493ed11 2280
2281 $match_str = '/\{.*?\}\//';
2282 $str_rep = '';
2283 $cidurl = preg_replace($match_str, $str_rep, $cidurl);
2284
e5e9381a 2285 $linkurl = find_ent_id($cidurl, $message);
9ebace19 2286 /* in case of non-safe cid links $httpurl should be replaced by a sort of
2287 unsafe link image */
e5e9381a 2288 $httpurl = '';
c8f5f606 2289
8bd0068d 2290 /**
2291 * This is part of a fix for Outlook Express 6.x generating
2292 * cid URLs without creating content-id headers. These images are
2293 * not part of the multipart/related html mail. The html contains
2294 * <img src="cid:{some_id}/image_filename.ext"> references to
2295 * attached images with as goal to render them inline although
2296 * the attachment disposition property is not inline.
2297 */
c8f5f606 2298
2299 if (empty($linkurl)) {
2300 if (preg_match('/{.*}\//', $cidurl)) {
2301 $cidurl = preg_replace('/{.*}\//','', $cidurl);
2302 if (!empty($cidurl)) {
2303 $linkurl = find_ent_id($cidurl, $message);
2304 }
2305 }
2306 }
f8a1ed5a 2307
c8f5f606 2308 if (!empty($linkurl)) {
fa26ab45 2309 $httpurl = $quotchar . sqm_baseuri() . 'src/download.php?absolute_dl=true&amp;' .
8bd0068d 2310 "passed_id=$id&amp;mailbox=" . urlencode($mailbox) .
2311 '&amp;ent_id=' . $linkurl . $quotchar;
c8f5f606 2312 } else {
2313 /**
2314 * If we couldn't generate a proper img url, drop in a blank image
2315 * instead of sending back empty, otherwise it causes unusual behaviour
2316 */
bc017c1d 2317 $httpurl = $quotchar . SM_PATH . 'images/blank.png' . $quotchar;
e5e9381a 2318 }
f8a1ed5a 2319
691a2d25 2320 return $httpurl;
2321}
2322
2323/**
8bd0068d 2324 * This function changes the <body> tag into a <div> tag since we
2325 * can't really have a body-within-body.
2326 *
2327 * @param $attary an array of attributes and values of <body>
2328 * @param $mailbox mailbox we're currently reading (for cid2http)
2329 * @param $message current message (for cid2http)
2330 * @param $id current message id (for cid2http)
2331 * @return a modified array of attributes to be set for <div>
2332 */
2dd879b8 2333function sq_body2div($attary, $mailbox, $message, $id){
b583c3e8 2334 $me = 'sq_body2div';
3d8371be 2335 $divattary = Array('class' => "'bodyclass'");
b583c3e8 2336 $text = '#000000';
c189a963 2337 $has_bgc_stl = $has_txt_stl = false;
b583c3e8 2338 $styledef = '';
691a2d25 2339 if (is_array($attary) && sizeof($attary) > 0){
2340 foreach ($attary as $attname=>$attvalue){
2341 $quotchar = substr($attvalue, 0, 1);
2342 $attvalue = str_replace($quotchar, "", $attvalue);
2343 switch ($attname){
3d8371be 2344 case 'background':
8bd0068d 2345 $attvalue = sq_cid2http($message, $id, $attvalue, $mailbox);
3d8371be 2346 $styledef .= "background-image: url('$attvalue'); ";
2347 break;
2348 case 'bgcolor':
c189a963 2349 $has_bgc_stl = true;
3d8371be 2350 $styledef .= "background-color: $attvalue; ";
2351 break;
2352 case 'text':
c189a963 2353 $has_txt_stl = true;
3d8371be 2354 $styledef .= "color: $attvalue; ";
2355 break;
691a2d25 2356 }
a3daaaf3 2357 }
c189a963 2358 // Outlook defines a white bgcolor and no text color. This can lead to
2359 // white text on a white bg with certain themes.
2360 if ($has_bgc_stl && !$has_txt_stl) {
2361 $styledef .= "color: $text; ";
2362 }
691a2d25 2363 if (strlen($styledef) > 0){
1710ad65 2364 $divattary["style"] = "\"$styledef\"";
691a2d25 2365 }
2366 }
2367 return $divattary;
2368}
a3daaaf3 2369
691a2d25 2370/**
8bd0068d 2371 * This is the main function and the one you should actually be calling.
2372 * There are several variables you should be aware of an which need
2373 * special description.
2374 *
2375 * Since the description is quite lengthy, see it here:
2376 * http://linux.duke.edu/projects/mini/htmlfilter/
2377 *
2378 * @param $body the string with HTML you wish to filter
2379 * @param $tag_list see description above
2380 * @param $rm_tags_with_content see description above
2381 * @param $self_closing_tags see description above
2382 * @param $force_tag_closing see description above
2383 * @param $rm_attnames see description above
2384 * @param $bad_attvals see description above
2385 * @param $add_attr_to_tag see description above
2386 * @param $message message object
2387 * @param $id message id
dfc64d14 2388 * @param $recursively_called boolean flag for recursive calls into this function (optional; default FALSE)
8bd0068d 2389 * @return sanitized html safe to show on your pages.
2390 */
da2415c1 2391function sq_sanitize($body,
8bd0068d 2392 $tag_list,
2393 $rm_tags_with_content,
2394 $self_closing_tags,
2395 $force_tag_closing,
2396 $rm_attnames,
2397 $bad_attvals,
2398 $add_attr_to_tag,
2399 $message,
2400 $id,
dfc64d14 2401 $mailbox,
2402 $recursively_called=FALSE
8bd0068d 2403 ){
b583c3e8 2404 $me = 'sq_sanitize';
dfc64d14 2405
2406 /**
2407 * See if tag_list is of tags to remove or tags to allow.
2408 * false means remove these tags
2409 * true means allow these tags
2410 */
2411 $orig_tag_list = $tag_list;
7d06541f 2412 $rm_tags = array_shift($tag_list);
dfc64d14 2413
691a2d25 2414 /**
8bd0068d 2415 * Normalize rm_tags and rm_tags_with_content.
2416 */
7d06541f 2417 @array_walk($tag_list, 'sq_casenormalize');
691a2d25 2418 @array_walk($rm_tags_with_content, 'sq_casenormalize');
2419 @array_walk($self_closing_tags, 'sq_casenormalize');
dfc64d14 2420
691a2d25 2421 $curpos = 0;
2422 $open_tags = Array();
2dd879b8 2423 $trusted = "\n<!-- begin sanitized html -->\n";
691a2d25 2424 $skip_content = false;
bb8d0799 2425 /**
8bd0068d 2426 * Take care of netscape's stupid javascript entities like
2427 * &{alert('boo')};
2428 */
bb8d0799 2429 $body = preg_replace("/&(\{.*?\};)/si", "&amp;\\1", $body);
691a2d25 2430
7d06541f 2431 while (($curtag = sq_getnxtag($body, $curpos)) != FALSE){
691a2d25 2432 list($tagname, $attary, $tagtype, $lt, $gt) = $curtag;
dfc64d14 2433
2434 /**
2435 * RCDATA and RAWTEXT tags are handled differently:
2436 * next instance of closing tag is used, whether or not
2437 * the HTML is well formed before that
2438 */
2439 global $rcdata_rawtext_tags;
2440 if (!$recursively_called
2441 && in_array($tagname, $rcdata_rawtext_tags)
2442 && $tagtype === 1){
2443 $closing_tag = false;
2444 $closing_tag_offset = $curpos;
2445 // seek out the closing tag for the current RCDATA/RAWTEXT tag
2446 while (1) {
2447 // first we need to move forward to next available closing tag
2448 // (intentionally leave off the closing > and let sq_getnxtag() validate a proper tag syntax)
2449 $next_tag = sq_findnxreg($body, $closing_tag_offset, "</\s*$tagname");
2450 if ($next_tag === false) {
2451 $closing_tag = false;
2452 break;
2453 }
2454 // but then we have to make sure it's a well-formed tag
2455 $closing_tag = sq_getnxtag($body, $next_tag[0]);
2456 if ($closing_tag === false)
2457 break;
2458 else if ($closing_tag[0] !== false
2459 // these should be redundant
2460 && $closing_tag[0] === $tagname && $closing_tag[2] === 2) {
2461 $trusted .= sq_sanitize(substr($body, $curpos, $closing_tag[4] - $curpos + 1),
2462 $orig_tag_list, $rm_tags_with_content, $self_closing_tags,
2463 $force_tag_closing, $rm_attnames, $bad_attvals, $add_attr_to_tag,
2464 $message, $id, $mailbox, true);
2465 $curpos = $closing_tag[4] + 1;
2466 continue 2;
2467 }
2468 $closing_tag_offset = $next_tag[0] + 1;
2469 }
2470 if ($closing_tag === false)
2471 { /* no-op... there was no closing tag for this RCDATA/RAWTEXT tag - we could probably set $curpos to the end of $body, but this HTML is malformed anyway and should just fall apart on its own */ }
2472 }
2473
691a2d25 2474 $free_content = substr($body, $curpos, $lt-$curpos);
2475 /**
8bd0068d 2476 * Take care of <style>
2477 */
7d06541f 2478 if ($tagname == "style" && $tagtype == 1){
da2415c1 2479 list($free_content, $curpos) =
e60a299a 2480 sq_fixstyle($body, $gt+1, $message, $id, $mailbox);
7d06541f 2481 if ($free_content != FALSE){
1d398b1d 2482 if ( !empty($attary) ) {
2483 $attary = sq_fixatts($tagname,
2484 $attary,
2485 $rm_attnames,
2486 $bad_attvals,
2487 $add_attr_to_tag,
2488 $message,
2489 $id,
2490 $mailbox
2491 );
2492 }
7d06541f 2493 $trusted .= sq_tagprint($tagname, $attary, $tagtype);
2494 $trusted .= $free_content;
2495 $trusted .= sq_tagprint($tagname, false, 2);
2496 }
2497 continue;
691a2d25 2498 }
2499 if ($skip_content == false){
2500 $trusted .= $free_content;
691a2d25 2501 }
2502 if ($tagname != FALSE){
2503 if ($tagtype == 2){
2504 if ($skip_content == $tagname){
2505 /**
8bd0068d 2506 * Got to the end of tag we needed to remove.
2507 */
691a2d25 2508 $tagname = false;
2509 $skip_content = false;
2510 } else {
2511 if ($skip_content == false){
c828931c 2512 if ($tagname == "body"){
2513 $tagname = "div";
2dd879b8 2514 }
1710ad65 2515 if (isset($open_tags[$tagname]) &&
2516 $open_tags[$tagname] > 0){
2517 $open_tags[$tagname]--;
691a2d25 2518 } else {
2dd879b8 2519 $tagname = false;
691a2d25 2520 }
691a2d25 2521 }
2522 }
2523 } else {
2524 /**
8bd0068d 2525 * $rm_tags_with_content
2526 */
691a2d25 2527 if ($skip_content == false){
2528 /**
8bd0068d 2529 * See if this is a self-closing type and change
2530 * tagtype appropriately.
2531 */
691a2d25 2532 if ($tagtype == 1
8bd0068d 2533 && in_array($tagname, $self_closing_tags)){
2dd879b8 2534 $tagtype = 3;
691a2d25 2535 }
2536 /**
8bd0068d 2537 * See if we should skip this tag and any content
2538 * inside it.
2539 */
691a2d25 2540 if ($tagtype == 1 &&
8bd0068d 2541 in_array($tagname, $rm_tags_with_content)){
691a2d25 2542 $skip_content = $tagname;
2543 } else {
da2415c1 2544 if (($rm_tags == false
8bd0068d 2545 && in_array($tagname, $tag_list)) ||
2546 ($rm_tags == true &&
2547 !in_array($tagname, $tag_list))){
691a2d25 2548 $tagname = false;
2549 } else {
2dd879b8 2550 /**
8bd0068d 2551 * Convert body into div.
2552 */
2dd879b8 2553 if ($tagname == "body"){
2554 $tagname = "div";
da2415c1 2555 $attary = sq_body2div($attary, $mailbox,
8bd0068d 2556 $message, $id);
2dd879b8 2557 }
691a2d25 2558 if ($tagtype == 1){
1710ad65 2559 if (isset($open_tags[$tagname])){
2560 $open_tags[$tagname]++;
691a2d25 2561 } else {
1710ad65 2562 $open_tags[$tagname]=1;
691a2d25 2563 }
2564 }
2565 /**
8bd0068d 2566 * This is where we run other checks.
2567 */
691a2d25 2568 if (is_array($attary) && sizeof($attary) > 0){
2569 $attary = sq_fixatts($tagname,
8bd0068d 2570 $attary,
2571 $rm_attnames,
2572 $bad_attvals,
2573 $add_attr_to_tag,
2574 $message,
2575 $id,
2576 $mailbox
2577 );
691a2d25 2578 }
2579 }
2580 }
691a2d25 2581 }
2582 }
2583 if ($tagname != false && $skip_content == false){
2584 $trusted .= sq_tagprint($tagname, $attary, $tagtype);
2585 }
691a2d25 2586 }
2587 $curpos = $gt+1;
a3daaaf3 2588 }
691a2d25 2589 $trusted .= substr($body, $curpos, strlen($body)-$curpos);
2590 if ($force_tag_closing == true){
2591 foreach ($open_tags as $tagname=>$opentimes){
2592 while ($opentimes > 0){
2593 $trusted .= '</' . $tagname . '>';
2594 $opentimes--;
2595 }
2596 }
2597 $trusted .= "\n";
2598 }
2599 $trusted .= "<!-- end sanitized html -->\n";
2600 return $trusted;
2601}
451f74a2 2602
691a2d25 2603/**
8bd0068d 2604 * This is a wrapper function to call html sanitizing routines.
2605 *
2606 * @param $body the body of the message
2607 * @param $id the id of the message
c189a963 2608
2609 * @param $message
2610 * @param $mailbox
2611 * @param boolean $take_mailto_links When TRUE, converts mailto: links
2612 * into internal SM compose links
2613 * (optional; default = TRUE)
8bd0068d 2614 * @return a string with html safe to display in the browser.
2615 */
c189a963 2616function magicHTML($body, $id, $message, $mailbox = 'INBOX', $take_mailto_links =true) {
2617
202bcbcc 2618 // require_once(SM_PATH . 'functions/url_parser.php'); // for $MailTo_PReg_Match
c189a963 2619
691a2d25 2620 global $attachment_common_show_images, $view_unsafe_images,
dfc64d14 2621 $has_unsafe_images, $allow_svg_display, $rcdata_rawtext_tags,
2622 $remove_rcdata_rawtext_tags_and_content;
2623
2624 $rcdata_rawtext_tags = array(
2625 "noscript",
2626 "noframes",
2627 "noembed",
2628 "textarea",
2629 // also "title", "xmp", "script", "iframe", "plaintext" which we already remove below
2630 );
2631
691a2d25 2632 /**
8bd0068d 2633 * Don't display attached images in HTML mode.
2b665f28 2634 *
d0187bd6 2635 * SB: why?
8bd0068d 2636 */
691a2d25 2637 $attachment_common_show_images = false;
2638 $tag_list = Array(
dfc64d14 2639 false, // remove these tags
8bd0068d 2640 "meta",
2641 "html",
2642 "head",
2643 "base",
2644 "link",
2645 "frame",
2646 "iframe",
2647 "plaintext",
caf0ab1d 2648 "marquee",
8bd0068d 2649 );
691a2d25 2650
2651 $rm_tags_with_content = Array(
8bd0068d 2652 "script",
caf0ab1d 2653 "object",
8bd0068d 2654 "applet",
2655 "embed",
2656 "title",
2657 "frameset",
0493ed11 2658 "xmp",
caf0ab1d 2659 "xml",
8bd0068d 2660 );
c88cef21 2661 if (!$allow_svg_display)
caf0ab1d 2662 $rm_tags_with_content[] = 'svg';
dfc64d14 2663 /**
2664 * SquirrelMail will parse RCDATA and RAWTEXT tags and handle them as the special
2665 * case that they are, but if you prefer to remove them and their contents entirely
2666 * (in most cases, should be a safe thing with minimal impact), you can add the
2667 * following to config/config_local.php
2668 * $remove_rcdata_rawtext_tags_and_content = TRUE;
2669 */
2670 if ($remove_rcdata_rawtext_tags_and_content)
2671 $rm_tags_with_content = array_merge($rm_tags_with_content, $rcdata_rawtext_tags);
691a2d25 2672
2673 $self_closing_tags = Array(
8bd0068d 2674 "img",
2675 "br",
2676 "hr",
2677 "input",
caf0ab1d 2678 "outbind",
8bd0068d 2679 );
691a2d25 2680
2dd879b8 2681 $force_tag_closing = true;
691a2d25 2682
2683 $rm_attnames = Array(
8bd0068d 2684 "/.*/" =>
2685 Array(
2686 "/target/i",
2687 "/^on.*/i",
2688 "/^dynsrc/i",
2689 "/^data.*/i",
caf0ab1d 2690 "/^lowsrc.*/i",
8bd0068d 2691 )
2692 );
691a2d25 2693
6ab8321f 2694 global $use_transparent_security_image;
2695 if ($use_transparent_security_image) $secremoveimg = '../images/spacer.png';
2696 else $secremoveimg = '../images/' . _("sec_remove_eng.png");
2697
691a2d25 2698 $bad_attvals = Array(
8bd0068d 2699 "/.*/" =>
691a2d25 2700 Array(
0a6ec9b5 2701 "/^src|background/i" =>
8bd0068d 2702 Array(
691a2d25 2703 Array(
8bd0068d 2704 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si",
2705 "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si",
2706 "/^([\'\"])\s*about\s*:.*([\'\"])/si"
691a2d25 2707 ),
8bd0068d 2708 Array(
2709 "\\1$secremoveimg\\2",
2710 "\\1$secremoveimg\\2",
2711 "\\1$secremoveimg\\2",
8bd0068d 2712 )
2713 ),
0a6ec9b5 2714 "/^href|action/i" =>
8bd0068d 2715 Array(
0a6ec9b5 2716 Array(
8bd0068d 2717 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si",
2718 "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si",
2719 "/^([\'\"])\s*about\s*:.*([\'\"])/si"
0a6ec9b5 2720 ),
691a2d25 2721 Array(
8bd0068d 2722 "\\1#\\1",
2723 "\\1#\\1",
2724 "\\1#\\1"
02474e43 2725 )
8bd0068d 2726 ),
2727 "/^style/i" =>
2728 Array(
2729 Array(
5db90261 2730 "/\/\*.*\*\//",
8bd0068d 2731 "/expression/i",
2732 "/binding/i",
2733 "/behaviou*r/i",
2734 "/include-source/i",
39352565 2735
2736 // position:relative can also be exploited
2737 // to put content outside of email body area
2738 // and position:fixed is similarly exploitable
2739 // as position:absolute, so we'll remove it
2740 // altogether....
2741 //
2742 // Does this screw up legitimate HTML messages?
2743 // If so, the only fix I see is to allow position
2744 // attributes (any values? I think we still have
2745 // to block static and fixed) only if $use_iframe
2746 // is enabled (1.5.0+)
2747 //
2748 // was: "/position\s*:\s*absolute/i",
2749 //
2750 "/position\s*:/i",
2751
1d935bc2 2752 "/(\\\\)?u(\\\\)?r(\\\\)?l(\\\\)?/i",
8bd0068d 2753 "/url\s*\(\s*([\'\"])\s*\S+script\s*:.*([\'\"])\s*\)/si",
2754 "/url\s*\(\s*([\'\"])\s*mocha\s*:.*([\'\"])\s*\)/si",
2755 "/url\s*\(\s*([\'\"])\s*about\s*:.*([\'\"])\s*\)/si",
39352565 2756 "/(.*)\s*:\s*url\s*\(\s*([\'\"]*)\s*\S+script\s*:.*([\'\"]*)\s*\)/si",
8bd0068d 2757 ),
2758 Array(
5db90261 2759 "",
8bd0068d 2760 "idiocy",
2761 "idiocy",
2762 "idiocy",
2763 "idiocy",
567dc524 2764 "idiocy",
5b4884be 2765 "url",
8bd0068d 2766 "url(\\1#\\1)",
2767 "url(\\1#\\1)",
2768 "url(\\1#\\1)",
8bd0068d 2769 "\\1:url(\\2#\\3)"
2770 )
691a2d25 2771 )
8bd0068d 2772 )
691a2d25 2773 );
9ebace19 2774
d31f73f1 2775 // If there's no "view_unsafe_images" variable in the URL, turn unsafe
2776 // images off by default.
afbf184c 2777 sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET, FALSE);
9ebace19 2778
691a2d25 2779 if (!$view_unsafe_images){
2780 /**
8bd0068d 2781 * Remove any references to http/https if view_unsafe_images set
2782 * to false.
2783 */
1710ad65 2784 array_push($bad_attvals['/.*/']['/^src|background/i'][0],
8bd0068d 2785 '/^([\'\"])\s*https*:.*([\'\"])/si');
1710ad65 2786 array_push($bad_attvals['/.*/']['/^src|background/i'][1],
8bd0068d 2787 "\\1$secremoveimg\\1");
1710ad65 2788 array_push($bad_attvals['/.*/']['/^style/i'][0],
7ef0b415 2789 '/url\([\'\"]?https?:[^\)]*[\'\"]?\)/si');
1710ad65 2790 array_push($bad_attvals['/.*/']['/^style/i'][1],
8bd0068d 2791 "url(\\1$secremoveimg\\1)");
691a2d25 2792 }
451f74a2 2793
691a2d25 2794 $add_attr_to_tag = Array(
8bd0068d 2795 "/^a$/i" =>
c25f2fbb 2796 Array('target'=>'"_blank"',
02474e43 2797 'title'=>'"'._("This external link will open in a new window").'"'
8bd0068d 2798 )
2799 );
da2415c1 2800 $trusted = sq_sanitize($body,
8bd0068d 2801 $tag_list,
2802 $rm_tags_with_content,
2803 $self_closing_tags,
2804 $force_tag_closing,
2805 $rm_attnames,
2806 $bad_attvals,
2807 $add_attr_to_tag,
2808 $message,
2809 $id,
2810 $mailbox
2811 );
567dc524 2812 if (strpos($trusted,$secremoveimg)){
691a2d25 2813 $has_unsafe_images = true;
da2415c1 2814 }
c189a963 2815
2816 // we want to parse mailto's in HTML output, change to SM compose links
2817 // this is a modified version of code from url_parser.php... but Marc is
2818 // right: we need a better filtering implementation; adding this randomly
2819 // here is not a great solution
2820 //
2821 if ($take_mailto_links) {
2822 // parseUrl($trusted); // this even parses URLs inside of tags... too aggressive
2823 global $MailTo_PReg_Match;
7196da62 2824 // some mailers (Microsoft, surprise surprise) produce mailto strings without being
2825 // inside an anchor (link) tag, so we have to make sure the regex looks for the
2826 // quote before mailto, and we'll also try to convert the non-links back into links
2827 $MailTo_PReg_Match = '/([\'"])?mailto:' . substr($MailTo_PReg_Match, 1) ;
c189a963 2828 if ((preg_match_all($MailTo_PReg_Match, $trusted, $regs)) && ($regs[0][0] != '')) {
2829 foreach ($regs[0] as $i => $mailto_before) {
7196da62 2830 $mailto_params = $regs[11][$i];
2831
2832 // get rid of any leading quote we may have captured but don't care about
2833 //
2834 $mailto_before = ltrim($mailto_before, '"\'');
2835
c189a963 2836 // get rid of any tailing quote since we have to add send_to to the end
2837 //
7196da62 2838 $mailto_before = rtrim($mailto_before, '"\'');
2839 $mailto_params = rtrim($mailto_params, '"\'');
c189a963 2840
7196da62 2841 if ($regs[2][$i]) { //if there is an email addr before '?', we need to merge it with the params
2842 $to = 'to=' . $regs[2][$i];
c189a963 2843 if (strpos($mailto_params, 'to=') > -1) //already a 'to='
2844 $mailto_params = str_replace('to=', $to . '%2C%20', $mailto_params);
2845 else {
2846 if ($mailto_params) //already some params, append to them
2847 $mailto_params .= '&amp;' . $to;
2848 else
2849 $mailto_params .= '?' . $to;
2850 }
2851 }
2852
2853 $url_str = preg_replace(array('/to=/i', '/(?<!b)cc=/i', '/bcc=/i'), array('send_to=', 'send_to_cc=', 'send_to_bcc='), $mailto_params);
2854
2855 // we'll already have target=_blank, no need to allow comp_in_new
2856 // here (which would be a lot more work anyway)
2857 //
2858 global $compose_new_win;
2859 $temp_comp_in_new = $compose_new_win;
2860 $compose_new_win = 0;
2861 $comp_uri = makeComposeLink('src/compose.php' . $url_str, $mailto_before);
2862 $compose_new_win = $temp_comp_in_new;
2863
2864 // remove <a href=" and anything after the next quote (we only
2865 // need the uri, not the link HTML) in compose uri
2866 //
7196da62 2867 // but only do this if the original mailto was in a real anchor tag
2868 //
2869 if (!empty($regs[1][$i])) {
2870 $comp_uri = substr($comp_uri, 9);
2871 $comp_uri = substr($comp_uri, 0, strpos($comp_uri, '"', 1));
2872 }
c189a963 2873 $trusted = str_replace($mailto_before, $comp_uri, $trusted);
2874 }
2875 }
2876 }
2877
691a2d25 2878 return $trusted;
451f74a2 2879}
a4a70693 2880
da2415c1 2881/**
8bd0068d 2882 * function SendDownloadHeaders - send file to the browser
2883 *
2884 * Original Source: SM core src/download.php
2885 * moved here to make it available to other code, and separate
2886 * front end from back end functionality.
2887 *
2888 * @param string $type0 first half of mime type
2889 * @param string $type1 second half of mime type
2890 * @param string $filename filename to tell the browser for downloaded file
2891 * @param boolean $force whether to force the download dialog to pop
2892 * @param optional integer $filesize send the Content-Header and length to the browser
2893 * @return void
2894 */
02474e43 2895function SendDownloadHeaders($type0, $type1, $filename, $force, $filesize=0) {
2896 global $languages, $squirrelmail_language;
cfffd60b 2897 $isIE = $isIE6plus = false;
02474e43 2898
2899 sqgetGlobalVar('HTTP_USER_AGENT', $HTTP_USER_AGENT, SQ_SERVER);
2900
2901 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
8bd0068d 2902 strstr($HTTP_USER_AGENT, 'Opera') === false) {
cfffd60b 2903 $isIE = true;
02474e43 2904 }
2905
cfffd60b 2906 if (preg_match('/compatible; MSIE ([0-9]+)/', $HTTP_USER_AGENT, $match) &&
2907 ((int)$match[1]) >= 6 && strstr($HTTP_USER_AGENT, 'Opera') === false) {
2908 $isIE6plus = true;
02474e43 2909 }
2910
2911 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
8bd0068d 2912 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_downloadfilename')) {
02474e43 2913 $filename =
8bd0068d 2914 call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_downloadfilename', $filename, $HTTP_USER_AGENT);
02474e43 2915 } else {
1a4bc4a6 2916 $filename = preg_replace('/[\\\\\/:*?"<>|;]/', '_', str_replace('&nbsp;', ' ', $filename));
02474e43 2917 }
2918
2919 // A Pox on Microsoft and it's Internet Explorer!
2920 //
2921 // IE has lots of bugs with file downloads.
2922 // It also has problems with SSL. Both of these cause problems
2923 // for us in this function.
2924 //
2925 // See this article on Cache Control headers and SSL
2926 // http://support.microsoft.com/default.aspx?scid=kb;en-us;323308
2927 //
2928 // The best thing you can do for IE is to upgrade to the latest
2929 // version
2930 //set all the Cache Control Headers for IE
2931 if ($isIE) {
2932 $filename=rawurlencode($filename);
2933 header ("Pragma: public");
8bd0068d 2934 header ("Cache-Control: no-store, max-age=0, no-cache, must-revalidate"); // HTTP/1.1
c2503a4e 2935 // does nothing - see: https://blogs.msdn.microsoft.com/ieinternals/2009/07/20/internet-explorers-cache-control-extensions/
2936 // header ("Cache-Control: post-check=0, pre-check=0", false);
8bd0068d 2937 header ("Cache-Control: private");
02474e43 2938
2939 //set the inline header for IE, we'll add the attachment header later if we need it
2940 header ("Content-Disposition: inline; filename=$filename");
2941 }
2942
2943 if (!$force) {
2944 // Try to show in browser window
2945 header ("Content-Disposition: inline; filename=\"$filename\"");
2946 header ("Content-Type: $type0/$type1; name=\"$filename\"");
2947 } else {
2948 // Try to pop up the "save as" box
2949
2950 // IE makes this hard. It pops up 2 save boxes, or none.
2951 // http://support.microsoft.com/support/kb/articles/Q238/5/88.ASP
2952 // http://support.microsoft.com/default.aspx?scid=kb;EN-US;260519
2953 // But, according to Microsoft, it is "RFC compliant but doesn't
2954 // take into account some deviations that allowed within the
2955 // specification." Doesn't that mean RFC non-compliant?
2956 // http://support.microsoft.com/support/kb/articles/Q258/4/52.ASP
2957
2958 // all browsers need the application/octet-stream header for this
2959 header ("Content-Type: application/octet-stream; name=\"$filename\"");
2960
2961 // http://support.microsoft.com/support/kb/articles/Q182/3/15.asp
2962 // Do not have quotes around filename, but that applied to
2963 // "attachment"... does it apply to inline too?
2964 header ("Content-Disposition: attachment; filename=\"$filename\"");
2965
cfffd60b 2966 if ($isIE && !$isIE6plus) {
02474e43 2967 // This combination seems to work mostly. IE 5.5 SP 1 has
2968 // known issues (see the Microsoft Knowledge Base)
2969
2970 // This works for most types, but doesn't work with Word files
2971 header ("Content-Type: application/download; name=\"$filename\"");
7e2ff844 2972 header ("Content-Type: application/force-download; name=\"$filename\"");
02474e43 2973 // These are spares, just in case. :-)
2974 //header("Content-Type: $type0/$type1; name=\"$filename\"");
2975 //header("Content-Type: application/x-msdownload; name=\"$filename\"");
2976 //header("Content-Type: application/octet-stream; name=\"$filename\"");
7e2ff844 2977 } else if ($isIE) {
2978 // This is to prevent IE for MIME sniffing and auto open a file in IE
2979 header ("Content-Type: application/force-download; name=\"$filename\"");
02474e43 2980 } else {
2981 // another application/octet-stream forces download for Netscape
2982 header ("Content-Type: application/octet-stream; name=\"$filename\"");
2983 }
2984 }
2985
2986 //send the content-length header if the calling function provides it
2987 if ($filesize > 0) {
2988 header("Content-Length: $filesize");
2989 }
07c49f57 2990
8d863f64 2991} // end fn SendDownloadHeaders