blob: e9246443caf3e71b0b986c207093aaac2d5160a9 [file] [log] [blame]
Andrey Andreevc9eface2014-09-02 15:19:01 +03001<?php
2/**
3 * CodeIgniter
4 *
Andrey Andreevbf6b11d2015-01-12 17:27:12 +02005 * An open source application development framework for PHP
Andrey Andreevc9eface2014-09-02 15:19:01 +03006 *
Andrey Andreev46f2f262014-11-11 14:37:51 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevc9eface2014-09-02 15:19:01 +03008 *
Andrey Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
Andrey Andreevc9eface2014-09-02 15:19:01 +030010 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020011 * 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 Andreevc9eface2014-09-02 15:19:01 +030017 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreev46f2f262014-11-11 14:37:51 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreev46f2f262014-11-11 14:37:51 +020035 * @since Version 3.0.0
Andrey Andreevc9eface2014-09-02 15:19:01 +030036 * @filesource
37 */
38defined('BASEPATH') OR exit('No direct script access allowed');
39
40/**
41 * CodeIgniter Session Memcached Driver
42 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020043 * @package CodeIgniter
Andrey Andreevc9eface2014-09-02 15:19:01 +030044 * @subpackage Libraries
45 * @category Sessions
Andrey Andreev46f2f262014-11-11 14:37:51 +020046 * @author Andrey Andreev
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/sessions.html
Andrey Andreevc9eface2014-09-02 15:19:01 +030048 */
49class CI_Session_memcached_driver extends CI_Session_driver implements SessionHandlerInterface {
50
51 /**
Andrey Andreevc9eface2014-09-02 15:19:01 +030052 * 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 Andreevdfb39be2014-10-06 01:50:14 +030084 if (empty($this->_config['save_path']))
Andrey Andreevc9eface2014-09-02 15:19:01 +030085 {
86 log_message('error', 'Session: No Memcached save path configured.');
87 }
88
Andrey Andreevdfb39be2014-10-06 01:50:14 +030089 if ($this->_config['match_ip'] === TRUE)
Andrey Andreevc9eface2014-09-02 15:19:01 +030090 {
91 $this->_key_prefix .= $_SERVER['REMOTE_ADDR'].':';
92 }
93 }
94
95 // ------------------------------------------------------------------------
96
Andrey Andreev10411fc2015-01-19 13:54:53 +020097 /**
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 Andreevc9eface2014-09-02 15:19:01 +0300106 public function open($save_path, $name)
107 {
108 $this->_memcached = new Memcached();
Andrey Andreev4f502562014-11-10 19:18:33 +0200109 $this->_memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, TRUE); // required for touch() usage
Andrey Andreevc9eface2014-09-02 15:19:01 +0300110 $server_list = array();
111 foreach ($this->_memcached->getServerList() as $server)
112 {
113 $server_list[] = $server['host'].':'.$server['port'];
114 }
115
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300116 if ( ! preg_match_all('#,?([^,:]+)\:(\d{1,5})(?:\:(\d+))?#', $this->_config['save_path'], $matches, PREG_SET_ORDER))
Andrey Andreevc9eface2014-09-02 15:19:01 +0300117 {
118 $this->_memcached = NULL;
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300119 log_message('error', 'Session: Invalid Memcached save path format: '.$this->_config['save_path']);
Andrey Andreevaf849692015-12-12 14:07:39 +0200120 return $this->_failure;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300121 }
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 Andreeva8f29f92014-11-10 18:55:55 +0200138 $server_list[] = $match[1].':'.$match[2];
Andrey Andreevc9eface2014-09-02 15:19:01 +0300139 }
140 }
141
142 if (empty($server_list))
143 {
144 log_message('error', 'Session: Memcached server pool is empty.');
Andrey Andreevaf849692015-12-12 14:07:39 +0200145 return $this->_failure;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300146 }
147
Andrey Andreevaf849692015-12-12 14:07:39 +0200148 return $this->_success;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300149 }
150
151 // ------------------------------------------------------------------------
152
Andrey Andreev10411fc2015-01-19 13:54:53 +0200153 /**
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 Andreevc9eface2014-09-02 15:19:01 +0300161 public function read($session_id)
162 {
163 if (isset($this->_memcached) && $this->_get_lock($session_id))
164 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200165 // Needed by write() to detect session_regenerate_id() calls
166 $this->_session_id = $session_id;
167
Andrey Andreevc9eface2014-09-02 15:19:01 +0300168 $session_data = (string) $this->_memcached->get($this->_key_prefix.$session_id);
169 $this->_fingerprint = md5($session_data);
170 return $session_data;
171 }
172
Andrey Andreevaf849692015-12-12 14:07:39 +0200173 return $this->_failure;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300174 }
175
Andrey Andreev10411fc2015-01-19 13:54:53 +0200176 // ------------------------------------------------------------------------
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 Andreevc9eface2014-09-02 15:19:01 +0300187 public function write($session_id, $session_data)
188 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200189 if ( ! isset($this->_memcached))
190 {
Andrey Andreevaf849692015-12-12 14:07:39 +0200191 return $this->_failure;
Andrey Andreev7474a672014-10-31 23:35:32 +0200192 }
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 {
Andrey Andreevaf849692015-12-12 14:07:39 +0200198 return $this->_failure;
Andrey Andreev7474a672014-10-31 23:35:32 +0200199 }
200
201 $this->_fingerprint = md5('');
202 $this->_session_id = $session_id;
203 }
204
205 if (isset($this->_lock_key))
Andrey Andreevc9eface2014-09-02 15:19:01 +0300206 {
Andrey Andreevc4de3c22016-02-10 07:41:43 +0200207 $key = $this->_key_prefix.$session_id;
208
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200209 $this->_memcached->replace($this->_lock_key, time(), 300);
Andrey Andreevc9eface2014-09-02 15:19:01 +0300210 if ($this->_fingerprint !== ($fingerprint = md5($session_data)))
211 {
Andrey Andreevc4de3c22016-02-10 07:41:43 +0200212
213 if (
214 $this->_memcached->replace($key, $session_data, $this->_config['expiration'])
215 OR ($this->_memcached->getResultCode() === Memcached::RES_NOTSTORED && $this->_memcached->set($key, $session_data, $this->_config['expiration']))
216 )
Andrey Andreevc9eface2014-09-02 15:19:01 +0300217 {
218 $this->_fingerprint = $fingerprint;
Andrey Andreevaf849692015-12-12 14:07:39 +0200219 return $this->_success;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300220 }
221
Andrey Andreevaf849692015-12-12 14:07:39 +0200222 return $this->_failure;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300223 }
224
Andrey Andreevaf849692015-12-12 14:07:39 +0200225 return $this->_memcached->touch($this->_key_prefix.$session_id, $this->_config['expiration'])
226 ? $this->_success
227 : $this->_failure;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300228 }
229
Andrey Andreevaf849692015-12-12 14:07:39 +0200230 return $this->_failure;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300231 }
232
233 // ------------------------------------------------------------------------
234
Andrey Andreev10411fc2015-01-19 13:54:53 +0200235 /**
236 * Close
237 *
238 * Releases locks and closes connection.
239 *
Gabriel Potkány1fb50002015-02-04 01:45:59 +0100240 * @return bool
Andrey Andreev10411fc2015-01-19 13:54:53 +0200241 */
Andrey Andreevc9eface2014-09-02 15:19:01 +0300242 public function close()
243 {
244 if (isset($this->_memcached))
245 {
246 isset($this->_lock_key) && $this->_memcached->delete($this->_lock_key);
247 if ( ! $this->_memcached->quit())
248 {
Andrey Andreevaf849692015-12-12 14:07:39 +0200249 return $this->_failure;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300250 }
251
252 $this->_memcached = NULL;
Andrey Andreevaf849692015-12-12 14:07:39 +0200253 return $this->_success;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300254 }
255
Andrey Andreevaf849692015-12-12 14:07:39 +0200256 return $this->_failure;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300257 }
258
259 // ------------------------------------------------------------------------
260
Andrey Andreev10411fc2015-01-19 13:54:53 +0200261 /**
262 * Destroy
263 *
264 * Destroys the current session.
265 *
266 * @param string $session_id Session ID
267 * @return bool
268 */
Andrey Andreevc9eface2014-09-02 15:19:01 +0300269 public function destroy($session_id)
270 {
271 if (isset($this->_memcached, $this->_lock_key))
272 {
273 $this->_memcached->delete($this->_key_prefix.$session_id);
Andrey Andreevaf849692015-12-12 14:07:39 +0200274 $this->_cookie_destroy();
275 return $this->_success;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300276 }
277
Andrey Andreevaf849692015-12-12 14:07:39 +0200278 return $this->_failure;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300279 }
280
281 // ------------------------------------------------------------------------
282
Andrey Andreev10411fc2015-01-19 13:54:53 +0200283 /**
284 * Garbage Collector
285 *
286 * Deletes expired sessions
287 *
288 * @param int $maxlifetime Maximum lifetime of sessions
289 * @return bool
290 */
Andrey Andreevc9eface2014-09-02 15:19:01 +0300291 public function gc($maxlifetime)
292 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200293 // Not necessary, Memcached takes care of that.
Andrey Andreevaf849692015-12-12 14:07:39 +0200294 return $this->_success;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300295 }
296
297 // ------------------------------------------------------------------------
298
Andrey Andreev10411fc2015-01-19 13:54:53 +0200299 /**
300 * Get lock
301 *
302 * Acquires an (emulated) lock.
303 *
304 * @param string $session_id Session ID
305 * @return bool
306 */
Andrey Andreevc9eface2014-09-02 15:19:01 +0300307 protected function _get_lock($session_id)
308 {
Andrey Andreev79b8a082016-01-07 13:55:21 +0200309 // PHP 7 reuses the SessionHandler object on regeneration,
310 // so we need to check here if the lock key is for the
311 // correct session ID.
312 if ($this->_lock_key === $this->_key_prefix.$session_id.':lock')
Andrey Andreevc9eface2014-09-02 15:19:01 +0300313 {
Andrey Andreevc4de3c22016-02-10 07:41:43 +0200314 if ( ! $this->_memcached->replace($this->_lock_key, time(), 300))
315 {
316 return ($this->_memcached->getResultCode() === Memcached::RES_NOTSTORED)
317 ? $this->_memcached->set($this->_lock_key, time(), 300)
318 : FALSE;
319 }
Andrey Andreevc9eface2014-09-02 15:19:01 +0300320 }
321
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200322 // 30 attempts to obtain a lock, in case another request already has it
Andrey Andreevc9eface2014-09-02 15:19:01 +0300323 $lock_key = $this->_key_prefix.$session_id.':lock';
Andrey Andreevc9eface2014-09-02 15:19:01 +0300324 $attempt = 0;
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200325 do
Andrey Andreevc9eface2014-09-02 15:19:01 +0300326 {
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200327 if ($this->_memcached->get($lock_key))
Andrey Andreevc9eface2014-09-02 15:19:01 +0300328 {
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200329 sleep(1);
Andrey Andreevc9eface2014-09-02 15:19:01 +0300330 continue;
331 }
332
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200333 if ( ! $this->_memcached->set($lock_key, time(), 300))
Andrey Andreevc9eface2014-09-02 15:19:01 +0300334 {
335 log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id);
Andrey Andreevc4de3c22016-02-10 07:41:43 +0200336 return FALSE;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300337 }
338
339 $this->_lock_key = $lock_key;
340 break;
341 }
Andrey Andreev73b9e852015-04-30 13:06:40 +0300342 while (++$attempt < 30);
Andrey Andreevc9eface2014-09-02 15:19:01 +0300343
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200344 if ($attempt === 30)
Andrey Andreevc9eface2014-09-02 15:19:01 +0300345 {
Master Yodac1dc4462015-03-06 22:22:24 -0800346 log_message('error', 'Session: Unable to obtain lock for '.$this->_key_prefix.$session_id.' after 30 attempts, aborting.');
Andrey Andreevc4de3c22016-02-10 07:41:43 +0200347 return FALSE;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300348 }
349
350 $this->_lock = TRUE;
Andrey Andreevc4de3c22016-02-10 07:41:43 +0200351 return TRUE;
Andrey Andreevc9eface2014-09-02 15:19:01 +0300352 }
353
354 // ------------------------------------------------------------------------
355
Andrey Andreev10411fc2015-01-19 13:54:53 +0200356 /**
357 * Release lock
358 *
359 * Releases a previously acquired lock
360 *
361 * @return bool
362 */
Andrey Andreevc9eface2014-09-02 15:19:01 +0300363 protected function _release_lock()
364 {
365 if (isset($this->_memcached, $this->_lock_key) && $this->_lock)
366 {
367 if ( ! $this->_memcached->delete($this->_lock_key) && $this->_memcached->getResultCode() !== Memcached::RES_NOTFOUND)
368 {
Andrey Andreev00025882015-02-11 16:23:46 +0200369 log_message('error', 'Session: Error while trying to free lock for '.$this->_lock_key);
Andrey Andreevc9eface2014-09-02 15:19:01 +0300370 return FALSE;
371 }
372
373 $this->_lock_key = NULL;
374 $this->_lock = FALSE;
375 }
376
377 return TRUE;
378 }
Andrey Andreevc4de3c22016-02-10 07:41:43 +0200379}