+/** Handle the alternate row colors */
+function asearch_get_row_color($color, $row_num)
+{
+/*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/
+ $color_string = $color[4];
+ if ($GLOBALS['alt_index_colors']) {
+ if (($row_num % 2) == 0) {
+ if (!isset($color[12]))
+ $color[12] = '#EAEAEA';
+ $color_string = $color[12];
+ }
+ }
+ return $color_string;
+}
+
+/** Print a whole query array, recent or saved */
+function asearch_print_query_array($query_array, $query_keys, $action_array, $title)
+{
+ global $color;
+
+ echo "<br>\n";
+ echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"');
+ echo html_tag('tr', html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=5'));
+ $main_key = $query_keys[0];
+ $query_count = count($query_array[$main_key]);
+ for ($query_num=0, $row_num=0; $query_num<$query_count; $query_num++) {
+ if (!empty($query_array[$main_key][$query_num])) {
+ echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num));
+
+ unset($search_array);
+ foreach ($query_keys as $query_key) {
+ $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
+ }
+ $mailbox_array = $search_array[1];
+ $biop_array = $search_array[3];
+ $unop_array = $search_array[4];
+ $where_array = $search_array[0];
+ $what_array = $search_array[2];
+ $exclude_array = $search_array[5];
+ $sub_array = $search_array[6];
+ $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
+
+ echo html_tag('td', $query_num+1, 'right');
+ echo html_tag('td', $query_display, 'center', '', 'width="80%"');
+ foreach ($action_array as $action => $action_display) {
+ echo html_tag('td', '<a href=search.php?submit=' . $action . '&rownum=' . $query_num . '>' . $action_display . '</a>', 'center');
+ }
+
+ echo '</tr>' . "\n";
+ $row_num++;
+ }
+ }
+ echo '</table>' . "\n";
+}
+
+/** Print the saved array */
+function asearch_print_saved($data_dir, $username)
+{
+ global $saved_prefkeys;
+
+ $saved_array = asearch_read_saved($data_dir, $username);
+ if (isset($saved_array[$saved_prefkeys[0]])) {
+ $saved_count = count($saved_array[$saved_prefkeys[0]]);
+ if ($saved_count > 0) {
+ $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete"));
+ asearch_print_query_array($saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"));
+ }
+ }
+}
+
+/** Print the recent array */
+function asearch_print_recent($data_dir, $username)
+{
+ global $recent_prefkeys;
+
+ $recent_array = asearch_read_recent($data_dir, $username);
+ if (isset($recent_array[$recent_prefkeys[0]])) {
+ $recent_count = count($recent_array[$recent_prefkeys[0]]);
+ $recent_max = min($recent_count, getPref($data_dir, $username, 'search_memory', 0));
+ if ($recent_max > 0) {
+ $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget"));
+ asearch_print_query_array($recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"));
+ }
+ }
+}
+
+/** Build an <option> statement */
+function asearch_opt($val, $sel, $tit)
+{
+ return '<option value="' . $val . '"' . ($sel == $val ? ' selected' : '') . '>' . $tit . '</option>' . "\n";
+}
+
+/** Build a <select> statement from an array */
+function asearch_opt_array($var_name, $opt_array, $cur_val)
+{
+ $output = '<select name="' . $var_name . '">' . "\n";
+ foreach($opt_array as $val => $display)
+ $output .= asearch_opt($val, $cur_val, $display);
+ $output .= '</select>' . "\n";
+ return $output;
+}
+
+/** Verify that a mailbox exists */
+function asearch_mailbox_exists($mailbox, $boxes)
+{
+ foreach ($boxes as $box) {
+ if ($box['unformatted'] == $mailbox)
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/** Print one form row */
+function asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
+{
+ global $imap_asearch_biops_in, $imap_asearch_unops, $imap_asearch_options;
+ global $color;
+
+ echo html_tag('tr', '', '', $color[4]);
+
+ echo html_tag('td', '', 'center');
+/* Binary operator */
+ if ($row_num)
+ echo asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop);
+ else
+ echo /*'<input type="hidden" name="biop[0]" value="">' .*/ '<b>' . _("In") . '</b>';
+ echo "</td>\n";
+
+ echo html_tag('td', '', 'center');
+/* Mailbox list */
+ echo '<select name="mailbox[' . $row_num . ']">';
+ if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes)))
+ echo asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox));
+ echo asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']');
+ echo sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes, NULL);
+ echo '</select>' . "\n";
+
+/* Include Subfolders */
+ echo _("and subfolders:") . '<input type=checkbox name="sub[' . $row_num .']"' . ($sub ? ' CHECKED' : '') . '></td>' . "\n";
+
+/* Unary operator and Search location */
+ echo html_tag('td',
+ asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
+ . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where),
+ 'center');
+
+/* Text input */
+ $what_disp = htmlspecialchars($what);
+ echo html_tag('td', '<input type="text" size="35" name="what[' . $row_num . ']" value="' . $what_disp . '">', 'center') . "\n";
+
+/* Exclude criteria */
+ echo html_tag('td',
+ _("Exclude Criteria:") . '<input type=checkbox name="exclude[' . $row_num .']"' . ($exclude ? ' CHECKED' : '') . '>', 'center', '') . "\n";
+
+ echo "</tr>\n";
+}
+
+/** Print the search form */
+function asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
+{
+ global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
+ global $color;
+
+ /* Search Form */
+ echo "<br>\n";
+ echo '<form action="search.php" name="form_asearch">' . "\n";
+
+ echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
+ echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
+ $row_count = count($where_array);
+ for ($row_num = 0; $row_num < $row_count; $row_num++) {
+ $mailbox = asearch_nz($mailbox_array[$row_num]);
+ $biop = strip_tags(asearch_nz($biop_array[$row_num]));
+ $unop = strip_tags(asearch_nz($unop_array[$row_num]));
+ $where = strip_tags(asearch_nz($where_array[$row_num]));
+ $what = asearch_nz($what_array[$row_num]);
+ $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
+ $sub = strip_tags(asearch_nz($sub_array[$row_num]));
+ asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
+ }
+ echo '</table>' . "\n";
+
+/* Submit buttons */
+ echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
+ echo html_tag('tr',
+ html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n"
+ . html_tag('td', getButton('SUBMIT', 'submit', $add_criteria_button_html), 'center') . "\n"
+ . html_tag('td', getButton('SUBMIT', 'submit', $del_all_button_html), 'center') . "\n"
+ . html_tag('td', getButton('SUBMIT', 'submit', $del_excluded_button_html), 'center') . "\n"
+ );
+ echo '</table>' . "\n";
+ echo '</form>' . "\n";
+}
+
+/** Print the $msgs messages from $mailbox mailbox */
+function asearch_print_mailbox_msgs($imapConnection, $mailbox, $msgs, $cnt, $sort, $color, $where, $what)
+{
+ if ($cnt > 0) {
+ global $allow_server_sort, $allow_thread_sort, $thread_sort_messages;
+
+ $thread_sort_messages = 0;
+ if ($allow_thread_sort) {
+ global $data_dir, $username;
+ $thread_sort_messages = getPref($data_dir, $username, 'thread_' . $mailbox);
+ $msort = $msgs;
+ $real_sort = 6;
+ }
+ elseif ($allow_server_sort) {
+ $msort = $msgs;
+ $real_sort = 6;
+ }
+ else {
+ $msort = calc_msort($msgs, $sort);
+ $real_sort = $sort;
+ }
+ $mailbox_display = asearch_get_mailbox_display($mailbox);
+
+ $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
+ $paginator_str = '<b><big>' . _("Folder:") . ' '. $mailbox_display . '</big></b>';
+
+ echo '<br><table border="0" width="100%" cellpadding="0" cellspacing="0">';
+
+ echo '<tr><td>';
+ mail_message_listing_beginning($imapConnection, NULL, $mailbox, $real_sort, $msg_cnt_str, $paginator_str, 1, 1);
+ echo '</td></tr>';
+
+ echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
+
+ echo '<tr><td>';
+ echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
+ echo ' <tr><td>';
+
+ echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
+ echo ' <tr><td>';
+ printHeader($mailbox, $sort, $color, !$thread_sort_messages);
+ displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $real_sort, $color, $cnt, $where, $what);
+ echo ' </td></tr>';
+ echo ' </table>';
+ echo ' </td></tr>';
+ echo ' </table>';
+ mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
+ echo '</td></tr>';
+
+ echo '</table>';
+ }
+}
+
+/**
+ * @param array $boxes mailboxes array (reference)
+ * @return array selectable unformatted mailboxes names
+ */
+function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
+{
+ $mboxes_array = array();
+ $boxcount = count($boxes);
+ for ($boxnum=0; $boxnum<$boxcount; $boxnum++) {
+ if (!in_array('noselect', $boxes[$boxnum]['flags']))
+ $mboxes_array[] = $boxes[$boxnum]['unformatted'];
+ }
+ return $mboxes_array;
+}
+
+/* ------------------------ main ------------------------ */
+/* get globals we may need */
+sqgetGlobalVar('username', $username, SQ_SESSION);
+sqgetGlobalVar('key', $key, SQ_COOKIE);
+sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); /* we really need this? */
+sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION); /* do we really need this? */
+
+$recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_biop', 'asearch_recent_unop', 'asearch_recent_exclude', 'asearch_recent_sub');
+$saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_biop', 'asearch_saved_unop', 'asearch_saved_exclude', 'asearch_saved_sub');
+/*$asearch_keys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/
+
+$search_button_html = _("Search");
+$search_button_text = asearch_unhtmlentities($search_button_html);
+$add_criteria_button_html = _("Add New Criteria");
+$add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
+$del_excluded_button_html = _("Remove Excluded Criteria");
+$del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
+$del_all_button_html = _("Remove All Criteria");
+$del_all_button_text = asearch_unhtmlentities($del_all_button_html);
+
+$imap_asearch_options = array(
+/* <message set>, */
+/*'ALL' is binary operator */
+ 'ANSWERED' => _("Answered"),
+ 'BCC' => _("Bcc"),
+ 'BEFORE' => _("Before"),
+ 'BODY' => _("Message Body"),
+ 'CC' => _("CC"),
+ 'DELETED' => _("Deleted"),
+ 'DRAFT' => _("Draft"),
+ 'FLAGGED' => _("Flagged"),
+ 'FROM' => _("Sent By"),
+ 'HEADER' => _("Header Field"),
+ 'KEYWORD' => _("Keyword"),
+ 'LARGER' => _("Larger Than"),
+ 'NEW' => _("New"),
+/*'NOT' is unary operator */
+ 'OLD' => _("Old"),
+ 'ON' => _("On"),
+/*'OR' is binary operator */
+ 'RECENT' => _("Recent"),
+ 'SEEN' => _("Seen"),
+ 'SENTBEFORE' => _("Sent Before"),
+ 'SENTON' => _("Sent On"),
+ 'SENTSINCE' => _("Sent Since"),
+ 'SINCE' => _("Since"),
+ 'SMALLER' => _("Smaller Than"),
+ 'SUBJECT' => _("Subject Contains"),
+ 'TEXT' => _("Header and Body"),
+ 'TO' => _("Sent To"),
+/*'UID' => 'anum',*/
+/*'UNANSWERED' => '',
+ 'UNDELETED' => '',
+ 'UNDRAFT' => '',
+ 'UNFLAGGED' => '',
+ 'UNKEYWORD' => _("Unkeyword"),
+ 'UNSEEN' => _("Unseen"),*/
+);
+uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
+
+$imap_asearch_unops = array(
+ '' => '',
+ 'NOT' => _("Not")
+);
+
+$imap_asearch_biops_in = array(
+ 'ALL' => _("And In"),
+ 'OR' => _("Or In")
+);
+
+$imap_asearch_biops = array(
+ 'ALL' => _("And"),
+ 'OR' => _("Or")
+);
+
+/** How we did enter the form
+ * - unset : Enter key, or called from outside (eg read_body)
+ * - $search_button_text : Search button
+ * - 'Search_no_update' : Search but don't update recent
+ * - 'Search_last' : Same as no_update but reload and search last
+ * - 'Search_silent' : Same as no_update but only display results
+ * - $add_criteria_button_text : Add New Criteria button
+ * - $del_excluded_button_text : Remove Excluded Criteria button
+ * - $del_all_button_text : Remove All Criteria button
+ * - 'save_recent'
+ * - 'search_recent'
+ * - 'forget_recent'
+ * - 'edit_saved'
+ * - 'search_saved'
+ * - 'delete_saved'
+ * @global string $submit
+ */
+if (isset($_GET['submit']))
+ $submit = strip_tags($_GET['submit']);
+
+/** Searched mailboxes
+ * @global array $mailbox_array
+ */
+if (isset($_GET['mailbox'])) {
+ $mailbox_array = $_GET['mailbox'];
+ if (!is_array($mailbox_array))
+ $mailbox_array = array($mailbox_array);
+}
+else
+ $mailbox_array = array();
+
+/** Binary operators
+ * @global array $biop_array
+ */
+if (isset($_GET['biop'])) {
+ $biop_array = $_GET['biop'];
+ if (!is_array($biop_array))
+ $biop_array = array($biop_array);
+}
+else
+ $biop_array = array();
+
+/** Unary operators
+ * @global array $unop_array
+ */
+if (isset($_GET['unop'])) {
+ $unop_array = $_GET['unop'];
+ if (!is_array($unop_array))
+ $unop_array = array($unop_array);
+}
+else
+ $unop_array = array();
+
+/** Where to search
+ * @global array $where_array
+ */
+if (isset($_GET['where'])) {
+ $where_array = $_GET['where'];
+ if (!is_array($where_array))
+ $where_array = array($where_array);
+}
+else
+ $where_array = array();
+
+/** What to search
+ * @global array $what_array
+ */
+if (isset($_GET['what'])) {
+ $what_array = $_GET['what'];
+ if (!is_array($what_array))
+ $what_array = array($what_array);
+}
+else
+ $what_array = array();
+
+/** Whether to exclude this criteria from search
+ * @global array $exclude_array
+ */
+if (isset($_GET['exclude']))
+ $exclude_array = $_GET['exclude'];
+else
+ $exclude_array = array();
+
+/** Search within subfolders
+ * @global array $sub_array
+ */
+if (isset($_GET['sub']))
+ $sub_array = $_GET['sub'];
+else
+ $sub_array = array();
+
+/** Row number used by recent and saved stuff
+ */
+if (isset($_GET['rownum']))
+ $submit_rownum = strip_tags($_GET['rownum']);
+
+/** Change global sort
+ */
+if (sqgetGlobalVar('newsort', $newsort, SQ_GET)) {
+ setPref($data_dir, $username, 'sort', $newsort);
+ $sort = $newsort;
+ sqsession_register($sort, 'sort');
+ asearch_edit_last($data_dir, $username);
+}
+
+/** Change mailbox threading
+ */
+if (sqgetGlobalVar('set_thread', $set_thread, SQ_GET)) {
+ setPref($data_dir, $username, 'thread_' . $mailbox_array[0], ($set_thread == 1) ? 1 : 0 );
+ asearch_edit_last($data_dir, $username);
+}
+
+/* end of get globals */
+
+/** If TRUE, do not show search interface
+ * @global bool $search_silent
+ */
+$search_silent = FALSE; /* Default is normal behaviour */
+
+/* See how the page was called and fire off correct function */
+if ((!isset($submit) || empty($submit)) && !empty($where_array)) {
+ /* This happens when the Enter key is used or called from outside */
+ $submit = $search_button_text;
+ if (count($where_array) != count($unop_array)) /* Hack needed to handle coming back from read_body et als */
+ asearch_edit_last($data_dir, $username);
+}
+
+if (!isset($submit)) {
+ $submit = '';
+}