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'); |
15 | require_once('../functions/array.php'); |
46d38f78 |
16 | require_once('../functions/strings.php'); |
c61bb006 |
17 | |
56e0b3b7 |
18 | |
19 | // here are some functions, could go in imap_search.php |
20 | // this was here, pretty handy |
21 | |
29eb5486 |
22 | function s_opt( $val, $sel, $tit ) { |
23 | echo " <option value=\"$val\""; |
0d672ac0 |
24 | if ( $sel == $val ) { |
bd9bbfef |
25 | echo ' selected'; |
99d2a184 |
26 | } |
29eb5486 |
27 | echo ">$tit</option>\n"; |
28 | } |
99d2a184 |
29 | |
56e0b3b7 |
30 | // function to get the recent searches and put them in arrays |
31 | |
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 | |
45 | function get_saved($pref_name, $username, $data_dir) { |
46 | $array = array (); |
47 | $n = 0; |
48 | for ($x=1;;$x++) { |
49 | $array[$n] = getPref($data_dir, $username, "$pref_name" . "$x", ""); |
50 | if ($array[$n] == "") { |
51 | array_pop($array); |
52 | return $array; |
53 | } |
54 | $n++; |
55 | } |
56 | return $array; |
57 | } |
58 | |
59 | // function to update pref file with recent searches |
60 | |
61 | function update_recent($array, $recent_value, $pref_name, $username, $data_dir) { |
62 | $array = get_recent($pref_name, $username, $data_dir); |
63 | array_push ($array, $recent_value); |
64 | array_shift ($array); |
65 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
66 | $n=0; |
67 | for ($i=1;$i<=$recent_count;$i++) { |
68 | setPref($data_dir, $username, "$pref_name" . "$i", $array[$n]); |
69 | $n++; |
70 | } |
71 | } |
72 | |
73 | // function to "forget" a recent search |
74 | |
75 | function forget_recent($forget_index, $username, $data_dir) { |
76 | $what_array = get_recent("search_what", $username, $data_dir); |
77 | $where_array = get_recent("search_where", $username, $data_dir); |
78 | $folder_array = get_recent("search_folder", $username, $data_dir); |
79 | array_splice($what_array, $forget_index, 1); |
80 | array_splice($where_array, $forget_index, 1); |
81 | array_splice($folder_array, $forget_index, 1); |
82 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
83 | $n=0; |
84 | for ($i=1;$i<=$recent_count;$i++) { |
85 | setPref($data_dir, $username, "search_what" . "$i", $what_array[$n]); |
86 | setPref($data_dir, $username, "search_where" . "$i", $where_array[$n]); |
87 | setPref($data_dir, $username, "search_folder" . "$i", $folder_array[$n]); |
88 | $n++; |
89 | } |
6c8388a9 |
90 | |
91 | // function to delete a saved search |
56e0b3b7 |
92 | } |
93 | function delete_saved($delete_index, $username, $data_dir) { |
94 | $saved_what_array = get_saved("saved_what", $username, $data_dir); |
95 | $saved_where_array = get_saved("saved_where", $username, $data_dir); |
96 | $saved_folder_array = get_saved("saved_folder", $username, $data_dir); |
97 | array_splice($saved_what_array, $delete_index, 1); |
98 | array_splice($saved_where_array, $delete_index, 1); |
99 | array_splice($saved_folder_array, $delete_index, 1); |
100 | $n=0; |
101 | $saved_count = count($saved_what_array); |
102 | $last_element = $saved_count + 1; |
103 | if ($last_element < 1) { |
104 | for ($i=1;$i<=$saved_count;$i++) { |
105 | setPref($data_dir, $username, "saved_what" . "$i", $saved_what_array[$n]); |
106 | setPref($data_dir, $username, "saved_where" . "$i", $saved_where_array[$n]); |
107 | setPref($data_dir, $username, "saved_folder" . "$i", $saved_folder_array[$n]); |
108 | $n++; |
109 | } |
110 | } |
111 | removePref($data_dir, $username, "saved_what" . "$last_element"); |
112 | removePref($data_dir, $username, "saved_where" . "$last_element"); |
113 | removePref($data_dir, $username, "saved_folder" . "$last_element"); |
114 | } |
6c8388a9 |
115 | |
116 | // function to save a search from recent to saved |
117 | |
56e0b3b7 |
118 | function save_recent($save_index, $username, $data_dir) { |
119 | $what_array = get_recent("search_what", $username, $data_dir); |
120 | $where_array = get_recent("search_where", $username, $data_dir); |
121 | $folder_array = get_recent("search_folder", $username, $data_dir); |
122 | $saved_what_once = array_slice($what_array, $save_index, 1); |
123 | $saved_where_once = array_slice($where_array, $save_index, 1); |
124 | $saved_folder_once = array_slice($folder_array, $save_index, 1); |
125 | $saved_array = get_saved("saved_what", $username, $data_dir); |
126 | $saved_count = (count($saved_array) + 1); |
127 | setPref($data_dir, $username, "saved_what" . "$saved_count", $saved_what_once[0]); |
128 | setPref($data_dir, $username, "saved_where" . "$saved_count", $saved_where_once[0]); |
129 | setPref($data_dir, $username, "saved_folder" . "$saved_count", $saved_folder_once[0]); |
130 | } |
131 | |
132 | |
133 | |
0d672ac0 |
134 | /* ------------------------ main ------------------------ */ |
135 | |
56e0b3b7 |
136 | // reset these arrays on each page load just in case |
137 | |
138 | $what_array = array (); |
139 | $where_array = array (); |
140 | $folder_array = array (); |
141 | $saved_what_array = array (); |
142 | $saved_where_array = array (); |
143 | $saved_folder_array = array (); |
144 | $search_all = "none"; |
145 | |
146 | // get mailbox names |
147 | |
29eb5486 |
148 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
c1cb7ba4 |
149 | $boxes = sqimap_mailbox_list($imapConnection); |
150 | |
56e0b3b7 |
151 | |
152 | // set current mailbox to INBOX if none was selected or if page |
153 | // was called to search all folders. |
154 | |
155 | if ($mailbox == 'None' || $mailbox == "" ) { |
156 | $mailbox = $boxes[0]['unformatted']; |
157 | } |
6c8388a9 |
158 | if ($mailbox == "All Folders") { |
56e0b3b7 |
159 | $search_all = "all"; |
c1cb7ba4 |
160 | } |
161 | |
56e0b3b7 |
162 | // page headers |
163 | |
c1cb7ba4 |
164 | displayPageHeader($color, $mailbox); |
c61bb006 |
165 | |
56e0b3b7 |
166 | // if the page is called from a search link or button update recent values |
167 | // in pref files here |
0d672ac0 |
168 | |
56e0b3b7 |
169 | if ($submit == "Search" && !empty($what)) { |
170 | update_recent($what_array, $what, "search_what", $username, $data_dir); |
171 | update_recent($where_array, $where, "search_where", $username, $data_dir); |
172 | update_recent($folder_array, $mailbox, "search_folder", $username, $data_dir); |
173 | } |
174 | // if the page is called from a "forget recent" link remove search from pref file |
175 | elseif ($submit == "forget") { |
176 | forget_recent($count, $username, $data_dir); |
177 | } |
178 | // if the page is called from a "save recent" link add search to saved searches |
179 | elseif ($submit == "save") { |
180 | save_recent($count, $username, $data_dir); |
181 | } |
182 | elseif ($submit == "delete") { |
183 | delete_saved($count, $username, $data_dir); |
184 | } |
185 | // if the page is called from a "delete saved" link delete saved search |
29eb5486 |
186 | do_hook('search_before_form'); |
56e0b3b7 |
187 | |
188 | echo "<BR>\n". |
a7d0eaf6 |
189 | " <table width=\"100%\" align=center cellpadding=2 cellspacing=0 border=0>\n". |
56e0b3b7 |
190 | " <TR><td bgcolor=\"$color[0]\">\n". |
191 | " <CENTER><B>"._("Search")."</B></CENTER>\n". |
192 | " </TD></TR>\n". |
193 | ' <TR><td align=center>'; |
c61bb006 |
194 | |
56e0b3b7 |
195 | // update the recent and saved searches from the pref files |
a7d0eaf6 |
196 | |
56e0b3b7 |
197 | $what_array = get_recent("search_what", $username, $data_dir); |
198 | $where_array = get_recent("search_where", $username, $data_dir); |
199 | $folder_array = get_recent("search_folder", $username, $data_dir); |
200 | $recent_count = getPref($data_dir, $username, 'search_memory', 0); |
201 | $saved_what_array = get_saved("saved_what", $username, $data_dir); |
202 | $saved_where_array = get_saved("saved_where", $username, $data_dir); |
203 | $saved_folder_array = get_saved("saved_folder", $username, $data_dir); |
204 | $saved_count = count($saved_what_array); |
a7d0eaf6 |
205 | |
56e0b3b7 |
206 | // saved search table |
207 | if ($saved_count > 0) { |
208 | echo "<BR><B>Saved Searches</B>\n". |
209 | "<TABLE WIDTH=\"95%\" CELLPADDING=0 CELLSPACING=0 BGCOLOR=\"$color[0]\">\n"; |
210 | for ($i=0;$i<$saved_count;$i++) { |
211 | echo "<TR><TD WIDTH=35%>$saved_folder_array[$i]</TD>\n". |
212 | "<TD ALIGN=LEFT>$saved_what_array[$i]</TD>\n". |
213 | "<TD ALIGN=RIGHT>$saved_where_array[$i] \n". |
214 | "<A HREF=search.php?". |
215 | "mailbox=", urlencode($saved_folder_array[$i]), "&". |
216 | "what=", urlencode($saved_what_array[$i]), "&". |
217 | "where=", urlencode($saved_where_array[$i]), ">". |
218 | "edit</A>\n". |
219 | " | <A HREF=search.php?". |
220 | "mailbox=", urlencode($saved_folder_array[$i]), "&". |
221 | "what=", urlencode($saved_what_array[$i]), "&". |
222 | "where=", urlencode($saved_where_array[$i]), "&". |
223 | "submit=Search_no_update>search</A>\n". |
224 | " | <A HREF=search.php?count=$i&submit=delete>delete</A>". |
225 | " "; |
226 | } |
227 | echo "</TABLE>\n"; |
a7d0eaf6 |
228 | } |
229 | |
56e0b3b7 |
230 | // recent search table |
c61bb006 |
231 | |
56e0b3b7 |
232 | if ($recent_count > 0) { |
233 | echo "<BR><B>Recent Searches</B>\n". |
234 | "<TABLE WIDTH=\"95%\" CELLPADDING=0 CELLSPACING=0 BGCOLOR=\"$color[0]\">\n"; |
235 | for ($i=0;$i<$recent_count;$i++) { |
236 | if (!empty($what_array[$i])) { |
237 | if ($folder_array[$i] == "") { |
238 | $folder_array[$i] = "INBOX"; |
239 | } |
240 | echo "<TR><TD WIDTH=35%>$folder_array[$i]</TD>\n". |
241 | "<TD ALIGN=LEFT>$what_array[$i]</TD>\n". |
242 | "<TD ALIGN=RIGHT>$where_array[$i] ". |
243 | "<A HREF=search.php?". |
244 | "count=$i&submit=save". |
245 | ">save</A>\n". |
246 | " | <A HREF=search.php?". |
247 | "mailbox=", urlencode($folder_array[$i]), "&". |
248 | "what=", urlencode($what_array[$i]), "&". |
249 | "where=", urlencode($where_array[$i]), "&". |
250 | "submit=Search_no_update>search</A>\n". |
251 | " | <A HREF=search.php?count=$i&submit=forget>forget</A>". |
252 | " </TD></TR>\n"; |
0d672ac0 |
253 | } |
29eb5486 |
254 | } |
56e0b3b7 |
255 | echo "</TABLE>\n". |
256 | "<BR>\n"; |
257 | } |
258 | // search form |
259 | echo "<B>Current Search</B>"; |
260 | echo " <TABLE WIDTH=\"95%\" CELLPADDING=0 CELLSPACING=0>\n"; |
261 | echo "<FORM ACTION=\"search.php\" NAME=s>\n". |
262 | " <TR >\n". |
263 | ' <TD><SELECT NAME="mailbox">'; |
264 | for ($i = 0; $i < count($boxes); $i++) { |
265 | if (!in_array('noselect', $boxes[$i]['flags'])) { |
266 | $box = $boxes[$i]['unformatted']; |
267 | $box2 = str_replace(' ', ' ', $boxes[$i]['unformatted-disp']); |
268 | if ($mailbox == $box) { |
269 | echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n"; |
0d672ac0 |
270 | } |
56e0b3b7 |
271 | else { |
272 | echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n"; |
273 | } |
274 | } |
275 | } |
6c8388a9 |
276 | echo "<OPTION VALUE=\"All Folders\""; |
277 | if ($mailbox == "All Folders") { |
56e0b3b7 |
278 | echo "SELECTED"; |
0d672ac0 |
279 | } |
56e0b3b7 |
280 | echo ">All folders</OPTION>\n"; |
281 | echo ' </SELECT>'. |
282 | " </TD>\n". |
283 | " <TD ALIGN=\"CENTER\">\n"; |
284 | $what_disp = str_replace(',', ' ', $what); |
285 | $what_disp = str_replace('\\\\', '\\', $what_disp); |
286 | $what_disp = str_replace('\\"', '"', $what_disp); |
287 | $what_disp = str_replace('"', '"', $what_disp); |
288 | echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n". |
289 | " </TD>\n". |
290 | "<TD ALIGN=\"RIGHT\">\n". |
291 | "<SELECT NAME=\"where\">"; |
292 | s_opt( 'BODY', $where, _("Body") ); |
293 | s_opt( 'TEXT', $where, _("Everywhere") ); |
294 | s_opt( 'SUBJECT', $where, _("Subject") ); |
295 | s_opt( 'FROM', $where, _("From") ); |
296 | s_opt( 'CC', $where, _("Cc") ); |
297 | s_opt( 'TO', $where, _("To") ); |
298 | echo " </SELECT>\n" . |
299 | " </TD>\n". |
300 | " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n". |
301 | " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Search\">\n". |
302 | " </TD>\n". |
303 | " </TR>\n". |
304 | "</FORM>\n". |
305 | " </TABLE>\n". |
306 | "</TD></TR></TABLE>\n"; |
307 | |
308 | |
29eb5486 |
309 | do_hook("search_after_form"); |
56e0b3b7 |
310 | |
311 | // search all folders option still in the works. returns a table for each |
312 | // folder it finds a match in. The toggle all link does not work |
313 | |
314 | |
315 | if ($search_all == "all") { |
316 | $mailbox == ""; |
317 | $boxcount = count($boxes); |
318 | echo "<BR><CENTER><B>Search Results</B><CENTER><BR>\n"; |
319 | for ($x=0;$x<$boxcount;$x++) { |
320 | if (!in_array('noselect', $boxes[$x]['flags'])) { |
321 | $mailbox = $boxes[$x]['unformatted']; |
322 | } |
323 | echo "<BR><CENTER><B>Folder: $mailbox</CENTER></B>"; |
324 | if (($submit == "Search" || $submit == "Search_no_update") && !empty($what)) { |
325 | sqimap_mailbox_select($imapConnection, $mailbox); |
326 | sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos); |
327 | } |
328 | } |
0d672ac0 |
329 | } |
0d672ac0 |
330 | |
56e0b3b7 |
331 | // search one folder option |
332 | |
333 | else { |
334 | if (($submit == "Search" || $submit == "Search_no_update") && !empty($what)) { |
335 | echo "<BR><CENTER><B>Search Results</B></CENTER>\n"; |
336 | sqimap_mailbox_select($imapConnection, $mailbox); |
337 | sqimap_search($imapConnection, $where, $what, $mailbox, $color, $pos); |
338 | } |
29eb5486 |
339 | } |
56e0b3b7 |
340 | |
341 | // must have search terms to search |
342 | |
343 | if ($submit == "Search" && empty($what)) { |
344 | echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n"; |
345 | } |
346 | |
29eb5486 |
347 | do_hook("search_bottom"); |
99d2a184 |
348 | |
56e0b3b7 |
349 | // all done |
350 | |
351 | sqimap_logout ($imapConnection); |
29eb5486 |
352 | echo '</body></html>'; |
99d2a184 |
353 | |
43fdb2a4 |
354 | ?> |