Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | bf6b11d | 2015-01-12 17:27:12 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 6 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 10 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 17 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 35 | * @since Version 3.0.0 |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
| 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
| 39 | |
| 40 | /** |
| 41 | * CodeIgniter Session Redis Driver |
| 42 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 43 | * @package CodeIgniter |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 44 | * @subpackage Libraries |
| 45 | * @category Sessions |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 46 | * @author Andrey Andreev |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/sessions.html |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 48 | */ |
| 49 | class CI_Session_redis_driver extends CI_Session_driver implements SessionHandlerInterface { |
| 50 | |
| 51 | /** |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 52 | * phpRedis instance |
| 53 | * |
Andrey Andreev | ee9d428 | 2017-06-05 10:44:37 +0300 | [diff] [blame] | 54 | * @var Redis |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 55 | */ |
| 56 | protected $_redis; |
| 57 | |
| 58 | /** |
| 59 | * Key prefix |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | protected $_key_prefix = 'ci_session:'; |
| 64 | |
| 65 | /** |
| 66 | * Lock key |
| 67 | * |
| 68 | * @var string |
| 69 | */ |
| 70 | protected $_lock_key; |
| 71 | |
Andrey Andreev | 173cf41 | 2016-02-05 14:36:50 +0200 | [diff] [blame] | 72 | /** |
| 73 | * Key exists flag |
| 74 | * |
| 75 | * @var bool |
| 76 | */ |
| 77 | protected $_key_exists = FALSE; |
| 78 | |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 79 | // ------------------------------------------------------------------------ |
| 80 | |
| 81 | /** |
| 82 | * Class constructor |
| 83 | * |
| 84 | * @param array $params Configuration parameters |
| 85 | * @return void |
| 86 | */ |
| 87 | public function __construct(&$params) |
| 88 | { |
| 89 | parent::__construct($params); |
| 90 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 91 | if (empty($this->_config['save_path'])) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 92 | { |
| 93 | log_message('error', 'Session: No Redis save path configured.'); |
| 94 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 95 | elseif (preg_match('#(?:tcp://)?([^:?]+)(?:\:(\d+))?(\?.+)?#', $this->_config['save_path'], $matches)) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 96 | { |
Andrey Andreev | 39ec295 | 2014-09-17 14:16:05 +0300 | [diff] [blame] | 97 | isset($matches[3]) OR $matches[3] = ''; // Just to avoid undefined index notices below |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 98 | $this->_config['save_path'] = array( |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 99 | 'host' => $matches[1], |
| 100 | 'port' => empty($matches[2]) ? NULL : $matches[2], |
| 101 | 'password' => preg_match('#auth=([^\s&]+)#', $matches[3], $match) ? $match[1] : NULL, |
| 102 | 'database' => preg_match('#database=(\d+)#', $matches[3], $match) ? (int) $match[1] : NULL, |
| 103 | 'timeout' => preg_match('#timeout=(\d+\.\d+)#', $matches[3], $match) ? (float) $match[1] : NULL |
| 104 | ); |
| 105 | |
| 106 | preg_match('#prefix=([^\s&]+)#', $matches[3], $match) && $this->_key_prefix = $match[1]; |
| 107 | } |
| 108 | else |
| 109 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 110 | log_message('error', 'Session: Invalid Redis save path format: '.$this->_config['save_path']); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 111 | } |
| 112 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 113 | if ($this->_config['match_ip'] === TRUE) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 114 | { |
| 115 | $this->_key_prefix .= $_SERVER['REMOTE_ADDR'].':'; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // ------------------------------------------------------------------------ |
| 120 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 121 | /** |
| 122 | * Open |
| 123 | * |
| 124 | * Sanitizes save_path and initializes connection. |
| 125 | * |
| 126 | * @param string $save_path Server path |
| 127 | * @param string $name Session cookie name, unused |
| 128 | * @return bool |
| 129 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 130 | public function open($save_path, $name) |
| 131 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 132 | if (empty($this->_config['save_path'])) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 133 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 134 | return $this->_fail(); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | $redis = new Redis(); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 138 | if ( ! $redis->connect($this->_config['save_path']['host'], $this->_config['save_path']['port'], $this->_config['save_path']['timeout'])) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 139 | { |
| 140 | log_message('error', 'Session: Unable to connect to Redis with the configured settings.'); |
| 141 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 142 | elseif (isset($this->_config['save_path']['password']) && ! $redis->auth($this->_config['save_path']['password'])) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 143 | { |
| 144 | log_message('error', 'Session: Unable to authenticate to Redis instance.'); |
| 145 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 146 | elseif (isset($this->_config['save_path']['database']) && ! $redis->select($this->_config['save_path']['database'])) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 147 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 148 | log_message('error', 'Session: Unable to select Redis database with index '.$this->_config['save_path']['database']); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 149 | } |
| 150 | else |
| 151 | { |
| 152 | $this->_redis = $redis; |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 153 | return $this->_success; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 154 | } |
| 155 | |
Andrey Andreev | a9da3dd | 2018-06-12 16:40:12 +0300 | [diff] [blame] | 156 | $this->php5_validate_id(); |
| 157 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 158 | return $this->_fail(); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // ------------------------------------------------------------------------ |
| 162 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 163 | /** |
| 164 | * Read |
| 165 | * |
| 166 | * Reads session data and acquires a lock |
| 167 | * |
| 168 | * @param string $session_id Session ID |
| 169 | * @return string Serialized session data |
| 170 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 171 | public function read($session_id) |
| 172 | { |
| 173 | if (isset($this->_redis) && $this->_get_lock($session_id)) |
| 174 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 175 | // Needed by write() to detect session_regenerate_id() calls |
| 176 | $this->_session_id = $session_id; |
| 177 | |
Andrey Andreev | 173cf41 | 2016-02-05 14:36:50 +0200 | [diff] [blame] | 178 | $session_data = $this->_redis->get($this->_key_prefix.$session_id); |
| 179 | |
| 180 | is_string($session_data) |
| 181 | ? $this->_key_exists = TRUE |
| 182 | : $session_data = ''; |
| 183 | |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 184 | $this->_fingerprint = md5($session_data); |
| 185 | return $session_data; |
| 186 | } |
| 187 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 188 | return $this->_fail(); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 189 | } |
| 190 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 191 | // ------------------------------------------------------------------------ |
| 192 | |
| 193 | /** |
| 194 | * Write |
| 195 | * |
| 196 | * Writes (create / update) session data |
| 197 | * |
| 198 | * @param string $session_id Session ID |
| 199 | * @param string $session_data Serialized session data |
| 200 | * @return bool |
| 201 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 202 | public function write($session_id, $session_data) |
| 203 | { |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 204 | if ( ! isset($this->_redis, $this->_lock_key)) |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 205 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 206 | return $this->_fail(); |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 207 | } |
| 208 | // Was the ID regenerated? |
| 209 | elseif ($session_id !== $this->_session_id) |
| 210 | { |
| 211 | if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id)) |
| 212 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 213 | return $this->_fail(); |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Andrey Andreev | 173cf41 | 2016-02-05 14:36:50 +0200 | [diff] [blame] | 216 | $this->_key_exists = FALSE; |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 217 | $this->_session_id = $session_id; |
| 218 | } |
| 219 | |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 220 | $this->_redis->setTimeout($this->_lock_key, 300); |
| 221 | if ($this->_fingerprint !== ($fingerprint = md5($session_data)) OR $this->_key_exists === FALSE) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 222 | { |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 223 | if ($this->_redis->set($this->_key_prefix.$session_id, $session_data, $this->_config['expiration'])) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 224 | { |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 225 | $this->_fingerprint = $fingerprint; |
| 226 | $this->_key_exists = TRUE; |
| 227 | return $this->_success; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 228 | } |
| 229 | |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 230 | return $this->_fail(); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 231 | } |
| 232 | |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 233 | return ($this->_redis->setTimeout($this->_key_prefix.$session_id, $this->_config['expiration'])) |
| 234 | ? $this->_success |
| 235 | : $this->_fail(); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // ------------------------------------------------------------------------ |
| 239 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 240 | /** |
| 241 | * Close |
| 242 | * |
| 243 | * Releases locks and closes connection. |
| 244 | * |
Gabriel Potkány | 1fb5000 | 2015-02-04 01:45:59 +0100 | [diff] [blame] | 245 | * @return bool |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 246 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 247 | public function close() |
| 248 | { |
| 249 | if (isset($this->_redis)) |
| 250 | { |
| 251 | try { |
| 252 | if ($this->_redis->ping() === '+PONG') |
| 253 | { |
Andrey Andreev | f06858c | 2016-02-29 17:35:12 +0200 | [diff] [blame] | 254 | $this->_release_lock(); |
Andrey Andreev | d680779 | 2016-05-26 10:28:04 +0300 | [diff] [blame] | 255 | if ($this->_redis->close() === FALSE) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 256 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 257 | return $this->_fail(); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | catch (RedisException $e) |
| 262 | { |
| 263 | log_message('error', 'Session: Got RedisException on close(): '.$e->getMessage()); |
| 264 | } |
| 265 | |
| 266 | $this->_redis = NULL; |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 267 | return $this->_success; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 268 | } |
| 269 | |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 270 | return $this->_success; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // ------------------------------------------------------------------------ |
| 274 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 275 | /** |
| 276 | * Destroy |
| 277 | * |
| 278 | * Destroys the current session. |
| 279 | * |
| 280 | * @param string $session_id Session ID |
| 281 | * @return bool |
| 282 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 283 | public function destroy($session_id) |
| 284 | { |
| 285 | if (isset($this->_redis, $this->_lock_key)) |
| 286 | { |
Andrey Andreev | abc8f00 | 2015-02-23 08:38:06 +0200 | [diff] [blame] | 287 | if (($result = $this->_redis->delete($this->_key_prefix.$session_id)) !== 1) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 288 | { |
| 289 | log_message('debug', 'Session: Redis::delete() expected to return 1, got '.var_export($result, TRUE).' instead.'); |
| 290 | } |
| 291 | |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 292 | $this->_cookie_destroy(); |
| 293 | return $this->_success; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 294 | } |
| 295 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 296 | return $this->_fail(); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // ------------------------------------------------------------------------ |
| 300 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 301 | /** |
| 302 | * Garbage Collector |
| 303 | * |
| 304 | * Deletes expired sessions |
| 305 | * |
| 306 | * @param int $maxlifetime Maximum lifetime of sessions |
| 307 | * @return bool |
| 308 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 309 | public function gc($maxlifetime) |
| 310 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 311 | // Not necessary, Redis takes care of that. |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 312 | return $this->_success; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 313 | } |
| 314 | |
Andrey Andreev | a9da3dd | 2018-06-12 16:40:12 +0300 | [diff] [blame] | 315 | // -------------------------------------------------------------------- |
| 316 | |
| 317 | /** |
| 318 | * Validate ID |
| 319 | * |
| 320 | * Checks whether a session ID record exists server-side, |
| 321 | * to enforce session.use_strict_mode. |
| 322 | * |
| 323 | * @param string $id |
| 324 | * @return bool |
| 325 | */ |
Michiel Vugteveen | d3e9273 | 2018-08-07 15:23:21 +0200 | [diff] [blame^] | 326 | public function validateSessionId($id) |
Andrey Andreev | a9da3dd | 2018-06-12 16:40:12 +0300 | [diff] [blame] | 327 | { |
| 328 | return (bool) $this->_redis->exists($this->_key_prefix.$id); |
| 329 | } |
| 330 | |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 331 | // ------------------------------------------------------------------------ |
| 332 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 333 | /** |
| 334 | * Get lock |
| 335 | * |
| 336 | * Acquires an (emulated) lock. |
| 337 | * |
| 338 | * @param string $session_id Session ID |
| 339 | * @return bool |
| 340 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 341 | protected function _get_lock($session_id) |
| 342 | { |
Andrey Andreev | 79b8a08 | 2016-01-07 13:55:21 +0200 | [diff] [blame] | 343 | // PHP 7 reuses the SessionHandler object on regeneration, |
| 344 | // so we need to check here if the lock key is for the |
| 345 | // correct session ID. |
| 346 | if ($this->_lock_key === $this->_key_prefix.$session_id.':lock') |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 347 | { |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 348 | return $this->_redis->setTimeout($this->_lock_key, 300); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 349 | } |
| 350 | |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 351 | // 30 attempts to obtain a lock, in case another request already has it |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 352 | $lock_key = $this->_key_prefix.$session_id.':lock'; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 353 | $attempt = 0; |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 354 | do |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 355 | { |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 356 | if (($ttl = $this->_redis->ttl($lock_key)) > 0) |
| 357 | { |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 358 | sleep(1); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 359 | continue; |
| 360 | } |
| 361 | |
Andrey Andreev | fdf4b59 | 2017-07-06 11:49:13 +0300 | [diff] [blame] | 362 | $result = ($ttl === -2) |
| 363 | ? $this->_redis->set($lock_key, time(), array('nx', 'ex' => 300)) |
| 364 | : $this->_redis->setex($lock_key, 300, time()); |
| 365 | |
| 366 | if ( ! $result) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 367 | { |
| 368 | log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); |
| 369 | return FALSE; |
| 370 | } |
| 371 | |
| 372 | $this->_lock_key = $lock_key; |
| 373 | break; |
| 374 | } |
Andrey Andreev | 73b9e85 | 2015-04-30 13:06:40 +0300 | [diff] [blame] | 375 | while (++$attempt < 30); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 376 | |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 377 | if ($attempt === 30) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 378 | { |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 379 | log_message('error', 'Session: Unable to obtain lock for '.$this->_key_prefix.$session_id.' after 30 attempts, aborting.'); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 380 | return FALSE; |
| 381 | } |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 382 | elseif ($ttl === -1) |
| 383 | { |
| 384 | log_message('debug', 'Session: Lock for '.$this->_key_prefix.$session_id.' had no TTL, overriding.'); |
| 385 | } |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 386 | |
| 387 | $this->_lock = TRUE; |
| 388 | return TRUE; |
| 389 | } |
| 390 | |
| 391 | // ------------------------------------------------------------------------ |
| 392 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 393 | /** |
| 394 | * Release lock |
| 395 | * |
| 396 | * Releases a previously acquired lock |
| 397 | * |
| 398 | * @return bool |
| 399 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 400 | protected function _release_lock() |
| 401 | { |
| 402 | if (isset($this->_redis, $this->_lock_key) && $this->_lock) |
| 403 | { |
| 404 | if ( ! $this->_redis->delete($this->_lock_key)) |
| 405 | { |
Andrey Andreev | 0002588 | 2015-02-11 16:23:46 +0200 | [diff] [blame] | 406 | log_message('error', 'Session: Error while trying to free lock for '.$this->_lock_key); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 407 | return FALSE; |
| 408 | } |
| 409 | |
| 410 | $this->_lock_key = NULL; |
| 411 | $this->_lock = FALSE; |
| 412 | } |
| 413 | |
| 414 | return TRUE; |
| 415 | } |
| 416 | |
| 417 | } |