From bc92ca56ef6c51393d2782b7654eaa162dfc2e10 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Mon, 30 Jul 2012 07:20:56 -0400
Subject: [PATCH] Fixes after default->classic switch
---
program/include/rcube.php | 205 +++++++++++++++++++++++++++++++++++++-------------
1 files changed, 151 insertions(+), 54 deletions(-)
diff --git a/program/include/rcube.php b/program/include/rcube.php
index 61cee8d..ab5879d 100644
--- a/program/include/rcube.php
+++ b/program/include/rcube.php
@@ -2,7 +2,7 @@
/*
+-----------------------------------------------------------------------+
- | program/include/rcmail.php |
+ | program/include/rcube.php |
| |
| This file is part of the Roundcube Webmail client |
| Copyright (C) 2008-2012, The Roundcube Dev Team |
@@ -18,9 +18,6 @@
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
+-----------------------------------------------------------------------+
-
- $Id$
-
*/
@@ -36,7 +33,7 @@
const INIT_WITH_PLUGINS = 2;
/**
- * Singleton instace of rcmail
+ * Singleton instace of rcube
*
* @var rcmail
*/
@@ -109,12 +106,14 @@
/**
* This implements the 'singleton' design pattern
*
- * @return rcmail The one and only instance
+ * @param integer Options to initialize with this instance. See rcube::INIT_WITH_* constants
+ * @return rcube The one and only instance
*/
- static function get_instance()
+ static function get_instance($mode = 0)
{
if (!self::$instance) {
self::$instance = new rcube();
+ self::$instance->init($mode);
}
return self::$instance;
@@ -194,11 +193,17 @@
$this->memcache = new Memcache;
$this->mc_available = 0;
- // add alll configured hosts to pool
+ // add all configured hosts to pool
$pconnect = $this->config->get('memcache_pconnect', true);
foreach ($this->config->get('memcache_hosts', array()) as $host) {
- list($host, $port) = explode(':', $host);
- if (!$port) $port = 11211;
+ if (substr($host, 0, 7) != 'unix://') {
+ list($host, $port) = explode(':', $host);
+ if (!$port) $port = 11211;
+ }
+ else {
+ $port = 0;
+ }
+
$this->mc_available += intval($this->memcache->addServer($host, $port, $pconnect, 1, 1, 15, false, array($this, 'memcache_failure')));
}
@@ -236,15 +241,15 @@
*
* @param string $name Cache identifier
* @param string $type Cache type ('db', 'apc' or 'memcache')
- * @param int $ttl Expiration time for cache items in seconds
+ * @param string $ttl Expiration time for cache items
* @param bool $packed Enables/disables data serialization
*
* @return rcube_cache Cache object
*/
public function get_cache($name, $type='db', $ttl=0, $packed=true)
{
- if (!isset($this->caches[$name])) {
- $this->caches[$name] = new rcube_cache($type, $_SESSION['user_id'], $name, $ttl, $packed);
+ if (!isset($this->caches[$name]) && ($userid = $this->get_user_id())) {
+ $this->caches[$name] = new rcube_cache($type, $userid, $name, $ttl, $packed);
}
return $this->caches[$name];
@@ -347,6 +352,7 @@
$options['port'] = $_SESSION['storage_port'];
$options['ssl'] = $_SESSION['storage_ssl'];
$options['password'] = $this->decrypt($_SESSION['password']);
+ $_SESSION[$driver.'_host'] = $_SESSION['storage_host'];
}
$options = $this->plugins->exec_hook("storage_init", $options);
@@ -358,35 +364,6 @@
$this->set_storage_prop();
}
-
- /**
- * 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))
- $this->output->show_message($storage->get_error_code() == -1 ? 'storageerror' : 'sessionerror', 'error');
- }
- else {
- $this->set_storage_prop();
- return $storage->is_connected();
- }
- }
-
- return false;
- }
/**
* Set storage parameters.
@@ -408,6 +385,113 @@
$storage->set_page($_SESSION['page']);
}
}
+
+
+ /**
+ * Create session object and start the session.
+ */
+ 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');
+ ini_set('session.cookie_httponly', 1);
+
+ // 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();
+ }
+ }
+
+
+ /**
+ * Configure session object internals
+ */
+ public function session_configure()
+ {
+ if (!$this->session) {
+ return;
+ }
+
+ $lifetime = $this->config->get('session_lifetime', 0) * 60;
+ $keep_alive = $this->config->get('keep_alive');
+
+ // set keep-alive/check-recent interval
+ if ($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') . dirname($_SERVER['SCRIPT_NAME']));
+ $this->session->set_ip_check($this->config->get('ip_check'));
+ }
+
+
+ /**
+ * 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 = time() - 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);
+ }
+ }
+
+
+ /**
+ * Garbage collector for cache entries.
+ * Set flag to expunge caches on shutdown
+ */
+ public 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;
+ }
/**
@@ -523,7 +607,7 @@
$this->texts = array_merge($this->texts, $messages);
// include user language files
- if ($lang != 'en' && is_dir(INSTALL_PATH . 'program/localization/' . $lang)) {
+ if ($lang != 'en' && $lang != 'en_US' && is_dir(INSTALL_PATH . 'program/localization/' . $lang)) {
include_once(INSTALL_PATH . 'program/localization/' . $lang . '/labels.inc');
include_once(INSTALL_PATH . 'program/localization/' . $lang . '/messages.inc');
@@ -718,7 +802,7 @@
/*-
* Trim PHP's padding and the canary byte; see note in
- * rcmail::encrypt() and http://php.net/mcrypt_generic#68082
+ * rcube::encrypt() and http://php.net/mcrypt_generic#68082
*/
$clear = substr(rtrim($clear, "\0"), 0, -1);
@@ -848,15 +932,13 @@
{
$args = func_get_args();
- if (class_exists('rcmail', false)) {
+ if (class_exists('rcube', false)) {
$rcube = self::get_instance();
- if (is_object($rcube->plugins)) {
- $plugin = $rcube->plugins->exec_hook('console', array('args' => $args));
- if ($plugin['abort']) {
- return;
- }
- $args = $plugin['args'];
+ $plugin = $rcube->plugins->exec_hook('console', array('args' => $args));
+ if ($plugin['abort']) {
+ return;
}
+ $args = $plugin['args'];
}
$msg = array();
@@ -944,6 +1026,18 @@
*/
public static function raise_error($arg = array(), $log = false, $terminate = false)
{
+ // handle PHP exceptions
+ if (is_object($arg) && is_a($arg, 'Exception')) {
+ $err = array(
+ 'type' => 'php',
+ 'code' => $arg->getCode(),
+ 'line' => $arg->getLine(),
+ 'file' => $arg->getFile(),
+ 'message' => $arg->getMessage(),
+ );
+ $arg = $err;
+ }
+
// installer
if (class_exists('rcube_install', false)) {
$rci = rcube_install::get_instance();
@@ -951,7 +1045,8 @@
return;
}
- if ($log && $arg['type'] && $arg['message']) {
+ if (($log || $terminate) && $arg['type'] && $arg['message']) {
+ $arg['fatal'] = $terminate;
self::log_bug($arg);
}
@@ -979,7 +1074,7 @@
}
// write error to local log file
- if ($level & 1) {
+ if (($level & 1) || !empty($arg_arr['fatal'])) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$post_query = '?_task='.urlencode($_POST['_task']).'&_action='.urlencode($_POST['_action']);
}
@@ -1067,6 +1162,9 @@
if (is_object($this->user)) {
return $this->user->ID;
}
+ else if (isset($_SESSION['user_id'])) {
+ return $_SESSION['user_id'];
+ }
return null;
}
@@ -1104,4 +1202,3 @@
return $args;
}
}
-
--
Gitblit v1.9.1