alecpl
2008-12-12 2b5c123aadfa50f09a60b3d05c240932f9e5907a
program/include/rcube_imap.php
@@ -107,7 +107,17 @@
    $ICL_PORT = $port;
    $IMAP_USE_INTERNAL_DATE = false;
    $this->conn = iil_Connect($host, $user, $pass, array('imap' => $auth_type ? $auth_type : 'check'));
    // set connection options
    $options['imap'] = $auth_type ? $auth_type : 'check';
    // Setting root and delimiter before iil_Connect can save time detecting them
    // using NAMESPACE and LIST
    if (is_string($imap_root = rcmail::get_instance()->config->get('imap_root')))
        $options['rootdir'] = $imap_root;
    if($imap_delimiter = rcmail::get_instance()->config->get('imap_delimiter'))
        $options['delimiter'] = $imap_delimiter;
    $this->conn = iil_Connect($host, $user, $pass, $options);
    $this->host = $host;
    $this->user = $user;
    $this->pass = $pass;
@@ -288,13 +298,13 @@
   */
  function set_search_set($str=null, $msgs=null, $charset=null, $sort_field=null)
    {
    if ($msgs == null)
    if (is_array($str) && $msgs == null)
      list($str, $msgs, $charset, $sort_field) = $str;
    if ($msgs != null && !is_array($msgs))
      $msgs = split(',', $msgs);
      
    $this->search_string = $str;
    $this->search_set = (array)$msgs;
    $this->search_set = $msgs;
    $this->search_charset = $charset;
    $this->search_sort_field = $sort_field;
    }
@@ -592,18 +602,13 @@
      // fetch reuested headers from server
      $a_msg_headers = array();
      $deleted_count = $this->_fetch_headers($mailbox, $msgs, $a_msg_headers, $cache_key);
      if ($this->sort_order == 'DESC' && $headers_sorted) {
        //since the sort order is not used in the iil_c_sort function we have to do it here
        $a_msg_headers = array_reverse($a_msg_headers);
      }
      // 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
@@ -677,23 +682,40 @@
      return array_values($a_msg_headers);
      }
    else { // SEARCH searching result
      // not sorted, so we must fetch headers for all messages
      // TODO: to minimize big memory consumption on servers without SORT
      // capability, we should fetch only headers used for sorting, and then
      // fetch all headers needed for displaying one page of messages list.
      // Of course it has sense only for big results if count($msgs) > $this->pagesize
      $this->_fetch_headers($mailbox, join(',', $msgs), $a_msg_headers, NULL);
      // return empty array if no messages found
      if (!is_array($a_msg_headers) || empty($a_msg_headers))
        return array();
    else { // SEARCH searching result, need sorting
      $cnt = count($msgs);
      if ($cnt > 300 && $cnt > $this->page_size) { // experimantal value for best result
        // use memory less expensive (and quick) method for big result set
   $a_index = $this->message_index($mailbox, $this->sort_field, $this->sort_order);
        // get messages uids for one page...
        $msgs = array_slice(array_keys($a_index), $start_msg, min($cnt-$start_msg, $this->page_size));
   // ...and fetch headers
        $this->_fetch_headers($mailbox, join(',', $msgs), $a_msg_headers, NULL);
      // if not already sorted
      $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
        // return empty array if no messages found
        if (!is_array($a_msg_headers) || empty($a_msg_headers))
          return array();
        $sorter = new rcube_header_sorter();
        $sorter->set_sequence_numbers($msgs);
        $sorter->sort_headers($a_msg_headers);
        return array_values($a_msg_headers);
        }
      else {
        // for small result set we can fetch all messages headers
        $this->_fetch_headers($mailbox, join(',', $msgs), $a_msg_headers, NULL);
        // return empty array if no messages found
        if (!is_array($a_msg_headers) || empty($a_msg_headers))
          return array();
        // if not already sorted
        $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
      
      // only return the requested part of the set
      return array_slice(array_values($a_msg_headers), $start_msg, min(count($msgs)-$start_msg, $this->page_size));
        // only return the requested part of the set
        return array_slice(array_values($a_msg_headers), $start_msg, min($cnt-$start_msg, $this->page_size));
        }
      }
    }
@@ -799,7 +821,7 @@
    // we have a saved search result. get index from there
    if (!isset($this->cache[$key]) && $this->search_string && $mailbox == $this->mailbox)
    {
      $this->cache[$key] = $a_msg_headers = array();
      $this->cache[$key] = array();
      
      if ($this->get_capability('sort'))
        {
@@ -813,11 +835,14 @@
        }
      else
        {
   // TODO: see list_header_set (fetch only one header field needed for sorting)
        $this->_fetch_headers($mailbox, join(',', $this->search_set), $a_msg_headers, NULL);
   $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, join(',', $this->search_set), $this->sort_field);
        foreach (iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order) as $i => $msg)
          $this->cache[$key][] = $msg->id;
        if ($this->sort_order=="ASC")
          asort($a_index);
        else if ($this->sort_order=="DESC")
          arsort($a_index);
        $this->cache[$key] = array_keys($a_index);
   }
    }
