+ $error = false;
+ if ($aMailbox['SEARCH'][$iSetIndx] && $aMailbox['SORT'] == 0) {
+ $aUid = sqimap_run_search($imapConnection, $aMailbox['SEARCH'][$iSetIndx], $aMailbox['CHARSET'][$iSetIndx]);
+ } else {
+ $error = get_sorted_msgs_list($imapConnection,$aMailbox,$error);
+ $aUid = $aMailbox['UIDSET'][$iSetIndx];
+ }
+ 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;
+ }
+
+ } 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;
+ }
+ }
+ }
+
+ /**
+ * 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;
+
+ // 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');
+?>
+<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
+
+}
+
+/**
+* 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 {
+ $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 "</table></table>";
+*
+* @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,
+ $aMailbox,
+ $msg_cnt_str = '',
+ $paginator = ' '
+ ) {
+ global $color, $show_flag_buttons, $PHP_SELF;
+ global $lastTargetMailbox, $boxes;
+
+ $php_self = $PHP_SELF;
+
+ $urlMailbox = urlencode($aMailbox['NAME']);
+
+ if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
+ $source_url = $regs[1];
+ } else {
+ $source_url = $php_self;
+ }
+
+ if (!isset($msg)) {
+ $msg = '';
+ }
+
+ $moveFields = addHidden('msg', $msg).
+ addHidden('mailbox', $aMailbox['NAME']).
+ addHidden('startMessage', $aMailbox['PAGEOFFSET']);
+
+ /* build thread sorting links */
+ $sort = $aMailbox['SORT'];
+ if ($aMailbox['ALLOW_THREAD']) {
+ if ($aMailbox['SORT'] & SQSORT_THREAD) {
+ $sort -= SQSORT_THREAD;
+ $thread_name = _("Unthread View");
+ } else {
+ $thread_name = _("Thread View");
+ $sort = $aMailbox['SORT'] + SQSORT_THREAD;
+ }
+ $thread_link_str = '<small>[<a href="' . $source_url . '?srt='
+ . $sort . '&start_messages=1'
+ . '&mailbox=' . urlencode($aMailbox['NAME']) . '">' . $thread_name
+ . '</a>]</small>';
+ } else {
+ $thread_link_str ='';
+ }
+ /*
+ * This is the beginning of the message list table.
+ * It wraps around all messages
+ */
+ $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
+ $form_name = "FormMsgs" . $safe_name;
+
+ echo '<form name="' . $form_name . '" method="post" action="'.$php_self.'">' ."\n"
+ . $moveFields;
+
+ $button_str = '';
+ // display flag buttons only if supported
+ if ($show_flag_buttons &&
+ in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true) ) {
+ $button_str .= getButton('SUBMIT', 'markUnflagged', _("Unflag"));
+ $button_str .= getButton('SUBMIT', 'markFlagged', _("Flag"));
+ $button_str .= " \n";
+ }
+ if (in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true)) {
+ $button_str .= getButton('SUBMIT', 'markUnread', _("Unread"));
+ $button_str .= getButton('SUBMIT', 'markRead', _("Read"));
+ $button_str .= " \n";
+ }
+ $button_str .= getButton('SUBMIT', 'attache',_("Forward")) .
+ " \n";
+ if (in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) {
+ $button_str .= getButton('SUBMIT', 'delete',_("Delete"));
+ $button_str .= '<input type="checkbox" name="bypass_trash" />' . _("Bypass Trash");
+ $button_str .= " \n";
+ }
+ if (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY') {
+ $button_str .= getButton('SUBMIT', 'expungeButton',_("Expunge")) .' ' . _("mailbox") . "\n";
+ $button_str .= ' ';
+ }
+?>
+ <table width="100%" cellpadding="1" cellspacing="0" style="border: 1px solid <?php echo $color[0]; ?>">
+ <tr>
+ <td>
+ <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1" cellspacing="0">
+ <tr>
+ <?php echo html_tag('td', '<small>' . $paginator . $thread_link_str . '</small>', 'left') . "\n"; ?>
+ <?php echo html_tag('td', '', 'center') . "\n"; ?>
+ <?php echo html_tag('td', '<small>' . $msg_cnt_str . '</small>', 'right') . "\n"; ?>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ <tr width="100%" cellpadding="1" cellspacing="0" border="0" bgcolor="<?php echo $color[0]; ?>">
+ <td>
+ <table border="0" width="100%" cellpadding="1" cellspacing="0">
+ <tr>
+ <?php echo html_tag('td', '', 'left') . "\n"; ?>
+ <small>
+ <?php echo $button_str; ?>
+ <?php do_hook('mailbox_display_buttons'); ?>
+ </small>
+ </td>
+ <?php
+ if (in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) {
+ ?>
+ <?php echo html_tag('td', '', 'right'); ?>
+ <small> <tt>
+ <select name="targetMailbox">
+ <?php echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes); ?>
+ </select></tt>
+ <?php echo getButton('SUBMIT', 'moveButton',_("Move")); ?>
+ </small>
+ <?php
+ }
+ ?>
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ </table>
+<?php
+ do_hook('mailbox_form_before');
+}
+
+/**
+* Function to add the last row in a message list, it contains the paginator and info about
+* the number of messages.
+*
+* @param integer $num_msgs number of messages in a mailbox
+* @param string $paginator_str Paginator string [Prev | Next] [ 1 2 3 ... 91 92 94 ] [Show all]
+* @param string $msg_cnt_str Message count string Viewing Messages: 21 to 1861 (20 total)
+*/
+function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str) {
+global $color;
+if ($num_msgs) {
+ /* space between list and footer */
+?>
+<tr><td height="5" bgcolor="<?php echo $color[4]; ?>" colspan="1"></td></tr>
+<tr>
+ <td>
+ <table width="100%" cellpadding="1" cellspacing="0" style="border: 1px solid <?php echo $color[0]; ?>">
+ <tr>
+ <td>
+ <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1" cellspacing="0">
+ <tr>
+ <?php echo html_tag('td', '<small>' . $paginator_str . '</small>', 'left'); ?>
+ <?php echo html_tag('td', '<small>' . $msg_cnt_str . '</small>', 'right'); ?>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ </table>
+ </td>
+</tr>
+<?php
+}
+ /* End of message-list table */
+
+ do_hook('mailbox_index_after');
+ echo "</form>\n";
+}
+
+/**
+* Prints the table header for the messages list view
+*
+* @param array $aMailbox
+*/
+function printHeader($aMailbox) {
+ global $index_order, $internal_date_sort, $color;
+
+ if ($aMailbox['SORT_METHOD'] != 'THREAD') {
+ $showsort = true;
+ } else {
+ $showsort = false;
+ }
+
+ echo html_tag( 'tr' ,'' , 'center', $color[5] );
+
+ /* calculate the width of the subject column based on the
+ * widths of the other columns */
+ $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5);
+ $subjectwidth = 100;
+ foreach($index_order as $item) {
+ $subjectwidth -= $widths[$item];
+ }
+
+ foreach ($index_order as $item) {
+ switch ($item) {
+ case 1: /* checkbox */
+ echo html_tag( 'td',get_selectall_link($aMailbox) , '', '', 'width="1%"' );
+ break;
+ case 5: /* flags */
+ echo html_tag( 'td','' , '', '', 'width="1%"' );
+ break;
+ case 2: /* from */
+ if (handleAsSent($aMailbox['NAME'])) {
+ echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
+ . '<b>' . _("To") . '</b>';
+ if ($showsort) {
+ ShowSortButton($aMailbox, SQSORT_TO_ASC, SQSORT_TO_DEC);
+ }
+ } else {
+ echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
+ . '<b>' . _("From") . '</b>';
+ if ($showsort) {
+ ShowSortButton($aMailbox, SQSORT_FROM_ASC, SQSORT_FROM_DEC);
+ }
+ }
+ echo "</td>\n";
+ break;
+ case 3: /* date */
+ echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' )
+ . '<b>' . _("Date") . '</b>';
+ if ($showsort) {
+ if ($internal_date_sort) {
+ ShowSortButton($aMailbox, SQSORT_INT_DATE_ASC, SQSORT_INT_DATE_DEC);
+ } else {
+ ShowSortButton($aMailbox, SQSORT_DATE_ASC, SQSORT_DATE_DEC);
+ }
+ }
+ echo "</td>\n";
+ break;
+ case 4: /* subject */
+ echo html_tag( 'td' ,'' , 'left', '', 'width="'.$subjectwidth.'%"' )
+ . '<b>' . _("Subject") . '</b>';
+ if ($showsort) {
+ ShowSortButton($aMailbox, SQSORT_SUBJ_ASC, SQSORT_SUBJ_DEC);
+ }
+ echo "</td>\n";
+ break;
+ case 6: /* size */
+ echo html_tag( 'td', '', 'center','','width="5%" nowrap')
+ . '<b>' . _("Size") . '</b>';
+ if ($showsort) {
+ ShowSortButton($aMailbox, SQSORT_SIZE_ASC, SQSORT_SIZE_DEC);
+ }
+ echo "</td>\n";
+ break;
+ }
+ }
+ echo "</tr>\n";
+}
+
+
+/**
+* This function shows the sort button. Isn't this a good comment?
+*
+* @param array $aMailbox
+* @param integer $Down
+* @param integer $Up
+*/
+function ShowSortButton($aMailbox, $Down, $Up ) {
+ global $PHP_SELF;
+
+ /* Figure out which image we want to use. */
+ if ($aMailbox['SORT'] != $Up && $aMailbox['SORT'] != $Down) {
+ $img = 'sort_none.png';
+ $which = $Up;
+ } elseif ($aMailbox['SORT'] == $Up) {
+ $img = 'up_pointer.png';
+ $which = $Down;
+ } else {
+ $img = 'down_pointer.png';
+ $which = 0;
+ }
+
+ if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
+ $source_url = $regs[1];
+ } else {
+ $source_url = $PHP_SELF;
+ }
+
+ /* Now that we have everything figured out, show the actual button. */
+ echo ' <a href="' . $source_url .'?srt=' . $which
+ . '&startMessage=1&mailbox=' . urlencode($aMailbox['NAME'])
+ . '"><img src="../images/' . $img
+ . '" border="0" width="12" height="10" alt="sort" title="'
+ . _("Click here to change the sorting of the message list") .'" /></a>';
+}
+
+/**
+* FIXME: Undocumented function
+*
+* @param array $aMailbox
+*/
+function get_selectall_link($aMailbox) {
+ global $checkall, $javascript_on;
+ global $PHP_SELF;
+
+ $result = '';
+ if ($javascript_on) {
+ $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
+ $func_name = "CheckAll" . $safe_name;
+ $form_name = "FormMsgs" . $safe_name;
+ $result = '<script language="JavaScript" type="text/javascript">'
+ . "\n<!-- \n"
+ . "function " . $func_name . "() {\n"
+ . " for (var i = 0; i < document." . $form_name . ".elements.length; i++) {\n"
+ . " if(document." . $form_name . ".elements[i].type == 'checkbox' && "
+ . " document." . $form_name . ".elements[i].name.substring(0,3) == 'msg'){\n"
+ . " document." . $form_name . ".elements[i].checked = "
+ . " !(document." . $form_name . ".elements[i].checked);\n"
+ . " }\n"
+ . " }\n"
+ . "}\n"
+ . "//-->\n"
+ . '</script>'
+ . '<input type="checkbox" name="toggleAll" title="'._("Toggle All").'" onclick="'.$func_name.'();" />';
+// . <a href="javascript:void(0)" onClick="' . $func_name . '();">' . _("Toggle All")
+// . "</a>\n";
+ } else {
+ if (strpos($PHP_SELF, "?")) {
+ $result .= "<a href=\"$PHP_SELF&mailbox=" . urlencode($aMailbox['NAME'])
+ . "&startMessage=$aMailbox[PAGEOFFSET]&srt=$aMailbox[SORT]&checkall=";
+ } else {
+ $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
+ . "&startMessage=$aMailbox[PAGEOFFSET]&srt=$aMailbox[SORT]&checkall=";
+ }
+ if (isset($checkall) && $checkall == '1') {
+ $result .= '0';