From 4df4ab500788f0792b75baf1fa98e4647d713ed1 Mon Sep 17 00:00:00 2001 From: corbosman Date: Thu, 19 Feb 2015 14:55:09 +0100 Subject: session refactor and add redis driver --- program/lib/Roundcube/rcube_session_redis.php | 210 ++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 program/lib/Roundcube/rcube_session_redis.php (limited to 'program/lib/Roundcube/rcube_session_redis.php') diff --git a/program/lib/Roundcube/rcube_session_redis.php b/program/lib/Roundcube/rcube_session_redis.php new file mode 100644 index 000000000..07a91cc45 --- /dev/null +++ b/program/lib/Roundcube/rcube_session_redis.php @@ -0,0 +1,210 @@ + | + | Author: Aleksander Machniak | + | Author: Cor Bosman | + +-----------------------------------------------------------------------+ +*/ + +/** + * Class to provide redis session storage + * + * @package Framework + * @subpackage Core + * @author Cor Bosman + */ +class rcube_session_redis extends rcube_session { + + private $redis; + + public function __construct() + { + // instantiate Redis object + $this->redis = new Redis(); + + if (! $this->redis) { + rcube::raise_error(array('code' => 604, 'type' => 'session', + 'line' => __LINE__, 'file' => __FILE__, + 'message' => "Failed to find Redis. Make sure php-redis is included"), + true, true); + } + + // get config instance + $hosts = rcube::get_instance()->config->get('redis_hosts', array()); + + // host config is wrong + if (!is_array($hosts) || empty($hosts) ) { + rcube::raise_error(array('code' => 604, 'type' => 'session', + 'line' => __LINE__, 'file' => __FILE__, + 'message' => "Redis host not configured"), + true, true); + } + + // only allow 1 host for now until we support clustering + if (count($hosts) > 1) { + rcube::raise_error(array('code' => 604, 'type' => 'session', + 'line' => __LINE__, 'file' => __FILE__, + 'message' => "Redis cluster not yet supported"), + true, true); + } + + foreach($hosts as $config) { + // explode individual fields + list($host, $port, $database, $password) = array_pad(explode(':', $config, 4), 4, null); + + // set default values if not set + $host = ($host !== null) ? $host : '127.0.0.1'; + $port = ($port !== null) ? $port : 6379; + $database = ($database !== null) ? $database : 0; + + if ($this->redis->connect($host, $port) === false) { + rcube::raise_error( + array( + 'code' => 604, + 'type' => 'session', + 'line' => __LINE__, + 'file' => __FILE__, + 'message' => "Could not connect to Redis server. Please check host and port" + ), + true, + true + ); + } + + if ($password != null && $this->redis->auth($password) === false) { + rcube::raise_error( + array( + 'code' => 604, + 'type' => 'session', + 'line' => __LINE__, + 'file' => __FILE__, + 'message' => "Could not authenticate with Redis server. Please check password." + ), + true, + true + ); + } + + if ($database != 0 && $this->redis->select($database) === false) { + rcube::raise_error( + array( + 'code' => 604, + 'type' => 'session', + 'line' => __LINE__, + 'file' => __FILE__, + 'message' => "Could not select Redis database. Please check database setting." + ), + true, + true + ); + } + } + + // register sessions handler + $this->register_session_handler(); + + } + + /** + * @param $save_path + * @param $session_name + * @return bool + */ + public function open($save_path, $session_name) + { + return true; + } + + /** + * @return bool + */ + public function close() + { + return true; + } + + /** + * remove data from store + * + * @param $key + * @return bool + */ + public function destroy($key) + { + if ($key) { + $this->redis->del($key); + } + + return true; + } + + + /** + * read data from redis store + * + * @param $key + * @return null + */ + public function read($key) + { + if ($value = $this->redis->get($key)) { + $arr = unserialize($value); + $this->changed = $arr['changed']; + $this->ip = $arr['ip']; + $this->vars = $arr['vars']; + $this->key = $key; + + return !empty($this->vars) ? (string) $this->vars : ''; + } + + return null; + } + + + /** + * write data to redis store + * + * @param $key + * @param $newvars + * @param $oldvars + * @return bool + */ + public function update($key, $newvars, $oldvars) + { + $ts = microtime(true); + + if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) { + $this->redis->setex($key, $this->lifetime + 60, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars))); + } + + return true; + } + + + /** + * write data to redis store + * + * @param $key + * @param $vars + * @return bool + */ + public function write($key, $vars) + { + return $this->redis->setex($key, $this->lifetime + 60, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars))); + } + + +} \ No newline at end of file -- cgit v1.2.3