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 | bf6b11d | 2015-01-12 17:27:12 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2015, 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 | c9eface | 2014-09-02 15:19:01 +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 | 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 |
| 47 | * @link http://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 | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 120 | return FALSE; |
| 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.'); |
| 145 | return FALSE; |
| 146 | } |
| 147 | |
| 148 | return TRUE; |
| 149 | } |
| 150 | |
| 151 | // ------------------------------------------------------------------------ |
| 152 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame^] | 153 | /** |
| 154 | * Read |
| 155 | * |
| 156 | * Reads session data and acquires a lock |
| 157 | * |
| 158 | * @param string $session_id Session ID |
| 159 | * @return string Serialized session data |
| 160 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 161 | public function read($session_id) |
| 162 | { |
| 163 | if (isset($this->_memcached) && $this->_get_lock($session_id)) |
| 164 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 165 | // Needed by write() to detect session_regenerate_id() calls |
| 166 | $this->_session_id = $session_id; |
| 167 | |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 168 | $session_data = (string) $this->_memcached->get($this->_key_prefix.$session_id); |
| 169 | $this->_fingerprint = md5($session_data); |
| 170 | return $session_data; |
| 171 | } |
| 172 | |
| 173 | return FALSE; |
| 174 | } |
| 175 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame^] | 176 | // ------------------------------------------------------------------------ |
| 177 | |
| 178 | /** |
| 179 | * Write |
| 180 | * |
| 181 | * Writes (create / update) session data |
| 182 | * |
| 183 | * @param string $session_id Session ID |
| 184 | * @param string $session_data Serialized session data |
| 185 | * @return bool |
| 186 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 187 | public function write($session_id, $session_data) |
| 188 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 189 | if ( ! isset($this->_memcached)) |
| 190 | { |
| 191 | return FALSE; |
| 192 | } |
| 193 | // Was the ID regenerated? |
| 194 | elseif ($session_id !== $this->_session_id) |
| 195 | { |
| 196 | if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id)) |
| 197 | { |
| 198 | return FALSE; |
| 199 | } |
| 200 | |
| 201 | $this->_fingerprint = md5(''); |
| 202 | $this->_session_id = $session_id; |
| 203 | } |
| 204 | |
| 205 | if (isset($this->_lock_key)) |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 206 | { |
| 207 | $this->_memcached->replace($this->_lock_key, time(), 5); |
| 208 | if ($this->_fingerprint !== ($fingerprint = md5($session_data))) |
| 209 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 210 | if ($this->_memcached->set($this->_key_prefix.$session_id, $session_data, $this->_config['expiration'])) |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 211 | { |
| 212 | $this->_fingerprint = $fingerprint; |
| 213 | return TRUE; |
| 214 | } |
| 215 | |
| 216 | return FALSE; |
| 217 | } |
| 218 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 219 | return $this->_memcached->touch($this->_key_prefix.$session_id, $this->_config['expiration']); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return FALSE; |
| 223 | } |
| 224 | |
| 225 | // ------------------------------------------------------------------------ |
| 226 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame^] | 227 | /** |
| 228 | * Close |
| 229 | * |
| 230 | * Releases locks and closes connection. |
| 231 | * |
| 232 | * @return void |
| 233 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 234 | public function close() |
| 235 | { |
| 236 | if (isset($this->_memcached)) |
| 237 | { |
| 238 | isset($this->_lock_key) && $this->_memcached->delete($this->_lock_key); |
| 239 | if ( ! $this->_memcached->quit()) |
| 240 | { |
| 241 | return FALSE; |
| 242 | } |
| 243 | |
| 244 | $this->_memcached = NULL; |
| 245 | return TRUE; |
| 246 | } |
| 247 | |
| 248 | return FALSE; |
| 249 | } |
| 250 | |
| 251 | // ------------------------------------------------------------------------ |
| 252 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame^] | 253 | /** |
| 254 | * Destroy |
| 255 | * |
| 256 | * Destroys the current session. |
| 257 | * |
| 258 | * @param string $session_id Session ID |
| 259 | * @return bool |
| 260 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 261 | public function destroy($session_id) |
| 262 | { |
| 263 | if (isset($this->_memcached, $this->_lock_key)) |
| 264 | { |
| 265 | $this->_memcached->delete($this->_key_prefix.$session_id); |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 266 | return $this->_cookie_destroy(); |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 267 | } |
| 268 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 269 | return FALSE; |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // ------------------------------------------------------------------------ |
| 273 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame^] | 274 | /** |
| 275 | * Garbage Collector |
| 276 | * |
| 277 | * Deletes expired sessions |
| 278 | * |
| 279 | * @param int $maxlifetime Maximum lifetime of sessions |
| 280 | * @return bool |
| 281 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 282 | public function gc($maxlifetime) |
| 283 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 284 | // Not necessary, Memcached takes care of that. |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 285 | return TRUE; |
| 286 | } |
| 287 | |
| 288 | // ------------------------------------------------------------------------ |
| 289 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame^] | 290 | /** |
| 291 | * Get lock |
| 292 | * |
| 293 | * Acquires an (emulated) lock. |
| 294 | * |
| 295 | * @param string $session_id Session ID |
| 296 | * @return bool |
| 297 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 298 | protected function _get_lock($session_id) |
| 299 | { |
| 300 | if (isset($this->_lock_key)) |
| 301 | { |
| 302 | return $this->_memcached->replace($this->_lock_key, time(), 5); |
| 303 | } |
| 304 | |
| 305 | $lock_key = $this->_key_prefix.$session_id.':lock'; |
| 306 | if ( ! ($ts = $this->_memcached->get($lock_key))) |
| 307 | { |
| 308 | if ( ! $this->_memcached->set($lock_key, TRUE, 5)) |
| 309 | { |
| 310 | log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); |
| 311 | return FALSE; |
| 312 | } |
| 313 | |
| 314 | $this->_lock_key = $lock_key; |
| 315 | $this->_lock = TRUE; |
| 316 | return TRUE; |
| 317 | } |
| 318 | |
| 319 | // Another process has the lock, we'll try to wait for it to free itself ... |
| 320 | $attempt = 0; |
| 321 | while ($attempt++ < 5) |
| 322 | { |
| 323 | usleep(((time() - $ts) * 1000000) - 20000); |
| 324 | if (($ts = $this->_memcached->get($lock_key)) < time()) |
| 325 | { |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | if ( ! $this->_memcached->set($lock_key, time(), 5)) |
| 330 | { |
| 331 | log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id); |
| 332 | return FALSE; |
| 333 | } |
| 334 | |
| 335 | $this->_lock_key = $lock_key; |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | if ($attempt === 5) |
| 340 | { |
| 341 | log_message('error', 'Session: Unable to obtain lock for '.$this->_key_prefix.$session_id.' after 5 attempts, aborting.'); |
| 342 | return FALSE; |
| 343 | } |
| 344 | |
| 345 | $this->_lock = TRUE; |
| 346 | return TRUE; |
| 347 | } |
| 348 | |
| 349 | // ------------------------------------------------------------------------ |
| 350 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame^] | 351 | /** |
| 352 | * Release lock |
| 353 | * |
| 354 | * Releases a previously acquired lock |
| 355 | * |
| 356 | * @return bool |
| 357 | */ |
Andrey Andreev | c9eface | 2014-09-02 15:19:01 +0300 | [diff] [blame] | 358 | protected function _release_lock() |
| 359 | { |
| 360 | if (isset($this->_memcached, $this->_lock_key) && $this->_lock) |
| 361 | { |
| 362 | if ( ! $this->_memcached->delete($this->_lock_key) && $this->_memcached->getResultCode() !== Memcached::RES_NOTFOUND) |
| 363 | { |
| 364 | log_message('error', 'Session: Error while trying to free lock for '.$this->_key_prefix.$session_id); |
| 365 | return FALSE; |
| 366 | } |
| 367 | |
| 368 | $this->_lock_key = NULL; |
| 369 | $this->_lock = FALSE; |
| 370 | } |
| 371 | |
| 372 | return TRUE; |
| 373 | } |
| 374 | |
| 375 | } |
| 376 | |
| 377 | /* End of file Session_memcached_driver.php */ |
| 378 | /* Location: ./system/libraries/Session/drivers/Session_memcached_driver.php */ |