' .
+ _("Server-side sorting is not supported by your IMAP server.") . ' ' .
+ _("Please contact your system administrator and report this error.") .
+ '
';
+ } else {
+ $aMailbox['UIDSET'][$iSetIndx] = $id;
+ }
+ break;
+ default:
+ $id = NULL;
+ if ($aMailbox['SEARCH'][$iSetIndx] != 'ALL') {
+ $id = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
+ }
+ $sSortField = getSortField($aMailbox['SORT'],false);
+ $aMailbox['UIDSET'][$iSetIndx] = get_squirrel_sort($imapConnection, $sSortField, $bDirection, $id);
+ break;
+ }
+ return $error;
+}
+
+
+
+
+function fetchMessageHeaders($imapConnection, &$aMailbox) {
+
+ /**
+ * Retrieve the UIDSET.
+ * 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'];
+ /**
+ * Adjust the start_msg
+ */
+ $start_msg = $aMailbox['PAGEOFFSET'];
+ if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
+ $start_msg -= $aMailbox['LIMIT'];
if($start_msg < 1) {
- $start_msg = 1;
+ $start_msg = 1;
}
- }
-
- /* This code and the next if() block check for
- * server-side sorting methods. The $id array is
- * formatted and $sort is set to 6 to disable
- * SM internal sorting
- */
-
- if ($thread_sort_messages == 1) {
- $mode = 'thread';
- } elseif ($allow_server_sort == 1) {
- $mode = 'serversort';
- } else {
- $mode = '';
- }
-
- switch ($mode) {
- case 'thread':
- session_unregister('msort');
- session_unregister('msgs');
- $msgs = getThreadMessages($imapConnection, $start_msg, $show_num,
- $num_msgs);
- if ($msgs === false) {
- echo '
' .
- _("Thread sorting is not supported by your IMAP server. Please report this to the system administrator.").
- '
';
- $thread_sort_messages = 0;
- $msort = $msgs = array();
- session_register('msort');
- session_register('msgs');
+ }
+
+
+ if (is_array($aMailbox['UIDSET'])) {
+ $aUid =& $aMailbox['UIDSET'][$iSetIndx];
+ } else {
+ $aUid = false;
+ }
+
+ // initialize the fields we want to retrieve:
+ $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Importance', 'Priority', 'Content-Type');
+ $aFetchItems = array('FLAGS', 'RFC822.SIZE');
+
+ // Are we sorting on internaldate then retrieve the internaldate value as well
+ if ($aMailbox['INTERNALDATE']) {
+ $aFetchItems[] = 'INTERNALDATE';
+ }
+
+
+ /**
+ * A uidset with sorted uid's is available. We can use the cache
+ */
+ if (($aMailbox['SORT'] != SQSORT_NONE || $aMailbox['SEARCH'][$iSetIndx] != 'ALL') &&
+ isset($aUid) && $aUid ) {
+
+ // limit the cache to SQM_MAX_PAGES_IN_CACHE
+ if (!$aMailbox['SHOWALL'][$iSetIndx]) {
+ $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE;
+ $iCacheSize = count($aMailbox['MSG_HEADERS']);
+ if ($iCacheSize > $iMaxMsgs) {
+ $iReduce = $iCacheSize - $iMaxMsgs;
+ foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
+ if ($iReduce) {
+ unset($aMailbox['MSG_HEADERS'][$iUid]);
+ } else {
+ break;
+ }
+ --$iReduce;
+ }
+ }
+ }
+
+ $id_slice = array_slice($aUid,$start_msg-1,$iLimit);
+ /* do some funky cache checks */
+ $aUidCached = array_keys($aMailbox['MSG_HEADERS']);
+ $aUidNotCached = array_values(array_diff($id_slice,$aUidCached));
+ /**
+ * $aUidNotCached contains an array with UID's which need to be fetched to
+ * complete the needed message headers.
+ */
+ if (count($aUidNotCached)) {
+ $aMsgs = sqimap_get_small_header_list($imapConnection,$aUidNotCached,
+ $aHeaderFields,$aFetchItems);
+ // append the msgs to the existend headers
+ $aMailbox['MSG_HEADERS'] += $aMsgs;
+ }
+
+ } else {
+ /**
+ * Initialize the sorted UID list and fetch the visible message headers
+ */
+ if ($aMailbox['SORT'] != SQSORT_NONE || $aMailbox['SEARCH'][$iSetIndx] != 'ALL') {// || $aMailbox['SORT_METHOD'] & SQSORT_THREAD 'THREAD') {
+
+ $error = false;
+ if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
+ $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
} else {
- $msort= $msgs;
- $sort = 6;
- session_register('msort');
- session_register('msgs');
+ $error = get_sorted_msgs_list($imapConnection,$aMailbox,$error);
+ $aUid = $aMailbox['UIDSET'][$iSetIndx];
}
-
- break;
- case 'serversort':
- $msgs = getServerSortMessages($imapConnection, $start_msg, $show_num,
- $num_msgs, $sort, $mbxresponse);
- if ($msgs === false) {
- echo '
' .
- _( "Server-side sorting is not supported by your IMAP server. Please report this to the system administrator.").
- '
';
- $sort = $server_sort_order;
- $allow_server_sort = FALSE;
- $msort = $msgs = array();
- session_register('msort');
- session_register('msgs');
- $id = array();
- } else {
- $sort = 6;
- $msort = $msgs;
- session_register('msort');
- session_register('msgs');
- }
- break;
- default:
- if (!$use_cache) {
- session_unregister('msgs');
- session_unregister('msort');
- $msgs= getSelfSortMessages($imapConnection, $start_msg, $show_num,
- $num_msgs, $sort, $mbxresponse);
- $msort = calc_msort($msgs, $sort);
- session_register('msort');
- session_register('msgs');
- } /* !use cache */
- break;
- } // switch
- } /* if exists > 0 */
-
- $end_msg = getEndMessage(&$start_msg, $show_num, $num_msgs);
-
- $paginator_str = get_paginator_str($mailbox, $start_msg, $end_msg,
- $num_msgs, $show_num, $sort);
-
- $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
-
- do_hook('mailbox_index_before');
-
- mail_message_listing_beginning($imapConnection, $mailbox, $sort,
- $msg_cnt_str, $paginator_str, $start_msg);
-
-
- printHeader($mailbox, $srt, $color, !$thread_sort_messages);
-
- displayMessageArray($imapConnection, $num_msgs, $start_msg,
- $msort, $mailbox, $sort, $color, $show_num,0,0);
-
- mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color);
-
- /**
- * TODO: Switch to using $_SESSION[] whenever we ditch the 4.0.x series.
- */
-}
+ if ($error === false) {
+ $id_slice = array_slice($aUid,$aMailbox['OFFSET'], $iLimit);
+ if (count($id_slice)) {
+ $aMailbox['MSG_HEADERS'] = sqimap_get_small_header_list($imapConnection,$id_slice,
+ $aHeaderFields,$aFetchItems);
+ } else {
+ return false;
+ }
-function calc_msort($msgs, $sort) {
+ } else {
+ // FIX ME, format message and fallback to squirrel sort
+ if ($error) {
+ echo $error;
+ }
+ }
+ } else {
+ // limit the cache to SQM_MAX_PAGES_IN_CACHE
+ if (!$aMailbox['SHOWALL'][$iSetIndx] && isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) {
+ $iMaxMsgs = $iLimit * SQM_MAX_PAGES_IN_CACHE;
+ $iCacheSize = count($aMailbox['MSG_HEADERS']);
+ if ($iCacheSize > $iMaxMsgs) {
+ $iReduce = $iCacheSize - $iMaxMsgs;
+ foreach ($aMailbox['MSG_HEADERS'] as $iUid => $value) {
+ if ($iReduce) {
+ $iId = $aMailbox['MSG_HEADERS'][$iUid]['ID'];
+ unset($aMailbox['MSG_HEADERS'][$iUid]);
+ unset($aMailbox['ID'][$iId]);
+ } else {
+ break;
+ }
+ --$iReduce;
+ }
+ }
+ }
- /*
- * 0 = Date (up)
- * 1 = Date (dn)
- * 2 = Name (up)
- * 3 = Name (dn)
- * 4 = Subject (up)
- * 5 = Subject (dn)
- */
- if (($sort == 0) || ($sort == 1)) {
- $msort = array_cleave ($msgs, 'TIME_STAMP');
- } elseif (($sort == 2) || ($sort == 3)) {
- $msort = array_cleave ($msgs, 'FROM-SORT');
- } elseif (($sort == 4) || ($sort == 5)) {
- $msort = array_cleave ($msgs, 'SUBJECT-SORT');
- } else {
- $msort = $msgs;
- }
- if ($sort < 6) {
- if ($sort % 2) {
- asort($msort);
- } else {
- arsort($msort);
- }
- }
- return $msort;
-}
-
-function fillMessageArray($imapConnection,$id,$count) {
- $msgs_list = sqimap_get_small_header_list($imapConnection, $id);
- $messages = array();
- if (sizeof($msgs_list)){
- foreach ($msgs_list as $hdr) {
- $unique_id[] = $hdr->uid;
- $from[] = $hdr->from;
- $date[] = $hdr->date;
- $subject[] = $hdr->subject;
- $to[] = $hdr->to;
- $priority[] = $hdr->priority;
- $cc[] = $hdr->cc;
- $size[] = $hdr->size;
- $type[] = $hdr->type0;
- $flag_deleted[] = $hdr->flag_deleted;
- $flag_answered[] = $hdr->flag_answered;
- $flag_seen[] = $hdr->flag_seen;
- $flag_flagged[] = $hdr->flag_flagged;
- }
- }
-
- $j = 0;
- while ($j < $count) {
-
- if (isset($date[$j])) {
- $date[$j] = str_replace(' ', ' ', $date[$j]);
- $tmpdate = explode(' ', trim($date[$j]));
- } else {
- $tmpdate = $date = array('', '', '', '', '', '');
- }
- $messages[$j]['TIME_STAMP'] = getTimeStamp($tmpdate);
- $messages[$j]['DATE_STRING'] =
- getDateString($messages[$j]['TIME_STAMP']);
- $messages[$j]['ID'] = $unique_id[$j];
- $messages[$j]['FROM'] = decodeHeader($from[$j]);
- $messages[$j]['FROM-SORT'] =
- strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
- $messages[$j]['SUBJECT'] = decodeHeader($subject[$j]);
- $messages[$j]['SUBJECT-SORT'] = strtolower(decodeHeader($subject[$j]));
- $messages[$j]['TO'] = decodeHeader($to[$j]);
- $messages[$j]['PRIORITY'] = $priority[$j];
- $messages[$j]['CC'] = $cc[$j];
- $messages[$j]['SIZE'] = $size[$j];
- $messages[$j]['TYPE0'] = $type[$j];
- $messages[$j]['FLAG_DELETED'] = $flag_deleted[$j];
- $messages[$j]['FLAG_ANSWERED'] = $flag_answered[$j];
- $messages[$j]['FLAG_SEEN'] = $flag_seen[$j];
- $messages[$j]['FLAG_FLAGGED'] = $flag_flagged[$j];
-
-
- /*
- * fix SUBJECT-SORT to remove Re:
- * vedr|sv (Danish)
- * re|aw (English)
- *
- * TODO: i18n should be incorporated here. E.g. we catch the ones
- * we know about, but also define in i18n what the localized
- * "Re: " is for this or that locale.
- */
- if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si",
- $messages[$j]['SUBJECT-SORT'], $matches)){
- $messages[$j]['SUBJECT-SORT'] = $matches[2];
- }
- $j++;
- }
- return $messages;
+ /**
+ * retrieve messages by sequence id's and fetch the UID to retrieve
+ * the UID. for sorted lists this is not needed because a UID FETCH
+ * automaticly add the UID value in fetch results
+ **/
+ $aFetchItems[] = 'UID';
+
+ //create id range
+ $iRangeStart = $aMailbox['EXISTS'] - $aMailbox['OFFSET'];
+ $iRangeEnd = ($iRangeStart > $iLimit) ?
+ ($iRangeStart - $iLimit+1):1;
+
+ $id_slice = range($iRangeStart, $iRangeEnd);
+ /**
+ * Non sorted mailbox with cached message headers
+ */
+ if (isset($aMailbox['ID']) && is_array($aMailbox['ID'])) {
+ // the fetched id => uid relation
+ $aId = $aMailbox['ID'];
+ $aIdCached = array();
+ foreach ($aId as $iId => $iUid) {
+ if (isset($aMailbox['MSG_HEADERS'][$iUid])) {
+ if ($iId <= $iRangeStart && $iId >= $iRangeEnd) {
+ $aIdCached[] = $iId;
+ }
+ }
+ }
+ $aIdNotCached = array_diff($id_slice,$aIdCached);
+ } else {
+ $aIdNotCached = $id_slice;
+ }
+
+ if (count($aIdNotCached)) {
+ $aMsgs = sqimap_get_small_header_list($imapConnection,$aIdNotCached,
+ $aHeaderFields,$aFetchItems);
+ // append the msgs to the existend headers
+ if (isset($aMailbox['MSG_HEADERS']) && is_array($aMailbox['MSG_HEADERS'])) {
+ $aMailbox['MSG_HEADERS'] += $aMsgs;
+ } else {
+ $aMailbox['MSG_HEADERS'] = $aMsgs;
+ }
+ // update the ID array
+ foreach ($aMsgs as $iUid => $aMsg) {
+ if (isset($aMsg['ID'])) {
+ $aMailbox['ID'][$aMsg['ID']] = $iUid;
+ }
+ }
+ }
+
+ /**
+ * In unsorted state we show newest messages first which means
+ * that the UIDSET which represents the order of the messages
+ * should contain a high to low ordered UID list
+ */
+ $aSortedUidList = array();
+ foreach ($id_slice as $iId) {
+ if (isset($aMailbox['ID'][$iId])) {
+ $aSortedUidList[] = $aMailbox['ID'][$iId];
+ }
+ }
+ $aMailbox['UIDSET'][$iSetIndx] = $aSortedUidList;
+ $aMailbox['OFFSET'] = 0;
+ }
+ }
+ return true;
}
+/**
+ * This function loops through a group of messages in the mailbox
+ * and shows them to the user.
+ *
+ * @param mixed $imapConnection
+ * @param array $aMailbox associative array with mailbox related vars
+ */
+function showMessagesForMailbox($imapConnection, &$aMailbox) {
+ global $color, $javascript_on, $compact_paginator;
+
+ // to retrieve the internaldate pref: (I know this is not the right place to do that, move up in front
+ // and use a properties array as function argument to provide user preferences
+ global $data_dir, $username;
+
+ if (!fetchMessageHeaders($imapConnection, $aMailbox)) {
+ return false;
+ }
+ $iSetIndx = $aMailbox['SETINDEX'];
+ $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $aMailbox['LIMIT'];
+ $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ?
+ $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS'];
+
+ $paginator_str = get_paginator_str($aMailbox['NAME'], $aMailbox['PAGEOFFSET'],
+ $aMailbox['EXISTS'], $aMailbox['LIMIT'], $aMailbox['SHOWALL'][$iSetIndx]);
+
+ $msg_cnt_str = get_msgcnt_str($aMailbox['PAGEOFFSET'], $iEnd,$aMailbox['EXISTS']);
+
+ do_hook('mailbox_index_before');
+
+ if ($javascript_on && $compact_paginator) {
+ // Insert compact paginator javascript
+ echo "\n\n"
+ . "\n"
+ . "\n";
+ }
+ mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $paginator_str);
+?>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ $show_num) {
- $end_msg = $show_num;
- $start_msg = 1;
- }
- }
- $endVar = $end_msg + 1;
-
- /*
- * Loop through and display the info for each message.
- * ($t is used for the checkbox number)
- */
- $t = 0;
-
- /* messages display */
-
- if ($num_msgs == 0) {
- /* if there's no messages in this folder */
- echo html_tag( 'tr',
- html_tag( 'td',
- " " . _("THIS FOLDER IS EMPTY") . " ",
- 'center',
- $color[4],
- 'COLSPAN="' . count($index_order) . '"'
- )
- );
- } elseif ($start_msg == $end_msg) {
- /* if there's only one message in the box, handle it differently. */
- if ($sort != 6){
- $i = $start_msg;
+/**
+ * Function to map an uid list with a msg header array by uid
+ * The mapped headers are printed with printMessage
+ * aMailbox parameters contains info about the page we are on, the
+ * used search criteria, the number of messages to show
+ *
+ * @param resource $imapConnection socket handle to imap
+ * @param array $aMailbox array with required elements MSG_HEADERS, UIDSET, OFFSET, LIMIT
+ * @return void
+ **/
+function displayMessageArray($imapConnection, $aMailbox) {
+ $iSetIndx = $aMailbox['SETINDEX'];
+ $aId = $aMailbox['UIDSET'][$iSetIndx];
+ $aHeaders = $aMailbox['MSG_HEADERS'];
+ $iOffset = $aMailbox['OFFSET'];
+ $sort = $aMailbox['SORT'];
+ $iPageOffset = $aMailbox['PAGEOFFSET'];
+ $sMailbox = $aMailbox['NAME'];
+ $sSearch = (isset($aMailbox['SEARCH'][$aMailbox['SETINDEX']])) ? $aMailbox['SEARCH'][$aMailbox['SETINDEX']] : false;
+ $aSearch = ($sSearch) ? array('search.php',$aMailbox['SETINDEX']) : null;
+
+ if ($aMailbox['SORT'] & SQSORT_THREAD) {
+ $aIndentArray =& $aMailbox['THREAD_INDENT'][$aMailbox['SETINDEX']];
+ $bThread = true;
} else {
- $i = 1;
- }
- reset($msort);
- $k = 0;
- do {
- $key = key($msort);
- next($msort);
- $k++;
- } while (isset ($key) && ($k < $i));
- printMessageInfo($imapConnection, $t, $i, $key, $mailbox,
- $real_startMessage, $where, $what);
- } else {
- $i = $start_msg;
- reset($msort);
- $k = 0;
- do {
- $key = key($msort);
- next($msort);
- $k++;
- } while (isset ($key) && ($k < $i));
- do {
- printMessageInfo($imapConnection, $t, $i, $key, $mailbox,
- $real_startMessage, $where, $what);
- $key = key($msort);
- $t++;
- $i++;
- next($msort);
- } while ($i && $i < $endVar);
- }
- echo '';
+ $bThread = false;
+ }
+ /*
+ * Loop through and display the info for each message.
+ * ($t is used for the checkbox number)
+ */
+ $iEnd = ($aMailbox['SHOWALL'][$iSetIndx]) ? $aMailbox['EXISTS'] : $iOffset + $aMailbox['LIMIT'];
+ for ($i=$iOffset,$t=0;$i<$iEnd;++$i) {
+ if (isset($aId[$i])) {
+ $bLast = ((isset($aId[$i+1]) && isset($aHeaders[$aId[$i+1]]))
+ || ($i == $iEnd )) ? false : true;
+ if ($bThread) {
+ $indent = (isset($aIndentArray[$aId[$i]])) ? $aIndentArray[$aId[$i]] : 0;
+ } else {
+ $indent = 0;
+ }
+ $aMsg = array(
+ 'HEADER' => $aHeaders[$aId[$i]],
+ 'INDX' => $t,
+ 'OFFSET' => $iOffset,
+ 'PAGEOFFSET' => $iPageOffset,
+ 'SORT' => $sort,
+ 'SEARCH' => $aSearch,
+ 'MAILBOX' => $sMailbox,
+ 'INDENT' => $indent,
+ 'LAST' => $bLast
+ );
+ printMessageInfo($aMsg);
+ ++$t;
+ } else {
+ break;
+ }
+ }
}
-/*
- * Displays the standard message list header. To finish the table,
- * you need to do a "";
+/**
+ * Displays the standard message list header.
+ *
+ * To finish the table, you need to do a "";
*
- * $moveURL is the URL to submit the delete/move form to
- * $mailbox is the current mailbox
- * $sort is the current sorting method (-1 for no sorting available [searches])
- * $Message is a message that is centered on top of the list
- * $More is a second line that is left aligned
+ * @param resource $imapConnection
+ * @param array $aMailbox associative array with mailbox related information
+ * @param string $msg_cnt_str
+ * @param string $paginator Paginator string
*/
-
-function mail_message_listing_beginning ($imapConnection,
- $mailbox = '', $sort = -1,
- $msg_cnt_str = '',
- $paginator = ' ',
- $start_msg = 1) {
- global $color, $auto_expunge, $base_uri, $thread_sort_messages,
- $allow_thread_sort, $allow_server_sort, $server_sort_order,
- $PHP_SELF;
-
- $urlMailbox = urlencode($mailbox);
-
- if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
- $source_url = $regs[1];
- } else {
- $source_url = $PHP_SELF;
- }
-
- if (!isset($msg)) {
- $msg = '';
- }
- $moveURL = "move_messages.php?msg=$msg&mailbox=$urlMailbox"
- . "&startMessage=$start_msg";
-
- /*
- * This is the beginning of the message list table.
- * It wraps around all messages
- */
-
- echo "