6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
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.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:") .
592 addCheckBox('sub[' . $row_num .']', $sub) . '</td>' . "\n";
594 /* Unary operator and Search location */
596 asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
597 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where),
601 echo html_tag('td', addInput('what['.$row_num.']', $what, '35'), 'center') . "\n";
603 /* Exclude criteria */
605 _("Exclude Criteria:") .
606 addCheckBox('exclude['.$row_num.']', $exclude), 'center', '') . "\n";
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)
614 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
619 echo '<form action="search.php" name="form_asearch">' . "\n";
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);
634 echo '</table>' . "\n";
637 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
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"
644 echo '</table>' . "\n";
645 echo '</form>' . "\n";
648 /** Print the $msgs messages from $mailbox mailbox */
649 function asearch_print_mailbox_msgs($imapConnection, $mailbox, $msgs, $cnt, $sort, $color, $where, $what)
652 global $allow_server_sort, $allow_thread_sort, $thread_sort_messages;
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);
661 elseif ($allow_server_sort) {
666 $msort = calc_msort($msgs, $sort);
669 $mailbox_display = asearch_get_mailbox_display($mailbox);
671 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
672 $paginator_str = '<b><big>' . _("Folder:") . ' '. $mailbox_display . ' </big></b>';
674 echo '<br><table border="0" width="100%" cellpadding="0" cellspacing="0">';
677 mail_message_listing_beginning($imapConnection, NULL, $mailbox, $real_sort, $msg_cnt_str, $paginator_str, 1, 1);
680 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
683 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
686 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
688 printHeader($mailbox, $sort, $color, !$thread_sort_messages);
689 displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $real_sort, $color, $cnt, $where, $what);
694 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
702 * @param array $boxes mailboxes array (reference)
703 * @return array selectable unformatted mailboxes names
705 function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
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'];
713 return $mboxes_array;
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? */
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');*/
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);
736 $imap_asearch_options = array(
738 /*'ALL' is binary operator */
739 'ANSWERED' => _("Answered"),
741 'BEFORE' => _("Before"),
742 'BODY' => _("Message Body"),
744 'DELETED' => _("Deleted"),
745 'DRAFT' => _("Draft"),
746 'FLAGGED' => _("Flagged"),
747 'FROM' => _("Sent By"),
748 'HEADER' => _("Header Field"),
749 'KEYWORD' => _("Keyword"),
750 'LARGER' => _("Larger Than"),
752 /*'NOT' is unary operator */
755 /*'OR' is binary operator */
756 'RECENT' => _("Recent"),
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"),
767 /*'UNANSWERED' => '',
771 'UNKEYWORD' => _("Unkeyword"),
772 'UNSEEN' => _("Unseen"),*/
774 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
776 $imap_asearch_unops = array(
781 $imap_asearch_biops_in = array(
782 'ALL' => _("And In"),
786 $imap_asearch_biops = array(
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
806 * @global string $submit
808 if (isset($_GET['submit']))
809 $submit = strip_tags($_GET['submit']);
811 /** Searched mailboxes
812 * @global array $mailbox_array
814 if (isset($_GET['mailbox'])) {
815 $mailbox_array = $_GET['mailbox'];
816 if (!is_array($mailbox_array))
817 $mailbox_array = array($mailbox_array);
820 $mailbox_array = array();
823 * @global array $biop_array
825 if (isset($_GET['biop'])) {
826 $biop_array = $_GET['biop'];
827 if (!is_array($biop_array))
828 $biop_array = array($biop_array);
831 $biop_array = array();
834 * @global array $unop_array
836 if (isset($_GET['unop'])) {
837 $unop_array = $_GET['unop'];
838 if (!is_array($unop_array))
839 $unop_array = array($unop_array);
842 $unop_array = array();
845 * @global array $where_array
847 if (isset($_GET['where'])) {
848 $where_array = $_GET['where'];
849 if (!is_array($where_array))
850 $where_array = array($where_array);
853 $where_array = array();
856 * @global array $what_array
858 if (isset($_GET['what'])) {
859 $what_array = $_GET['what'];
860 if (!is_array($what_array))
861 $what_array = array($what_array);
864 $what_array = array();
866 /** Whether to exclude this criteria from search
867 * @global array $exclude_array
869 if (isset($_GET['exclude']))
870 $exclude_array = $_GET['exclude'];
872 $exclude_array = array();
874 /** Search within subfolders
875 * @global array $sub_array
877 if (isset($_GET['sub']))
878 $sub_array = $_GET['sub'];
880 $sub_array = array();
882 /** Row number used by recent and saved stuff
884 if (isset($_GET['rownum']))
885 $submit_rownum = strip_tags($_GET['rownum']);
887 /** Change global sort
889 if (sqgetGlobalVar('newsort', $newsort, SQ_GET
)) {
890 setPref($data_dir, $username, 'sort', $newsort);
892 sqsession_register($sort, 'sort');
893 asearch_edit_last($data_dir, $username);
896 /** Change mailbox threading
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);
903 /* end of get globals */
905 /** If TRUE, do not show search interface
906 * @global bool $search_silent
908 $search_silent = FALSE; /* Default is normal behaviour */
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);
918 if (!isset($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);
927 case 'Search_silent':
928 $search_silent = TRUE;
930 case 'Search_no_update':
931 $submit = $search_button_text;
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);
945 $exclude_array = array();
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();
957 asearch_save_recent($data_dir, $username, $submit_rownum);
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);
964 case 'edit_recent': /* no link to do this, yet */
965 asearch_edit_recent($data_dir, $username, $submit_rownum);
967 case 'forget_recent':
968 asearch_forget_recent($data_dir, $username, $submit_rownum);
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);
976 asearch_edit_saved($data_dir, $username, $submit_rownum);
979 asearch_delete_saved($data_dir, $username, $submit_rownum);
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 ;)
993 if (isset($composenew) && $composenew) {
994 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox) .
995 "&session=$composesession&attachedmessages=true&";
996 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
999 displayPageHeader($color, $mailbox);
1001 do_hook('search_before_form');
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])
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;
1014 $mailbox_array[0] = $mailbox;
1015 $biop_array[0] = '';
1016 $unop_array[0] = '';
1017 if ($mailbox == $sent_folder)
1018 $where_array[0] = 'TO';
1020 $where_array[0] = 'FROM';
1021 $what_array[0] = '';
1022 $exclude_array[0] = '';
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]);
1035 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
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;
1047 do_hook('search_after_form');
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";
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";
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)));
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";
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));
1084 $allow_thread_sort = $old_allow_thread_sort; // Restore thread sort
1088 do_hook('search_bottom');
1089 sqimap_logout($imapConnection);
1090 echo '</body></html>';