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 |
12 | require_once('../src/validate.php'); |
13 | require_once('../functions/imap.php'); |
14 | require_once('../functions/imap_search.php'); |
241da12d |
15 | require_once('../functions/imap_utf7_decode_local'); |
35586184 |
16 | require_once('../functions/array.php'); |
46d38f78 |
17 | require_once('../functions/strings.php'); |
c61bb006 |
18 | |
e92eab8b |
19 | global $allow_thread_sort; |
56e0b3b7 |
20 | |
23a9084b |
21 | /* here are some functions, could go in imap_search.php |
56e0b3b7 |
22 | |
23a9084b |
23 | this was here, pretty handy */ |
29eb5486 |
24 | function s_opt( $val, $sel, $tit ) { |
25 | echo " <option value=\"$val\""; |
0d672ac0 |
26 | if ( $sel == $val ) { |
bd9bbfef |
27 | echo ' selected'; |
99d2a184 |
28 | } |
29eb5486 |
29 | echo ">$tit</option>\n"; |
30 | } |
99d2a184 |
31 | |
bebf762c |
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'); |
56e0b3b7 |
36 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
56e0b3b7 |
37 | for ($x=1;$x<=$recent_count;$x++) { |
bebf762c |
38 | reset($types); |
39 | foreach ($types as $key) { |
40 | $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, ""); |
41 | } |
56e0b3b7 |
42 | } |
bebf762c |
43 | return $attributes; |
56e0b3b7 |
44 | } |
45 | |
bebf762c |
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 | } |
56e0b3b7 |
58 | } |
bebf762c |
59 | return $saved_attributes; |
56e0b3b7 |
60 | } |
61 | |
23a9084b |
62 | /* function to update recent pref arrays */ |
63 | function update_recent($what, $where, $mailbox, $username, $data_dir) { |
bebf762c |
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); |
70c4fd84 |
69 | $dupe = 'no'; |
bebf762c |
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 | } |
23a9084b |
77 | } |
78 | } |
70c4fd84 |
79 | if ($dupe == 'no') { |
bebf762c |
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 | } |
56e0b3b7 |
95 | } |
96 | } |
97 | |
23a9084b |
98 | /* function to forget a recent search */ |
56e0b3b7 |
99 | function forget_recent($forget_index, $username, $data_dir) { |
bebf762c |
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); |
56e0b3b7 |
109 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
110 | $n=0; |
111 | for ($i=1;$i<=$recent_count;$i++) { |
bebf762c |
112 | reset($types); |
113 | foreach ($types as $key) { |
114 | setPref($data_dir, $username, $key.$i, $attributes[$key][$n]); |
115 | } |
23a9084b |
116 | $n++; |
56e0b3b7 |
117 | } |
118 | } |
23a9084b |
119 | |
120 | /* function to delete a saved search */ |
56e0b3b7 |
121 | function delete_saved($delete_index, $username, $data_dir) { |
bebf762c |
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); |
56e0b3b7 |
128 | $n=0; |
bebf762c |
129 | $saved_count = count($attributes['saved_what']); |
56e0b3b7 |
130 | $last_element = $saved_count + 1; |
56e0b3b7 |
131 | for ($i=1;$i<=$saved_count;$i++) { |
bebf762c |
132 | reset($types); |
133 | foreach ($types as $key) { |
134 | setPref($data_dir, $username, $key.$i, $attributes[$key][$n]); |
135 | } |
23a9084b |
136 | $n++; |
56e0b3b7 |
137 | } |
bebf762c |
138 | reset($types); |
139 | foreach($types as $key) { |
140 | removePref($data_dir, $username, $key.$last_element); |
56e0b3b7 |
141 | } |
70c4fd84 |
142 | } |
6c8388a9 |
143 | |
23a9084b |
144 | /* function to save a search from recent to saved */ |
56e0b3b7 |
145 | function save_recent($save_index, $username, $data_dir) { |
bebf762c |
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 | } |
56e0b3b7 |
160 | } |
161 | |
0d672ac0 |
162 | /* ------------------------ main ------------------------ */ |
163 | |
23a9084b |
164 | /* reset these arrays on each page load just in case */ |
bebf762c |
165 | $attributes = array (); |
166 | $saved_attributes = array (); |
70c4fd84 |
167 | $search_all = 'none'; |
23a9084b |
168 | $perbox_count = array (); |
aa45f943 |
169 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
56e0b3b7 |
170 | |
23a9084b |
171 | /* get mailbox names */ |
29eb5486 |
172 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
c1cb7ba4 |
173 | $boxes = sqimap_mailbox_list($imapConnection); |
174 | |
23a9084b |
175 | /* set current mailbox to INBOX if none was selected or if page |
176 | was called to search all folders. */ |
14c62c12 |
177 | if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) { |
56e0b3b7 |
178 | $mailbox = $boxes[0]['unformatted']; |
179 | } |
3b7d68e6 |
180 | if ($mailbox == 'All Folders') { |
70c4fd84 |
181 | $search_all = 'all'; |
c1cb7ba4 |
182 | } |
183 | |
548a552a |
184 | if (isset($composenew) && $composenew) { |
185 | displayPageHeader($color, $mailbox, 'comp_in_new();'); |
186 | } else { |
187 | displayPageHeader($color, $mailbox); |
188 | } |
23a9084b |
189 | /* See how the page was called and fire off correct function */ |
d4144adf |
190 | if ((!isset($submit) || empty($submit)) && !empty($what)) { |
70c4fd84 |
191 | $submit = _("Search"); |
d4144adf |
192 | } |
70c4fd84 |
193 | if ( !isset( $submit ) ) { |
70c4fd84 |
194 | $submit = ''; |
195 | } else if ($submit == _("Search") && !empty($what)) { |
aa45f943 |
196 | if ($recent_count > 0) { |
197 | update_recent($what, $where, $mailbox, $username, $data_dir); |
198 | } |
56e0b3b7 |
199 | } |
88cb1b4d |
200 | elseif ($submit == 'forget') { |
56e0b3b7 |
201 | forget_recent($count, $username, $data_dir); |
202 | } |
88cb1b4d |
203 | elseif ($submit == 'save') { |
56e0b3b7 |
204 | save_recent($count, $username, $data_dir); |
205 | } |
88cb1b4d |
206 | elseif ($submit == 'delete') { |
56e0b3b7 |
207 | delete_saved($count, $username, $data_dir); |
208 | } |
23a9084b |
209 | |
29eb5486 |
210 | do_hook('search_before_form'); |
56e0b3b7 |
211 | |
212 | echo "<BR>\n". |
88cb1b4d |
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"; |
c61bb006 |
218 | |
23a9084b |
219 | /* update the recent and saved searches from the pref files */ |
bebf762c |
220 | $attributes = get_recent($username, $data_dir); |
bebf762c |
221 | $saved_attributes = get_saved($username, $data_dir); |
222 | $saved_count = count($saved_attributes['saved_what']); |
91954f9e |
223 | $count_all = 0; |
a7d0eaf6 |
224 | |
184ef883 |
225 | /* Saved Search Table */ |
56e0b3b7 |
226 | if ($saved_count > 0) { |
184ef883 |
227 | echo "<BR>\n" |
88cb1b4d |
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>'; |
184ef883 |
231 | for ($i=0; $i < $saved_count; ++$i) { |
88cb1b4d |
232 | if ($i % 2) { |
233 | echo "<TR BGCOLOR=\"$color[0]\">"; |
234 | } else { |
184ef883 |
235 | echo "<TR BGCOLOR=\"$color[4]\">"; |
236 | } |
bebf762c |
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>" |
88cb1b4d |
240 | . '<TD ALIGN=RIGHT>' |
241 | . '<A HREF=search.php' |
bebf762c |
242 | . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i]) |
243 | . '&what=' . urlencode($saved_attributes['saved_what'][$i]) |
244 | . '&where=' . urlencode($saved_attributes['saved_where'][$i]) |
88cb1b4d |
245 | . '>' . _("edit") . '</A>' |
246 | . ' | ' |
247 | . '<A HREF=search.php' |
bebf762c |
248 | . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i]) |
249 | . '&what=' . urlencode($saved_attributes['saved_what'][$i]) |
250 | . '&where=' . urlencode($saved_attributes['saved_where'][$i]) |
5e9e90fd |
251 | . '&submit=Search_no_update' |
88cb1b4d |
252 | . '>' . _("search") . '</A>' |
253 | . ' | ' |
5e9e90fd |
254 | . "<A HREF=search.php?count=$i&submit=delete>" |
88cb1b4d |
255 | . _("delete") |
256 | . '</A>' |
257 | . '</TD></TR>'; |
56e0b3b7 |
258 | } |
184ef883 |
259 | echo "</TABLE></TD></TR></TABLE>\n"; |
a7d0eaf6 |
260 | } |
261 | |
184ef883 |
262 | /* Recent Search Table */ |
56e0b3b7 |
263 | if ($recent_count > 0) { |
184ef883 |
264 | echo "<BR>\n" |
265 | . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n" |
962c725d |
266 | . '<TR><TD ALIGN=CENTER><B>' . _("Recent Searches") . '</B></TD></TR><TR><TD>' |
184ef883 |
267 | . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>'; |
bebf762c |
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 | } |
184ef883 |
273 | } |
274 | if ($i % 2) { |
275 | echo "<TR BGCOLOR=\"$color[0]\">"; |
276 | } else { |
277 | echo "<TR BGCOLOR=\"$color[4]\">"; |
278 | } |
bebf762c |
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>" |
184ef883 |
284 | . '<TD ALIGN=RIGHT>' |
5e9e90fd |
285 | . "<A HREF=search.php?count=$i&submit=save>" |
184ef883 |
286 | . _("save") |
287 | . '</A>' |
288 | . ' | ' |
289 | . '<A HREF=search.php' |
bebf762c |
290 | . '?mailbox=' . urlencode($attributes['search_folder'][$i]) |
291 | . '&what=' . urlencode($attributes['search_what'][$i]) |
292 | . '&where=' . urlencode($attributes['search_where'][$i]) |
5e9e90fd |
293 | . '&submit=Search_no_update' |
184ef883 |
294 | . '>' . _("search") . '</A>' |
295 | . ' | ' |
5e9e90fd |
296 | . "<A HREF=search.php?count=$i&submit=forget>" |
184ef883 |
297 | . _("forget") |
298 | . '</A>' |
299 | . '</TD></TR>'; |
0d672ac0 |
300 | } |
bebf762c |
301 | } |
184ef883 |
302 | echo '</TABLE></TD></TR></TABLE><BR>'; |
56e0b3b7 |
303 | } |
184ef883 |
304 | |
305 | /* Search Form */ |
3b7d68e6 |
306 | echo '<B>' . _("Current Search") . '</B>' |
f1c7f8bb |
307 | . '<FORM ACTION="search.php" NAME=s>' |
184ef883 |
308 | . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>' |
309 | . ' <TR>' |
310 | . ' <TD><SELECT NAME="mailbox">'; |
56e0b3b7 |
311 | for ($i = 0; $i < count($boxes); $i++) { |
312 | if (!in_array('noselect', $boxes[$i]['flags'])) { |
313 | $box = $boxes[$i]['unformatted']; |
241da12d |
314 | $box2 = imap_utf7_decode_local(str_replace(' ', ' ', $boxes[$i]['unformatted-disp'])); |
3b7d68e6 |
315 | if( $box2 == 'INBOX' ) { |
316 | $box2 = _("INBOX"); |
70c4fd84 |
317 | } |
56e0b3b7 |
318 | if ($mailbox == $box) { |
319 | echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n"; |
0d672ac0 |
320 | } |
56e0b3b7 |
321 | else { |
322 | echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n"; |
88cb1b4d |
323 | } |
56e0b3b7 |
324 | } |
325 | } |
6c8388a9 |
326 | echo "<OPTION VALUE=\"All Folders\""; |
327 | if ($mailbox == "All Folders") { |
56e0b3b7 |
328 | echo "SELECTED"; |
0d672ac0 |
329 | } |
56e0b3b7 |
330 | echo ">All folders</OPTION>\n"; |
331 | echo ' </SELECT>'. |
332 | " </TD>\n". |
333 | " <TD ALIGN=\"CENTER\">\n"; |
88cb1b4d |
334 | if ( !isset( $what ) ) { |
335 | $what = ''; |
336 | } |
e970aa41 |
337 | if ( !isset( $where ) ) { |
338 | $where = ''; |
339 | } |
340 | |
341 | |
56e0b3b7 |
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('"', '"', $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". |
70c4fd84 |
359 | " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"" . _("Search") . "\">\n". |
56e0b3b7 |
360 | " </TD>\n". |
361 | " </TR>\n". |
362 | "</FORM>\n". |
363 | " </TABLE>\n". |
364 | "</TD></TR></TABLE>\n"; |
365 | |
366 | |
88cb1b4d |
367 | do_hook('search_after_form'); |
56e0b3b7 |
368 | |
88cb1b4d |
369 | /* |
370 | search all folders option still in the works. returns a table for each |
70c4fd84 |
371 | folder it finds a match in. |
88cb1b4d |
372 | */ |
56e0b3b7 |
373 | |
e92eab8b |
374 | $old_value = 0; |
794d59c0 |
375 | if ($allow_thread_sort == TRUE) { |
e92eab8b |
376 | $old_value = $allow_thread_sort; |
794d59c0 |
377 | $allow_thread_sort = FALSE; |
e92eab8b |
378 | } |
379 | |
88cb1b4d |
380 | if ($search_all == 'all') { |
381 | $mailbox == ''; |
56e0b3b7 |
382 | $boxcount = count($boxes); |
88cb1b4d |
383 | echo '<BR><CENTER><B>' . |
384 | _("Search Results") . |
385 | "</B><CENTER><BR>\n"; |
56e0b3b7 |
386 | for ($x=0;$x<$boxcount;$x++) { |
387 | if (!in_array('noselect', $boxes[$x]['flags'])) { |
88cb1b4d |
388 | $mailbox = $boxes[$x]['unformatted']; |
389 | } |
70c4fd84 |
390 | if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) { |
56e0b3b7 |
391 | sqimap_mailbox_select($imapConnection, $mailbox); |
88cb1b4d |
392 | $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all); |
23a9084b |
393 | array_push($perbox_count, $count_all); |
394 | } |
395 | } |
396 | for ($i=0;$i<count($perbox_count);$i++) { |
397 | if ($perbox_count[$i] != "") { |
e13e66be |
398 | $count_all = "found"; |
399 | break; |
88cb1b4d |
400 | } |
401 | } |
e13e66be |
402 | if ($count_all != "found") { |
88cb1b4d |
403 | echo '<br><b>' . |
404 | _("No Messages found") . |
405 | '</b><br>'; |
56e0b3b7 |
406 | } |
0d672ac0 |
407 | } |
0d672ac0 |
408 | |
23a9084b |
409 | /* search one folder option */ |
56e0b3b7 |
410 | else { |
70c4fd84 |
411 | if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) { |
88cb1b4d |
412 | echo '<BR><CENTER><B>' . |
413 | _("Search Results") . |
414 | "</B></CENTER>\n"; |
56e0b3b7 |
415 | sqimap_mailbox_select($imapConnection, $mailbox); |
88cb1b4d |
416 | sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all); |
56e0b3b7 |
417 | } |
29eb5486 |
418 | } |
56e0b3b7 |
419 | |
23a9084b |
420 | /* must have search terms to search */ |
70c4fd84 |
421 | if ($submit == _("Search") && empty($what)) { |
56e0b3b7 |
422 | echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n"; |
423 | } |
424 | |
e92eab8b |
425 | $allow_thread_sort = $old_value; |
426 | |
70c4fd84 |
427 | do_hook('search_bottom'); |
56e0b3b7 |
428 | sqimap_logout ($imapConnection); |
29eb5486 |
429 | echo '</body></html>'; |
99d2a184 |
430 | |
91954f9e |
431 | ?> |