thread sort view is now saved per folder thanks Robert Packard
[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
35586184 12require_once('../src/validate.php');
13require_once('../functions/imap.php');
14require_once('../functions/imap_search.php');
15require_once('../functions/array.php');
46d38f78 16require_once('../functions/strings.php');
c61bb006 17
e92eab8b 18global $allow_thread_sort;
56e0b3b7 19
23a9084b 20/* here are some functions, could go in imap_search.php
56e0b3b7 21
23a9084b 22 this was here, pretty handy */
29eb5486 23function s_opt( $val, $sel, $tit ) {
24 echo " <option value=\"$val\"";
0d672ac0 25 if ( $sel == $val ) {
bd9bbfef 26 echo ' selected';
99d2a184 27 }
29eb5486 28 echo ">$tit</option>\n";
29}
99d2a184 30
23a9084b 31/* function to get the recent searches and put them in arrays */
56e0b3b7 32function get_recent($pref_name, $username, $data_dir) {
33 $array = array ();
34 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
35 $n = 0;
36 for ($x=1;$x<=$recent_count;$x++) {
23a9084b 37 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
38 $n++;
56e0b3b7 39 }
40 return $array;
41}
42
23a9084b 43/* function to get the saved searches and put them in arrays */
56e0b3b7 44function get_saved($pref_name, $username, $data_dir) {
45 $array = array ();
46 $n = 0;
47 for ($x=1;;$x++) {
48 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
23a9084b 49 if ($array[$n] == "") {
50 array_pop($array);
51 return $array;
52 }
53 $n++;
56e0b3b7 54 }
55 return $array;
56}
57
23a9084b 58/* function to update recent pref arrays */
59function update_recent($what, $where, $mailbox, $username, $data_dir) {
70c4fd84 60 $what_array = get_recent('search_what', $username, $data_dir);
61 $where_array = get_recent('search_where', $username, $data_dir);
62 $folder_array = get_recent('search_folder', $username, $data_dir);
63 $dupe = 'no';
23a9084b 64 for ($i=0;$i<count($what_array);$i++) {
65 if ($what == $what_array[$i] &&
66 $where == $where_array[$i] &&
67 $mailbox == $folder_array[$i]) {
70c4fd84 68
69 $dupe = 'yes';
23a9084b 70 }
71 }
70c4fd84 72 if ($dupe == 'no') {
23a9084b 73 array_push ($what_array, $what);
74 array_push ($where_array, $where);
75 array_push ($folder_array, $mailbox);
76 array_shift ($what_array);
77 array_shift ($where_array);
78 array_shift ($folder_array);
56e0b3b7 79 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
80 $n=0;
81 for ($i=1;$i<=$recent_count;$i++) {
70c4fd84 82 setPref($data_dir, $username, "search_what$i", $what_array[$n]);
83 setPref($data_dir, $username, "search_where$i", $where_array[$n]);
84 setPref($data_dir, $username, "search_folder$i", $folder_array[$n]);
56e0b3b7 85 $n++;
23a9084b 86 }
56e0b3b7 87 }
88}
89
23a9084b 90/* function to forget a recent search */
56e0b3b7 91function forget_recent($forget_index, $username, $data_dir) {
70c4fd84 92 $what_array = get_recent('search_what', $username, $data_dir);
93 $where_array = get_recent('search_where', $username, $data_dir);
94 $folder_array = get_recent('search_folder', $username, $data_dir);
56e0b3b7 95 array_splice($what_array, $forget_index, 1);
96 array_splice($where_array, $forget_index, 1);
97 array_splice($folder_array, $forget_index, 1);
70c4fd84 98 array_unshift($what_array, '');
99 array_unshift($where_array, '');
100 array_unshift($folder_array, '');
56e0b3b7 101 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
102 $n=0;
103 for ($i=1;$i<=$recent_count;$i++) {
70c4fd84 104 setPref($data_dir, $username, "search_what$i", $what_array[$n]);
105 setPref($data_dir, $username, "search_where$i", $where_array[$n]);
106 setPref($data_dir, $username, "search_folder$i", $folder_array[$n]);
23a9084b 107 $n++;
56e0b3b7 108 }
109}
23a9084b 110
111/* function to delete a saved search */
56e0b3b7 112function delete_saved($delete_index, $username, $data_dir) {
70c4fd84 113 $saved_what_array = get_saved('saved_what', $username, $data_dir);
114 $saved_where_array = get_saved('saved_where', $username, $data_dir);
115 $saved_folder_array = get_saved('saved_folder', $username, $data_dir);
56e0b3b7 116 array_splice($saved_what_array, $delete_index, 1);
117 array_splice($saved_where_array, $delete_index, 1);
118 array_splice($saved_folder_array, $delete_index, 1);
119 $n=0;
120 $saved_count = count($saved_what_array);
121 $last_element = $saved_count + 1;
122 if ($last_element < 1) {
123 for ($i=1;$i<=$saved_count;$i++) {
70c4fd84 124 setPref($data_dir, $username, "saved_what$i", $saved_what_array[$n]);
125 setPref($data_dir, $username, "saved_where$i", $saved_where_array[$n]);
126 setPref($data_dir, $username, "saved_folder$i", $saved_folder_array[$n]);
23a9084b 127 $n++;
56e0b3b7 128 }
129 }
70c4fd84 130 removePref($data_dir, $username, "saved_what$last_element");
131 removePref($data_dir, $username, "saved_where$last_element");
132 removePref($data_dir, $username, "saved_folder$last_element");
133}
6c8388a9 134
23a9084b 135/* function to save a search from recent to saved */
56e0b3b7 136function save_recent($save_index, $username, $data_dir) {
70c4fd84 137 $what_array = get_recent('search_what', $username, $data_dir);
138 $where_array = get_recent('search_where', $username, $data_dir);
139 $folder_array = get_recent('search_folder', $username, $data_dir);
56e0b3b7 140 $saved_what_once = array_slice($what_array, $save_index, 1);
141 $saved_where_once = array_slice($where_array, $save_index, 1);
142 $saved_folder_once = array_slice($folder_array, $save_index, 1);
70c4fd84 143 $saved_array = get_saved('saved_what', $username, $data_dir);
56e0b3b7 144 $saved_count = (count($saved_array) + 1);
70c4fd84 145 setPref($data_dir, $username, "saved_what$saved_count", $saved_what_once[0]);
146 setPref($data_dir, $username, "saved_where$saved_count", $saved_where_once[0]);
147 setPref($data_dir, $username, "saved_folder$saved_count", $saved_folder_once[0]);
56e0b3b7 148}
149
0d672ac0 150/* ------------------------ main ------------------------ */
151
23a9084b 152/* reset these arrays on each page load just in case */
56e0b3b7 153$what_array = array ();
154$where_array = array ();
155$folder_array = array ();
156$saved_what_array = array ();
157$saved_where_array = array ();
158$saved_folder_array = array ();
70c4fd84 159$search_all = 'none';
23a9084b 160$perbox_count = array ();
56e0b3b7 161
23a9084b 162/* get mailbox names */
29eb5486 163$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
c1cb7ba4 164$boxes = sqimap_mailbox_list($imapConnection);
165
23a9084b 166/* set current mailbox to INBOX if none was selected or if page
167 was called to search all folders. */
14c62c12 168if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
56e0b3b7 169 $mailbox = $boxes[0]['unformatted'];
170}
3b7d68e6 171if ($mailbox == 'All Folders') {
70c4fd84 172 $search_all = 'all';
c1cb7ba4 173}
174
175displayPageHeader($color, $mailbox);
c61bb006 176
23a9084b 177/* See how the page was called and fire off correct function */
d4144adf 178if ((!isset($submit) || empty($submit)) && !empty($what)) {
70c4fd84 179 $submit = _("Search");
d4144adf 180}
70c4fd84 181if ( !isset( $submit ) ) {
182 /*
183 Jason, leave E_ALL only on error reporting in php.ini
184 Variables must contain a value befor to check them,
185 or else there are compilation slow down as PHP has
186 to guess.
187 Remove this comment once read 8-)
188 */
189 $submit = '';
190} else if ($submit == _("Search") && !empty($what)) {
191 update_recent($what, $where, $mailbox, $username, $data_dir);
56e0b3b7 192}
88cb1b4d 193elseif ($submit == 'forget') {
56e0b3b7 194 forget_recent($count, $username, $data_dir);
195}
88cb1b4d 196elseif ($submit == 'save') {
56e0b3b7 197 save_recent($count, $username, $data_dir);
198}
88cb1b4d 199elseif ($submit == 'delete') {
56e0b3b7 200 delete_saved($count, $username, $data_dir);
201}
23a9084b 202
29eb5486 203do_hook('search_before_form');
56e0b3b7 204
205echo "<BR>\n".
88cb1b4d 206 "<table width=\"100%\">\n".
207 "<TR><td bgcolor=\"$color[0]\">\n".
208 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
209 "</TD></TR>\n".
210 "</TABLE>\n";
c61bb006 211
23a9084b 212/* update the recent and saved searches from the pref files */
70c4fd84 213$what_array = get_recent('search_what', $username, $data_dir);
214$where_array = get_recent('search_where', $username, $data_dir);
215$folder_array = get_recent('search_folder', $username, $data_dir);
56e0b3b7 216$recent_count = getPref($data_dir, $username, 'search_memory', 0);
70c4fd84 217$saved_what_array = get_saved('saved_what', $username, $data_dir);
218$saved_where_array = get_saved('saved_where', $username, $data_dir);
219$saved_folder_array = get_saved('saved_folder', $username, $data_dir);
56e0b3b7 220$saved_count = count($saved_what_array);
91954f9e 221$count_all = 0;
a7d0eaf6 222
184ef883 223/* Saved Search Table */
56e0b3b7 224if ($saved_count > 0) {
184ef883 225 echo "<BR>\n"
88cb1b4d 226 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
227 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
228 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
184ef883 229 for ($i=0; $i < $saved_count; ++$i) {
88cb1b4d 230 if ($i % 2) {
231 echo "<TR BGCOLOR=\"$color[0]\">";
232 } else {
184ef883 233 echo "<TR BGCOLOR=\"$color[4]\">";
234 }
235 echo "<TD WIDTH=\"35%\">$saved_folder_array[$i]</TD>"
88cb1b4d 236 . "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>"
237 . "<TD ALIGN=CENTER>$saved_where_array[$i]</TD>"
238 . '<TD ALIGN=RIGHT>'
239 . '<A HREF=search.php'
240 . '?mailbox=' . urlencode($saved_folder_array[$i])
5e9e90fd 241 . '&amp;what=' . urlencode($saved_what_array[$i])
242 . '&amp;where=' . urlencode($saved_where_array[$i])
88cb1b4d 243 . '>' . _("edit") . '</A>'
244 . '&nbsp;|&nbsp;'
245 . '<A HREF=search.php'
246 . '?mailbox=' . urlencode($saved_folder_array[$i])
5e9e90fd 247 . '&amp;what=' . urlencode($saved_what_array[$i])
248 . '&amp;where=' . urlencode($saved_where_array[$i])
249 . '&amp;submit=Search_no_update'
88cb1b4d 250 . '>' . _("search") . '</A>'
251 . '&nbsp;|&nbsp;'
5e9e90fd 252 . "<A HREF=search.php?count=$i&amp;submit=delete>"
88cb1b4d 253 . _("delete")
254 . '</A>'
255 . '</TD></TR>';
56e0b3b7 256 }
184ef883 257 echo "</TABLE></TD></TR></TABLE>\n";
a7d0eaf6 258}
259
184ef883 260/* Recent Search Table */
56e0b3b7 261if ($recent_count > 0) {
184ef883 262 echo "<BR>\n"
263 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
962c725d 264 . '<TR><TD ALIGN=CENTER><B>' . _("Recent Searches") . '</B></TD></TR><TR><TD>'
184ef883 265 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
266 for ($i=0; $i < $recent_count; ++$i) {
56e0b3b7 267 if (!empty($what_array[$i])) {
184ef883 268 if ($folder_array[$i] == "") {
269 $folder_array[$i] = "INBOX";
270 }
271 if ($i % 2) {
272 echo "<TR BGCOLOR=\"$color[0]\">";
273 } else {
274 echo "<TR BGCOLOR=\"$color[4]\">";
275 }
276 echo "<TD WIDTH=35%>$folder_array[$i]</TD>"
277 . "<TD ALIGN=LEFT>$what_array[$i]</TD>"
278 . "<TD ALIGN=CENTER>$where_array[$i]</TD>"
279 . '<TD ALIGN=RIGHT>'
5e9e90fd 280 . "<A HREF=search.php?count=$i&amp;submit=save>"
184ef883 281 . _("save")
282 . '</A>'
283 . '&nbsp;|&nbsp;'
284 . '<A HREF=search.php'
285 . '?mailbox=' . urlencode($folder_array[$i])
5e9e90fd 286 . '&amp;what=' . urlencode($what_array[$i])
287 . '&amp;where=' . urlencode($where_array[$i])
288 . '&amp;submit=Search_no_update'
184ef883 289 . '>' . _("search") . '</A>'
290 . '&nbsp;|&nbsp;'
5e9e90fd 291 . "<A HREF=search.php?count=$i&amp;submit=forget>"
184ef883 292 . _("forget")
293 . '</A>'
294 . '</TD></TR>';
0d672ac0 295 }
29eb5486 296 }
184ef883 297 echo '</TABLE></TD></TR></TABLE><BR>';
56e0b3b7 298}
184ef883 299
300/* Search Form */
3b7d68e6 301echo '<B>' . _("Current Search") . '</B>'
f1c7f8bb 302 . '<FORM ACTION="search.php" NAME=s>'
184ef883 303 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
304 . ' <TR>'
305 . ' <TD><SELECT NAME="mailbox">';
56e0b3b7 306for ($i = 0; $i < count($boxes); $i++) {
307 if (!in_array('noselect', $boxes[$i]['flags'])) {
308 $box = $boxes[$i]['unformatted'];
309 $box2 = str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']);
3b7d68e6 310 if( $box2 == 'INBOX' ) {
311 $box2 = _("INBOX");
70c4fd84 312 }
56e0b3b7 313 if ($mailbox == $box) {
314 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
0d672ac0 315 }
56e0b3b7 316 else {
317 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
88cb1b4d 318 }
56e0b3b7 319 }
320}
6c8388a9 321 echo "<OPTION VALUE=\"All Folders\"";
322 if ($mailbox == "All Folders") {
56e0b3b7 323 echo "SELECTED";
0d672ac0 324 }
56e0b3b7 325 echo ">All folders</OPTION>\n";
326echo ' </SELECT>'.
327 " </TD>\n".
328 " <TD ALIGN=\"CENTER\">\n";
88cb1b4d 329if ( !isset( $what ) ) {
330 $what = '';
331}
56e0b3b7 332$what_disp = str_replace(',', ' ', $what);
333$what_disp = str_replace('\\\\', '\\', $what_disp);
334$what_disp = str_replace('\\"', '"', $what_disp);
335$what_disp = str_replace('"', '&quot;', $what_disp);
336echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
337 " </TD>\n".
338 "<TD ALIGN=\"RIGHT\">\n".
339 "<SELECT NAME=\"where\">";
340s_opt( 'BODY', $where, _("Body") );
341s_opt( 'TEXT', $where, _("Everywhere") );
342s_opt( 'SUBJECT', $where, _("Subject") );
343s_opt( 'FROM', $where, _("From") );
344s_opt( 'CC', $where, _("Cc") );
345s_opt( 'TO', $where, _("To") );
346echo " </SELECT>\n" .
347 " </TD>\n".
348 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
70c4fd84 349 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"" . _("Search") . "\">\n".
56e0b3b7 350 " </TD>\n".
351 " </TR>\n".
352 "</FORM>\n".
353 " </TABLE>\n".
354 "</TD></TR></TABLE>\n";
355
356
88cb1b4d 357do_hook('search_after_form');
56e0b3b7 358
88cb1b4d 359/*
360 search all folders option still in the works. returns a table for each
70c4fd84 361 folder it finds a match in.
88cb1b4d 362*/
56e0b3b7 363
e92eab8b 364$old_value = 0;
365if ($allow_thread_sort == true) {
366 $old_value = $allow_thread_sort;
367 $allow_thread_sort = false;
368}
369
88cb1b4d 370if ($search_all == 'all') {
371 $mailbox == '';
56e0b3b7 372 $boxcount = count($boxes);
88cb1b4d 373 echo '<BR><CENTER><B>' .
374 _("Search Results") .
375 "</B><CENTER><BR>\n";
56e0b3b7 376 for ($x=0;$x<$boxcount;$x++) {
377 if (!in_array('noselect', $boxes[$x]['flags'])) {
88cb1b4d 378 $mailbox = $boxes[$x]['unformatted'];
379 }
70c4fd84 380 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
56e0b3b7 381 sqimap_mailbox_select($imapConnection, $mailbox);
88cb1b4d 382 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
23a9084b 383 array_push($perbox_count, $count_all);
384 }
385 }
386 for ($i=0;$i<count($perbox_count);$i++) {
387 if ($perbox_count[$i] != "") {
388 break;
88cb1b4d 389 }
23a9084b 390 $count_all = "none";
88cb1b4d 391 }
23a9084b 392 if ($count_all == "none") {
88cb1b4d 393 echo '<br><b>' .
394 _("No Messages found") .
395 '</b><br>';
56e0b3b7 396 }
0d672ac0 397}
0d672ac0 398
23a9084b 399/* search one folder option */
56e0b3b7 400else {
70c4fd84 401 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
88cb1b4d 402 echo '<BR><CENTER><B>' .
403 _("Search Results") .
404 "</B></CENTER>\n";
56e0b3b7 405 sqimap_mailbox_select($imapConnection, $mailbox);
88cb1b4d 406 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
56e0b3b7 407 }
29eb5486 408}
56e0b3b7 409
23a9084b 410/* must have search terms to search */
70c4fd84 411if ($submit == _("Search") && empty($what)) {
56e0b3b7 412 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
413}
414
e92eab8b 415$allow_thread_sort = $old_value;
416
70c4fd84 417do_hook('search_bottom');
56e0b3b7 418sqimap_logout ($imapConnection);
29eb5486 419echo '</body></html>';
99d2a184 420
91954f9e 421?>