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