From 52877803f86c4f1b4e8a40b9a53b40586f653f2f Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 02 Jun 2012 10:59:01 -0400
Subject: [PATCH] Merge pull request #12 from mrhein/master

---
 program/include/rcmail.php |  166 +++++++++++++++++++------------------------------------
 1 files changed, 57 insertions(+), 109 deletions(-)

diff --git a/program/include/rcmail.php b/program/include/rcmail.php
index a352cfc..a10a2aa 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -19,9 +19,6 @@
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  | Author: Aleksander Machniak <alec@alec.pl>                            |
  +-----------------------------------------------------------------------+
-
- $Id$
-
 */
 
 
@@ -325,7 +322,7 @@
     $this->output->set_charset(RCMAIL_CHARSET);
 
     // add some basic labels to client
-    $this->output->add_label('loading', 'servererror');
+    $this->output->add_label('loading', 'servererror', 'requesttimedout');
 
     return $this->output;
   }
@@ -350,38 +347,7 @@
    */
   public function session_init()
   {
-    // session started (Installer?)
-    if (session_id())
-      return;
-
-    $sess_name   = $this->config->get('session_name');
-    $sess_domain = $this->config->get('session_domain');
-    $lifetime    = $this->config->get('session_lifetime', 0) * 60;
-
-    // set session domain
-    if ($sess_domain) {
-      ini_set('session.cookie_domain', $sess_domain);
-    }
-    // set session garbage collecting time according to session_lifetime
-    if ($lifetime) {
-      ini_set('session.gc_maxlifetime', $lifetime * 2);
-    }
-
-    ini_set('session.cookie_secure', rcube_utils::https_check());
-    ini_set('session.name', $sess_name ? $sess_name : 'roundcube_sessid');
-    ini_set('session.use_cookies', 1);
-    ini_set('session.use_only_cookies', 1);
-    ini_set('session.serialize_handler', 'php');
-
-    // use database for storing session data
-    $this->session = new rcube_session($this->get_dbh(), $this->config);
-
-    $this->session->register_gc_handler(array($this, 'temp_gc'));
-    $this->session->register_gc_handler(array($this, 'cache_gc'));
-
-    // start PHP session (if not in CLI mode)
-    if ($_SERVER['REMOTE_ADDR'])
-      session_start();
+    parent::session_init();
 
     // set initial session vars
     if (!$_SESSION['user_id'])
@@ -390,30 +356,6 @@
     // restore skin selection after logout
     if ($_SESSION['temp'] && !empty($_SESSION['skin']))
       $this->config->set('skin', $_SESSION['skin']);
-  }
-
-
-  /**
-   * Configure session object internals
-   */
-  public function session_configure()
-  {
-    if (!$this->session)
-      return;
-
-    $lifetime = $this->config->get('session_lifetime', 0) * 60;
-
-    // set keep-alive/check-recent interval
-    if ($keep_alive = $this->config->get('keep_alive')) {
-      // be sure that it's less than session lifetime
-      if ($lifetime)
-        $keep_alive = min($keep_alive, $lifetime - 30);
-      $keep_alive = max(60, $keep_alive);
-      $this->session->set_keep_alive($keep_alive);
-    }
-
-    $this->session->set_secret($this->config->get('des_key') . $_SERVER['HTTP_USER_AGENT']);
-    $this->session->set_ip_check($this->config->get('ip_check'));
   }
 
 
@@ -575,7 +517,7 @@
       $_SESSION['storage_port'] = $port;
       $_SESSION['storage_ssl']  = $ssl;
       $_SESSION['password']     = $this->encrypt($pass);
-      $_SESSION['login_time']   = mktime();
+      $_SESSION['login_time']   = time();
 
       if (isset($_REQUEST['_timezone']) && $_REQUEST['_timezone'] != '_default_')
         $_SESSION['timezone'] = floatval($_REQUEST['_timezone']);
@@ -604,15 +546,16 @@
 
     if (is_array($default_host)) {
       $post_host = rcube_utils::get_input_value('_host', rcube_utils::INPUT_POST);
+      $post_user = rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST);
+
+      list($user, $domain) = explode('@', $post_user);
 
       // direct match in default_host array
       if ($default_host[$post_host] || in_array($post_host, array_values($default_host))) {
         $host = $post_host;
       }
-
       // try to select host by mail domain
