redis.php 10.1 KB
Newer Older
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Redis based session handler.
 *
 * @package    core
 * @copyright  2016 Nicholas Hoobin <nicholashoobin@catalyst-au.net>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace core\session;

defined('MOODLE_INTERNAL') || die();

/**
 * Redis based session handler.
 *
 * @package    core
 * @copyright  2016 Nicholas Hoobin <nicholashoobin@catalyst-au.net>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class redis extends handler {
    /** @var string $savepath save_path string */
    protected $savepath;

    /** @var array $servers list of servers parsed from save_path */
    protected $servers;

    /** @var int $acquiretimeout how long to wait for session lock */
    protected $acquiretimeout = 120;

    /**
     * Create new instance of handler.
     */
    public function __construct() {
        global $CFG;

        if (!empty($CFG->session_redis_acquire_lock_timeout)) {
            $this->acquiretimeout = $CFG->session_redis_acquire_lock_timeout;
        }

        if (empty($CFG->session_redis_save_path)) {
            $this->savepath = '';
        } else {
            $this->savepath = $CFG->session_redis_save_path;
        }

        if (empty($this->savepath)) {
            $this->servers = array();
        } else {
            $this->servers = $this->connection_string_to_redis_servers($this->savepath);
        }

    }

    /**
     * Start the session.
     * @return bool success
     */
    public function start() {
        $default = ini_get('max_execution_time');
        set_time_limit($this->acquiretimeout);

        $result = parent::start();

        set_time_limit($default);

        return $result;
    }

    /**
     * Init session handler.
     */
    public function init() {
        if (!extension_loaded('Redis')) {
            throw new exception('sessionhandlerproblem', 'error', '', null, 'redis extension is not loaded');
        }

        // The session handler requires a version of Redis with the SETEX command (at least 2.0).
        $version = phpversion('Redis');
        if (!$version or version_compare($version, '2.0') <= 0) {
            throw new exception('sessionhandlerproblem', 'error', '', null, 'redis extension version must be at least 2.0');
        }

        if (empty($this->savepath)) {
            throw new exception('sessionhandlerproblem', 'error', '', null,
                '$CFG->session_redis_save_path must be specified in config.php');
        }

        ini_set('session.save_handler', 'redis');
        ini_set('session.save_path', $this->savepath);
    }

    /**
     * Check the backend contains data for this session id.
     *
     * Note: this is intended to be called from manager::session_exists() only.
     *
     * @param string $sid
     * @return bool true if session found.
     */
    public function session_exists($sid) {
        if (!$this->servers) {
            return false;
        }

        foreach ($this->servers as $server) {
            if ($redis = $this->redis_connect($server)) {
                $value = $redis->get($server['prefix'] . $sid);
                $redis->close();
            }

            if ($value !== false) {
                return true;
            }
        }

        return false;
    }

    /**
     * Kill all active sessions, the core sessions table is
     * purged afterwards.
     */
    public function kill_all_sessions() {
        global $DB;
        if (!$this->servers) {
            return false;
        }

        $serverlist = array();
        foreach ($this->servers as $server) {
            if ($redis = $this->redis_connect($server)) {
                $serverlist[] = array($redis, $server['prefix']);
            }
        }

        $rs = $DB->get_recordset('sessions', array(), 'id DESC', 'id, sid');
        foreach ($rs as $record) {
            foreach ($serverlist as $arr) {
                list($server, $prefix) = $arr;
                $server->delete($prefix . $sid);
            }
        }

        foreach ($serverlist as $arr) {
            list($server, $prefix) = $arr;
            $server->close();
        }
    }

    /**
     * Kill one session, the session record is removed afterwards.
     * @param string $sid
     */
    public function kill_session($sid) {
        if (!$this->servers) {
            return false;
        }

        // Go through the list of all servers because
        // we do not know where the session handler put the
        // data.

        foreach ($this->servers as $server) {
            if ($redis = $this->redis_connect($server)) {
                $redis->delete($server['prefix'] . $sid);
                $redis->close();
            }
        }
    }

    /**
     * Convert a connection string to an array of servers
     *
     * Example conversion,
     * "tcp://host1:123?database=0, unix:///var/run/redis/redis.sock?database=0" to
     *
     *  array(
     *      (
     *          [scheme]   => 'tcp',
     *          [host]     => 'host1',
     *          [port]     => 123,
     *          [database] => 0,
     *          [prefix]   => 'PHPREDIS_SESSION:'
     *      ),
     *      (
     *          [scheme]   => 'unix',
     *          [path]     => '/var/run/redis/redis.sock',
     *          [database] => 0,
     *          [prefix]   => 'PHPREDIS_SESSION:'
     *      )
     *  )
     *
     * @copyright  2016 Nicholas Hoobin <nicholashoobin@catalyst-au.net>
     * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
     * @author     Nicholas Hoobin
     *
     * @param string $str save_path value containing redis connection string
     * @return array
     */
    public function connection_string_to_redis_servers($str) {
        $servers     = array();
        $connections = array_map('trim', explode(',', $str));

        foreach ($connections as $con) {
            if (strpos($con, "unix:///") !== false) {
                $fields = $this->parse_unix_sock($con);

            } else if (strpos($con, "tcp://") !== false) {
                $fields = $this->parse_url_tcp($con);

            } else {
                $fields = false;
                debugging("Invalid Redis schema in connection savepath");

            }

            // Parsing failed.
            if ($fields === false) {
                continue;
            }

            // Setting the default prefix.
            if (!isset($fields['prefix'])) {
                $fields['prefix'] = 'PHPREDIS_SESSION:';
            }

            // Setting the default database.
            if (!isset($fields['database'])) {
                $fields['database'] = 0;
            }

            // Setting the default timeout.
            if (!isset($fields['timeout'])) {
                $fields['timeout'] = 86400;
            }

            $servers[] = $fields;
        }

        return $servers;
    }

    /**
     * Parses the tcp connection string and returns an object.
     * @param string $con connection string
     * @return object $con connection data object
     */
    private function parse_url_tcp($con) {
        $con = parse_url($con);

        // Seriously wrong url, parsing failed.
        if ($con === false) {
            return false;
        }

        // Parsing the query string.
        if (isset($con['query'])) {
            $query = $con['query'];
            $parts = explode('&', $query);

            foreach ($parts as $part) {
                list($key, $value) = explode('=', $part);
                $con[$key] = $value;
            }
        }

        // Setting the default port.
        if (!isset($con['port'])) {
            $con['port'] = 6379;
        }

        return $con;
    }

    /**
     * Parses the unix domain socket connection string and returns an object.
     * @param string $con connection string
     * @return object $con connection data object
     */
    private function parse_unix_sock($con) {
        // Lets use parse_url to get the bits we need.
        // To use this, replace the three slashes with two slashes.
        $con = str_replace(":///", "://", $con);
        $con = parse_url($con);

        // Seriously wrong url, parsing failed.
        if ($con === false) {
            return false;
        }

        /* Eg. host = var
               path = run/redis/redis.sock
               new path = /var/run/redis/redis.sock
        */
        $con['path'] = '/' . $con['host'] . $con['path'];
        unset($con['host']);

        // Parsing the query string.
        if (isset($con['query'])) {
            $query = $con['query'];
            $parts = explode('&', $query);

            foreach ($parts as $part) {
                list($key, $value) = explode('=', $part);
                $con[$key] = $value;
            }
        }

        return $con;
    }

    /**
     * Connects to the Redis server with the details from the connection object.
     * @param object $con connection details object
     * @return redis $redis redis connection
     */
    private function redis_connect($con) {
        $redis = new \Redis();

        $func = isset($con['persistent']) ? 'pconnect' : 'connect';

        if ($con['scheme'] === 'tcp') {
            // Only TCP connections will have a port, default 6379.
            $result = $redis->$func($con['host'], $con['port'], $con['timeout']);
        } else if ($con['scheme'] === 'unix') {
            // Unix domain socket.
            $result = $redis->$func($con['path']);
        }

        $result = true ? $redis->select($con['database']) : false;

        return $redis;
    }
}