blob: f34269002b791dbf8aafd87ef15075f10e90f94e [file] [log] [blame]
Andrey Andreev47a47fb2014-05-31 16:08:30 +03001<?php
2/**
3 * CodeIgniter
4 *
Andrey Andreevbf6b11d2015-01-12 17:27:12 +02005 * An open source application development framework for PHP
Andrey Andreev47a47fb2014-05-31 16:08:30 +03006 *
Andrey Andreev46f2f262014-11-11 14:37:51 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev47a47fb2014-05-31 16:08:30 +03008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev47a47fb2014-05-31 16:08:30 +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 Andreev47a47fb2014-05-31 16:08:30 +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 Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, 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 Andreev47a47fb2014-05-31 16:08:30 +030036 * @filesource
37 */
38defined('BASEPATH') OR exit('No direct script access allowed');
39
40/**
41 * CodeIgniter Session Database Driver
42 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020043 * @package CodeIgniter
Andrey Andreev47a47fb2014-05-31 16:08:30 +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 Andreev47a47fb2014-05-31 16:08:30 +030048 */
49class CI_Session_database_driver extends CI_Session_driver implements SessionHandlerInterface {
50
51 /**
52 * DB object
53 *
54 * @var object
55 */
56 protected $_db;
57
58 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +030059 * Row exists flag
60 *
61 * @var bool
62 */
63 protected $_row_exists = FALSE;
64
65 /**
66 * Lock "driver" flag
67 *
68 * @var string
69 */
Andrey Andreeve9ca0122015-01-15 17:42:17 +020070 protected $_platform;
Andrey Andreev47a47fb2014-05-31 16:08:30 +030071
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
84 $CI =& get_instance();
85 isset($CI->db) OR $CI->load->database();
Andrey Andreev00c222d2015-01-29 18:14:31 +020086 $this->_db = $CI->db;
Andrey Andreev47a47fb2014-05-31 16:08:30 +030087
88 if ( ! $this->_db instanceof CI_DB_query_builder)
89 {
90 throw new Exception('Query Builder not enabled for the configured database. Aborting.');
91 }
92 elseif ($this->_db->pconnect)
93 {
94 throw new Exception('Configured database connection is persistent. Aborting.');
95 }
Andrey Andreev737a5662015-03-21 12:41:38 +020096 elseif ($this->_db->cache_on)
97 {
98 throw new Exception('Configured database connection has cache enabled. Aborting.');
99 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300100
101 $db_driver = $this->_db->dbdriver.(empty($this->_db->subdriver) ? '' : '_'.$this->_db->subdriver);
102 if (strpos($db_driver, 'mysql') !== FALSE)
103 {
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200104 $this->_platform = 'mysql';
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300105 }
106 elseif (in_array($db_driver, array('postgre', 'pdo_pgsql'), TRUE))
107 {
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200108 $this->_platform = 'postgre';
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300109 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300110
Andrey Andreev7f8eb362015-01-15 18:01:41 +0200111 // Note: BC work-around for the old 'sess_table_name' setting, should be removed in the future.
Andrey Andreev85dfc2a2016-04-01 22:54:15 +0300112 if ( ! isset($this->_config['save_path']) && ($this->_config['save_path'] = config_item('sess_table_name')))
113 {
114 log_message('debug', 'Session: "sess_save_path" is empty; using BC fallback to "sess_table_name".');
115 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300116 }
117
118 // ------------------------------------------------------------------------
119
Andrey Andreev10411fc2015-01-19 13:54:53 +0200120 /**
121 * Open
122 *
123 * Initializes the database connection
124 *
125 * @param string $save_path Table name
126 * @param string $name Session cookie name, unused
127 * @return bool
128 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300129 public function open($save_path, $name)
130 {
Andrey Andreevaf849692015-12-12 14:07:39 +0200131 if (empty($this->_db->conn_id) && ! $this->_db->db_connect())
132 {
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200133 return $this->_fail();
Andrey Andreevaf849692015-12-12 14:07:39 +0200134 }
135
Andrey Andreeva9da3dd2018-06-12 16:40:12 +0300136 $this->php5_validate_id();
137
Andrey Andreevaf849692015-12-12 14:07:39 +0200138 return $this->_success;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300139 }
140
141 // ------------------------------------------------------------------------
142
Andrey Andreev10411fc2015-01-19 13:54:53 +0200143 /**
144 * Read
145 *
146 * Reads session data and acquires a lock
147 *
148 * @param string $session_id Session ID
149 * @return string Serialized session data
150 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300151 public function read($session_id)
152 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300153 if ($this->_get_lock($session_id) !== FALSE)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300154 {
Andrey Andreevfd5fe1a642016-01-11 11:58:40 +0200155 // Prevent previous QB calls from messing with our queries
156 $this->_db->reset_query();
157
Andrey Andreev7474a672014-10-31 23:35:32 +0200158 // Needed by write() to detect session_regenerate_id() calls
159 $this->_session_id = $session_id;
160
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300161 $this->_db
162 ->select('data')
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300163 ->from($this->_config['save_path'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300164 ->where('id', $session_id);
165
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300166 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300167 {
168 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
169 }
170
Andrey Andreev7bdd4952016-03-10 14:01:09 +0200171 if ( ! ($result = $this->_db->get()) OR ($result = $result->row()) === NULL)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300172 {
Andrey Andreevde8b82c2015-10-18 20:58:38 +0300173 // PHP7 will reuse the same SessionHandler object after
174 // ID regeneration, so we need to explicitly set this to
175 // FALSE instead of relying on the default ...
176 $this->_row_exists = FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300177 $this->_fingerprint = md5('');
178 return '';
179 }
180
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200181 // PostgreSQL's variant of a BLOB datatype is Bytea, which is a
182 // PITA to work with, so we use base64-encoded data in a TEXT
183 // field instead.
Andrey Andreevd0122552015-01-15 21:25:58 +0200184 $result = ($this->_platform === 'postgre')
185 ? base64_decode(rtrim($result->data))
186 : $result->data;
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200187
Andrey Andreevd0122552015-01-15 21:25:58 +0200188 $this->_fingerprint = md5($result);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300189 $this->_row_exists = TRUE;
Andrey Andreev74009752015-01-15 21:36:25 +0200190 return $result;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300191 }
192
193 $this->_fingerprint = md5('');
194 return '';
195 }
196
Andrey Andreev10411fc2015-01-19 13:54:53 +0200197 // ------------------------------------------------------------------------
198
199 /**
200 * Write
201 *
202 * Writes (create / update) session data
203 *
204 * @param string $session_id Session ID
205 * @param string $session_data Serialized session data
206 * @return bool
207 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300208 public function write($session_id, $session_data)
209 {
Andrey Andreevfd5fe1a642016-01-11 11:58:40 +0200210 // Prevent previous QB calls from messing with our queries
211 $this->_db->reset_query();
212
Andrey Andreev7474a672014-10-31 23:35:32 +0200213 // Was the ID regenerated?
Andrey Andreev37226c02017-01-11 12:05:32 +0200214 if (isset($this->_session_id) && $session_id !== $this->_session_id)
Andrey Andreev7474a672014-10-31 23:35:32 +0200215 {
216 if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id))
217 {
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200218 return $this->_fail();
Andrey Andreev7474a672014-10-31 23:35:32 +0200219 }
220
221 $this->_row_exists = FALSE;
222 $this->_session_id = $session_id;
223 }
Andrey Andreev37226c02017-01-11 12:05:32 +0200224 elseif ($this->_lock === FALSE)
225 {
226 return $this->_fail();
227 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300228
229 if ($this->_row_exists === FALSE)
230 {
Andrey Andreev5231d322015-01-19 02:29:49 +0200231 $insert_data = array(
232 'id' => $session_id,
233 'ip_address' => $_SERVER['REMOTE_ADDR'],
234 'timestamp' => time(),
235 'data' => ($this->_platform === 'postgre' ? base64_encode($session_data) : $session_data)
236 );
237
238 if ($this->_db->insert($this->_config['save_path'], $insert_data))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300239 {
240 $this->_fingerprint = md5($session_data);
Andrey Andreevaf849692015-12-12 14:07:39 +0200241 $this->_row_exists = TRUE;
242 return $this->_success;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300243 }
244
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200245 return $this->_fail();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300246 }
247
248 $this->_db->where('id', $session_id);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300249 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300250 {
251 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
252 }
253
Andrey Andreevc33c3ad2015-01-19 10:54:21 +0200254 $update_data = array('timestamp' => time());
Andrey Andreev5231d322015-01-19 02:29:49 +0200255 if ($this->_fingerprint !== md5($session_data))
256 {
257 $update_data['data'] = ($this->_platform === 'postgre')
258 ? base64_encode($session_data)
259 : $session_data;
260 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300261
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300262 if ($this->_db->update($this->_config['save_path'], $update_data))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300263 {
264 $this->_fingerprint = md5($session_data);
Andrey Andreevaf849692015-12-12 14:07:39 +0200265 return $this->_success;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300266 }
267
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200268 return $this->_fail();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300269 }
270
271 // ------------------------------------------------------------------------
272
Andrey Andreev10411fc2015-01-19 13:54:53 +0200273 /**
274 * Close
275 *
276 * Releases locks
277 *
Gabriel Potkány1fb50002015-02-04 01:45:59 +0100278 * @return bool
Andrey Andreev10411fc2015-01-19 13:54:53 +0200279 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300280 public function close()
281 {
Andrey Andreevaf849692015-12-12 14:07:39 +0200282 return ($this->_lock && ! $this->_release_lock())
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200283 ? $this->_fail()
Andrey Andreevaf849692015-12-12 14:07:39 +0200284 : $this->_success;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300285 }
286
287 // ------------------------------------------------------------------------
288
Andrey Andreev10411fc2015-01-19 13:54:53 +0200289 /**
290 * Destroy
291 *
292 * Destroys the current session.
293 *
294 * @param string $session_id Session ID
295 * @return bool
296 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300297 public function destroy($session_id)
298 {
299 if ($this->_lock)
300 {
Andrey Andreevfd5fe1a642016-01-11 11:58:40 +0200301 // Prevent previous QB calls from messing with our queries
302 $this->_db->reset_query();
303
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300304 $this->_db->where('id', $session_id);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300305 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300306 {
307 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
308 }
309
Andrey Andreevaf849692015-12-12 14:07:39 +0200310 if ( ! $this->_db->delete($this->_config['save_path']))
311 {
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200312 return $this->_fail();
Andrey Andreevaf849692015-12-12 14:07:39 +0200313 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300314 }
315
Andrey Andreevbb71dba2015-12-15 13:00:52 +0200316 if ($this->close() === $this->_success)
Andrey Andreevaf849692015-12-12 14:07:39 +0200317 {
318 $this->_cookie_destroy();
319 return $this->_success;
320 }
321
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200322 return $this->_fail();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300323 }
324
325 // ------------------------------------------------------------------------
326
Andrey Andreev10411fc2015-01-19 13:54:53 +0200327 /**
328 * Garbage Collector
329 *
330 * Deletes expired sessions
331 *
332 * @param int $maxlifetime Maximum lifetime of sessions
333 * @return bool
334 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300335 public function gc($maxlifetime)
336 {
Andrey Andreevfd5fe1a642016-01-11 11:58:40 +0200337 // Prevent previous QB calls from messing with our queries
338 $this->_db->reset_query();
339
Andrey Andreevaf849692015-12-12 14:07:39 +0200340 return ($this->_db->delete($this->_config['save_path'], 'timestamp < '.(time() - $maxlifetime)))
341 ? $this->_success
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200342 : $this->_fail();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300343 }
344
Andrey Andreeva9da3dd2018-06-12 16:40:12 +0300345 // --------------------------------------------------------------------
346
347 /**
348 * Validate ID
349 *
350 * Checks whether a session ID record exists server-side,
351 * to enforce session.use_strict_mode.
352 *
353 * @param string $id
354 * @return bool
355 */
Michiel Vugteveend3e92732018-08-07 15:23:21 +0200356 public function validateSessionId($id)
Andrey Andreeva9da3dd2018-06-12 16:40:12 +0300357 {
358 // Prevent previous QB calls from messing with our queries
359 $this->_db->reset_query();
360
361 $this->_db->select('1')->from($this->_config['save_path'])->where('id', $id);
362 empty($this->_config['match_ip']) OR $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
363 $result = $this->_db->get();
364 empty($result) OR $result = $result->row();
365
366 return ! empty($result);
367 }
368
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300369 // ------------------------------------------------------------------------
370
Andrey Andreev10411fc2015-01-19 13:54:53 +0200371 /**
372 * Get lock
373 *
374 * Acquires a lock, depending on the underlying platform.
375 *
376 * @param string $session_id Session ID
377 * @return bool
378 */
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300379 protected function _get_lock($session_id)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300380 {
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200381 if ($this->_platform === 'mysql')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300382 {
Andrey Andreeve49aa1f2016-11-22 12:02:55 +0200383 $arg = md5($session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : ''));
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200384 if ($this->_db->query("SELECT GET_LOCK('".$arg."', 300) AS ci_session_lock")->row()->ci_session_lock)
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300385 {
386 $this->_lock = $arg;
387 return TRUE;
388 }
389
390 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300391 }
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200392 elseif ($this->_platform === 'postgre')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300393 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300394 $arg = "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" : '');
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300395 if ($this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')'))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300396 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300397 $this->_lock = $arg;
398 return TRUE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300399 }
400
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300401 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300402 }
403
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300404 return parent::_get_lock($session_id);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300405 }
406
407 // ------------------------------------------------------------------------
408
Andrey Andreev10411fc2015-01-19 13:54:53 +0200409 /**
410 * Release lock
411 *
412 * Releases a previously acquired lock
413 *
414 * @return bool
415 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300416 protected function _release_lock()
417 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300418 if ( ! $this->_lock)
419 {
420 return TRUE;
421 }
422
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200423 if ($this->_platform === 'mysql')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300424 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300425 if ($this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock)
426 {
427 $this->_lock = FALSE;
428 return TRUE;
429 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300430
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300431 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300432 }
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200433 elseif ($this->_platform === 'postgre')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300434 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300435 if ($this->_db->simple_query('SELECT pg_advisory_unlock('.$this->_lock.')'))
436 {
437 $this->_lock = FALSE;
438 return TRUE;
439 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300440
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300441 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300442 }
443
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300444 return parent::_release_lock();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300445 }
Andrey Andreeve49aa1f2016-11-22 12:02:55 +0200446}