svncommit
2005-10-14 17fc718915a856a5bec7a415031acbba76c8bcfe
program/include/rcube_imap.inc
@@ -24,18 +24,17 @@
require_once('lib/imap.inc');
require_once('lib/mime.inc');
// check for Open-SSL support in PHP build
//$ICL_SSL = TRUE;
//$ICL_PORT = 993;
class rcube_imap
  {
  var $conn;
  var $root_ns = '';
  var $root_dir = '';
  var $mailbox = 'INBOX';
  var $list_page = 1;
  var $page_size = 10;
  var $cacheing_enabled = FALSE;
  var $delimiter = NULL;
  var $caching_enabled = FALSE;
  var $default_folders = array('inbox', 'drafts', 'sent', 'junk', 'trash');
  var $cache = array();
  var $cache_changes = array();  
@@ -46,8 +45,7 @@
  // PHP 5 constructor
  function __construct()
    {
    if (function_exists('rcube_read_cache'))
      $this->cacheing_enabled = TRUE;
    }
  // PHP 4 compatibility
@@ -57,33 +55,51 @@
    }
  function iloha_imap($connection='')
  function connect($host, $user, $pass, $port=143, $use_ssl=FALSE)
    {
    if ($connection)
    global $ICL_PORT, $CONFIG;
    // check for Open-SSL support in PHP build
    if ($use_ssl && in_array('openssl', get_loaded_extensions()))
      $ICL_SSL = TRUE;
    else if ($use_ssl)
      {
      $a_url = parse_url($connection);
      $scheme = $a_url['scheme'] ? $a_url['scheme'] : 'imap';
      $port = $a_url['port'] ? $a_url['port'] : ($scheme=='imaps' ? 993 : 143);
      $host = $a_url['host'];
      $user = $a_url['user'];
      $pass = $a_url['pass'];
      //var_dump($a_url);
      $this->connect($host, $user, $pass, $port);
      raise_error(array('code' => 403,
                        'type' => 'imap',
                        'message' => 'Open SSL not available;'), TRUE, FALSE);
      $port = 143;
      }
    }
  function connect($host, $user, $pass, $port=143)
    {
    global $ICL_PORT;
    $ICL_PORT = $port;
    $this->conn = iil_Connect($host, $user, $pass);
    $this->conn = iil_Connect($host, $user, $pass, array('imap' => 'check'));
    $this->host = $host;
    $this->user = $user;
    $this->pass = $pass;
    $this->port = $port;
    $this->ssl = $use_ssl;
    // print trace mesages
    if ($this->conn && ($CONFIG['debug_level'] & 8))
      console($this->conn->message);
    // write error log
    else if (!$this->conn && $GLOBALS['iil_error'])
      {
      raise_error(array('code' => 403,
                       'type' => 'imap',
                       'message' => $GLOBALS['iil_error']), TRUE, FALSE);
      }
    // get account namespace
    if ($this->conn)
      {
      iil_C_NameSpace($this->conn);
      if (!empty($this->conn->delimiter))
        $this->delimiter = $this->conn->delimiter;
      if (!empty($this->conn->rootdir))
        $this->root_ns = $this->conn->rootdir;
      }
    return $this->conn ? TRUE : FALSE;
    }
@@ -96,12 +112,22 @@
    }
  function reconnect()
    {
    $this->close();
    $this->connect($this->host, $this->user, $this->pass, $this->port, $this->ssl);
    }
  function set_rootdir($root)
    {
    if (substr($root, -1, 1)==='/')
    if (ereg('[\.\/]$', $root)) //(substr($root, -1, 1)==='/')
      $root = substr($root, 0, -1);
    $this->root_dir = $root;
    if (empty($this->delimiter))
      $this->get_hierarchy_delimiter();
    }
@@ -153,6 +179,14 @@
    return $this->conn ? $this->_mod_mailbox($this->mailbox, 'out') : '';
    }
  function get_hierarchy_delimiter()
    {
    if ($this->conn && empty($this->delimiter))
      $this->delimiter = iil_C_GetHierarchyDelimiter($this->conn);
    return $this->delimiter;
    }
  // public method for mailbox listing
  // convert mailbox name with root dir first
@@ -263,11 +297,9 @@
  function _list_headers($mailbox='', $page=NULL, $sort_field='date', $sort_order='DESC')
    {
    $max = $this->_messagecount($mailbox /*, 'ALL', TRUE*/);
    $a_out = array();
    
    if (!strlen($mailbox))
      return $a_out;
      return array();
    // get cached headers
    $a_msg_headers = $this->get_cache($mailbox.'.msg');
@@ -280,7 +312,8 @@
      $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, "1:$max");
      $a_msg_headers = array();
      foreach ($a_header_index as $i => $headers)
        $a_msg_headers[$headers->uid] = $headers;
      if (!$headers->deleted)
         $a_msg_headers[$headers->uid] = $headers;
        
