6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * IMAP Advanced search page
11 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
14 * @package squirrelmail
15 * @link http://www.ietf.org/rfc/rfc3501.txt
16 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
20 * Path for SquirrelMail required files.
23 define('SM_PATH','../');
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()...
37 * @return bool strcoll()-like result
39 function asearch_unhtml_strcoll($a, $b)
41 return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b));
45 * @param string $mailbox mailbox name
46 * @return string mailbox name ready to display
48 function imap_get_mailbox_display($mailbox)
50 if (strtoupper($mailbox) == 'INBOX')
52 return imap_utf7_decode_local($mailbox);
56 * @param string $mailbox mailbox name
57 * @return string mailbox name ready to display
59 function asearch_get_mailbox_display($mailbox)
61 if ($mailbox == 'All Folders')
62 return _("All Folders");
63 return imap_get_mailbox_display($mailbox);
67 * @param array $color color array
68 * @param string $txt text to display
69 * @return string title ready to display
71 function asearch_get_title_display($color, $txt)
73 return '<b><big>' . $txt . '</big></b>';
77 * @param array $color color array
78 * @param string $txt text to display
79 * @return string error text ready to display
81 function asearch_get_error_display($color, $txt)
83 return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>';
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
90 function asearch_serialize($input_array)
92 return serialize($input_array);
96 * @param string $input_string string to unserialize
99 function asearch_unserialize($input_string)
101 return unserialize($input_string);
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
112 function asearch_getPref($data_dir, $username, $key, $index, $default = '')
114 return getPref($data_dir, $username, $key . $index, $default);
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
125 function asearch_setPref($data_dir, $username, $key, $index, $value)
127 return setPref($data_dir, $username, $key . $index, $value);
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
137 function asearch_removePref($data_dir, $username, $key, $index)
139 return removePref($data_dir, $username, $key . $index);
142 /** Sanity checks, done before running the imap command and before push_recent */
143 function asearch_check_query($where_array, $what_array, $exclude_array)
145 global $imap_asearch_opcodes;
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");
160 /** Read the recent searches */
161 function asearch_read_recent($data_dir, $username)
163 global $recent_prefkeys;
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;
173 if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) {
174 foreach ($recent_prefkeys as $key) {
175 array_pop($recent_array[$key]);
180 return $recent_array;
183 /** Get the saved searches */
184 function asearch_read_saved($data_dir, $username)
186 global $saved_prefkeys;
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);
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);
203 /** Save a recent search */
204 function asearch_save_recent($data_dir, $username, $recent_index)
206 global $recent_prefkeys, $saved_prefkeys;
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);
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]);
217 asearch_removePref($data_dir, $username, $saved_prefkeys[$n], $saved_index);
222 /** Write a recent search */
223 function asearch_write_recent($data_dir, $username, $recent_array)
225 global $recent_prefkeys;
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]);
234 for (; $recent_num < $recent_max; $recent_num++
) {
235 foreach ($recent_prefkeys as $key) {
236 asearch_removePref($data_dir, $username, $key, $recent_num);
241 /** Forget a recent search */
242 function asearch_forget_recent($data_dir, $username, $forget_index)
244 global $recent_prefkeys;
246 $recent_array = asearch_read_recent($data_dir, $username);
247 foreach ($recent_prefkeys as $key) {
248 array_splice($recent_array[$key], $forget_index, 1);
250 asearch_write_recent($data_dir, $username, $recent_array);
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)
256 global $recent_prefkeys;
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])) {
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]
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)
286 global $recent_prefkeys;
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);
297 $input = array($where_array, $mailbox_array, $what_array, $biop_array, $unop_array, $exclude_array, $sub_array);
299 foreach ($recent_prefkeys as $key) {
300 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
303 asearch_write_recent($data_dir, $username, $recent_array);
308 * Edit a recent search
309 * @global array mailbox_array searched mailboxes
311 function asearch_edit_recent($data_dir, $username, $index)
313 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array;
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));
324 /** Edit the last recent search if the prefs permit it */
325 function asearch_edit_last($data_dir, $username)
327 if (getPref($data_dir, $username, 'search_memory', 0) > 0)
328 asearch_edit_recent($data_dir, $username, 0);
331 /** Edit a saved search */
332 function asearch_edit_saved($data_dir, $username, $index)
334 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array;
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));
345 /** Write a saved searches */
346 function asearch_write_saved($data_dir, $username, $saved_array)
348 global $saved_prefkeys;
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]);
356 foreach ($saved_prefkeys as $key) {
357 asearch_removePref($data_dir, $username, $key, $saved_count);
361 /** Delete a saved search */
362 function asearch_delete_saved($data_dir, $username, $saved_index)
364 global $saved_prefkeys;
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);
371 asearch_write_saved($data_dir, $username, $saved_array);
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)
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];*/
384 $what_display = _("(Illegal date)");
387 $what_display = _("(Wrong date)");
388 return $what_display;
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)
394 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
395 global $imap_asearch_opcodes;
397 $last_mailbox = $mailbox_array[0];
398 if (empty($last_mailbox))
399 $last_mailbox = 'INBOX';
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");
412 $biop_display = $imap_asearch_biops_in[$biop];
413 $last_mailbox = $cur_mailbox;
416 $mailbox_display = '';
417 $biop_display = $imap_asearch_biops[$biop];
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>';
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 */
432 $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)"));
434 if ($what_type == 'adate')
435 $what_display = asearch_get_date_display($what);
437 $what_display = htmlspecialchars($what);
438 $what_display = ' <B>' . $what_display . '</B>';
443 $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)"));
447 $query_display .= ' ' . $biop_display . $mailbox_display . $where_display . $what_display;
450 return $query_display;
453 /** Handle the alternate row colors */
454 function asearch_get_row_color($color, $row_num)
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];
465 return $color_string;
468 /** Print a whole query array, recent or saved */
469 function asearch_print_query_array($query_array, $query_keys, $action_array, $title)
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));
482 unset($search_array);
483 foreach ($query_keys as $query_key) {
484 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
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);
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_advanced.php?submit=' . $action . '&rownum=' . $query_num . '>' . $action_display . '</a>', 'center');
505 echo '</table>' . "\n";
508 /** Print the saved array */
509 function asearch_print_saved($data_dir, $username)
511 global $saved_prefkeys;
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"));
523 /** Print the recent array */
524 function asearch_print_recent($data_dir, $username)
526 global $recent_prefkeys;
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"));
539 /** Build an <option> statement */
540 function asearch_opt($val, $sel, $tit)
542 return '<option value="' . $val . '"' . ($sel == $val ?
' selected' : '') . '>' . $tit . '</option>' . "\n";
545 /** Build a <select> statement from an array */
546 function asearch_opt_array($var_name, $opt_array, $cur_val)
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";
555 /** Verify that a mailbox exists */
556 function asearch_mailbox_exists($mailbox, $boxes)
558 foreach ($boxes as $box) {
559 if ($box['unformatted'] == $mailbox)
565 /** Print one form row */
566 function asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
568 global $imap_asearch_biops_in, $imap_asearch_unops, $imap_asearch_options;
571 echo html_tag('tr', '', '', $color[4]);
573 echo html_tag('td', '', 'center');
574 /* Binary operator */
576 echo asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop);
578 echo /*'<input type="hidden" name="biop[0]" value="">' .*/ '<b>' . _("In") . '</b>';
581 echo html_tag('td', '', 'center');
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";
590 /* Include Subfolders */
591 echo _("and subfolders:") . '<input type=checkbox name="sub[' . $row_num .']"' . ($sub ?
' CHECKED' : '') . '></td>' . "\n";
593 /* Unary operator and Search location */
595 asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
596 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where),
600 $what_disp = htmlspecialchars($what);
601 echo html_tag('td', '<input type="text" size="35" name="what[' . $row_num . ']" value="' . $what_disp . '">', 'center') . "\n";
603 /* Exclude criteria */
605 _("Exclude Criteria:") . '<input type=checkbox name="exclude[' . $row_num .']"' . ($exclude ?
' CHECKED' : '') . '>', 'center', '') . "\n";
610 /** Print the search form */
611 function asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
613 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
618 echo '<form action="search_advanced.php" name="form_asearch">' . "\n";
620 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
621 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
622 $row_count = count($where_array);
623 for ($row_num = 0; $row_num < $row_count; $row_num++
) {
624 $mailbox = asearch_nz($mailbox_array[$row_num]);
625 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
626 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
627 $where = strip_tags(asearch_nz($where_array[$row_num]));
628 $what = asearch_nz($what_array[$row_num]);
629 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
630 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
631 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
633 echo '</table>' . "\n";
636 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
638 html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n"
639 . html_tag('td', getButton('SUBMIT', 'submit', $add_criteria_button_html), 'center') . "\n"
640 . html_tag('td', getButton('SUBMIT', 'submit', $del_all_button_html), 'center') . "\n"
641 . html_tag('td', getButton('SUBMIT', 'submit', $del_excluded_button_html), 'center') . "\n"
643 echo '</table>' . "\n";
644 echo '</form>' . "\n";
647 /** Print the $msgs messages from $mailbox mailbox */
648 function asearch_print_mailbox_msgs($imapConnection, $mailbox, $msgs, $cnt, $sort, $color, $where, $what)
651 global $allow_server_sort, $allow_thread_sort, $thread_sort_messages;
653 $thread_sort_messages = 0;
654 if ($allow_thread_sort) {
655 global $data_dir, $username;
656 $thread_sort_messages = getPref($data_dir, $username, 'thread_' . $mailbox);
660 elseif ($allow_server_sort) {
665 $msort = calc_msort($msgs, $sort);
668 $mailbox_display = asearch_get_mailbox_display($mailbox);
670 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
671 $paginator_str = '<b><big>' . _("Folder:") . ' '. $mailbox_display . '</big></b>';
673 echo '<br><table border="0" width="100%" cellpadding="0" cellspacing="0">';
676 mail_message_listing_beginning($imapConnection, NULL, $mailbox, $real_sort, $msg_cnt_str, $paginator_str, 1, 1);
679 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
682 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
685 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
687 printHeader($mailbox, $sort, $color, !$thread_sort_messages);
688 displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $real_sort, $color, $cnt, $where, $what);
693 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
701 * @param array $boxes mailboxes array (reference)
702 * @return array selectable unformatted mailboxes names
704 function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
706 $mboxes_array = array();
707 $boxcount = count($boxes);
708 for ($boxnum=0; $boxnum<$boxcount; $boxnum++
) {
709 if (!in_array('noselect', $boxes[$boxnum]['flags']))
710 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
712 return $mboxes_array;
715 /* ------------------------ main ------------------------ */
716 /* get globals we may need */
717 sqgetGlobalVar('username', $username, SQ_SESSION
);
718 sqgetGlobalVar('key', $key, SQ_COOKIE
);
719 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
); /* we really need this? */
720 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
); /* do we really need this? */
722 $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_biop', 'asearch_recent_unop', 'asearch_recent_exclude', 'asearch_recent_sub');
723 $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_biop', 'asearch_saved_unop', 'asearch_saved_exclude', 'asearch_saved_sub');
724 /*$asearch_keys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/
726 $search_button_html = _("Search");
727 $search_button_text = asearch_unhtmlentities($search_button_html);
728 $add_criteria_button_html = _("Add New Criteria");
729 $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
730 $del_excluded_button_html = _("Remove Excluded Criteria");
731 $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
732 $del_all_button_html = _("Remove All Criteria");
733 $del_all_button_text = asearch_unhtmlentities($del_all_button_html);
735 $imap_asearch_options = array(
737 /*'ALL' is binary operator */
738 'ANSWERED' => _("Answered"),
740 'BEFORE' => _("Before"),
741 'BODY' => _("Message Body"),
743 'DELETED' => _("Deleted"),
744 'DRAFT' => _("Draft"),
745 'FLAGGED' => _("Flagged"),
746 'FROM' => _("Sent By"),
747 'HEADER' => _("Header Field"),
748 'KEYWORD' => _("Keyword"),
749 'LARGER' => _("Larger Than"),
751 /*'NOT' is unary operator */
754 /*'OR' is binary operator */
755 'RECENT' => _("Recent"),
757 'SENTBEFORE' => _("Sent Before"),
758 'SENTON' => _("Sent On"),
759 'SENTSINCE' => _("Sent Since"),
760 'SINCE' => _("Since"),
761 'SMALLER' => _("Smaller Than"),
762 'SUBJECT' => _("Subject Contains"),
763 'TEXT' => _("Header and Body"),
764 'TO' => _("Sent To"),
766 /*'UNANSWERED' => '',
770 'UNKEYWORD' => _("Unkeyword"),
771 'UNSEEN' => _("Unseen"),*/
773 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
775 $imap_asearch_unops = array(
780 $imap_asearch_biops_in = array(
781 'ALL' => _("And In"),
785 $imap_asearch_biops = array(
790 /** How we did enter the form
791 * - unset : Enter key, or called from outside (eg read_body)
792 * - $search_button_text : Search button
793 * - 'Search_no_update' : Search but don't update recent
794 * - 'Search_last' : Same as no_update but reload and search last
795 * - 'Search_silent' : Same as no_update but only display results
796 * - $add_criteria_button_text : Add New Criteria button
797 * - $del_excluded_button_text : Remove Excluded Criteria button
798 * - $del_all_button_text : Remove All Criteria button
805 * @global string $submit
807 if (isset($_GET['submit']))
808 $submit = strip_tags($_GET['submit']);
810 /** Searched mailboxes
811 * @global array $mailbox_array
813 if (isset($_GET['mailbox'])) {
814 $mailbox_array = $_GET['mailbox'];
815 if (!is_array($mailbox_array))
816 $mailbox_array = array($mailbox_array);
819 $mailbox_array = array();
822 * @global array $biop_array
824 if (isset($_GET['biop'])) {
825 $biop_array = $_GET['biop'];
826 if (!is_array($biop_array))
827 $biop_array = array($biop_array);
830 $biop_array = array();
833 * @global array $unop_array
835 if (isset($_GET['unop'])) {
836 $unop_array = $_GET['unop'];
837 if (!is_array($unop_array))
838 $unop_array = array($unop_array);
841 $unop_array = array();
844 * @global array $where_array
846 if (isset($_GET['where'])) {
847 $where_array = $_GET['where'];
848 if (!is_array($where_array))
849 $where_array = array($where_array);
852 $where_array = array();
855 * @global array $what_array
857 if (isset($_GET['what'])) {
858 $what_array = $_GET['what'];
859 if (!is_array($what_array))
860 $what_array = array($what_array);
863 $what_array = array();
865 /** Whether to exclude this criteria from search
866 * @global array $exclude_array
868 if (isset($_GET['exclude']))
869 $exclude_array = $_GET['exclude'];
871 $exclude_array = array();
873 /** Search within subfolders
874 * @global array $sub_array
876 if (isset($_GET['sub']))
877 $sub_array = $_GET['sub'];
879 $sub_array = array();
881 /** Row number used by recent and saved stuff
883 if (isset($_GET['rownum']))
884 $submit_rownum = strip_tags($_GET['rownum']);
886 /** Change global sort
888 if (sqgetGlobalVar('newsort', $newsort, SQ_GET
)) {
889 setPref($data_dir, $username, 'sort', $newsort);
891 sqsession_register($sort, 'sort');
892 asearch_edit_last($data_dir, $username);
895 /** Change mailbox threading
897 if (sqgetGlobalVar('set_thread', $set_thread, SQ_GET
)) {
898 setPref($data_dir, $username, 'thread_' . $mailbox_array[0], ($set_thread == 1) ?
1 : 0 );
899 asearch_edit_last($data_dir, $username);
902 /* end of get globals */
904 /** If TRUE, do not show search interface
905 * @global bool $search_silent
907 $search_silent = FALSE; /* Default is normal behaviour */
909 /* See how the page was called and fire off correct function */
910 if ((!isset($submit) ||
empty($submit)) && !empty($where_array)) {
911 /* This happens when the Enter key is used or called from outside */
912 $submit = $search_button_text;
913 if (count($where_array) != count($unop_array)) /* Hack needed to handle coming back from read_body et als */
914 asearch_edit_last($data_dir, $username);
917 if (!isset($submit)) {
922 case $search_button_text:
923 if (asearch_check_query($where_array, $what_array, $exclude_array) == '')
924 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
926 case 'Search_silent':
927 $search_silent = TRUE;
929 case 'Search_no_update':
930 $submit = $search_button_text;
932 case $del_excluded_button_text:
933 $delarray = array_keys($exclude_array);
934 while (!empty($delarray)) {
935 $delrow = array_pop($delarray);
936 array_splice($mailbox_array, $delrow, 1);
937 array_splice($biop_array, $delrow, 1);
938 array_splice($unop_array, $delrow, 1);
939 array_splice($where_array, $delrow, 1);
940 array_splice($what_array, $delrow, 1);
941 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
942 array_splice($sub_array, $delrow, 1);
944 $exclude_array = array();
946 case $del_all_button_text:
947 $mailbox_array = array();
948 $biop_array = array();
949 $unop_array = array();
950 $where_array = array();
951 $what_array = array();
952 $exclude_array = array();
953 $sub_array = array();
956 asearch_save_recent($data_dir, $username, $submit_rownum);
958 case 'search_recent':
959 $submit = $search_button_text;
960 asearch_edit_recent($data_dir, $username, $submit_rownum);
961 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
963 case 'edit_recent': /* no link to do this, yet */
964 asearch_edit_recent($data_dir, $username, $submit_rownum);
966 case 'forget_recent':
967 asearch_forget_recent($data_dir, $username, $submit_rownum);
970 $submit = $search_button_text;
971 asearch_edit_saved($data_dir, $username, $submit_rownum);
972 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
975 asearch_edit_saved($data_dir, $username, $submit_rownum);
978 asearch_delete_saved($data_dir, $username, $submit_rownum);
983 /* open IMAP connection */
984 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
985 /* get mailbox names once here */
986 $boxes = sqimap_mailbox_list($imapConnection);
987 /* ensure we have a valid default mailbox name */
988 $mailbox = asearch_nz($mailbox_array[0]);
989 if (($mailbox == '') ||
($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
990 $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;)
992 if (isset($composenew) && $composenew) {
993 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox) .
994 "&session=$composesession&attachedmessages=true&";
995 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
998 displayPageHeader($color, $mailbox);
1000 do_hook('search_before_form');
1002 if (!$search_silent) {
1003 echo html_tag('table',
1004 html_tag('tr', "\n" .
1005 html_tag('td', asearch_get_title_display($color, _("Search")), 'center', $color[0])
1007 '', '', 'width="100%"') . "\n";
1008 asearch_print_saved($data_dir, $username);
1009 asearch_print_recent($data_dir, $username);
1010 if (empty($where_array)) {
1011 global $sent_folder;
1013 $mailbox_array[0] = $mailbox;
1014 $biop_array[0] = '';
1015 $unop_array[0] = '';
1016 if ($mailbox == $sent_folder)
1017 $where_array[0] = 'TO';
1019 $where_array[0] = 'FROM';
1020 $what_array[0] = '';
1021 $exclude_array[0] = '';
1024 if ($submit == $add_criteria_button_text) {
1025 $last_index = max(count($where_array) - 1, 0);
1026 $mailbox_array[] = asearch_nz($mailbox_array[$last_index]);
1027 $biop_array[] = asearch_nz($biop_array[$last_index]);
1028 $unop_array[] = asearch_nz($unop_array[$last_index]);
1029 $where_array[] = asearch_nz($where_array[$last_index]);
1030 $what_array[] = asearch_nz($what_array[$last_index]);
1031 $exclude_array[] = asearch_nz($exclude_array[$last_index]);
1032 $sub_array[] = asearch_nz($sub_array[$last_index]);
1034 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1037 /*********************************************************************
1038 * Check to see if we can use cache or not. Currently the only time *
1039 * when you will not use it is when a link on the left hand frame is *
1040 * used. Also check to make sure we actually have the array in the *
1041 * registered session data. :) *
1042 *********************************************************************/
1043 if (!isset($use_mailbox_cache))
1044 $use_mailbox_cache = 0;
1046 do_hook('search_after_form');
1048 if ($submit == $search_button_text) {
1049 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1050 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
1051 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]));
1052 echo '</table>' . "\n";
1056 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
1057 if ($query_error != '')
1058 echo '<br>' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
1061 // Disable thread sort for now if there is more than one mailbox or at least one 'All Folders'
1062 global $allow_thread_sort;
1063 $old_allow_thread_sort = $allow_thread_sort;
1064 $allow_thread_sort = ((count(array_unique($mailbox_array)) <= 1) && (!in_array('All Folders', $mailbox_array)));
1066 $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes);
1067 $mboxes_msgs = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array);
1068 if (empty($mboxes_msgs))
1069 echo '<br>' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
1071 foreach($mboxes_msgs as $mailbox => $msgs) {
1072 sqimap_mailbox_select($imapConnection, $mailbox);
1073 $msgs = fillMessageArray($imapConnection, $msgs, count($msgs));
1074 /* For now just keep the first criteria to make the regular search happy if the user tries to come back to search */
1075 /* $where = asearch_serialize($where_array);
1076 $what = asearch_serialize($what_array);*/
1077 $where = $where_array[0];
1078 $what = $what_array[0];
1079 asearch_print_mailbox_msgs($imapConnection, $mailbox, $msgs, count($msgs), $sort, $color, urlencode($where), urlencode($what));
1083 $allow_thread_sort = $old_allow_thread_sort; // Restore thread sort
1087 do_hook('search_bottom');
1088 sqimap_logout($imapConnection);
1089 echo '</body></html>';