thomascube
2006-05-18 cead5c727147faac362e742aa7bcecf07f68cd99
program/include/rcube_imap.inc
@@ -21,11 +21,24 @@
*/
/**
 * Obtain classes from the Iloha IMAP library
 */
require_once('lib/imap.inc');
require_once('lib/mime.inc');
require_once('lib/utf7.inc');
/**
 * Interface class for accessing an IMAP server
 *
 * This is a wrapper that implements the Iloha IMAP Library (IIL)
 *
 * @package    RoundCube Webmail
 * @author     Thomas Bruederli <roundcube@gmail.com>
 * @version    1.22
 * @link       http://ilohamail.org
 */
class rcube_imap
  {
  var $db;
@@ -42,42 +55,64 @@
  var $default_folders = array('inbox', 'drafts', 'sent', 'junk', 'trash');
  var $cache = array();
  var $cache_keys = array();  
  var $cache_changes = array();
  var $cache_changes = array();
  var $uid_id_map = array();
  var $msg_headers = array();
  var $capabilities = array();
  var $skip_deleted = FALSE;
  var $debug_level = 1;
  // PHP 5 constructor
  /**
   * Object constructor
   *
   * @param  object  Database connection
   */
  function __construct($db_conn)
    {
    $this->db = $db_conn;
    $this->db = $db_conn;
    }
  // PHP 4 compatibility
  /**
   * PHP 4 object constructor
   *
   * @see  rcube_imap::__construct
   */
  function rcube_imap($db_conn)
    {
    $this->__construct($db_conn);
    }
  /**
   * Connect to an IMAP server
   *
   * @param  string   Host to connect
   * @param  string   Username for IMAP account
   * @param  string   Password for IMAP account
   * @param  number   Port to connect to
   * @param  boolean  Use SSL connection
   * @return boolean  TRUE on success, FALSE on failure
   * @access public
   */
  function connect($host, $user, $pass, $port=143, $use_ssl=FALSE)
    {
    global $ICL_SSL, $ICL_PORT, $CONFIG;
    global $ICL_SSL, $ICL_PORT, $IMAP_USE_INTERNAL_DATE;
    
    // check for Open-SSL support in PHP build
    if ($use_ssl && in_array('openssl', get_loaded_extensions()))
      $ICL_SSL = TRUE;
    else if ($use_ssl)
      {
      raise_error(array('code' => 403,
                        'type' => 'imap',
                        'file' => __FILE__,
      raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__,
                        'message' => 'Open SSL not available;'), TRUE, FALSE);
      $port = 143;
      }
    $ICL_PORT = $port;
    $IMAP_USE_INTERNAL_DATE = false;
    $this->conn = iil_Connect($host, $user, $pass, array('imap' => 'check'));
    $this->host = $host;
    $this->user = $user;
@@ -86,7 +121,7 @@
    $this->ssl = $use_ssl;
    
    // print trace mesages
    if ($this->conn && ($CONFIG['debug_level'] & 8))
    if ($this->conn && ($this->debug_level & 8))
      console($this->conn->message);
    
    // write error log
@@ -116,6 +151,12 @@
    }
  /**
   * Close IMAP connection
   * Usually done on script shutdown
   *
   * @access public
   */
  function close()
    {    
    if ($this->conn)
@@ -123,6 +164,12 @@
    }
  /**
   * Close IMAP connection and re-connect
   * This is used to avoid some strange socket errors when talking to Courier IMAP
   *
   * @access public
   */
  function reconnect()
    {
    $this->close();
@@ -130,6 +177,15 @@
    }
  /**
   * Set a root folder for the IMAP connection.
   *
   * Only folders within this root folder will be displayed
   * and all folder paths will be translated using this folder name
   *
   * @param  string   Root folder
   * @access public
   */
  function set_rootdir($root)
    {
    if (ereg('[\.\/]$', $root)) //(substr($root, -1, 1)==='/')
@@ -142,6 +198,12 @@
    }
  /**
   * This list of folders will be listed above all other folders
   *
   * @param  array  Indexed list of folder names
   * @access public
   */
  function set_default_mailboxes($arr)
    {
    if (is_array($arr))
@@ -149,8 +211,8 @@
      $this->default_folders = array();
      
      // add mailbox names lower case
      foreach ($arr as $mbox)
        $this->default_folders[] = strtolower($mbox);
      foreach ($arr as $mbox_row)
        $this->default_folders[] = strtolower($mbox_row);
      
      // add inbox if not included
      if (!in_array('inbox', $this->default_folders))
@@ -159,9 +221,17 @@
    }
  function set_mailbox($mbox)
  /**
   * Set internal mailbox reference.
   *
   * All operations will be perfomed on this mailbox/folder
   *
   * @param  string  Mailbox/Folder name
   * @access public
   */
  function set_mailbox($new_mbox)
    {
    $mailbox = $this->_mod_mailbox($mbox);
    $mailbox = $this->_mod_mailbox($new_mbox);
    if ($this->mailbox == $mailbox)
      return;
@@ -173,24 +243,49 @@
    }
  /**
   * Set internal list page
   *
   * @param  number  Page number to list
   * @access public
   */
  function set_page($page)
    {
    $this->list_page = (int)$page;
    }
  /**
   * Set internal page size
   *
   * @param  number  Number of messages to display on one page
   * @access public
   */
  function set_pagesize($size)
    {
    $this->page_size = (int)$size;
    }
  /**
   * Returns the currently used mailbox name
   *
   * @return  string Name of the mailbox/folder
   * @access  public
   */
  function get_mailbox_name()
    {
    return $this->conn ? $this->_mod_mailbox($this->mailbox, 'out') : '';
    }
  /**
   * Returns the IMAP server's capability
   *
   * @param   string  Capability name
   * @return  mixed   Capability value or TRUE if supported, FALSE if not
   * @access  public
   */
  function get_capability($cap)
    {
    $cap = strtoupper($cap);
@@ -198,6 +293,12 @@
    }
  /**
   * Returns the delimiter that is used by the IMAP server for folder separation
   *
   * @return  string  Delimiter string
   * @access  public
   */
  function get_hierarchy_delimiter()
    {
    if ($this->conn && empty($this->delimiter))
@@ -209,16 +310,25 @@
    return $this->delimiter;
    }
  // public method for mailbox listing
  // convert mailbox name with root dir first
  /**
   * Public method for mailbox listing.
   *
   * Converts mailbox name with root dir first
   *
   * @param   string  Optional root folder
   * @param   string  Optional filter for mailbox listing
   * @return  array   List of mailboxes/folders
   * @access  public
   */
  function list_mailboxes($root='', $filter='*')
    {
    $a_out = array();
    $a_mboxes = $this->_list_mailboxes($root, $filter);
    foreach ($a_mboxes as $mbox)
    foreach ($a_mboxes as $mbox_row)
      {
      $name = $this->_mod_mailbox($mbox, 'out');
      $name = $this->_mod_mailbox($mbox_row, 'out');
      if (strlen($name))
        $a_out[] = $name;
      }
@@ -229,7 +339,14 @@
    return $a_out;
    }
  // private method for mailbox listing
  /**
   * Private method for mailbox listing
   *
   * @return  array   List of mailboxes/folders
   * @access  private
   * @see     rcube_imap::list_mailboxes
   */
  function _list_mailboxes($root='', $filter='*')
    {
    $a_defaults = $a_out = array();
@@ -261,20 +378,34 @@
    }
  // get message count for a specific mailbox; acceptes modes are: ALL, UNSEEN
  function messagecount($mbox='', $mode='ALL', $force=FALSE)
  /**
   * Get message count for a specific mailbox
   *
   * @param   string   Mailbox/folder name
   * @param   string   Mode for count [ALL|UNSEEN|RECENT]
   * @param   boolean  Force reading from server and update cache
   * @return  number   Number of messages
   * @access  public
   */
  function messagecount($mbox_name='', $mode='ALL', $force=FALSE)
    {
    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
    $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    return $this->_messagecount($mailbox, $mode, $force);
    }
  // private method for getting nr of mesages
  /**
   * 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 (!$mailbox)
    if (empty($mailbox))
      $mailbox = $this->mailbox;
    $a_mailbox_cache = $this->get_cache('messagecount');
@@ -282,25 +413,40 @@
    // return cached value
    if (!$force && is_array($a_mailbox_cache[$mailbox]) && isset($a_mailbox_cache[$mailbox][$mode]))
      return $a_mailbox_cache[$mailbox][$mode];
    $search_str = "ALL UNDELETED";
    // get message count and store in cache
    if ($mode == 'UNSEEN')
      $search_str .= " UNSEEN";
    // RECENT count is fetched abit different
    if ($mode == 'RECENT')
       $count = iil_C_CheckForRecent($this->conn, $mailbox);
    // 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))
    // use SEARCH for message counting
    else if ($this->skip_deleted)
      {
      $str = implode(",", $index);
      if (!empty($str))
        $count = count($index);
      $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]))
    if (!is_array($a_mailbox_cache[$mailbox]))
      $a_mailbox_cache[$mailbox] = array();
      
    $a_mailbox_cache[$mailbox][$mode] = (int)$count;
@@ -312,16 +458,30 @@
    }
  // public method for listing headers
  // convert mailbox name with root dir first
  function list_headers($mbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL)
  /**
   * Public method for listing headers
   * convert mailbox name with root dir first
   *
   * @param   string   Mailbox/folder name
   * @param   number   Current page to list
   * @param   string   Header field to sort by
   * @param   string   Sort order [ASC|DESC]
   * @return  array    Indexed array with message header objects
   * @access  public
   */
  function list_headers($mbox_name='', $page=NULL, $sort_field=NULL, $sort_order=NULL)
    {
    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
    $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    return $this->_list_headers($mailbox, $page, $sort_field, $sort_order);
    }
  // private method for listing message header
  /**
   * Private method for listing message headers
   *
   * @access  private
   * @see     rcube_imap::list_headers
   */
  function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE)
    {
    if (!strlen($mailbox))
@@ -335,12 +495,15 @@
    $max = $this->_messagecount($mailbox);
    $start_msg = ($this->list_page-1) * $this->page_size;
    
    list($begin, $end) = $this->_get_message_range($max, $page);
    /*
    if ($page=='all')
      {
      $begin = 0;
      $end = $max;
      }
    else if ($this->sort_order=='DESC')
    else if (!$this->get_capability('sort') && $this->sort_order=='DESC')
      {
      $begin = $max - $this->page_size - $start_msg;
      $end =   $max - $start_msg;
@@ -348,13 +511,14 @@
    else
      {
      $begin = $start_msg;
      $end =   $start_msg + $this->page_size;
      $end   = $start_msg + $this->page_size;
      }
    if ($begin < 0) $begin = 0;
    if ($end < 0) $end = $max;
    if ($end > $max) $end = $max;
   */
//console("fetch headers $start_msg to ".($start_msg+$this->page_size)." (msg $begin to $end)");
    $headers_sorted = FALSE;
@@ -372,18 +536,13 @@
    else
      {
      // retrieve headers from IMAP
      if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field)))
      if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')))
        {
//console("$mailbox: ".count($msg_index));
//console("$mailbox: ".join(',', $msg_index));
        
        $msgs = $msg_index[$begin];
        for ($i=$begin; $i < $end; $i++)
          {
          if ($this->sort_order == 'DESC')
            $msgs = $msg_index[$i].','.$msgs;
          else
            $msgs = $msgs.','.$msg_index[$i];
          }
        for ($i=$begin+1; $i < $end; $i++)
          $msgs = $msgs.','.$msg_index[$i];
        $sorted = TRUE;
        }
