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