@@ -833,9 +858,8 @@
    if ($cache_status>0)
      {
      $a_index = $this->get_message_cache_index($cache_key, TRUE, $this->sort_field, $this->sort_order);
      return array_values($a_index);
      return array_keys($a_index);
      }
    // fetch complete message index
    $msg_count = $this->_messagecount($mailbox);
@@ -855,7 +879,7 @@
      else if ($this->sort_order=="DESC")
        arsort($a_index);
        
      $this->cache[$key] = $a_index;
      $this->cache[$key] = array_keys($a_index);
      }
    return $this->cache[$key];
@@ -923,12 +947,16 @@
   */
  function search($mbox_name='', $str=NULL, $charset=NULL, $sort_field=NULL)
    {
    if (!$str)
      return false;
    $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    $results = $this->_search_index($mailbox, $str, $charset, $sort_field);
    // try search with ISO charset (should be supported by server)
    if (empty($results) && !empty($charset) && $charset!='ISO-8859-1')
    // only if UTF-8 search is not supported
    if (empty($results) && !is_array($results) && !empty($charset) && $charset!='ISO-8859-1')
      {
   // convert strings to ISO-8859-1
        if(preg_match_all('/\{([0-9]+)\}\r\n/', $str, $matches, PREG_OFFSET_CAPTURE))
@@ -964,10 +992,13 @@
   * @access private
   * @see rcube_imap::search()
   */
  function _search_index($mailbox, $criteria='ALL', $charset='', $sort_field='')
  function _search_index($mailbox, $criteria='ALL', $charset=NULL, $sort_field=NULL)
    {
    if ($this->get_capability('sort'))
    if ($sort_field && $this->get_capability('sort'))
      {
      $charset = $charset ? $charset : $this->default_charset;
      $a_messages = iil_C_Sort($this->conn, $mailbox, $sort_field, $criteria, FALSE, $charset);
      }
    else
      $a_messages = iil_C_Search($this->conn, $mailbox, ($charset ? "CHARSET $charset " : '') . $criteria);
@@ -1343,6 +1374,8 @@
      
    // TODO: Add caching for message parts
    if (!$part) $part = 'TEXT';
    if ($print)
      {
      $mode = $o_part->encoding == 'base64' ? 3 : ($o_part->encoding == 'quoted-printable' ? 1 : 2);
@@ -1414,10 +1447,7 @@
    if (!($msg_id = $this->_uid2id($uid)))
      return FALSE;
    $body = iil_C_FetchPartHeader($this->conn, $this->mailbox, $msg_id, NULL);
    $body .= iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, 1);
    return $body;
    return iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id);
    }
@@ -1448,8 +1478,6 @@
    if (!($msg_id = $this->_uid2id($uid)))
      return FALSE;
    print iil_C_FetchPartHeader($this->conn, $this->mailbox, $msg_id, NULL);
    flush();
    iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, 2);
    }
@@ -2127,8 +2155,7 @@
      {
      $this->db->query(
        "UPDATE ".get_table_name('cache')."
         SET    created=".$this->db->now().",
                data=?
         SET    created=". $this->db->now().", data=?
         WHERE  user_id=?
         AND    cache_key=?",
        $data,
@@ -2478,12 +2505,13 @@
  /**
   * Decode a mime-encoded string to internal charset
   *
   * @param string  Header value
   * @param string  Fallback charset if none specified
   * @param string $input    Header value
   * @param string $fallback Fallback charset if none specified
   *
   * @return string Decoded string
   * @static
   */
  function decode_mime_string($input, $fallback=null)
  public static function decode_mime_string($input, $fallback=null)
    {
    // Initialize variable
    $out = '';
@@ -2692,32 +2720,43 @@
        $folders[$folder] = rc_strtolower(rcube_charset_convert($folder, 'UTF-7'));
      }
    // sort folders and place defaults on the top
    asort($folders, SORT_LOCALE_STRING);
    ksort($a_defaults);
    $folders = array_merge($a_defaults, array_keys($folders));
    // finally we must rebuild the list to move 
    // subfolders of default folders to their place...
    // ...also do this for the rest of folders because
    // asort() is not properly sorting case sensitive names
    // set the type of folder name variable (#1485527)
    while (list($key, $folder) = each($folders)) {
      // set the type of folder name variable (#1485527)
      $a_out[] = (string) $folder;
      unset($folders[$key]);
      foreach ($folders as $idx => $f) {
   if (strpos($f, $folder.$delimiter) === 0) {
         $a_out[] = (string) $f;
     unset($folders[$idx]);
     }
        }
      reset($folders);
      $this->_rsort($folder, $delimiter, $folders, $a_out);
      }
    return $a_out;
    }
  /**
   * @access private
   */
  function _rsort($folder, $delimiter, &$list, &$out)
    {
      while (list($key, $name) = each($list)) {
   if (strpos($name, $folder.$delimiter) === 0) {
     // set the type of folder name variable (#1485527)
         $out[] = (string) $name;
     unset($list[$key]);
     $this->_rsort($name, $delimiter, $list, $out);
     }
        }
      reset($list);
    }
  /**
   * @access private
   */