@@ -401,44 +560,15 @@
        return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE);
        }      
      // cache is incomplete
      $cache_index = $this->get_message_cache_index($cache_key);
        
      // fetch reuested headers from server
      $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs);
      $a_msg_headers = array();
      $deleted_count = 0;
      if (!empty($a_header_index))
        {
        foreach ($a_header_index as $i => $headers)
          {
          if ($headers->deleted)
            {
            // delete from cache
            if ($cache_index[$headers->id] && $cache_index[$headers->id] == $headers->uid)
              $this->remove_message_cache($cache_key, $headers->id);
            $deleted_count++;
            continue;
            }
          // add message to cache
          if ($this->caching_enabled && $cache_index[$headers->id] != $headers->uid)
            $this->add_message_cache($cache_key, $headers->id, $headers);
          $a_msg_headers[$headers->uid] = $headers;
          }
        }
      $deleted_count = $this->_fetch_headers($mailbox, $msgs, $a_msg_headers, $cache_key);
      // delete cached messages with a higher index than $max
      $this->clear_message_cache($cache_key, $max);
      // fetch more headers of there were any deleted messages
      // ...
      // kick child process to sync cache
      // ...
      
@@ -451,22 +581,168 @@
    // if not already sorted
    if (!$headers_sorted)
      $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