// print "/**** fetch headers ****/\n";
      }
@@ -289,22 +322,19 @@
    // sort headers by a specific col
    $a_headers = iil_SortHeaders($a_msg_headers, $sort_field, $sort_order);
   // free memory
   unset($a_msg_headers);
    // write headers list to cache
    if (!$headers_cached)
      $this->update_cache($mailbox.'.msg', $a_msg_headers);
    if (is_array($a_headers))
      foreach ($a_headers as $header)
        if (!$header->deleted)
          $a_out[] = $header;
      $this->update_cache($mailbox.'.msg', $a_headers);
    // return complete list of messages
    if (strtolower($page)=='all')
      return $a_out;
      return $a_headers;
    $start_msg = ($this->list_page-1) * $this->page_size;
    return array_slice($a_out, $start_msg, $this->page_size);
    return array_slice($a_headers, $start_msg, $this->page_size);
    }
@@ -349,10 +379,10 @@
    // return cached header
    if ($a_msg_headers[$uid])
      return $a_msg_headers[$uid];
    $msg_id = $this->_uid2id($uid);
    $header = iil_C_FetchHeader($this->conn, $mailbox, $msg_id);
    // write headers cache
    $a_msg_headers[$uid] = $header;
    $this->update_cache($mailbox.'.msg', $a_msg_headers);
@@ -408,11 +438,13 @@
    else
      $result = iil_C_Flag($this->conn, $this->mailbox, join(',', $msg_ids), $flag);
    // reload message headers if cached
    $cache_key = $this->mailbox.'.msg';
    if ($result && ($a_cached_headers = $this->get_cache($cache_key)))
    if ($this->caching_enabled && $result && ($a_cached_headers = $this->get_cache($cache_key)))
      {
      // close and re-open connection
      $this->reconnect();
      foreach ($uids as $uid)
        {
        if (isset($a_cached_headers[$uid]))
@@ -473,12 +505,12 @@
    // exit if no message uids are specified
    if (!is_array($a_uids))
      return false;
    // convert uids to message ids
    $a_mids = array();
    foreach ($a_uids as $uid)
      $a_mids[] = $this->_uid2id($uid, $from_mbox);
    $moved = iil_C_Move($this->conn, join(',', $a_mids), $from_mbox, $to_mbox);
    
    // send expunge command in order to have the moved message
@@ -543,7 +575,6 @@
      }
    return $deleted;
    }
@@ -627,15 +658,18 @@
    $result = FALSE;
    $abs_name = $this->_mod_mailbox($name);
    $a_mailbox_cache = $this->get_cache('mailboxes');
    if (strlen($this->root_ns))
      $abs_name = $this->root_ns.$abs_name;
    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, iil_utf7_encode($abs_name));
    // update mailboxlist cache
    if ($result && $subscribe)
      $this->subscribe($name);
      $this->subscribe($this->root_ns.$name);
    return $result;
    return $result ? $this->root_ns.$name : FALSE;
    }
@@ -681,14 +715,22 @@
  /* --------------------------------
   *   internal cacheing functions
   *   internal caching functions
   * --------------------------------*/
  function set_caching($set)
    {
    if ($set && function_exists('rcube_read_cache'))
      $this->caching_enabled = TRUE;
    else
      $this->caching_enabled = FALSE;
    }
  function get_cache($key)
    {
    // read cache
    if (!isset($this->cache[$key]) && $this->cacheing_enabled)
    if (!isset($this->cache[$key]) && $this->caching_enabled)
      {
      $cache_data = rcube_read_cache('IMAP.'.$key);
      $this->cache[$key] = strlen($cache_data) ? unserialize($cache_data) : FALSE;
@@ -708,7 +750,7 @@
  function write_cache()
    {
    if ($this->cacheing_enabled && $this->cache_changed)
    if ($this->caching_enabled && $this->cache_changed)
      {
      foreach ($this->cache as $key => $data)
        {
@@ -884,9 +926,9 @@
  function _mod_mailbox($mbox, $mode='in')
    {
    if ($this->root_dir && $mode=='in')
      $mbox = $this->root_dir.'/'.$mbox;
    else if ($this->root_dir && $mode=='out')
    if (!empty($this->root_dir) && $mode=='in')
      $mbox = $this->root_dir.$this->delimiter.$mbox;
    else if (strlen($this->root_dir) && $mode=='out')
      $mbox = substr($mbox, strlen($this->root_dir)+1);
    return $mbox;