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 | bf6b11d | 2015-01-12 17:27:12 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2015, 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 | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | bf6b11d | 2015-01-12 17:27:12 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2015, 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 |
| 34 | * @link http://codeigniter.com |
| 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 |
| 47 | * @link http://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 | * |
| 54 | * @var resource |
| 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 | |
| 72 | // ------------------------------------------------------------------------ |
| 73 | |
| 74 | /** |
| 75 | * Class constructor |
| 76 | * |
| 77 | * @param array $params Configuration parameters |
| 78 | * @return void |
| 79 | */ |
| 80 | public function __construct(&$params) |
| 81 | { |
| 82 | parent::__construct($params); |
| 83 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 84 | if (empty($this->_config['save_path'])) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 85 | { |
| 86 | log_message('error', 'Session: No Redis save path configured.'); |
| 87 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 88 | elseif (preg_match('#(?:tcp://)?([^:?]+)(?:\:(\d+))?(\?.+)?#', $this->_config['save_path'], $matches)) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 89 | { |
Andrey Andreev | 39ec295 | 2014-09-17 14:16:05 +0300 | [diff] [blame] | 90 | 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] | 91 | $this->_config['save_path'] = array( |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 92 | 'host' => $matches[1], |
| 93 | 'port' => empty($matches[2]) ? NULL : $matches[2], |
| 94 | 'password' => preg_match('#auth=([^\s&]+)#', $matches[3], $match) ? $match[1] : NULL, |
| 95 | 'database' => preg_match('#database=(\d+)#', $matches[3], $match) ? (int) $match[1] : NULL, |
| 96 | 'timeout' => preg_match('#timeout=(\d+\.\d+)#', $matches[3], $match) ? (float) $match[1] : NULL |
| 97 | ); |
| 98 | |
| 99 | preg_match('#prefix=([^\s&]+)#', $matches[3], $match) && $this->_key_prefix = $match[1]; |
| 100 | } |
| 101 | else |
| 102 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 103 | 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] | 104 | } |
| 105 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 106 | if ($this->_config['match_ip'] === TRUE) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 107 | { |
| 108 | $this->_key_prefix .= $_SERVER['REMOTE_ADDR'].':'; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // ------------------------------------------------------------------------ |
| 113 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 114 | /** |
| 115 | * Open |
| 116 | * |
| 117 | * Sanitizes save_path and initializes connection. |
| 118 | * |
| 119 | * @param string $save_path Server path |
| 120 | * @param string $name Session cookie name, unused |
| 121 | * @return bool |
| 122 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 123 | public function open($save_path, $name) |
| 124 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 125 | if (empty($this->_config['save_path'])) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 126 | { |
| 127 | return FALSE; |
| 128 | } |
| 129 | |
| 130 | $redis = new Redis(); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 131 | 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] | 132 | { |
| 133 | log_message('error', 'Session: Unable to connect to Redis with the configured settings.'); |
| 134 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 135 | 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] | 136 | { |
| 137 | log_message('error', 'Session: Unable to authenticate to Redis instance.'); |
| 138 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 139 | 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] | 140 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 141 | 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] | 142 | } |
| 143 | else |
| 144 | { |
| 145 | $this->_redis = $redis; |
| 146 | return TRUE; |
| 147 | } |
| 148 | |
| 149 | return FALSE; |
| 150 | } |
| 151 | |
| 152 | // ------------------------------------------------------------------------ |
| 153 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 154 | /** |
| 155 | * Read |
| 156 | * |
| 157 | * Reads session data and acquires a lock |
| 158 | * |
| 159 | * @param string $session_id Session ID |
| 160 | * @return string Serialized session data |
| 161 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 162 | public function read($session_id) |
| 163 | { |
| 164 | if (isset($this->_redis) && $this->_get_lock($session_id)) |
| 165 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 166 | // Needed by write() to detect session_regenerate_id() calls |
| 167 | $this->_session_id = $session_id; |
| 168 | |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 169 | $session_data = (string) $this->_redis->get($this->_key_prefix.$session_id); |
| 170 | $this->_fingerprint = md5($session_data); |
| 171 | return $session_data; |
| 172 | } |
| 173 | |
Gabriel Potkány | 0dd2538 | 2015-02-04 12:15:06 +0100 | [diff] [blame] | 174 | return FALSE; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 175 | } |
| 176 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 177 | // ------------------------------------------------------------------------ |
| 178 | |
| 179 | /** |
| 180 | * Write |
| 181 | * |
| 182 | * Writes (create / update) session data |
| 183 | * |
| 184 | * @param string $session_id Session ID |
| 185 | * @param string $session_data Serialized session data |
| 186 | * @return bool |
| 187 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 188 | public function write($session_id, $session_data) |
| 189 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 190 | if ( ! isset($this->_redis)) |
| 191 | { |
| 192 | return FALSE; |
| 193 | } |
| 194 | // Was the ID regenerated? |
| 195 | elseif ($session_id !== $this->_session_id) |
| 196 | { |
| 197 | if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id)) |
| 198 | { |
| 199 | return FALSE; |
| 200 | } |
| 201 | |
| 202 | $this->_fingerprint = md5(''); |
| 203 | $this->_session_id = $session_id; |
| 204 | } |
| 205 | |
| 206 | if (isset($this->_lock_key)) |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 207 | { |
Andrey Andreev | 2a1f940 | 2014-08-27 23:52:55 +0300 | [diff] [blame] | 208 | $this->_redis->setTimeout($this->_lock_key, 5); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 209 | if ($this->_fingerprint !== ($fingerprint = md5($session_data))) |
| 210 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 211 | 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] | 212 | { |
| 213 | $this->_fingerprint = $fingerprint; |
| 214 | return TRUE; |
| 215 | } |
| 216 | |
| 217 | return FALSE; |
| 218 | } |
| 219 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 220 | return $this->_redis->setTimeout($this->_key_prefix.$session_id, $this->_config['expiration']); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return FALSE; |
| 224 | } |
| 225 | |
| 226 | // ------------------------------------------------------------------------ |
| 227 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 228 | /** |
| 229 | * Close |
| 230 | * |
| 231 | * Releases locks and closes connection. |
| 232 | * |
Gabriel Potkány | 1fb5000 | 2015-02-04 01:45:59 +0100 | [diff] [blame] | 233 | * @return bool |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 234 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 235 | public function close() |
| 236 | { |
| 237 | if (isset($this->_redis)) |
| 238 | { |
| 239 | try { |
| 240 | if ($this->_redis->ping() === '+PONG') |
| 241 | { |
| 242 | isset($this->_lock_key) && $this->_redis->delete($this->_lock_key); |
| 243 | if ( ! $this->_redis->close()) |
| 244 | { |
| 245 | return FALSE; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | catch (RedisException $e) |
| 250 | { |
| 251 | log_message('error', 'Session: Got RedisException on close(): '.$e->getMessage()); |
| 252 | } |
| 253 | |
| 254 | $this->_redis = NULL; |
| 255 | return TRUE; |
| 256 | } |
| 257 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 258 | return TRUE; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // ------------------------------------------------------------------------ |
| 262 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 263 | /** |
| 264 | * Destroy |
| 265 | * |
| 266 | * Destroys the current session. |
| 267 | * |
| 268 | * @param string $session_id Session ID |
| 269 | * @return bool |
| 270 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 271 | public function destroy($session_id) |
| 272 | { |
| 273 | if (isset($this->_redis, $this->_lock_key)) |
| 274 | { |
| 275 | if ($this->_redis->delete($this->_key_prefix.$session_id) !== 1) |
| 276 | { |
| 277 | log_message('debug', 'Session: Redis::delete() expected to return 1, got '.var_export($result, TRUE).' instead.'); |
| 278 | } |
| 279 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 280 | return $this->_cookie_destroy(); |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 281 | } |
| 282 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 283 | return FALSE; |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // ------------------------------------------------------------------------ |
| 287 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 288 | /** |
| 289 | * Garbage Collector |
| 290 | * |
| 291 | * Deletes expired sessions |
| 292 | * |
| 293 | * @param int $maxlifetime Maximum lifetime of sessions |
| 294 | * @return bool |
| 295 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 296 | public function gc($maxlifetime) |
| 297 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 298 | // Not necessary, Redis takes care of that. |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 299 | return TRUE; |
| 300 | } |
| 301 | |
| 302 | // ------------------------------------------------------------------------ |
| 303 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 304 | /** |
| 305 | * Get lock |
| 306 | * |
| 307 | * Acquires an (emulated) lock. |
| 308 | * |
| 309 | * @param string $session_id Session ID |
| 310 | * @return bool |
| 311 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 312 | protected function _get_lock($session_id) |
| 313 | { |
| 314 | if (isset($this->_lock_key)) |
| 315 | { |
| 316 | return $this->_redis->setTimeout($this->_lock_key, 5); |
| 317 | } |
| 318 | |
| 319 | $lock_key = $this->_key_prefix.$session_id.':lock'; |
| 320 | if (($ttl = $this->_redis->ttl($lock_key)) < 1) |
| 321 | { |
| 322 | if ( ! $this->_redis->setex($lock_key, 5, time())) |
| 323 | { |
| 324 | log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); |
| 325 | return FALSE; |
| 326 | } |
| 327 | |
| 328 | $this->_lock_key = $lock_key; |
| 329 | |
| 330 | if ($ttl === -1) |
| 331 | { |
| 332 | log_message('debug', 'Session: Lock for '.$this->_key_prefix.$session_id.' had no TTL, overriding.'); |
| 333 | } |
| 334 | |
| 335 | $this->_lock = TRUE; |
| 336 | return TRUE; |
| 337 | } |
| 338 | |
| 339 | // Another process has the lock, we'll try to wait for it to free itself ... |
| 340 | $attempt = 0; |
| 341 | while ($attempt++ < 5) |
| 342 | { |
| 343 | usleep(($ttl * 1000000) - 20000); |
| 344 | if (($ttl = $this->_redis->ttl($lock_key)) > 0) |
| 345 | { |
| 346 | continue; |
| 347 | } |
| 348 | |
| 349 | if ( ! $this->_redis->setex($lock_key, 5, time())) |
| 350 | { |
| 351 | log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); |
| 352 | return FALSE; |
| 353 | } |
| 354 | |
| 355 | $this->_lock_key = $lock_key; |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | if ($attempt === 5) |
| 360 | { |
| 361 | log_message('error', 'Session: Unable to obtain lock for '.$this->_key_prefix.$session_id.' after 5 attempts, aborting.'); |
| 362 | return FALSE; |
| 363 | } |
| 364 | |
| 365 | $this->_lock = TRUE; |
| 366 | return TRUE; |
| 367 | } |
| 368 | |
| 369 | // ------------------------------------------------------------------------ |
| 370 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 371 | /** |
| 372 | * Release lock |
| 373 | * |
| 374 | * Releases a previously acquired lock |
| 375 | * |
| 376 | * @return bool |
| 377 | */ |
Andrey Andreev | 43f6cdb | 2014-08-27 22:26:40 +0300 | [diff] [blame] | 378 | protected function _release_lock() |
| 379 | { |
| 380 | if (isset($this->_redis, $this->_lock_key) && $this->_lock) |
| 381 | { |
| 382 | if ( ! $this->_redis->delete($this->_lock_key)) |
| 383 | { |
Andrey Andreev | 0002588 | 2015-02-11 16:23:46 +0200 | [diff] [blame^] | 384 | 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] | 385 | return FALSE; |
| 386 | } |
| 387 | |
| 388 | $this->_lock_key = NULL; |
| 389 | $this->_lock = FALSE; |
| 390 | } |
| 391 | |
| 392 | return TRUE; |
| 393 | } |
| 394 | |
| 395 | } |