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