+ if ($aMailbox['EXISTS'] == 0) {
+ $string = '<b>' . _("THIS FOLDER IS EMPTY") . '</b>';
+ echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
+ echo ' <tr><td>';
+ echo ' <table width="100%" cellpadding="0" cellspacing="0" align="center" border="0" bgcolor="'.$color[4].'">';
+ echo ' <tr><td><br />';
+ echo ' <table cellpadding="1" cellspacing="5" align="center" border="0">';
+ echo ' <tr>' . html_tag( 'td', $string."\n", 'left')
+ . '</tr>';
+ echo ' </table>';
+ echo ' <br /></td></tr>';
+ echo ' </table></td></tr>';
+ echo ' </table>';
+ return;
+ } else {
+ /*
+ * Adjust the start_msg
+ */
+ $start_msg = $aMailbox['PAGEOFFSET'];
+ if($aMailbox['PAGEOFFSET'] > $aMailbox['EXISTS']) {
+ $start_msg -= $aMailbox['LIMIT'];
+ if($start_msg < 1) {
+ $start_msg = 1;
+ }
+ }
+ $aMailbox['SEARCH'] = array(); // Alex maybe you can change this so that you can use it from a search
+
+ // initialize the fields we want to retrieve:
+ $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Content-Type');
+ $aFetchItems = array('FLAGS', 'RFC822.SIZE');
+ // Are we sorting on internaldate then retrieve the internaldate value as well
+ $internaldate = getPref($data_dir, $username, 'internal_date_sort');
+ if ($internaldate) {
+ $aFetchItems[] = 'INTERNALDATE';
+ }
+
+ if ($aMailbox['SORT'] != SQSORT_NONE && isset($aMailbox['UIDSET']) &&
+ $aMailbox['UIDSET'] ) {
+ $id = $aMailbox['UIDSET'];
+ if (sqsession_is_registered('msgs')) {
+ sqsession_unregister('msgs');
+ }
+ $id_slice = array_slice($id,$start_msg-1,$aMailbox['LIMIT']);
+ if (count($id_slice)) {
+ $msgs = sqimap_get_small_header_list($imapConnection,$id_slice,$aMailbox['LIMIT'],
+ $aHeaderFields,$aFetchItems);
+
+ } else {
+ return false;
+ }
+ // FIX ME, move to msgs cache bound to a mailbox
+ sqsession_register($msgs, 'msgs');
+ } else {
+ if (sqsession_is_registered('server_sort_array')) {
+ sqsession_unregister('server_sort_array');
+ }
+
+ if ($aMailbox['SORT'] != SQSORT_NONE || $aMailbox['SORT_METHOD'] == 'THREAD') {
+ $id = get_sorted_msgs_list($imapConnection,$aMailbox,$error);
+ if ($id !== false) {
+ $id_slice = array_slice($id,$aMailbox['OFFSET'], $aMailbox['LIMIT']);
+ if (count($id_slice)) {
+ $msgs = sqimap_get_small_header_list($imapConnection,$id_slice,$aMailbox['LIMIT'],
+ $aHeaderFields,$aFetchItems);
+ } else {
+ // FIX ME do error handling
+ return false;
+ }
+ } else {
+ // FIX ME, format message and fallback to squirrel sort
+ if ($error) {
+ echo $error;
+ }
+ }
+ } else {
+ /**
+ * 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
+ $iRangeEnd = (($aMailbox['EXISTS'] - $aMailbox['OFFSET']) > $aMailbox['LIMIT']) ?
+ $aMailbox['EXISTS'] - $aMailbox['OFFSET'] +1 - $aMailbox['LIMIT']:
+ $aMailbox['EXISTS'] - $aMailbox['OFFSET'];
+ $iRangeStart = (($aMailbox['EXISTS'] - $aMailbox['OFFSET']) > $aMailbox['LIMIT']) ?
+ $aMailbox['EXISTS']-$aMailbox['OFFSET'] +1:1;
+ $id_slice = range($iRangeStart, $iRangeEnd);
+ $msgs = sqimap_get_small_header_list($imapConnection,$id_slice,$aMailbox['LIMIT'],
+ $aHeaderFields,$aFetchItems);
+ $msgs = array_reverse($msgs,true /* preserve key */);
+ // generate id array
+ $id = array_keys($msgs);
+ $aMailbox['OFFSET'] = 0;
+ }
+ // FIX ME, use an id list bound to a mailbox
+ if ($id !== false) {
+ sqsession_register($id, 'server_sort_array');
+ }
+
+ sqsession_register($msgs, 'msgs');
+ }
+ $aMailbox['UIDSET'] =& $id;
+ $aMailbox['MSG_HEADERS'] =& $msgs;
+ if ($aMailbox['SORT_METHOD'] == 'THREAD') {
+ // retrieve indent array for thread sort
+ sqgetGlobalVar('indent_array',$indent_array,SQ_SESSION);
+ $aMailbox['THREAD_INDENT'] =& $indent_array;
+ }
+ } /* if exists > 0 */
+
+ $iEnd = ($aMailbox['PAGEOFFSET'] + ($aMailbox['LIMIT'] - 1) < $aMailbox['EXISTS']) ?
+ $aMailbox['PAGEOFFSET'] + $aMailbox['LIMIT'] - 1 : $aMailbox['EXISTS'];
+
+ $paginator_str = get_paginator_str($aMailbox['NAME'], $aMailbox['PAGEOFFSET'], $iEnd,
+ $aMailbox['EXISTS'], $aMailbox['LIMIT'], $aMailbox['SORT']);
+
+ $msg_cnt_str = get_msgcnt_str($aMailbox['PAGEOFFSET'], $iEnd,$aMailbox['EXISTS']);
+
+ do_hook('mailbox_index_before');
+?>
+<table border="0" width="100%" cellpadding="0" cellspacing="0">
+<tr>
+ <td>
+ <?php mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $paginator_str); ?>
+ </td>
+</tr>
+<tr><td height="5" bgcolor="<?php echo $color[4]; ?>"></td></tr>
+<tr>
+ <td>
+ <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="<?php echo $color[9]; ?>">
+ <tr>
+ <td>
+ <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="<?php echo $color[5]; ?>">
+ <tr>
+ <td>
+ <?php
+ printHeader($aMailbox);
+ displayMessageArray($imapConnection, $aMailbox);
+ ?>
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ </table>
+ <?php
+ mail_message_listing_end($aMailbox['EXISTS'], $paginator_str, $msg_cnt_str);
+ ?>
+ </td>
+</tr>
+</table>
+<?php
+ //$t = elapsed($start);
+ //echo("elapsed time = $t seconds\n");
+}
+
+/**
+* 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) {
+
+ $aId = $aMailbox['UIDSET'];
+ $aHeaders = $aMailbox['MSG_HEADERS'];
+ $iOffset = $aMailbox['OFFSET'];
+ $sort = $aMailbox['SORT'];
+ $iPageOffset = $aMailbox['PAGEOFFSET'];
+ $sMailbox = $aMailbox['NAME'];
+ $aSearch = (isset($aMailbox['SEARCH'])) ? $aMailbox['SEARCH'] : false;
+ if ($aMailbox['SORT_METHOD'] == 'THREAD') {
+ $aIndentArray =& $aMailbox['THREAD_INDENT'];
+ $bThread = true;