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