-      list($user, $domain) = explode('@', rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST));
-      if (!empty($domain)) {
+      else if (!empty($domain)) {
         foreach ($default_host as $storage_host => $mail_domains) {
           if (is_array($mail_domains) && in_array_nocase($domain, $mail_domains)) {
             $host = $storage_host;
@@ -674,18 +617,6 @@
     if (!empty($_SESSION['preferences'])) {
       $this->user->save_prefs(unserialize($_SESSION['preferences']));
     }
-  }
-
-
-  /**
-   * Garbage collector for cache entries.
-   * Set flag to expunge caches on shutdown
-   */
-  function cache_gc()
-  {
-    // because this gc function is called before storage is initialized,
-    // we just set a flag to expunge storage cache on shutdown.
-    $this->expunge_cache = true;
   }
 
 
@@ -1159,31 +1090,6 @@
 
 
     /**
-     * Garbage collector function for temp files.
-     * Remove temp files older than two days
-     */
-    public function temp_gc()
-    {
-        $tmp = unslashify($this->config->get('temp_dir'));
-        $expire = mktime() - 172800;  // expire in 48 hours
-
-        if ($tmp && ($dir = opendir($tmp))) {
-            while (($fname = readdir($dir)) !== false) {
-                if ($fname{0} == '.') {
-                    continue;
-                }
-
-                if (filemtime($tmp.'/'.$fname) < $expire) {
-                    @unlink($tmp.'/'.$fname);
-                }
-            }
-
-            closedir($dir);
-        }
-    }
-
-
-    /**
      * Create a HTML table based on the given data
      *
      * @param  array  Named table attributes
@@ -1421,11 +1327,12 @@
         $attrib      = $hook['attribs'];
 
         if ($type == 'select') {
+            $attrib['is_escaped'] = true;
             $select = new html_select($attrib);
 
             // add no-selection option
             if ($attrib['noselection']) {
-                $select->add($rcmail->gettext($attrib['noselection']), '');
+                $select->add(html::quote($rcmail->gettext($attrib['noselection'])), '');
             }
 
             $rcmail->render_folder_tree_select($a_mailboxes, $mbox_name, $attrib['maxlength'], $select, $attrib['realnames']);
@@ -1454,7 +1361,7 @@
      */
     public function folder_selector($p = array())
     {
-        $p += array('maxlength' => 100, 'realnames' => false);
+        $p += array('maxlength' => 100, 'realnames' => false, 'is_escaped' => true);
         $a_mailboxes = array();
         $storage = $this->get_storage();
 
@@ -1480,7 +1387,7 @@
         $select = new html_select($p);
 
         if ($p['noselection']) {
-            $select->add($p['noselection'], '');
+            $select->add(html::quote($p['noselection']), '');
         }
 
         $this->render_folder_tree_select($a_mailboxes, $mbox, $p['maxlength'], $select, $p['realnames'], 0, $p);
@@ -1669,13 +1576,13 @@
                 if ($maxlength && $maxlength > 1) {
                     $foldername = abbreviate_string($foldername, $maxlength);
                 }
+            }
 
-                 $select->add(str_repeat('&nbsp;', $nestLevel*4) . $foldername, $folder['id']);
+            $select->add(str_repeat('&nbsp;', $nestLevel*4) . html::quote($foldername), $folder['id']);
 
-                if (!empty($folder['folders'])) {
-                    $out .= $this->render_folder_tree_select($folder['folders'], $mbox_name, $maxlength,
-                        $select, $realnames, $nestLevel+1, $opts);
-                }
+            if (!empty($folder['folders'])) {
+                $out .= $this->render_folder_tree_select($folder['folders'], $mbox_name, $maxlength,
+                    $select, $realnames, $nestLevel+1, $opts);
             }
         }
 
@@ -2098,4 +2005,45 @@
     {
         rcube_utils::setcookie($name, $value, $exp);
     }
+
+    public function imap_connect()
+    {
+        return $this->storage_connect();
+    }
+
+    public function imap_init()
+    {
+        return $this->storage_init();
+    }
+
+    /**
+     * Connect to the mail storage server with stored session data
+     *
+     * @return bool True on success, False on error
+     */
+    public function storage_connect()
+    {
+        $storage = $this->get_storage();
+
+        if ($_SESSION['storage_host'] && !$storage->is_connected()) {
+            $host = $_SESSION['storage_host'];
+            $user = $_SESSION['username'];
+            $port = $_SESSION['storage_port'];
+            $ssl  = $_SESSION['storage_ssl'];
+            $pass = $this->decrypt($_SESSION['password']);
+
+            if (!$storage->connect($host, $user, $pass, $port, $ssl)) {
+                if (is_object($this->output)) {
+                    $error = $storage->get_error_code() == -1 ? 'storageerror' : 'sessionerror';
+                    $this->output->show_message($error, 'error');
+                }
+            }
+            else {
+                $this->set_storage_prop();
+                return $storage->is_connected();
+            }
+        }
+
+        return false;
+    }
 }

--
Gitblit v1.9.1