blob: da0331220fa6fba6eb0b3fdddc6021a563aa3b08 [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 Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, 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 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 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 Andreevdfb39be2014-10-06 01:50:14 +0300112 isset($this->_config['save_path']) OR $this->_config['save_path'] = config_item('sess_table_name');
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300113 }
114
115 // ------------------------------------------------------------------------
116
Andrey Andreev10411fc2015-01-19 13:54:53 +0200117 /**
118 * Open
119 *
120 * Initializes the database connection
121 *
122 * @param string $save_path Table name
123 * @param string $name Session cookie name, unused
124 * @return bool
125 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300126 public function open($save_path, $name)
127 {
Andrey Andreevaf849692015-12-12 14:07:39 +0200128 if (empty($this->_db->conn_id) && ! $this->_db->db_connect())
129 {
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200130 return $this->_fail();
Andrey Andreevaf849692015-12-12 14:07:39 +0200131 }
132
133 return $this->_success;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300134 }
135
136 // ------------------------------------------------------------------------
137
Andrey Andreev10411fc2015-01-19 13:54:53 +0200138 /**
139 * Read
140 *
141 * Reads session data and acquires a lock
142 *
143 * @param string $session_id Session ID
144 * @return string Serialized session data
145 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300146 public function read($session_id)
147 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300148 if ($this->_get_lock($session_id) !== FALSE)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300149 {
Andrey Andreevfd5fe1a642016-01-11 11:58:40 +0200150 // Prevent previous QB calls from messing with our queries
151 $this->_db->reset_query();
152
Andrey Andreev7474a672014-10-31 23:35:32 +0200153 // Needed by write() to detect session_regenerate_id() calls
154 $this->_session_id = $session_id;
155
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300156 $this->_db
157 ->select('data')
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300158 ->from($this->_config['save_path'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300159 ->where('id', $session_id);
160
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300161 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300162 {
163 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
164 }
165
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200166 if ( ! ($result = $this->_db->get()) OR $result->row() === NULL)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300167 {
Andrey Andreevde8b82c2015-10-18 20:58:38 +0300168 // PHP7 will reuse the same SessionHandler object after
169 // ID regeneration, so we need to explicitly set this to
170 // FALSE instead of relying on the default ...
171 $this->_row_exists = FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300172 $this->_fingerprint = md5('');
173 return '';
174 }
175
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200176 // PostgreSQL's variant of a BLOB datatype is Bytea, which is a
177 // PITA to work with, so we use base64-encoded data in a TEXT
178 // field instead.
Andrey Andreevd0122552015-01-15 21:25:58 +0200179 $result = ($this->_platform === 'postgre')
180 ? base64_decode(rtrim($result->data))
181 : $result->data;
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200182
Andrey Andreevd0122552015-01-15 21:25:58 +0200183 $this->_fingerprint = md5($result);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300184 $this->_row_exists = TRUE;
Andrey Andreev74009752015-01-15 21:36:25 +0200185 return $result;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300186 }
187
188 $this->_fingerprint = md5('');
189 return '';
190 }
191
Andrey Andreev10411fc2015-01-19 13:54:53 +0200192 // ------------------------------------------------------------------------
193
194 /**
195 * Write
196 *
197 * Writes (create / update) session data
198 *
199 * @param string $session_id Session ID
200 * @param string $session_data Serialized session data
201 * @return bool
202 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300203 public function write($session_id, $session_data)
204 {
Andrey Andreevfd5fe1a642016-01-11 11:58:40 +0200205 // Prevent previous QB calls from messing with our queries
206 $this->_db->reset_query();
207
Andrey Andreev7474a672014-10-31 23:35:32 +0200208 // Was the ID regenerated?
Shakespeare2000305186d2014-11-02 11:28:47 +0100209 if ($session_id !== $this->_session_id)
Andrey Andreev7474a672014-10-31 23:35:32 +0200210 {
211 if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id))
212 {
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200213 return $this->_fail();
Andrey Andreev7474a672014-10-31 23:35:32 +0200214 }
215
216 $this->_row_exists = FALSE;
217 $this->_session_id = $session_id;
218 }
Shakespeare2000305186d2014-11-02 11:28:47 +0100219 elseif ($this->_lock === FALSE)
220 {
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200221 return $this->_fail();
Shakespeare2000305186d2014-11-02 11:28:47 +0100222 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300223
224 if ($this->_row_exists === FALSE)
225 {
Andrey Andreev5231d322015-01-19 02:29:49 +0200226 $insert_data = array(
227 'id' => $session_id,
228 'ip_address' => $_SERVER['REMOTE_ADDR'],
229 'timestamp' => time(),
230 'data' => ($this->_platform === 'postgre' ? base64_encode($session_data) : $session_data)
231 );
232
233 if ($this->_db->insert($this->_config['save_path'], $insert_data))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300234 {
235 $this->_fingerprint = md5($session_data);
Andrey Andreevaf849692015-12-12 14:07:39 +0200236 $this->_row_exists = TRUE;
237 return $this->_success;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300238 }
239
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200240 return $this->_fail();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300241 }
242
243 $this->_db->where('id', $session_id);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300244 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300245 {
246 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
247 }
248
Andrey Andreevc33c3ad2015-01-19 10:54:21 +0200249 $update_data = array('timestamp' => time());
Andrey Andreev5231d322015-01-19 02:29:49 +0200250 if ($this->_fingerprint !== md5($session_data))
251 {
252 $update_data['data'] = ($this->_platform === 'postgre')
253 ? base64_encode($session_data)
254 : $session_data;
255 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300256
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300257 if ($this->_db->update($this->_config['save_path'], $update_data))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300258 {
259 $this->_fingerprint = md5($session_data);
Andrey Andreevaf849692015-12-12 14:07:39 +0200260 return $this->_success;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300261 }
262
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200263 return $this->_fail();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300264 }
265
266 // ------------------------------------------------------------------------
267
Andrey Andreev10411fc2015-01-19 13:54:53 +0200268 /**
269 * Close
270 *
271 * Releases locks
272 *
Gabriel Potkány1fb50002015-02-04 01:45:59 +0100273 * @return bool
Andrey Andreev10411fc2015-01-19 13:54:53 +0200274 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300275 public function close()
276 {
Andrey Andreevaf849692015-12-12 14:07:39 +0200277 return ($this->_lock && ! $this->_release_lock())
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200278 ? $this->_fail()
Andrey Andreevaf849692015-12-12 14:07:39 +0200279 : $this->_success;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300280 }
281
282 // ------------------------------------------------------------------------
283
Andrey Andreev10411fc2015-01-19 13:54:53 +0200284 /**
285 * Destroy
286 *
287 * Destroys the current session.
288 *
289 * @param string $session_id Session ID
290 * @return bool
291 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300292 public function destroy($session_id)
293 {
294 if ($this->_lock)
295 {
Andrey Andreevfd5fe1a642016-01-11 11:58:40 +0200296 // Prevent previous QB calls from messing with our queries
297 $this->_db->reset_query();
298
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300299 $this->_db->where('id', $session_id);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300300 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300301 {
302 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
303 }
304
Andrey Andreevaf849692015-12-12 14:07:39 +0200305 if ( ! $this->_db->delete($this->_config['save_path']))
306 {
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200307 return $this->_fail();
Andrey Andreevaf849692015-12-12 14:07:39 +0200308 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300309 }
310
Andrey Andreevbb71dba2015-12-15 13:00:52 +0200311 if ($this->close() === $this->_success)
Andrey Andreevaf849692015-12-12 14:07:39 +0200312 {
313 $this->_cookie_destroy();
314 return $this->_success;
315 }
316
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200317 return $this->_fail();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300318 }
319
320 // ------------------------------------------------------------------------
321
Andrey Andreev10411fc2015-01-19 13:54:53 +0200322 /**
323 * Garbage Collector
324 *
325 * Deletes expired sessions
326 *
327 * @param int $maxlifetime Maximum lifetime of sessions
328 * @return bool
329 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300330 public function gc($maxlifetime)
331 {
Andrey Andreevfd5fe1a642016-01-11 11:58:40 +0200332 // Prevent previous QB calls from messing with our queries
333 $this->_db->reset_query();
334
Andrey Andreevaf849692015-12-12 14:07:39 +0200335 return ($this->_db->delete($this->_config['save_path'], 'timestamp < '.(time() - $maxlifetime)))
336 ? $this->_success
Andrey Andreeva027a7f2016-03-10 13:59:20 +0200337 : $this->_fail();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300338 }
339
340 // ------------------------------------------------------------------------
341
Andrey Andreev10411fc2015-01-19 13:54:53 +0200342 /**
343 * Get lock
344 *
345 * Acquires a lock, depending on the underlying platform.
346 *
347 * @param string $session_id Session ID
348 * @return bool
349 */
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300350 protected function _get_lock($session_id)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300351 {
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200352 if ($this->_platform === 'mysql')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300353 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300354 $arg = $session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : '');
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200355 if ($this->_db->query("SELECT GET_LOCK('".$arg."', 300) AS ci_session_lock")->row()->ci_session_lock)
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300356 {
357 $this->_lock = $arg;
358 return TRUE;
359 }
360
361 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300362 }
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200363 elseif ($this->_platform === 'postgre')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300364 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300365 $arg = "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" : '');
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300366 if ($this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')'))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300367 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300368 $this->_lock = $arg;
369 return TRUE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300370 }
371
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300372 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300373 }
374
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300375 return parent::_get_lock($session_id);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300376 }
377
378 // ------------------------------------------------------------------------
379
Andrey Andreev10411fc2015-01-19 13:54:53 +0200380 /**
381 * Release lock
382 *
383 * Releases a previously acquired lock
384 *
385 * @return bool
386 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300387 protected function _release_lock()
388 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300389 if ( ! $this->_lock)
390 {
391 return TRUE;
392 }
393
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200394 if ($this->_platform === 'mysql')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300395 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300396 if ($this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock)
397 {
398 $this->_lock = FALSE;
399 return TRUE;
400 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300401
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300402 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300403 }
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200404 elseif ($this->_platform === 'postgre')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300405 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300406 if ($this->_db->simple_query('SELECT pg_advisory_unlock('.$this->_lock.')'))
407 {
408 $this->_lock = FALSE;
409 return TRUE;
410 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300411
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300412 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300413 }
414
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300415 return parent::_release_lock();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300416 }
Andrey Andreevaf849692015-12-12 14:07:39 +0200417}