//    if (!$headers_sorted)
//      $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
      if (!$headers_sorted && $this->sort_order == 'DESC')
        $a_msg_headers = array_reverse($a_msg_headers);
    return array_values($a_msg_headers);
    }
  /**
   * Public method for listing a specific set of headers
   * convert mailbox name with root dir first
   *
   * @param   string   Mailbox/folder name
   * @param   array    List of message ids to list
   * @param   number   Current page to list
   * @param   string   Header field to sort by
   * @param   string   Sort order [ASC|DESC]
   * @return  array    Indexed array with message header objects
   * @access  public
   */
  function list_header_set($mbox_name='', $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL)
    {
    $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    return $this->_list_header_set($mailbox, $msgs, $page, $sort_field, $sort_order);
    }
  /**
   * Private method for listing a set of message headers
   *
   * @access  private
   * @see     rcube_imap::list_header_set
   */
  function _list_header_set($mailbox, $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL)
    {
    // also accept a comma-separated list of message ids
    if (is_string($msgs))
      $msgs = split(',', $msgs);
    if (!strlen($mailbox) || empty($msgs))
      return array();
    if ($sort_field!=NULL)
      $this->sort_field = $sort_field;
    if ($sort_order!=NULL)
      $this->sort_order = strtoupper($sort_order);
    $max = count($msgs);
    $start_msg = ($this->list_page-1) * $this->page_size;
    // fetch reuested headers from server
    $a_msg_headers = array();
    $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($max-$start_msg, $this->page_size));
    }
  /**
   * Helper function to get first and last index of the requested set
   *
   * @param  number  message count
   * @param  mixed   page number to show, or string 'all'
   * @return array   array with two values: first index, last index
   * @access private
   */
  function _get_message_range($max, $page)
    {
    $start_msg = ($this->list_page-1) * $this->page_size;
    if ($page=='all')
      {
      $begin = 0;
      $end = $max;
      }
    else if ($this->sort_order=='DESC')
      {
      $begin = $max - $this->page_size - $start_msg;
      $end =   $max - $start_msg;
      }
    else
      {
      $begin = $start_msg;
      $end   = $start_msg + $this->page_size;
      }
    if ($begin < 0) $begin = 0;
    if ($end < 0) $end = $max;
    if ($end > $max) $end = $max;
    return array($begin, $end);
    }
  /**
   * Fetches message headers
   * Used for loop
   *
   * @param  string  Mailbox name
   * @param  string  Message index to fetch
   * @param  array   Reference to message headers array
   * @param  array   Array with cache index
   * @return number  Number of deleted messages
   * @access private
   */
  function _fetch_headers($mailbox, $msgs, &$a_msg_headers, $cache_key)
    {
    // cache is incomplete
    $cache_index = $this->get_message_cache_index($cache_key);
    // fetch reuested headers from server
    $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs);
    $deleted_count = 0;
    if (!empty($a_header_index))
      {
      foreach ($a_header_index as $i => $headers)
        {
        if ($headers->deleted && $this->skip_deleted)
          {
          // delete from cache
          if ($cache_index[$headers->id] && $cache_index[$headers->id] == $headers->uid)
            $this->remove_message_cache($cache_key, $headers->id);
          $deleted_count++;
          continue;
          }
        // add message to cache
        if ($this->caching_enabled && $cache_index[$headers->id] != $headers->uid)
          $this->add_message_cache($cache_key, $headers->id, $headers);
        $a_msg_headers[$headers->uid] = $headers;
        }
      }
    return $deleted_count;
    }
  // return sorted array of message UIDs
  function message_index($mbox='', $sort_field=NULL, $sort_order=NULL)
  function message_index($mbox_name='', $sort_field=NULL, $sort_order=NULL)
    {
    if ($sort_field!=NULL)
      $this->sort_field = $sort_field;
    if ($sort_order!=NULL)
      $this->sort_order = strtoupper($sort_order);
    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
    $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    $key = "$mbox:".$this->sort_field.":".$this->sort_order.".msgi";
    // have stored it in RAM
@@ -480,24 +756,20 @@
    // cache is OK
    if ($cache_status>0)
      {
      $a_index = $this->get_message_cache_index($cache_key, FALSE, $this->sort_field);
      $a_index = $this->get_message_cache_index($cache_key, TRUE, $this->sort_field, $this->sort_order);
      return array_values($a_index);
      }
    // fetch complete message index
    $msg_count = $this->_messagecount($mailbox);
    if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field)))
    if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, '', TRUE)))
      {
      $a_uids = iil_C_FetchUIDs($this->conn, $mailbox);
      if ($this->sort_order == 'DESC')
        $a_index = array_reverse($a_index);
      $i = 0;
      $this->cache[$key] = array();
      foreach ($a_index as $index => $value)
        $this->cache[$key][$i++] = $a_uids[$value];
      $this->cache[$key] = $a_index;
      }
    else
      {
@@ -570,29 +842,59 @@
    }
  function search($mbox='', $criteria='ALL')
  /**
   * Invoke search request to IMAP server
   *
   * @param  string  mailbox name to search in
   * @param  string  search criteria (ALL, TO, FROM, SUBJECT, etc)
   * @param  string  search string
   * @return array   search results as list of message ids
   * @access public
   */
  function search($mbox_name='', $criteria='ALL', $str=NULL)
    {
    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
    return $this->_search_index($mailbox, $criteria);
    }
    $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    if ($str && $criteria)
      {
      $criteria = 'CHARSET UTF-8 '.$criteria.' "'.UTF7EncodeString($str).'"';
      return $this->_search_index($mailbox, $criteria);
      }
    else
      return $this->_search_index($mailbox, $criteria);
    }
  /**
   * Private search method
   *
   * @return array   search results as list of message ids
   * @access private
   * @see rcube_imap::search()
   */
  function _search_index($mailbox, $criteria='ALL')
    {
    $a_messages = iil_C_Search($this->conn, $mailbox, $criteria);
    // clean message list (there might be some empty entries)
    if (is_array($a_messages))
      {
      foreach ($a_messages as $i => $val)
        if (empty($val))
          unset($a_messages[$i]);
      }
    return $a_messages;
    }
  function get_headers($uid, $mbox=NULL)
  function get_headers($id, $mbox_name=NULL, $is_uid=TRUE)
    {
    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
    $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    // get cached headers
    if ($headers = $this->get_cached_message($mailbox.'.msg', $uid))
    if ($is_uid && ($headers = $this->get_cached_message($mailbox.'.msg', $id)))
      return $headers;
    $msg_id = $this->_uid2id($uid);
    $msg_id = $is_uid ? $this->_uid2id($id) : $id;
    $headers = iil_C_FetchHeader($this->conn, $mailbox, $msg_id);
    // write headers cache
@@ -634,18 +936,21 @@
  // set message flag to one or several messages
  // possible flgs are: SEEN, DELETED, RECENT, ANSWERED, DRAFT
  // possible flags are: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT
  function set_flag($uids, $flag)
    {
    $flag = strtoupper($flag);
    $msg_ids = array();
    if (!is_array($uids))
      $uids = array($uids);
      $uids = explode(',',$uids);
      
    foreach ($uids as $uid)
    foreach ($uids as $uid) {
      $msg_ids[$uid] = $this->_uid2id($uid);
    }
      
    if ($flag=='UNSEEN')
    if ($flag=='UNDELETED')
      $result = iil_C_Undelete($this->conn, $this->mailbox, join(',', array_values($msg_ids)));
    else if ($flag=='UNSEEN')
      $result = iil_C_Unseen($this->conn, $this->mailbox, join(',', array_values($msg_ids)));
    else
      $result = iil_C_Flag($this->conn, $this->mailbox, join(',', array_values($msg_ids)), $flag);
@@ -684,11 +989,11 @@
  // append a mail message (source) to a specific mailbox
  function save_message($mbox, $message)
  function save_message($mbox_name, $message)
    {
    $mailbox = $this->_mod_mailbox($mbox);
    $mailbox = $this->_mod_mailbox($mbox_name);
    // make shure mailbox exists
    // make sure mailbox exists
    if (in_array($mailbox, $this->_list_mailboxes()))
      $saved = iil_C_Append($this->conn, $mailbox, $message);
@@ -708,10 +1013,15 @@
    $to_mbox = $this->_mod_mailbox($to_mbox);
    $from_mbox = $from_mbox ? $this->_mod_mailbox($from_mbox) : $this->mailbox;
    // make shure mailbox exists
    // make sure mailbox exists
    if (!in_array($to_mbox, $this->_list_mailboxes()))
      return FALSE;
      {
      if (in_array(strtolower($to_mbox), $this->default_folders))
        $this->create_mailbox($to_mbox, TRUE);
      else
        return FALSE;
      }
    // convert the list of uids to array
    $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL);
    
@@ -755,9 +1065,9 @@
  // mark messages as deleted and expunge mailbox
  function delete_message($uids, $mbox='')
  function delete_message($uids, $mbox_name='')
    {
    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
    $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    // convert the list of uids to array
    $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL);
@@ -802,15 +1112,25 @@
  // clear all messages in a specific mailbox
  function clear_mailbox($mbox)
  function clear_mailbox($mbox_name=NULL)
    {
    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
    $mailbox = !empty($mbox_name) ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    $msg_count = $this->_messagecount($mailbox, 'ALL');
    
    if ($msg_count>0)
      {
      $this->clear_message_cache($mailbox.'.msg');
      return iil_C_ClearFolder($this->conn, $mailbox);
      $cleared = iil_C_ClearFolder($this->conn, $mailbox);
      // make sure the message count cache is cleared as well
      if ($cleared)
        {
        $this->clear_message_cache($mailbox.'.msg');
        $a_mailbox_cache = $this->get_cache('messagecount');
        unset($a_mailbox_cache[$mailbox]);
        $this->update_cache('messagecount', $a_mailbox_cache);
        }
      return $cleared;
      }
    else
      return 0;
@@ -818,9 +1138,9 @@
  // send IMAP expunge command and clear cache
  function expunge($mbox='', $clear_cache=TRUE)
  function expunge($mbox_name='', $clear_cache=TRUE)
    {
    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
    $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
    return $this->_expunge($mailbox, $clear_cache);
    }
@@ -857,9 +1177,9 @@
    $a_mboxes = iil_C_ListMailboxes($this->conn, $this->_mod_mailbox($root), '*');
    // modify names with root dir
    foreach ($a_mboxes as $mbox)
    foreach ($a_mboxes as $mbox_name)
      {
      $name = $this->_mod_mailbox($mbox, 'out');
      $name = $this->_mod_mailbox($mbox_name, 'out');
      if (strlen($name))
        $a_folders[] = $name;
      }
@@ -870,13 +1190,30 @@
    }
  // subscribe to a specific mailbox(es)
  function subscribe($mbox, $mode='subscribe')
  /**
   * Get quota
   * added by Nuny
   */
  function get_quota()
    {
    if (is_array($mbox))
      $a_mboxes = $mbox;
    else if (is_string($mbox) && strlen($mbox))
      $a_mboxes = explode(',', $mbox);
    if ($this->get_capability('QUOTA'))
      {
      $result = iil_C_GetQuota($this->conn);
      if ($result["total"])
        return sprintf("%.2fMB / %.2fMB (%.0f%%)", $result["used"] / 1000.0, $result["total"] / 1000.0, $result["percent"]);
      }
    return FALSE;
    }
  // subscribe to a specific mailbox(es)
  function subscribe($mbox_name, $mode='subscribe')
    {
    if (is_array($mbox_name))
      $a_mboxes = $mbox_name;
    else if (is_string($mbox_name) && strlen($mbox_name))
      $a_mboxes = explode(',', $mbox_name);
    
    // let this common function do the main work
    return $this->_change_subscription($a_mboxes, 'subscribe');
@@ -884,12 +1221,12 @@
  // unsubscribe mailboxes
  function unsubscribe($mbox)
  function unsubscribe($mbox_name)
    {
    if (is_array($mbox))
      $a_mboxes = $mbox;
    else if (is_string($mbox) && strlen($mbox))
      $a_mboxes = explode(',', $mbox);
    if (is_array($mbox_name))
      $a_mboxes = $mbox_name;
    else if (is_string($mbox_name) && strlen($mbox_name))
      $a_mboxes = explode(',', $mbox_name);
    // let this common function do the main work
    return $this->_change_subscription($a_mboxes, 'unsubscribe');
@@ -924,26 +1261,26 @@
  // set a new name to an existing mailbox
  function rename_mailbox($mbox, $new_name)
  function rename_mailbox($mbox_name, $new_name)
    {
    // not implemented yet
    }
  // remove mailboxes from server
  function delete_mailbox($mbox)
  function delete_mailbox($mbox_name)
    {
    $deleted = FALSE;
    if (is_array($mbox))
      $a_mboxes = $mbox;
    else if (is_string($mbox) && strlen($mbox))
      $a_mboxes = explode(',', $mbox);
    if (is_array($mbox_name))
      $a_mboxes = $mbox_name;
    else if (is_string($mbox_name) && strlen($mbox_name))
      $a_mboxes = explode(',', $mbox_name);
    if (is_array($a_mboxes))
      foreach ($a_mboxes as $mbox)
      foreach ($a_mboxes as $mbox_name)
        {
        $mailbox = $this->_mod_mailbox($mbox);
        $mailbox = $this->_mod_mailbox($mbox_name);
        // unsubscribe mailbox before deleting
        iil_C_UnSubscribe($this->conn, $mailbox);
@@ -1147,14 +1484,14 @@
      $header = iil_C_FetchHeader($this->conn, $mailbox, "$msg_count");
      $cache_uid = array_pop($cache_index);
      
      // uids of highes message matches -> cache seems OK
      // uids of highest message matches -> cache seems OK
      if ($cache_uid == $header->uid)
        return 1;
      // cache is dirty
      return -1;
      }
    // if cache count differs less that 10% report as dirty
    // if cache count differs less than 10% report as dirty
    else if (abs($msg_count - $cache_count) < $msg_count/10)
      return -1;
    else
@@ -1233,9 +1570,13 @@
    }
   
  function get_message_cache_index($key, $force=FALSE, $sort_col='idx')
  function get_message_cache_index($key, $force=FALSE, $sort_col='idx', $sort_order='ASC')
    {
    static $sa_message_index = array();
    // empty key -> empty array
    if (empty($key))
      return array();
    
    if (!empty($sa_message_index[$key]) && !$force)
      return $sa_message_index[$key];
@@ -1246,7 +1587,7 @@
       FROM ".get_table_name('messages')."
       WHERE  user_id=?
       AND    cache_key=?
       ORDER BY ".$sort_col." ASC",
       ORDER BY ".$this->db->quote_identifier($sort_col)." ".$sort_order,
      $_SESSION['user_id'],
      $key);
@@ -1259,21 +1600,21 @@
  function add_message_cache($key, $index, $headers)
    {
    if (!is_object($headers) || empty($headers->uid))
    if (!$key || !is_object($headers) || empty($headers->uid))
      return;
    $this->db->query(
      "INSERT INTO ".get_table_name('messages')."
       (user_id, del, cache_key, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers)
       VALUES (?, 0, ?, ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?)",
       (user_id, del, cache_key, created, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers)
       VALUES (?, 0, ?, now(), ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?)",
      $_SESSION['user_id'],
      $key,
      $index,
      $headers->uid,
      $this->decode_header($headers->subject, TRUE),
      $this->decode_header($headers->from, TRUE),
      $this->decode_header($headers->to, TRUE),
      $this->decode_header($headers->cc, TRUE),
      (string)substr($this->decode_header($headers->subject, TRUE), 0, 128),
      (string)substr($this->decode_header($headers->from, TRUE), 0, 128),
      (string)substr($this->decode_header($headers->to, TRUE), 0, 128),
      (string)substr($this->decode_header($headers->cc, TRUE), 0, 128),
      (int)$headers->size,
      serialize($headers));
    }
@@ -1399,10 +1740,10 @@
        $rest = quoted_printable_decode($rest);
        }
      return decode_specialchars($rest, $a[0]);
      return rcube_charset_convert($rest, $a[0]);
      }
    else
      return $str;    //we dont' know what to do with this
      return $str;    // we dont' know what to do with this
    }
@@ -1450,7 +1791,7 @@
  function charset_decode($body, $ctype_param)
    {
    if (is_array($ctype_param) && !empty($ctype_param['charset']))
      return decode_specialchars($body, $ctype_param['charset']);
      return rcube_charset_convert($body, $ctype_param['charset']);
    return $body;
    }
@@ -1462,17 +1803,17 @@
   * --------------------------------*/
  function _mod_mailbox($mbox, $mode='in')
  function _mod_mailbox($mbox_name, $mode='in')
    {
    if ((!empty($this->root_ns) && $this->root_ns == $mbox) || ($mbox == 'INBOX' && $mode == 'in'))
      return $mbox;
    if ((!empty($this->root_ns) && $this->root_ns == $mbox_name) || ($mbox_name == 'INBOX' && $mode == 'in'))
      return $mbox_name;
    if (!empty($this->root_dir) && $mode=='in') 
      $mbox = $this->root_dir.$this->delimiter.$mbox;
      $mbox_name = $this->root_dir.$this->delimiter.$mbox_name;
    else if (strlen($this->root_dir) && $mode=='out') 
      $mbox = substr($mbox, strlen($this->root_dir)+1);
      $mbox_name = substr($mbox_name, strlen($this->root_dir)+1);
    return $mbox;
    return $mbox_name;
    }
@@ -1499,16 +1840,33 @@
    return array_merge($a_defaults, $a_out);
    }
  function _uid2id($uid, $mbox=NULL)
  function get_id($uid)
    {
    if (!$mbox)
      $mbox = $this->mailbox;
    if (!isset($this->uid_id_map[$mbox][$uid]))
      $this->uid_id_map[$mbox][$uid] = iil_C_UID2ID($this->conn, $mbox, $uid);
      return $this->_uid2id($uid);
    }
  function get_uid($id)
    {
      return $this->_id2uid($id);
    }
    return $this->uid_id_map[$mbox][$uid];
  function _uid2id($uid, $mbox_name=NULL)
    {
    if (!$mbox_name)
      $mbox_name = $this->mailbox;
    if (!isset($this->uid_id_map[$mbox_name][$uid]))
      $this->uid_id_map[$mbox_name][$uid] = iil_C_UID2ID($this->conn, $mbox_name, $uid);
    return $this->uid_id_map[$mbox_name][$uid];
    }
  function _id2uid($id, $mbox_name=NULL)
    {
    if (!$mbox_name)
      $mbox_name = $this->mailbox;
    return iil_C_ID2UID($this->conn, $mbox_name, $id);
    }
