Source for file mailbox_display.php
Documentation is available at mailbox_display.php
* This contains functions that display mailbox information, such as the
* table row that has sender, date, subject, etc...
* @copyright © 1999-2006 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: mailbox_display.php,v 1.465 2006/07/15 12:00:45 tokul Exp $
* 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
* 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
$aDefaultConfigProps =
array(
// 'charset' => 'US-ASCII',
'user' =>
false, /* no pref storage if false */
$iSetIndx =
$aConfig['setindex'];
if (isset
($mailbox_cache[$account.
'_'.
$mailbox])) {
$aCachedMailbox =
$mailbox_cache[$account.
'_'.
$mailbox];
if (count($mailbox_cache) >
$aConfig['max_cache_size'] -
1) {
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) {
* 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;
* Deal with imap servers that do not return the required UIDNEXT or
* from a SELECT call (since rfc 3501 it's required).
if (!isset
($aMbxResponse['UIDNEXT']) ||
!isset
($aMbxResponse['UIDVALIDITY'])) {
array('UIDNEXT','UIDVALIDITY'));
$aMbxResponse['UIDNEXT'] =
$aStatus['UIDNEXT'];
$aMbxResponse['UIDVALIDTY'] =
$aStatus['UIDVALIDITY'];
$aMailbox['ACCOUNT'] =
$account;
$aMailbox['UIDSET'][$iSetIndx] =
false;
$aMailbox['SETINDEX'] =
$iSetIndx;
$aMailbox['MSG_HEADERS'] =
false;
* 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']) ||
/* always set search from the searchpage */
$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]) {
$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]);
$server_sort_array =
false;
$aMailbox['MSG_HEADERS'] =
false;
// store the new sort value in the mailbox pref
// FIXME, in ideal situation, we write back the
// prefs at the end of the script
$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'];
$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];
$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'];
* 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'];
* Restore the ordered columns to show when no new ordered columns are provided
$aMailbox['COLUMNS'] =
$aCachedMailbox['COLUMNS'];
* Restore the headers we fetch the last time. Saves intitialisation stuff in read_body.
$aMailbox['FETCHHEADERS'] =
(isset
($aCachedMailbox['FETCHHEADERS'])) ?
$aCachedMailbox['FETCHHEADERS'] :
null;
$aMailbox['AUTO_EXPUNGE'] =
$aCachedMailbox['AUTO_EXPUNGE'];
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]) {
$aMailbox['SEARCH'][$iSetIndx] =
$aConfig['search'];
$aMailbox['PAGEOFFSET'] =
1;
$aMailbox['SEARCH'][$iSetIndx] =
(isset
($aConfig['search'])) ?
$aConfig['search'] :
'ALL';
if (!isset
($aConfig['charset']) && isset
($aCachedMailbox['CHARSET'][$iSetIndx])) {
$aMailbox['CHARSET'][$iSetIndx] =
$aCachedMailbox['CHARSET'][$iSetIndx];
$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 */
$aMailbox['THREAD_INDENT'] =
$aCachedMailbox['THREAD_INDENT'];
$aMailbox['THREAD_INDENT'] =
false;
/* set a timestamp for cachecontrol */
$aMailbox['TIMESTAMP'] =
time();
* Fetch the message headers for a mailbox. Settings are part of the aMailbox
* array. Dependent of the mailbox settings it deals with sort, thread and search
* If server sort is supported then SORT is also used for retrieving sorted search results
* @param resource $imapConnection imap socket handle
* @param array $aMailbox (reference) mailbox retrieved from sqm_api_mailbox_select
* @return error $error error number
* @author Marc Groot Koerkamp
/* FIX ME, this function is kind of big, maybe we can split it up in
a couple of functions. Make sure the functions are private and starts with _
Also make sure that the error codes are propagated */
* Setindex is used to be able to store multiple uid sets. That will make it
* possible to display the mailbox multiple times in different sort order
* or to store serach results separate from normal mailbox view.
$iSetIndx =
(isset
($aMailbox['SETINDEX'])) ?
$aMailbox['SETINDEX'] :
0;
$iLimit =
($aMailbox['SHOWALL'][$iSetIndx]) ?
$aMailbox['EXISTS'] :
$aMailbox['LIMIT'];
$start_msg =
$aMailbox['PAGEOFFSET'];
if($aMailbox['PAGEOFFSET'] >
$aMailbox['EXISTS']) {
$start_msg -=
$aMailbox['LIMIT'];
$aUid =
& $aMailbox['UIDSET'][$iSetIndx];
$aFetchHeaders =
$aMailbox['FETCHHEADERS'];
$aFetchItems =
$aHeaderItems =
array();
// initialize the fields we want to retrieve:
$aHeaderFields =
array();
foreach ($aFetchHeaders as $v) {
* A uidset with sorted uid's is available. We can use the cache
if (isset
($aUid) &&
$aUid ) {
// limit the cache to SQM_MAX_PAGES_IN_CACHE
if (!$aMailbox['SHOWALL'][$iSetIndx] && isset
($aMailbox['MSG_HEADERS'])) {
$iCacheSize =
count($aMailbox['MSG_HEADERS']);
if ($iCacheSize >
$iMaxMsgs) {
$iReduce =
$iCacheSize -
$iMaxMsgs;
foreach ($aMailbox['MSG_HEADERS'] as $iUid =>
$value) {
unset
($aMailbox['MSG_HEADERS'][$iUid]);
/* do some funky cache checks */
if (isset
($aMailbox['MSG_HEADERS']) &&
is_array($aMailbox['MSG_HEADERS'])) {
// temp code, read_body del / next links fo not update fields.
foreach ($aMailbox['MSG_HEADERS'] as $iUid =>
$aValue) {
if (!isset
($aValue['UID'])) {
unset
($aMailbox['MSG_HEADERS'][$iUid]);
$aUidCached =
array_keys($aMailbox['MSG_HEADERS']);
$aMailbox['MSG_HEADERS'] =
array();
* $aUidNotCached contains an array with UID's which need to be fetched to
* complete the needed message headers.
if (count($aUidNotCached)) {
$aHeaderFields,$aFetchItems);
// append the msgs to the existend headers
$aMailbox['MSG_HEADERS'] +=
$aMsgs;
* Initialize the sorted UID list or initiate a UID list with search
* results and fetch the visible message headers
if ($aMailbox['SEARCH'][$iSetIndx] !=
'ALL') { // in case of a search request
if ($aMailbox['SEARCH'][$iSetIndx] &&
$aMailbox['SORT'] ==
0) {
$aUid =
sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
$aUid =
$aMailbox['UIDSET'][$iSetIndx];
* Number of messages is the resultset
$aMailbox['TOTAL'][$iSetIndx] =
count($aUid);
$id_slice =
array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
$aHeaderFields,$aFetchItems);
$iError =
1; // FIX ME, define an error code
$aUid =
$aMailbox['UIDSET'][$iSetIndx];
* Number of messages is the resultset
$aMailbox['TOTAL'][$iSetIndx] =
count($aUid);
$id_slice =
array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
$aHeaderFields,$aFetchItems);
$iError =
1; // FIX ME, define an error code