Andrey Andreev | c9eface | 2014-09-02 15:19:01 +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 | c9eface | 2014-09-02 15:19:01 +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 | c9eface | 2014-09-02 15:19:01 +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 | c9eface | 2014-09-02 15:19:01 +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 | c9eface | 2014-09-02 15:19:01 +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 | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
| 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
| 39 | |
| 40 | /** |
| 41 | * CodeIgniter Session Memcached Driver |
| 42 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 43 | * @package CodeIgniter |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +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 | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 48 | */ |
| 49 | class CI_Session_memcached_driver extends CI_Session_driver implements SessionHandlerInterface { |
| 50 | |
| 51 | /** |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 52 | * Memcached instance |
| 53 | * |
| 54 | * @var Memcached |
| 55 | */ |
| 56 | protected $_memcached; |
| 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 | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 85 | { |
| 86 | log_message('error', 'Session: No Memcached save path configured.'); |
| 87 | } |
| 88 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 89 | if ($this->_config['match_ip'] === TRUE) |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 90 | { |
| 91 | $this->_key_prefix .= $_SERVER['REMOTE_ADDR'].':'; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // ------------------------------------------------------------------------ |
| 96 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 97 | /** |
| 98 | * Open |
| 99 | * |
| 100 | * Sanitizes save_path and initializes connections. |
| 101 | * |
| 102 | * @param string $save_path Server path(s) |
| 103 | * @param string $name Session cookie name, unused |
| 104 | * @return bool |
| 105 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 106 | public function open($save_path, $name) |
| 107 | { |
| 108 | $this->_memcached = new Memcached(); |
Andrey Andreev | 4f50256 | 2014-11-10 19:18:33 +0200 | [diff] [blame] | 109 | $this->_memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, TRUE); // required for touch() usage |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 110 | $server_list = array(); |
| 111 | foreach ($this->_memcached->getServerList() as $server) |
| 112 | { |
| 113 | $server_list[] = $server['host'].':'.$server['port']; |
| 114 | } |
| 115 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 116 | if ( ! preg_match_all('#,?([^,:]+)\:(\d{1,5})(?:\:(\d+))?#', $this->_config['save_path'], $matches, PREG_SET_ORDER)) |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 117 | { |
| 118 | $this->_memcached = NULL; |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 119 | log_message('error', 'Session: Invalid Memcached save path format: '.$this->_config['save_path']); |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 120 | return $this->_fail(); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | foreach ($matches as $match) |
| 124 | { |
| 125 | // If Memcached already has this server (or if the port is invalid), skip it |
| 126 | if (in_array($match[1].':'.$match[2], $server_list, TRUE)) |
| 127 | { |
| 128 | log_message('debug', 'Session: Memcached server pool already has '.$match[1].':'.$match[2]); |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | if ( ! $this->_memcached->addServer($match[1], $match[2], isset($match[3]) ? $match[3] : 0)) |
| 133 | { |
| 134 | log_message('error', 'Could not add '.$match[1].':'.$match[2].' to Memcached server pool.'); |
| 135 | } |
| 136 | else |
| 137 | { |
Andrey Andreev | a8f29f9 | 2014-11-10 18:55:55 +0200 | [diff] [blame] | 138 | $server_list[] = $match[1].':'.$match[2]; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | if (empty($server_list)) |
| 143 | { |
| 144 | log_message('error', 'Session: Memcached server pool is empty.'); |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 145 | return $this->_fail(); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 146 | } |
| 147 | |
Andrey Andreev | a9da3dd | 2018-06-12 16:40:12 +0300 | [diff] [blame^] | 148 | $this->php5_validate_id(); |
| 149 | |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 150 | return $this->_success; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // ------------------------------------------------------------------------ |
| 154 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 155 | /** |
| 156 | * Read |
| 157 | * |
| 158 | * Reads session data and acquires a lock |
| 159 | * |
| 160 | * @param string $session_id Session ID |
| 161 | * @return string Serialized session data |
| 162 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 163 | public function read($session_id) |
| 164 | { |
| 165 | if (isset($this->_memcached) && $this->_get_lock($session_id)) |
| 166 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 167 | // Needed by write() to detect session_regenerate_id() calls |
| 168 | $this->_session_id = $session_id; |
| 169 | |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 170 | $session_data = (string) $this->_memcached->get($this->_key_prefix.$session_id); |
| 171 | $this->_fingerprint = md5($session_data); |
| 172 | return $session_data; |
| 173 | } |
| 174 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 175 | return $this->_fail(); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 176 | } |
| 177 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 178 | // ------------------------------------------------------------------------ |
| 179 | |
| 180 | /** |
| 181 | * Write |
| 182 | * |
| 183 | * Writes (create / update) session data |
| 184 | * |
| 185 | * @param string $session_id Session ID |
| 186 | * @param string $session_data Serialized session data |
| 187 | * @return bool |
| 188 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 189 | public function write($session_id, $session_data) |
| 190 | { |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 191 | if ( ! isset($this->_memcached, $this->_lock_key)) |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 192 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 193 | return $this->_fail(); |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 194 | } |
| 195 | // Was the ID regenerated? |
| 196 | elseif ($session_id !== $this->_session_id) |
| 197 | { |
| 198 | if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id)) |
| 199 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 200 | return $this->_fail(); |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | $this->_fingerprint = md5(''); |
| 204 | $this->_session_id = $session_id; |
| 205 | } |
| 206 | |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 207 | $key = $this->_key_prefix.$session_id; |
| 208 | |
| 209 | $this->_memcached->replace($this->_lock_key, time(), 300); |
| 210 | if ($this->_fingerprint !== ($fingerprint = md5($session_data))) |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 211 | { |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 212 | if ($this->_memcached->set($key, $session_data, $this->_config['expiration'])) |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 213 | { |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 214 | $this->_fingerprint = $fingerprint; |
Andrey Andreev | a54a2b9 | 2016-02-10 19:55:39 +0200 | [diff] [blame] | 215 | return $this->_success; |
| 216 | } |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 217 | |
| 218 | return $this->_fail(); |
| 219 | } |
| 220 | elseif ( |
| 221 | $this->_memcached->touch($key, $this->_config['expiration']) |
| 222 | OR ($this->_memcached->getResultCode() === Memcached::RES_NOTFOUND && $this->_memcached->set($key, $session_data, $this->_config['expiration'])) |
| 223 | ) |
| 224 | { |
| 225 | return $this->_success; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 226 | } |
| 227 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 228 | return $this->_fail(); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // ------------------------------------------------------------------------ |
| 232 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 233 | /** |
| 234 | * Close |
| 235 | * |
| 236 | * Releases locks and closes connection. |
| 237 | * |
Gabriel Potkány | 1fb5000 | 2015-02-04 01:45:59 +0100 | [diff] [blame] | 238 | * @return bool |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 239 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 240 | public function close() |
| 241 | { |
| 242 | if (isset($this->_memcached)) |
| 243 | { |
Andrey Andreev | 2159221 | 2016-02-29 17:38:51 +0200 | [diff] [blame] | 244 | $this->_release_lock(); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 245 | if ( ! $this->_memcached->quit()) |
| 246 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 247 | return $this->_fail(); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | $this->_memcached = NULL; |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 251 | return $this->_success; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 252 | } |
| 253 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 254 | return $this->_fail(); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // ------------------------------------------------------------------------ |
| 258 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 259 | /** |
| 260 | * Destroy |
| 261 | * |
| 262 | * Destroys the current session. |
| 263 | * |
| 264 | * @param string $session_id Session ID |
| 265 | * @return bool |
| 266 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 267 | public function destroy($session_id) |
| 268 | { |
| 269 | if (isset($this->_memcached, $this->_lock_key)) |
| 270 | { |
| 271 | $this->_memcached->delete($this->_key_prefix.$session_id); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 272 | $this->_cookie_destroy(); |
| 273 | return $this->_success; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 274 | } |
| 275 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 276 | return $this->_fail(); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // ------------------------------------------------------------------------ |
| 280 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 281 | /** |
| 282 | * Garbage Collector |
| 283 | * |
| 284 | * Deletes expired sessions |
| 285 | * |
| 286 | * @param int $maxlifetime Maximum lifetime of sessions |
| 287 | * @return bool |
| 288 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 289 | public function gc($maxlifetime) |
| 290 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 291 | // Not necessary, Memcached takes care of that. |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 292 | return $this->_success; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 293 | } |
| 294 | |
Andrey Andreev | a9da3dd | 2018-06-12 16:40:12 +0300 | [diff] [blame^] | 295 | // -------------------------------------------------------------------- |
| 296 | |
| 297 | /** |
| 298 | * Validate ID |
| 299 | * |
| 300 | * Checks whether a session ID record exists server-side, |
| 301 | * to enforce session.use_strict_mode. |
| 302 | * |
| 303 | * @param string $id |
| 304 | * @return bool |
| 305 | */ |
| 306 | public function validateId($id) |
| 307 | { |
| 308 | $this->_memcached-get($this->_key_prefix.$id); |
| 309 | return ($this->_memcached->getResultCode() === Memcached::RES_SUCCESS); |
| 310 | } |
| 311 | |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 312 | // ------------------------------------------------------------------------ |
| 313 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 314 | /** |
| 315 | * Get lock |
| 316 | * |
| 317 | * Acquires an (emulated) lock. |
| 318 | * |
| 319 | * @param string $session_id Session ID |
| 320 | * @return bool |
| 321 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 322 | protected function _get_lock($session_id) |
| 323 | { |
Andrey Andreev | 79b8a08 | 2016-01-07 13:55:21 +0200 | [diff] [blame] | 324 | // PHP 7 reuses the SessionHandler object on regeneration, |
| 325 | // so we need to check here if the lock key is for the |
| 326 | // correct session ID. |
| 327 | if ($this->_lock_key === $this->_key_prefix.$session_id.':lock') |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 328 | { |
Andrey Andreev | c4de3c2 | 2016-02-10 07:41:43 +0200 | [diff] [blame] | 329 | if ( ! $this->_memcached->replace($this->_lock_key, time(), 300)) |
| 330 | { |
Andrey Andreev | 8215e2f | 2016-02-11 20:30:43 +0200 | [diff] [blame] | 331 | return ($this->_memcached->getResultCode() === Memcached::RES_NOTFOUND) |
Andrey Andreev | fdf4b59 | 2017-07-06 11:49:13 +0300 | [diff] [blame] | 332 | ? $this->_memcached->add($this->_lock_key, time(), 300) |
Andrey Andreev | c4de3c2 | 2016-02-10 07:41:43 +0200 | [diff] [blame] | 333 | : FALSE; |
| 334 | } |
Andrey Andreev | 615f3d9 | 2018-01-12 12:33:44 +0200 | [diff] [blame] | 335 | |
| 336 | return TRUE; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 337 | } |
| 338 | |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 339 | // 30 attempts to obtain a lock, in case another request already has it |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 340 | $lock_key = $this->_key_prefix.$session_id.':lock'; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 341 | $attempt = 0; |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 342 | do |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 343 | { |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 344 | if ($this->_memcached->get($lock_key)) |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 345 | { |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 346 | sleep(1); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 347 | continue; |
| 348 | } |
| 349 | |
Andrey Andreev | fdf4b59 | 2017-07-06 11:49:13 +0300 | [diff] [blame] | 350 | $method = ($this->_memcached->getResultCode() === Memcached::RES_NOTFOUND) ? 'add' : 'set'; |
| 351 | if ( ! $this->_memcached->$method($lock_key, time(), 300)) |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 352 | { |
| 353 | log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); |
Andrey Andreev | c4de3c2 | 2016-02-10 07:41:43 +0200 | [diff] [blame] | 354 | return FALSE; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | $this->_lock_key = $lock_key; |
| 358 | break; |
| 359 | } |
Andrey Andreev | 73b9e85 | 2015-04-30 13:06:40 +0300 | [diff] [blame] | 360 | while (++$attempt < 30); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 361 | |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 362 | if ($attempt === 30) |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 363 | { |
Master Yoda | c1dc446 | 2015-03-06 22:22:24 -0800 | [diff] [blame] | 364 | log_message('error', 'Session: Unable to obtain lock for '.$this->_key_prefix.$session_id.' after 30 attempts, aborting.'); |
Andrey Andreev | c4de3c2 | 2016-02-10 07:41:43 +0200 | [diff] [blame] | 365 | return FALSE; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | $this->_lock = TRUE; |
Andrey Andreev | c4de3c2 | 2016-02-10 07:41:43 +0200 | [diff] [blame] | 369 | return TRUE; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // ------------------------------------------------------------------------ |
| 373 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 374 | /** |
| 375 | * Release lock |
| 376 | * |
| 377 | * Releases a previously acquired lock |
| 378 | * |
| 379 | * @return bool |
| 380 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 381 | protected function _release_lock() |
| 382 | { |
| 383 | if (isset($this->_memcached, $this->_lock_key) && $this->_lock) |
| 384 | { |
| 385 | if ( ! $this->_memcached->delete($this->_lock_key) && $this->_memcached->getResultCode() !== Memcached::RES_NOTFOUND) |
| 386 | { |
Andrey Andreev | 0002588 | 2015-02-11 16:23:46 +0200 | [diff] [blame] | 387 | log_message('error', 'Session: Error while trying to free lock for '.$this->_lock_key); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 388 | return FALSE; |
| 389 | } |
| 390 | |
| 391 | $this->_lock_key = NULL; |
| 392 | $this->_lock = FALSE; |
| 393 | } |
| 394 | |
| 395 | return TRUE; |
| 396 | } |
Andrey Andreev | 6276926 | 2016-11-29 15:30:30 +0200 | [diff] [blame] | 397 | } |