alecpl
2010-03-24 d8c440c03f3c66b93793c40e54d3c9329d9b5ea9
program/include/rcube_imap.php
@@ -110,7 +110,7 @@
    else if ($use_ssl) {
      raise_error(array('code' => 403, 'type' => 'imap',
        'file' => __FILE__, 'line' => __LINE__,
        'message' => "Open SSL not available"), TRUE, FALSE);
        'message' => "Open SSL not available"), true, false);
      $port = 143;
    }
@@ -142,7 +142,7 @@
      $this->error_code = $GLOBALS['iil_errornum'];
      raise_error(array('code' => 403, 'type' => 'imap',
        'file' => __FILE__, 'line' => __LINE__,
        'message' => $GLOBALS['iil_error']), TRUE, FALSE);
        'message' => $GLOBALS['iil_error']), true, false);
      }
    // get server properties
@@ -154,7 +154,7 @@
   $this->get_hierarchy_delimiter();
      }
    return $this->conn ? TRUE : FALSE;
    return $this->conn ? true : false;
    }
@@ -471,11 +471,10 @@
    if (isset($data['folders'])) {
        $a_folders = $data['folders'];
    }
    else{
    else {
        // retrieve list of folders from IMAP server
        $a_folders = iil_C_ListSubscribed($this->conn, $this->mod_mailbox($root), $filter);
    }
    
    if (!is_array($a_folders) || !sizeof($a_folders))
      $a_folders = array();
@@ -491,12 +490,12 @@
   * Get message count for a specific mailbox
   *
   * @param   string   Mailbox/folder name
   * @param   string   Mode for count [ALL|UNSEEN|RECENT]
   * @param   string   Mode for count [ALL|THREADS|UNSEEN|RECENT]
   * @param   boolean  Force reading from server and update cache
   * @return  int      Number of messages
   * @access  public
   */
  function messagecount($mbox_name='', $mode='ALL', $force=FALSE)
  function messagecount($mbox_name='', $mode='ALL', $force=false)
    {
    $mailbox = $mbox_name ? $this->mod_mailbox($mbox_name) : $this->mailbox;
    return $this->_messagecount($mailbox, $mode, $force);
@@ -509,7 +508,7 @@
   * @access  private
   * @see     rcube_imap::messagecount()
   */
  private function _messagecount($mailbox='', $mode='ALL', $force=FALSE)
  private function _messagecount($mailbox='', $mode='ALL', $force=false)
    {
    $mode = strtoupper($mode);
@@ -533,16 +532,16 @@
    if (!is_array($a_mailbox_cache[$mailbox]))
      $a_mailbox_cache[$mailbox] = array();
    if ($mode == 'THREADS')
      $count = $this->_threadcount($mailbox);
    if ($mode == 'THREADS') {
      $count = $this->_threadcount($mailbox, $msg_count);
      $_SESSION['maxuid'][$mailbox] = $msg_count ? $this->_id2uid($msg_count) : 0;
      }
    // RECENT count is fetched a bit different
    else if ($mode == 'RECENT')
    else if ($mode == 'RECENT') {
       $count = iil_C_CheckForRecent($this->conn, $mailbox);
      }
    // use SEARCH for message counting
    else if ($this->skip_deleted)
      {
    else if ($this->skip_deleted) {
      $search_str = "ALL UNDELETED";
      // get message count and store in cache
@@ -551,7 +550,12 @@
      // get message count using SEARCH
      // not very performant but more precise (using UNDELETED)
      // disable THREADS for this request
      $threads = $this->threading;
      $this->threading = false;
      $index = $this->_search_index($mailbox, $search_str);
      $this->threading = $threads;
      $count = is_array($index) ? count($index) : 0;
      if ($mode == 'ALL')
@@ -581,12 +585,14 @@
   * @access  private
   * @see     rcube_imap::messagecount()
   */
  private function _threadcount($mailbox)
  private function _threadcount($mailbox, &$msg_count)
    {
    if (!empty($this->icache['threads']))
      return count($this->icache['threads']['tree']);
    
    list ($thread_tree, $msg_depth, $has_children) = $this->_fetch_threads($mailbox);
    $msg_count = count($msg_depth);
//    $this->update_thread_cache($mailbox, $thread_tree, $msg_depth, $has_children);
    return count($thread_tree);  
@@ -618,7 +624,7 @@
   * @access  private
   * @see     rcube_imap::list_headers
   */
  private function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE, $slice=0)
  private function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=false, $slice=0)
    {
    if (!strlen($mailbox))
      return array();
@@ -650,7 +656,7 @@
    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);
      return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, true, $slice);
      }
    // retrieve headers from IMAP
@@ -682,7 +688,7 @@
          $this->_fetch_headers($mailbox, join(",", $msg_index), $a_msg_headers, $cache_key);
      }
    // use SORT command
    else if ($this->get_capability('sort'))
    else if ($this->get_capability('SORT'))
      {
      if ($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);
@@ -741,7 +747,7 @@
   * @access  private
   * @see     rcube_imap::list_headers
   */
  private function _list_thread_headers($mailbox, $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE, $slice=0)
  private function _list_thread_headers($mailbox, $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=false, $slice=0)
    {
    $this->_set_sort_order($sort_field, $sort_order);
@@ -925,7 +931,7 @@
      }
    // sorted messages, so we can first slice array and then fetch only wanted headers
    if ($this->get_capability('sort')) // SORT searching result
    if ($this->get_capability('SORT')) // SORT searching result
      {
      // reset search set if sorting field has been changed
      if ($this->sort_field && $this->search_sort_field != $this->sort_field)
@@ -1171,7 +1177,7 @@
          $this->cache[$key] = $msgs;
      }
      // sort with SORT command
      else if ($this->get_capability('sort'))
      else if ($this->get_capability('SORT'))
      {
        if ($this->sort_field && $this->search_sort_field != $this->sort_field)
          $this->search('', $this->search_string, $this->search_charset, $this->sort_field);
@@ -1206,7 +1212,7 @@
    // cache is OK
    if ($cache_status>0)
      {
      $a_index = $this->get_message_cache_index($cache_key, TRUE, $this->sort_field, $this->sort_order);
      $a_index = $this->get_message_cache_index($cache_key, true, $this->sort_field, $this->sort_order);
      return array_keys($a_index);
      }
@@ -1225,7 +1231,7 @@
      $this->cache[$key] = $a_index;
      }
    // fetch complete message index
    else if ($this->get_capability('sort'))
    else if ($this->get_capability('SORT'))
      {
      if ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')) {
        if ($this->sort_order == 'DESC')
@@ -1282,7 +1288,7 @@
    // cache is OK
    if ($cache_status>0)
      {
      $a_index = $this->get_message_cache_index($cache_key, TRUE, $this->sort_field, $this->sort_order);
      $a_index = $this->get_message_cache_index($cache_key, true, $this->sort_field, $this->sort_order);
      return array_keys($a_index);
      }
*/
@@ -1352,7 +1358,7 @@
        }
        
      // message in cache but in wrong position
      if (in_array((string)$uid, $cache_index, TRUE))
      if (in_array((string)$uid, $cache_index, true))
        {
        unset($cache_index[$id]);
        }
@@ -1459,9 +1465,9 @@
   'children' => $has_children
        );
      }
    else if ($sort_field && $this->get_capability('sort')) {
    else 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);
      $a_messages = iil_C_Sort($this->conn, $mailbox, $sort_field, $criteria, false, $charset);
      if (!$a_messages)
   return array();