@@ -1545,9 +1903,9 @@
    $updated = FALSE;
    
    if (is_array($a_mboxes))
      foreach ($a_mboxes as $i => $mbox)
      foreach ($a_mboxes as $i => $mbox_name)
        {
        $mailbox = $this->_mod_mailbox($mbox);
        $mailbox = $this->_mod_mailbox($mbox_name);
        $a_mboxes[$i] = $mailbox;
        if ($mode=='subscribe')
@@ -1581,10 +1939,10 @@
  // increde/decrese messagecount for a specific mailbox
  function _set_messagecount($mbox, $mode, $increment)
  function _set_messagecount($mbox_name, $mode, $increment)
    {
    $a_mailbox_cache = FALSE;
    $mailbox = $mbox ? $mbox : $this->mailbox;
    $mailbox = $mbox_name ? $mbox_name : $this->mailbox;
    $mode = strtoupper($mode);
    $a_mailbox_cache = $this->get_cache('messagecount');
@@ -1607,10 +1965,10 @@
  // remove messagecount of a specific mailbox from cache
  function _clear_messagecount($mbox='')
  function _clear_messagecount($mbox_name='')
    {
    $a_mailbox_cache = FALSE;
    $mailbox = $mbox ? $mbox : $this->mailbox;
    $mailbox = $mbox_name ? $mbox_name : $this->mailbox;
    $a_mailbox_cache = $this->get_cache('messagecount');
@@ -1728,4 +2086,4 @@
  return trim($output);
  }
?>
?>