summaryrefslogtreecommitdiff
path: root/program/lib/Roundcube/rcube_session_redis.php
blob: 4822db7f91d831b0b15f6e480bf677bb77bbddb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php

/*
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2005-2014, The Roundcube Dev Team                       |
 |                                                                       |
 | 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 redis supported session management                       |
 +-----------------------------------------------------------------------+
 | Author: Cor Bosman <cor@roundcu.be>                                   |
 +-----------------------------------------------------------------------+
*/

/**
 * Class to provide redis session storage
 *
 * @package    Framework
 * @subpackage Core
 * @author     Cor Bosman <cor@roundcu.be>
 */
class rcube_session_redis extends rcube_session {

    private $redis;

    /**
     * @param Object $config
     */
    public function __construct($config)
    {
        parent::__construct($config);

        // 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 = $this->config->get('redis_hosts', array('localhost'));

        // 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 $host) {
            // explode individual fields
            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';
            $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)));
    }


}