flush(); $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) { $aMailboxPrefSer = getPref($data_dir, $username, "pref_{$mbx}"); if ($aMailboxPrefSer) { $aMailboxPref = unserialize($aMailboxPrefSer); } else { setUserPref($username, "pref_{$mbx}", serialize($aMailboxGlobalPref)); $aMailboxPref = $aMailboxGlobalPref; } if (isset($srt) && $targetmailbox == $mbx) { $aMailboxPref[MBX_PREF_SORT] = (int) $srt; } if (isset($startMessage) && $targetmailbox == $mbx) { $aConfig['offset'] = $startMessage; } else { if (isset($aConfig['offset'])) { unset($aConfig['offset']); } } if (isset($showall) && $targetmailbox == $mbx) { $aConfig['showall'] = $showall; } else {
/** * Selects a mailbox for header retrieval. * Cache control for message headers is embedded. * * @param resource $imapConnection imap socket handle * @param string $mailbox mailbox to select and retrieve message headers from * @param array $aConfig array with system config settings and incoming vars * @param array $aProps mailbox specific properties * * @return array $aMailbox mailbox array with all relevant information * * @since 1.5.1 * @author Marc Groot Koerkamp */ function sqm_api_mailbox_select($imapConnection, $account, $mailbox, $aConfig, $aProps) { /** * NB: retrieve this from the session before accessing this function * and make sure you write it back at the end of the script after * the aMailbox var is added so that the headers are added to the cache */ global $mailbox_cache; $aDefaultConfigProps = array('user' => false, 'setindex' => 0, 'max_cache_size' => SQM_MAX_MBX_IN_CACHE); $aConfig = array_merge($aDefaultConfigProps, $aConfig); $iSetIndx = $aConfig['setindex']; $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox); if ($mailbox_cache) { if (isset($mailbox_cache[$account . '_' . $mailbox])) { $aCachedMailbox = $mailbox_cache[$account . '_' . $mailbox]; } else { $aCachedMailbox = false; } /* cleanup cache */ if (count($mailbox_cache) > $aConfig['max_cache_size'] - 1) { $aTime = array(); foreach ($mailbox_cache as $cachedmailbox => $aVal) { $aTime[$aVal['TIMESTAMP']] = $cachedmailbox; } if (ksort($aTime, SORT_NUMERIC)) { for ($i = 0, $iCnt = count($mailbox_cache); $i < $iCnt - $aConfig['max_cache_size']; ++$i) { $sOldestMbx = array_shift($aTime); /** * Remove only the UIDSET and MSG_HEADERS from cache because those can * contain large amounts of data. */ if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) { $mailbox_cache[$sOldestMbx]['UIDSET'] = false; } if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) { $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false; } } } } } else { $aCachedMailbox = false; } /** * Deal with imap servers that do not return the required UIDNEXT or * UIDVALIDITY response * from a SELECT call (since rfc 3501 it's required). */ if (!isset($aMbxResponse['UIDNEXT']) || !isset($aMbxResponse['UIDVALIDITY'])) { $aStatus = sqimap_status_messages($imapConnection, $mailbox, array('UIDNEXT', 'UIDVALIDITY')); $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT']; $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY']; } $aMailbox['ACCOUNT'] = $account; $aMailbox['UIDSET'][$iSetIndx] = false; $aMailbox['ID'] = false; $aMailbox['SETINDEX'] = $iSetIndx; $aMailbox['MSG_HEADERS'] = false; if ($aCachedMailbox) { /** * Validate integrity of cached data */ if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] && $aMbxResponse['EXISTS'] && $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] && $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] && isset($aCachedMailbox['SEARCH'][$iSetIndx]) && (!isset($aConfig['search']) || $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) { if (isset($aCachedMailbox['MSG_HEADERS'])) { $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS']; } $aMailbox['ID'] = $aCachedMailbox['ID']; if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) { if (isset($aProps[MBX_PREF_SORT]) && $aProps[MBX_PREF_SORT] != $aCachedMailbox['SORT']) { $newsort = $aProps[MBX_PREF_SORT]; $oldsort = $aCachedMailbox['SORT']; /** * If it concerns a reverse sort we do not need to invalidate * the cached sorted UIDSET, a reverse is sufficient. */ if ($newsort % 2 && $newsort + 1 == $oldsort || !($newsort % 2) && $newsort - 1 == $oldsort) { $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]); } else { $server_sort_array = false; $aMailbox['MSG_HEADERS'] = false; $aMailbox['ID'] = false; } // store the new sort value in the mailbox pref if ($aConfig['user']) { // FIXME, in ideal situation, we write back the // prefs at the end of the script setUserPref($aConfig['user'], 'pref_' . $account . '_' . $mailbox, serialize($aProps)); } } else { $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx]; } } } } /** * Restore the offset in the paginator if no new offset is provided. */ if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) { $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET']; $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET']; } else { $aMailbox['OFFSET'] = isset($aConfig['offset']) && $aConfig['offset'] ? $aConfig['offset'] - 1 : 0; $aMailbox['PAGEOFFSET'] = isset($aConfig['offset']) && $aConfig['offset'] ? $aConfig['offset'] : 1; } /** * Restore the number of messages in the result set */ if (isset($aCachedMailbox['TOTAL'][$iSetIndx]) && $aCachedMailbox['TOTAL'][$iSetIndx]) { $aMailbox['TOTAL'][$iSetIndx] = $aCachedMailbox['TOTAL'][$iSetIndx]; } /** * Restore the showall value no new showall value is provided. */ if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) && isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) { $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx]; } else { $aMailbox['SHOWALL'][$iSetIndx] = isset($aConfig['showall']) && $aConfig['showall'] ? 1 : 0; } /** * Restore the sort order if no new sort order is provided. */ if (!isset($aProps[MBX_PREF_SORT]) && isset($aCachedMailbox['SORT'])) { $aMailbox['SORT'] = $aCachedMailbox['SORT']; } else { $aMailbox['SORT'] = isset($aProps[MBX_PREF_SORT]) ? $aProps[MBX_PREF_SORT] : 0; } /** * Restore the number of message to show per page when no new limit is provided */ if (!isset($aProps[MBX_PREF_LIMIT]) && isset($aCachedMailbox['LIMIT'])) { $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT']; } else { $aMailbox['LIMIT'] = isset($aProps[MBX_PREF_LIMIT]) ? $aProps[MBX_PREF_LIMIT] : 15; } /** * Restore the ordered columns to show when no new ordered columns are provided */ if (!isset($aProps[MBX_PREF_COLUMNS]) && isset($aCachedMailbox['COLUMNS'])) { $aMailbox['COLUMNS'] = $aCachedMailbox['COLUMNS']; } else { $aMailbox['COLUMNS'] = isset($aProps[MBX_PREF_COLUMNS]) ? $aProps[MBX_PREF_COLUMNS] : array(SQM_COL_FLAGS, SQM_COL_FROM, SQM_COL_SUBJ, SQM_COL_FLAGS); } /** * Restore the headers we fetch the last time. Saves intitialisation stuff in read_body. */ $aMailbox['FETCHHEADERS'] = isset($aCachedMailbox['FETCHHEADERS']) ? $aCachedMailbox['FETCHHEADERS'] : null; if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) { $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE']; } else { $aMailbox['AUTO_EXPUNGE'] = isset($aProps[MBX_PREF_AUTO_EXPUNGE]) ? $aProps[MBX_PREF_AUTO_EXPUNGE] : false; } if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) { $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx]; } else { if (isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx]) && $aConfig['search'] != $aCachedMailbox['SEARCH'][$iSetIndx]) { // reset the pageindex $aMailbox['SEARCH'][$iSetIndx] = $aConfig['search']; $aMailbox['OFFSET'] = 0; $aMailbox['PAGEOFFSET'] = 1; } else { $aMailbox['SEARCH'][$iSetIndx] = isset($aConfig['search']) ? $aConfig['search'] : 'ALL'; } } if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) { $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx]; } else { $aMailbox['CHARSET'][$iSetIndx] = isset($aConfig['charset']) ? $aConfig['charset'] : 'US-ASCII'; } $aMailbox['NAME'] = $mailbox; $aMailbox['EXISTS'] = $aMbxResponse['EXISTS']; $aMailbox['SEEN'] = isset($aMbxResponse['SEEN']) ? $aMbxResponse['SEEN'] : $aMbxResponse['EXISTS']; $aMailbox['RECENT'] = isset($aMbxResponse['RECENT']) ? $aMbxResponse['RECENT'] : 0; $aMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY']; $aMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT']; $aMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS']; $aMailbox['RIGHTS'] = $aMbxResponse['RIGHTS']; /* decide if we are thread sorting or not */ if ($aMailbox['SORT'] & SQSORT_THREAD) { if (!sqimap_capability($imapConnection, 'THREAD')) { $aMailbox['SORT'] ^= SQSORT_THREAD; } else { $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT']; } } else { $aMailbox['THREAD_INDENT'] = false; } /* set a timestamp for cachecontrol */ $aMailbox['TIMESTAMP'] = time(); return $aMailbox; }
/** * Selects a mailbox for header retrieval. * Cache control for message headers is embedded. * * @param resource $imapConnection imap socket handle * @param string $mailbox mailbox to select and retrieve message headers from * @param array $aConfig array with system config settings and incoming vars * @param array $aProps mailbox specific properties * @return array $aMailbox mailbox array with all relevant information * @author Marc Groot Koerkamp */ function sqm_api_mailbox_select($imapConnection, $mailbox, $aConfig, $aProps) { /** * NB: retrieve this from the session before accessing this function * and make sure you write it back at the end of the script after * the aMailbox var is added so that the headers are added to the cache */ global $mailbox_cache; /** * In case the properties arrays are empty set the defaults. */ $aDefaultMbxPref = array(); // MBX_PREF_SORT => 0, // MBX_PREF_LIMIT => 15, // MBX_PREF_AUTO_EXPUNGE => 0, // MBX_PREF_INTERNALDATE => 0 // ); /* array_merge doesn't work with integers as keys */ // foreach ($aDefaultMbxPref as $key => $value) { // if (!isset($aProps[$key])) { // $aProps[$key] = $value; // } // } $aDefaultConfigProps = array('allow_server_sort' => sqimap_capability($imapConnection, 'SORT'), 'user' => false, 'setindex' => 0, 'max_cache_size' => SQM_MAX_MBX_IN_CACHE); $aConfig = array_merge($aDefaultConfigProps, $aConfig); $iSetIndx = $aConfig['setindex']; $aMbxResponse = sqimap_mailbox_select($imapConnection, $mailbox); if ($mailbox_cache) { if (isset($mailbox_cache[$mailbox])) { $aCachedMailbox = $mailbox_cache[$mailbox]; } else { $aCachedMailbox = false; } /* cleanup cache */ if (count($mailbox_cache) > $aConfig['max_cache_size'] - 1) { $aTime = array(); foreach ($mailbox_cache as $cachedmailbox => $aVal) { $aTime[$aVal['TIMESTAMP']] = $cachedmailbox; } if (ksort($aTime, SORT_NUMERIC)) { for ($i = 0, $iCnt = count($mailbox_cache); $i < $iCnt - $aConfig['max_cache_size']; ++$i) { $sOldestMbx = array_shift($aTime); /** * Remove only the UIDSET and MSG_HEADERS from cache because those can * contain large amounts of data. */ if (isset($mailbox_cache[$sOldestMbx]['UIDSET'])) { $mailbox_cache[$sOldestMbx]['UIDSET'] = false; } if (isset($mailbox_cache[$sOldestMbx]['MSG_HEADERS'])) { $mailbox_cache[$sOldestMbx]['MSG_HEADERS'] = false; } } } } } else { $aCachedMailbox = false; } /** * Deal with imap servers that do not return the required UIDNEXT or * UIDVALIDITY response * from a SELECT call (since rfc 3501 it's required). */ if (!isset($aMbxResponse['UIDNEXT']) || !isset($aMbxResponse['UIDVALIDITY'])) { $aStatus = sqimap_status_messages($imapConnection, $mailbox, array('UIDNEXT', 'UIDVALIDITY')); $aMbxResponse['UIDNEXT'] = $aStatus['UIDNEXT']; $aMbxResponse['UIDVALIDTY'] = $aStatus['UIDVALIDITY']; } $aMailbox['UIDSET'][$iSetIndx] = false; $aMailbox['ID'] = false; $aMailbox['SETINDEX'] = $iSetIndx; if ($aCachedMailbox) { /** * Validate integrity of cached data */ if ($aCachedMailbox['EXISTS'] == $aMbxResponse['EXISTS'] && $aMbxResponse['EXISTS'] && $aCachedMailbox['UIDVALIDITY'] == $aMbxResponse['UIDVALIDITY'] && $aCachedMailbox['UIDNEXT'] == $aMbxResponse['UIDNEXT'] && isset($aCachedMailbox['SEARCH'][$iSetIndx]) && (!isset($aConfig['search']) || $aCachedMailbox['SEARCH'][$iSetIndx] == $aConfig['search'])) { if (isset($aCachedMailbox['MSG_HEADERS'])) { $aMailbox['MSG_HEADERS'] = $aCachedMailbox['MSG_HEADERS']; } $aMailbox['ID'] = $aCachedMailbox['ID']; if (isset($aCachedMailbox['UIDSET'][$iSetIndx]) && $aCachedMailbox['UIDSET'][$iSetIndx]) { if (isset($aProps[MBX_PREF_SORT]) && $aProps[MBX_PREF_SORT] != $aCachedMailbox['SORT']) { $newsort = $aProps[MBX_PREF_SORT]; $oldsort = $aCachedMailbox['SORT']; /** * If it concerns a reverse sort we do not need to invalidate * the cached sorted UIDSET, a reverse is sufficient. */ if ($newsort % 2 && $newsort + 1 == $oldsort || !($newsort % 2) && $newsort - 1 == $oldsort) { $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]); } else { $server_sort_array = false; $aMailbox['MSG_HEADERS'] = false; $aMailbox['ID'] = false; } // store the new sort value in the mailbox pref if ($aConfig['user']) { // FIXME, in ideal situation, we write back the // prefs at the end of the script setUserPref($aConfig['user'], "pref_{$mailbox}", serialize($aProps)); } } else { $aMailbox['UIDSET'][$iSetIndx] = $aCachedMailbox['UIDSET'][$iSetIndx]; } } } } /** * Restore the offset in the paginator if no new offset is provided. */ if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['offset']) && $aCachedMailbox['OFFSET']) { $aMailbox['OFFSET'] = $aCachedMailbox['OFFSET']; $aMailbox['PAGEOFFSET'] = $aCachedMailbox['PAGEOFFSET']; } else { $aMailbox['OFFSET'] = isset($aConfig['offset']) && $aConfig['offset'] ? $aConfig['offset'] - 1 : 0; $aMailbox['PAGEOFFSET'] = isset($aConfig['offset']) && $aConfig['offset'] ? $aConfig['offset'] : 1; } /** * Restore the showall value no new showall value is provided. */ if (isset($aMailbox['UIDSET'][$iSetIndx]) && !isset($aConfig['showall']) && isset($aCachedMailbox['SHOWALL'][$iSetIndx]) && $aCachedMailbox['SHOWALL'][$iSetIndx]) { $aMailbox['SHOWALL'][$iSetIndx] = $aCachedMailbox['SHOWALL'][$iSetIndx]; } else { $aMailbox['SHOWALL'][$iSetIndx] = isset($aConfig['showall']) && $aConfig['showall'] ? 1 : 0; } if (!isset($aProps[MBX_PREF_SORT]) && isset($aCachedMailbox['SORT'])) { $aMailbox['SORT'] = $aCachedMailbox['SORT']; } else { $aMailbox['SORT'] = isset($aProps[MBX_PREF_SORT]) ? $aProps[MBX_PREF_SORT] : 0; } if (!isset($aProps[MBX_PREF_LIMIT]) && isset($aCachedMailbox['LIMIT'])) { $aMailbox['LIMIT'] = $aCachedMailbox['LIMIT']; } else { $aMailbox['LIMIT'] = isset($aProps[MBX_PREF_LIMIT]) ? $aProps[MBX_PREF_LIMIT] : 15; } if (!isset($aProps[MBX_PREF_INTERNALDATE]) && isset($aCachedMailbox['INTERNALDATE'])) { $aMailbox['INTERNALDATE'] = $aCachedMailbox['INTERNALDATE']; } else { $aMailbox['INTERNALDATE'] = isset($aProps[MBX_PREF_INTERNALDATE]) ? $aProps[MBX_PREF_INTERNALDATE] : false; } if (!isset($aProps[MBX_PREF_AUTO_EXPUNGE]) && isset($aCachedMailbox['AUTO_EXPUNGE'])) { $aMailbox['AUTO_EXPUNGE'] = $aCachedMailbox['AUTO_EXPUNGE']; } else { $aMailbox['AUTO_EXPUNGE'] = isset($aProps[MBX_PREF_AUTO_EXPUNGE]) ? $aProps[MBX_PREF_AUTO_EXPUNGE] : false; } if (!isset($aConfig['allow_thread_sort']) && isset($aCachedMailbox['ALLOW_THREAD'])) { $aMailbox['ALLOW_THREAD'] = $aCachedMailbox['ALLOW_THREAD']; } else { $aMailbox['ALLOW_THREAD'] = isset($aConfig['allow_thread_sort']) ? $aConfig['allow_thread_sort'] : false; } if (!isset($aConfig['search']) && isset($aCachedMailbox['SEARCH'][$iSetIndx])) { $aMailbox['SEARCH'][$iSetIndx] = $aCachedMailbox['SEARCH'][$iSetIndx]; } else { $aMailbox['SEARCH'][$iSetIndx] = isset($aConfig['search']) ? $aConfig['search'] : 'ALL'; } if (!isset($aConfig['charset']) && isset($aCachedMailbox['CHARSET'][$iSetIndx])) { $aMailbox['CHARSET'][$iSetIndx] = $aCachedMailbox['CHARSET'][$iSetIndx]; } else { $aMailbox['CHARSET'][$iSetIndx] = isset($aConfig['charset']) ? $aConfig['charset'] : 'US-ASCII'; } $aMailbox['NAME'] = $mailbox; $aMailbox['EXISTS'] = $aMbxResponse['EXISTS']; $aMailbox['SEEN'] = isset($aMbxResponse['SEEN']) ? $aMbxResponse['SEEN'] : $aMbxResponse['EXISTS']; $aMailbox['RECENT'] = isset($aMbxResponse['RECENT']) ? $aMbxResponse['RECENT'] : 0; $aMailbox['UIDVALIDITY'] = $aMbxResponse['UIDVALIDITY']; $aMailbox['UIDNEXT'] = $aMbxResponse['UIDNEXT']; $aMailbox['PERMANENTFLAGS'] = $aMbxResponse['PERMANENTFLAGS']; $aMailbox['RIGHTS'] = $aMbxResponse['RIGHTS']; /* decide if we are thread sorting or not */ if (!$aMailbox['ALLOW_THREAD']) { if ($aMailbox['SORT'] & SQSORT_THREAD) { $aMailbox['SORT'] -= SQSORT_THREAD; } } if ($aMailbox['SORT'] & SQSORT_THREAD) { $aMailbox['SORT_METHOD'] = 'THREAD'; $aMailbox['THREAD_INDENT'] = $aCachedMailbox['THREAD_INDENT']; } else { if (isset($aConfig['allow_server_sort']) && $aConfig['allow_server_sort']) { $aMailbox['SORT_METHOD'] = 'SERVER'; $aMailbox['THREAD_INDENT'] = false; } else { $aMailbox['SORT_METHOD'] = 'SQUIRREL'; $aMailbox['THREAD_INDENT'] = false; } } /* set a timestamp for cachecontrol */ $aMailbox['TIMESTAMP'] = time(); return $aMailbox; }
$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 (handleAsSent($mailbox) && !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_' . $account . '_' . $mailbox, serialize($aMailboxPref)); } /** * Set the config options for the messages list */ $aColumns = array(); // contains settings per column. Switch to key -> value based array, order is the order of the array keys 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: