alecpl
2010-03-19 9b3fdc25c171d2b2461af42224ea16ad6c032c49
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();
@@ -496,7 +495,7 @@
   * @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);
@@ -618,7 +617,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 +649,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,17 +681,19 @@
          $this->_fetch_headers($mailbox, join(",", $msg_index), $a_msg_headers, $cache_key);
      }
    // use SORT command
    else if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')))
    else if ($this->get_capability('SORT'))
      {
      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 ($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);
        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);
        // fetch reqested headers from server
        $this->_fetch_headers($mailbox, join(',', $msg_index), $a_msg_headers, $cache_key);
        }
      }
    // fetch specified header for all messages and sort
    else if ($a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:*", $this->sort_field, $this->skip_deleted))
@@ -739,7 +740,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);
@@ -773,7 +774,7 @@
      // get all threads
      list ($thread_tree, $msg_depth, $has_children) = iil_C_Thread($this->conn,
        $mailbox, $this->threading, $this->skip_deleted ? 'UNDELETED' : '');
      // add to internal (fast) cache
      $this->icache['threads'] = array();
      $this->icache['threads']['tree'] = $thread_tree;
@@ -923,7 +924,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)
@@ -1082,25 +1083,25 @@
    // fetch reqested headers from server
    $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs, false, false, $this->fetch_add_headers);
    if (!empty($a_header_index))
      {
      // cache is incomplete
      $cache_index = $this->get_message_cache_index($cache_key);
    if (empty($a_header_index))
      return 0;
      foreach ($a_header_index as $i => $headers) {
        if ($this->caching_enabled && $cache_index[$headers->id] != $headers->uid) {
          // prevent index duplicates
          if ($cache_index[$headers->id]) {
            $this->remove_message_cache($cache_key, $headers->id, true);
            unset($cache_index[$headers->id]);
    // cache is incomplete
    $cache_index = $this->get_message_cache_index($cache_key);
    foreach ($a_header_index as $i => $headers) {
      if ($this->caching_enabled && $cache_index[$headers->id] != $headers->uid) {
        // prevent index duplicates
        if ($cache_index[$headers->id]) {
          $this->remove_message_cache($cache_key, $headers->id, true);
          unset($cache_index[$headers->id]);
          }
          // add message to cache
          $this->add_message_cache($cache_key, $headers->id, $headers, NULL,
            !in_array($headers->uid, $cache_index));
        // add message to cache
        $this->add_message_cache($cache_key, $headers->id, $headers, NULL,
          !in_array($headers->uid, $cache_index));
        }
        $a_msg_headers[$headers->uid] = $headers;
        }
      $a_msg_headers[$headers->uid] = $headers;
      }
    return count($a_msg_headers);
@@ -1169,7 +1170,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);
@@ -1204,7 +1205,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);
      }
@@ -1223,17 +1224,16 @@
      $this->cache[$key] = $a_index;
      }
    // fetch complete message index
    else if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')))
    else if ($this->get_capability('SORT'))
      {
      if ($this->sort_order == 'DESC')
        $a_index = array_reverse($a_index);
      $this->cache[$key] = $a_index;
      if ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')) {
        if ($this->sort_order == 'DESC')
          $a_index = array_reverse($a_index);
        $this->cache[$key] = $a_index;
   }
      }
    else
      {
      $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:*", $this->sort_field, $this->skip_deleted);
    else if ($a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:*", $this->sort_field, $this->skip_deleted)) {
      if ($this->sort_order=="ASC")
        asort($a_index);
      else if ($this->sort_order=="DESC")
@@ -1281,7 +1281,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);
      }
*/
@@ -1351,7 +1351,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]);
        }
@@ -1458,9 +1458,12 @@
   '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();
      }
    else {
      if ($orig_criteria == 'ALL') {
@@ -1469,7 +1472,10 @@
        }
      else {
        $a_messages = iil_C_Search($this->conn, $mailbox, ($charset ? "CHARSET $charset " : '') . $criteria);
   if (!$a_messages)
     return array();
        // I didn't found that SEARCH always returns sorted IDs
        if (!$this->sort_field)
          sort($a_messages);
@@ -1500,19 +1506,28 @@
    // THREAD=REFS:       sorting by the most recent date in each thread
    // default sorting
    if (!$this->sort_field || ($this->sort_field == 'date' && $this->threading == 'REFS')) {
        return array_keys($thread_tree);
        return array_keys((array)$thread_tree);
      }
    // 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' : ''));
   // return unsorted tree if we've got no index data
   if (!$a_index)
     return array_keys((array)$thread_tree);
        }
      else {
        // fetch specified headers for all messages and sort them
        $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, !empty($ids) ? $ids : "1:*",
       $this->sort_field, $this->skip_deleted);
   // return unsorted tree if we've got no index data
   if (!$a_index)
     return array_keys((array)$thread_tree);
        asort($a_index); // ASC
   $a_index = array_values($a_index);
        }
@@ -1606,7 +1621,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);
@@ -2152,7 +2167,7 @@
    $mailbox = $this->mod_mailbox($mbox_name);
    // make sure mailbox exists
    if (($mailbox == 'INBOX') || in_array($mailbox, $this->_list_mailboxes())) {
    if ($mailbox == 'INBOX' || $this->mailbox_exists($mbox_name)) {
      if ($is_file) {
        $separator = rcmail::get_instance()->config->header_delimiter();
        $saved = iil_C_AppendFromFile($this->conn, $mailbox, $message,
@@ -2167,7 +2182,7 @@
      // increase messagecount of the target mailbox
      $this->_set_messagecount($mailbox, 'ALL', 1);
      }
    return $saved;
    }
@@ -2187,21 +2202,21 @@
    $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);
    // exit if no message uids are specified
    if (!is_array($a_uids) || empty($a_uids))
      return false;
    // make sure mailbox exists
    if ($to_mbox != 'INBOX' && !$this->mailbox_exists($tbox))
      {
      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;
@@ -2217,7 +2232,7 @@
    // 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, $a_uids);
      $this->_clear_messagecount($from_mbox);
      $this->_clear_messagecount($to_mbox);
    }
@@ -2255,6 +2270,49 @@
  /**
   * Copy a message from one mailbox to another
   *
   * @param string List of UIDs to copy, separated by comma
   * @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;
    // convert the list of uids to array
    $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL);
    // exit if no message uids are specified
    if (!is_array($a_uids) || empty($a_uids))
      return false;
    // make sure mailbox exists
    if ($to_mbox != 'INBOX' && !$this->mailbox_exists($tbox))
      {
      if (in_array($tbox, $this->default_folders))
        $this->create_mailbox($tbox, true);
      else
        return false;
      }
    // copy messages
    $iil_copy = iil_C_Copy($this->conn, join(',', $a_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
@@ -2277,7 +2335,7 @@
    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, $a_uids);
      $this->_clear_messagecount($mailbox);
      unset($this->uid_id_map[$mailbox]);
@@ -2346,7 +2404,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);
@@ -2363,7 +2421,7 @@
   * @return boolean True on success
   * @access private
   */
  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;
@@ -2428,7 +2486,7 @@
    if ($this->get_capability('QUOTA'))
      return iil_C_GetQuota($this->conn);
   
    return FALSE;
    return false;
    }
@@ -2471,24 +2529,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;
    }
@@ -2501,7 +2555,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);
@@ -2542,7 +2596,7 @@
    if ($result && $subscribed)
      iil_C_Subscribe($this->conn, $abs_name);
    return $result ? $name : FALSE;
    return $result ? $name : false;
    }
@@ -2554,19 +2608,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(''),
     $mailbox . $this->delimiter . '*');
        // unsubscribe mailbox before deleting
        iil_C_UnSubscribe($this->conn, $mailbox);
@@ -2574,23 +2628,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))
            {
            iil_C_UnSubscribe($this->conn, $c_mbox);
            $result = iil_C_DeleteFolder($this->conn, $c_mbox);
            if ($result >= 0) {
              $deleted = TRUE;
             $this->clear_message_cache($c_mbox.'.msg');
              }
       }
        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;
           $this->clear_message_cache($c_mbox.'.msg');
            }
          }
        }
@@ -2607,18 +2656,45 @@
   */
  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;
      $mbox = $this->mod_mailbox($mbox_name);
      if ($subscription) {
        if ($a_folders = iil_C_ListSubscribed($this->conn, $this->mod_mailbox(''), $mbox))
          return true;
        }
      else {
        $a_folders = iil_C_ListMailboxes($this->conn, $this->mod_mailbox(''), $mbox);
   if (is_array($a_folders) && in_array($mbox, $a_folders))
          return true;
        }
      }
    return false;
    }
@@ -2633,9 +2709,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;
    }
  /**
@@ -2658,8 +2734,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;
    }
  /**
@@ -2691,13 +2767,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]);
      }
    }
@@ -2722,7 +2798,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;
        }
      }
@@ -2735,7 +2811,7 @@
  private function _write_cache_record($key, $data)
    {
    if (!$this->db)
      return FALSE;
      return false;
    // update existing cache record
    if ($this->cache_keys[$key])
@@ -2930,7 +3006,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();
    
@@ -3015,10 +3091,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
@@ -3177,7 +3253,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)
@@ -3488,7 +3564,7 @@
   */
  private function _change_subscription($a_mboxes, $mode)
    {
    $updated = FALSE;
    $updated = false;
    if (is_array($a_mboxes))
      foreach ($a_mboxes as $i => $mbox_name)
@@ -3529,14 +3605,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;
@@ -3548,7 +3624,7 @@
    // write back to cache
    $this->update_cache('messagecount', $a_mailbox_cache);
    
    return TRUE;
    return true;
    }
@@ -3558,7 +3634,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');