+ }
+}
+
+/** Build an <option> statement
+*/
+function asearch_opt($val, $sel, $tit)
+{
+ return '<option value="' . $val . '"' . ($sel == $val ? ' selected="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, asearch_nz($display, $val));
+ $output .= '</select>' . "\n";
+ return $output;
+}
+
+/** Verify that a mailbox exists
+* @return bool mailbox exists
+*/
+function asearch_mailbox_exists($mailbox, &$boxes)
+{
+ foreach ($boxes as $box) {
+ if ($box['unformatted'] == $mailbox)
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/** Build the mailbox select
+*/
+function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num = 0)
+{
+ if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) {
+ $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox));
+ } else {
+ $missing = '';
+ }
+ return '<select name="mailbox[' . $row_num . ']">'
+ . $missing
+ . asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']')
+ . sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes, NULL)
+ . '</select>';
+}
+
+/** Build the Include subfolders checkbox
+*/
+function asearch_get_form_sub($sub, $row_num = 0)
+{
+ return function_exists('addCheckBox') ? addCheckBox('sub[' . $row_num .']', $sub)
+ : '<input type="checkbox" name="sub[' . $row_num .']"' . ($sub ? ' checked="checked"' : '') . ' />';
+}
+
+/** Build the 2 unop and where selects
+*/
+function asearch_get_form_location($unop, $where, $row_num = 0)
+{
+ global $imap_asearch_unops, $imap_asearch_options;
+
+ return asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
+ . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where);
+}
+
+/** Build the what text input
+*/
+function asearch_get_form_what($what, $row_num = 0)
+{
+ return function_exists('addInput') ? addInput('what[' . $row_num . ']', $what, '35')
+ : '<input type="text" size="35" name="what[' . $row_num . ']" value="' . htmlspecialchars($what) . '" />';
+}
+
+/** Build the Exclude criteria checkbox
+*/
+function asearch_get_form_exclude($exclude, $row_num = 0)
+{
+ return function_exists('addCheckBox') ? addCheckBox('exclude['.$row_num.']', $exclude)
+ : '<input type="checkbox" name="exclude[' . $row_num .']"' . ($exclude ? ' checked="checked"' : '') . ' />';
+}
+
+/** Print one advanced form row
+*/
+function asearch_print_form_row($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
+{
+ global $imap_asearch_biops_in;
+ global $color;
+
+ echo html_tag('tr', '', '', $color[4]);
+
+//Binary operator
+ echo html_tag('td', $row_num ?
+ asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop)
+ : '<b>' . _("In") . '</b>', 'center') . "\n";
+
+//Mailbox list and Include Subfolders
+ echo html_tag('td',
+ asearch_get_form_mailbox($imapConnection, $boxes, $mailbox, $row_num)
+ . _("and subfolders:") . asearch_get_form_sub($sub, $row_num), 'center') . "\n";
+
+//Unary operator and Search location
+ echo html_tag('td', asearch_get_form_location($unop, $where, $row_num), 'center') . "\n";
+
+//Text input
+ echo html_tag('td', asearch_get_form_what($what, $row_num), 'center') . "\n";
+
+//Exclude criteria
+ echo html_tag('td', _("Exclude Criteria:") . asearch_get_form_exclude($exclude, $row_num), 'center') . "\n";
+
+ echo "</tr>\n";
+}
+
+/** Print the advanced 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="' . asearch_get_href() . '" 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 one basic form row
+*/
+function asearch_print_form_row_basic($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
+{
+ global $search_button_html;
+ global $color;
+
+ echo html_tag('tr', '', '', $color[4]);
+
+//Mailbox list
+ echo html_tag('td', '<b>' . _("In") . '</b> ' . asearch_get_form_mailbox($imapConnection, $boxes, $mailbox), 'center') . "\n";
+
+//Unary operator and Search location
+ echo html_tag('td', asearch_get_form_location($unop, $where), 'center') . "\n";
+
+//Text input
+ echo html_tag('td', asearch_get_form_what($what), 'center') . "\n";
+
+//Submit button
+ echo html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n";
+
+ echo "</tr>\n";
+}
+
+/** Print the basic search form
+*/
+function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
+{
+ global $color;
+
+//Search Form
+ echo "<br />\n";
+ echo '<form action="' . asearch_get_href() . '" 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=4'));
+ $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_basic($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
+ }
+ echo '</table>' . "\n";
+ echo '</form>' . "\n";
+}
+
+/** Print the $msgs messages from $mailbox mailbox
+*/
+
+function asearch_print_mailbox_msgs($imapConnection, &$aMailbox, $color)
+{
+ /**
+ * A mailbox can contain different sets with uid's. Default, for normal
+ * message list view we use '0' as setindex and for search a different
+ * setindex.
+ */
+ $iSetIndx = $aMailbox['SETINDEX'];
+
+ $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']);
+ $mailbox_title = '<b><big>' . _("Folder:") . ' '. $mailbox_display . ' </big></b>';
+
+ /**
+ * UIDSET contains the array with uid's returned by a search
+ */
+ $cnt = count($aMailbox['UIDSET'][$iSetIndx]);
+
+ $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $cnt : $aMailbox['LIMIT'];
+ $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ?
+ $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $cnt;
+
+ $paginator_str = get_paginator_str($aMailbox['NAME'], $aMailbox['PAGEOFFSET'],
+ $cnt, $aMailbox['LIMIT'], $aMailbox['SHOWALL'][$iSetIndx]);
+
+ $msg_cnt_str = get_msgcnt_str($aMailbox['PAGEOFFSET'], $iEnd,$cnt);
+
+ echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
+
+ echo '<tr><td>';
+ mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $mailbox_title . " $paginator_str");
+ 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($aMailbox);
+ displayMessageArray($imapConnection, $aMailbox);
+ echo ' </td></tr>';
+ echo ' </table>';
+ echo ' </td></tr>';
+ echo ' </table>';
+ mail_message_listing_end($cnt, '', $msg_cnt_str);
+ 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();