| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2414 |
lars |
9 |
* Copyright (c) 2014 - 2019, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
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:
|
|
|
17 |
*
|
|
|
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
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2414 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
|
|
|
33 |
* @license https://opensource.org/licenses/MIT MIT License
|
| 68 |
lars |
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 3.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* CodeIgniter Session Database Driver
|
|
|
42 |
*
|
|
|
43 |
* @package CodeIgniter
|
|
|
44 |
* @subpackage Libraries
|
|
|
45 |
* @category Sessions
|
|
|
46 |
* @author Andrey Andreev
|
|
|
47 |
* @link https://codeigniter.com/user_guide/libraries/sessions.html
|
|
|
48 |
*/
|
|
|
49 |
class 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 |
/**
|
|
|
59 |
* Row exists flag
|
|
|
60 |
*
|
|
|
61 |
* @var bool
|
|
|
62 |
*/
|
|
|
63 |
protected $_row_exists = FALSE;
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Lock "driver" flag
|
|
|
67 |
*
|
|
|
68 |
* @var string
|
|
|
69 |
*/
|
|
|
70 |
protected $_platform;
|
|
|
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 |
|
|
|
84 |
$CI =& get_instance();
|
|
|
85 |
isset($CI->db) OR $CI->load->database();
|
|
|
86 |
$this->_db = $CI->db;
|
|
|
87 |
|
|
|
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 |
}
|
|
|
96 |
elseif ($this->_db->cache_on)
|
|
|
97 |
{
|
|
|
98 |
throw new Exception('Configured database connection has cache enabled. Aborting.');
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$db_driver = $this->_db->dbdriver.(empty($this->_db->subdriver) ? '' : '_'.$this->_db->subdriver);
|
|
|
102 |
if (strpos($db_driver, 'mysql') !== FALSE)
|
|
|
103 |
{
|
|
|
104 |
$this->_platform = 'mysql';
|
|
|
105 |
}
|
|
|
106 |
elseif (in_array($db_driver, array('postgre', 'pdo_pgsql'), TRUE))
|
|
|
107 |
{
|
|
|
108 |
$this->_platform = 'postgre';
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// Note: BC work-around for the old 'sess_table_name' setting, should be removed in the future.
|
|
|
112 |
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 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
// ------------------------------------------------------------------------
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
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 |
*/
|
|
|
129 |
public function open($save_path, $name)
|
|
|
130 |
{
|
|
|
131 |
if (empty($this->_db->conn_id) && ! $this->_db->db_connect())
|
|
|
132 |
{
|
| 2414 |
lars |
133 |
return $this->_failure;
|
| 68 |
lars |
134 |
}
|
|
|
135 |
|
| 2414 |
lars |
136 |
$this->php5_validate_id();
|
|
|
137 |
|
| 68 |
lars |
138 |
return $this->_success;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
// ------------------------------------------------------------------------
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
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 |
*/
|
|
|
151 |
public function read($session_id)
|
|
|
152 |
{
|
| 2414 |
lars |
153 |
if ($this->_get_lock($session_id) === FALSE)
|
| 68 |
lars |
154 |
{
|
| 2414 |
lars |
155 |
return $this->_failure;
|
|
|
156 |
}
|
| 68 |
lars |
157 |
|
| 2414 |
lars |
158 |
// Prevent previous QB calls from messing with our queries
|
|
|
159 |
$this->_db->reset_query();
|
| 68 |
lars |
160 |
|
| 2414 |
lars |
161 |
// Needed by write() to detect session_regenerate_id() calls
|
|
|
162 |
$this->_session_id = $session_id;
|
| 68 |
lars |
163 |
|
| 2414 |
lars |
164 |
$this->_db
|
|
|
165 |
->select('data')
|
|
|
166 |
->from($this->_config['save_path'])
|
|
|
167 |
->where('id', $session_id);
|
| 68 |
lars |
168 |
|
| 2414 |
lars |
169 |
if ($this->_config['match_ip'])
|
|
|
170 |
{
|
|
|
171 |
$this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
|
|
|
172 |
}
|
| 68 |
lars |
173 |
|
| 2414 |
lars |
174 |
if ( ! ($result = $this->_db->get()) OR ($result = $result->row()) === NULL)
|
|
|
175 |
{
|
|
|
176 |
// PHP7 will reuse the same SessionHandler object after
|
|
|
177 |
// ID regeneration, so we need to explicitly set this to
|
|
|
178 |
// FALSE instead of relying on the default ...
|
|
|
179 |
$this->_row_exists = FALSE;
|
|
|
180 |
$this->_fingerprint = md5('');
|
|
|
181 |
return '';
|
| 68 |
lars |
182 |
}
|
|
|
183 |
|
| 2414 |
lars |
184 |
// PostgreSQL's variant of a BLOB datatype is Bytea, which is a
|
|
|
185 |
// PITA to work with, so we use base64-encoded data in a TEXT
|
|
|
186 |
// field instead.
|
|
|
187 |
$result = ($this->_platform === 'postgre')
|
|
|
188 |
? base64_decode(rtrim($result->data))
|
|
|
189 |
: $result->data;
|
|
|
190 |
|
|
|
191 |
$this->_fingerprint = md5($result);
|
|
|
192 |
$this->_row_exists = TRUE;
|
|
|
193 |
return $result;
|
| 68 |
lars |
194 |
}
|
|
|
195 |
|
|
|
196 |
// ------------------------------------------------------------------------
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Write
|
|
|
200 |
*
|
|
|
201 |
* Writes (create / update) session data
|
|
|
202 |
*
|
|
|
203 |
* @param string $session_id Session ID
|
|
|
204 |
* @param string $session_data Serialized session data
|
|
|
205 |
* @return bool
|
|
|
206 |
*/
|
|
|
207 |
public function write($session_id, $session_data)
|
|
|
208 |
{
|
|
|
209 |
// Prevent previous QB calls from messing with our queries
|
|
|
210 |
$this->_db->reset_query();
|
|
|
211 |
|
|
|
212 |
// Was the ID regenerated?
|
| 2107 |
lars |
213 |
if (isset($this->_session_id) && $session_id !== $this->_session_id)
|
| 68 |
lars |
214 |
{
|
|
|
215 |
if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id))
|
|
|
216 |
{
|
| 2414 |
lars |
217 |
return $this->_failure;
|
| 68 |
lars |
218 |
}
|
|
|
219 |
|
|
|
220 |
$this->_row_exists = FALSE;
|
|
|
221 |
$this->_session_id = $session_id;
|
|
|
222 |
}
|
| 2107 |
lars |
223 |
elseif ($this->_lock === FALSE)
|
|
|
224 |
{
|
| 2414 |
lars |
225 |
return $this->_failure;
|
| 2107 |
lars |
226 |
}
|
| 68 |
lars |
227 |
|
|
|
228 |
if ($this->_row_exists === FALSE)
|
|
|
229 |
{
|
|
|
230 |
$insert_data = array(
|
|
|
231 |
'id' => $session_id,
|
|
|
232 |
'ip_address' => $_SERVER['REMOTE_ADDR'],
|
|
|
233 |
'timestamp' => time(),
|
|
|
234 |
'data' => ($this->_platform === 'postgre' ? base64_encode($session_data) : $session_data)
|
|
|
235 |
);
|
|
|
236 |
|
|
|
237 |
if ($this->_db->insert($this->_config['save_path'], $insert_data))
|
|
|
238 |
{
|
|
|
239 |
$this->_fingerprint = md5($session_data);
|
|
|
240 |
$this->_row_exists = TRUE;
|
|
|
241 |
return $this->_success;
|
|
|
242 |
}
|
|
|
243 |
|
| 2414 |
lars |
244 |
return $this->_failure;
|
| 68 |
lars |
245 |
}
|
|
|
246 |
|
|
|
247 |
$this->_db->where('id', $session_id);
|
|
|
248 |
if ($this->_config['match_ip'])
|
|
|
249 |
{
|
|
|
250 |
$this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
$update_data = array('timestamp' => time());
|
|
|
254 |
if ($this->_fingerprint !== md5($session_data))
|
|
|
255 |
{
|
|
|
256 |
$update_data['data'] = ($this->_platform === 'postgre')
|
|
|
257 |
? base64_encode($session_data)
|
|
|
258 |
: $session_data;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
if ($this->_db->update($this->_config['save_path'], $update_data))
|
|
|
262 |
{
|
|
|
263 |
$this->_fingerprint = md5($session_data);
|
|
|
264 |
return $this->_success;
|
|
|
265 |
}
|
|
|
266 |
|
| 2414 |
lars |
267 |
return $this->_failure;
|
| 68 |
lars |
268 |
}
|
|
|
269 |
|
|
|
270 |
// ------------------------------------------------------------------------
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* Close
|
|
|
274 |
*
|
|
|
275 |
* Releases locks
|
|
|
276 |
*
|
|
|
277 |
* @return bool
|
|
|
278 |
*/
|
|
|
279 |
public function close()
|
|
|
280 |
{
|
|
|
281 |
return ($this->_lock && ! $this->_release_lock())
|
| 2414 |
lars |
282 |
? $this->_failure
|
| 68 |
lars |
283 |
: $this->_success;
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
// ------------------------------------------------------------------------
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Destroy
|
|
|
290 |
*
|
|
|
291 |
* Destroys the current session.
|
|
|
292 |
*
|
|
|
293 |
* @param string $session_id Session ID
|
|
|
294 |
* @return bool
|
|
|
295 |
*/
|
|
|
296 |
public function destroy($session_id)
|
|
|
297 |
{
|
|
|
298 |
if ($this->_lock)
|
|
|
299 |
{
|
|
|
300 |
// Prevent previous QB calls from messing with our queries
|
|
|
301 |
$this->_db->reset_query();
|
|
|
302 |
|
|
|
303 |
$this->_db->where('id', $session_id);
|
|
|
304 |
if ($this->_config['match_ip'])
|
|
|
305 |
{
|
|
|
306 |
$this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
if ( ! $this->_db->delete($this->_config['save_path']))
|
|
|
310 |
{
|
| 2414 |
lars |
311 |
return $this->_failure;
|
| 68 |
lars |
312 |
}
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
if ($this->close() === $this->_success)
|
|
|
316 |
{
|
|
|
317 |
$this->_cookie_destroy();
|
|
|
318 |
return $this->_success;
|
|
|
319 |
}
|
|
|
320 |
|
| 2414 |
lars |
321 |
return $this->_failure;
|
| 68 |
lars |
322 |
}
|
|
|
323 |
|
|
|
324 |
// ------------------------------------------------------------------------
|
|
|
325 |
|
|
|
326 |
/**
|
|
|
327 |
* Garbage Collector
|
|
|
328 |
*
|
|
|
329 |
* Deletes expired sessions
|
|
|
330 |
*
|
|
|
331 |
* @param int $maxlifetime Maximum lifetime of sessions
|
|
|
332 |
* @return bool
|
|
|
333 |
*/
|
|
|
334 |
public function gc($maxlifetime)
|
|
|
335 |
{
|
|
|
336 |
// Prevent previous QB calls from messing with our queries
|
|
|
337 |
$this->_db->reset_query();
|
|
|
338 |
|
|
|
339 |
return ($this->_db->delete($this->_config['save_path'], 'timestamp < '.(time() - $maxlifetime)))
|
|
|
340 |
? $this->_success
|
| 2414 |
lars |
341 |
: $this->_failure;
|
| 68 |
lars |
342 |
}
|
|
|
343 |
|
| 2414 |
lars |
344 |
// --------------------------------------------------------------------
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
* Validate ID
|
|
|
348 |
*
|
|
|
349 |
* Checks whether a session ID record exists server-side,
|
|
|
350 |
* to enforce session.use_strict_mode.
|
|
|
351 |
*
|
|
|
352 |
* @param string $id
|
|
|
353 |
* @return bool
|
|
|
354 |
*/
|
|
|
355 |
public function validateSessionId($id)
|
|
|
356 |
{
|
|
|
357 |
// Prevent previous QB calls from messing with our queries
|
|
|
358 |
$this->_db->reset_query();
|
|
|
359 |
|
|
|
360 |
$this->_db->select('1')->from($this->_config['save_path'])->where('id', $id);
|
|
|
361 |
empty($this->_config['match_ip']) OR $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
|
|
|
362 |
$result = $this->_db->get();
|
|
|
363 |
empty($result) OR $result = $result->row();
|
|
|
364 |
|
|
|
365 |
return ! empty($result);
|
|
|
366 |
}
|
|
|
367 |
|
| 68 |
lars |
368 |
// ------------------------------------------------------------------------
|
|
|
369 |
|
|
|
370 |
/**
|
|
|
371 |
* Get lock
|
|
|
372 |
*
|
|
|
373 |
* Acquires a lock, depending on the underlying platform.
|
|
|
374 |
*
|
|
|
375 |
* @param string $session_id Session ID
|
|
|
376 |
* @return bool
|
|
|
377 |
*/
|
|
|
378 |
protected function _get_lock($session_id)
|
|
|
379 |
{
|
|
|
380 |
if ($this->_platform === 'mysql')
|
|
|
381 |
{
|
| 2049 |
lars |
382 |
$arg = md5($session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : ''));
|
| 68 |
lars |
383 |
if ($this->_db->query("SELECT GET_LOCK('".$arg."', 300) AS ci_session_lock")->row()->ci_session_lock)
|
|
|
384 |
{
|
|
|
385 |
$this->_lock = $arg;
|
|
|
386 |
return TRUE;
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
return FALSE;
|
|
|
390 |
}
|
|
|
391 |
elseif ($this->_platform === 'postgre')
|
|
|
392 |
{
|
|
|
393 |
$arg = "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" : '');
|
|
|
394 |
if ($this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')'))
|
|
|
395 |
{
|
|
|
396 |
$this->_lock = $arg;
|
|
|
397 |
return TRUE;
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
return FALSE;
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
return parent::_get_lock($session_id);
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
// ------------------------------------------------------------------------
|
|
|
407 |
|
|
|
408 |
/**
|
|
|
409 |
* Release lock
|
|
|
410 |
*
|
|
|
411 |
* Releases a previously acquired lock
|
|
|
412 |
*
|
|
|
413 |
* @return bool
|
|
|
414 |
*/
|
|
|
415 |
protected function _release_lock()
|
|
|
416 |
{
|
|
|
417 |
if ( ! $this->_lock)
|
|
|
418 |
{
|
|
|
419 |
return TRUE;
|
|
|
420 |
}
|
|
|
421 |
|
|
|
422 |
if ($this->_platform === 'mysql')
|
|
|
423 |
{
|
|
|
424 |
if ($this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock)
|
|
|
425 |
{
|
|
|
426 |
$this->_lock = FALSE;
|
|
|
427 |
return TRUE;
|
|
|
428 |
}
|
|
|
429 |
|
|
|
430 |
return FALSE;
|
|
|
431 |
}
|
|
|
432 |
elseif ($this->_platform === 'postgre')
|
|
|
433 |
{
|
|
|
434 |
if ($this->_db->simple_query('SELECT pg_advisory_unlock('.$this->_lock.')'))
|
|
|
435 |
{
|
|
|
436 |
$this->_lock = FALSE;
|
|
|
437 |
return TRUE;
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
return FALSE;
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
return parent::_release_lock();
|
|
|
444 |
}
|
| 2049 |
lars |
445 |
}
|