Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | bf6b11d | 2015-01-12 17:27:12 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 6 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 8 | * |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, British Columbia Institute of Technology |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 10 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 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: |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 17 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 35 | * @since Version 3.0.0 |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
| 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
| 39 | |
| 40 | /** |
| 41 | * CodeIgniter Session Database Driver |
| 42 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 43 | * @package CodeIgniter |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 44 | * @subpackage Libraries |
| 45 | * @category Sessions |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 46 | * @author Andrey Andreev |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/sessions.html |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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 | */ |
Andrey Andreev | e9ca012 | 2015-01-15 17:42:17 +0200 | [diff] [blame] | 70 | protected $_platform; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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(); |
Andrey Andreev | 00c222d | 2015-01-29 18:14:31 +0200 | [diff] [blame] | 86 | $this->_db = $CI->db; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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 | } |
Andrey Andreev | 737a566 | 2015-03-21 12:41:38 +0200 | [diff] [blame] | 96 | elseif ($this->_db->cache_on) |
| 97 | { |
| 98 | throw new Exception('Configured database connection has cache enabled. Aborting.'); |
| 99 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 100 | |
| 101 | $db_driver = $this->_db->dbdriver.(empty($this->_db->subdriver) ? '' : '_'.$this->_db->subdriver); |
| 102 | if (strpos($db_driver, 'mysql') !== FALSE) |
| 103 | { |
Andrey Andreev | e9ca012 | 2015-01-15 17:42:17 +0200 | [diff] [blame] | 104 | $this->_platform = 'mysql'; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 105 | } |
| 106 | elseif (in_array($db_driver, array('postgre', 'pdo_pgsql'), TRUE)) |
| 107 | { |
Andrey Andreev | e9ca012 | 2015-01-15 17:42:17 +0200 | [diff] [blame] | 108 | $this->_platform = 'postgre'; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 109 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 110 | |
Andrey Andreev | 7f8eb36 | 2015-01-15 18:01:41 +0200 | [diff] [blame] | 111 | // Note: BC work-around for the old 'sess_table_name' setting, should be removed in the future. |
Andrey Andreev | 85dfc2a | 2016-04-01 22:54:15 +0300 | [diff] [blame^] | 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 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // ------------------------------------------------------------------------ |
| 119 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 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 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 129 | public function open($save_path, $name) |
| 130 | { |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 131 | if (empty($this->_db->conn_id) && ! $this->_db->db_connect()) |
| 132 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 133 | return $this->_fail(); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | return $this->_success; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // ------------------------------------------------------------------------ |
| 140 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 141 | /** |
| 142 | * Read |
| 143 | * |
| 144 | * Reads session data and acquires a lock |
| 145 | * |
| 146 | * @param string $session_id Session ID |
| 147 | * @return string Serialized session data |
| 148 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 149 | public function read($session_id) |
| 150 | { |
Andrey Andreev | d069b9b | 2014-09-16 10:18:16 +0300 | [diff] [blame] | 151 | if ($this->_get_lock($session_id) !== FALSE) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 152 | { |
Andrey Andreev | fd5fe1a64 | 2016-01-11 11:58:40 +0200 | [diff] [blame] | 153 | // Prevent previous QB calls from messing with our queries |
| 154 | $this->_db->reset_query(); |
| 155 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 156 | // Needed by write() to detect session_regenerate_id() calls |
| 157 | $this->_session_id = $session_id; |
| 158 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 159 | $this->_db |
| 160 | ->select('data') |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 161 | ->from($this->_config['save_path']) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 162 | ->where('id', $session_id); |
| 163 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 164 | if ($this->_config['match_ip']) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 165 | { |
| 166 | $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']); |
| 167 | } |
| 168 | |
Andrey Andreev | 7bdd495 | 2016-03-10 14:01:09 +0200 | [diff] [blame] | 169 | if ( ! ($result = $this->_db->get()) OR ($result = $result->row()) === NULL) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 170 | { |
Andrey Andreev | de8b82c | 2015-10-18 20:58:38 +0300 | [diff] [blame] | 171 | // PHP7 will reuse the same SessionHandler object after |
| 172 | // ID regeneration, so we need to explicitly set this to |
| 173 | // FALSE instead of relying on the default ... |
| 174 | $this->_row_exists = FALSE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 175 | $this->_fingerprint = md5(''); |
| 176 | return ''; |
| 177 | } |
| 178 | |
Andrey Andreev | e9ca012 | 2015-01-15 17:42:17 +0200 | [diff] [blame] | 179 | // PostgreSQL's variant of a BLOB datatype is Bytea, which is a |
| 180 | // PITA to work with, so we use base64-encoded data in a TEXT |
| 181 | // field instead. |
Andrey Andreev | d012255 | 2015-01-15 21:25:58 +0200 | [diff] [blame] | 182 | $result = ($this->_platform === 'postgre') |
| 183 | ? base64_decode(rtrim($result->data)) |
| 184 | : $result->data; |
Andrey Andreev | e9ca012 | 2015-01-15 17:42:17 +0200 | [diff] [blame] | 185 | |
Andrey Andreev | d012255 | 2015-01-15 21:25:58 +0200 | [diff] [blame] | 186 | $this->_fingerprint = md5($result); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 187 | $this->_row_exists = TRUE; |
Andrey Andreev | 7400975 | 2015-01-15 21:36:25 +0200 | [diff] [blame] | 188 | return $result; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | $this->_fingerprint = md5(''); |
| 192 | return ''; |
| 193 | } |
| 194 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 195 | // ------------------------------------------------------------------------ |
| 196 | |
| 197 | /** |
| 198 | * Write |
| 199 | * |
| 200 | * Writes (create / update) session data |
| 201 | * |
| 202 | * @param string $session_id Session ID |
| 203 | * @param string $session_data Serialized session data |
| 204 | * @return bool |
| 205 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 206 | public function write($session_id, $session_data) |
| 207 | { |
Andrey Andreev | fd5fe1a64 | 2016-01-11 11:58:40 +0200 | [diff] [blame] | 208 | // Prevent previous QB calls from messing with our queries |
| 209 | $this->_db->reset_query(); |
| 210 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 211 | // Was the ID regenerated? |
Shakespeare2000 | 305186d | 2014-11-02 11:28:47 +0100 | [diff] [blame] | 212 | if ($session_id !== $this->_session_id) |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 213 | { |
| 214 | if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id)) |
| 215 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 216 | return $this->_fail(); |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | $this->_row_exists = FALSE; |
| 220 | $this->_session_id = $session_id; |
| 221 | } |
Shakespeare2000 | 305186d | 2014-11-02 11:28:47 +0100 | [diff] [blame] | 222 | elseif ($this->_lock === FALSE) |
| 223 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 224 | return $this->_fail(); |
Shakespeare2000 | 305186d | 2014-11-02 11:28:47 +0100 | [diff] [blame] | 225 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 226 | |
| 227 | if ($this->_row_exists === FALSE) |
| 228 | { |
Andrey Andreev | 5231d32 | 2015-01-19 02:29:49 +0200 | [diff] [blame] | 229 | $insert_data = array( |
| 230 | 'id' => $session_id, |
| 231 | 'ip_address' => $_SERVER['REMOTE_ADDR'], |
| 232 | 'timestamp' => time(), |
| 233 | 'data' => ($this->_platform === 'postgre' ? base64_encode($session_data) : $session_data) |
| 234 | ); |
| 235 | |
| 236 | if ($this->_db->insert($this->_config['save_path'], $insert_data)) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 237 | { |
| 238 | $this->_fingerprint = md5($session_data); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 239 | $this->_row_exists = TRUE; |
| 240 | return $this->_success; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 241 | } |
| 242 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 243 | return $this->_fail(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | $this->_db->where('id', $session_id); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 247 | if ($this->_config['match_ip']) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 248 | { |
| 249 | $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']); |
| 250 | } |
| 251 | |
Andrey Andreev | c33c3ad | 2015-01-19 10:54:21 +0200 | [diff] [blame] | 252 | $update_data = array('timestamp' => time()); |
Andrey Andreev | 5231d32 | 2015-01-19 02:29:49 +0200 | [diff] [blame] | 253 | if ($this->_fingerprint !== md5($session_data)) |
| 254 | { |
| 255 | $update_data['data'] = ($this->_platform === 'postgre') |
| 256 | ? base64_encode($session_data) |
| 257 | : $session_data; |
| 258 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 259 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 260 | if ($this->_db->update($this->_config['save_path'], $update_data)) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 261 | { |
| 262 | $this->_fingerprint = md5($session_data); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 263 | return $this->_success; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 264 | } |
| 265 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 266 | return $this->_fail(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // ------------------------------------------------------------------------ |
| 270 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 271 | /** |
| 272 | * Close |
| 273 | * |
| 274 | * Releases locks |
| 275 | * |
Gabriel Potkány | 1fb5000 | 2015-02-04 01:45:59 +0100 | [diff] [blame] | 276 | * @return bool |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 277 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 278 | public function close() |
| 279 | { |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 280 | return ($this->_lock && ! $this->_release_lock()) |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 281 | ? $this->_fail() |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 282 | : $this->_success; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // ------------------------------------------------------------------------ |
| 286 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 287 | /** |
| 288 | * Destroy |
| 289 | * |
| 290 | * Destroys the current session. |
| 291 | * |
| 292 | * @param string $session_id Session ID |
| 293 | * @return bool |
| 294 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 295 | public function destroy($session_id) |
| 296 | { |
| 297 | if ($this->_lock) |
| 298 | { |
Andrey Andreev | fd5fe1a64 | 2016-01-11 11:58:40 +0200 | [diff] [blame] | 299 | // Prevent previous QB calls from messing with our queries |
| 300 | $this->_db->reset_query(); |
| 301 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 302 | $this->_db->where('id', $session_id); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 303 | if ($this->_config['match_ip']) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 304 | { |
| 305 | $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']); |
| 306 | } |
| 307 | |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 308 | if ( ! $this->_db->delete($this->_config['save_path'])) |
| 309 | { |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 310 | return $this->_fail(); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 311 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 312 | } |
| 313 | |
Andrey Andreev | bb71dba | 2015-12-15 13:00:52 +0200 | [diff] [blame] | 314 | if ($this->close() === $this->_success) |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 315 | { |
| 316 | $this->_cookie_destroy(); |
| 317 | return $this->_success; |
| 318 | } |
| 319 | |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 320 | return $this->_fail(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // ------------------------------------------------------------------------ |
| 324 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 325 | /** |
| 326 | * Garbage Collector |
| 327 | * |
| 328 | * Deletes expired sessions |
| 329 | * |
| 330 | * @param int $maxlifetime Maximum lifetime of sessions |
| 331 | * @return bool |
| 332 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 333 | public function gc($maxlifetime) |
| 334 | { |
Andrey Andreev | fd5fe1a64 | 2016-01-11 11:58:40 +0200 | [diff] [blame] | 335 | // Prevent previous QB calls from messing with our queries |
| 336 | $this->_db->reset_query(); |
| 337 | |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 338 | return ($this->_db->delete($this->_config['save_path'], 'timestamp < '.(time() - $maxlifetime))) |
| 339 | ? $this->_success |
Andrey Andreev | a027a7f | 2016-03-10 13:59:20 +0200 | [diff] [blame] | 340 | : $this->_fail(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // ------------------------------------------------------------------------ |
| 344 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 345 | /** |
| 346 | * Get lock |
| 347 | * |
| 348 | * Acquires a lock, depending on the underlying platform. |
| 349 | * |
| 350 | * @param string $session_id Session ID |
| 351 | * @return bool |
| 352 | */ |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 353 | protected function _get_lock($session_id) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 354 | { |
Andrey Andreev | e9ca012 | 2015-01-15 17:42:17 +0200 | [diff] [blame] | 355 | if ($this->_platform === 'mysql') |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 356 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 357 | $arg = $session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : ''); |
Andrey Andreev | e1a5bb3 | 2015-03-04 13:33:39 +0200 | [diff] [blame] | 358 | if ($this->_db->query("SELECT GET_LOCK('".$arg."', 300) AS ci_session_lock")->row()->ci_session_lock) |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 359 | { |
| 360 | $this->_lock = $arg; |
| 361 | return TRUE; |
| 362 | } |
| 363 | |
| 364 | return FALSE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 365 | } |
Andrey Andreev | e9ca012 | 2015-01-15 17:42:17 +0200 | [diff] [blame] | 366 | elseif ($this->_platform === 'postgre') |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 367 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 368 | $arg = "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" : ''); |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 369 | if ($this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')')) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 370 | { |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 371 | $this->_lock = $arg; |
| 372 | return TRUE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 373 | } |
| 374 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 375 | return FALSE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 376 | } |
| 377 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 378 | return parent::_get_lock($session_id); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // ------------------------------------------------------------------------ |
| 382 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 383 | /** |
| 384 | * Release lock |
| 385 | * |
| 386 | * Releases a previously acquired lock |
| 387 | * |
| 388 | * @return bool |
| 389 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 390 | protected function _release_lock() |
| 391 | { |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 392 | if ( ! $this->_lock) |
| 393 | { |
| 394 | return TRUE; |
| 395 | } |
| 396 | |
Andrey Andreev | e9ca012 | 2015-01-15 17:42:17 +0200 | [diff] [blame] | 397 | if ($this->_platform === 'mysql') |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 398 | { |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 399 | if ($this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock) |
| 400 | { |
| 401 | $this->_lock = FALSE; |
| 402 | return TRUE; |
| 403 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 404 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 405 | return FALSE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 406 | } |
Andrey Andreev | e9ca012 | 2015-01-15 17:42:17 +0200 | [diff] [blame] | 407 | elseif ($this->_platform === 'postgre') |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 408 | { |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 409 | if ($this->_db->simple_query('SELECT pg_advisory_unlock('.$this->_lock.')')) |
| 410 | { |
| 411 | $this->_lock = FALSE; |
| 412 | return TRUE; |
| 413 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 414 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 415 | return FALSE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 416 | } |
| 417 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 418 | return parent::_release_lock(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 419 | } |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 420 | } |