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