| | |
| | | <?php |
| | | |
| | | /* |
| | | /** |
| | | +-----------------------------------------------------------------------+ |
| | | | This file is part of the Roundcube Webmail client | |
| | | | Copyright (C) 2005-2014, The Roundcube Dev Team | |
| | | | Copyright (C) 2011, Kolab Systems AG | |
| | | | | |
| | | | Licensed under the GNU General Public License version 3 or | |
| | | | any later version with exceptions for skins & plugins. | |
| | | | See the README file for a full license statement. | |
| | | | | |
| | | | PURPOSE: | |
| | | | Provide database supported session management | |
| | | | Provide redis supported session management | |
| | | +-----------------------------------------------------------------------+ |
| | | | Author: Thomas Bruederli <roundcube@gmail.com> | |
| | | | Author: Aleksander Machniak <alec@alec.pl> | |
| | | | Author: Cor Bosman <cor@roundcu.be> | |
| | | +-----------------------------------------------------------------------+ |
| | | */ |
| | |
| | | |
| | | private $redis; |
| | | |
| | | public function __construct() |
| | | /** |
| | | * @param Object $config |
| | | */ |
| | | public function __construct($config) |
| | | { |
| | | parent::__construct($config); |
| | | |
| | | // instantiate Redis object |
| | | $this->redis = new Redis(); |
| | | |
| | | if (! $this->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"), |
| | |
| | | } |
| | | |
| | | // get config instance |
| | | $hosts = rcube::get_instance()->config->get('redis_hosts', array()); |
| | | $hosts = $this->config->get('redis_hosts', array('localhost')); |
| | | |
| | | // host config is wrong |
| | | if (!is_array($hosts) || empty($hosts) ) { |
| | | 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); |
| | | } |
| | | |
| | | foreach($hosts as $config) { |
| | | foreach ($hosts as $host) { |
| | | // explode individual fields |
| | | list($host, $port, $database, $password) = array_pad(explode(':', $config, 4), 4, null); |
| | | list($host, $port, $database, $password) = array_pad(explode(':', $host, 4), 4, null); |
| | | |
| | | // set default values if not set |
| | | $host = ($host !== null) ? $host : '127.0.0.1'; |
| | |
| | | |
| | | // register sessions handler |
| | | $this->register_session_handler(); |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * read data from redis store |
| | | * |
| | |
| | | return !empty($this->vars) ? (string) $this->vars : ''; |
| | | } |
| | | |
| | | return null; |
| | | return ''; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * write data to redis store |
| | |
| | | $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))); |
| | | $data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $newvars)); |
| | | $this->redis->setex($key, $this->lifetime + 60, $data); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * write data to redis store |
| | |
| | | */ |
| | | public function write($key, $vars) |
| | | { |
| | | return $this->redis->setex($key, $this->lifetime + 60, serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars))); |
| | | $data = serialize(array('changed' => time(), 'ip' => $this->ip, 'vars' => $vars)); |
| | | |
| | | return $this->redis->setex($key, $this->lifetime + 60, $data); |
| | | } |
| | | |
| | | |
| | | } |
| | | } |