Example #1
0
 /**
  * Private method for listing message headers
  *
  * @access  private
  * @see     rcube_imap::list_headers
  */
 private function _list_headers($mailbox = '', $page = NULL, $sort_field = NULL, $sort_order = NULL, $recursive = FALSE, $slice = 0)
 {
     if (!strlen($mailbox)) {
         return array();
     }
     // use saved message set
     if ($this->search_string && $mailbox == $this->mailbox) {
         return $this->_list_header_set($mailbox, $page, $sort_field, $sort_order, $slice);
     }
     $this->_set_sort_order($sort_field, $sort_order);
     $page = $page ? $page : $this->list_page;
     $cache_key = $mailbox . '.msg';
     $cache_status = $this->check_cache_status($mailbox, $cache_key);
     // cache is OK, we can get all messages from local cache
     if ($cache_status > 0) {
         $start_msg = ($page - 1) * $this->page_size;
         $a_msg_headers = $this->get_message_cache($cache_key, $start_msg, $start_msg + $this->page_size, $this->sort_field, $this->sort_order);
         $result = array_values($a_msg_headers);
         if ($slice) {
             $result = array_slice($result, -$slice, $slice);
         }
         return $result;
     } else {
         if ($this->caching_enabled && $cache_status == -1 && !$recursive) {
             $this->sync_header_index($mailbox);
             return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE, $slice);
         }
     }
     // retrieve headers from IMAP
     $a_msg_headers = array();
     // use message index sort for sorting by Date (for better performance)
     if ($this->index_sort && $this->sort_field == 'date') {
         if ($this->skip_deleted) {
             // @TODO: this could be cached
             if ($msg_index = $this->_search_index($mailbox, 'ALL UNDELETED')) {
                 $max = max($msg_index);
                 list($begin, $end) = $this->_get_message_range(count($msg_index), $page);
                 $msg_index = array_slice($msg_index, $begin, $end - $begin);
             }
         } else {
             if ($max = iil_C_CountMessages($this->conn, $mailbox)) {
                 list($begin, $end) = $this->_get_message_range($max, $page);
                 $msg_index = range($begin + 1, $end);
             } else {
                 $msg_index = array();
             }
         }
         if ($slice) {
             $msg_index = array_slice($msg_index, $this->sort_order == 'DESC' ? 0 : -$slice, $slice);
         }
         // fetch reqested headers from server
         if ($msg_index) {
             $this->_fetch_headers($mailbox, join(",", $msg_index), $a_msg_headers, $cache_key);
         }
     } else {
         if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : ''))) {
             list($begin, $end) = $this->_get_message_range(count($msg_index), $page);
             $max = max($msg_index);
             $msg_index = array_slice($msg_index, $begin, $end - $begin);
             if ($slice) {
                 $msg_index = array_slice($msg_index, $this->sort_order == 'DESC' ? 0 : -$slice, $slice);
             }
             // fetch reqested headers from server
             $this->_fetch_headers($mailbox, join(',', $msg_index), $a_msg_headers, $cache_key);
         } else {
             if ($a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:*", $this->sort_field, $this->skip_deleted)) {
                 asort($a_index);
                 // ASC
                 $msg_index = array_keys($a_index);
                 $max = max($msg_index);
                 list($begin, $end) = $this->_get_message_range(count($msg_index), $page);
                 $msg_index = array_slice($msg_index, $begin, $end - $begin);
                 if ($slice) {
                     $msg_index = array_slice($msg_index, $this->sort_order == 'DESC' ? 0 : -$slice, $slice);
                 }
                 // fetch reqested headers from server
                 $this->_fetch_headers($mailbox, join(",", $msg_index), $a_msg_headers, $cache_key);
             }
         }
     }
     // delete cached messages with a higher index than $max+1
     // Changed $max to $max+1 to fix this bug : #1484295
     $this->clear_message_cache($cache_key, $max + 1);
     // kick child process to sync cache
     // ...
     // return empty array if no messages found
     if (!is_array($a_msg_headers) || empty($a_msg_headers)) {
         return array();
     }
     // use this class for message sorting
     $sorter = new rcube_header_sorter();
     $sorter->set_sequence_numbers($msg_index);
     $sorter->sort_headers($a_msg_headers);
     if ($this->sort_order == 'DESC') {
         $a_msg_headers = array_reverse($a_msg_headers);
     }
     return array_values($a_msg_headers);
 }
Example #2
0
 /**
  * Private method for getting nr of messages
  *
  * @access  private
  * @see     rcube_imap::messagecount()
  */
 function _messagecount($mailbox = '', $mode = 'ALL', $force = FALSE)
 {
     $a_mailbox_cache = FALSE;
     $mode = strtoupper($mode);
     if (empty($mailbox)) {
         $mailbox = $this->mailbox;
     }
     // count search set
     if ($this->search_string && $mailbox == $this->mailbox && $mode == 'ALL' && !$force) {
         return count((array) $this->search_set);
     }
     $a_mailbox_cache = $this->get_cache('messagecount');
     // return cached value
     if (!$force && is_array($a_mailbox_cache[$mailbox]) && isset($a_mailbox_cache[$mailbox][$mode])) {
         return $a_mailbox_cache[$mailbox][$mode];
     }
     // RECENT count is fetched a bit different
     if ($mode == 'RECENT') {
         $count = iil_C_CheckForRecent($this->conn, $mailbox);
     } else {
         if ($this->skip_deleted) {
             $search_str = "ALL UNDELETED";
             // get message count and store in cache
             if ($mode == 'UNSEEN') {
                 $search_str .= " UNSEEN";
             }
             // get message count using SEARCH
             // not very performant but more precise (using UNDELETED)
             $count = 0;
             $index = $this->_search_index($mailbox, $search_str);
             if (is_array($index)) {
                 $str = implode(",", $index);
                 if (!empty($str)) {
                     $count = count($index);
                 }
             }
         } else {
             if ($mode == 'UNSEEN') {
                 $count = iil_C_CountUnseen($this->conn, $mailbox);
             } else {
                 $count = iil_C_CountMessages($this->conn, $mailbox);
             }
         }
     }
     if (!is_array($a_mailbox_cache[$mailbox])) {
         $a_mailbox_cache[$mailbox] = array();
     }
     $a_mailbox_cache[$mailbox][$mode] = (int) $count;
     // write back to cache
     $this->update_cache('messagecount', $a_mailbox_cache);
     return (int) $count;
 }