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