+/* 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;
+}
+
+/* print one form row */
+function asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $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 . ']">' . '<option value="All Folders"';
+ if ($mailbox == 'All Folders')
+ echo ' selected';
+ echo '>[ ' . asearch_get_mailbox_display('All Folders') . " ]</option>\n";
+ echo sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes);
+ echo '</select></td>' . "\n";
+
+/* Unary operator and Search location */
+ if (empty($where))
+ $where = 'FROM';
+ 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 */
+/* This is the original stuff. Except it doesn't work (eg commas are lost), why so much trouble?
+ $what_disp = str_replace(',', ' ', $what);
+ $what_disp = str_replace('\\\\', '\\', $what_disp);
+ $what_disp = str_replace('\\"', '"', $what_disp);
+ $what_disp = str_replace('"', '"', $what_disp);*/
+ $what_disp = htmlspecialchars($what, ENT_QUOTES);
+ 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, $add_criteria)
+{
+ 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) + $add_criteria;
+ $mailbox = '';
+ for ($row_num = 0; $row_num < $row_count; $row_num++) {
+ /* Keep the last non-empty mailbox as default choice */
+ if (strip_tags(asearch_nz($mailbox_array[$row_num])) != '')
+ $mailbox = strip_tags($mailbox_array[$row_num]);
+ if ($mailbox == '')
+ $mailbox = $boxes[0]['unformatted'];
+ $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]));
+ asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $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 */
+/* this is almost the original code */
+function asearch_print_mailbox_msgs($msgs, $mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false)
+{
+ global $sort, $color;
+
+ if ($cnt > 0) {
+ $msort = calc_msort($msgs, $sort);
+ $showbox = asearch_get_mailbox_display($mailbox);
+ echo html_tag( 'div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
+
+ $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
+ $toggle_all = get_selectall_link(1, $sort);
+
+ echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
+
+ echo '<tr><td>';
+ mail_message_listing_beginning($imapConnection, $mailbox, $sort, $msg_cnt_str, $toggle_all, 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, 6, $color, false);
+ displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $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>';
+ }
+}
+
+/* ------------------------ main ------------------------ */
+global $allow_thread_sort;
+
+/* 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');
+$saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_biop', 'asearch_saved_unop', 'asearch_saved_exclude');
+/*$asearch_keys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude');*/
+
+$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")
+);