c61bb006 |
1 | <?php |
245a6892 |
2 | |
35586184 |
3 | /** |
a5ee5ac2 |
4 | * search.php |
35586184 |
5 | * |
82d304a0 |
6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
40fbe929 |
9 | * IMAP search page |
10 | * |
30967a1e |
11 | * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas! |
12 | * |
13 | * @version $Id$ |
8f6f9ba5 |
14 | * @package squirrelmail |
313fd1f4 |
15 | * @link http://www.ietf.org/rfc/rfc3501.txt |
40fbe929 |
16 | * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr |
35586184 |
17 | */ |
2d367c68 |
18 | |
30967a1e |
19 | /** |
20 | * Path for SquirrelMail required files. |
21 | * @ignore |
22 | */ |
86725763 |
23 | define('SM_PATH','../'); |
24 | |
7d60bda5 |
25 | /** SquirrelMail required files. |
26 | */ |
08185f2a |
27 | require_once(SM_PATH . 'include/validate.php'); |
b2e7eb53 |
28 | require_once(SM_PATH . 'functions/strings.php'); |
6b37252b |
29 | require_once(SM_PATH . 'functions/imap_asearch.php'); |
86725763 |
30 | require_once(SM_PATH . 'functions/imap_mailbox.php'); |
b65c5db0 |
31 | require_once(SM_PATH . 'functions/imap_messages.php'); |
0ca9a6ec |
32 | require_once(SM_PATH . 'functions/mime.php'); |
b2e7eb53 |
33 | require_once(SM_PATH . 'functions/mailbox_display.php'); //getButton()... |
c61bb006 |
34 | |
7d60bda5 |
35 | /** Prefs array ordinals. Must match $recent_prefkeys and $saved_prefkeys |
36 | */ |
37 | define('ASEARCH_WHERE', 0); |
38 | define('ASEARCH_MAILBOX', 1); |
39 | define('ASEARCH_WHAT', 2); |
40 | define('ASEARCH_UNOP', 3); |
41 | define('ASEARCH_BIOP', 4); |
42 | define('ASEARCH_EXCLUDE', 5); |
43 | define('ASEARCH_SUB', 6); |
44 | |
45 | /** Builds a href with params |
46 | */ |
47 | function asearch_get_href($params = '') |
48 | { |
49 | $href = 'search.php'; |
50 | if ($params != '') |
51 | $href .= '?' . $params; |
52 | return $href; |
53 | } |
54 | |
55 | /** Builds a [link] |
56 | */ |
94375458 |
57 | function asearch_get_link(&$href, $text, $title = '') |
7d60bda5 |
58 | { |
94375458 |
59 | if ($title != '') |
60 | $title = ' title="' . $title . '"'; |
61 | return '<small>[<a href="' . $href . '"' . $title . '>' . $text . '</a>]</small>'; |
7d60bda5 |
62 | } |
63 | |
64 | /** Builds a toggle [link] |
65 | */ |
94375458 |
66 | function asearch_get_toggle_link($value, $action, $text_array, $title_array = array()) |
7d60bda5 |
67 | { |
94375458 |
68 | return asearch_get_link(asearch_get_href($action . '=' . (int)$value), $text_array[$value], asearch_nz($title_array[$value])); |
7d60bda5 |
69 | } |
70 | |
40fbe929 |
71 | /** |
72 | * @param string $a |
73 | * @param string $b |
74 | * @return bool strcoll()-like result |
75 | */ |
6b37252b |
76 | function asearch_unhtml_strcoll($a, $b) |
77 | { |
78 | return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b)); |
79 | } |
40fbe929 |
80 | |
81 | /** |
7d60bda5 |
82 | * @param string $mailbox mailbox name utf7 encoded inc. special case INBOX |
83 | * @return string mailbox name ready to display (utf7 decoded or localized INBOX) |
40fbe929 |
84 | */ |
6b37252b |
85 | function imap_get_mailbox_display($mailbox) |
86 | { |
3dffe83f |
87 | if (strtoupper($mailbox) == 'INBOX') |
6b37252b |
88 | return _("INBOX"); |
89 | return imap_utf7_decode_local($mailbox); |
90 | } |
56e0b3b7 |
91 | |
40fbe929 |
92 | /** |
7d60bda5 |
93 | * @param string $mailbox mailbox name or special case 'All Folders' |
94 | * @return string mailbox name ready to display (utf7 decoded or localized 'All Folders') |
40fbe929 |
95 | */ |
6b37252b |
96 | function asearch_get_mailbox_display($mailbox) |
97 | { |
98 | if ($mailbox == 'All Folders') |
99 | return _("All Folders"); |
100 | return imap_get_mailbox_display($mailbox); |
101 | } |
0b97a708 |
102 | |
40fbe929 |
103 | /** |
104 | * @param array $color color array |
105 | * @param string $txt text to display |
106 | * @return string title ready to display |
107 | */ |
7d60bda5 |
108 | function asearch_get_title_display(&$color, $txt) |
6b37252b |
109 | { |
110 | return '<b><big>' . $txt . '</big></b>'; |
111 | } |
0b97a708 |
112 | |
40fbe929 |
113 | /** |
114 | * @param array $color color array |
115 | * @param string $txt text to display |
116 | * @return string error text ready to display |
117 | */ |
7d60bda5 |
118 | function asearch_get_error_display(&$color, $txt) |
6b37252b |
119 | { |
05f5b952 |
120 | return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>'; |
0b97a708 |
121 | } |
6b37252b |
122 | |
40fbe929 |
123 | /** |
124 | * @param array $input_array array to serialize |
125 | * @return string a string containing a byte-stream representation of value that can be stored anywhere |
126 | */ |
7d60bda5 |
127 | function asearch_serialize(&$input_array) |
6b37252b |
128 | { |
7d60bda5 |
129 | global $search_advanced; |
130 | if ($search_advanced) |
131 | return serialize($input_array); |
132 | return $input_array[0]; |
0b97a708 |
133 | } |
6b37252b |
134 | |
40fbe929 |
135 | /** |
136 | * @param string $input_string string to unserialize |
137 | * @return array |
138 | */ |
6b37252b |
139 | function asearch_unserialize($input_string) |
140 | { |
7d60bda5 |
141 | global $search_advanced; |
142 | if ($search_advanced) |
143 | return unserialize($input_string); |
144 | return array($input_string); |
0b97a708 |
145 | } |
6b37252b |
146 | |
40fbe929 |
147 | /** |
40fbe929 |
148 | * @param string $key the pref key |
149 | * @param integer $index the pref key index |
150 | * @param string $default default value |
151 | * @return string pref value |
152 | */ |
7d60bda5 |
153 | function asearch_getPref(&$key, $index, $default = '') |
6b37252b |
154 | { |
7d60bda5 |
155 | global $data_dir, $username, $search_advanced; |
156 | return getPref($data_dir, $username, $key . ($index + !$search_advanced), $default); |
0b97a708 |
157 | } |
6b37252b |
158 | |
40fbe929 |
159 | /** |
40fbe929 |
160 | * @param string $key the pref key |
161 | * @param integer $index the pref key index |
162 | * @param string $value pref value to set |
163 | * @return bool status |
164 | */ |
7d60bda5 |
165 | function asearch_setPref(&$key, $index, $value) |
6b37252b |
166 | { |
7d60bda5 |
167 | global $data_dir, $username, $search_advanced; |
168 | return setPref($data_dir, $username, $key . ($index + !$search_advanced), $value); |
0b97a708 |
169 | } |
6b37252b |
170 | |
40fbe929 |
171 | /** |
40fbe929 |
172 | * @param string $key the pref key |
173 | * @param integer $index the pref key index |
174 | * @return bool status |
175 | */ |
7d60bda5 |
176 | function asearch_removePref(&$key, $index) |
6b37252b |
177 | { |
7d60bda5 |
178 | global $data_dir, $username, $search_advanced; |
179 | return removePref($data_dir, $username, $key . ($index + !$search_advanced)); |
0b97a708 |
180 | } |
0b97a708 |
181 | |
7d60bda5 |
182 | /** Sanity checks, done before running the imap command and before calling push_recent |
183 | */ |
184 | function asearch_check_query(&$where_array, &$what_array, &$exclude_array) |
6b37252b |
185 | { |
05f5b952 |
186 | global $imap_asearch_opcodes; |
187 | |
6b37252b |
188 | if (empty($where_array)) |
189 | return _("Please enter something to search for"); |
190 | if (count($exclude_array) == count($where_array)) |
191 | return _("There must be at least one criteria to search for"); |
05f5b952 |
192 | for ($crit_num = 0; $crit_num < count($where_array); $crit_num++) { |
193 | $where = $where_array[$crit_num]; |
194 | $what = $what_array[$crit_num]; |
195 | if (!(($what == '') ^ ($imap_asearch_opcodes[$where] != ''))) |
196 | return _("Error in criteria argument"); |
197 | } |
6b37252b |
198 | return ''; |
56e0b3b7 |
199 | } |
200 | |
7d60bda5 |
201 | /** Read the recent searches from the prefs |
202 | */ |
203 | function asearch_read_recent() |
6b37252b |
204 | { |
7d60bda5 |
205 | global $recent_prefkeys, $search_memory; |
6b37252b |
206 | |
207 | $recent_array = array(); |
7d60bda5 |
208 | $recent_num = 0; |
209 | for ($pref_num = 0; $pref_num < $search_memory; $pref_num++) { |
6b37252b |
210 | foreach ($recent_prefkeys as $prefkey) { |
7d60bda5 |
211 | $pref = asearch_getPref($prefkey, $pref_num); |
6b37252b |
212 | /* if (!empty($pref))*/ |
213 | $recent_array[$prefkey][$recent_num] = $pref; |
214 | } |
215 | if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) { |
216 | foreach ($recent_prefkeys as $key) { |
217 | array_pop($recent_array[$key]); |
218 | } |
7d60bda5 |
219 | // break; //Disabled to support old search code broken prefs |
6b37252b |
220 | } |
7d60bda5 |
221 | else |
222 | $recent_num++; |
6b37252b |
223 | } |
224 | return $recent_array; |
56e0b3b7 |
225 | } |
23a9084b |
226 | |
7d60bda5 |
227 | /** Read the saved searches from the prefs |
228 | */ |
229 | function asearch_read_saved() |
6b37252b |
230 | { |
231 | global $saved_prefkeys; |
232 | |
233 | $saved_array = array(); |
234 | $saved_key = $saved_prefkeys[0]; |
235 | for ($saved_count = 0; ; $saved_count++) { |
7d60bda5 |
236 | $pref = asearch_getPref($saved_key, $saved_count); |
6b37252b |
237 | if (empty($pref)) |
238 | break; |
239 | } |
240 | for ($saved_num = 0; $saved_num < $saved_count; $saved_num++) { |
241 | foreach ($saved_prefkeys as $key) { |
7d60bda5 |
242 | $saved_array[$key][$saved_num] = asearch_getPref($key, $saved_num); |
6b37252b |
243 | } |
244 | } |
245 | return $saved_array; |
246 | } |
247 | |
7d60bda5 |
248 | /** Save a recent search to the prefs |
249 | */ |
250 | function asearch_save_recent($recent_index) |
6b37252b |
251 | { |
252 | global $recent_prefkeys, $saved_prefkeys; |
253 | |
7d60bda5 |
254 | $saved_array = asearch_read_saved(); |
6b37252b |
255 | $saved_index = count($saved_array[$saved_prefkeys[0]]); |
7d60bda5 |
256 | $recent_array = asearch_read_recent(); |
6b37252b |
257 | $n = 0; |
258 | foreach ($recent_prefkeys as $key) { |
259 | $recent_slice = array_slice($recent_array[$key], $recent_index, 1); |
260 | if (!empty($recent_slice[0])) |
7d60bda5 |
261 | asearch_setPref($saved_prefkeys[$n], $saved_index, $recent_slice[0]); |
6b37252b |
262 | else |
7d60bda5 |
263 | asearch_removePref($saved_prefkeys[$n], $saved_index); |
6b37252b |
264 | $n++; |
265 | } |
266 | } |
267 | |
7d60bda5 |
268 | /** Write a recent search to prefs |
269 | */ |
270 | function asearch_write_recent(&$recent_array) |
6b37252b |
271 | { |
7d60bda5 |
272 | global $recent_prefkeys, $search_memory; |
6b37252b |
273 | |
7d60bda5 |
274 | $recent_count = min($search_memory, count($recent_array[$recent_prefkeys[0]])); |
275 | for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) { |
6b37252b |
276 | foreach ($recent_prefkeys as $key) { |
7d60bda5 |
277 | asearch_setPref($key, $recent_num, $recent_array[$key][$recent_num]); |
6b37252b |
278 | } |
279 | } |
7d60bda5 |
280 | for (; $recent_num < $search_memory; $recent_num++) { |
6b37252b |
281 | foreach ($recent_prefkeys as $key) { |
7d60bda5 |
282 | asearch_removePref($key, $recent_num); |
6b37252b |
283 | } |
284 | } |
285 | } |
286 | |
7d60bda5 |
287 | /** Remove a recent search from prefs |
288 | */ |
289 | function asearch_forget_recent($forget_index) |
6b37252b |
290 | { |
291 | global $recent_prefkeys; |
292 | |
7d60bda5 |
293 | $recent_array = asearch_read_recent(); |
6b37252b |
294 | foreach ($recent_prefkeys as $key) { |
295 | array_splice($recent_array[$key], $forget_index, 1); |
296 | } |
7d60bda5 |
297 | asearch_write_recent($recent_array); |
6b37252b |
298 | } |
299 | |
7d60bda5 |
300 | /** Find a recent search in the prefs (used to avoid saving duplicates) |
301 | */ |
302 | function asearch_find_recent(&$recent_array, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array) |
6b37252b |
303 | { |
7d60bda5 |
304 | global $recent_prefkeys, $search_advanced; |
6b37252b |
305 | |
6b37252b |
306 | $where_string = asearch_serialize($where_array); |
7d60bda5 |
307 | $mailbox_string = asearch_serialize($mailbox_array); |
6b37252b |
308 | $what_string = asearch_serialize($what_array); |
7d60bda5 |
309 | $unop_string = asearch_serialize($unop_array); |
310 | if ($search_advanced) { |
311 | $biop_string = asearch_serialize($biop_array); |
312 | $exclude_string = asearch_serialize($exclude_array); |
313 | $sub_string = asearch_serialize($sub_array); |
314 | } |
315 | $recent_count = count($recent_array[$recent_prefkeys[ASEARCH_WHERE]]); |
316 | for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) { |
317 | if (isset($recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num])) { |
6b37252b |
318 | if ( |
7d60bda5 |
319 | $where_string == $recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num] && |
320 | $mailbox_string == $recent_array[$recent_prefkeys[ASEARCH_MAILBOX]][$recent_num] && |
321 | $what_string == $recent_array[$recent_prefkeys[ASEARCH_WHAT]][$recent_num] && |
322 | $unop_string == $recent_array[$recent_prefkeys[ASEARCH_UNOP]][$recent_num] && |
323 | ((!$search_advanced) || |
324 | ($biop_string == $recent_array[$recent_prefkeys[ASEARCH_BIOP]][$recent_num] && |
325 | $exclude_string == $recent_array[$recent_prefkeys[ASEARCH_EXCLUDE]][$recent_num] && |
326 | $sub_string == $recent_array[$recent_prefkeys[ASEARCH_SUB]][$recent_num])) |
6b37252b |
327 | ) |
c2d47d51 |
328 | return $recent_num; |
6b37252b |
329 | } |
330 | } |
c2d47d51 |
331 | return -1; |
6b37252b |
332 | } |
333 | |
7d60bda5 |
334 | /** Push a recent search into the prefs |
335 | */ |
336 | function asearch_push_recent(&$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array) |
6b37252b |
337 | { |
7d60bda5 |
338 | global $recent_prefkeys, $search_memory; |
6b37252b |
339 | |
7d60bda5 |
340 | if ($search_memory > 0) { |
341 | $recent_array = asearch_read_recent(); |
40fbe929 |
342 | $recent_found = asearch_find_recent($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
c2d47d51 |
343 | if ($recent_found >= 0) { // Remove identical recent |
6b37252b |
344 | foreach ($recent_prefkeys as $key) { |
c2d47d51 |
345 | array_splice($recent_array[$key], $recent_found, 1); |
6b37252b |
346 | } |
c2d47d51 |
347 | } |
7d60bda5 |
348 | $input = array($where_array, $mailbox_array, $what_array, $unop_array, $biop_array, $exclude_array, $sub_array); |
c2d47d51 |
349 | $i = 0; |
350 | foreach ($recent_prefkeys as $key) { |
351 | array_unshift($recent_array[$key], asearch_serialize($input[$i])); |
352 | $i++; |
353 | } |
7d60bda5 |
354 | asearch_write_recent($recent_array); |
6b37252b |
355 | } |
356 | } |
357 | |
7d60bda5 |
358 | /** Edit a recent search |
2d2f8bb8 |
359 | * @global array mailbox_array searched mailboxes |
40fbe929 |
360 | */ |
7d60bda5 |
361 | function asearch_edit_recent($index) |
6b37252b |
362 | { |
7d60bda5 |
363 | global $recent_prefkeys, $search_advanced; |
364 | global $where_array, $mailbox_array, $what_array, $unop_array; |
365 | global $biop_array, $exclude_array, $sub_array; |
366 | |
367 | $where_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHERE], $index)); |
368 | $mailbox_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_MAILBOX], $index)); |
369 | $what_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHAT], $index)); |
370 | $unop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_UNOP], $index)); |
371 | if ($search_advanced) { |
372 | $biop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_BIOP], $index)); |
373 | $exclude_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_EXCLUDE], $index)); |
374 | $sub_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_SUB], $index)); |
375 | } |
6b37252b |
376 | } |
377 | |
7d60bda5 |
378 | /** Edit the last recent search if the prefs permit it |
379 | */ |
380 | function asearch_edit_last() |
6b37252b |
381 | { |
7d60bda5 |
382 | global $search_memory; |
383 | if ($search_memory > 0) |
384 | asearch_edit_recent(0); |
6b37252b |
385 | } |
386 | |
7d60bda5 |
387 | /** Edit a saved search |
388 | */ |
389 | function asearch_edit_saved($index) |
6b37252b |
390 | { |
7d60bda5 |
391 | global $saved_prefkeys, $search_advanced; |
392 | global $where_array, $mailbox_array, $what_array, $unop_array; |
393 | global $biop_array, $exclude_array, $sub_array; |
394 | |
395 | $where_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHERE], $index)); |
396 | $mailbox_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_MAILBOX], $index)); |
397 | $what_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHAT], $index)); |
398 | $unop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_UNOP], $index)); |
399 | if ($search_advanced) { |
400 | $biop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_BIOP], $index)); |
401 | $exclude_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_EXCLUDE], $index)); |
402 | $sub_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_SUB], $index)); |
403 | } |
6b37252b |
404 | } |
405 | |
7d60bda5 |
406 | /** Write a saved search to the prefs |
407 | */ |
408 | function asearch_write_saved(&$saved_array) |
6b37252b |
409 | { |
410 | global $saved_prefkeys; |
411 | |
412 | $saved_count = count($saved_array[$saved_prefkeys[0]]); |
413 | for ($saved_num=0; $saved_num < $saved_count; $saved_num++) { |
414 | foreach ($saved_prefkeys as $key) { |
7d60bda5 |
415 | asearch_setPref($key, $saved_num, $saved_array[$key][$saved_num]); |
6b37252b |
416 | } |
417 | } |
418 | foreach ($saved_prefkeys as $key) { |
7d60bda5 |
419 | asearch_removePref($key, $saved_count); |
6b37252b |
420 | } |
421 | } |
422 | |
7d60bda5 |
423 | /** Delete a saved search from the prefs |
424 | */ |
425 | function asearch_delete_saved($saved_index) |
6b37252b |
426 | { |
427 | global $saved_prefkeys; |
428 | |
7d60bda5 |
429 | $saved_array = asearch_read_saved(); |
6b37252b |
430 | $asearch_keys = $saved_prefkeys; |
431 | foreach ($asearch_keys as $key) { |
432 | array_splice($saved_array[$key], $saved_index, 1); |
433 | } |
7d60bda5 |
434 | asearch_write_saved($saved_array); |
6b37252b |
435 | } |
436 | |
7d60bda5 |
437 | /** Translate the input date to imap date to local date display, |
438 | * so the user can know if the date is wrong or illegal |
439 | * @return string locally formatted date or error text |
440 | */ |
441 | function asearch_get_date_display(&$what) |
6b37252b |
442 | { |
443 | $what_parts = sqimap_asearch_parse_date($what); |
444 | if (count($what_parts) == 4) { |
7d60bda5 |
445 | if (checkdate($what_parts[2], $what_parts[1], $what_parts[3])) |
446 | return date_intl(_("M j, Y"), mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3])); |
447 | //return $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3]; |
448 | return _("(Illegal date)"); |
6b37252b |
449 | } |
7d60bda5 |
450 | return _("(Wrong date)"); |
6b37252b |
451 | } |
452 | |
7d60bda5 |
453 | /** Translate the query to rough natural display |
454 | * @return string rough natural query ready to display |
455 | */ |
456 | function asearch_get_query_display(&$color, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array) |
6b37252b |
457 | { |
458 | global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options; |
459 | global $imap_asearch_opcodes; |
460 | |
461 | $last_mailbox = $mailbox_array[0]; |
462 | if (empty($last_mailbox)) |
463 | $last_mailbox = 'INBOX'; |
05f5b952 |
464 | $query_display = ''; |
465 | for ($crit_num=0; $crit_num < count($where_array); $crit_num++) { |
466 | if ((!isset($exclude_array[$crit_num])) || (!$exclude_array[$crit_num])) { |
467 | $cur_mailbox = $mailbox_array[$crit_num]; |
6b37252b |
468 | if (empty($cur_mailbox)) |
469 | $cur_mailbox = 'INBOX'; |
05f5b952 |
470 | $biop = asearch_nz($biop_array[$crit_num]); |
471 | if (($query_display == '') || ($cur_mailbox != $last_mailbox)) { |
6b37252b |
472 | $mailbox_display = ' <B>' . asearch_get_mailbox_display($cur_mailbox) . '</B>'; |
05f5b952 |
473 | if ($query_display == '') |
6b37252b |
474 | $biop_display = _("In"); |
475 | else |
476 | $biop_display = $imap_asearch_biops_in[$biop]; |
477 | $last_mailbox = $cur_mailbox; |
478 | } |
479 | else { |
480 | $mailbox_display = ''; |
481 | $biop_display = $imap_asearch_biops[$biop]; |
482 | } |
483 | $biop_display = ' <U><I>' . $biop_display . '</I></U>'; |
05f5b952 |
484 | $unop = $unop_array[$crit_num]; |
6b37252b |
485 | $unop_display = $imap_asearch_unops[$unop]; |
05f5b952 |
486 | $where = $where_array[$crit_num]; |
7d60bda5 |
487 | $where_display = asearch_nz($imap_asearch_options[$where], $where); |
6b37252b |
488 | if ($unop_display != '') |
489 | $where_display = ' <U><I>' . $unop_display . ' ' . $where_display . '</I></U>'; |
490 | else |
491 | $where_display = ' <U><I>' . $where_display . '</I></U>'; |
492 | $what_type = $imap_asearch_opcodes[$where]; |
05f5b952 |
493 | $what = $what_array[$crit_num]; |
494 | if ($what_type) { /* Check opcode parameter */ |
495 | if ($what == '') |
496 | $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)")); |
6b37252b |
497 | else { |
05f5b952 |
498 | if ($what_type == 'adate') |
499 | $what_display = asearch_get_date_display($what); |
6b37252b |
500 | else |
a722a461 |
501 | $what_display = htmlspecialchars($what); |
05f5b952 |
502 | $what_display = ' <B>' . $what_display . '</B>'; |
6b37252b |
503 | } |
6b37252b |
504 | } |
05f5b952 |
505 | else { |
506 | if ($what) |
507 | $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)")); |
508 | else |
509 | $what_display = ''; |
510 | } |
511 | $query_display .= ' ' . $biop_display . $mailbox_display . $where_display . $what_display; |
6b37252b |
512 | } |
513 | } |
05f5b952 |
514 | return $query_display; |
6b37252b |
515 | } |
516 | |
7d60bda5 |
517 | /** Handle the alternate row colors |
518 | * @return string color value |
519 | */ |
520 | function asearch_get_row_color(&$color, $row_num) |
6b37252b |
521 | { |
522 | /*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/ |
523 | $color_string = $color[4]; |
524 | if ($GLOBALS['alt_index_colors']) { |
525 | if (($row_num % 2) == 0) { |
526 | if (!isset($color[12])) |
527 | $color[12] = '#EAEAEA'; |
528 | $color_string = $color[12]; |
529 | } |
530 | } |
531 | return $color_string; |
532 | } |
533 | |
7d60bda5 |
534 | /** Print a whole query array, recent or saved |
535 | */ |
536 | function asearch_print_query_array(&$boxes, &$query_array, &$query_keys, &$action_array, $title, $show_pref) |
6b37252b |
537 | { |
538 | global $color; |
7d60bda5 |
539 | global $data_dir, $username; |
540 | |
541 | $show_flag = getPref($data_dir, $username, $show_pref, 0) & 1; |
94375458 |
542 | $toggle_link = asearch_get_toggle_link(!$show_flag, $show_pref, array(_("-"), _("+")), array(_("Hide"), _("Show"))); |
6b37252b |
543 | |
544 | echo "<br>\n"; |
920ec5ec |
545 | echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"'); |
94375458 |
546 | echo html_tag('tr', |
547 | html_tag('td', $toggle_link, 'center', $color[5], 'width="5%"') |
548 | . html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=4')); |
7d60bda5 |
549 | if ($show_flag) { |
550 | $main_key = $query_keys[ASEARCH_WHERE]; |
551 | $query_count = count($query_array[$main_key]); |
552 | for ($query_num = 0, $row_num = 0; $query_num < $query_count; $query_num++) { |
553 | if (!empty($query_array[$main_key][$query_num])) { |
554 | echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num)); |
555 | |
556 | unset($search_array); |
557 | foreach ($query_keys as $query_key) { |
558 | $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]); |
559 | } |
560 | $where_array = $search_array[ASEARCH_WHERE]; |
561 | $mailbox_array = $search_array[ASEARCH_MAILBOX]; |
562 | $what_array = $search_array[ASEARCH_WHAT]; |
563 | $unop_array = $search_array[ASEARCH_UNOP]; |
564 | $biop_array = asearch_nz($search_array[ASEARCH_BIOP], array()); |
565 | $exclude_array = asearch_nz($search_array[ASEARCH_EXCLUDE], array()); |
566 | $sub_array = asearch_nz($search_array[ASEARCH_SUB], array()); |
567 | $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
568 | |
569 | echo html_tag('td', $query_num + 1, 'right'); |
570 | echo html_tag('td', $query_display, 'center', '', 'width="80%"'); |
571 | foreach ($action_array as $action => $action_display) { |
572 | echo html_tag('td', '<a href="' . asearch_get_href('submit=' . $action . '&rownum=' . $query_num) . '">' . $action_display . '</a>', 'center'); |
573 | } |
6b37252b |
574 | |
7d60bda5 |
575 | echo '</tr>' . "\n"; |
576 | $row_num++; |
577 | } |
6b37252b |
578 | } |
579 | } |
580 | echo '</table>' . "\n"; |
581 | } |
582 | |
7d60bda5 |
583 | /** Print the saved array |
584 | */ |
585 | function asearch_print_saved(&$boxes) |
6b37252b |
586 | { |
587 | global $saved_prefkeys; |
588 | |
7d60bda5 |
589 | $saved_array = asearch_read_saved(); |
05f5b952 |
590 | if (isset($saved_array[$saved_prefkeys[0]])) { |
591 | $saved_count = count($saved_array[$saved_prefkeys[0]]); |
592 | if ($saved_count > 0) { |
593 | $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete")); |
7d60bda5 |
594 | asearch_print_query_array($boxes, $saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"), 'search_show_saved'); |
05f5b952 |
595 | } |
6b37252b |
596 | } |
70c4fd84 |
597 | } |
6c8388a9 |
598 | |
7d60bda5 |
599 | /** |
600 | * Print the recent array |
601 | */ |
602 | function asearch_print_recent(&$boxes) |
6b37252b |
603 | { |
7d60bda5 |
604 | global $recent_prefkeys, $search_memory; |
6b37252b |
605 | |
7d60bda5 |
606 | $recent_array = asearch_read_recent(); |
05f5b952 |
607 | if (isset($recent_array[$recent_prefkeys[0]])) { |
608 | $recent_count = count($recent_array[$recent_prefkeys[0]]); |
7d60bda5 |
609 | if (min($recent_count, $search_memory) > 0) { |
05f5b952 |
610 | $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget")); |
7d60bda5 |
611 | asearch_print_query_array($boxes, $recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"), 'search_show_recent'); |
05f5b952 |
612 | } |
6b37252b |
613 | } |
56e0b3b7 |
614 | } |
615 | |
7d60bda5 |
616 | /** Build an <option> statement |
617 | */ |
920ec5ec |
618 | function asearch_opt($val, $sel, $tit) |
6b37252b |
619 | { |
f8984459 |
620 | return '<option value="' . $val . '"' . ($sel == $val ? ' selected' : '') . '>' . $tit . '</option>' . "\n"; |
6b37252b |
621 | } |
622 | |
7d60bda5 |
623 | /** Build a <select> statement from an array |
624 | */ |
6b37252b |
625 | function asearch_opt_array($var_name, $opt_array, $cur_val) |
626 | { |
627 | $output = '<select name="' . $var_name . '">' . "\n"; |
628 | foreach($opt_array as $val => $display) |
7d60bda5 |
629 | $output .= asearch_opt($val, $cur_val, asearch_nz($display, $val)); |
6b37252b |
630 | $output .= '</select>' . "\n"; |
631 | return $output; |
632 | } |
633 | |
7d60bda5 |
634 | /** Verify that a mailbox exists |
635 | * @return bool mailbox exists |
636 | */ |
637 | function asearch_mailbox_exists($mailbox, &$boxes) |
920ec5ec |
638 | { |
639 | foreach ($boxes as $box) { |
640 | if ($box['unformatted'] == $mailbox) |
641 | return TRUE; |
642 | } |
643 | return FALSE; |
644 | } |
645 | |
7d60bda5 |
646 | function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num = 0) |
6b37252b |
647 | { |
7d60bda5 |
648 | if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) |
649 | $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox)); |
650 | else |
651 | $missing = ''; |
652 | return '<select name="mailbox[' . $row_num . ']">' |
653 | . $missing |
654 | . asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']') |
655 | . sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes, NULL) |
656 | . '</select>'; |
657 | } |
658 | |
659 | function asearch_get_form_sub($sub, $row_num = 0) |
660 | { |
661 | return function_exists('addCheckBox') ? addCheckBox('sub[' . $row_num .']', $sub) |
662 | : '<input type=checkbox name="sub[' . $row_num .']"' . ($sub ? ' CHECKED' : '') . '>'; |
663 | } |
664 | |
665 | function asearch_get_form_location($unop, $where, $row_num = 0) |
666 | { |
667 | global $imap_asearch_unops, $imap_asearch_options; |
668 | |
669 | return asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop) |
670 | . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where); |
671 | } |
672 | |
673 | function asearch_get_form_what($what, $row_num = 0) |
674 | { |
675 | return function_exists('addInput') ? addInput('what[' . $row_num . ']', $what, '35') |
676 | : '<input type="text" size="35" name="what[' . $row_num . ']" value="' . htmlspecialchars($what) . '">'; |
677 | } |
678 | |
679 | function asearch_get_form_exclude($exclude, $row_num = 0) |
680 | { |
681 | return function_exists('addCheckBox') ? addCheckBox('exclude['.$row_num.']', $exclude) |
682 | : '<input type=checkbox name="exclude[' . $row_num .']"' . ($exclude ? ' CHECKED' : '') . '>'; |
683 | } |
684 | |
685 | /** Print one advanced form row |
686 | */ |
687 | function asearch_print_form_row($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num) |
688 | { |
689 | global $imap_asearch_biops_in; |
6b37252b |
690 | global $color; |
691 | |
920ec5ec |
692 | echo html_tag('tr', '', '', $color[4]); |
6b37252b |
693 | |
7d60bda5 |
694 | //Binary operator |
695 | echo html_tag('td', $row_num ? |
696 | asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop) |
697 | : '<b>' . _("In") . '</b>', 'center') . "\n"; |
698 | |
699 | //Mailbox list and Include Subfolders |
920ec5ec |
700 | echo html_tag('td', |
7d60bda5 |
701 | asearch_get_form_mailbox($imapConnection, $boxes, $mailbox, $row_num) |
702 | . _("and subfolders:") . asearch_get_form_sub($sub, $row_num), 'center') . "\n"; |
6b37252b |
703 | |
7d60bda5 |
704 | //Unary operator and Search location |
705 | echo html_tag('td', asearch_get_form_location($unop, $where, $row_num), 'center') . "\n"; |
6b37252b |
706 | |
7d60bda5 |
707 | //Text input |
708 | echo html_tag('td', asearch_get_form_what($what, $row_num), 'center') . "\n"; |
709 | |
710 | //Exclude criteria |
711 | echo html_tag('td', _("Exclude Criteria:") . asearch_get_form_exclude($exclude, $row_num), 'center') . "\n"; |
6b37252b |
712 | |
713 | echo "</tr>\n"; |
714 | } |
715 | |
7d60bda5 |
716 | /** Print the advanced search form |
717 | */ |
718 | function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array) |
6b37252b |
719 | { |
720 | global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html; |
721 | global $color; |
722 | |
7d60bda5 |
723 | //Search Form |
5a383256 |
724 | echo "<br>\n"; |
7d60bda5 |
725 | echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n"; |
6b37252b |
726 | |
920ec5ec |
727 | echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"'); |
728 | echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5')); |
edda50ae |
729 | $row_count = count($where_array); |
6b37252b |
730 | for ($row_num = 0; $row_num < $row_count; $row_num++) { |
edda50ae |
731 | $mailbox = asearch_nz($mailbox_array[$row_num]); |
6b37252b |
732 | $biop = strip_tags(asearch_nz($biop_array[$row_num])); |
733 | $unop = strip_tags(asearch_nz($unop_array[$row_num])); |
edda50ae |
734 | $where = strip_tags(asearch_nz($where_array[$row_num])); |
6b37252b |
735 | $what = asearch_nz($what_array[$row_num]); |
736 | $exclude = strip_tags(asearch_nz($exclude_array[$row_num])); |
40fbe929 |
737 | $sub = strip_tags(asearch_nz($sub_array[$row_num])); |
738 | asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num); |
6b37252b |
739 | } |
740 | echo '</table>' . "\n"; |
741 | |
7d60bda5 |
742 | //Submit buttons |
920ec5ec |
743 | echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"'); |
6b37252b |
744 | echo html_tag('tr', |
745 | html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n" |
746 | . html_tag('td', getButton('SUBMIT', 'submit', $add_criteria_button_html), 'center') . "\n" |
747 | . html_tag('td', getButton('SUBMIT', 'submit', $del_all_button_html), 'center') . "\n" |
748 | . html_tag('td', getButton('SUBMIT', 'submit', $del_excluded_button_html), 'center') . "\n" |
749 | ); |
750 | echo '</table>' . "\n"; |
751 | echo '</form>' . "\n"; |
752 | } |
753 | |
7d60bda5 |
754 | /** Print one basic form row |
755 | */ |
756 | function asearch_print_form_row_basic($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num) |
757 | { |
758 | global $search_button_html; |
759 | global $color; |
760 | |
761 | echo html_tag('tr', '', '', $color[4]); |
762 | |
763 | //Mailbox list |
764 | echo html_tag('td', '<b>' . _("In") . '</b> ' . asearch_get_form_mailbox($imapConnection, $boxes, $mailbox), 'center') . "\n"; |
765 | |
766 | //Unary operator and Search location |
767 | echo html_tag('td', asearch_get_form_location($unop, $where), 'center') . "\n"; |
768 | |
769 | //Text input |
770 | echo html_tag('td', asearch_get_form_what($what), 'center') . "\n"; |
771 | |
772 | //Submit button |
773 | echo html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n"; |
774 | |
775 | echo "</tr>\n"; |
776 | } |
777 | |
778 | /** Print the basic search form |
779 | */ |
780 | function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array) |
781 | { |
782 | global $color; |
783 | |
784 | //Search Form |
785 | echo "<br>\n"; |
786 | echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n"; |
787 | |
788 | echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"'); |
789 | //echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=4')); |
790 | $row_count = count($where_array); |
791 | for ($row_num = 0; $row_num < $row_count; $row_num++) { |
792 | $mailbox = asearch_nz($mailbox_array[$row_num]); |
793 | $biop = strip_tags(asearch_nz($biop_array[$row_num])); |
794 | $unop = strip_tags(asearch_nz($unop_array[$row_num])); |
795 | $where = strip_tags(asearch_nz($where_array[$row_num])); |
796 | $what = asearch_nz($what_array[$row_num]); |
797 | $exclude = strip_tags(asearch_nz($exclude_array[$row_num])); |
798 | $sub = strip_tags(asearch_nz($sub_array[$row_num])); |
799 | asearch_print_form_row_basic($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num); |
800 | } |
801 | echo '</table>' . "\n"; |
802 | echo '</form>' . "\n"; |
803 | } |
804 | |
805 | /** Print the $msgs messages from $mailbox mailbox |
806 | */ |
9db01361 |
807 | function asearch_print_mailbox_msgs($imapConnection, $mbxresponse, $mailbox, $msgs, $cnt, $sort, $color, $where, $what) |
6b37252b |
808 | { |
cf6cb8b1 |
809 | if ($cnt > 0) { |
c2d47d51 |
810 | global $allow_server_sort, $allow_thread_sort, $thread_sort_messages; |
811 | |
812 | $thread_sort_messages = 0; |
813 | if ($allow_thread_sort) { |
814 | global $data_dir, $username; |
815 | $thread_sort_messages = getPref($data_dir, $username, 'thread_' . $mailbox); |
816 | $msort = $msgs; |
817 | $real_sort = 6; |
818 | } |
819 | elseif ($allow_server_sort) { |
820 | $msort = $msgs; |
821 | $real_sort = 6; |
822 | } |
823 | else { |
824 | $msort = calc_msort($msgs, $sort); |
825 | $real_sort = $sort; |
826 | } |
70f1b6b5 |
827 | |
7d60bda5 |
828 | $mailbox_display = asearch_get_mailbox_display($mailbox); |
829 | $mailbox_title = '<b><big>' . _("Folder:") . ' '. $mailbox_display . ' </big></b>'; |
830 | $devel = check_sm_version(1, 5, 0); |
831 | if (!$devel) { |
832 | echo html_tag('div', $mailbox_title, 'center') . "\n"; |
833 | $mailbox_title = get_selectall_link(1, $real_sort); |
834 | } |
6b37252b |
835 | $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt); |
70f1b6b5 |
836 | |
7d60bda5 |
837 | echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">'; |
0fd2f513 |
838 | |
6b37252b |
839 | echo '<tr><td>'; |
7d60bda5 |
840 | if ($devel) |
9db01361 |
841 | mail_message_listing_beginning($imapConnection, $mbxresponse, $mailbox, $real_sort, $msg_cnt_str, $mailbox_title, 1, 1); |
7d60bda5 |
842 | else |
843 | mail_message_listing_beginning($imapConnection, $mailbox, $real_sort, $msg_cnt_str, $mailbox_title, 1, 1); |
6b37252b |
844 | echo '</td></tr>'; |
70f1b6b5 |
845 | |
6b37252b |
846 | echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>'; |
70f1b6b5 |
847 | |
6b37252b |
848 | echo '<tr><td>'; |
849 | echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">'; |
850 | echo ' <tr><td>'; |
70f1b6b5 |
851 | |
6b37252b |
852 | echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">'; |
853 | echo ' <tr><td>'; |
c2d47d51 |
854 | printHeader($mailbox, $sort, $color, !$thread_sort_messages); |
855 | displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $real_sort, $color, $cnt, $where, $what); |
6b37252b |
856 | echo ' </td></tr>'; |
857 | echo ' </table>'; |
858 | echo ' </td></tr>'; |
859 | echo ' </table>'; |
860 | mail_message_listing_end($cnt, '', $msg_cnt_str, $color); |
861 | echo '</td></tr>'; |
70f1b6b5 |
862 | |
6b37252b |
863 | echo '</table>'; |
864 | } |
59a623e6 |
865 | } |
866 | |
40fbe929 |
867 | /** |
868 | * @param array $boxes mailboxes array (reference) |
869 | * @return array selectable unformatted mailboxes names |
870 | */ |
871 | function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes) |
872 | { |
873 | $mboxes_array = array(); |
874 | $boxcount = count($boxes); |
7d60bda5 |
875 | for ($boxnum = 0; $boxnum < $boxcount; $boxnum++) { |
40fbe929 |
876 | if (!in_array('noselect', $boxes[$boxnum]['flags'])) |
877 | $mboxes_array[] = $boxes[$boxnum]['unformatted']; |
878 | } |
879 | return $mboxes_array; |
880 | } |
881 | |
0d672ac0 |
882 | /* ------------------------ main ------------------------ */ |
7d60bda5 |
883 | /* get globals we will need */ |
6b37252b |
884 | sqgetGlobalVar('username', $username, SQ_SESSION); |
885 | sqgetGlobalVar('key', $key, SQ_COOKIE); |
7d60bda5 |
886 | sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); |
887 | sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION); |
6b37252b |
888 | |
889 | $search_button_html = _("Search"); |
890 | $search_button_text = asearch_unhtmlentities($search_button_html); |
891 | $add_criteria_button_html = _("Add New Criteria"); |
892 | $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html); |
34a85c56 |
893 | $del_excluded_button_html = _("Remove Excluded Criteria"); |
6b37252b |
894 | $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html); |
34a85c56 |
895 | $del_all_button_html = _("Remove All Criteria"); |
6b37252b |
896 | $del_all_button_text = asearch_unhtmlentities($del_all_button_html); |
897 | |
7d60bda5 |
898 | /** Maximum number of recent searches to handle |
899 | * Default 0 |
900 | * @global integer $search_memory |
901 | */ |
902 | $search_memory = getPref($data_dir, $username, 'search_memory', 0); |
903 | |
904 | /** Advanced search control |
905 | * - 0 => allow basic interface only |
906 | * - 1 => allow advanced interface only |
907 | * - 2 => allow both |
908 | * Default 2 |
909 | * @global integer $allow_advanced_search |
910 | */ |
911 | $allow_advanced_search = getPref($data_dir, $username, 'allow_advanced_search', 2); |
6b37252b |
912 | |
7d60bda5 |
913 | /** |
914 | * Toggle advanced/basic search |
915 | */ |
916 | if (sqgetGlobalVar('advanced', $search_advanced, SQ_GET)) |
917 | setPref($data_dir, $username, 'search_advanced', $search_advanced & 1); |
6b37252b |
918 | |
7d60bda5 |
919 | /** If 1, show advanced search interface |
920 | * Default from allow_advanced_search pref |
921 | * @global integer $search_advanced |
922 | */ |
923 | if ($allow_advanced_search > 1) |
924 | $search_advanced = getPref($data_dir, $username, 'search_advanced', 0); |
925 | else |
926 | $search_advanced = $allow_advanced_search; |
6b37252b |
927 | |
7d60bda5 |
928 | if ($search_advanced) { |
929 | /** Set recent prefkeys according to $search_advanced |
930 | * @global array $recent_prefkeys |
931 | */ |
932 | $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_unop', 'asearch_recent_biop', 'asearch_recent_exclude', 'asearch_recent_sub'); |
933 | |
934 | /** Set saved prefkeys according to $search_advanced |
935 | * @global array $saved_prefkeys |
936 | */ |
937 | $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_unop', 'asearch_saved_biop', 'asearch_saved_exclude', 'asearch_saved_sub'); |
938 | |
939 | /*$asearch_prefkeys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/ |
940 | } |
941 | else { |
942 | $recent_prefkeys = array('search_where', 'search_folder', 'search_what', 'search_unop'); |
943 | $saved_prefkeys = array('saved_where', 'saved_folder', 'saved_what', 'saved_unop'); |
944 | } |
6b37252b |
945 | |
2d2f8bb8 |
946 | /** How we did enter the form |
947 | * - unset : Enter key, or called from outside (eg read_body) |
948 | * - $search_button_text : Search button |
949 | * - 'Search_no_update' : Search but don't update recent |
950 | * - 'Search_last' : Same as no_update but reload and search last |
951 | * - 'Search_silent' : Same as no_update but only display results |
952 | * - $add_criteria_button_text : Add New Criteria button |
953 | * - $del_excluded_button_text : Remove Excluded Criteria button |
954 | * - $del_all_button_text : Remove All Criteria button |
955 | * - 'save_recent' |
956 | * - 'search_recent' |
957 | * - 'forget_recent' |
958 | * - 'edit_saved' |
959 | * - 'search_saved' |
960 | * - 'delete_saved' |
961 | * @global string $submit |
962 | */ |
6b37252b |
963 | if (isset($_GET['submit'])) |
964 | $submit = strip_tags($_GET['submit']); |
965 | |
2d2f8bb8 |
966 | /** Searched mailboxes |
967 | * @global array $mailbox_array |
968 | */ |
6b37252b |
969 | if (isset($_GET['mailbox'])) { |
970 | $mailbox_array = $_GET['mailbox']; |
971 | if (!is_array($mailbox_array)) |
972 | $mailbox_array = array($mailbox_array); |
56e0b3b7 |
973 | } |
6b37252b |
974 | else |
975 | $mailbox_array = array(); |
976 | |
2d2f8bb8 |
977 | /** Binary operators |
978 | * @global array $biop_array |
979 | */ |
6b37252b |
980 | if (isset($_GET['biop'])) { |
981 | $biop_array = $_GET['biop']; |
982 | if (!is_array($biop_array)) |
983 | $biop_array = array($biop_array); |
c1cb7ba4 |
984 | } |
6b37252b |
985 | else |
986 | $biop_array = array(); |
c1cb7ba4 |
987 | |
2d2f8bb8 |
988 | /** Unary operators |
989 | * @global array $unop_array |
990 | */ |
6b37252b |
991 | if (isset($_GET['unop'])) { |
992 | $unop_array = $_GET['unop']; |
993 | if (!is_array($unop_array)) |
994 | $unop_array = array($unop_array); |
548a552a |
995 | } |
6b37252b |
996 | else |
997 | $unop_array = array(); |
998 | |
2d2f8bb8 |
999 | /** Where to search |
1000 | * @global array $where_array |
1001 | */ |
6b37252b |
1002 | if (isset($_GET['where'])) { |
1003 | $where_array = $_GET['where']; |
1004 | if (!is_array($where_array)) |
1005 | $where_array = array($where_array); |
1006 | } |
1007 | else |
1008 | $where_array = array(); |
1009 | |
2d2f8bb8 |
1010 | /** What to search |
1011 | * @global array $what_array |
1012 | */ |
6b37252b |
1013 | if (isset($_GET['what'])) { |
1014 | $what_array = $_GET['what']; |
1015 | if (!is_array($what_array)) |
1016 | $what_array = array($what_array); |
1017 | } |
1018 | else |
1019 | $what_array = array(); |
1020 | |
2d2f8bb8 |
1021 | /** Whether to exclude this criteria from search |
1022 | * @global array $exclude_array |
1023 | */ |
6b37252b |
1024 | if (isset($_GET['exclude'])) |
1025 | $exclude_array = $_GET['exclude']; |
1026 | else |
1027 | $exclude_array = array(); |
1028 | |
2d2f8bb8 |
1029 | /** Search within subfolders |
1030 | * @global array $sub_array |
1031 | */ |
40fbe929 |
1032 | if (isset($_GET['sub'])) |
1033 | $sub_array = $_GET['sub']; |
1034 | else |
1035 | $sub_array = array(); |
1036 | |
2d2f8bb8 |
1037 | /** Row number used by recent and saved stuff |
1038 | */ |
c2d47d51 |
1039 | if (isset($_GET['rownum'])) |
6b37252b |
1040 | $submit_rownum = strip_tags($_GET['rownum']); |
c2d47d51 |
1041 | |
2d2f8bb8 |
1042 | /** Change global sort |
1043 | */ |
c2d47d51 |
1044 | if (sqgetGlobalVar('newsort', $newsort, SQ_GET)) { |
1045 | setPref($data_dir, $username, 'sort', $newsort); |
1046 | $sort = $newsort; |
1047 | sqsession_register($sort, 'sort'); |
7d60bda5 |
1048 | asearch_edit_last(); |
c2d47d51 |
1049 | } |
1050 | |
7d60bda5 |
1051 | /** Toggle mailbox threading |
2d2f8bb8 |
1052 | */ |
c2d47d51 |
1053 | if (sqgetGlobalVar('set_thread', $set_thread, SQ_GET)) { |
7d60bda5 |
1054 | setPref($data_dir, $username, 'thread_' . $mailbox_array[0], $set_thread & 1); |
1055 | asearch_edit_last(); |
56e0b3b7 |
1056 | } |
6b37252b |
1057 | |
7d60bda5 |
1058 | /** Toggle show/hide saved searches |
1059 | */ |
1060 | if (sqgetGlobalVar('search_show_saved', $search_show_saved, SQ_GET)) |
1061 | setPref($data_dir, $username, 'search_show_saved', $search_show_saved & 1); |
1062 | |
1063 | /** Toggle show/hide recent searches |
1064 | */ |
1065 | if (sqgetGlobalVar('search_show_recent', $search_show_recent, SQ_GET)) |
1066 | setPref($data_dir, $username, 'search_show_recent', $search_show_recent & 1); |
1067 | |
1068 | // end of get globals |
6b37252b |
1069 | |
40fbe929 |
1070 | /** If TRUE, do not show search interface |
7d60bda5 |
1071 | * Default FALSE |
40fbe929 |
1072 | * @global bool $search_silent |
1073 | */ |
7d60bda5 |
1074 | $search_silent = FALSE; |
6b37252b |
1075 | |
1076 | /* See how the page was called and fire off correct function */ |
7d60bda5 |
1077 | if ((empty($submit)) && (!empty($where_array))) { |
6b37252b |
1078 | /* This happens when the Enter key is used or called from outside */ |
1079 | $submit = $search_button_text; |
1080 | if (count($where_array) != count($unop_array)) /* Hack needed to handle coming back from read_body et als */ |
7d60bda5 |
1081 | asearch_edit_last(); |
56e0b3b7 |
1082 | } |
6b37252b |
1083 | |
1084 | if (!isset($submit)) { |
1085 | $submit = ''; |
56e0b3b7 |
1086 | } |
6b37252b |
1087 | else { |
1088 | switch ($submit) { |
1089 | case $search_button_text: |
05f5b952 |
1090 | if (asearch_check_query($where_array, $what_array, $exclude_array) == '') |
7d60bda5 |
1091 | asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
6b37252b |
1092 | break; |
1093 | case 'Search_silent': |
1094 | $search_silent = TRUE; |
1095 | /*nobreak;*/ |
1096 | case 'Search_no_update': |
1097 | $submit = $search_button_text; |
1098 | break; |
1099 | case $del_excluded_button_text: |
1100 | $delarray = array_keys($exclude_array); |
1101 | while (!empty($delarray)) { |
1102 | $delrow = array_pop($delarray); |
1103 | array_splice($mailbox_array, $delrow, 1); |
1104 | array_splice($biop_array, $delrow, 1); |
1105 | array_splice($unop_array, $delrow, 1); |
1106 | array_splice($where_array, $delrow, 1); |
1107 | array_splice($what_array, $delrow, 1); |
1108 | /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */ |
40fbe929 |
1109 | array_splice($sub_array, $delrow, 1); |
6b37252b |
1110 | } |
1111 | $exclude_array = array(); |
1112 | break; |
1113 | case $del_all_button_text: |
1114 | $mailbox_array = array(); |
1115 | $biop_array = array(); |
1116 | $unop_array = array(); |
1117 | $where_array = array(); |
1118 | $what_array = array(); |
1119 | $exclude_array = array(); |
40fbe929 |
1120 | $sub_array = array(); |
6b37252b |
1121 | break; |
1122 | case 'save_recent': |
7d60bda5 |
1123 | asearch_save_recent($submit_rownum); |
6b37252b |
1124 | break; |
1125 | case 'search_recent': |
1126 | $submit = $search_button_text; |
7d60bda5 |
1127 | asearch_edit_recent($submit_rownum); |
1128 | asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
c2d47d51 |
1129 | break; |
6b37252b |
1130 | case 'edit_recent': /* no link to do this, yet */ |
7d60bda5 |
1131 | asearch_edit_recent($submit_rownum); |
6b37252b |
1132 | break; |
1133 | case 'forget_recent': |
7d60bda5 |
1134 | asearch_forget_recent($submit_rownum); |
6b37252b |
1135 | break; |
1136 | case 'search_saved': |
1137 | $submit = $search_button_text; |
7d60bda5 |
1138 | asearch_edit_saved($submit_rownum); |
1139 | asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
c2d47d51 |
1140 | break; |
6b37252b |
1141 | case 'edit_saved': |
7d60bda5 |
1142 | asearch_edit_saved($submit_rownum); |
6b37252b |
1143 | break; |
1144 | case 'delete_saved': |
7d60bda5 |
1145 | asearch_delete_saved($submit_rownum); |
6b37252b |
1146 | break; |
1147 | } |
56e0b3b7 |
1148 | } |
23a9084b |
1149 | |
7d60bda5 |
1150 | //Texts in both basic and advanced form |
1151 | $imap_asearch_unops = array( |
1152 | '' => '', |
1153 | 'NOT' => _("Not") |
1154 | ); |
1155 | |
1156 | if ($search_advanced) { |
1157 | //Texts in advanced form only |
1158 | $imap_asearch_options = array( |
1159 | //<message set>, |
1160 | //'ALL' is binary operator |
1161 | 'ANSWERED' => _("Answered"), |
1162 | 'BCC' => _("Bcc"), |
1163 | 'BEFORE' => _("Before"), |
1164 | 'BODY' => _("Message Body"), |
1165 | 'CC' => _("CC"), |
1166 | 'DELETED' => _("Deleted"), |
1167 | 'DRAFT' => _("Draft"), |
1168 | 'FLAGGED' => _("Flagged"), |
1169 | 'FROM' => _("Sent By"), |
1170 | 'HEADER' => _("Header Field"), |
1171 | 'KEYWORD' => _("Keyword"), |
1172 | 'LARGER' => _("Larger Than"), |
1173 | 'NEW' => _("New"), |
1174 | //'NOT' is unary operator |
1175 | 'OLD' => _("Old"), |
1176 | 'ON' => _("On"), |
1177 | //'OR' is binary operator |
1178 | 'RECENT' => _("Recent"), |
1179 | 'SEEN' => _("Seen"), |
1180 | 'SENTBEFORE' => _("Sent Before"), |
1181 | 'SENTON' => _("Sent On"), |
1182 | 'SENTSINCE' => _("Sent Since"), |
1183 | 'SINCE' => _("Since"), |
1184 | 'SMALLER' => _("Smaller Than"), |
1185 | 'SUBJECT' => _("Subject Contains"), |
1186 | 'TEXT' => _("Header and Body"), |
1187 | 'TO' => _("Sent To"), |
1188 | //'UID' => 'anum', |
1189 | /* 'UNANSWERED' => '', |
1190 | 'UNDELETED' => '', |
1191 | 'UNDRAFT' => '', |
1192 | 'UNFLAGGED' => '', |
1193 | 'UNKEYWORD' => _("Unkeyword"), |
1194 | 'UNSEEN' => _("Unseen"),*/ |
1195 | ); |
1196 | |
1197 | $imap_asearch_biops_in = array( |
1198 | 'ALL' => _("And In"), |
1199 | 'OR' => _("Or In") |
1200 | ); |
1201 | |
1202 | $imap_asearch_biops = array( |
1203 | 'ALL' => _("And"), |
1204 | 'OR' => _("Or") |
1205 | ); |
1206 | } |
1207 | else { |
1208 | //Texts in basic form only |
1209 | $imap_asearch_options = array( |
1210 | 'BCC' => _("Bcc"), |
1211 | 'BODY' => _("Body"), |
1212 | 'CC' => _("Cc"), |
1213 | 'FROM' => _("From"), |
1214 | 'SUBJECT' => _("Subject"), |
1215 | 'TEXT' => _("Everywhere"), |
1216 | 'TO' => _("To"), |
1217 | ); |
1218 | } |
1219 | |
1220 | uasort($imap_asearch_options, 'asearch_unhtml_strcoll'); |
1221 | |
6b37252b |
1222 | /* open IMAP connection */ |
1223 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
1224 | /* get mailbox names once here */ |
1225 | $boxes = sqimap_mailbox_list($imapConnection); |
99f3175e |
1226 | /* ensure we have a valid default mailbox name */ |
edda50ae |
1227 | $mailbox = asearch_nz($mailbox_array[0]); |
1228 | if (($mailbox == '') || ($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?) |
1229 | $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;) |
99f3175e |
1230 | |
6b37252b |
1231 | if (isset($composenew) && $composenew) { |
7d60bda5 |
1232 | $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox) |
1233 | . "&session=$composesession&attachedmessages=true&"; |
6b37252b |
1234 | displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false); |
56e0b3b7 |
1235 | } |
6b37252b |
1236 | else |
1237 | displayPageHeader($color, $mailbox); |
184ef883 |
1238 | |
6b37252b |
1239 | do_hook('search_before_form'); |
d81e351b |
1240 | |
6b37252b |
1241 | if (!$search_silent) { |
7d60bda5 |
1242 | //Add a link to the other search mode if allowed |
1243 | if ($allow_advanced_search > 1) |
94375458 |
1244 | $toggle_link = ' - ' . asearch_get_toggle_link(!$search_advanced, 'advanced', array(_("Standard search"), _("Advanced search"))); |
7d60bda5 |
1245 | else |
1246 | $toggle_link = ''; |
1247 | |
920ec5ec |
1248 | echo html_tag('table', |
7d60bda5 |
1249 | html_tag('tr', "\n" |
1250 | . html_tag('td', asearch_get_title_display($color, _("Search")) . $toggle_link, 'center', $color[0]) |
6b37252b |
1251 | ) , |
1252 | '', '', 'width="100%"') . "\n"; |
7d60bda5 |
1253 | asearch_print_saved($boxes); |
1254 | asearch_print_recent($boxes); |
edda50ae |
1255 | if (empty($where_array)) { |
40fbe929 |
1256 | global $sent_folder; |
1257 | |
edda50ae |
1258 | $mailbox_array[0] = $mailbox; |
1259 | $biop_array[0] = ''; |
1260 | $unop_array[0] = ''; |
40fbe929 |
1261 | if ($mailbox == $sent_folder) |
1262 | $where_array[0] = 'TO'; |
1263 | else |
1264 | $where_array[0] = 'FROM'; |
edda50ae |
1265 | $what_array[0] = ''; |
1266 | $exclude_array[0] = ''; |
40fbe929 |
1267 | $sub_array[0] = ''; |
edda50ae |
1268 | } |
7d60bda5 |
1269 | //Display advanced or basic form |
1270 | if ($search_advanced) { |
1271 | if ($submit == $add_criteria_button_text) { |
1272 | $last_index = max(count($where_array) - 1, 0); |
1273 | $mailbox_array[] = asearch_nz($mailbox_array[$last_index]); |
1274 | $biop_array[] = asearch_nz($biop_array[$last_index]); |
1275 | $unop_array[] = asearch_nz($unop_array[$last_index]); |
1276 | $where_array[] = asearch_nz($where_array[$last_index]); |
1277 | $what_array[] = asearch_nz($what_array[$last_index]); |
1278 | $exclude_array[] = asearch_nz($exclude_array[$last_index]); |
1279 | $sub_array[] = asearch_nz($sub_array[$last_index]); |
1280 | } |
1281 | asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
edda50ae |
1282 | } |
7d60bda5 |
1283 | else |
1284 | asearch_print_form_basic($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
6b37252b |
1285 | } |
1286 | |
d81e351b |
1287 | /********************************************************************* |
1288 | * Check to see if we can use cache or not. Currently the only time * |
1289 | * when you will not use it is when a link on the left hand frame is * |
1290 | * used. Also check to make sure we actually have the array in the * |
1291 | * registered session data. :) * |
1292 | *********************************************************************/ |
edda50ae |
1293 | if (!isset($use_mailbox_cache)) |
d81e351b |
1294 | $use_mailbox_cache = 0; |
d81e351b |
1295 | |
88cb1b4d |
1296 | do_hook('search_after_form'); |
56e0b3b7 |
1297 | |
6b37252b |
1298 | if ($submit == $search_button_text) { |
920ec5ec |
1299 | echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"'); |
1300 | echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5])); |
40fbe929 |
1301 | echo html_tag('tr', html_tag('td', asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array), 'center', $color[4])); |
313fd1f4 |
1302 | echo '</table>' . "\n"; |
05f5b952 |
1303 | |
2303858b |
1304 | flush(); |
1305 | |
05f5b952 |
1306 | $query_error = asearch_check_query($where_array, $what_array, $exclude_array); |
6b37252b |
1307 | if ($query_error != '') |
920ec5ec |
1308 | echo '<br>' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n"; |
6b37252b |
1309 | else { |
40fbe929 |
1310 | // Disable thread sort for now if there is more than one mailbox or at least one 'All Folders' |
c2d47d51 |
1311 | global $allow_thread_sort; |
1312 | $old_allow_thread_sort = $allow_thread_sort; |
40fbe929 |
1313 | $allow_thread_sort = ((count(array_unique($mailbox_array)) <= 1) && (!in_array('All Folders', $mailbox_array))); |
6b37252b |
1314 | |
40fbe929 |
1315 | $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes); |
1316 | $mboxes_msgs = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array); |
6b37252b |
1317 | if (empty($mboxes_msgs)) |
920ec5ec |
1318 | echo '<br>' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n"; |
6b37252b |
1319 | else { |
1320 | foreach($mboxes_msgs as $mailbox => $msgs) { |
7d60bda5 |
1321 | echo '<br>'; |
9db01361 |
1322 | $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox); |
7d60bda5 |
1323 | $msgs = fillMessageArray($imapConnection, $msgs, count($msgs)); |
6b37252b |
1324 | /* For now just keep the first criteria to make the regular search happy if the user tries to come back to search */ |
7d60bda5 |
1325 | /* $where = asearch_serialize($where_array); |
1326 | $what = asearch_serialize($what_array);*/ |
1327 | $where = $where_array[0]; |
1328 | $what = $what_array[0]; |
9db01361 |
1329 | asearch_print_mailbox_msgs($imapConnection, $mbxresponse, $mailbox, $msgs, count($msgs), $sort, $color, urlencode($where), urlencode($what)); |
6b37252b |
1330 | } |
1331 | } |
1332 | |
c2d47d51 |
1333 | $allow_thread_sort = $old_allow_thread_sort; // Restore thread sort |
59a623e6 |
1334 | } |
56e0b3b7 |
1335 | } |
1336 | |
70c4fd84 |
1337 | do_hook('search_bottom'); |
6b37252b |
1338 | sqimap_logout($imapConnection); |
dcc1cc82 |
1339 | echo '</body></html>'; |
99d2a184 |
1340 | |
7d60bda5 |
1341 | ?> |