disable thread sort from search page output, not compatible with the search page...
[squirrelmail.git] / src / search.php
1 <?php
2
3 /**
4 * search.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
11
12 require_once('../src/validate.php');
13 require_once('../functions/imap.php');
14 require_once('../functions/imap_search.php');
15 require_once('../functions/array.php');
16 require_once('../functions/strings.php');
17
18 global $allow_thread_sort;
19
20 /* here are some functions, could go in imap_search.php
21
22 this was here, pretty handy */
23 function s_opt( $val, $sel, $tit ) {
24 echo " <option value=\"$val\"";
25 if ( $sel == $val ) {
26 echo ' selected';
27 }
28 echo ">$tit</option>\n";
29 }
30
31 /* function to get the recent searches and put them in arrays */
32 function 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++) {
37 $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", "");
38 $n++;
39 }
40 return $array;
41 }
42
43 /* function to get the saved searches and put them in arrays */
44 function 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", "");
49 if ($array[$n] == "") {
50 array_pop($array);
51 return $array;
52 }
53 $n++;
54 }
55 return $array;
56 }
57
58 /* function to update recent pref arrays */
59 function update_recent($what, $where, $mailbox, $username, $data_dir) {
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';
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]) {
68
69 $dupe = 'yes';
70 }
71 }
72 if ($dupe == 'no') {
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);
79 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
80 $n=0;
81 for ($i=1;$i<=$recent_count;$i++) {
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]);
85 $n++;
86 }
87 }
88 }
89
90 /* function to forget a recent search */
91 function forget_recent($forget_index, $username, $data_dir) {
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);
95 array_splice($what_array, $forget_index, 1);
96 array_splice($where_array, $forget_index, 1);
97 array_splice($folder_array, $forget_index, 1);
98 array_unshift($what_array, '');
99 array_unshift($where_array, '');
100 array_unshift($folder_array, '');
101 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
102 $n=0;
103 for ($i=1;$i<=$recent_count;$i++) {
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]);
107 $n++;
108 }
109 }
110
111 /* function to delete a saved search */
112 function delete_saved($delete_index, $username, $data_dir) {
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);
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++) {
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]);
127 $n++;
128 }
129 }
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 }
134
135 /* function to save a search from recent to saved */
136 function save_recent($save_index, $username, $data_dir) {
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);
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);
143 $saved_array = get_saved('saved_what', $username, $data_dir);
144 $saved_count = (count($saved_array) + 1);
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]);
148 }
149
150 /* ------------------------ main ------------------------ */
151
152 /* reset these arrays on each page load just in case */
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 ();
159 $search_all = 'none';
160 $perbox_count = array ();
161
162 /* get mailbox names */
163 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
164 $boxes = sqimap_mailbox_list($imapConnection);
165
166 /* set current mailbox to INBOX if none was selected or if page
167 was called to search all folders. */
168 if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
169 $mailbox = $boxes[0]['unformatted'];
170 }
171 if ($mailbox == 'All Folders') {
172 $search_all = 'all';
173 }
174
175 displayPageHeader($color, $mailbox);
176
177 /* See how the page was called and fire off correct function */
178 if ((!isset($submit) || empty($submit)) && !empty($what)) {
179 $submit = _("Search");
180 }
181 if ( !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);
192 }
193 elseif ($submit == 'forget') {
194 forget_recent($count, $username, $data_dir);
195 }
196 elseif ($submit == 'save') {
197 save_recent($count, $username, $data_dir);
198 }
199 elseif ($submit == 'delete') {
200 delete_saved($count, $username, $data_dir);
201 }
202
203 do_hook('search_before_form');
204
205 echo "<BR>\n".
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";
211
212 /* update the recent and saved searches from the pref files */
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);
216 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
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);
220 $saved_count = count($saved_what_array);
221 $count_all = 0;
222
223 /* Saved Search Table */
224 if ($saved_count > 0) {
225 echo "<BR>\n"
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>';
229 for ($i=0; $i < $saved_count; ++$i) {
230 if ($i % 2) {
231 echo "<TR BGCOLOR=\"$color[0]\">";
232 } else {
233 echo "<TR BGCOLOR=\"$color[4]\">";
234 }
235 echo "<TD WIDTH=\"35%\">$saved_folder_array[$i]</TD>"
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])
241 . '&amp;what=' . urlencode($saved_what_array[$i])
242 . '&amp;where=' . urlencode($saved_where_array[$i])
243 . '>' . _("edit") . '</A>'
244 . '&nbsp;|&nbsp;'
245 . '<A HREF=search.php'
246 . '?mailbox=' . urlencode($saved_folder_array[$i])
247 . '&amp;what=' . urlencode($saved_what_array[$i])
248 . '&amp;where=' . urlencode($saved_where_array[$i])
249 . '&amp;submit=Search_no_update'
250 . '>' . _("search") . '</A>'
251 . '&nbsp;|&nbsp;'
252 . "<A HREF=search.php?count=$i&amp;submit=delete>"
253 . _("delete")
254 . '</A>'
255 . '</TD></TR>';
256 }
257 echo "</TABLE></TD></TR></TABLE>\n";
258 }
259
260 /* Recent Search Table */
261 if ($recent_count > 0) {
262 echo "<BR>\n"
263 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
264 . '<TR><TD ALIGN=CENTER><B>' . _("Recent Searches") . '</B></TD></TR><TR><TD>'
265 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
266 for ($i=0; $i < $recent_count; ++$i) {
267 if (!empty($what_array[$i])) {
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>'
280 . "<A HREF=search.php?count=$i&amp;submit=save>"
281 . _("save")
282 . '</A>'
283 . '&nbsp;|&nbsp;'
284 . '<A HREF=search.php'
285 . '?mailbox=' . urlencode($folder_array[$i])
286 . '&amp;what=' . urlencode($what_array[$i])
287 . '&amp;where=' . urlencode($where_array[$i])
288 . '&amp;submit=Search_no_update'
289 . '>' . _("search") . '</A>'
290 . '&nbsp;|&nbsp;'
291 . "<A HREF=search.php?count=$i&amp;submit=forget>"
292 . _("forget")
293 . '</A>'
294 . '</TD></TR>';
295 }
296 }
297 echo '</TABLE></TD></TR></TABLE><BR>';
298 }
299
300 /* Search Form */
301 echo '<B>' . _("Current Search") . '</B>'
302 . '<FORM ACTION="search.php" NAME=s>'
303 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
304 . ' <TR>'
305 . ' <TD><SELECT NAME="mailbox">';
306 for ($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']);
310 if( $box2 == 'INBOX' ) {
311 $box2 = _("INBOX");
312 }
313 if ($mailbox == $box) {
314 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
315 }
316 else {
317 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
318 }
319 }
320 }
321 echo "<OPTION VALUE=\"All Folders\"";
322 if ($mailbox == "All Folders") {
323 echo "SELECTED";
324 }
325 echo ">All folders</OPTION>\n";
326 echo ' </SELECT>'.
327 " </TD>\n".
328 " <TD ALIGN=\"CENTER\">\n";
329 if ( !isset( $what ) ) {
330 $what = '';
331 }
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);
336 echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
337 " </TD>\n".
338 "<TD ALIGN=\"RIGHT\">\n".
339 "<SELECT NAME=\"where\">";
340 s_opt( 'BODY', $where, _("Body") );
341 s_opt( 'TEXT', $where, _("Everywhere") );
342 s_opt( 'SUBJECT', $where, _("Subject") );
343 s_opt( 'FROM', $where, _("From") );
344 s_opt( 'CC', $where, _("Cc") );
345 s_opt( 'TO', $where, _("To") );
346 echo " </SELECT>\n" .
347 " </TD>\n".
348 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
349 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"" . _("Search") . "\">\n".
350 " </TD>\n".
351 " </TR>\n".
352 "</FORM>\n".
353 " </TABLE>\n".
354 "</TD></TR></TABLE>\n";
355
356
357 do_hook('search_after_form');
358
359 /*
360 search all folders option still in the works. returns a table for each
361 folder it finds a match in.
362 */
363
364 $old_value = 0;
365 if ($allow_thread_sort == true) {
366 $old_value = $allow_thread_sort;
367 $allow_thread_sort = false;
368 }
369
370 if ($search_all == 'all') {
371 $mailbox == '';
372 $boxcount = count($boxes);
373 echo '<BR><CENTER><B>' .
374 _("Search Results") .
375 "</B><CENTER><BR>\n";
376 for ($x=0;$x<$boxcount;$x++) {
377 if (!in_array('noselect', $boxes[$x]['flags'])) {
378 $mailbox = $boxes[$x]['unformatted'];
379 }
380 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
381 sqimap_mailbox_select($imapConnection, $mailbox);
382 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
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;
389 }
390 $count_all = "none";
391 }
392 if ($count_all == "none") {
393 echo '<br><b>' .
394 _("No Messages found") .
395 '</b><br>';
396 }
397 }
398
399 /* search one folder option */
400 else {
401 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
402 echo '<BR><CENTER><B>' .
403 _("Search Results") .
404 "</B></CENTER>\n";
405 sqimap_mailbox_select($imapConnection, $mailbox);
406 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
407 }
408 }
409
410 /* must have search terms to search */
411 if ($submit == _("Search") && empty($what)) {
412 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
413 }
414
415 $allow_thread_sort = $old_value;
416
417 do_hook('search_bottom');
418 sqimap_logout ($imapConnection);
419 echo '</body></html>';
420
421 ?>