blob: 72b39d12d475badb588e3dfaafdc65001c103b69 [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 Andreevbf6b11d2015-01-12 17:27:12 +02009 * Copyright (c) 2014 - 2015, 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 Andreev47a47fb2014-05-31 16:08:30 +030031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbf6b11d2015-01-12 17:27:12 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreev46f2f262014-11-11 14:37:51 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
47 * @link http://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 {
128 return empty($this->_db->conn_id)
Andrey Andreev6c7c8912015-02-19 14:44:18 +0200129 ? (bool) $this->_db->db_connect()
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300130 : TRUE;
131 }
132
133 // ------------------------------------------------------------------------
134
Andrey Andreev10411fc2015-01-19 13:54:53 +0200135 /**
136 * Read
137 *
138 * Reads session data and acquires a lock
139 *
140 * @param string $session_id Session ID
141 * @return string Serialized session data
142 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300143 public function read($session_id)
144 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300145 if ($this->_get_lock($session_id) !== FALSE)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300146 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200147 // Needed by write() to detect session_regenerate_id() calls
148 $this->_session_id = $session_id;
149
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300150 $this->_db
151 ->select('data')
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300152 ->from($this->_config['save_path'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300153 ->where('id', $session_id);
154
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300155 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300156 {
157 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
158 }
159
160 if (($result = $this->_db->get()->row()) === NULL)
161 {
Andrey Andreevde8b82c2015-10-18 20:58:38 +0300162 // PHP7 will reuse the same SessionHandler object after
163 // ID regeneration, so we need to explicitly set this to
164 // FALSE instead of relying on the default ...
165 $this->_row_exists = FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300166 $this->_fingerprint = md5('');
167 return '';
168 }
169
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200170 // PostgreSQL's variant of a BLOB datatype is Bytea, which is a
171 // PITA to work with, so we use base64-encoded data in a TEXT
172 // field instead.
Andrey Andreevd0122552015-01-15 21:25:58 +0200173 $result = ($this->_platform === 'postgre')
174 ? base64_decode(rtrim($result->data))
175 : $result->data;
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200176
Andrey Andreevd0122552015-01-15 21:25:58 +0200177 $this->_fingerprint = md5($result);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300178 $this->_row_exists = TRUE;
Andrey Andreev74009752015-01-15 21:36:25 +0200179 return $result;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300180 }
181
182 $this->_fingerprint = md5('');
183 return '';
184 }
185
Andrey Andreev10411fc2015-01-19 13:54:53 +0200186 // ------------------------------------------------------------------------
187
188 /**
189 * Write
190 *
191 * Writes (create / update) session data
192 *
193 * @param string $session_id Session ID
194 * @param string $session_data Serialized session data
195 * @return bool
196 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300197 public function write($session_id, $session_data)
198 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200199 // Was the ID regenerated?
Shakespeare2000305186d2014-11-02 11:28:47 +0100200 if ($session_id !== $this->_session_id)
Andrey Andreev7474a672014-10-31 23:35:32 +0200201 {
202 if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id))
203 {
204 return FALSE;
205 }
206
207 $this->_row_exists = FALSE;
208 $this->_session_id = $session_id;
209 }
Shakespeare2000305186d2014-11-02 11:28:47 +0100210 elseif ($this->_lock === FALSE)
211 {
212 return FALSE;
213 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300214
215 if ($this->_row_exists === FALSE)
216 {
Andrey Andreev5231d322015-01-19 02:29:49 +0200217 $insert_data = array(
218 'id' => $session_id,
219 'ip_address' => $_SERVER['REMOTE_ADDR'],
220 'timestamp' => time(),
221 'data' => ($this->_platform === 'postgre' ? base64_encode($session_data) : $session_data)
222 );
223
224 if ($this->_db->insert($this->_config['save_path'], $insert_data))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300225 {
226 $this->_fingerprint = md5($session_data);
227 return $this->_row_exists = TRUE;
228 }
229
230 return FALSE;
231 }
232
233 $this->_db->where('id', $session_id);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300234 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300235 {
236 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
237 }
238
Andrey Andreevc33c3ad2015-01-19 10:54:21 +0200239 $update_data = array('timestamp' => time());
Andrey Andreev5231d322015-01-19 02:29:49 +0200240 if ($this->_fingerprint !== md5($session_data))
241 {
242 $update_data['data'] = ($this->_platform === 'postgre')
243 ? base64_encode($session_data)
244 : $session_data;
245 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300246
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300247 if ($this->_db->update($this->_config['save_path'], $update_data))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300248 {
249 $this->_fingerprint = md5($session_data);
250 return TRUE;
251 }
252
253 return FALSE;
254 }
255
256 // ------------------------------------------------------------------------
257
Andrey Andreev10411fc2015-01-19 13:54:53 +0200258 /**
259 * Close
260 *
261 * Releases locks
262 *
Gabriel Potkány1fb50002015-02-04 01:45:59 +0100263 * @return bool
Andrey Andreev10411fc2015-01-19 13:54:53 +0200264 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300265 public function close()
266 {
267 return ($this->_lock)
268 ? $this->_release_lock()
269 : TRUE;
270 }
271
272 // ------------------------------------------------------------------------
273
Andrey Andreev10411fc2015-01-19 13:54:53 +0200274 /**
275 * Destroy
276 *
277 * Destroys the current session.
278 *
279 * @param string $session_id Session ID
280 * @return bool
281 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300282 public function destroy($session_id)
283 {
284 if ($this->_lock)
285 {
286 $this->_db->where('id', $session_id);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300287 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300288 {
289 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
290 }
291
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300292 return $this->_db->delete($this->_config['save_path'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300293 ? ($this->close() && $this->_cookie_destroy())
294 : FALSE;
295 }
296
297 return ($this->close() && $this->_cookie_destroy());
298 }
299
300 // ------------------------------------------------------------------------
301
Andrey Andreev10411fc2015-01-19 13:54:53 +0200302 /**
303 * Garbage Collector
304 *
305 * Deletes expired sessions
306 *
307 * @param int $maxlifetime Maximum lifetime of sessions
308 * @return bool
309 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300310 public function gc($maxlifetime)
311 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300312 return $this->_db->delete($this->_config['save_path'], 'timestamp < '.(time() - $maxlifetime));
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300313 }
314
315 // ------------------------------------------------------------------------
316
Andrey Andreev10411fc2015-01-19 13:54:53 +0200317 /**
318 * Get lock
319 *
320 * Acquires a lock, depending on the underlying platform.
321 *
322 * @param string $session_id Session ID
323 * @return bool
324 */
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300325 protected function _get_lock($session_id)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300326 {
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200327 if ($this->_platform === 'mysql')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300328 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300329 $arg = $session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : '');
Andrey Andreeve1a5bb32015-03-04 13:33:39 +0200330 if ($this->_db->query("SELECT GET_LOCK('".$arg."', 300) AS ci_session_lock")->row()->ci_session_lock)
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300331 {
332 $this->_lock = $arg;
333 return TRUE;
334 }
335
336 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300337 }
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200338 elseif ($this->_platform === 'postgre')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300339 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300340 $arg = "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" : '');
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300341 if ($this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')'))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300342 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300343 $this->_lock = $arg;
344 return TRUE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300345 }
346
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300347 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300348 }
349
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300350 return parent::_get_lock($session_id);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300351 }
352
353 // ------------------------------------------------------------------------
354
Andrey Andreev10411fc2015-01-19 13:54:53 +0200355 /**
356 * Release lock
357 *
358 * Releases a previously acquired lock
359 *
360 * @return bool
361 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300362 protected function _release_lock()
363 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300364 if ( ! $this->_lock)
365 {
366 return TRUE;
367 }
368
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200369 if ($this->_platform === 'mysql')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300370 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300371 if ($this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock)
372 {
373 $this->_lock = FALSE;
374 return TRUE;
375 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300376
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300377 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300378 }
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200379 elseif ($this->_platform === 'postgre')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300380 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300381 if ($this->_db->simple_query('SELECT pg_advisory_unlock('.$this->_lock.')'))
382 {
383 $this->_lock = FALSE;
384 return TRUE;
385 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300386
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300387 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300388 }
389
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300390 return parent::_release_lock();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300391 }
392
393}