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