fsf changes, meant to be rebased on upstream
[squirrelmail.git] / functions / mime.php
1 <?php
2
3 /**
4 * mime.php
5 *
6 * This contains the functions necessary to detect and decode MIME
7 * messages.
8 *
9 * @copyright 1999-2022 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 */
14
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
32 functions/imap.php
33 functions/attachment_common.php
34 functions/display_messages.php
35
36 magicHtml => url_parser
37 translateText => url_parser
38
39 */
40
41
42 /* -------------------------------------------------------------------------- */
43 /* MIME DECODING */
44 /* -------------------------------------------------------------------------- */
45
46 /**
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 */
53 function mime_structure ($bodystructure, $flags=array()) {
54
55 /* Isolate the body structure and remove beginning and end parenthesis. */
56 $read = trim(substr ($bodystructure, strpos(strtolower($bodystructure), 'bodystructure') + 13));
57 $read = trim(substr ($read, 0, -1));
58 $i = 0;
59 $msg = Message::parseStructure($read,$i);
60
61 if (!is_object($msg)) {
62 global $color, $mailbox;
63 displayPageHeader( $color, $mailbox );
64 $errormessage = _("SquirrelMail could not decode the bodystructure of the message");
65 $errormessage .= '<br />'._("The bodystructure provided by your IMAP server:").'<br /><br />';
66 $errormessage .= '<pre>' . sm_encode_html_special_chars($read) . '</pre>';
67 plain_error_message( $errormessage );
68 echo '</body></html>';
69 exit;
70 }
71 if (count($flags)) {
72 foreach ($flags as $flag) {
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.
74 $char = strtoupper($flag[1]);
75 switch ($char) {
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 }
95 else if (strtolower($flag) == '$forwarded') {
96 $msg->is_forwarded = true;
97 }
98 break;
99 case 'M':
100 if (strtolower($flag) == '$mdnsent') {
101 $msg->is_mdnsent = true;
102 }
103 break;
104 default:
105 break;
106 }
107 }
108 }
109 // listEntities($msg);
110 return $msg;
111 }
112
113
114
115 /* This starts the parsing of a particular structure. It is called recursively,
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 */
123
124 function mime_fetch_body($imap_stream, $id, $ent_id=1, $fetch_size=0) {
125 /* Do a bit of error correction. If we couldn't find the entity id, just guess
126 * that it is the first one. That is usually the case anyway.
127 */
128
129 if (!$ent_id) {
130 $cmd = "FETCH $id BODY[]";
131 } else {
132 $cmd = "FETCH $id BODY[$ent_id]";
133 }
134
135 if ($fetch_size!=0) $cmd .= "<0.$fetch_size>";
136
137 $data = sqimap_run_command ($imap_stream, $cmd, true, $response, $message, TRUE);
138 do {
139 $topline = trim(array_shift($data));
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.
147
148 $wholemessage = implode('', $data);
149 if (preg_match('/\{([^\}]*)\}/', $topline, $regs)) {
150 $ret = substr($wholemessage, 0, $regs[1]);
151 /* There is some information in the content info header that could be important
152 * in order to parse html messages. Let's get them here.
153 */
154 // if ($ret[0] == '<') {
155 // $data = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent_id.MIME]", true, $response, $message, TRUE);
156 // }
157 } else if (preg_match('/"([^"]*)"/', $topline, $regs)) {
158 $ret = $regs[1];
159 } else if ((stristr($topline, 'nil') !== false) && (empty($wholemessage))) {
160 $ret = $wholemessage;
161 } else {
162 global $where, $what, $mailbox, $passed_id, $startMessage;
163 $par = 'mailbox=' . urlencode($mailbox) . '&amp;passed_id=' . $passed_id;
164 if (isset($where) && isset($what)) {
165 $par .= '&amp;where=' . urlencode($where) . '&amp;what=' . urlencode($what);
166 } else {
167 $par .= '&amp;startMessage=' . $startMessage . '&amp;show_more=0';
168 }
169 $par .= '&amp;response=' . urlencode($response) .
170 '&amp;message=' . urlencode($message) .
171 '&amp;topline=' . urlencode($topline);
172
173 echo '<tt><br />' .
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 />";
183
184 $data = sqimap_run_command ($imap_stream, "FETCH $passed_id BODY[]", true, $response, $message, TRUE);
185 array_shift($data);
186 $wholemessage = implode('', $data);
187
188 $ret = $wholemessage;
189 }
190 return $ret;
191 }
192
193 // TODO: Needs documentation. $ent_id default is usually 1
194 function mime_print_body_lines ($imap_stream, $id, $ent_id, $encoding, $rStream='php://stdout', $force_crlf='') {
195
196 /* Don't kill the connection if the browser is over a dialup
197 * and it would take over 30 seconds to download it.
198 * Don't call set_time_limit in safe mode.
199 */
200
201 if (!ini_get('safe_mode')) {
202 set_time_limit(0);
203 }
204 /* in case of base64 encoded attachments, do not buffer them.
205 Instead, echo the decoded attachment directly to screen */
206 if (strtolower($encoding) == 'base64') {
207 if (!$ent_id) {
208 $query = "FETCH $id BODY[]";
209 } else {
210 $query = "FETCH $id BODY[$ent_id]";
211 }
212 sqimap_run_command($imap_stream,$query,true,$response,$message,TRUE,'sqimap_base64_decode',$rStream,true);
213 } else {
214 $body = mime_fetch_body ($imap_stream, $id, $ent_id);
215 if (is_resource($rStream)) {
216 fputs($rStream,decodeBody($body, $encoding, $force_crlf));
217 } else {
218 echo decodeBody($body, $encoding, $force_crlf);
219 }
220 }
221
222 /*
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 */
232 /*
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 */
236
237 return;
238 }
239
240 /* -[ END MIME DECODING ]----------------------------------------------------------- */
241
242 /* This is here for debugging purposes. It will print out a list
243 * of all the entity IDs that are in the $message object.
244 */
245 function listEntities ($message) {
246 if ($message) {
247 echo "<tt>" . $message->entity_id . ' : ' . $message->type0 . '/' . $message->type1 . ' parent = '. $message->parent->entity_id. '<br />';
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 }
257 }
258 }
259
260 function getPriorityStr($priority) {
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;
285 }
286
287 /* returns a $message object for a particular entity id */
288 function getEntity ($message, $ent_id) {
289 return $message->getEntity($ent_id);
290 }
291
292 /* translateText
293 * Extracted from strings.php 23/03/2002
294 */
295
296 function translateText(&$body, $wrap_at, $charset) {
297 global $where, $what; /* from searching */
298 global $color; /* color theme */
299
300 // require_once(SM_PATH . 'functions/url_parser.php');
301
302 $body_ary = explode("\n", $body);
303 for ($i=0; $i < count($body_ary); $i++) {
304 $line = rtrim($body_ary[$i],"\r");
305
306 if (strlen($line) - 2 >= $wrap_at) {
307 sqWordWrap($line, $wrap_at, $charset);
308 }
309 $line = charset_decode($charset, $line);
310 $line = str_replace("\t", ' ', $line);
311
312 parseUrl ($line);
313
314 $quotes = 0;
315 $pos = 0;
316 $j = strlen($line);
317
318 while ($pos < $j) {
319 if ($line[$pos] == ' ') {
320 $pos++;
321 } else if (strpos($line, '&gt;', $pos) === $pos) {
322 $pos += 4;
323 $quotes++;
324 } else {
325 break;
326 }
327 }
328
329 if ($quotes % 2) {
330 $line = '<span class="quote1">' . $line . '</span>';
331 } elseif ($quotes) {
332 $line = '<span class="quote2">' . $line . '</span>';
333 }
334
335 $body_ary[$i] = $line;
336 }
337 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
338 }
339
340 /**
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.
345 *
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
356 * @return string html formated message text
357 */
358 function formatBody($imap_stream, $message, $color, $wrap_at, $ent_num, $id, $mailbox='INBOX') {
359 /* This if statement checks for the entity to show as the
360 * primary message. To add more of them, just put them in the
361 * order that is their priority.
362 */
363 global $startMessage, $languages, $squirrelmail_language,
364 $show_html_default, $sort, $has_unsafe_images, $passed_ent_id,
365 $use_iframe, $iframe_height, $download_and_unsafe_link,
366 $download_href, $unsafe_image_toggle_href, $unsafe_image_toggle_text,
367 $oTemplate, $nbsp;
368
369 // workaround for not updated config.php
370 if (! isset($use_iframe)) $use_iframe = false;
371
372 // If there's no "view_unsafe_images" variable in the URL, turn unsafe
373 // images off by default.
374 sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET, FALSE);
375
376 $body = '';
377 $urlmailbox = urlencode($mailbox);
378 $body_message = getEntity($message, $ent_num);
379 if (($body_message->header->type0 == 'text') ||
380 ($body_message->header->type0 == 'rfc822')) {
381 $body = mime_fetch_body ($imap_stream, $id, $ent_num);
382 $body = decodeBody($body, $body_message->header->encoding);
383
384 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
385 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
386 if (mb_detect_encoding($body) != 'ASCII') {
387 $body = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode',$body);
388 }
389 }
390
391 /* As of 1.5.2, $body is passed (and modified) by reference */
392 do_hook('message_body', $body);
393
394 /* If there are other types that shouldn't be formatted, add
395 * them here.
396 */
397
398 if ($body_message->header->type1 == 'html') {
399 // Do we need to make an HTML part viewable as non-HTML plain text?
400 if ($show_html_default != 1) {
401 $entity_conv = array('&nbsp;' => ' ',
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",
411 '&gt;' => '>',
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
420 $body = strtr($body, $entity_conv);
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
424 $body = strip_tags($body);
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);
431 $body = trim($body);
432 translateText($body, $wrap_at,
433 $body_message->header->getParameter('charset'));
434 } elseif ($use_iframe) {
435 /**
436 * If we don't add html message between iframe tags,
437 * we must detect unsafe images and modify $has_unsafe_images.
438 */
439 $html_body = magicHTML($body, $id, $message, $mailbox);
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);
442
443 // creating iframe url
444 $iframeurl=sqm_baseuri().'src/view_html.php?'
445 . 'mailbox=' . $urlmailbox
446 . '&amp;passed_id=' . $id
447 . '&amp;ent_id=' . $ent_num
448 . '&amp;view_unsafe_images=' . (int) $view_unsafe_images;
449
450 global $oTemplate;
451 $oTemplate->assign('iframe_url', $iframeurl);
452 $oTemplate->assign('iframe_height', $iframe_height);
453 $oTemplate->assign('html_body', $html_body);
454
455 $body = $oTemplate->fetch('read_html_iframe.tpl');
456 } else {
457 // old way of html rendering
458 /**
459 * convert character set. charset_decode does not remove html special chars
460 * applied by magicHTML functions and does not sanitize them second time if
461 * fourth argument is true.
462 */
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);
468 }
469 } else {
470 translateText($body, $wrap_at,
471 $body_message->header->getParameter('charset'));
472 }
473
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.
485 $download_and_unsafe_link = '';
486
487 // Prepare and build a link for downloading the mail.
488 $link = 'passed_id=' . $id . '&amp;ent_id='.$ent_num.
489 '&amp;mailbox=' . $urlmailbox .'&amp;sort=' . $sort .
490 '&amp;startMessage=' . $startMessage . '&amp;show_more=0';
491 if (isset($passed_ent_id)) {
492 $link .= '&amp;passed_ent_id='.$passed_ent_id;
493 }
494 $download_href = SM_PATH . 'src/download.php?absolute_dl=true&amp;' . $link;
495
496 // Always add the link for downloading the mail as a file to the global
497 // variable.
498 $download_and_unsafe_link .= "$nbsp|$nbsp"
499 . create_hyperlink($download_href, _("Download this as a file"));
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.
505 if ($view_unsafe_images) {
506 $text = _("Hide Unsafe Images");
507 } else {
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 }
514 }
515
516 // Only create a link for unsafe images if there's need for one. If so:
517 // add it to the global variable.
518 if($text != '') {
519 $unsafe_image_toggle_href = SM_PATH . 'src/read_body.php?'.$link;
520 $unsafe_image_toggle_text = $text;
521 $download_and_unsafe_link .= "$nbsp|$nbsp"
522 . create_hyperlink($unsafe_image_toggle_href, $text);
523 }
524 }
525 return $body;
526 }
527
528 /**
529 * Generate attachments array for passing to templates.
530 *
531 * @since 1.5.2
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
536 */
537 function buildAttachmentArray($message, $exclude_id, $mailbox, $id) {
538 global $where, $what, $startMessage, $color, $passed_ent_id,
539 $base_uri, $block_svg_download;
540
541 $att_ar = $message->getAttachments($exclude_id);
542 $urlMailbox = urlencode($mailbox);
543
544 $attachments = array();
545 foreach ($att_ar as $att) {
546 $ent = $att->entity_id;
547 $header = $att->header;
548 $type0 = strtolower($header->type0);
549 $type1 = strtolower($header->type1);
550 if ($block_svg_download && strpos($type1, 'svg') === 0)
551 continue;
552
553 $name = '';
554 $links = array();
555 $links['download link']['text'] = _("Download");
556 $links['download link']['href'] = $base_uri .
557 "src/download.php?absolute_dl=true&amp;passed_id=$id&amp;mailbox=$urlMailbox&amp;ent_id=$ent";
558
559 if ($type0 =='message' && $type1 == 'rfc822') {
560 $default_page = $base_uri . 'src/read_body.php';
561 $rfc822_header = $att->rfc822_header;
562 $filename = $rfc822_header->subject;
563 if (trim( $filename ) == '') {
564 $filename = 'untitled-[' . $ent . ']' ;
565 }
566 $from_o = $rfc822_header->from;
567 if (is_object($from_o)) {
568 $from_name = decodeHeader($from_o->getAddress(false));
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));
576 } else {
577 $from_name = _("Unknown sender");
578 }
579 $description = _("From").': '.$from_name;
580 } else {
581 $default_page = $base_uri . 'src/download.php';
582 $filename = $att->getFilename();
583 if ($header->description) {
584 $description = decodeHeader($header->description);
585 } else {
586 $description = '';
587 }
588 }
589
590 $display_filename = $filename;
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"
597 . "&amp;passed_id=$id&amp;mailbox=$urlMailbox"
598 . '&amp;ent_id='.$ent.$passed_ent_id_link;
599 if ($where && $what) {
600 $defaultlink .= '&amp;where='. urlencode($where).'&amp;what='.urlencode($what);
601 }
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 }
607
608 /* This executes the attachment hook with a specific MIME-type.
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.
612 */
613 // First remember the default link.
614 $defaultlink_orig = $defaultlink;
615
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. */
621 $temp = array(&$links, &$startMessage, &$id, &$urlMailbox, &$ent,
622 &$defaultlink, &$display_filename, &$where, &$what,
623 &$type0, &$type1);
624 do_hook("attachment $type0/$type1", $temp);
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,
631 &$defaultlink, &$display_filename, &$where, &$what,
632 &$type0, &$type1);
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;
638 }
639 do_hook("attachment $type0/*", $temp);
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. */
645 $temp = array(&$links, &$startMessage, &$id, &$urlMailbox, &$ent,
646 &$defaultlink, &$display_filename, &$where, &$what,
647 &$type0, &$type1);
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 }
654 do_hook("attachment */*", $temp);
655
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'] : '';
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
669 $this_attachment['ContentType'] = sm_encode_html_special_chars($type0 .'/'. $type1);
670 $this_attachment['OtherLinks'] = array();
671 foreach ($links as $val) {
672 if ($val['text']==_("Download")) {
673 $this_attachment['DownloadHREF'] = $val['href'];
674 continue;
675 }
676 if ($val['text']==_("View")) {
677 $this_attachment['ViewHREF'] = $val['href'];
678 continue;
679 }
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']))
687 continue;
688
689 $temp = array();
690 $temp['HREF'] = $val['href'];
691 $temp['Text'] = $val['text'];
692 $temp['Extra'] = (empty($val['extra']) ? '' : $val['extra']);
693 $this_attachment['OtherLinks'][] = $temp;
694 }
695 $attachments[] = $this_attachment;
696
697 unset($links);
698 }
699
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 */
718 function formatAttachments($message, $exclude_id, $mailbox, $id) {
719 global $oTemplate;
720
721 $attach = buildAttachmentArray($message, $exclude_id, $mailbox, $id);
722
723 $oTemplate->assign('attachments', $attach);
724 $oTemplate->display('read_attachments.tpl');
725 }
726
727 function sqimap_base64_decode(&$string) {
728
729 // Base64 encoded data goes in pairs of 4 bytes. To achieve on the
730 // fly decoding (to reduce memory usage) you have to check if the
731 // data has incomplete pairs
732
733 // Remove the noise in order to check if the 4 bytes pairs are complete
734 $string = str_replace(array("\r\n","\n", "\r", " "),array('','','',''),$string);
735
736 $sStringRem = '';
737 $iMod = strlen($string) % 4;
738 if ($iMod) {
739 $sStringRem = substr($string,-$iMod);
740 // Check if $sStringRem contains padding characters
741 if (substr($sStringRem,-1) != '=') {
742 $string = substr($string,0,-$iMod);
743 } else {
744 $sStringRem = '';
745 }
746 }
747 $string = base64_decode($string);
748 return $sStringRem;
749 }
750
751 /**
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,
769 * the global $force_crlf_default will
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
780 *
781 * @since 1.0
782 *
783 */
784 function 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);
798
799 $encoding = strtolower($encoding);
800
801 $encoding_handler = do_hook('decode_body', $encoding);
802
803
804 // plugins get first shot at decoding the string
805 //
806 if (!empty($encoding_handler) && function_exists($encoding_handler)) {
807 $string = $encoding_handler('decode', $string);
808
809 } elseif ($encoding == 'quoted-printable' ||
810 $encoding == 'quoted_printable') {
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
819 } elseif ($encoding == 'base64') {
820 $string = base64_decode($string);
821 }
822
823 // All other encodings are returned raw.
824 return $string;
825 }
826
827 /**
828 * Decodes headers
829 *
830 * This function decodes strings that are encoded according to
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
836 * @param boolean $htmlsafe preserve spaces and sanitize html special characters. defaults to true
837 * @param boolean $decide decide if string can be utfencoded. defaults to false
838 * @return string decoded header string
839 */
840 function decodeHeader ($string, $utfencode=true,$htmlsafe=true,$decide=false) {
841 global $languages, $squirrelmail_language,$default_charset, $fix_broken_base64_encoded_messages;
842 if (is_array($string)) {
843 $string = implode("\n", $string);
844 }
845
846 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
847 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decodeheader')) {
848 $string = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decodeheader', $string);
849 // Do we need to return at this point?
850 // return $string;
851 }
852 $i = 0;
853 $iLastMatch = -2;
854 $encoded = true;
855
856 // FIXME: spaces are allowed inside quoted-printable encoding, but the following line will bust up any such encoded strings
857 $aString = explode(' ',$string);
858 $ret = '';
859 foreach ($aString as $chunk) {
860 if ($encoded && $chunk === '') {
861 continue;
862 } elseif ($chunk === '') {
863 $ret .= ' ';
864 continue;
865 }
866 $encoded = false;
867 /* if encoded words are not separated by a linear-space-white we still catch them */
868 $j = $i-1;
869
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) {
873 if ($htmlsafe) {
874 $ret .= '&#32;';
875 } else {
876 $ret .= ' ';
877 }
878 }
879 $iLastMatch = $i;
880 $j = $i;
881 if ($htmlsafe) {
882 $ret .= sm_encode_html_special_chars($res[1]);
883 } else {
884 $ret .= $res[1];
885 }
886 $encoding = ucfirst($res[3]);
887
888 /* decide about valid decoding */
889 if ($decide && is_conversion_safe($res[2])) {
890 $utfencode=true;
891 $can_be_encoded=true;
892 } else {
893 $can_be_encoded=false;
894 }
895 switch ($encoding)
896 {
897 case 'B':
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 }
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 */
911 $ret .= charset_convert($res[2],$replace,$default_charset,$htmlsafe);
912 } else {
913 // convert string to html codes in order to display it
914 $ret .= charset_decode($res[2],$replace);
915 }
916 } else {
917 if ($htmlsafe) {
918 $replace = sm_encode_html_special_chars($replace);
919 }
920 $ret.= $replace;
921 }
922 break;
923 case 'Q':
924 $replace = str_replace('_', ' ', $res[4]);
925 $replace = preg_replace_callback('/=([0-9a-f]{2})/i',
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);
931 if ($utfencode) {
932 if ($can_be_encoded) {
933 /* convert string to different charset,
934 * if functions asks for it (usually in compose)
935 */
936 $replace = charset_convert($res[2], $replace,$default_charset,$htmlsafe);
937 } else {
938 // convert string to html codes in order to display it
939 $replace = charset_decode($res[2], $replace);
940 }
941 } else {
942 if ($htmlsafe) {
943 $replace = sm_encode_html_special_chars($replace);
944 }
945 }
946 $ret .= $replace;
947 break;
948 default:
949 break;
950 }
951 $chunk = $res[5];
952 $encoded = true;
953 }
954 if (!$encoded) {
955 if ($htmlsafe) {
956 $ret .= '&#32;';
957 } else {
958 $ret .= ' ';
959 }
960 }
961
962 if (!$encoded && $htmlsafe) {
963 $ret .= sm_encode_html_special_chars($chunk);
964 } else {
965 $ret .= $chunk;
966 }
967 ++$i;
968 }
969 /* remove the first added space */
970 if ($ret) {
971 if ($htmlsafe) {
972 $ret = substr($ret,5);
973 } else {
974 $ret = substr($ret,1);
975 }
976 }
977
978 return $ret;
979 }
980
981 /**
982 * Encodes header
983 *
984 * Function uses XTRA_CODE _encodeheader function, if such function exists.
985 *
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
988 * looks like it should be encoded.
989 *
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
993 * multibyte character set unsupported by mbstring extension.
994 *
995 * @param string $string header string, that has to be encoded
996 * @return string quoted-printable encoded string
997 * @todo make $mb_charsets system wide constant
998 */
999 function encodeHeader ($string) {
1000 global $default_charset, $languages, $squirrelmail_language;
1001
1002 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1003 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encodeheader')) {
1004 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encodeheader', $string);
1005 }
1006
1007 // Use B encoding for multibyte charsets
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 }
1019
1020 // Encode only if the string contains 8-bit characters or =?
1021 $j = strlen($string);
1022 $max_l = 75 - strlen($default_charset) - 7;
1023 $aRet = array();
1024 $ret = '';
1025 $iEncStart = $enc_init = false;
1026 $cur_l = $iOffset = 0;
1027 for($i = 0; $i < $j; ++$i) {
1028 switch($string[$i])
1029 {
1030 case '"':
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 */
1043 $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
1044 $aRet[] = "=?$default_charset?Q?$ret?=";
1045 $iOffset = $i;
1046 $cur_l = 0;
1047 $ret = '';
1048 $iEncStart = false;
1049 } else {
1050 $ret .= sprintf("=%02X",ord($string[$i]));
1051 }
1052 break;
1053 case '(':
1054 case ')':
1055 if ($iEncStart !== false) {
1056 $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
1057 $aRet[] = "=?$default_charset?Q?$ret?=";
1058 $iOffset = $i;
1059 $cur_l = 0;
1060 $ret = '';
1061 $iEncStart = false;
1062 }
1063 break;
1064 case ' ':
1065 if ($iEncStart !== false) {
1066 $cur_l++;
1067 if ($cur_l > $max_l) {
1068 $aRet[] = substr($string,$iOffset,$iEncStart-$iOffset);
1069 $aRet[] = "=?$default_charset?Q?$ret?=";
1070 $iOffset = $i;
1071 $cur_l = 0;
1072 $ret = '';
1073 $iEncStart = false;
1074 } else {
1075 $ret .= '_';
1076 }
1077 }
1078 break;
1079 default:
1080 $k = ord($string[$i]);
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 {
1114 $ret .= $string[$i];
1115 }
1116 }
1117 }
1118 break;
1119 }
1120 }
1121
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);
1130 }
1131 return $string;
1132 }
1133
1134 /**
1135 * Encodes string according to rfc2047 B encoding header formating rules
1136 *
1137 * It is recommended way to encode headers with character sets that store
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 *
1143 * Minimal requirements - php 4.0.6 with mbstring extension. Please note,
1144 * that mbstring functions will generate E_WARNING errors, if unsupported
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 *
1149 * Used php code functions - function_exists(), trigger_error(), strlen()
1150 * (is used with charset names and base64 strings). Used php mbstring
1151 * functions - mb_strlen and mb_substr.
1152 *
1153 * Related documents: rfc 2045 (BASE64 encoding), rfc 2047 (mime header
1154 * encoding), rfc 2822 (header folding)
1155 *
1156 * @param string $string header string that must be encoded
1157 * @param string $charset character set. Must be supported by mbstring extension.
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 */
1166 function 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
1228 /* This function trys to locate the entity_id of a specific mime element */
1229 function find_ent_id($id, $message) {
1230 for ($i = 0, $ret = ''; $ret == '' && $i < count($message->entities); $i++) {
1231 if ($message->entities[$i]->header->type0 == 'multipart') {
1232 $ret = find_ent_id($id, $message->entities[$i]);
1233 } else {
1234 if (strcasecmp($message->entities[$i]->header->id, $id) == 0) {
1235 // if (sq_check_save_extension($message->entities[$i])) {
1236 return $message->entities[$i]->entity_id;
1237 // }
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 }
1247 }
1248 }
1249 }
1250 return $ret;
1251 }
1252
1253 function 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');
1257 return in_array($ext, $save_extensions);
1258 }
1259
1260
1261 /**
1262 ** HTMLFILTER ROUTINES
1263 */
1264
1265 /**
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.
1269 *
1270 * @param $attvalue A string to run entity check against.
1271 * @return Nothing, modifies a reference value.
1272 */
1273 function sq_defang(&$attvalue){
1274 $me = 'sq_defang';
1275 /**
1276 * Skip this if there aren't ampersands or backslashes.
1277 */
1278 if (strpos($attvalue, '&') === false
1279 && strpos($attvalue, '\\') === false){
1280 return;
1281 }
1282 $m = false;
1283 // before deent, translate the dangerous unicode characters and ... to safe values
1284 // otherwise the regular expressions do not match.
1285
1286
1287
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);
1295 }
1296
1297 /**
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.
1303 * @return attvalue Nothing, modifies a reference value.
1304 */
1305 function 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);
1310 }
1311 }
1312
1313 /**
1314 * Translate all dangerous Unicode or Shift_JIS characters which are accepted by
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 */
1321 function sq_fixIE_idiocy(&$attvalue) {
1322 // remove NUL
1323 $attvalue = str_replace("\0", "", $attvalue);
1324 // remove comments
1325 $attvalue = preg_replace("/(\/\*.*?\*\/)/","",$attvalue);
1326
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"
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 */
1334 '&#xFF25;', '&#65317;' ,/* Unicode FULLWIDTH LATIN CAPITAL LETTER E */
1335 '&#xFF45;', '&#65349;' ,/* Unicode FULLWIDTH LATIN SMALL LETTER E */
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 */
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 */
1379 array('l', 'l', 'r','r','n','n',
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'));
1383 $attvalue = str_replace($aDangerousCharsReplacementTable[0],$aDangerousCharsReplacementTable[1],$attvalue);
1384
1385 // Escapes are useful for special characters like "{}[]()'&. In other cases they are
1386 // used for XSS.
1387 $attvalue = preg_replace("/(\\\\)([a-zA-Z]{1})/",'$2',$attvalue);
1388 }
1389
1390 /**
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 */
1400 function sq_tagprint($tagname, $attary, $tagtype){
1401 $me = 'sq_tagprint';
1402
1403 if ($tagtype == 2){
1404 $fulltag = '</' . $tagname . '>';
1405 } else {
1406 $fulltag = '<' . $tagname;
1407 if (is_array($attary) && sizeof($attary)){
1408 $atts = Array();
1409 foreach ($attary as $attname => $attvalue){
1410 array_push($atts, "$attname=$attvalue");
1411 }
1412 $fulltag .= ' ' . join(" ", $atts);
1413 }
1414 if ($tagtype == 3){
1415 $fulltag .= ' /';
1416 }
1417 $fulltag .= '>';
1418 }
1419 return $fulltag;
1420 }
1421
1422 /**
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 */
1429 function sq_casenormalize(&$val){
1430 $val = strtolower($val);
1431 }
1432
1433 /**
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 */
1443 function sq_skipspace($body, $offset){
1444 $me = 'sq_skipspace';
1445 preg_match('/^(\s*)/s', substr($body, $offset), $matches);
1446 if (!empty($matches[1])){
1447 $offset += strlen($matches[1]);
1448 }
1449 return $offset;
1450 }
1451
1452 /**
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 */
1463 function sq_findnxstr($body, $offset, $needle){
1464 $me = 'sq_findnxstr';
1465 $pos = strpos($body, $needle, $offset);
1466 if ($pos === FALSE){
1467 $pos = strlen($body);
1468 }
1469 return $pos;
1470 }
1471
1472 /**
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 */
1485 function sq_findnxreg($body, $offset, $reg){
1486 $me = 'sq_findnxreg';
1487 $matches = Array();
1488 $retarr = Array();
1489 preg_match("%^(.*?)($reg)%si", substr($body, $offset), $matches);
1490 if (!isset($matches[0]) || !$matches[0]){
1491 $retarr = false;
1492 } else {
1493 $retarr[0] = $offset + strlen($matches[1]);
1494 $retarr[1] = $matches[1];
1495 $retarr[2] = $matches[2];
1496 }
1497 return $retarr;
1498 }
1499
1500 /**
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 */
1514 function sq_getnxtag($body, $offset){
1515 $me = 'sq_getnxtag';
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 /**
1524 * We are here:
1525 * blah blah <tag attribute="value">
1526 * \---------^
1527 */
1528 $pos = sq_skipspace($body, $lt+1);
1529 if ($pos >= strlen($body)){
1530 return Array(false, false, false, $lt, strlen($body));
1531 }
1532 /**
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 */
1541 $tagtype = false;
1542 switch (substr($body, $pos, 1)){
1543 case '/':
1544 $tagtype = 2;
1545 $pos++;
1546 break;
1547 case '!':
1548 /**
1549 * A comment or an SGML declaration.
1550 */
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);
1559 } else {
1560 $gt = sq_findnxstr($body, $pos, ">");
1561 return Array(false, false, false, $lt, $gt);
1562 }
1563 break;
1564 default:
1565 /**
1566 * Assume tagtype 1 for now. If it's type 3, we'll switch values
1567 * later.
1568 */
1569 $tagtype = 1;
1570 break;
1571 }
1572
1573 $tag_start = $pos;
1574 $tagname = '';
1575 /**
1576 * Look for next [\W-_], which will indicate the end of the tag name.
1577 */
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 /**
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 */
1593 switch ($match){
1594 case '/':
1595 /**
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 */
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 /**
1613 * Check if it's whitespace
1614 */
1615 if (!preg_match('/\s/', $match)){
1616 /**
1617 * This is an invalid tag! Look for the next closing ">".
1618 */
1619 $gt = sq_findnxstr($body, $lt, ">");
1620 return Array(false, false, false, $lt, $gt);
1621 }
1622 break;
1623 }
1624
1625 /**
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 */
1632 $attname = '';
1633 $atttype = false;
1634 $attary = Array();
1635
1636 while ($pos <= strlen($body)){
1637 $pos = sq_skipspace($body, $pos);
1638 if ($pos == strlen($body)){
1639 /**
1640 * Non-closed tag.
1641 */
1642 return Array(false, false, false, $lt, $pos);
1643 }
1644 /**
1645 * See if we arrived at a ">" or "/>", which means that we reached
1646 * the end of the tag.
1647 */
1648 $matches = Array();
1649 if (preg_match("%^(\s*)(>|/>)%s", substr($body, $pos), $matches)) {
1650 /**
1651 * Yep. So we did.
1652 */
1653 $pos += strlen($matches[1]);
1654 if ($matches[2] == "/>"){
1655 $tagtype = 3;
1656 $pos++;
1657 }
1658 return Array($tagname, $attary, $tagtype, $lt, $pos);
1659 }
1660
1661 /**
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 */
1678 $regary = sq_findnxreg($body, $pos, "[^:\w\-_]");
1679 if ($regary == false){
1680 /**
1681 * Looks like body ended before the end of tag.
1682 */
1683 return Array(false, false, false, $lt, strlen($body));
1684 }
1685 list($pos, $attname, $match) = $regary;
1686 $attname = strtolower($attname);
1687 /**
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 */
1695 switch($match){
1696 case '/':
1697 /**
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 */
1702 if (substr($body, $pos, 2) == "/>"){
1703 $pos++;
1704 $tagtype = 3;
1705 } else {
1706 $gt = sq_findnxstr($body, $pos, ">");
1707 $retary = Array(false, false, false, $lt, $gt);
1708 return $retary;
1709 }
1710 case '>':
1711 $attary[$attname] = '"yes"';
1712 return Array($tagname, $attary, $tagtype, $lt, $pos);
1713 break;
1714 default:
1715 /**
1716 * Skip whitespace and see what we arrive at.
1717 */
1718 $pos = sq_skipspace($body, $pos);
1719 $char = substr($body, $pos, 1);
1720 /**
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 */
1728 if ($char == "="){
1729 $pos++;
1730 $pos = sq_skipspace($body, $pos);
1731 /**
1732 * Here are 3 possibilities:
1733 * "'" attribute type 1
1734 * '"' attribute type 2
1735 * everything else is the content of tag type 3
1736 */
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++;
1745 $attary[$attname] = "'" . $attval . "'";
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++;
1753 $attary[$attname] = '"' . $attval . '"';
1754 } else {
1755 /**
1756 * These are hateful. Look for \s, or >.
1757 */
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 /**
1764 * If it's ">" it will be caught at the top.
1765 */
1766 $attval = preg_replace("/\"/s", "&quot;", $attval);
1767 $attary[$attname] = '"' . $attval . '"';
1768 }
1769 } else if (preg_match("|[\w/>]|", $char)) {
1770 /**
1771 * That was attribute type 4.
1772 */
1773 $attary[$attname] = '"yes"';
1774 } else {
1775 /**
1776 * An illegal character. Find next '>' and return.
1777 */
1778 $gt = sq_findnxstr($body, $pos, ">");
1779 return Array(false, false, false, $lt, $gt);
1780 }
1781 break;
1782 }
1783 }
1784 /**
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 */
1788 return Array(false, false, false, $lt, strlen($body));
1789 }
1790
1791 /**
1792 * Translates entities into literal values so they can be checked.
1793 *
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.
1798 */
1799 function sq_deent(&$attvalue, $regex, $hex=false){
1800 $me = 'sq_deent';
1801 $ret_match = false;
1802 // remove comments
1803 //$attvalue = preg_replace("/(\/\*.*\*\/)/","",$attvalue);
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);
1811 }
1812 $repl[$matches[0][$i]] = chr($numval);
1813 }
1814 $attvalue = strtr($attvalue, $repl);
1815 return true;
1816 } else {
1817 return false;
1818 }
1819 }
1820
1821 /**
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 */
1833 function sq_fixatts($tagname,
1834 $attary,
1835 $rm_attnames,
1836 $bad_attvals,
1837 $add_attr_to_tag,
1838 $message,
1839 $id,
1840 $mailbox
1841 ){
1842 $me = 'sq_fixatts';
1843 foreach ($attary as $attname => $attvalue){
1844 /**
1845 * See if this attribute should be removed.
1846 */
1847 foreach ($rm_attnames as $matchtag=>$matchattrs){
1848 if (preg_match($matchtag, $tagname)){
1849 foreach ($matchattrs as $matchattr){
1850 if (preg_match($matchattr, $attname)){
1851 unset($attary[$attname]);
1852 continue;
1853 }
1854 }
1855 }
1856 }
1857 /**
1858 * Workaround for IE quirks
1859 */
1860 sq_fixIE_idiocy($attvalue);
1861
1862 /**
1863 * Remove any backslashes, entities, and extraneous whitespace.
1864 */
1865
1866 $oldattvalue = $attvalue;
1867 sq_defang($attvalue);
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";
1872 $attary[$attname] = $attvalue;
1873 }
1874 sq_unspace($attvalue);
1875
1876 /**
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 */
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 /**
1887 * There are two arrays in valary.
1888 * First is matches.
1889 * Second one is replacements
1890 */
1891 list($valmatch, $valrepl) = $valary;
1892 $newvalue =
1893 preg_replace($valmatch, $valrepl, $attvalue);
1894 if ($newvalue != $attvalue){
1895 $attary[$attname] = $newvalue;
1896 $attvalue = $newvalue;
1897 }
1898 }
1899 }
1900 }
1901 }
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
1905 $attary[$attname] = '"disallowed character"';
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,"'");
1913 $attary[$attname] = str_replace($sMatch,$urlvalue,$attvalue);
1914 }
1915 }
1916 }
1917 /**
1918 * Use white list based filtering on attributes which can contain url's
1919 */
1920 else if ($attname == 'href' || $attname == 'xlink:href' || $attname == 'src'
1921 || $attname == 'poster' || $attname == 'formaction'
1922 || $attname == 'background' || $attname == 'action') {
1923 sq_fix_url($attname, $attvalue, $message, $id, $mailbox);
1924 $attary[$attname] = $attvalue;
1925 }
1926 }
1927 /**
1928 * See if we need to append any attributes to this tag.
1929 */
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;
1936 }
1937
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 */
1947 function 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
1955 // If there's no "view_unsafe_images" variable in the URL, turn unsafe
1956 // images off by default.
1957 sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET, FALSE);
1958
1959 global $use_transparent_security_image;
1960 if ($use_transparent_security_image) $secremoveimg = '../images/spacer.png';
1961 else $secremoveimg = '../images/' . _("sec_remove_eng.png");
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'])) {
1986 case 'mailto':
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'])) {
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 /* ---------------------------------
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'))) {
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 }
2053 }
2054 --------------------------------- */
2055 } else {
2056 $attvalue = $sQuote . SM_PATH . 'images/blank.png'. $sQuote;
2057 }
2058 }
2059 } else {
2060 $attvalue = $sQuote . $attvalue . $sQuote;
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 */
2069 $attvalue = $sQuote . sq_cid2http($message, $id, $attvalue, $mailbox) . $sQuote;
2070 break;
2071 case 'cid':
2072 /**
2073 * Turn cid: urls into http-friendly ones.
2074 */
2075 $attvalue = $sQuote . sq_cid2http($message, $id, $attvalue, $mailbox) . $sQuote;
2076 break;
2077 default:
2078 $attvalue = $sQuote . SM_PATH . 'images/blank.png' . $sQuote;
2079 break;
2080 }
2081 } else {
2082 if (!isset($aUrl['path']) || $aUrl['path'] != $secremoveimg) {
2083 // parse_url did not lead to satisfying result
2084 $attvalue = $sQuote . SM_PATH . 'images/blank.png' . $sQuote;
2085 }
2086 }
2087 }
2088 }
2089 }
2090
2091 /**
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 */
2101 function sq_fixstyle($body, $pos, $message, $id, $mailbox){
2102 $me = 'sq_fixstyle';
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) {
2110 $char = $body[$i];
2111 switch ($char) {
2112 case '<':
2113 $sToken = $char;
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
2141 if (isset($body[$i+2]) && substr($body,$i,3) == '!--') {
2142 $i = strpos($body,'-->',$i+3);
2143 if ($i === false) { // no end comment
2144 $i = strlen($body);
2145 }
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){
2162 return array(FALSE, strlen($body));
2163 }
2164
2165
2166
2167 /**
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 */
2173 // $content = preg_replace("|body(\s*\{.*?\})|si", ".bodyclass\\1", $content);
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);
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
2189 /**
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);
2194
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
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
2207 // translate ur\l and variations (IE parses that)
2208 // TODO check if the sq_fixIE_idiocy function already handles this.
2209 $content = preg_replace("/(\\\\)?u(\\\\)?r(\\\\)?l(\\\\)?/i", 'url', $content);
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;
2219 }
2220 $content = str_replace($aValue,$aReplace,$content);
2221 }
2222
2223 /**
2224 * Remove any backslashes, entities, and extraneous whitespace.
2225 */
2226 $contentTemp = $content;
2227 sq_defang($contentTemp);
2228 sq_unspace($contentTemp);
2229
2230 /**
2231 * Fix stupid css declarations which lead to vulnerabilities
2232 * in IE.
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.
2239 */
2240 $match = Array('/\/\*.*\*\//', // removes /* blah blah */
2241 '/expression/i',
2242 '/behaviou*r/i',
2243 '/binding/i',
2244 '/include-source/i',
2245 '/javascript/i',
2246 '/script/i',
2247 '/position/i');
2248 $replace = Array('','idiocy', 'idiocy', 'idiocy', 'idiocy', 'idiocy', 'idiocy', '');
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 }
2255 return array($content, $newpos);
2256 }
2257
2258
2259 /**
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 */
2269 function sq_cid2http($message, $id, $cidurl, $mailbox){
2270 /**
2271 * Get rid of quotes.
2272 */
2273 $quotchar = substr($cidurl, 0, 1);
2274 if ($quotchar == '"' || $quotchar == "'"){
2275 $cidurl = str_replace($quotchar, "", $cidurl);
2276 } else {
2277 $quotchar = '';
2278 }
2279 $cidurl = substr(trim($cidurl), 4);
2280
2281 $match_str = '/\{.*?\}\//';
2282 $str_rep = '';
2283 $cidurl = preg_replace($match_str, $str_rep, $cidurl);
2284
2285 $linkurl = find_ent_id($cidurl, $message);
2286 /* in case of non-safe cid links $httpurl should be replaced by a sort of
2287 unsafe link image */
2288 $httpurl = '';
2289
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 */
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 }
2307
2308 if (!empty($linkurl)) {
2309 $httpurl = $quotchar . sqm_baseuri() . 'src/download.php?absolute_dl=true&amp;' .
2310 "passed_id=$id&amp;mailbox=" . urlencode($mailbox) .
2311 '&amp;ent_id=' . $linkurl . $quotchar;
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 */
2317 $httpurl = $quotchar . SM_PATH . 'images/blank.png' . $quotchar;
2318 }
2319
2320 return $httpurl;
2321 }
2322
2323 /**
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 */
2333 function sq_body2div($attary, $mailbox, $message, $id){
2334 $me = 'sq_body2div';
2335 $divattary = Array('class' => "'bodyclass'");
2336 $text = '#000000';
2337 $has_bgc_stl = $has_txt_stl = false;
2338 $styledef = '';
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){
2344 case 'background':
2345 $attvalue = sq_cid2http($message, $id, $attvalue, $mailbox);
2346 $styledef .= "background-image: url('$attvalue'); ";
2347 break;
2348 case 'bgcolor':
2349 $has_bgc_stl = true;
2350 $styledef .= "background-color: $attvalue; ";
2351 break;
2352 case 'text':
2353 $has_txt_stl = true;
2354 $styledef .= "color: $attvalue; ";
2355 break;
2356 }
2357 }
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 }
2363 if (strlen($styledef) > 0){
2364 $divattary["style"] = "\"$styledef\"";
2365 }
2366 }
2367 return $divattary;
2368 }
2369
2370 /**
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
2388 * @param $recursively_called boolean flag for recursive calls into this function (optional; default FALSE)
2389 * @return sanitized html safe to show on your pages.
2390 */
2391 function sq_sanitize($body,
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,
2401 $mailbox,
2402 $recursively_called=FALSE
2403 ){
2404 $me = 'sq_sanitize';
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;
2412 $rm_tags = array_shift($tag_list);
2413
2414 /**
2415 * Normalize rm_tags and rm_tags_with_content.
2416 */
2417 @array_walk($tag_list, 'sq_casenormalize');
2418 @array_walk($rm_tags_with_content, 'sq_casenormalize');
2419 @array_walk($self_closing_tags, 'sq_casenormalize');
2420
2421 $curpos = 0;
2422 $open_tags = Array();
2423 $trusted = "\n<!-- begin sanitized html -->\n";
2424 $skip_content = false;
2425 /**
2426 * Take care of netscape's stupid javascript entities like
2427 * &{alert('boo')};
2428 */
2429 $body = preg_replace("/&(\{.*?\};)/si", "&amp;\\1", $body);
2430
2431 while (($curtag = sq_getnxtag($body, $curpos)) != FALSE){
2432 list($tagname, $attary, $tagtype, $lt, $gt) = $curtag;
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
2474 $free_content = substr($body, $curpos, $lt-$curpos);
2475 /**
2476 * Take care of <style>
2477 */
2478 if ($tagname == "style" && $tagtype == 1){
2479 list($free_content, $curpos) =
2480 sq_fixstyle($body, $gt+1, $message, $id, $mailbox);
2481 if ($free_content != FALSE){
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 }
2493 $trusted .= sq_tagprint($tagname, $attary, $tagtype);
2494 $trusted .= $free_content;
2495 $trusted .= sq_tagprint($tagname, false, 2);
2496 }
2497 continue;
2498 }
2499 if ($skip_content == false){
2500 $trusted .= $free_content;
2501 }
2502 if ($tagname != FALSE){
2503 if ($tagtype == 2){
2504 if ($skip_content == $tagname){
2505 /**
2506 * Got to the end of tag we needed to remove.
2507 */
2508 $tagname = false;
2509 $skip_content = false;
2510 } else {
2511 if ($skip_content == false){
2512 if ($tagname == "body"){
2513 $tagname = "div";
2514 }
2515 if (isset($open_tags[$tagname]) &&
2516 $open_tags[$tagname] > 0){
2517 $open_tags[$tagname]--;
2518 } else {
2519 $tagname = false;
2520 }
2521 }
2522 }
2523 } else {
2524 /**
2525 * $rm_tags_with_content
2526 */
2527 if ($skip_content == false){
2528 /**
2529 * See if this is a self-closing type and change
2530 * tagtype appropriately.
2531 */
2532 if ($tagtype == 1
2533 && in_array($tagname, $self_closing_tags)){
2534 $tagtype = 3;
2535 }
2536 /**
2537 * See if we should skip this tag and any content
2538 * inside it.
2539 */
2540 if ($tagtype == 1 &&
2541 in_array($tagname, $rm_tags_with_content)){
2542 $skip_content = $tagname;
2543 } else {
2544 if (($rm_tags == false
2545 && in_array($tagname, $tag_list)) ||
2546 ($rm_tags == true &&
2547 !in_array($tagname, $tag_list))){
2548 $tagname = false;
2549 } else {
2550 /**
2551 * Convert body into div.
2552 */
2553 if ($tagname == "body"){
2554 $tagname = "div";
2555 $attary = sq_body2div($attary, $mailbox,
2556 $message, $id);
2557 }
2558 if ($tagtype == 1){
2559 if (isset($open_tags[$tagname])){
2560 $open_tags[$tagname]++;
2561 } else {
2562 $open_tags[$tagname]=1;
2563 }
2564 }
2565 /**
2566 * This is where we run other checks.
2567 */
2568 if (is_array($attary) && sizeof($attary) > 0){
2569 $attary = sq_fixatts($tagname,
2570 $attary,
2571 $rm_attnames,
2572 $bad_attvals,
2573 $add_attr_to_tag,
2574 $message,
2575 $id,
2576 $mailbox
2577 );
2578 }
2579 }
2580 }
2581 }
2582 }
2583 if ($tagname != false && $skip_content == false){
2584 $trusted .= sq_tagprint($tagname, $attary, $tagtype);
2585 }
2586 }
2587 $curpos = $gt+1;
2588 }
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 }
2602
2603 /**
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
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)
2614 * @return a string with html safe to display in the browser.
2615 */
2616 function magicHTML($body, $id, $message, $mailbox = 'INBOX', $take_mailto_links =true) {
2617
2618 // require_once(SM_PATH . 'functions/url_parser.php'); // for $MailTo_PReg_Match
2619
2620 global $attachment_common_show_images, $view_unsafe_images,
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
2632 /**
2633 * Don't display attached images in HTML mode.
2634 *
2635 * SB: why?
2636 */
2637 $attachment_common_show_images = false;
2638 $tag_list = Array(
2639 false, // remove these tags
2640 "meta",
2641 "html",
2642 "head",
2643 "base",
2644 "link",
2645 "frame",
2646 "iframe",
2647 "plaintext",
2648 "marquee",
2649 );
2650
2651 $rm_tags_with_content = Array(
2652 "script",
2653 "object",
2654 "applet",
2655 "embed",
2656 "title",
2657 "frameset",
2658 "xmp",
2659 "xml",
2660 );
2661 if (!$allow_svg_display)
2662 $rm_tags_with_content[] = 'svg';
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);
2672
2673 $self_closing_tags = Array(
2674 "img",
2675 "br",
2676 "hr",
2677 "input",
2678 "outbind",
2679 );
2680
2681 $force_tag_closing = true;
2682
2683 $rm_attnames = Array(
2684 "/.*/" =>
2685 Array(
2686 "/target/i",
2687 "/^on.*/i",
2688 "/^dynsrc/i",
2689 "/^data.*/i",
2690 "/^lowsrc.*/i",
2691 )
2692 );
2693
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
2698 $bad_attvals = Array(
2699 "/.*/" =>
2700 Array(
2701 "/^src|background/i" =>
2702 Array(
2703 Array(
2704 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si",
2705 "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si",
2706 "/^([\'\"])\s*about\s*:.*([\'\"])/si"
2707 ),
2708 Array(
2709 "\\1$secremoveimg\\2",
2710 "\\1$secremoveimg\\2",
2711 "\\1$secremoveimg\\2",
2712 )
2713 ),
2714 "/^href|action/i" =>
2715 Array(
2716 Array(
2717 "/^([\'\"])\s*\S+script\s*:.*([\'\"])/si",
2718 "/^([\'\"])\s*mocha\s*:*.*([\'\"])/si",
2719 "/^([\'\"])\s*about\s*:.*([\'\"])/si"
2720 ),
2721 Array(
2722 "\\1#\\1",
2723 "\\1#\\1",
2724 "\\1#\\1"
2725 )
2726 ),
2727 "/^style/i" =>
2728 Array(
2729 Array(
2730 "/\/\*.*\*\//",
2731 "/expression/i",
2732 "/binding/i",
2733 "/behaviou*r/i",
2734 "/include-source/i",
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
2752 "/(\\\\)?u(\\\\)?r(\\\\)?l(\\\\)?/i",
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",
2756 "/(.*)\s*:\s*url\s*\(\s*([\'\"]*)\s*\S+script\s*:.*([\'\"]*)\s*\)/si",
2757 ),
2758 Array(
2759 "",
2760 "idiocy",
2761 "idiocy",
2762 "idiocy",
2763 "idiocy",
2764 "idiocy",
2765 "url",
2766 "url(\\1#\\1)",
2767 "url(\\1#\\1)",
2768 "url(\\1#\\1)",
2769 "\\1:url(\\2#\\3)"
2770 )
2771 )
2772 )
2773 );
2774
2775 // If there's no "view_unsafe_images" variable in the URL, turn unsafe
2776 // images off by default.
2777 sqgetGlobalVar('view_unsafe_images', $view_unsafe_images, SQ_GET, FALSE);
2778
2779 if (!$view_unsafe_images){
2780 /**
2781 * Remove any references to http/https if view_unsafe_images set
2782 * to false.
2783 */
2784 array_push($bad_attvals['/.*/']['/^src|background/i'][0],
2785 '/^([\'\"])\s*https*:.*([\'\"])/si');
2786 array_push($bad_attvals['/.*/']['/^src|background/i'][1],
2787 "\\1$secremoveimg\\1");
2788 array_push($bad_attvals['/.*/']['/^style/i'][0],
2789 '/url\([\'\"]?https?:[^\)]*[\'\"]?\)/si');
2790 array_push($bad_attvals['/.*/']['/^style/i'][1],
2791 "url(\\1$secremoveimg\\1)");
2792 }
2793
2794 $add_attr_to_tag = Array(
2795 "/^a$/i" =>
2796 Array('target'=>'"_blank"',
2797 'title'=>'"'._("This external link will open in a new window").'"'
2798 )
2799 );
2800 $trusted = sq_sanitize($body,
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 );
2812 if (strpos($trusted,$secremoveimg)){
2813 $has_unsafe_images = true;
2814 }
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;
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) ;
2828 if ((preg_match_all($MailTo_PReg_Match, $trusted, $regs)) && ($regs[0][0] != '')) {
2829 foreach ($regs[0] as $i => $mailto_before) {
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
2836 // get rid of any tailing quote since we have to add send_to to the end
2837 //
2838 $mailto_before = rtrim($mailto_before, '"\'');
2839 $mailto_params = rtrim($mailto_params, '"\'');
2840
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];
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 //
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 }
2873 $trusted = str_replace($mailto_before, $comp_uri, $trusted);
2874 }
2875 }
2876 }
2877
2878 return $trusted;
2879 }
2880
2881 /**
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 */
2895 function SendDownloadHeaders($type0, $type1, $filename, $force, $filesize=0) {
2896 global $languages, $squirrelmail_language;
2897 $isIE = $isIE6plus = false;
2898
2899 sqgetGlobalVar('HTTP_USER_AGENT', $HTTP_USER_AGENT, SQ_SERVER);
2900
2901 if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
2902 strstr($HTTP_USER_AGENT, 'Opera') === false) {
2903 $isIE = true;
2904 }
2905
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;
2909 }
2910
2911 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
2912 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_downloadfilename')) {
2913 $filename =
2914 call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_downloadfilename', $filename, $HTTP_USER_AGENT);
2915 } else {
2916 $filename = preg_replace('/[\\\\\/:*?"<>|;]/', '_', str_replace('&nbsp;', ' ', $filename));
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");
2934 header ("Cache-Control: no-store, max-age=0, no-cache, must-revalidate"); // HTTP/1.1
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);
2937 header ("Cache-Control: private");
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
2966 if ($isIE && !$isIE6plus) {
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\"");
2972 header ("Content-Type: application/force-download; name=\"$filename\"");
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\"");
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\"");
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 }
2990
2991 } // end fn SendDownloadHeaders