+/**
+ * Incoming submit buttons from the message list with search results
+ */
+if (sqgetGlobalVar('moveButton', $moveButton, SQ_POST) ||
+ sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST) ||
+ sqgetGlobalVar('delete', $markDelete, SQ_POST) ||
+ sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST) ||
+ sqgetGlobalVar('markRead', $markRead, SQ_POST) ||
+ sqgetGlobalVar('markUnread', $markUnread, SQ_POST) ||
+ sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST) ||
+ sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST) ||
+ sqgetGlobalVar('attache', $attache, SQ_POST)) {
+ asearch_edit_last(1);
+ $submit = '';
+ asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
+}
+
+
+
+/** Toggle show/hide saved searches
+ */
+if (sqgetGlobalVar('search_show_saved', $search_show_saved, SQ_GET)) {
+ setPref($data_dir, $username, 'search_show_saved', $search_show_saved & 1);
+}
+/** Toggle show/hide recent searches
+ */
+if (sqgetGlobalVar('search_show_recent', $search_show_recent, SQ_GET)) {
+ setPref($data_dir, $username, 'search_show_recent', $search_show_recent & 1);
+}
+// end of get globals
+
+/** If TRUE, do not show search interface
+ * Default FALSE
+ * @global bool $search_silent
+ */
+$search_silent = FALSE;
+
+/* See how the page was called and fire off correct function */
+if ((empty($submit)) && (!empty($where_array))) {
+ /* This happens when the Enter key is used or called from outside */
+ $submit = $search_button_text;
+ /* Hack needed to handle coming back from read_body et als */
+ if (count($where_array) != count($unop_array)) {
+ /**
+ * Hack to use already existen where and what vars.
+ * where now contains the initiator page of the messagelist
+ * and in this case 'search'. what contains an index to access
+ * the search history
+ */
+
+ sqgetGlobalVar('what',$what,SQ_GET);
+ asearch_edit_last($what);
+ }
+}
+
+if (!isset($submit)) {
+ $submit = '';
+} else {
+ switch ($submit) {
+ case $search_button_text:
+ if (asearch_check_query($where_array, $what_array, $exclude_array) == '') {
+ asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
+ }
+ break;
+ case 'Search_silent':
+ $search_silent = TRUE;
+ /*nobreak;*/
+ case 'Search_no_update':
+ $submit = $search_button_text;
+ break;
+ case $del_excluded_button_text:
+ $delarray = array_keys($exclude_array);
+ while (!empty($delarray)) {
+ $delrow = array_pop($delarray);
+ array_splice($mailbox_array, $delrow, 1);
+ array_splice($biop_array, $delrow, 1);
+ array_splice($unop_array, $delrow, 1);
+ array_splice($where_array, $delrow, 1);
+ array_splice($what_array, $delrow, 1);
+ /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
+ array_splice($sub_array, $delrow, 1);
+ }
+ $exclude_array = array();
+ break;
+ case $del_all_button_text:
+ $mailbox_array = array();
+ $biop_array = array();
+ $unop_array = array();
+ $where_array = array();
+ $what_array = array();
+ $exclude_array = array();
+ $sub_array = array();
+ break;
+ case 'save_recent':
+ asearch_save_recent($submit_rownum);
+ break;
+ case 'search_recent':
+ $submit = $search_button_text;
+ asearch_edit_recent($submit_rownum);
+ asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
+ break;
+ case 'edit_recent': /* no link to do this, yet */
+ asearch_edit_recent($submit_rownum);
+ break;
+ case 'forget_recent':
+ asearch_forget_recent($submit_rownum);
+ break;
+ case 'search_saved':
+ $submit = $search_button_text;
+ asearch_edit_saved($submit_rownum);
+ asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
+ break;
+ case 'edit_saved':
+ asearch_edit_saved($submit_rownum);
+ break;
+ case 'delete_saved':
+ asearch_delete_saved($submit_rownum);
+ break;
+ }
+}
+
+//Texts in both basic and advanced form
+$imap_asearch_unops = array(
+ '' => '',
+ 'NOT' => _("Not")
+);
+
+if ($search_advanced) {
+ //Texts in advanced form only
+ $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"),*/
+ );
+
+ $imap_asearch_biops_in = array(
+ 'ALL' => _("And In"),
+ 'OR' => _("Or In")
+ );
+
+ $imap_asearch_biops = array(
+ 'ALL' => _("And"),
+ 'OR' => _("Or")
+ );
+} else {
+ //Texts in basic form only
+ $imap_asearch_options = array(
+ 'BCC' => _("Bcc"),
+ 'BODY' => _("Body"),
+ 'CC' => _("Cc"),
+ 'FROM' => _("From"),
+ 'SUBJECT' => _("Subject"),
+ 'TEXT' => _("Everywhere"),
+ 'TO' => _("To"),
+ );
+}
+
+uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
+
+/* open IMAP connection */
+$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
+
+
+/* get mailboxes once here */
+$boxes = sqimap_mailbox_list($imapConnection);
+/* ensure we have a valid default mailbox name */
+$mailbox = asearch_nz($mailbox_array[0]);
+if (($mailbox == '') || ($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
+ $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;)
+
+
+/**
+* Handle form actions like flag / unflag, seen / unseen, delete
+*/
+if (sqgetGlobalVar('mailbox',$postMailbox,SQ_POST)) {
+ if ($postMailbox) {
+ /**
+ * system wide admin settings and incoming vars.
+ */
+ $aConfig = array(
+ 'user' => $username,
+ );
+ $aConfig['setindex'] = 1; // $what $where = 'search'
+ /**
+ * Set the max cache size to the number of mailboxes to avoid cache cleanups
+ * when searching all mailboxes
+ */
+ $aConfig['max_cache_size'] = count($boxes) +1;
+
+ $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $postMailbox,$aConfig,array());
+ $sError = handleMessageListForm($imapConnection,$aMailbox);
+ /* add the mailbox to the cache */
+ $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
+
+ if ($sError) {
+ $note = $sError;
+ }
+ }
+}
+
+if (isset($aMailbox['FORWARD_SESSION'])) {
+ /* add the mailbox to the cache */
+ $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
+ sqsession_register($mailbox_cache,'mailbox_cache');
+
+ if ($compose_new_win) {
+ // write the session in order to make sure that the compose window has
+ // access to the composemessages array which is stored in the session
+ session_write_close();
+ // restart the session. Do not use sqsession_is_active because the session_id
+ // isn't empty after a session_write_close
+ sqsession_start();
+
+ if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
+ $compose_width = '640';
+ }
+ if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
+ $compose_height = '550';
+ }
+ // do not use &, it will break the query string and $session will not be detected!!!
+ $comp_uri = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
+ '&session='.$aMailbox['FORWARD_SESSION'];
+ displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri', $compose_width, $compose_height);", false);
+ } else {
+ // save mailboxstate
+ sqsession_register($aMailbox,'aLastSelectedMailbox');
+ session_write_close();
+ // we have to redirect to the compose page
+ $location = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
+ '&session='.$aMailbox['FORWARD_SESSION'];
+ header("Location: $location");
+ exit;
+ }
+} else {
+ displayPageHeader($color, $mailbox);
+// $compose_uri = $base_uri.'src/compose.php?newmessage=1';
+}
+
+if (isset($note)) {
+ echo html_tag( 'div', '<b>' . htmlspecialchars($note) .'</b>', 'center' ) . "<br />\n";
+}
+
+
+do_hook('search_before_form');
+
+if (!$search_silent) {
+ //Add a link to the other search mode if allowed
+ if ($allow_advanced_search > 1)
+ $toggle_link = ' - <small>['
+ . asearch_get_toggle_link(!$search_advanced, 'advanced', array(_("Standard search"), _("Advanced search")))
+ . ']</small>';
+ else
+ $toggle_link = '';
+
+ echo html_tag('table',
+ html_tag('tr', "\n"
+ . html_tag('td', asearch_get_title_display($color, _("Search")) . $toggle_link, 'center', $color[0])
+ ) ,
+ '', '', 'width="100%"') . "\n";
+ asearch_print_saved($boxes);
+ asearch_print_recent($boxes);
+ if (empty($where_array)) {
+ global $sent_folder;
+
+ $mailbox_array[0] = $mailbox;
+ $biop_array[0] = '';
+ $unop_array[0] = '';
+ if ($mailbox == $sent_folder) {
+ $where_array[0] = 'TO';
+ } else {
+ $where_array[0] = 'FROM';
+ }
+ $what_array[0] = '';
+ $exclude_array[0] = '';
+ $sub_array[0] = '';
+ }
+ //Display advanced or basic form
+ if ($search_advanced) {
+ if ($submit == $add_criteria_button_text) {
+ $last_index = max(count($where_array) - 1, 0);
+ $mailbox_array[] = asearch_nz($mailbox_array[$last_index]);
+ $biop_array[] = asearch_nz($biop_array[$last_index]);
+ $unop_array[] = asearch_nz($unop_array[$last_index]);
+ $where_array[] = asearch_nz($where_array[$last_index]);
+ $what_array[] = asearch_nz($what_array[$last_index]);
+ $exclude_array[] = asearch_nz($exclude_array[$last_index]);
+ $sub_array[] = asearch_nz($sub_array[$last_index]);
+ }
+ asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
+ } else {
+ asearch_print_form_basic($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
+ }
+}
+
+do_hook('search_after_form');
+
+if ($submit == $search_button_text) {
+ $msgsfound = false;
+ echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
+ echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
+ 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]));
+ echo '</table>' . "\n";
+
+ flush();
+ $iMsgCnt = 0;
+ $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
+ if ($query_error != '') {
+ echo '<br />' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
+ } else {
+ $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes);
+ /**
+ * Retrieve the search queries
+ */
+ $mboxes_mailbox = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array);
+ foreach($mboxes_mailbox as $mbx => $search) {
+
+ /**
+ * until there is no per mailbox option screen to set prefs we override
+ * the mailboxprefs by the default ones
+ */
+
+ $aMailboxPrefSer=getPref($data_dir, $username,'pref_'.$iAccount.'_'.$mbx);
+ if ($aMailboxPrefSer) {
+ $aMailboxPref = unserialize($aMailboxPrefSer);
+ $aMailboxPref[MBX_PREF_COLUMNS] = $index_order;
+ } else {
+ setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($default_mailbox_pref));
+ $aMailboxPref = $default_mailbox_pref;
+ }
+ if (isset($srt) && $targetmailbox == $mbx) {
+ $aMailboxPref[MBX_PREF_SORT] = (int) $srt;
+ }
+
+ $trash_folder = (isset($trash_folder)) ? $trash_folder : false;
+ $sent_folder = (isset($sent_folder)) ? $sent_folder : false;
+ $draft_folder = (isset($draft_folder)) ? $draft_folder : false;
+
+
+ /**
+ * until there is no per mailbox option screen to set prefs we override
+ * the mailboxprefs by the default ones
+ */
+ $aMailboxPref[MBX_PREF_LIMIT] = (int) $show_num;
+ $aMailboxPref[MBX_PREF_AUTO_EXPUNGE] = (bool) $auto_expunge;
+ $aMailboxPref[MBX_PREF_INTERNALDATE] = (bool) getPref($data_dir, $username, 'internal_date_sort');
+ $aMailboxPref[MBX_PREF_COLUMNS] = $index_order;
+
+ /**
+ * Replace From => To in case it concerns a draft or sent folder
+ */
+ if (($mbx == $sent_folder || $mbx == $draft_folder) &&
+ !in_array(SQM_COL_TO,$aMailboxPref[MBX_PREF_COLUMNS])) {
+ $aNewOrder = array(); // nice var name ;)
+ foreach($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) {
+ if ($iCol == SQM_COL_FROM) {
+ $iCol = SQM_COL_TO;
+ }
+ $aNewOrder[] = $iCol;
+ }
+ $aMailboxPref[MBX_PREF_COLUMNS] = $aNewOrder;
+ setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($aMailboxPref));
+ }
+
+ $aConfig['search'] = $search['search'];
+ $aConfig['charset'] = $search['charset'];
+
+ /**
+ * Set the max cache size to the number of mailboxes to avoid cache cleanups
+ * when searching all mailboxes
+ */
+ $aConfig['max_cache_size'] = count($mboxes_mailbox) +1;
+ if (isset($startMessage) && $targetmailbox == $mbx) {
+ $aConfig['offset'] = $startMessage;
+ } else {
+ $aConfig['offset'] = 0;
+ }
+ if (isset($showall) && $targetmailbox == $mbx) {
+ $aConfig['showall'] = $showall;
+ } else {
+ if (isset($aConfig['showall'])) {
+ unset($aConfig['showall']);
+ }
+ $showall = false;
+ }
+
+ /**
+ * Set the config options for the messages list
+ */
+ $aColumns = array();
+ foreach ($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) {
+ $aColumns[$iCol] = array();
+ switch ($iCol) {
+ case SQM_COL_SUBJ:
+ if ($truncate_subject) {
+ $aColumns[$iCol]['truncate'] = $truncate_subject;
+ }
+ break;
+ case SQM_COL_FROM:
+ case SQM_COL_TO:
+ case SQM_COL_CC:
+ case SQM_COL_BCC:
+ if ($truncate_sender) {
+ $aColumns[$iCol]['truncate'] = $truncate_sender;
+ }
+ break;
+ }
+ }
+
+
+ $aProps = array(
+ 'columns' => $aColumns,
+ 'config' => array('alt_index_colors' => $alt_index_colors,
+ 'highlight_list' => $message_highlight_list,
+ 'fancy_index_highlite' => $fancy_index_highlite,
+ 'show_flag_buttons' => (isset($show_flag_buttons)) ? $show_flag_buttons : true,
+ 'lastTargetMailbox' => (isset($lastTargetMailbox)) ? $lastTargetMailbox : '',
+ 'trash_folder' => $trash_folder,
+ 'sent_folder' => $sent_folder,
+ 'draft_folder' => $draft_folder,
+ 'enablesort' => true,
+ 'color' => $color
+ ),
+ 'mailbox' => $mbx,
+ 'account' => (isset($iAccount)) ? $iAccount : 0,
+ 'module' => 'read_body',
+ 'email' => false);
+
+
+ $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $mbx,$aConfig,$aMailboxPref);
+
+ $iError = 0;
+ $aTemplate = showMessagesForMailbox($imapConnection, $aMailbox,$aProps, $iError);
+
+ // in th future we can make use of multiple message sets, now set it to 1 for search.
+ $iSetIndex = 1;
+ if (isset($aMailbox['UIDSET'][$iSetIndex])) {
+ $iMsgCnt += count($aMailbox['UIDSET'][$iSetIndex]);
+ }
+ if ($iError) {
+ // error handling
+ } else {
+ /**
+ * In the future, move this the the initialisation area
+ */
+ sqgetGlobalVar('align',$align,SQ_SESSION);
+
+ $oTemplate = new Template($sTplDir);
+
+ if ($aMailbox['EXISTS'] > 0) {
+ if ($iError) {
+ // TODO
+ echo "ERROR occured, errorhandler will be implemented very soon";
+ } else {
+ foreach ($aTemplate as $k => $v) {
+ $oTemplate->assign($k, $v);
+ }
+
+ $oTemplate->assign('page_selector', $page_selector);
+ $oTemplate->assign('page_selector_max', $page_selector_max);
+ $oTemplate->assign('compact_paginator', $compact_paginator);
+ $oTemplate->assign('javascript_on', $javascript_on);
+ $oTemplate->assign('enablesort', (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false);
+ $oTemplate->assign('icon_theme_path', $icon_theme_path);
+ $oTemplate->assign('use_icons', (isset($use_icons)) ? $use_icons : false);
+ $oTemplate->assign('aOrder', array_keys($aColumns));
+ $oTemplate->assign('alt_index_colors', isset($alt_index_colors) ? $alt_index_colors: false);
+ $oTemplate->assign('color', $color);
+ $oTemplate->assign('align', $align);
+
+ $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']);
+ if (strtoupper($mbx) == 'INBOX') {
+ $mailbox_display = _("INBOX");
+ } else {
+ $mailbox_display = imap_utf7_decode_local($mbx);
+ }
+
+ echo '<br /><b><big>' . _("Folder:") . ' '. htmlspecialchars($mailbox_display) . ' </big></b>';
+
+ $oTemplate->display('message_list.tpl');
+ }
+ }
+ }
+
+ /* add the mailbox to the cache */
+ $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
+
+ }
+ }
+ if(!$iMsgCnt) {
+ echo '<br />' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
+ }
+}