@@ -1512,7 +1518,7 @@
    // here we'll implement REFS sorting, for performance reason
    else { // ($sort_field == 'date' && $this->threading != 'REFS')
      // use SORT command
      if ($this->get_capability('sort')) {
      if ($this->get_capability('SORT')) {
        $a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field,
       !empty($ids) ? $ids : ($this->skip_deleted ? 'UNDELETED' : ''));
@@ -1622,7 +1628,7 @@
   * @param boolean True if we need also BODYSTRUCTURE in headers
   * @return object Message headers representation
   */
  function get_headers($id, $mbox_name=NULL, $is_uid=TRUE, $bodystr=FALSE)
  function get_headers($id, $mbox_name=NULL, $is_uid=true, $bodystr=false)
    {
    $mailbox = $mbox_name ? $this->mod_mailbox($mbox_name) : $this->mailbox;
    $uid = $is_uid ? $id : $this->_id2uid($id);
@@ -2098,41 +2104,41 @@
  /**
   * Set message flag to one or several messages
   *
   * @param mixed  Message UIDs as array or as comma-separated string
   * @param string Flag to set: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT, MDNSENT
   * @param string Folder name
   * @param mixed   Message UIDs as array or comma-separated string, or '*'
   * @param string  Flag to set: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT, MDNSENT
   * @param string  Folder name
   * @param boolean True to skip message cache clean up
   * @return boolean True on success, False on failure
   * @return int    Number of flagged messages, -1 on failure
   */
  function set_flag($uids, $flag, $mbox_name=NULL, $skip_cache=false)
    {
    $mailbox = $mbox_name ? $this->mod_mailbox($mbox_name) : $this->mailbox;
    $flag = strtoupper($flag);
    if (!is_array($uids))
      $uids = explode(',',$uids);
    list($uids, $all_mode) = $this->_parse_uids($uids);
    if (strpos($flag, 'UN') === 0)
      $result = iil_C_UnFlag($this->conn, $mailbox, join(',', $uids), substr($flag, 2));
      $result = iil_C_UnFlag($this->conn, $mailbox, $uids, substr($flag, 2));
    else
      $result = iil_C_Flag($this->conn, $mailbox, join(',', $uids), $flag);
      $result = iil_C_Flag($this->conn, $mailbox, $uids, $flag);
    // reload message headers if cached
    if ($this->caching_enabled && !$skip_cache) {
      $cache_key = $mailbox.'.msg';
      $this->remove_message_cache($cache_key, $uids);
    if ($result >= 0) {
      // reload message headers if cached
      if ($this->caching_enabled && !$skip_cache) {
        $cache_key = $mailbox.'.msg';
        if ($all_mode)
          $this->clear_message_cache($cache_key);
        else
          $this->remove_message_cache($cache_key, explode(',', $uids));
        }
      // update counters
      if ($flag=='SEEN')
        $this->_set_messagecount($mailbox, 'UNSEEN', $result*(-1));
      else if ($flag=='UNSEEN')
        $this->_set_messagecount($mailbox, 'UNSEEN', $result);
      else if ($flag=='DELETED')
        $this->_set_messagecount($mailbox, 'ALL', $result*(-1));
      }
    // set nr of messages that were flaged
    $count = count($uids);
    // clear message count cache
    if ($result && $flag=='SEEN')
      $this->_set_messagecount($mailbox, 'UNSEEN', $count*(-1));
    else if ($result && $flag=='UNSEEN')
      $this->_set_messagecount($mailbox, 'UNSEEN', $count);
    else if ($result && $flag=='DELETED')
      $this->_set_messagecount($mailbox, 'ALL', $count*(-1));
    return $result;
    }
@@ -2141,10 +2147,10 @@
  /**
   * Remove message flag for one or several messages
   *
   * @param mixed  Message UIDs as array or as comma-separated string
   * @param mixed  Message UIDs as array or comma-separated string, or '*'
   * @param string Flag to unset: SEEN, DELETED, RECENT, ANSWERED, DRAFT, MDNSENT
   * @param string Folder name
   * @return boolean True on success, False on failure
   * @return int   Number of flagged messages, -1 on failure
   * @see set_flag
   */
  function unset_flag($uids, $flag, $mbox_name=NULL)
@@ -2168,7 +2174,7 @@
    $mailbox = $this->mod_mailbox($mbox_name);
    // make sure mailbox exists
    if (($mailbox == 'INBOX') || in_array($mailbox, $this->_list_mailboxes())) {
    if ($this->mailbox_exists($mbox_name, true)) {
      if ($is_file) {
        $separator = rcmail::get_instance()->config->header_delimiter();
        $saved = iil_C_AppendFromFile($this->conn, $mailbox, $message,
@@ -2183,7 +2189,7 @@
      // increase messagecount of the target mailbox
      $this->_set_messagecount($mailbox, 'ALL', 1);
      }
    return $saved;
    }
@@ -2191,7 +2197,7 @@
  /**
   * Move a message from one mailbox to another
   *
   * @param string List of UIDs to move, separated by comma
   * @param mixed  Message UIDs as array or comma-separated string, or '*'
   * @param string Target mailbox
   * @param string Source mailbox
   * @return boolean True on success, False on error
@@ -2203,21 +2209,20 @@
    $to_mbox = $this->mod_mailbox($to_mbox);
    $from_mbox = $from_mbox ? $this->mod_mailbox($from_mbox) : $this->mailbox;
    // make sure mailbox exists
    if ($to_mbox != 'INBOX' && !in_array($to_mbox, $this->_list_mailboxes()))
      {
      if (in_array($tbox, $this->default_folders))
        $this->create_mailbox($tbox, TRUE);
      else
        return FALSE;
      }
    // convert the list of uids to array
    $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL);
    list($uids, $all_mode) = $this->_parse_uids($uids);
    // exit if no message uids are specified
    if (!is_array($a_uids) || empty($a_uids))
    if (empty($uids))
      return false;
    // make sure mailbox exists
    if ($to_mbox != 'INBOX' && !$this->mailbox_exists($tbox, true))
      {
      if (in_array($tbox, $this->default_folders))
        $this->create_mailbox($tbox, true);
      else
        return false;
      }
    // flag messages as read before moving them
    $config = rcmail::get_instance()->config;
@@ -2227,19 +2232,19 @@
      }
    // move messages
    $iil_move = iil_C_Move($this->conn, join(',', $a_uids), $from_mbox, $to_mbox);
    $iil_move = iil_C_Move($this->conn, $uids, $from_mbox, $to_mbox);
    $moved = !($iil_move === false || $iil_move < 0);
    // send expunge command in order to have the moved message
    // really deleted from the source mailbox
    if ($moved) {
      $this->_expunge($from_mbox, FALSE, $a_uids);
      $this->_expunge($from_mbox, false, $uids);
      $this->_clear_messagecount($from_mbox);
      $this->_clear_messagecount($to_mbox);
    }
    // moving failed
    else if ($config->get('delete_always', false) && $tbox == $config->get('trash_mbox')) {
      $moved = $this->delete_message($a_uids, $fbox);
      $moved = $this->delete_message($uids, $fbox);
    }
    if ($moved) {
@@ -2249,10 +2254,11 @@
      // remove message ids from search set
      if ($this->search_set && $from_mbox == $this->mailbox) {
        // threads are too complicated to just remove messages from set
        if ($this->search_threads)
        if ($this->search_threads || $all_mode)
          $this->refresh_search();
        else {
          foreach ($a_uids as $uid)
          $uids = explode(',', $uids);
          foreach ($uids as $uid)
            $a_mids[] = $this->_uid2id($uid, $from_mbox);
          $this->search_set = array_diff($this->search_set, $a_mids);
          }
@@ -2260,9 +2266,9 @@
      // update cached message headers
      $cache_key = $from_mbox.'.msg';
      if ($start_index = $this->get_message_cache_index_min($cache_key, $a_uids)) {
      if ($all_mode || ($start_index = $this->get_message_cache_index_min($cache_key, $uids))) {
        // clear cache from the lowest index on
        $this->clear_message_cache($cache_key, $start_index);
        $this->clear_message_cache($cache_key, $all_mode ? 1 : $start_index);
       }
    }
@@ -2271,9 +2277,51 @@
  /**
   * Copy a message from one mailbox to another
   *
   * @param mixed  Message UIDs as array or comma-separated string, or '*'
   * @param string Target mailbox
   * @param string Source mailbox
   * @return boolean True on success, False on error
   */
  function copy_message($uids, $to_mbox, $from_mbox='')
  {
    $fbox = $from_mbox;
    $tbox = $to_mbox;
    $to_mbox = $this->mod_mailbox($to_mbox);
    $from_mbox = $from_mbox ? $this->mod_mailbox($from_mbox) : $this->mailbox;
    list($uids, $all_mode) = $this->_parse_uids($uids);
    // exit if no message uids are specified
    if (empty($uids))
      return false;
    // make sure mailbox exists
    if ($to_mbox != 'INBOX' && !$this->mailbox_exists($tbox, true))
      {
      if (in_array($tbox, $this->default_folders))
        $this->create_mailbox($tbox, true);
      else
        return false;
      }
    // copy messages
    $iil_copy = iil_C_Copy($this->conn, $uids, $from_mbox, $to_mbox);
    $copied = !($iil_copy === false || $iil_copy < 0);
    if ($copied) {
      $this->_clear_messagecount($to_mbox);
    }
    return $copied;
  }
  /**
   * Mark messages as deleted and expunge mailbox
   *
   * @param string List of UIDs to move, separated by comma
   * @param mixed  Message UIDs as array or comma-separated string, or '*'
   * @param string Source mailbox
   * @return boolean True on success, False on error
   */
@@ -2281,19 +2329,18 @@
  {
    $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);
    list($uids, $all_mode) = $this->_parse_uids($uids);
    // exit if no message uids are specified
    if (!is_array($a_uids) || empty($a_uids))
    if (empty($uids))
      return false;
    $deleted = iil_C_Delete($this->conn, $mailbox, join(',', $a_uids));
    $deleted = iil_C_Delete($this->conn, $mailbox, $uids);
    if ($deleted) {
      // send expunge command in order to have the deleted message
      // really deleted from the mailbox
      $this->_expunge($mailbox, FALSE, $a_uids);
      $this->_expunge($mailbox, false, $uids);
      $this->_clear_messagecount($mailbox);
      unset($this->uid_id_map[$mailbox]);
@@ -2303,10 +2350,11 @@
      // remove message ids from search set
      if ($this->search_set && $mailbox == $this->mailbox) {
        // threads are too complicated to just remove messages from set
        if ($this->search_threads)
        if ($this->search_threads || $all_mode)
          $this->refresh_search();
        else {
          foreach ($a_uids as $uid)
          $uids = explode(',', $uids);
          foreach ($uids as $uid)
            $a_mids[] = $this->_uid2id($uid, $mailbox);
          $this->search_set = array_diff($this->search_set, $a_mids);
          }
@@ -2314,9 +2362,9 @@
      // remove deleted messages from cache
      $cache_key = $mailbox.'.msg';
      if ($start_index = $this->get_message_cache_index_min($cache_key, $a_uids)) {
      if ($all_mode || ($start_index = $this->get_message_cache_index_min($cache_key, $uids))) {
        // clear cache from the lowest index on
        $this->clear_message_cache($cache_key, $start_index);
        $this->clear_message_cache($cache_key, $all_mode ? 1 : $start_index);
      }
    }
@@ -2362,7 +2410,7 @@
   * @param boolean False if cache should not be cleared
   * @return boolean True on success
   */
  function expunge($mbox_name='', $clear_cache=TRUE)
  function expunge($mbox_name='', $clear_cache=true)
    {
    $mailbox = $mbox_name ? $this->mod_mailbox($mbox_name) : $this->mailbox;
    return $this->_expunge($mailbox, $clear_cache);
@@ -2372,14 +2420,14 @@
  /**
   * Send IMAP expunge command and clear cache
   *
   * @see rcube_imap::expunge()
   * @param string    Mailbox name
   * @param boolean    False if cache should not be cleared
   * @param string    List of UIDs to remove, separated by comma
   * @param string     Mailbox name
   * @param boolean  False if cache should not be cleared
   * @param mixed    Message UIDs as array or comma-separated string, or '*'
   * @return boolean True on success
   * @access private
   * @see rcube_imap::expunge()
   */
  private function _expunge($mailbox, $clear_cache=TRUE, $uids=NULL)
  private function _expunge($mailbox, $clear_cache=true, $uids=NULL)
    {
    if ($uids && $this->get_capability('UIDPLUS')) 
      $a_uids = is_array($uids) ? join(',', $uids) : $uids;
@@ -2398,10 +2446,34 @@
    }
  /**
   * Parse message UIDs input
   *
   * @param mixed  UIDs array or comma-separated list or '*' or '1:*'
   * @return array Two elements array with UIDs converted to list and ALL flag
   * @access private
   */
  private function _parse_uids($uids)
    {
    if ($uids === '*' || $uids === '1:*') {
      $uids = '1:*';
      $all = true;
      }
    else {
      if (is_array($uids))
        $uids = join(',', $uids);
      if (preg_match('/[^0-9,]/', $uids))
        $uids = '';
      }
    return array($uids, (bool) $all);
    }
  /* --------------------------------
   *        folder managment
   * --------------------------------*/
  /**
   * Get a list of all folders available on the IMAP server
@@ -2444,7 +2516,7 @@
    if ($this->get_capability('QUOTA'))
      return iil_C_GetQuota($this->conn);
   
    return FALSE;
    return false;
    }
@@ -2487,24 +2559,20 @@
   * @param boolean True if the new mailbox should be subscribed
   * @param string  Name of the created mailbox, false on error
   */
  function create_mailbox($name, $subscribe=FALSE)
  function create_mailbox($name, $subscribe=false)
    {
    $result = FALSE;
    $result = false;
    
    // reduce mailbox name to 100 chars
    $name = substr($name, 0, 100);
    $abs_name = $this->mod_mailbox($name);
    $a_mailbox_cache = $this->get_cache('mailboxes');
    if (strlen($abs_name) && (!is_array($a_mailbox_cache) || !in_array($abs_name, $a_mailbox_cache)))
      $result = iil_C_CreateFolder($this->conn, $abs_name);
    $result = iil_C_CreateFolder($this->conn, $abs_name);
    // try to subscribe it
    if ($result && $subscribe)
      $this->subscribe($name);
    return $result ? $name : FALSE;
    return $result ? $name : false;
    }
@@ -2517,7 +2585,7 @@
   */
  function rename_mailbox($mbox_name, $new_name)
    {
    $result = FALSE;
    $result = false;
    // encode mailbox name and reduce it to 100 chars
    $name = substr($new_name, 0, 100);
@@ -2558,7 +2626,7 @@
    if ($result && $subscribed)
      iil_C_Subscribe($this->conn, $abs_name);
    return $result ? $name : FALSE;
    return $result ? $name : false;
    }
@@ -2570,19 +2638,19 @@
   */
  function delete_mailbox($mbox_name)
    {
    $deleted = FALSE;
    $deleted = false;
    if (is_array($mbox_name))
      $a_mboxes = $mbox_name;
    else if (is_string($mbox_name) && strlen($mbox_name))
      $a_mboxes = explode(',', $mbox_name);
    $all_mboxes = iil_C_ListMailboxes($this->conn, $this->mod_mailbox($root), '*');
    if (is_array($a_mboxes))
      foreach ($a_mboxes as $mbox_name)
        {
        $mailbox = $this->mod_mailbox($mbox_name);
        $sub_mboxes = iil_C_ListMailboxes($this->conn, $this->mod_mailbox(''),
     $mbox_name . $this->delimiter . '*');
        // unsubscribe mailbox before deleting
        iil_C_UnSubscribe($this->conn, $mailbox);
@@ -2590,23 +2658,18 @@
        // send delete command to server
        $result = iil_C_DeleteFolder($this->conn, $mailbox);
        if ($result >= 0) {
          $deleted = TRUE;
          $deleted = true;
          $this->clear_message_cache($mailbox.'.msg');
     }
     
        foreach ($all_mboxes as $c_mbox)
          {
          $regex = preg_quote($mailbox . $this->delimiter, '/');
          $regex = '/^' . $regex . '/';
          if (preg_match($regex, $c_mbox))
            {
        foreach ($sub_mboxes as $c_mbox)
          if ($c_mbox != 'INBOX') {
            iil_C_UnSubscribe($this->conn, $c_mbox);
            $result = iil_C_DeleteFolder($this->conn, $c_mbox);
            if ($result >= 0) {
              $deleted = TRUE;
              $deleted = true;
             $this->clear_message_cache($c_mbox.'.msg');
              }
       }
            }
          }
        }
@@ -2623,18 +2686,43 @@
   */
  function create_default_folders()
    {
    $a_folders = iil_C_ListMailboxes($this->conn, $this->mod_mailbox(''), '*');
    $a_subscribed = iil_C_ListSubscribed($this->conn, $this->mod_mailbox(''), '*');
    // create default folders if they do not exist
    foreach ($this->default_folders as $folder)
      {
      $abs_name = $this->mod_mailbox($folder);
      if (!in_array($abs_name, $a_folders))
        $this->create_mailbox($folder, TRUE);
      else if (!in_array($abs_name, $a_subscribed))
      if (!$this->mailbox_exists($folder))
        $this->create_mailbox($folder, true);
      else if (!$this->mailbox_exists($folder, true))
        $this->subscribe($folder);
      }
    }
  /**
   * Checks if folder exists and is subscribed
   *
   * @param string   Folder name
   * @param boolean  Enable subscription checking
   * @return boolean TRUE or FALSE
   */
  function mailbox_exists($mbox_name, $subscription=false)
    {
    if ($mbox_name) {
      if ($mbox_name == 'INBOX')
        return true;
      if ($subscription) {
        if ($a_folders = iil_C_ListSubscribed($this->conn, $this->mod_mailbox(''), $mbox_name))
          return true;
        }
      else {
        $a_folders = iil_C_ListMailboxes($this->conn, $this->mod_mailbox(''), $mbox_mbox);
   if (is_array($a_folders) && in_array($this->mod_mailbox($mbox_name), $a_folders))
          return true;
        }
      }
    return false;
    }
@@ -2649,9 +2737,9 @@
  function set_caching($set)
    {
    if ($set && is_object($this->db))
      $this->caching_enabled = TRUE;
      $this->caching_enabled = true;
    else
      $this->caching_enabled = FALSE;
      $this->caching_enabled = false;
    }
  /**
@@ -2674,8 +2762,8 @@
  function update_cache($key, $data)
    {
    $this->cache[$key] = $data;
    $this->cache_changed = TRUE;
    $this->cache_changes[$key] = TRUE;
    $this->cache_changed = true;
    $this->cache_changes[$key] = true;
    }
  /**
@@ -2707,13 +2795,13 @@
        $this->_clear_cache_record($key);
      $this->cache = array();
      $this->cache_changed = FALSE;
      $this->cache_changed = false;
      $this->cache_changes = array();
      }
    else
      {
      $this->_clear_cache_record($key);
      $this->cache_changes[$key] = FALSE;
      $this->cache_changes[$key] = false;
      unset($this->cache[$key]);
      }
    }
@@ -2738,7 +2826,7 @@
   $sql_key = preg_replace('/^IMAP\./', '', $sql_arr['cache_key']);
        $this->cache_keys[$sql_key] = $sql_arr['cache_id'];
   if (!isset($this->cache[$sql_key]))
     $this->cache[$sql_key] = $sql_arr['data'] ? unserialize($sql_arr['data']) : FALSE;
     $this->cache[$sql_key] = $sql_arr['data'] ? unserialize($sql_arr['data']) : false;
        }
      }
@@ -2751,7 +2839,7 @@
  private function _write_cache_record($key, $data)
    {
    if (!$this->db)
      return FALSE;
      return false;
    // update existing cache record
    if ($this->cache_keys[$key])
@@ -2946,7 +3034,7 @@
  /**
   * @access private
   */  
  private function get_message_cache_index($key, $force=FALSE, $sort_field='idx', $sort_order='ASC')
  private function get_message_cache_index($key, $force=false, $sort_field='idx', $sort_order='ASC')
    {
    static $sa_message_index = array();
    
@@ -3031,10 +3119,10 @@
        $key,
        $index,
        $headers->uid,
        (string)mb_substr($this->db->encode($this->decode_header($headers->subject, TRUE)), 0, 128),
        (string)mb_substr($this->db->encode($this->decode_header($headers->from, TRUE)), 0, 128),
        (string)mb_substr($this->db->encode($this->decode_header($headers->to, TRUE)), 0, 128),
        (string)mb_substr($this->db->encode($this->decode_header($headers->cc, TRUE)), 0, 128),
        (string)mb_substr($this->db->encode($this->decode_header($headers->subject, true)), 0, 128),
        (string)mb_substr($this->db->encode($this->decode_header($headers->from, true)), 0, 128),
        (string)mb_substr($this->db->encode($this->decode_header($headers->to, true)), 0, 128),
        (string)mb_substr($this->db->encode($this->decode_header($headers->cc, true)), 0, 128),
        (int)$headers->size,
        serialize($this->db->encode(clone $headers)),
        is_object($struct) ? serialize($this->db->encode(clone $struct)) : NULL
@@ -3083,6 +3171,13 @@
    if (!$this->caching_enabled)
      return;
    
    if (!empty($uids) && !is_array($uids)) {
      if ($uids == '*' || $uids == '1:*')
        $uids = NULL;
      else
        $uids = explode(',', $uids);
      }
    $sql_result = $this->db->query(
      "SELECT MIN(idx) AS minidx
      FROM ".get_table_name('messages')."
@@ -3193,7 +3288,7 @@
   * @param boolean Remove quotes if necessary
   * @return string Decoded string
   */
  function decode_header($input, $remove_quotes=FALSE)
  function decode_header($input, $remove_quotes=false)
    {
    $str = rcube_imap::decode_mime_string((string)$input, $this->default_charset);
    if ($str{0}=='"' && $remove_quotes)
@@ -3504,7 +3599,7 @@
   */
  private function _change_subscription($a_mboxes, $mode)
    {
    $updated = FALSE;
    $updated = false;
    if (is_array($a_mboxes))
      foreach ($a_mboxes as $i => $mbox_name)
@@ -3545,14 +3640,14 @@
   */
  private function _set_messagecount($mbox_name, $mode, $increment)
    {
    $a_mailbox_cache = FALSE;
    $a_mailbox_cache = false;
    $mailbox = $mbox_name ? $mbox_name : $this->mailbox;
    $mode = strtoupper($mode);
    $a_mailbox_cache = $this->get_cache('messagecount');
    
    if (!is_array($a_mailbox_cache[$mailbox]) || !isset($a_mailbox_cache[$mailbox][$mode]) || !is_numeric($increment))
      return FALSE;
      return false;
    
    // add incremental value to messagecount
    $a_mailbox_cache[$mailbox][$mode] += $increment;
@@ -3564,7 +3659,7 @@
    // write back to cache
    $this->update_cache('messagecount', $a_mailbox_cache);
    
    return TRUE;
    return true;
    }
@@ -3574,7 +3669,7 @@
   */
  private function _clear_messagecount($mbox_name='')
    {
    $a_mailbox_cache = FALSE;
    $a_mailbox_cache = false;
    $mailbox = $mbox_name ? $mbox_name : $this->mailbox;
    $a_mailbox_cache = $this->get_cache('messagecount');