c61bb006 |
1 | <?php |
245a6892 |
2 | |
35586184 |
3 | /** |
134e4174 |
4 | * search.php |
5 | * |
6c84ba1e |
6 | * Copyright (c) 1999-2005 The SquirrelMail Project Team |
134e4174 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * IMAP search page |
10 | * |
11 | * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas! |
12 | * |
13 | * @version $Id$ |
14 | * @package squirrelmail |
3f294f60 |
15 | * @subpackage search |
134e4174 |
16 | * @link http://www.ietf.org/rfc/rfc3501.txt |
17 | * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr |
18 | */ |
2d367c68 |
19 | |
30967a1e |
20 | /** |
134e4174 |
21 | * Path for SquirrelMail required files. |
22 | * @ignore |
23 | */ |
86725763 |
24 | define('SM_PATH','../'); |
25 | |
7d60bda5 |
26 | /** SquirrelMail required files. |
134e4174 |
27 | */ |
08185f2a |
28 | require_once(SM_PATH . 'include/validate.php'); |
3f294f60 |
29 | include_once(SM_PATH . 'functions/strings.php'); |
30 | include_once(SM_PATH . 'functions/imap_asearch.php'); |
31 | include_once(SM_PATH . 'functions/imap_mailbox.php'); |
32 | include_once(SM_PATH . 'functions/imap_messages.php'); |
33 | include_once(SM_PATH . 'functions/mime.php'); |
34 | include_once(SM_PATH . 'functions/mailbox_display.php'); //getButton()... |
91c27aee |
35 | include_once(SM_PATH . 'class/template/template.class.php'); |
c61bb006 |
36 | |
7d60bda5 |
37 | /** Prefs array ordinals. Must match $recent_prefkeys and $saved_prefkeys |
134e4174 |
38 | */ |
7d60bda5 |
39 | define('ASEARCH_WHERE', 0); |
40 | define('ASEARCH_MAILBOX', 1); |
41 | define('ASEARCH_WHAT', 2); |
42 | define('ASEARCH_UNOP', 3); |
43 | define('ASEARCH_BIOP', 4); |
44 | define('ASEARCH_EXCLUDE', 5); |
45 | define('ASEARCH_SUB', 6); |
5b6abe97 |
46 | define('ASEARCH_MAX', 7); |
47 | |
48 | /** Name of session var |
134e4174 |
49 | */ |
5b6abe97 |
50 | define('ASEARCH_CRITERIA', 'criteria'); |
7d60bda5 |
51 | |
52 | /** Builds a href with params |
134e4174 |
53 | * @param string $params optional parameters to GET |
54 | */ |
7d60bda5 |
55 | function asearch_get_href($params = '') |
56 | { |
8cc8ec79 |
57 | $href = 'search.php'; |
58 | if ($params != '') |
59 | $href .= '?' . $params; |
60 | return $href; |
7d60bda5 |
61 | } |
62 | |
63 | /** Builds a [link] |
134e4174 |
64 | * @param string $href (reference) |
65 | * @param string $text |
66 | * @param string $title |
67 | */ |
94375458 |
68 | function asearch_get_link(&$href, $text, $title = '') |
7d60bda5 |
69 | { |
8cc8ec79 |
70 | if ($title != '') |
71 | $title = ' title="' . $title . '"'; |
72 | return '<a href="' . $href . '"' . $title . '>' . $text . '</a>'; |
7d60bda5 |
73 | } |
74 | |
75 | /** Builds a toggle [link] |
134e4174 |
76 | * @param integer $value |
77 | * @param string $action |
78 | * @param array $text_array |
79 | * @param array $title_array |
80 | */ |
94375458 |
81 | function asearch_get_toggle_link($value, $action, $text_array, $title_array = array()) |
7d60bda5 |
82 | { |
8cc8ec79 |
83 | return asearch_get_link(asearch_get_href($action . '=' . (int)$value), $text_array[$value], asearch_nz($title_array[$value])); |
7d60bda5 |
84 | } |
85 | |
40fbe929 |
86 | /** |
134e4174 |
87 | * @param string $a |
88 | * @param string $b |
89 | * @return bool strcoll()-like result |
90 | */ |
6b37252b |
91 | function asearch_unhtml_strcoll($a, $b) |
92 | { |
8cc8ec79 |
93 | return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b)); |
6b37252b |
94 | } |
40fbe929 |
95 | |
91c27aee |
96 | |
40fbe929 |
97 | /** |
134e4174 |
98 | * @param string $mailbox mailbox name utf7 encoded inc. special case INBOX |
99 | * @return string mailbox name ready to display (utf7 decoded or localized INBOX) |
100 | */ |
6b37252b |
101 | function imap_get_mailbox_display($mailbox) |
102 | { |
8cc8ec79 |
103 | if (strtoupper($mailbox) == 'INBOX') |
104 | return _("INBOX"); |
105 | return imap_utf7_decode_local($mailbox); |
6b37252b |
106 | } |
56e0b3b7 |
107 | |
40fbe929 |
108 | /** |
134e4174 |
109 | * @param string $mailbox mailbox name or special case 'All Folders' |
110 | * @return string mailbox name ready to display (utf7 decoded or localized 'All Folders') |
111 | */ |
6b37252b |
112 | function asearch_get_mailbox_display($mailbox) |
113 | { |
8cc8ec79 |
114 | if ($mailbox == 'All Folders') |
115 | return _("All Folders"); |
116 | return imap_get_mailbox_display($mailbox); |
6b37252b |
117 | } |
0b97a708 |
118 | |
40fbe929 |
119 | /** |
134e4174 |
120 | * @param array $color color array |
121 | * @param string $txt text to display |
122 | * @return string title ready to display |
123 | */ |
7d60bda5 |
124 | function asearch_get_title_display(&$color, $txt) |
6b37252b |
125 | { |
8cc8ec79 |
126 | return '<b><big>' . $txt . '</big></b>'; |
6b37252b |
127 | } |
40fbe929 |
128 | /** |
134e4174 |
129 | * @param array $color color array |
130 | * @param string $txt text to display |
131 | * @return string error text ready to display |
132 | */ |
7d60bda5 |
133 | function asearch_get_error_display(&$color, $txt) |
6b37252b |
134 | { |
8cc8ec79 |
135 | return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>'; |
0b97a708 |
136 | } |
6b37252b |
137 | |
40fbe929 |
138 | /** |
134e4174 |
139 | * @param array $input_array array to serialize |
140 | * @return string a string containing a byte-stream representation of value that can be stored anywhere |
141 | */ |
7d60bda5 |
142 | function asearch_serialize(&$input_array) |
6b37252b |
143 | { |
8cc8ec79 |
144 | global $search_advanced; |
145 | if ($search_advanced) |
146 | return serialize($input_array); |
147 | return $input_array[0]; |
0b97a708 |
148 | } |
6b37252b |
149 | |
40fbe929 |
150 | /** |
134e4174 |
151 | * @param string $input_string string to unserialize |
152 | * @return array |
153 | */ |
6b37252b |
154 | function asearch_unserialize($input_string) |
155 | { |
8cc8ec79 |
156 | global $search_advanced; |
157 | if ($search_advanced) |
158 | return unserialize($input_string); |
159 | return array($input_string); |
0b97a708 |
160 | } |
6b37252b |
161 | |
40fbe929 |
162 | /** |
134e4174 |
163 | * @param string $key the pref key |
164 | * @param integer $index the pref key index |
165 | * @param string $default default value |
166 | * @return string pref value |
167 | */ |
7d60bda5 |
168 | function asearch_getPref(&$key, $index, $default = '') |
6b37252b |
169 | { |
8cc8ec79 |
170 | global $data_dir, $username, $search_advanced; |
171 | return getPref($data_dir, $username, $key . ($index + !$search_advanced), $default); |
0b97a708 |
172 | } |
6b37252b |
173 | |
40fbe929 |
174 | /** |
134e4174 |
175 | * @param string $key the pref key |
176 | * @param integer $index the pref key index |
177 | * @param string $value pref value to set |
178 | * @return bool status |
179 | */ |
7d60bda5 |
180 | function asearch_setPref(&$key, $index, $value) |
6b37252b |
181 | { |
8cc8ec79 |
182 | global $data_dir, $username, $search_advanced; |
183 | return setPref($data_dir, $username, $key . ($index + !$search_advanced), $value); |
0b97a708 |
184 | } |
6b37252b |
185 | |
40fbe929 |
186 | /** |
134e4174 |
187 | * @param string $key the pref key |
188 | * @param integer $index the pref key index |
189 | * @return bool status |
190 | */ |
7d60bda5 |
191 | function asearch_removePref(&$key, $index) |
6b37252b |
192 | { |
8cc8ec79 |
193 | global $data_dir, $username, $search_advanced; |
194 | return removePref($data_dir, $username, $key . ($index + !$search_advanced)); |
0b97a708 |
195 | } |
0b97a708 |
196 | |
7d60bda5 |
197 | /** Sanity checks, done before running the imap command and before calling push_recent |
134e4174 |
198 | */ |
7d60bda5 |
199 | function asearch_check_query(&$where_array, &$what_array, &$exclude_array) |
6b37252b |
200 | { |
8cc8ec79 |
201 | global $imap_asearch_opcodes; |
05f5b952 |
202 | |
8cc8ec79 |
203 | if (empty($where_array)) |
204 | return _("Please enter something to search for"); |
205 | if (count($exclude_array) == count($where_array)) |
206 | return _("There must be at least one criteria to search for"); |
207 | for ($crit_num = 0; $crit_num < count($where_array); $crit_num++) { |
208 | $where = $where_array[$crit_num]; |
209 | $what = $what_array[$crit_num]; |
210 | if (!(($what == '') ^ ($imap_asearch_opcodes[$where] != ''))) |
211 | return _("Error in criteria argument"); |
212 | } |
213 | return ''; |
56e0b3b7 |
214 | } |
215 | |
7d60bda5 |
216 | /** Read the recent searches from the prefs |
134e4174 |
217 | */ |
7d60bda5 |
218 | function asearch_read_recent() |
6b37252b |
219 | { |
8cc8ec79 |
220 | global $recent_prefkeys, $search_memory; |
6b37252b |
221 | |
8cc8ec79 |
222 | $recent_array = array(); |
223 | $recent_num = 0; |
224 | for ($pref_num = 0; $pref_num < $search_memory; $pref_num++) { |
225 | foreach ($recent_prefkeys as $prefkey) { |
226 | $pref = asearch_getPref($prefkey, $pref_num); |
134e4174 |
227 | /* if (!empty($pref))*/ |
8cc8ec79 |
228 | $recent_array[$prefkey][$recent_num] = $pref; |
229 | } |
230 | if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) { |
231 | foreach ($recent_prefkeys as $key) { |
232 | array_pop($recent_array[$key]); |
233 | } |
134e4174 |
234 | // break; //Disabled to support old search code broken prefs |
8cc8ec79 |
235 | } |
236 | else |
237 | $recent_num++; |
238 | } |
239 | return $recent_array; |
56e0b3b7 |
240 | } |
23a9084b |
241 | |
7d60bda5 |
242 | /** Read the saved searches from the prefs |
134e4174 |
243 | */ |
7d60bda5 |
244 | function asearch_read_saved() |
6b37252b |
245 | { |
8cc8ec79 |
246 | global $saved_prefkeys; |
247 | |
248 | $saved_array = array(); |
249 | $saved_key = $saved_prefkeys[0]; |
250 | for ($saved_count = 0; ; $saved_count++) { |
251 | $pref = asearch_getPref($saved_key, $saved_count); |
252 | if (empty($pref)) |
253 | break; |
254 | } |
255 | for ($saved_num = 0; $saved_num < $saved_count; $saved_num++) { |
256 | foreach ($saved_prefkeys as $key) { |
257 | $saved_array[$key][$saved_num] = asearch_getPref($key, $saved_num); |
258 | } |
259 | } |
260 | return $saved_array; |
6b37252b |
261 | } |
262 | |
7d60bda5 |
263 | /** Save a recent search to the prefs |
134e4174 |
264 | */ |
7d60bda5 |
265 | function asearch_save_recent($recent_index) |
6b37252b |
266 | { |
8cc8ec79 |
267 | global $recent_prefkeys, $saved_prefkeys; |
268 | |
269 | $saved_array = asearch_read_saved(); |
270 | $saved_index = count($saved_array[$saved_prefkeys[0]]); |
271 | $recent_array = asearch_read_recent(); |
272 | $n = 0; |
273 | foreach ($recent_prefkeys as $key) { |
274 | $recent_slice = array_slice($recent_array[$key], $recent_index, 1); |
275 | if (!empty($recent_slice[0])) |
276 | asearch_setPref($saved_prefkeys[$n], $saved_index, $recent_slice[0]); |
277 | else |
278 | asearch_removePref($saved_prefkeys[$n], $saved_index); |
279 | $n++; |
280 | } |
6b37252b |
281 | } |
282 | |
7d60bda5 |
283 | /** Write a recent search to prefs |
134e4174 |
284 | */ |
7d60bda5 |
285 | function asearch_write_recent(&$recent_array) |
6b37252b |
286 | { |
8cc8ec79 |
287 | global $recent_prefkeys, $search_memory; |
6b37252b |
288 | |
8cc8ec79 |
289 | $recent_count = min($search_memory, count($recent_array[$recent_prefkeys[0]])); |
290 | for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) { |
291 | foreach ($recent_prefkeys as $key) { |
292 | asearch_setPref($key, $recent_num, $recent_array[$key][$recent_num]); |
293 | } |
294 | } |
295 | for (; $recent_num < $search_memory; $recent_num++) { |
296 | foreach ($recent_prefkeys as $key) { |
297 | asearch_removePref($key, $recent_num); |
298 | } |
299 | } |
6b37252b |
300 | } |
301 | |
7d60bda5 |
302 | /** Remove a recent search from prefs |
134e4174 |
303 | */ |
7d60bda5 |
304 | function asearch_forget_recent($forget_index) |
6b37252b |
305 | { |
8cc8ec79 |
306 | global $recent_prefkeys; |
6b37252b |
307 | |
8cc8ec79 |
308 | $recent_array = asearch_read_recent(); |
309 | foreach ($recent_prefkeys as $key) { |
310 | array_splice($recent_array[$key], $forget_index, 1); |
311 | } |
312 | asearch_write_recent($recent_array); |
6b37252b |
313 | } |
314 | |
7d60bda5 |
315 | /** Find a recent search in the prefs (used to avoid saving duplicates) |
134e4174 |
316 | */ |
7d60bda5 |
317 | function asearch_find_recent(&$recent_array, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array) |
6b37252b |
318 | { |
8cc8ec79 |
319 | global $recent_prefkeys, $search_advanced; |
320 | |
321 | $where_string = asearch_serialize($where_array); |
322 | $mailbox_string = asearch_serialize($mailbox_array); |
323 | $what_string = asearch_serialize($what_array); |
324 | $unop_string = asearch_serialize($unop_array); |
325 | if ($search_advanced) { |
326 | $biop_string = asearch_serialize($biop_array); |
327 | $exclude_string = asearch_serialize($exclude_array); |
328 | $sub_string = asearch_serialize($sub_array); |
329 | } |
330 | $recent_count = count($recent_array[$recent_prefkeys[ASEARCH_WHERE]]); |
331 | for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) { |
332 | if (isset($recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num])) { |
333 | if ( |
334 | $where_string == $recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num] && |
335 | $mailbox_string == $recent_array[$recent_prefkeys[ASEARCH_MAILBOX]][$recent_num] && |
336 | $what_string == $recent_array[$recent_prefkeys[ASEARCH_WHAT]][$recent_num] && |
337 | $unop_string == $recent_array[$recent_prefkeys[ASEARCH_UNOP]][$recent_num] && |
338 | ((!$search_advanced) || |
339 | ($biop_string == $recent_array[$recent_prefkeys[ASEARCH_BIOP]][$recent_num] && |
340 | $exclude_string == $recent_array[$recent_prefkeys[ASEARCH_EXCLUDE]][$recent_num] && |
341 | $sub_string == $recent_array[$recent_prefkeys[ASEARCH_SUB]][$recent_num])) |
342 | ) |
343 | return $recent_num; |
344 | } |
345 | } |
346 | return -1; |
6b37252b |
347 | } |
348 | |
7d60bda5 |
349 | /** Push a recent search into the prefs |
134e4174 |
350 | */ |
7d60bda5 |
351 | function asearch_push_recent(&$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array) |
6b37252b |
352 | { |
8cc8ec79 |
353 | global $recent_prefkeys, $search_memory; |
324ac3c5 |
354 | //global $what; // Hack to access issued search from read_body.php |
355 | $what = 1; |
356 | /** |
357 | * Update search history and store it in the session so we can retrieve the |
358 | * issued search when returning from an external page like read_body.php |
359 | */ |
360 | $criteria[$what] = array($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
8cc8ec79 |
361 | sqsession_register($criteria, ASEARCH_CRITERIA); |
362 | if ($search_memory > 0) { |
363 | $recent_array = asearch_read_recent(); |
364 | $recent_found = asearch_find_recent($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
134e4174 |
365 | if ($recent_found >= 0) { // Remove identical recent |
8cc8ec79 |
366 | foreach ($recent_prefkeys as $key) { |
367 | array_splice($recent_array[$key], $recent_found, 1); |
368 | } |
369 | } |
370 | $input = array($where_array, $mailbox_array, $what_array, $unop_array, $biop_array, $exclude_array, $sub_array); |
371 | $i = 0; |
372 | foreach ($recent_prefkeys as $key) { |
373 | array_unshift($recent_array[$key], asearch_serialize($input[$i])); |
374 | $i++; |
375 | } |
376 | asearch_write_recent($recent_array); |
377 | } |
6b37252b |
378 | } |
379 | |
7d60bda5 |
380 | /** Edit a recent search |
134e4174 |
381 | * @global array mailbox_array searched mailboxes |
382 | */ |
7d60bda5 |
383 | function asearch_edit_recent($index) |
6b37252b |
384 | { |
8cc8ec79 |
385 | global $recent_prefkeys, $search_advanced; |
386 | global $where_array, $mailbox_array, $what_array, $unop_array; |
387 | global $biop_array, $exclude_array, $sub_array; |
7d60bda5 |
388 | |
8cc8ec79 |
389 | $where_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHERE], $index)); |
390 | $mailbox_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_MAILBOX], $index)); |
391 | $what_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHAT], $index)); |
392 | $unop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_UNOP], $index)); |
393 | if ($search_advanced) { |
394 | $biop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_BIOP], $index)); |
395 | $exclude_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_EXCLUDE], $index)); |
396 | $sub_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_SUB], $index)); |
397 | } |
6b37252b |
398 | } |
399 | |
958becc9 |
400 | /** Get last search criteria from session or prefs |
134e4174 |
401 | * FIX ME, try to avoid globals |
402 | */ |
324ac3c5 |
403 | function asearch_edit_last($index) { |
8cc8ec79 |
404 | if (sqGetGlobalVar(ASEARCH_CRITERIA, $criteria, SQ_SESSION)) { |
405 | global $where_array, $mailbox_array, $what_array, $unop_array; |
406 | global $biop_array, $exclude_array, $sub_array; |
324ac3c5 |
407 | $mailbox_array = $criteria[$index][0]; |
408 | $biop_array = $criteria[$index][1]; |
409 | $unop_array = $criteria[$index][2]; |
410 | $where_array = $criteria[$index][3]; |
411 | $what_array = $criteria[$index][4]; |
412 | $exclude_array = $criteria[$index][5]; |
413 | $sub_array = $criteria[$index][6]; |
414 | unset($criteria[$index]); |
415 | //sqsession_unregister(ASEARCH_CRITERIA); |
416 | } else { |
8cc8ec79 |
417 | global $search_memory; |
324ac3c5 |
418 | if ($search_memory > 0) { |
8cc8ec79 |
419 | asearch_edit_recent(0); |
324ac3c5 |
420 | } |
8cc8ec79 |
421 | } |
6b37252b |
422 | } |
423 | |
7d60bda5 |
424 | /** Edit a saved search |
134e4174 |
425 | */ |
7d60bda5 |
426 | function asearch_edit_saved($index) |
6b37252b |
427 | { |
8cc8ec79 |
428 | global $saved_prefkeys, $search_advanced; |
429 | global $where_array, $mailbox_array, $what_array, $unop_array; |
430 | global $biop_array, $exclude_array, $sub_array; |
7d60bda5 |
431 | |
8cc8ec79 |
432 | $where_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHERE], $index)); |
433 | $mailbox_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_MAILBOX], $index)); |
434 | $what_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHAT], $index)); |
435 | $unop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_UNOP], $index)); |
436 | if ($search_advanced) { |
437 | $biop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_BIOP], $index)); |
438 | $exclude_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_EXCLUDE], $index)); |
439 | $sub_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_SUB], $index)); |
440 | } |
6b37252b |
441 | } |
442 | |
7d60bda5 |
443 | /** Write a saved search to the prefs |
134e4174 |
444 | */ |
7d60bda5 |
445 | function asearch_write_saved(&$saved_array) |
6b37252b |
446 | { |
8cc8ec79 |
447 | global $saved_prefkeys; |
6b37252b |
448 | |
8cc8ec79 |
449 | $saved_count = count($saved_array[$saved_prefkeys[0]]); |
450 | for ($saved_num=0; $saved_num < $saved_count; $saved_num++) { |
451 | foreach ($saved_prefkeys as $key) { |
452 | asearch_setPref($key, $saved_num, $saved_array[$key][$saved_num]); |
453 | } |
454 | } |
455 | foreach ($saved_prefkeys as $key) { |
456 | asearch_removePref($key, $saved_count); |
457 | } |
6b37252b |
458 | } |
459 | |
7d60bda5 |
460 | /** Delete a saved search from the prefs |
134e4174 |
461 | */ |
7d60bda5 |
462 | function asearch_delete_saved($saved_index) |
6b37252b |
463 | { |
8cc8ec79 |
464 | global $saved_prefkeys; |
6b37252b |
465 | |
8cc8ec79 |
466 | $saved_array = asearch_read_saved(); |
467 | $asearch_keys = $saved_prefkeys; |
468 | foreach ($asearch_keys as $key) { |
469 | array_splice($saved_array[$key], $saved_index, 1); |
470 | } |
471 | asearch_write_saved($saved_array); |
6b37252b |
472 | } |
473 | |
7d60bda5 |
474 | /** Translate the input date to imap date to local date display, |
134e4174 |
475 | * so the user can know if the date is wrong or illegal |
476 | * @return string locally formatted date or error text |
477 | */ |
7d60bda5 |
478 | function asearch_get_date_display(&$what) |
6b37252b |
479 | { |
8cc8ec79 |
480 | $what_parts = sqimap_asearch_parse_date($what); |
481 | if (count($what_parts) == 4) { |
482 | if (checkdate($what_parts[2], $what_parts[1], $what_parts[3])) |
483 | return date_intl(_("M j, Y"), mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3])); |
484 | //return $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3]; |
485 | return _("(Illegal date)"); |
486 | } |
487 | return _("(Wrong date)"); |
6b37252b |
488 | } |
489 | |
7d60bda5 |
490 | /** Translate the query to rough natural display |
134e4174 |
491 | * @return string rough natural query ready to display |
492 | */ |
7d60bda5 |
493 | function asearch_get_query_display(&$color, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array) |
6b37252b |
494 | { |
8cc8ec79 |
495 | global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options; |
496 | global $imap_asearch_opcodes; |
497 | |
498 | $last_mailbox = $mailbox_array[0]; |
499 | if (empty($last_mailbox)) |
500 | $last_mailbox = 'INBOX'; |
501 | $query_display = ''; |
502 | for ($crit_num=0; $crit_num < count($where_array); $crit_num++) { |
503 | if ((!isset($exclude_array[$crit_num])) || (!$exclude_array[$crit_num])) { |
504 | $cur_mailbox = $mailbox_array[$crit_num]; |
505 | if (empty($cur_mailbox)) |
506 | $cur_mailbox = 'INBOX'; |
507 | $biop = asearch_nz($biop_array[$crit_num]); |
508 | if (($query_display == '') || ($cur_mailbox != $last_mailbox)) { |
509 | $mailbox_display = ' <b>' . asearch_get_mailbox_display($cur_mailbox) . '</b>'; |
510 | if ($query_display == '') |
511 | $biop_display = _("In"); |
512 | else |
513 | $biop_display = $imap_asearch_biops_in[$biop]; |
514 | $last_mailbox = $cur_mailbox; |
515 | } |
516 | else { |
517 | $mailbox_display = ''; |
518 | $biop_display = $imap_asearch_biops[$biop]; |
519 | } |
520 | $unop = $unop_array[$crit_num]; |
521 | $unop_display = $imap_asearch_unops[$unop]; |
522 | if ($unop_display != '') |
523 | $unop_display .= ' '; |
524 | $where = $where_array[$crit_num]; |
525 | $where_display = $unop_display . asearch_nz($imap_asearch_options[$where], $where); |
526 | $what_type = $imap_asearch_opcodes[$where]; |
527 | $what = $what_array[$crit_num]; |
134e4174 |
528 | if ($what_type) { /* Check opcode parameter */ |
8cc8ec79 |
529 | if ($what == '') |
530 | $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)")); |
531 | else { |
532 | if ($what_type == 'adate') |
533 | $what_display = asearch_get_date_display($what); |
534 | else |
535 | $what_display = htmlspecialchars($what); |
536 | $what_display = ' <b>' . $what_display . '</b>'; |
537 | } |
538 | } |
539 | else { |
540 | if ($what) |
541 | $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)")); |
542 | else |
543 | $what_display = ''; |
544 | } |
545 | if ($mailbox_display != '') |
546 | $query_display .= ' <u><i>' . $biop_display . '</i></u>' . $mailbox_display . ' <u><i>' . $where_display . '</i></u>' . $what_display; |
547 | else |
548 | $query_display .= ' <u><i>' . $biop_display . ' ' . $where_display . '</i></u>' . $what_display; |
549 | } |
550 | } |
551 | return $query_display; |
6b37252b |
552 | } |
553 | |
91c27aee |
554 | /** |
555 | * Creates button |
556 | * |
557 | * @deprecated see form functions available in 1.5.1 and 1.4.3. |
558 | * @param string $type |
559 | * @param string $name |
560 | * @param string $value |
561 | * @param string $js |
562 | * @param bool $enabled |
563 | */ |
564 | function getButton($type, $name, $value, $js = '', $enabled = TRUE) { |
565 | $disabled = ( $enabled ? '' : 'disabled ' ); |
566 | $js = ( $js ? $js.' ' : '' ); |
567 | return '<input '.$disabled.$js. |
568 | 'type="'.$type. |
569 | '" name="'.$name. |
570 | '" value="'.$value . |
571 | '" style="padding: 0px; margin: 0px" />'; |
572 | } |
573 | |
574 | |
7d60bda5 |
575 | /** Handle the alternate row colors |
134e4174 |
576 | * @return string color value |
577 | */ |
7d60bda5 |
578 | function asearch_get_row_color(&$color, $row_num) |
6b37252b |
579 | { |
580 | /*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/ |
8cc8ec79 |
581 | $color_string = $color[4]; |
582 | if ($GLOBALS['alt_index_colors']) { |
583 | if (($row_num % 2) == 0) { |
584 | if (!isset($color[12])) |
585 | $color[12] = '#EAEAEA'; |
586 | $color_string = $color[12]; |
587 | } |
588 | } |
589 | return $color_string; |
6b37252b |
590 | } |
591 | |
7d60bda5 |
592 | /** Print a whole query array, recent or saved |
134e4174 |
593 | */ |
7d60bda5 |
594 | function asearch_print_query_array(&$boxes, &$query_array, &$query_keys, &$action_array, $title, $show_pref) |
6b37252b |
595 | { |
8cc8ec79 |
596 | global $color; |
597 | global $data_dir, $username; |
598 | global $use_icons, $icon_theme; |
599 | |
600 | $show_flag = getPref($data_dir, $username, $show_pref, 0) & 1; |
601 | $use_icons_flag = ($use_icons) && ($icon_theme != 'none'); |
602 | if ($use_icons_flag) |
603 | $text_array = array('<img src="' . SM_PATH . 'images/minus.png" border="0" height="7" width="7" />', |
604 | '<img src="' . SM_PATH . 'images/plus.png" border="0" height="7" width="7" />'); |
605 | else |
606 | $text_array = array('-', '+'); |
607 | $toggle_link = asearch_get_toggle_link(!$show_flag, $show_pref, $text_array, array(_("Fold"), _("Unfold"))); |
608 | if (!$use_icons_flag) |
609 | $toggle_link = '<small>[' . $toggle_link . ']</small>'; |
610 | |
611 | echo "<br />\n"; |
612 | echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"'); |
613 | echo html_tag('tr', |
614 | html_tag('td', $toggle_link, 'center', $color[5], 'width="5%"') |
615 | . html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=4')); |
616 | if ($show_flag) { |
617 | $main_key = $query_keys[ASEARCH_WHERE]; |
618 | $query_count = count($query_array[$main_key]); |
619 | for ($query_num = 0, $row_num = 0; $query_num < $query_count; $query_num++) { |
620 | if (!empty($query_array[$main_key][$query_num])) { |
621 | echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num)); |
622 | |
623 | unset($search_array); |
624 | foreach ($query_keys as $query_key) { |
625 | $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]); |
626 | } |
627 | $where_array = $search_array[ASEARCH_WHERE]; |
628 | $mailbox_array = $search_array[ASEARCH_MAILBOX]; |
629 | $what_array = $search_array[ASEARCH_WHAT]; |
630 | $unop_array = $search_array[ASEARCH_UNOP]; |
631 | $biop_array = asearch_nz($search_array[ASEARCH_BIOP], array()); |
632 | $exclude_array = asearch_nz($search_array[ASEARCH_EXCLUDE], array()); |
633 | $sub_array = asearch_nz($search_array[ASEARCH_SUB], array()); |
634 | $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
635 | |
636 | echo html_tag('td', $query_num + 1, 'right'); |
637 | echo html_tag('td', $query_display, 'center', '', 'width="80%"'); |
638 | foreach ($action_array as $action => $action_display) { |
639 | echo html_tag('td', '<a href="' . asearch_get_href('submit=' . $action . '&rownum=' . $query_num) . '">' . $action_display . '</a>', 'center'); |
640 | } |
641 | |
642 | echo '</tr>' . "\n"; |
643 | $row_num++; |
644 | } |
645 | } |
646 | } |
647 | echo '</table>' . "\n"; |
6b37252b |
648 | } |
649 | |
7d60bda5 |
650 | /** Print the saved array |
134e4174 |
651 | */ |
7d60bda5 |
652 | function asearch_print_saved(&$boxes) |
6b37252b |
653 | { |
8cc8ec79 |
654 | global $saved_prefkeys; |
6b37252b |
655 | |
8cc8ec79 |
656 | $saved_array = asearch_read_saved(); |
657 | if (isset($saved_array[$saved_prefkeys[0]])) { |
658 | $saved_count = count($saved_array[$saved_prefkeys[0]]); |
659 | if ($saved_count > 0) { |
660 | $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete")); |
661 | asearch_print_query_array($boxes, $saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"), 'search_show_saved'); |
662 | } |
663 | } |
70c4fd84 |
664 | } |
6c8388a9 |
665 | |
7d60bda5 |
666 | /** |
134e4174 |
667 | * Print the recent array |
668 | */ |
7d60bda5 |
669 | function asearch_print_recent(&$boxes) |
6b37252b |
670 | { |
8cc8ec79 |
671 | global $recent_prefkeys, $search_memory; |
6b37252b |
672 | |
8cc8ec79 |
673 | $recent_array = asearch_read_recent(); |
674 | if (isset($recent_array[$recent_prefkeys[0]])) { |
675 | $recent_count = count($recent_array[$recent_prefkeys[0]]); |
676 | if (min($recent_count, $search_memory) > 0) { |
677 | $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget")); |
678 | asearch_print_query_array($boxes, $recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"), 'search_show_recent'); |
679 | } |
680 | } |
56e0b3b7 |
681 | } |
682 | |
7d60bda5 |
683 | /** Build an <option> statement |
134e4174 |
684 | */ |
920ec5ec |
685 | function asearch_opt($val, $sel, $tit) |
6b37252b |
686 | { |
e1728a7a |
687 | return '<option value="' . $val . '"' . ($sel == $val ? ' selected="selected"' : '') . '>' . $tit . '</option>' . "\n"; |
6b37252b |
688 | } |
689 | |
7d60bda5 |
690 | /** Build a <select> statement from an array |
134e4174 |
691 | */ |
6b37252b |
692 | function asearch_opt_array($var_name, $opt_array, $cur_val) |
693 | { |
8cc8ec79 |
694 | $output = '<select name="' . $var_name . '">' . "\n"; |
695 | foreach($opt_array as $val => $display) |
696 | $output .= asearch_opt($val, $cur_val, asearch_nz($display, $val)); |
697 | $output .= '</select>' . "\n"; |
698 | return $output; |
6b37252b |
699 | } |
700 | |
7d60bda5 |
701 | /** Verify that a mailbox exists |
134e4174 |
702 | * @return bool mailbox exists |
703 | */ |
7d60bda5 |
704 | function asearch_mailbox_exists($mailbox, &$boxes) |
920ec5ec |
705 | { |
8cc8ec79 |
706 | foreach ($boxes as $box) { |
707 | if ($box['unformatted'] == $mailbox) |
708 | return TRUE; |
709 | } |
710 | return FALSE; |
920ec5ec |
711 | } |
712 | |
5b6abe97 |
713 | /** Build the mailbox select |
134e4174 |
714 | */ |
7d60bda5 |
715 | function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num = 0) |
6b37252b |
716 | { |
324ac3c5 |
717 | if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) { |
8cc8ec79 |
718 | $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox)); |
324ac3c5 |
719 | } else { |
8cc8ec79 |
720 | $missing = ''; |
324ac3c5 |
721 | } |
8cc8ec79 |
722 | return '<select name="mailbox[' . $row_num . ']">' |
723 | . $missing |
724 | . asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']') |
725 | . sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes, NULL) |
726 | . '</select>'; |
7d60bda5 |
727 | } |
728 | |
5b6abe97 |
729 | /** Build the Include subfolders checkbox |
e47df910 |
730 | * @todo if(function_exists('addCheckBox')) ? |
134e4174 |
731 | */ |
7d60bda5 |
732 | function asearch_get_form_sub($sub, $row_num = 0) |
733 | { |
8cc8ec79 |
734 | return function_exists('addCheckBox') ? addCheckBox('sub[' . $row_num .']', $sub) |
735 | : '<input type="checkbox" name="sub[' . $row_num .']"' . ($sub ? ' checked="checked"' : '') . ' />'; |
7d60bda5 |
736 | } |
737 | |
5b6abe97 |
738 | /** Build the 2 unop and where selects |
134e4174 |
739 | */ |
7d60bda5 |
740 | function asearch_get_form_location($unop, $where, $row_num = 0) |
741 | { |
8cc8ec79 |
742 | global $imap_asearch_unops, $imap_asearch_options; |
7d60bda5 |
743 | |
8cc8ec79 |
744 | return asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop) |
745 | . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where); |
7d60bda5 |
746 | } |
747 | |
5b6abe97 |
748 | /** Build the what text input |
134e4174 |
749 | */ |
7d60bda5 |
750 | function asearch_get_form_what($what, $row_num = 0) |
751 | { |
8cc8ec79 |
752 | return function_exists('addInput') ? addInput('what[' . $row_num . ']', $what, '35') |
753 | : '<input type="text" size="35" name="what[' . $row_num . ']" value="' . htmlspecialchars($what) . '" />'; |
7d60bda5 |
754 | } |
755 | |
5b6abe97 |
756 | /** Build the Exclude criteria checkbox |
e47df910 |
757 | * @todo if(function_exists('addCheckBox')) ? |
134e4174 |
758 | */ |
7d60bda5 |
759 | function asearch_get_form_exclude($exclude, $row_num = 0) |
760 | { |
8cc8ec79 |
761 | return function_exists('addCheckBox') ? addCheckBox('exclude['.$row_num.']', $exclude) |
762 | : '<input type="checkbox" name="exclude[' . $row_num .']"' . ($exclude ? ' checked="checked"' : '') . ' />'; |
7d60bda5 |
763 | } |
764 | |
765 | /** Print one advanced form row |
134e4174 |
766 | */ |
7d60bda5 |
767 | function asearch_print_form_row($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num) |
768 | { |
8cc8ec79 |
769 | global $imap_asearch_biops_in; |
770 | global $color; |
6b37252b |
771 | |
8cc8ec79 |
772 | echo html_tag('tr', '', '', $color[4]); |
6b37252b |
773 | |
7d60bda5 |
774 | //Binary operator |
8cc8ec79 |
775 | echo html_tag('td', $row_num ? |
776 | asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop) |
134e4174 |
777 | : '<b>' . _("In") . '</b>', 'center') . "\n"; |
7d60bda5 |
778 | |
779 | //Mailbox list and Include Subfolders |
8cc8ec79 |
780 | echo html_tag('td', |
781 | asearch_get_form_mailbox($imapConnection, $boxes, $mailbox, $row_num) |
782 | . _("and subfolders:") . asearch_get_form_sub($sub, $row_num), 'center') . "\n"; |
6b37252b |
783 | |
7d60bda5 |
784 | //Unary operator and Search location |
8cc8ec79 |
785 | echo html_tag('td', asearch_get_form_location($unop, $where, $row_num), 'center') . "\n"; |
6b37252b |
786 | |
7d60bda5 |
787 | //Text input |
8cc8ec79 |
788 | echo html_tag('td', asearch_get_form_what($what, $row_num), 'center') . "\n"; |
7d60bda5 |
789 | |
790 | //Exclude criteria |
8cc8ec79 |
791 | echo html_tag('td', _("Exclude Criteria:") . asearch_get_form_exclude($exclude, $row_num), 'center') . "\n"; |
6b37252b |
792 | |
8cc8ec79 |
793 | echo "</tr>\n"; |
6b37252b |
794 | } |
795 | |
7d60bda5 |
796 | /** Print the advanced search form |
134e4174 |
797 | */ |
7d60bda5 |
798 | function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array) |
6b37252b |
799 | { |
8cc8ec79 |
800 | global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html; |
801 | global $color; |
6b37252b |
802 | |
7d60bda5 |
803 | //Search Form |
8cc8ec79 |
804 | echo "<br />\n"; |
805 | echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n"; |
806 | |
807 | echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"'); |
808 | echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5')); |
809 | $row_count = count($where_array); |
810 | for ($row_num = 0; $row_num < $row_count; $row_num++) { |
811 | $mailbox = asearch_nz($mailbox_array[$row_num]); |
812 | $biop = strip_tags(asearch_nz($biop_array[$row_num])); |
813 | $unop = strip_tags(asearch_nz($unop_array[$row_num])); |
814 | $where = strip_tags(asearch_nz($where_array[$row_num])); |
815 | $what = asearch_nz($what_array[$row_num]); |
816 | $exclude = strip_tags(asearch_nz($exclude_array[$row_num])); |
817 | $sub = strip_tags(asearch_nz($sub_array[$row_num])); |
818 | asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num); |
819 | } |
820 | echo '</table>' . "\n"; |
6b37252b |
821 | |
7d60bda5 |
822 | //Submit buttons |
8cc8ec79 |
823 | echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"'); |
824 | echo html_tag('tr', |
e1728a7a |
825 | html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n" |
826 | . html_tag('td', getButton('submit', 'submit', $add_criteria_button_html), 'center') . "\n" |
827 | . html_tag('td', getButton('submit', 'submit', $del_all_button_html), 'center') . "\n" |
828 | . html_tag('td', getButton('submit', 'submit', $del_excluded_button_html), 'center') . "\n" |
8cc8ec79 |
829 | ); |
830 | echo '</table>' . "\n"; |
831 | echo '</form>' . "\n"; |
6b37252b |
832 | } |
833 | |
7d60bda5 |
834 | /** Print one basic form row |
134e4174 |
835 | */ |
7d60bda5 |
836 | function asearch_print_form_row_basic($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num) |
837 | { |
8cc8ec79 |
838 | global $search_button_html; |
839 | global $color; |
7d60bda5 |
840 | |
8cc8ec79 |
841 | echo html_tag('tr', '', '', $color[4]); |
7d60bda5 |
842 | |
843 | //Mailbox list |
8cc8ec79 |
844 | echo html_tag('td', '<b>' . _("In") . '</b> ' . asearch_get_form_mailbox($imapConnection, $boxes, $mailbox), 'center') . "\n"; |
7d60bda5 |
845 | |
846 | //Unary operator and Search location |
8cc8ec79 |
847 | echo html_tag('td', asearch_get_form_location($unop, $where), 'center') . "\n"; |
7d60bda5 |
848 | |
849 | //Text input |
8cc8ec79 |
850 | echo html_tag('td', asearch_get_form_what($what), 'center') . "\n"; |
7d60bda5 |
851 | |
852 | //Submit button |
e1728a7a |
853 | echo html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n"; |
7d60bda5 |
854 | |
8cc8ec79 |
855 | echo "</tr>\n"; |
7d60bda5 |
856 | } |
857 | |
858 | /** Print the basic search form |
134e4174 |
859 | */ |
7d60bda5 |
860 | function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array) |
861 | { |
8cc8ec79 |
862 | global $color; |
7d60bda5 |
863 | |
864 | //Search Form |
8cc8ec79 |
865 | echo "<br />\n"; |
866 | echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n"; |
867 | |
868 | echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"'); |
869 | //echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=4')); |
870 | $row_count = count($where_array); |
871 | for ($row_num = 0; $row_num < $row_count; $row_num++) { |
872 | $mailbox = asearch_nz($mailbox_array[$row_num]); |
873 | $biop = strip_tags(asearch_nz($biop_array[$row_num])); |
874 | $unop = strip_tags(asearch_nz($unop_array[$row_num])); |
875 | $where = strip_tags(asearch_nz($where_array[$row_num])); |
876 | $what = asearch_nz($what_array[$row_num]); |
877 | $exclude = strip_tags(asearch_nz($exclude_array[$row_num])); |
878 | $sub = strip_tags(asearch_nz($sub_array[$row_num])); |
879 | asearch_print_form_row_basic($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num); |
880 | } |
881 | echo '</table>' . "\n"; |
882 | echo '</form>' . "\n"; |
7d60bda5 |
883 | } |
884 | |
59a623e6 |
885 | |
40fbe929 |
886 | /** |
134e4174 |
887 | * @param array $boxes mailboxes array (reference) |
888 | * @return array selectable unformatted mailboxes names |
889 | */ |
40fbe929 |
890 | function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes) |
891 | { |
8cc8ec79 |
892 | $mboxes_array = array(); |
893 | $boxcount = count($boxes); |
894 | for ($boxnum = 0; $boxnum < $boxcount; $boxnum++) { |
895 | if (!in_array('noselect', $boxes[$boxnum]['flags'])) |
896 | $mboxes_array[] = $boxes[$boxnum]['unformatted']; |
897 | } |
898 | return $mboxes_array; |
40fbe929 |
899 | } |
900 | |
0d672ac0 |
901 | /* ------------------------ main ------------------------ */ |
7d60bda5 |
902 | /* get globals we will need */ |
6b37252b |
903 | sqgetGlobalVar('username', $username, SQ_SESSION); |
904 | sqgetGlobalVar('key', $key, SQ_COOKIE); |
7d60bda5 |
905 | sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); |
906 | sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION); |
6b37252b |
907 | |
ad2212db |
908 | if ( sqgetGlobalVar('checkall', $temp, SQ_GET) ) { |
139b3991 |
909 | $checkall = (int) $temp; |
ad2212db |
910 | } |
911 | |
324ac3c5 |
912 | /** |
913 | * Retrieve the mailbox cache from the session. |
914 | */ |
915 | sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION); |
916 | |
139b3991 |
917 | |
6b37252b |
918 | $search_button_html = _("Search"); |
919 | $search_button_text = asearch_unhtmlentities($search_button_html); |
920 | $add_criteria_button_html = _("Add New Criteria"); |
921 | $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html); |
34a85c56 |
922 | $del_excluded_button_html = _("Remove Excluded Criteria"); |
6b37252b |
923 | $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html); |
34a85c56 |
924 | $del_all_button_html = _("Remove All Criteria"); |
6b37252b |
925 | $del_all_button_text = asearch_unhtmlentities($del_all_button_html); |
926 | |
7d60bda5 |
927 | /** Maximum number of recent searches to handle |
134e4174 |
928 | * Default 0 |
929 | * @global integer $search_memory |
930 | */ |
7d60bda5 |
931 | $search_memory = getPref($data_dir, $username, 'search_memory', 0); |
932 | |
933 | /** Advanced search control |
134e4174 |
934 | * - 0 => allow basic interface only |
935 | * - 1 => allow advanced interface only |
936 | * - 2 => allow both |
937 | * Default 2 |
938 | */ |
5b6abe97 |
939 | $allow_advanced_search = asearch_nz($allow_advanced_search, 2); |
6b37252b |
940 | |
7d60bda5 |
941 | /** |
134e4174 |
942 | * Toggle advanced/basic search |
943 | */ |
324ac3c5 |
944 | if (sqgetGlobalVar('advanced', $search_advanced, SQ_GET)) { |
8cc8ec79 |
945 | setPref($data_dir, $username, 'search_advanced', $search_advanced & 1); |
324ac3c5 |
946 | } |
7d60bda5 |
947 | /** If 1, show advanced search interface |
134e4174 |
948 | * Default from allow_advanced_search pref |
949 | * @global integer $search_advanced |
950 | */ |
324ac3c5 |
951 | if ($allow_advanced_search > 1) { |
8cc8ec79 |
952 | $search_advanced = getPref($data_dir, $username, 'search_advanced', 0); |
324ac3c5 |
953 | } else { |
8cc8ec79 |
954 | $search_advanced = $allow_advanced_search; |
324ac3c5 |
955 | } |
7d60bda5 |
956 | if ($search_advanced) { |
957 | /** Set recent prefkeys according to $search_advanced |
134e4174 |
958 | * @global array $recent_prefkeys |
959 | */ |
8cc8ec79 |
960 | $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_unop', 'asearch_recent_biop', 'asearch_recent_exclude', 'asearch_recent_sub'); |
7d60bda5 |
961 | |
962 | /** Set saved prefkeys according to $search_advanced |
134e4174 |
963 | * @global array $saved_prefkeys |
964 | */ |
8cc8ec79 |
965 | $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_unop', 'asearch_saved_biop', 'asearch_saved_exclude', 'asearch_saved_sub'); |
7d60bda5 |
966 | |
967 | /*$asearch_prefkeys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/ |
324ac3c5 |
968 | } else { |
8cc8ec79 |
969 | $recent_prefkeys = array('search_where', 'search_folder', 'search_what', 'search_unop'); |
970 | $saved_prefkeys = array('saved_where', 'saved_folder', 'saved_what', 'saved_unop'); |
7d60bda5 |
971 | } |
6b37252b |
972 | |
2d2f8bb8 |
973 | /** How we did enter the form |
134e4174 |
974 | * - unset : Enter key, or called from outside (eg read_body) |
975 | * - $search_button_text : Search button |
976 | * - 'Search_no_update' : Search but don't update recent |
977 | * - 'Search_last' : Same as no_update but reload and search last |
978 | * - 'Search_silent' : Same as no_update but only display results |
979 | * - $add_criteria_button_text : Add New Criteria button |
980 | * - $del_excluded_button_text : Remove Excluded Criteria button |
981 | * - $del_all_button_text : Remove All Criteria button |
982 | * - 'save_recent' |
983 | * - 'search_recent' |
984 | * - 'forget_recent' |
985 | * - 'edit_saved' |
986 | * - 'search_saved' |
987 | * - 'delete_saved' |
988 | * @global string $submit |
989 | */ |
e09e556f |
990 | $searchpressed = false; |
324ac3c5 |
991 | if (isset($_GET['submit'])) { |
8cc8ec79 |
992 | $submit = strip_tags($_GET['submit']); |
324ac3c5 |
993 | } |
e09e556f |
994 | |
2d2f8bb8 |
995 | /** Searched mailboxes |
134e4174 |
996 | * @global array $mailbox_array |
997 | */ |
6b37252b |
998 | if (isset($_GET['mailbox'])) { |
8cc8ec79 |
999 | $mailbox_array = $_GET['mailbox']; |
324ac3c5 |
1000 | $targetmailbox = $_GET['mailbox']; |
1001 | if (!is_array($mailbox_array)) { |
8cc8ec79 |
1002 | $mailbox_array = array($mailbox_array); |
324ac3c5 |
1003 | } |
1004 | } else { |
8cc8ec79 |
1005 | $mailbox_array = array(); |
d0dd72b5 |
1006 | $targetmailbox = array(); |
324ac3c5 |
1007 | } |
1008 | $aMailboxGlobalPref = array( |
1009 | MBX_PREF_SORT => 0, |
1010 | MBX_PREF_LIMIT => (int) $show_num, |
1011 | MBX_PREF_AUTO_EXPUNGE => (bool) $auto_expunge, |
1012 | MBX_PREF_INTERNALDATE => (bool) getPref($data_dir, $username, 'internal_date_sort') |
1013 | // MBX_PREF_FUTURE => (var) $future |
1014 | ); |
1015 | |
1016 | /** |
1017 | * system wide admin settings and incoming vars. |
1018 | */ |
1019 | $aConfig = array( |
1020 | 'allow_thread_sort' => $allow_thread_sort, |
1021 | 'allow_server_sort' => $allow_server_sort, |
1022 | 'user' => $username, |
91c27aee |
1023 | 'setindex' => 1 |
324ac3c5 |
1024 | ); |
6b37252b |
1025 | |
2d2f8bb8 |
1026 | /** Binary operators |
134e4174 |
1027 | * @global array $biop_array |
1028 | */ |
6b37252b |
1029 | if (isset($_GET['biop'])) { |
8cc8ec79 |
1030 | $biop_array = $_GET['biop']; |
1031 | if (!is_array($biop_array)) |
1032 | $biop_array = array($biop_array); |
324ac3c5 |
1033 | } else { |
8cc8ec79 |
1034 | $biop_array = array(); |
324ac3c5 |
1035 | } |
2d2f8bb8 |
1036 | /** Unary operators |
134e4174 |
1037 | * @global array $unop_array |
1038 | */ |
6b37252b |
1039 | if (isset($_GET['unop'])) { |
8cc8ec79 |
1040 | $unop_array = $_GET['unop']; |
1041 | if (!is_array($unop_array)) |
1042 | $unop_array = array($unop_array); |
324ac3c5 |
1043 | } else { |
8cc8ec79 |
1044 | $unop_array = array(); |
324ac3c5 |
1045 | } |
2d2f8bb8 |
1046 | /** Where to search |
134e4174 |
1047 | * @global array $where_array |
1048 | */ |
6b37252b |
1049 | if (isset($_GET['where'])) { |
8cc8ec79 |
1050 | $where_array = $_GET['where']; |
324ac3c5 |
1051 | if (!is_array($where_array)) { |
8cc8ec79 |
1052 | $where_array = array($where_array); |
324ac3c5 |
1053 | } |
1054 | } else { |
8cc8ec79 |
1055 | $where_array = array(); |
324ac3c5 |
1056 | } |
2d2f8bb8 |
1057 | /** What to search |
134e4174 |
1058 | * @global array $what_array |
1059 | */ |
6b37252b |
1060 | if (isset($_GET['what'])) { |
8cc8ec79 |
1061 | $what_array = $_GET['what']; |
324ac3c5 |
1062 | if (!is_array($what_array)) { |
8cc8ec79 |
1063 | $what_array = array($what_array); |
324ac3c5 |
1064 | } |
1065 | } else { |
8cc8ec79 |
1066 | $what_array = array(); |
324ac3c5 |
1067 | } |
2d2f8bb8 |
1068 | /** Whether to exclude this criteria from search |
134e4174 |
1069 | * @global array $exclude_array |
1070 | */ |
324ac3c5 |
1071 | if (isset($_GET['exclude'])) { |
8cc8ec79 |
1072 | $exclude_array = $_GET['exclude']; |
324ac3c5 |
1073 | } else { |
8cc8ec79 |
1074 | $exclude_array = array(); |
324ac3c5 |
1075 | } |
2d2f8bb8 |
1076 | /** Search within subfolders |
134e4174 |
1077 | * @global array $sub_array |
1078 | */ |
324ac3c5 |
1079 | if (isset($_GET['sub'])) { |
8cc8ec79 |
1080 | $sub_array = $_GET['sub']; |
324ac3c5 |
1081 | } else { |
8cc8ec79 |
1082 | $sub_array = array(); |
324ac3c5 |
1083 | } |
2d2f8bb8 |
1084 | /** Row number used by recent and saved stuff |
134e4174 |
1085 | */ |
324ac3c5 |
1086 | if (isset($_GET['rownum'])) { |
6b37252b |
1087 | $submit_rownum = strip_tags($_GET['rownum']); |
324ac3c5 |
1088 | } |
2d2f8bb8 |
1089 | /** Change global sort |
134e4174 |
1090 | */ |
324ac3c5 |
1091 | if (sqgetGlobalVar('srt', $temp, SQ_GET)) { |
1092 | $srt = (int) $temp; |
1093 | asearch_edit_last(1); |
91c27aee |
1094 | // asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
324ac3c5 |
1095 | } |
1096 | if (sqgetGlobalVar('startMessage', $temp, SQ_GET)) { |
1097 | $startMessage = (int) $temp; |
1098 | asearch_edit_last(1); |
91c27aee |
1099 | // asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
c2d47d51 |
1100 | } |
1101 | |
324ac3c5 |
1102 | if ( sqgetGlobalVar('showall', $temp, SQ_GET) ) { |
1103 | $showall = (int) $temp; |
1104 | asearch_edit_last(1); |
91c27aee |
1105 | // asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
56e0b3b7 |
1106 | } |
91c27aee |
1107 | |
1108 | if ( sqgetGlobalVar('account', $temp, SQ_GET) ) { |
1109 | $iAccount = (int) $temp; |
1110 | } else { |
1111 | $iAccount = 0; |
1112 | } |
1113 | |
1114 | /** |
1115 | * Which templatedir are we using. TODO, add make a config var of this and make it possible to switch templates |
1116 | */ |
1117 | $sTplDir = SM_PATH . 'templates/default/'; |
1118 | |
1119 | |
324ac3c5 |
1120 | /** |
1121 | * Incoming submit buttons from the message list with search results |
1122 | */ |
1123 | if (sqgetGlobalVar('moveButton', $moveButton, SQ_POST) || |
1124 | sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST) || |
1125 | sqgetGlobalVar('delete', $markDelete, SQ_POST) || |
1126 | sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST) || |
1127 | sqgetGlobalVar('markRead', $markRead, SQ_POST) || |
1128 | sqgetGlobalVar('markUnread', $markUnread, SQ_POST) || |
1129 | sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST) || |
1130 | sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST) || |
1131 | sqgetGlobalVar('attache', $attache, SQ_POST)) { |
1132 | asearch_edit_last(1); |
1133 | $submit = ''; |
1134 | asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
1135 | } |
1136 | |
1137 | |
6b37252b |
1138 | |
7d60bda5 |
1139 | /** Toggle show/hide saved searches |
134e4174 |
1140 | */ |
324ac3c5 |
1141 | if (sqgetGlobalVar('search_show_saved', $search_show_saved, SQ_GET)) { |
8cc8ec79 |
1142 | setPref($data_dir, $username, 'search_show_saved', $search_show_saved & 1); |
324ac3c5 |
1143 | } |
7d60bda5 |
1144 | /** Toggle show/hide recent searches |
134e4174 |
1145 | */ |
324ac3c5 |
1146 | if (sqgetGlobalVar('search_show_recent', $search_show_recent, SQ_GET)) { |
8cc8ec79 |
1147 | setPref($data_dir, $username, 'search_show_recent', $search_show_recent & 1); |
324ac3c5 |
1148 | } |
7d60bda5 |
1149 | // end of get globals |
6b37252b |
1150 | |
40fbe929 |
1151 | /** If TRUE, do not show search interface |
134e4174 |
1152 | * Default FALSE |
1153 | * @global bool $search_silent |
1154 | */ |
7d60bda5 |
1155 | $search_silent = FALSE; |
6b37252b |
1156 | |
1157 | /* See how the page was called and fire off correct function */ |
7d60bda5 |
1158 | if ((empty($submit)) && (!empty($where_array))) { |
8cc8ec79 |
1159 | /* This happens when the Enter key is used or called from outside */ |
1160 | $submit = $search_button_text; |
324ac3c5 |
1161 | /* Hack needed to handle coming back from read_body et als */ |
1162 | if (count($where_array) != count($unop_array)) { |
1163 | /** |
1164 | * Hack to use already existen where and what vars. |
1165 | * where now contains the initiator page of the messagelist |
1166 | * and in this case 'search'. what contains an index to access |
1167 | * the search history |
1168 | */ |
1169 | |
1170 | sqgetGlobalVar('what',$what,SQ_GET); |
1171 | asearch_edit_last($what); |
1172 | } |
56e0b3b7 |
1173 | } |
6b37252b |
1174 | |
1175 | if (!isset($submit)) { |
8cc8ec79 |
1176 | $submit = ''; |
324ac3c5 |
1177 | } else { |
8cc8ec79 |
1178 | switch ($submit) { |
324ac3c5 |
1179 | case $search_button_text: |
1180 | if (asearch_check_query($where_array, $what_array, $exclude_array) == '') { |
1181 | asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
1182 | } |
8cc8ec79 |
1183 | break; |
324ac3c5 |
1184 | case 'Search_silent': |
1185 | $search_silent = TRUE; |
8cc8ec79 |
1186 | /*nobreak;*/ |
324ac3c5 |
1187 | case 'Search_no_update': |
1188 | $submit = $search_button_text; |
8cc8ec79 |
1189 | break; |
324ac3c5 |
1190 | case $del_excluded_button_text: |
1191 | $delarray = array_keys($exclude_array); |
1192 | while (!empty($delarray)) { |
1193 | $delrow = array_pop($delarray); |
1194 | array_splice($mailbox_array, $delrow, 1); |
1195 | array_splice($biop_array, $delrow, 1); |
1196 | array_splice($unop_array, $delrow, 1); |
1197 | array_splice($where_array, $delrow, 1); |
1198 | array_splice($what_array, $delrow, 1); |
134e4174 |
1199 | /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */ |
324ac3c5 |
1200 | array_splice($sub_array, $delrow, 1); |
1201 | } |
1202 | $exclude_array = array(); |
8cc8ec79 |
1203 | break; |
324ac3c5 |
1204 | case $del_all_button_text: |
1205 | $mailbox_array = array(); |
1206 | $biop_array = array(); |
1207 | $unop_array = array(); |
1208 | $where_array = array(); |
1209 | $what_array = array(); |
1210 | $exclude_array = array(); |
1211 | $sub_array = array(); |
8cc8ec79 |
1212 | break; |
324ac3c5 |
1213 | case 'save_recent': |
1214 | asearch_save_recent($submit_rownum); |
8cc8ec79 |
1215 | break; |
324ac3c5 |
1216 | case 'search_recent': |
1217 | $submit = $search_button_text; |
1218 | asearch_edit_recent($submit_rownum); |
1219 | asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
8cc8ec79 |
1220 | break; |
134e4174 |
1221 | case 'edit_recent': /* no link to do this, yet */ |
324ac3c5 |
1222 | asearch_edit_recent($submit_rownum); |
8cc8ec79 |
1223 | break; |
324ac3c5 |
1224 | case 'forget_recent': |
1225 | asearch_forget_recent($submit_rownum); |
8cc8ec79 |
1226 | break; |
324ac3c5 |
1227 | case 'search_saved': |
1228 | $submit = $search_button_text; |
1229 | asearch_edit_saved($submit_rownum); |
1230 | asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
8cc8ec79 |
1231 | break; |
324ac3c5 |
1232 | case 'edit_saved': |
1233 | asearch_edit_saved($submit_rownum); |
8cc8ec79 |
1234 | break; |
324ac3c5 |
1235 | case 'delete_saved': |
1236 | asearch_delete_saved($submit_rownum); |
8cc8ec79 |
1237 | break; |
1238 | } |
56e0b3b7 |
1239 | } |
23a9084b |
1240 | |
7d60bda5 |
1241 | //Texts in both basic and advanced form |
1242 | $imap_asearch_unops = array( |
8cc8ec79 |
1243 | '' => '', |
1244 | 'NOT' => _("Not") |
7d60bda5 |
1245 | ); |
1246 | |
1247 | if ($search_advanced) { |
8cc8ec79 |
1248 | //Texts in advanced form only |
1249 | $imap_asearch_options = array( |
1250 | //<message set>, |
1251 | //'ALL' is binary operator |
1252 | 'ANSWERED' => _("Answered"), |
1253 | 'BCC' => _("Bcc"), |
1254 | 'BEFORE' => _("Before"), |
1255 | 'BODY' => _("Message Body"), |
48027e89 |
1256 | 'CC' => _("Cc"), |
8cc8ec79 |
1257 | 'DELETED' => _("Deleted"), |
1258 | 'DRAFT' => _("Draft"), |
1259 | 'FLAGGED' => _("Flagged"), |
1260 | 'FROM' => _("Sent By"), |
1261 | 'HEADER' => _("Header Field"), |
1262 | 'KEYWORD' => _("Keyword"), |
1263 | 'LARGER' => _("Larger Than"), |
1264 | 'NEW' => _("New"), |
1265 | //'NOT' is unary operator |
1266 | 'OLD' => _("Old"), |
1267 | 'ON' => _("On"), |
1268 | //'OR' is binary operator |
1269 | 'RECENT' => _("Recent"), |
1270 | 'SEEN' => _("Seen"), |
1271 | 'SENTBEFORE' => _("Sent Before"), |
1272 | 'SENTON' => _("Sent On"), |
1273 | 'SENTSINCE' => _("Sent Since"), |
1274 | 'SINCE' => _("Since"), |
1275 | 'SMALLER' => _("Smaller Than"), |
1276 | 'SUBJECT' => _("Subject Contains"), |
1277 | 'TEXT' => _("Header and Body"), |
1278 | 'TO' => _("Sent To"), |
1279 | //'UID' => 'anum', |
134e4174 |
1280 | /* 'UNANSWERED' => '', |
8cc8ec79 |
1281 | 'UNDELETED' => '', |
1282 | 'UNDRAFT' => '', |
1283 | 'UNFLAGGED' => '', |
1284 | 'UNKEYWORD' => _("Unkeyword"), |
1285 | 'UNSEEN' => _("Unseen"),*/ |
1286 | ); |
1287 | |
1288 | $imap_asearch_biops_in = array( |
1289 | 'ALL' => _("And In"), |
1290 | 'OR' => _("Or In") |
1291 | ); |
1292 | |
1293 | $imap_asearch_biops = array( |
1294 | 'ALL' => _("And"), |
1295 | 'OR' => _("Or") |
1296 | ); |
324ac3c5 |
1297 | } else { |
8cc8ec79 |
1298 | //Texts in basic form only |
1299 | $imap_asearch_options = array( |
1300 | 'BCC' => _("Bcc"), |
1301 | 'BODY' => _("Body"), |
1302 | 'CC' => _("Cc"), |
1303 | 'FROM' => _("From"), |
1304 | 'SUBJECT' => _("Subject"), |
1305 | 'TEXT' => _("Everywhere"), |
1306 | 'TO' => _("To"), |
1307 | ); |
7d60bda5 |
1308 | } |
1309 | |
1310 | uasort($imap_asearch_options, 'asearch_unhtml_strcoll'); |
1311 | |
6b37252b |
1312 | /* open IMAP connection */ |
1313 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
324ac3c5 |
1314 | |
1315 | |
5b6abe97 |
1316 | /* get mailboxes once here */ |
6b37252b |
1317 | $boxes = sqimap_mailbox_list($imapConnection); |
99f3175e |
1318 | /* ensure we have a valid default mailbox name */ |
edda50ae |
1319 | $mailbox = asearch_nz($mailbox_array[0]); |
134e4174 |
1320 | if (($mailbox == '') || ($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?) |
1321 | $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;) |
99f3175e |
1322 | |
91c27aee |
1323 | |
1324 | /** |
1325 | * Handle form actions like flag / unflag, seen / unseen, delete |
1326 | */ |
1327 | if (sqgetGlobalVar('mailbox',$postMailbox,SQ_POST)) { |
1328 | if ($postMailbox) { |
1329 | /** |
1330 | * system wide admin settings and incoming vars. |
1331 | */ |
1332 | $aConfig = array( |
1333 | 'user' => $username, |
1334 | ); |
1335 | $aConfig['setindex'] = 1; // $what $where = 'search' |
1336 | /** |
1337 | * Set the max cache size to the number of mailboxes to avoid cache cleanups |
1338 | * when searching all mailboxes |
1339 | */ |
1340 | $aConfig['max_cache_size'] = count($boxes) +1; |
1341 | |
1342 | $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $postMailbox,$aConfig,array()); |
1343 | $sError = handleMessageListForm($imapConnection,$aMailbox); |
1344 | /* add the mailbox to the cache */ |
1345 | $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox; |
1346 | |
1347 | if ($sError) { |
1348 | $note = $sError; |
1349 | } |
1350 | } |
1351 | } |
1352 | |
1353 | if (isset($aMailbox['FORWARD_SESSION'])) { |
1354 | /* add the mailbox to the cache */ |
1355 | $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox; |
1356 | sqsession_register($mailbox_cache,'mailbox_cache'); |
1357 | |
1358 | if ($compose_new_win) { |
1359 | // write the session in order to make sure that the compose window has |
1360 | // access to the composemessages array which is stored in the session |
1361 | session_write_close(); |
8517e764 |
1362 | // restart the session. Do not use sqsession_is_active because the session_id |
1363 | // isn't empty after a session_write_close |
1364 | session_start(); |
91c27aee |
1365 | |
1366 | if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) { |
1367 | $compose_width = '640'; |
1368 | } |
1369 | if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) { |
1370 | $compose_height = '550'; |
1371 | } |
1372 | // do not use &, it will break the query string and $session will not be detected!!! |
1373 | $comp_uri = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox). |
1374 | '&session='.$aMailbox['FORWARD_SESSION']; |
1375 | displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri', $compose_width, $compose_height);", false); |
1376 | } else { |
1377 | // save mailboxstate |
1378 | sqsession_register($aMailbox,'aLastSelectedMailbox'); |
1379 | session_write_close(); |
1380 | // we have to redirect to the compose page |
1381 | $location = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox). |
1382 | '&session='.$aMailbox['FORWARD_SESSION']; |
1383 | header("Location: $location"); |
1384 | exit; |
1385 | } |
324ac3c5 |
1386 | } else { |
8cc8ec79 |
1387 | displayPageHeader($color, $mailbox); |
91c27aee |
1388 | // $compose_uri = $base_uri.'src/compose.php?newmessage=1'; |
1389 | } |
1390 | |
1391 | if (isset($note)) { |
a6d3eff6 |
1392 | echo html_tag( 'div', '<b>' . htmlspecialchars($note) .'</b>', 'center' ) . "<br />\n"; |
324ac3c5 |
1393 | } |
91c27aee |
1394 | |
1395 | |
6b37252b |
1396 | do_hook('search_before_form'); |
d81e351b |
1397 | |
6b37252b |
1398 | if (!$search_silent) { |
8cc8ec79 |
1399 | //Add a link to the other search mode if allowed |
1400 | if ($allow_advanced_search > 1) |
1401 | $toggle_link = ' - <small>[' |
1402 | . asearch_get_toggle_link(!$search_advanced, 'advanced', array(_("Standard search"), _("Advanced search"))) |
1403 | . ']</small>'; |
1404 | else |
1405 | $toggle_link = ''; |
1406 | |
1407 | echo html_tag('table', |
1408 | html_tag('tr', "\n" |
1409 | . html_tag('td', asearch_get_title_display($color, _("Search")) . $toggle_link, 'center', $color[0]) |
1410 | ) , |
1411 | '', '', 'width="100%"') . "\n"; |
1412 | asearch_print_saved($boxes); |
1413 | asearch_print_recent($boxes); |
1414 | if (empty($where_array)) { |
1415 | global $sent_folder; |
1416 | |
1417 | $mailbox_array[0] = $mailbox; |
1418 | $biop_array[0] = ''; |
1419 | $unop_array[0] = ''; |
324ac3c5 |
1420 | if ($mailbox == $sent_folder) { |
8cc8ec79 |
1421 | $where_array[0] = 'TO'; |
324ac3c5 |
1422 | } else { |
8cc8ec79 |
1423 | $where_array[0] = 'FROM'; |
324ac3c5 |
1424 | } |
8cc8ec79 |
1425 | $what_array[0] = ''; |
1426 | $exclude_array[0] = ''; |
1427 | $sub_array[0] = ''; |
1428 | } |
1429 | //Display advanced or basic form |
1430 | if ($search_advanced) { |
1431 | if ($submit == $add_criteria_button_text) { |
1432 | $last_index = max(count($where_array) - 1, 0); |
1433 | $mailbox_array[] = asearch_nz($mailbox_array[$last_index]); |
1434 | $biop_array[] = asearch_nz($biop_array[$last_index]); |
1435 | $unop_array[] = asearch_nz($unop_array[$last_index]); |
1436 | $where_array[] = asearch_nz($where_array[$last_index]); |
1437 | $what_array[] = asearch_nz($what_array[$last_index]); |
1438 | $exclude_array[] = asearch_nz($exclude_array[$last_index]); |
1439 | $sub_array[] = asearch_nz($sub_array[$last_index]); |
1440 | } |
1441 | asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
324ac3c5 |
1442 | } else { |
8cc8ec79 |
1443 | asearch_print_form_basic($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array); |
324ac3c5 |
1444 | } |
6b37252b |
1445 | } |
1446 | |
88cb1b4d |
1447 | do_hook('search_after_form'); |
56e0b3b7 |
1448 | |
6b37252b |
1449 | if ($submit == $search_button_text) { |
a73ae1c2 |
1450 | $msgsfound = false; |
8cc8ec79 |
1451 | echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"'); |
1452 | echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5])); |
1453 | 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])); |
1454 | echo '</table>' . "\n"; |
1455 | |
1456 | flush(); |
e09e556f |
1457 | $iMsgCnt = 0; |
8cc8ec79 |
1458 | $query_error = asearch_check_query($where_array, $what_array, $exclude_array); |
324ac3c5 |
1459 | if ($query_error != '') { |
8cc8ec79 |
1460 | echo '<br />' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n"; |
324ac3c5 |
1461 | } else { |
8cc8ec79 |
1462 | $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes); |
324ac3c5 |
1463 | /** |
1464 | * Retrieve the search queries |
1465 | */ |
1466 | $mboxes_mailbox = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array); |
1467 | foreach($mboxes_mailbox as $mbx => $search) { |
324ac3c5 |
1468 | |
91c27aee |
1469 | /** |
1470 | * until there is no per mailbox option screen to set prefs we override |
1471 | * the mailboxprefs by the default ones |
1472 | */ |
1473 | |
1474 | $aMailboxPrefSer=getPref($data_dir, $username,'pref_'.$iAccount.'_'.$mbx); |
324ac3c5 |
1475 | if ($aMailboxPrefSer) { |
1476 | $aMailboxPref = unserialize($aMailboxPrefSer); |
91c27aee |
1477 | $aMailboxPref[MBX_PREF_COLUMNS] = $index_order; |
324ac3c5 |
1478 | } else { |
91c27aee |
1479 | setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($default_mailbox_pref)); |
1480 | $aMailboxPref = $default_mailbox_pref; |
8cc8ec79 |
1481 | } |
324ac3c5 |
1482 | if (isset($srt) && $targetmailbox == $mbx) { |
1483 | $aMailboxPref[MBX_PREF_SORT] = (int) $srt; |
1484 | } |
91c27aee |
1485 | |
1486 | $trash_folder = (isset($trash_folder)) ? $trash_folder : false; |
1487 | $sent_folder = (isset($sent_folder)) ? $sent_folder : false; |
1488 | $draft_folder = (isset($draft_folder)) ? $draft_folder : false; |
1489 | |
324ac3c5 |
1490 | |
1491 | /** |
1492 | * until there is no per mailbox option screen to set prefs we override |
1493 | * the mailboxprefs by the default ones |
1494 | */ |
1495 | $aMailboxPref[MBX_PREF_LIMIT] = (int) $show_num; |
1496 | $aMailboxPref[MBX_PREF_AUTO_EXPUNGE] = (bool) $auto_expunge; |
1497 | $aMailboxPref[MBX_PREF_INTERNALDATE] = (bool) getPref($data_dir, $username, 'internal_date_sort'); |
91c27aee |
1498 | $aMailboxPref[MBX_PREF_COLUMNS] = $index_order; |
1499 | |
1500 | /** |
1501 | * Replace From => To in case it concerns a draft or sent folder |
1502 | */ |
1503 | if (($mbx == $sent_folder || $mbx == $draft_folder) && |
1504 | !in_array(SQM_COL_TO,$aMailboxPref[MBX_PREF_COLUMNS])) { |
1505 | $aNewOrder = array(); // nice var name ;) |
1506 | foreach($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) { |
1507 | if ($iCol == SQM_COL_FROM) { |
1508 | $iCol = SQM_COL_TO; |
1509 | } |
1510 | $aNewOrder[] = $iCol; |
1511 | } |
1512 | $aMailboxPref[MBX_PREF_COLUMNS] = $aNewOrder; |
f16dc830 |
1513 | setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($aMailboxPref)); |
91c27aee |
1514 | } |
324ac3c5 |
1515 | |
324ac3c5 |
1516 | $aConfig['search'] = $search['search']; |
1517 | $aConfig['charset'] = $search['charset']; |
324ac3c5 |
1518 | |
324ac3c5 |
1519 | /** |
91c27aee |
1520 | * Set the max cache size to the number of mailboxes to avoid cache cleanups |
1521 | * when searching all mailboxes |
1522 | */ |
1523 | $aConfig['max_cache_size'] = count($mboxes_mailbox) +1; |
1524 | if (isset($startMessage) && $targetmailbox == $mbx) { |
1525 | $aConfig['offset'] = $startMessage; |
e09e556f |
1526 | } else { |
1527 | $aConfig['offset'] = 0; |
91c27aee |
1528 | } |
1529 | if (isset($showall) && $targetmailbox == $mbx) { |
1530 | $aConfig['showall'] = $showall; |
1531 | } else { |
1532 | if (isset($aConfig['showall'])) { |
1533 | unset($aConfig['showall']); |
1534 | } |
1535 | $showall = false; |
1536 | } |
1537 | |
1538 | /** |
1539 | * Set the config options for the messages list |
324ac3c5 |
1540 | */ |
91c27aee |
1541 | $aColumns = array(); |
1542 | foreach ($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) { |
1543 | $aColumns[$iCol] = array(); |
1544 | switch ($iCol) { |
1545 | case SQM_COL_SUBJ: |
1546 | if ($truncate_subject) { |
1547 | $aColumns[$iCol]['truncate'] = $truncate_subject; |
1548 | } |
1549 | break; |
1550 | case SQM_COL_FROM: |
1551 | case SQM_COL_TO: |
1552 | case SQM_COL_CC: |
1553 | case SQM_COL_BCC: |
1554 | if ($truncate_sender) { |
1555 | $aColumns[$iCol]['truncate'] = $truncate_sender; |
1556 | } |
1557 | break; |
324ac3c5 |
1558 | } |
1559 | } |
91c27aee |
1560 | |
1561 | |
1562 | $aProps = array( |
1563 | 'columns' => $aColumns, |
1564 | 'config' => array('alt_index_colors' => $alt_index_colors, |
1565 | 'highlight_list' => $message_highlight_list, |
1566 | 'fancy_index_highlite' => $fancy_index_highlite, |
1567 | 'show_flag_buttons' => (isset($show_flag_buttons)) ? $show_flag_buttons : true, |
1568 | 'lastTargetMailbox' => (isset($lastTargetMailbox)) ? $lastTargetMailbox : '', |
1569 | 'trash_folder' => $trash_folder, |
1570 | 'sent_folder' => $sent_folder, |
1571 | 'draft_folder' => $draft_folder, |
1572 | 'enablesort' => true, |
1573 | 'color' => $color |
1574 | ), |
1575 | 'mailbox' => $mbx, |
1576 | 'account' => (isset($iAccount)) ? $iAccount : 0, |
1577 | 'module' => 'read_body', |
1578 | 'email' => false); |
1579 | |
1580 | |
1581 | $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $mbx,$aConfig,$aMailboxPref); |
1582 | |
1583 | $iError = 0; |
1584 | $aTemplate = showMessagesForMailbox($imapConnection, $aMailbox,$aProps, $iError); |
e09e556f |
1585 | |
1586 | // in th future we can make use of multiple message sets, now set it to 1 for search. |
1587 | $iSetIndex = 1; |
1588 | if (isset($aMailbox['UIDSET'][$iSetIndex])) { |
1589 | $iMsgCnt += count($aMailbox['UIDSET'][$iSetIndex]); |
1590 | } |
91c27aee |
1591 | if ($iError) { |
1592 | // error handling |
1593 | } else { |
1594 | /** |
1595 | * In the future, move this the the initialisation area |
1596 | */ |
1597 | sqgetGlobalVar('align',$align,SQ_SESSION); |
1598 | |
1599 | $oTemplate = new Template($sTplDir); |
1600 | |
1601 | if ($aMailbox['EXISTS'] > 0) { |
91c27aee |
1602 | if ($iError) { |
e09e556f |
1603 | // TODO |
1604 | echo "ERROR occured, errorhandler will be implemented very soon"; |
91c27aee |
1605 | } else { |
e09e556f |
1606 | foreach ($aTemplate as $k => $v) { |
1607 | $oTemplate->assign($k, $v); |
1608 | } |
1609 | $oTemplate->assign('page_selector', $page_selector); |
1610 | $oTemplate->assign('page_selector_max', $page_selector_max); |
1611 | $oTemplate->assign('compact_paginator', $compact_paginator); |
1612 | $oTemplate->assign('javascript_on', $javascript_on); |
1613 | $oTemplate->assign('enablesort', (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false); |
1614 | // Aaaaaahhhhhhh FIX ME DO NOT USE the string "none" for a var when you mean the boolean false or null |
1615 | $oTemplate->assign('icon_theme', (isset($icon_theme) && $icon_theme !== 'none') ? $icon_theme : false); |
1616 | $oTemplate->assign('use_icons', (isset($use_icons)) ? $use_icons : false); |
1617 | $oTemplate->assign('aOrder', array_keys($aColumns)); |
1618 | $oTemplate->assign('alt_index_colors', isset($alt_index_colors) ? $alt_index_colors: false); |
1619 | $oTemplate->assign('color', $color); |
1620 | $oTemplate->assign('align', $align); |
e09e556f |
1621 | |
1622 | $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']); |
1623 | if (strtoupper($mbx) == 'INBOX') { |
1624 | $mailbox_display = _("INBOX"); |
1625 | } else { |
1626 | $mailbox_display = imap_utf7_decode_local($mbx); |
1627 | } |
91c27aee |
1628 | |
e09e556f |
1629 | echo '<br /><b><big>' . _("Folder:") . ' '. $mailbox_display . ' </big></b>'; |
91c27aee |
1630 | |
e09e556f |
1631 | $oTemplate->display('message_list.tpl'); |
1632 | } |
91c27aee |
1633 | } |
a73ae1c2 |
1634 | } |
91c27aee |
1635 | |
324ac3c5 |
1636 | /* add the mailbox to the cache */ |
91c27aee |
1637 | $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox; |
8cc8ec79 |
1638 | |
324ac3c5 |
1639 | } |
8cc8ec79 |
1640 | } |
e09e556f |
1641 | if(!$iMsgCnt) { |
a73ae1c2 |
1642 | echo '<br />' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n"; |
1643 | } |
56e0b3b7 |
1644 | } |
1645 | |
70c4fd84 |
1646 | do_hook('search_bottom'); |
6b37252b |
1647 | sqimap_logout($imapConnection); |
dcc1cc82 |
1648 | echo '</body></html>'; |
324ac3c5 |
1649 | sqsession_register($mailbox_cache,'mailbox_cache'); |
a2b193bc |
1650 | |
e50f5ac2 |
1651 | ?> |