+ if($start_msg > $num_msgs) {
+ $start_msg -= $show_num;
+ if($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) {
+ $id = get_thread_sort($imapConnection);
+ if ($id == 'no') {
+ echo '<b><small><center><font color=red>Thread sorting is not'.
+ ' supported by your IMAP server.<br>Please report this'.
+ ' to the system administrator.</center></small></b>';
+ $thread_sort_messages = 0;
+ $id = array();
+ }
+ else {
+ $sort = 6;
+ if ($start_msg + ($show_num - 1) < $num_msgs) {
+ $end_msg = $start_msg + ($show_num-1);
+ }
+ else {
+ $end_msg = $num_msgs;
+ }
+ $id = array_slice($id, ($start_msg-1), ($end_msg));
+ }
+ }
+
+ if ($allow_server_sort == TRUE && $thread_sort_messages != 1) {
+ $server_sort_order = $sort;
+ $id = sqimap_get_sort_order($imapConnection, $server_sort_order);
+ if ($id == 'no') {
+ echo '<b><small><center><font color=red>Server-side sorting'.
+ ' is not supported by your IMAP server.<br>Please report this'.
+ ' to the system administrator.</center></small></b>';
+ $sort = $server_sort_order;
+ $allow_server_sort = FALSE;
+ $id = array();
+ }
+ else {
+ $sort = 6;
+ if ($start_msg + ($show_num - 1) < $num_msgs) {
+ $end_msg = $start_msg + ($show_num-1);
+ }
+ else {
+ $end_msg = $num_msgs;
+ }
+ $id = array_slice($id, ($start_msg-1), ($end_msg));
+ }
+ }
+
+ /* If autoexpunge is turned on, then do it now. */
+ if ($auto_expunge == true) {
+ sqimap_mailbox_expunge($imapConnection, $mailbox, false);
+ }
+ sqimap_mailbox_select($imapConnection, $mailbox);
+ $issent = handleAsSent($mailbox);
+ if (!$use_cache) {
+ /* If it is sorted... */
+ if ($num_msgs >= 1) {
+ if ($sort < 6 ) {
+ $id = range(1, $num_msgs);
+ }
+ elseif ($thread_sort_messages != 1 && $allow_server_sort != TRUE && $sort == 6) {
+ /* if it's not sorted */
+ if ($start_msg + ($show_num - 1) < $num_msgs){
+ $end_msg = $start_msg + ($show_num - 1);
+ } else {
+ $end_msg = $num_msgs;
+ }
+ if ($end_msg < $start_msg) {
+ $start_msg = $start_msg - $show_num;
+ if ($start_msg < 1) {
+ $start_msg = 1;
+ }
+ }
+
+ $real_startMessage = $num_msgs - $start_msg + 1;
+ $real_endMessage = $num_msgs - $start_msg - $show_num + 2;
+ if ($real_endMessage <= 0) {
+ $real_endMessage = 1;
+ }
+ $id = array_reverse(range($real_endMessage, $real_startMessage));
+ }
+ $msgs_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
+// $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
+ if (sizeof($msgs_list)){
+ foreach ($msgs_list as $hdr) {
+ $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;
+ if ($sort == 6) {
+ $end = $start_msg + $show_num - 1;
+ if ($num_msgs < $show_num) {
+ $end_loop = $num_msgs;
+ } else if ($end > $num_msgs) {
+ $end_loop = $num_msgs - $start_msg + 1;
+ } else {
+ $end_loop = $show_num;
+ }
+ } else {
+ $end = $num_msgs;
+ $end_loop = $end;
+ }
+ while ($j < $end_loop) {
+ 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'] = $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];
+ }
+/*
+ $num = 0;
+ while ($num < count($flags[$j])) {
+ if ($flags[$j][$num] == 'Deleted') {
+ $messages[$j]['FLAG_DELETED'] = true;
+ } else if ($flags[$j][$num] == 'Answered') {
+ $messages[$j]['FLAG_ANSWERED'] = true;
+ } else if ($flags[$j][$num] == 'Seen') {
+ $messages[$j]['FLAG_SEEN'] = true;
+ } else if ($flags[$j][$num] == 'Flagged') {
+ $messages[$j]['FLAG_FLAGGED'] = true;
+ }
+ $num++;
+ }
+*/
+ $j++;