XSS fix based on Jason's fix
[squirrelmail.git] / src / search.php
CommitLineData
c61bb006 1<?php
245a6892 2
35586184 3/**
a5ee5ac2 4 * search.php
35586184 5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
2d367c68 11
86725763 12/* Path for SquirrelMail required files. */
13define('SM_PATH','../');
14
15/* SquirrelMail required files. */
08185f2a 16require_once(SM_PATH . 'include/validate.php');
86725763 17require_once(SM_PATH . 'functions/imap.php');
18require_once(SM_PATH . 'functions/imap_search.php');
19require_once(SM_PATH . 'functions/imap_mailbox.php');
20require_once(SM_PATH . 'functions/array.php');
21require_once(SM_PATH . 'functions/strings.php');
c61bb006 22
e92eab8b 23global $allow_thread_sort;
56e0b3b7 24
23a9084b 25/* here are some functions, could go in imap_search.php
23a9084b 26 this was here, pretty handy */
29eb5486 27function s_opt( $val, $sel, $tit ) {
28 echo " <option value=\"$val\"";
0d672ac0 29 if ( $sel == $val ) {
bd9bbfef 30 echo ' selected';
99d2a184 31 }
29eb5486 32 echo ">$tit</option>\n";
33}
99d2a184 34
bebf762c 35/* function to get the recent searches and put them in the attributes array */
36function get_recent($username, $data_dir) {
37 $attributes = array();
38 $types = array('search_what', 'search_where', 'search_folder');
56e0b3b7 39 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
56e0b3b7 40 for ($x=1;$x<=$recent_count;$x++) {
bebf762c 41 reset($types);
42 foreach ($types as $key) {
43 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
44 }
56e0b3b7 45 }
bebf762c 46 return $attributes;
56e0b3b7 47}
48
bebf762c 49/* function to get the saved searches and put them in the saved_attributes array */
50function get_saved($username, $data_dir) {
51 $saved_attributes = array();
52 $types = array('saved_what', 'saved_where', 'saved_folder');
53 foreach ($types as $key) {
54 for ($x=1;;$x++) {
55 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
56 if ($saved_attributes[$key][$x] == "") {
57 array_pop($saved_attributes[$key]);
58 break;
59 }
60 }
56e0b3b7 61 }
bebf762c 62 return $saved_attributes;
56e0b3b7 63}
64
23a9084b 65/* function to update recent pref arrays */
66function update_recent($what, $where, $mailbox, $username, $data_dir) {
bebf762c 67 $attributes = array();
68 $types = array('search_what', 'search_where', 'search_folder');
69 $input = array($what, $where, $mailbox);
70 $attributes = get_recent( $username, $data_dir);
71 reset($types);
70c4fd84 72 $dupe = 'no';
bebf762c 73 for ($i=1;$i<=count($attributes['search_what']);$i++) {
74 if (isset($attributes['search_what'][$i])) {
75 if ($what == $attributes['search_what'][$i] &&
76 $where == $attributes['search_where'][$i] &&
77 $mailbox == $attributes['search_folder'][$i]) {
78 $dupe = 'yes';
79 }
23a9084b 80 }
81 }
70c4fd84 82 if ($dupe == 'no') {
bebf762c 83 $i = 0;
84 foreach ($types as $key) {
85 array_push ($attributes[$key], $input[$i]);
86 array_shift ($attributes[$key]);
87 $i++;
88 }
89 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
90 $n=0;
91 for ($i=1;$i<=$recent_count;$i++) {
92 reset($types);
93 foreach ($types as $key) {
94 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
95 }
96 $n++;
97 }
56e0b3b7 98 }
99}
100
23a9084b 101/* function to forget a recent search */
56e0b3b7 102function forget_recent($forget_index, $username, $data_dir) {
bebf762c 103 $attributes = array();
104 $types = array('search_what', 'search_where', 'search_folder');
105 $attributes = get_recent( $username, $data_dir);
106 reset($types);
107 foreach ($types as $key) {
7dba227d 108 array_splice($attributes[$key], $forget_index - 1, 1);
bebf762c 109 array_unshift($attributes[$key], '');
110 }
111 reset($types);
56e0b3b7 112 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
113 $n=0;
114 for ($i=1;$i<=$recent_count;$i++) {
bebf762c 115 reset($types);
116 foreach ($types as $key) {
117 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
118 }
23a9084b 119 $n++;
56e0b3b7 120 }
121}
23a9084b 122
123/* function to delete a saved search */
56e0b3b7 124function delete_saved($delete_index, $username, $data_dir) {
bebf762c 125 $types = array('saved_what', 'saved_where', 'saved_folder');
126 $attributes = get_saved($username, $data_dir);
127 foreach ($types as $key) {
128 array_splice($attributes[$key], $delete_index, 1);
129 }
130 reset($types);
56e0b3b7 131 $n=0;
bebf762c 132 $saved_count = count($attributes['saved_what']);
56e0b3b7 133 $last_element = $saved_count + 1;
56e0b3b7 134 for ($i=1;$i<=$saved_count;$i++) {
bebf762c 135 reset($types);
136 foreach ($types as $key) {
137 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
138 }
23a9084b 139 $n++;
56e0b3b7 140 }
bebf762c 141 reset($types);
142 foreach($types as $key) {
143 removePref($data_dir, $username, $key.$last_element);
56e0b3b7 144 }
70c4fd84 145}
6c8388a9 146
23a9084b 147/* function to save a search from recent to saved */
56e0b3b7 148function save_recent($save_index, $username, $data_dir) {
bebf762c 149 $attributes = array();
150 $types = array('search_what', 'search_where', 'search_folder');
151 $saved_types = array(0 => 'saved_what', 1 => 'saved_where', 2 => 'saved_folder');
152 $saved_array = get_saved($username, $data_dir);
153 $save_index = $save_index -1;
154 $saved_count = (count($saved_array['saved_what']) + 1);
155 $attributes = get_recent ($username, $data_dir);
156 $n = 0;
157 foreach ($types as $key) {
158 $slice = array_slice($attributes[$key], $save_index, 1);
159 $name = $saved_types[$n];
160 setPref($data_dir, $username, $name.$saved_count, $slice[0]);
161 $n++;
162 }
56e0b3b7 163}
164
d215ca7d 165function printSearchMessages($msgs,$mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false) {
59a623e6 166 global $sort, $color;
167
70f1b6b5 168 $msort = calc_msort($msgs, $sort);
59a623e6 169 if ($cnt > 0) {
170 if ( $mailbox == 'INBOX' ) {
171 $showbox = _("INBOX");
172 } else {
173 $showbox = imap_utf7_decode_local($mailbox);
174 }
175 echo html_tag( 'div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
176
70f1b6b5 177
178 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
4a6762c5 179 $toggle_all = get_selectall_link(1, $sort);
70f1b6b5 180
4a6762c5 181 echo '<table bgcolor="' . $color[0] . '" border="0" width="100%" cellpadding="1" cellspacing="0"><tr><td>';
70f1b6b5 182 mail_message_listing_beginning($imapConnection, $mailbox, $sort,
4a6762c5 183 $msg_cnt_str, $toggle_all, 1);
70f1b6b5 184
185
186 printHeader($mailbox, 6, $color, false);
187
59a623e6 188 displayMessageArray($imapConnection, $cnt, 1,
d215ca7d 189 $msort, $mailbox, $sort, $color, $cnt, $where, $what);
70f1b6b5 190
191 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
4a6762c5 192 echo '</td></tr></table>';
193
59a623e6 194 }
195}
196
0d672ac0 197/* ------------------------ main ------------------------ */
198
23a9084b 199/* reset these arrays on each page load just in case */
bebf762c 200$attributes = array ();
201$saved_attributes = array ();
70c4fd84 202$search_all = 'none';
23a9084b 203$perbox_count = array ();
aa45f943 204$recent_count = getPref($data_dir, $username, 'search_memory', 0);
56e0b3b7 205
ba5f492c 206/* get globals we may need */
207
208$key = $_COOKIE['key'];
209$username = $_SESSION['username'];
210$onetimepad = $_SESSION['onetimepad'];
211$delimiter = $_SESSION['delimiter'];
212
213if (isset($_GET['mailbox'])) {
214 $mailbox = strip_tags($_GET['mailbox']);
215}
216if (isset($_GET['submit'])) {
217 $submit = strip_tags($_GET['submit']);
218}
219if (isset($_GET['what'])) {
220 $what = $_GET['what'];
221}
222if (isset($_GET['where'])) {
223 $where = strip_tags($_GET['where']);
224}
225if (isset($_GET['checkall'])) {
226 $checkall = strip_tags($_GET['checkall']);
227}
228if (isset($_GET['count'])) {
229 $count = strip_tags($_GET['count']);
230}
d81e351b 231
23a9084b 232/* get mailbox names */
29eb5486 233$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
c1cb7ba4 234$boxes = sqimap_mailbox_list($imapConnection);
235
23a9084b 236/* set current mailbox to INBOX if none was selected or if page
237 was called to search all folders. */
14c62c12 238if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
56e0b3b7 239 $mailbox = $boxes[0]['unformatted'];
240}
3b7d68e6 241if ($mailbox == 'All Folders') {
70c4fd84 242 $search_all = 'all';
c1cb7ba4 243}
244
548a552a 245if (isset($composenew) && $composenew) {
d7f8e6e6 246 $comp_uri = "../src/compose.php?mailbox=". urlencode($mailbox).
247 "&amp;session=$composesession&amp;attachedmessages=true&amp";
248 displayPageHeader($color, $mailbox, "comp_in_new(false,'$comp_uri');", false);
548a552a 249} else {
250 displayPageHeader($color, $mailbox);
251}
23a9084b 252/* See how the page was called and fire off correct function */
d4144adf 253if ((!isset($submit) || empty($submit)) && !empty($what)) {
70c4fd84 254 $submit = _("Search");
d4144adf 255}
70c4fd84 256if ( !isset( $submit ) ) {
70c4fd84 257 $submit = '';
258} else if ($submit == _("Search") && !empty($what)) {
aa45f943 259 if ($recent_count > 0) {
260 update_recent($what, $where, $mailbox, $username, $data_dir);
261 }
56e0b3b7 262}
88cb1b4d 263elseif ($submit == 'forget') {
56e0b3b7 264 forget_recent($count, $username, $data_dir);
265}
88cb1b4d 266elseif ($submit == 'save') {
56e0b3b7 267 save_recent($count, $username, $data_dir);
268}
88cb1b4d 269elseif ($submit == 'delete') {
56e0b3b7 270 delete_saved($count, $username, $data_dir);
271}
23a9084b 272
29eb5486 273do_hook('search_before_form');
56e0b3b7 274
6206f6c4 275echo html_tag( 'table',
dcbe1ecc 276 html_tag( 'tr', "\n" .
277 html_tag( 'td', '<b>' . _("Search") . '</b>', 'center', $color[0] )
278 ) ,
279 '', '', 'width="100%"') . "\n";
c61bb006 280
23a9084b 281/* update the recent and saved searches from the pref files */
bebf762c 282$attributes = get_recent($username, $data_dir);
bebf762c 283$saved_attributes = get_saved($username, $data_dir);
284$saved_count = count($saved_attributes['saved_what']);
91954f9e 285$count_all = 0;
a7d0eaf6 286
184ef883 287/* Saved Search Table */
56e0b3b7 288if ($saved_count > 0) {
dcbe1ecc 289 echo "<br>\n"
290 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
291 . html_tag( 'tr',
292 html_tag( 'td', '<b>Saved Searches</b>', 'center' )
293 )
294 . html_tag( 'tr' )
295 . html_tag( 'td' )
296 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="2" cellspacing="2" border="0"' );
184ef883 297 for ($i=0; $i < $saved_count; ++$i) {
88cb1b4d 298 if ($i % 2) {
dcbe1ecc 299 echo html_tag( 'tr', '', '', $color[0] );
88cb1b4d 300 } else {
dcbe1ecc 301 echo html_tag( 'tr', '', '', $color[4] );
184ef883 302 }
dcbe1ecc 303 echo html_tag( 'td', $saved_attributes['saved_folder'][$i], 'left', '', 'width="35%"' )
304 . html_tag( 'td', $saved_attributes['saved_what'][$i], 'left' )
305 . html_tag( 'td', $saved_attributes['saved_where'][$i], 'center' )
306 . html_tag( 'td', '', 'right' )
307 . '<a href=search.php'
ba5f492c 308 . '?mailbox=' . htmlentities($saved_attributes['saved_folder'][$i])
309 . '&amp;what=' . htmlentities($saved_attributes['saved_what'][$i])
310 . '&amp;where=' . htmlentities($saved_attributes['saved_where'][$i])
dcbe1ecc 311 . '>' . _("edit") . '</a>'
88cb1b4d 312 . '&nbsp;|&nbsp;'
dcbe1ecc 313 . '<a href=search.php'
bebf762c 314 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
315 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
316 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
5e9e90fd 317 . '&amp;submit=Search_no_update'
dcbe1ecc 318 . '>' . _("search") . '</a>'
88cb1b4d 319 . '&nbsp;|&nbsp;'
dcbe1ecc 320 . "<a href=search.php?count=$i&amp;submit=delete>"
88cb1b4d 321 . _("delete")
dcbe1ecc 322 . '</a>'
323 . '</td></tr>';
56e0b3b7 324 }
dcbe1ecc 325 echo "</table></td></tr></table>\n";
a7d0eaf6 326}
327
56e0b3b7 328if ($recent_count > 0) {
fab3baa6 329 echo "<br>\n"
330 . html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' )
331 . html_tag( 'tr',
332 html_tag( 'td', '<b>' . _("Recent Searches") . '</b>', 'center' )
333 )
334 . html_tag( 'tr' )
335 . html_tag( 'td' )
336 . html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="0" cellspacing="0" border="0"' );
bebf762c 337 for ($i=1; $i <= $recent_count; ++$i) {
338 if (isset($attributes['search_folder'][$i])) {
339 if ($attributes['search_folder'][$i] == "") {
340 $attributes['search_folder'][$i] = "INBOX";
341 }
184ef883 342 }
343 if ($i % 2) {
fab3baa6 344 echo html_tag( 'tr', '', '', $color[0] );
184ef883 345 } else {
fab3baa6 346 echo html_tag( 'tr', '', '', $color[0] );
184ef883 347 }
bebf762c 348 if (isset($attributes['search_what'][$i]) &&
349 !empty($attributes['search_what'][$i])) {
fab3baa6 350 echo html_tag( 'td', $attributes['search_folder'][$i], 'left', '', 'width="35%"' )
351 . html_tag( 'td', $attributes['search_what'][$i], 'left' )
352 . html_tag( 'td', $attributes['search_where'][$i], 'center' )
353 . html_tag( 'td', '', 'right' )
354 . "<a href=search.php?count=$i&amp;submit=save>"
184ef883 355 . _("save")
fab3baa6 356 . '</a>'
184ef883 357 . '&nbsp;|&nbsp;'
fab3baa6 358 . '<a href=search.php'
bebf762c 359 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
360 . '&amp;what=' . urlencode($attributes['search_what'][$i])
361 . '&amp;where=' . urlencode($attributes['search_where'][$i])
5e9e90fd 362 . '&amp;submit=Search_no_update'
fab3baa6 363 . '>' . _("search") . '</a>'
184ef883 364 . '&nbsp;|&nbsp;'
fab3baa6 365 . "<a href=search.php?count=$i&amp;submit=forget>"
184ef883 366 . _("forget")
fab3baa6 367 . '</a>'
368 . '</td></tr>';
0d672ac0 369 }
bebf762c 370 }
fab3baa6 371 echo '</table></td></tr></table><br>';
56e0b3b7 372}
184ef883 373
d81e351b 374
375if (isset($newsort)) {
376 $sort = $newsort;
377 session_register('sort');
378}
379
380/*********************************************************************
381 * Check to see if we can use cache or not. Currently the only time *
382 * when you will not use it is when a link on the left hand frame is *
383 * used. Also check to make sure we actually have the array in the *
384 * registered session data. :) *
385 *********************************************************************/
386if (! isset($use_mailbox_cache)) {
387 $use_mailbox_cache = 0;
388}
389
390/* There is a problem with registered vars in 4.1 */
391/*
392if( substr( phpversion(), 0, 3 ) == '4.1' ) {
393 $use_mailbox_cache = FALSE;
394}
395*/
396
184ef883 397/* Search Form */
fab3baa6 398echo html_tag( 'div', '<b>' . _("Current Search") . '</b>', 'left' ) . "\n"
399 . '<form action="search.php" name="s">'
400 . html_tag( 'table', '', '', '', 'width="95%" cellpadding="0" cellspacing="0" border="0"' )
401 . html_tag( 'tr' )
402 . html_tag( 'td', '', 'left' )
403 . '<select name="mailbox">';
56e0b3b7 404for ($i = 0; $i < count($boxes); $i++) {
405 if (!in_array('noselect', $boxes[$i]['flags'])) {
406 $box = $boxes[$i]['unformatted'];
46bc57c1 407 $box2 = str_replace(' ', '&nbsp;',
408 imap_utf7_decode_local($boxes[$i]['unformatted-disp']));
3b7d68e6 409 if( $box2 == 'INBOX' ) {
410 $box2 = _("INBOX");
70c4fd84 411 }
fab3baa6 412 echo ' <option value="' . $box . '"';
413 if ($mailbox == $box) { echo ' selected'; }
414 echo '>' . $box2 . '</option>' . "\n";
56e0b3b7 415 }
416}
fab3baa6 417 echo '<option value="All Folders"';
418 if ($mailbox == 'All Folders') {
419 echo ' selected';
0d672ac0 420 }
fab3baa6 421 echo ">All folders</option>\n";
422echo ' </select>'.
423 " </td>\n";
88cb1b4d 424if ( !isset( $what ) ) {
425 $what = '';
426}
e970aa41 427if ( !isset( $where ) ) {
428 $where = '';
429}
430
431
56e0b3b7 432$what_disp = str_replace(',', ' ', $what);
433$what_disp = str_replace('\\\\', '\\', $what_disp);
434$what_disp = str_replace('\\"', '"', $what_disp);
435$what_disp = str_replace('"', '&quot;', $what_disp);
fab3baa6 436echo html_tag( 'td', '<input type="text" size="35" name="what" value="' . $what_disp . '">' . "\n", 'center' )
437 . html_tag( 'td', '', 'right' )
438 . "<select name=\"where\">";
56e0b3b7 439s_opt( 'BODY', $where, _("Body") );
440s_opt( 'TEXT', $where, _("Everywhere") );
441s_opt( 'SUBJECT', $where, _("Subject") );
442s_opt( 'FROM', $where, _("From") );
443s_opt( 'CC', $where, _("Cc") );
444s_opt( 'TO', $where, _("To") );
fab3baa6 445echo " </select>\n" .
446 " </td>\n".
447 html_tag( 'td', '<input type="submit" name="submit" value="' . _("Search") . '">' . "\n", 'center', '', 'colspan="3"' ) .
448 " </tr>\n".
449 "</form>\n".
450 " </table>\n".
451 "</td></tr></table>\n";
56e0b3b7 452
453
88cb1b4d 454do_hook('search_after_form');
56e0b3b7 455
88cb1b4d 456/*
457 search all folders option still in the works. returns a table for each
70c4fd84 458 folder it finds a match in.
88cb1b4d 459*/
56e0b3b7 460
e92eab8b 461$old_value = 0;
794d59c0 462if ($allow_thread_sort == TRUE) {
e92eab8b 463 $old_value = $allow_thread_sort;
794d59c0 464 $allow_thread_sort = FALSE;
e92eab8b 465}
466
88cb1b4d 467if ($search_all == 'all') {
468 $mailbox == '';
56e0b3b7 469 $boxcount = count($boxes);
d81e351b 470 echo '<BR><CENTER><B>' .
88cb1b4d 471 _("Search Results") .
d81e351b 472 "</B><CENTER><BR>\n";
56e0b3b7 473 for ($x=0;$x<$boxcount;$x++) {
474 if (!in_array('noselect', $boxes[$x]['flags'])) {
88cb1b4d 475 $mailbox = $boxes[$x]['unformatted'];
476 }
70c4fd84 477 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
56e0b3b7 478 sqimap_mailbox_select($imapConnection, $mailbox);
d81e351b 479 $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
480 $count_all = count($msgs);
70f1b6b5 481 printSearchMessages($msgs, $mailbox, $count_all, $imapConnection,
d215ca7d 482 $where, $what, false, false);
d81e351b 483 array_push($perbox_count, $count_all);
23a9084b 484 }
485 }
486 for ($i=0;$i<count($perbox_count);$i++) {
59a623e6 487 if ($perbox_count[$i]) {
488 $count_all = true;
e13e66be 489 break;
88cb1b4d 490 }
491 }
59a623e6 492 if (!$count_all) {
493 echo '<br><center>' . _("No Messages Found") . '</center>';
56e0b3b7 494 }
0d672ac0 495}
0d672ac0 496
23a9084b 497/* search one folder option */
56e0b3b7 498else {
70c4fd84 499 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
dcbe1ecc 500 echo '<br>'
501 . html_tag( 'div', '<b>' . _("Search Results") . '</b>', 'center' ) . "\n";
56e0b3b7 502 sqimap_mailbox_select($imapConnection, $mailbox);
d81e351b 503 $msgs = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
59a623e6 504 if (count($msgs)) {
70f1b6b5 505 printSearchMessages($msgs, $mailbox, count($msgs), $imapConnection,
d215ca7d 506 $where, $what, false, false);
59a623e6 507 } else {
508 echo '<br><center>' . _("No Messages Found") . '</center>';
509 }
56e0b3b7 510 }
29eb5486 511}
56e0b3b7 512
23a9084b 513/* must have search terms to search */
70c4fd84 514if ($submit == _("Search") && empty($what)) {
dcbe1ecc 515 echo '<br>'
516 . html_tag( 'div', '<b>Please enter something to search for</b>', 'center' ) . "\n";
56e0b3b7 517}
518
e92eab8b 519$allow_thread_sort = $old_value;
520
d81e351b 521
70c4fd84 522do_hook('search_bottom');
56e0b3b7 523sqimap_logout ($imapConnection);
29eb5486 524echo '</body></html>';
99d2a184 525
91954f9e 526?>