Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.2.4 or newer |
| 6 | * |
| 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
| 19 | * @package CodeIgniter |
| 20 | * @author Andrey Andreev |
| 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
| 24 | * @since Version 3.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
| 28 | |
| 29 | /** |
| 30 | * CodeIgniter Session Database Driver |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Sessions |
| 35 | * @author Andrey Andreev |
| 36 | * @link http://codeigniter.com/user_guide/libraries/sessions.html |
| 37 | */ |
| 38 | class CI_Session_database_driver extends CI_Session_driver implements SessionHandlerInterface { |
| 39 | |
| 40 | /** |
| 41 | * DB object |
| 42 | * |
| 43 | * @var object |
| 44 | */ |
| 45 | protected $_db; |
| 46 | |
| 47 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 48 | * Row exists flag |
| 49 | * |
| 50 | * @var bool |
| 51 | */ |
| 52 | protected $_row_exists = FALSE; |
| 53 | |
| 54 | /** |
| 55 | * Lock "driver" flag |
| 56 | * |
| 57 | * @var string |
| 58 | */ |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 59 | protected $_lock_driver = 'semaphore'; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 60 | |
| 61 | // ------------------------------------------------------------------------ |
| 62 | |
| 63 | /** |
| 64 | * Class constructor |
| 65 | * |
| 66 | * @param array $params Configuration parameters |
| 67 | * @return void |
| 68 | */ |
| 69 | public function __construct(&$params) |
| 70 | { |
| 71 | parent::__construct($params); |
| 72 | |
| 73 | $CI =& get_instance(); |
| 74 | isset($CI->db) OR $CI->load->database(); |
| 75 | $this->_db =& $CI->db; |
| 76 | |
| 77 | if ( ! $this->_db instanceof CI_DB_query_builder) |
| 78 | { |
| 79 | throw new Exception('Query Builder not enabled for the configured database. Aborting.'); |
| 80 | } |
| 81 | elseif ($this->_db->pconnect) |
| 82 | { |
| 83 | throw new Exception('Configured database connection is persistent. Aborting.'); |
| 84 | } |
| 85 | |
| 86 | $db_driver = $this->_db->dbdriver.(empty($this->_db->subdriver) ? '' : '_'.$this->_db->subdriver); |
| 87 | if (strpos($db_driver, 'mysql') !== FALSE) |
| 88 | { |
Andrey Andreev | e1b9665 | 2014-06-02 10:09:56 +0300 | [diff] [blame] | 89 | $this->_lock_driver = 'mysql'; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 90 | } |
| 91 | elseif (in_array($db_driver, array('postgre', 'pdo_pgsql'), TRUE)) |
| 92 | { |
Andrey Andreev | e1b9665 | 2014-06-02 10:09:56 +0300 | [diff] [blame] | 93 | $this->_lock_driver = 'postgre'; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 94 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 95 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 96 | isset($this->_config['save_path']) OR $this->_config['save_path'] = config_item('sess_table_name'); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // ------------------------------------------------------------------------ |
| 100 | |
| 101 | public function open($save_path, $name) |
| 102 | { |
| 103 | return empty($this->_db->conn_id) |
| 104 | ? ( ! $this->_db->autoinit && $this->_db->db_connect()) |
| 105 | : TRUE; |
| 106 | } |
| 107 | |
| 108 | // ------------------------------------------------------------------------ |
| 109 | |
| 110 | public function read($session_id) |
| 111 | { |
Andrey Andreev | d069b9b | 2014-09-16 10:18:16 +0300 | [diff] [blame] | 112 | if ($this->_get_lock($session_id) !== FALSE) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 113 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 114 | // Needed by write() to detect session_regenerate_id() calls |
| 115 | $this->_session_id = $session_id; |
| 116 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 117 | $this->_db |
| 118 | ->select('data') |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 119 | ->from($this->_config['save_path']) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 120 | ->where('id', $session_id); |
| 121 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 122 | if ($this->_config['match_ip']) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 123 | { |
| 124 | $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']); |
| 125 | } |
| 126 | |
| 127 | if (($result = $this->_db->get()->row()) === NULL) |
| 128 | { |
| 129 | $this->_fingerprint = md5(''); |
| 130 | return ''; |
| 131 | } |
| 132 | |
| 133 | $this->_fingerprint = md5(rtrim($result->data)); |
| 134 | $this->_row_exists = TRUE; |
| 135 | return $result->data; |
| 136 | } |
| 137 | |
| 138 | $this->_fingerprint = md5(''); |
| 139 | return ''; |
| 140 | } |
| 141 | |
| 142 | public function write($session_id, $session_data) |
| 143 | { |
| 144 | if ($this->_lock === FALSE) |
| 145 | { |
| 146 | return FALSE; |
| 147 | } |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 148 | // Was the ID regenerated? |
| 149 | elseif ($session_id !== $this->_session_id) |
| 150 | { |
| 151 | if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id)) |
| 152 | { |
| 153 | return FALSE; |
| 154 | } |
| 155 | |
| 156 | $this->_row_exists = FALSE; |
| 157 | $this->_session_id = $session_id; |
| 158 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 159 | |
| 160 | if ($this->_row_exists === FALSE) |
| 161 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 162 | if ($this->_db->insert($this->_config['save_path'], array('id' => $session_id, 'ip_address' => $_SERVER['REMOTE_ADDR'], 'timestamp' => time(), 'data' => $session_data))) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 163 | { |
| 164 | $this->_fingerprint = md5($session_data); |
| 165 | return $this->_row_exists = TRUE; |
| 166 | } |
| 167 | |
| 168 | return FALSE; |
| 169 | } |
| 170 | |
| 171 | $this->_db->where('id', $session_id); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 172 | if ($this->_config['match_ip']) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 173 | { |
| 174 | $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']); |
| 175 | } |
| 176 | |
| 177 | $update_data = ($this->_fingerprint === md5($session_data)) |
| 178 | ? array('timestamp' => time()) |
| 179 | : array('timestamp' => time(), 'data' => $session_data); |
| 180 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 181 | if ($this->_db->update($this->_config['save_path'], $update_data)) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 182 | { |
| 183 | $this->_fingerprint = md5($session_data); |
| 184 | return TRUE; |
| 185 | } |
| 186 | |
| 187 | return FALSE; |
| 188 | } |
| 189 | |
| 190 | // ------------------------------------------------------------------------ |
| 191 | |
| 192 | public function close() |
| 193 | { |
| 194 | return ($this->_lock) |
| 195 | ? $this->_release_lock() |
| 196 | : TRUE; |
| 197 | } |
| 198 | |
| 199 | // ------------------------------------------------------------------------ |
| 200 | |
| 201 | public function destroy($session_id) |
| 202 | { |
| 203 | if ($this->_lock) |
| 204 | { |
| 205 | $this->_db->where('id', $session_id); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 206 | if ($this->_config['match_ip']) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 207 | { |
| 208 | $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']); |
| 209 | } |
| 210 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 211 | return $this->_db->delete($this->_config['save_path']) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 212 | ? ($this->close() && $this->_cookie_destroy()) |
| 213 | : FALSE; |
| 214 | } |
| 215 | |
| 216 | return ($this->close() && $this->_cookie_destroy()); |
| 217 | } |
| 218 | |
| 219 | // ------------------------------------------------------------------------ |
| 220 | |
| 221 | public function gc($maxlifetime) |
| 222 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 223 | return $this->_db->delete($this->_config['save_path'], 'timestamp < '.(time() - $maxlifetime)); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // ------------------------------------------------------------------------ |
| 227 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 228 | protected function _get_lock($session_id) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 229 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 230 | if ($this->_lock_driver === 'mysql') |
| 231 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 232 | $arg = $session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : ''); |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 233 | if ($this->_db->query("SELECT GET_LOCK('".$arg."', 10) AS ci_session_lock")->row()->ci_session_lock) |
| 234 | { |
| 235 | $this->_lock = $arg; |
| 236 | return TRUE; |
| 237 | } |
| 238 | |
| 239 | return FALSE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 240 | } |
| 241 | elseif ($this->_lock_driver === 'postgre') |
| 242 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 243 | $arg = "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" : ''); |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 244 | if ($this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')')) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 245 | { |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 246 | $this->_lock = $arg; |
| 247 | return TRUE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 248 | } |
| 249 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 250 | return FALSE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 251 | } |
| 252 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 253 | return parent::_get_lock($session_id); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // ------------------------------------------------------------------------ |
| 257 | |
| 258 | protected function _release_lock() |
| 259 | { |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 260 | if ( ! $this->_lock) |
| 261 | { |
| 262 | return TRUE; |
| 263 | } |
| 264 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 265 | if ($this->_lock_driver === 'mysql') |
| 266 | { |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 267 | if ($this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock) |
| 268 | { |
| 269 | $this->_lock = FALSE; |
| 270 | return TRUE; |
| 271 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 272 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 273 | return FALSE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 274 | } |
| 275 | elseif ($this->_lock_driver === 'postgre') |
| 276 | { |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 277 | if ($this->_db->simple_query('SELECT pg_advisory_unlock('.$this->_lock.')')) |
| 278 | { |
| 279 | $this->_lock = FALSE; |
| 280 | return TRUE; |
| 281 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 282 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 283 | return FALSE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 284 | } |
| 285 | |
Andrey Andreev | 93d9fa7 | 2014-08-27 22:14:36 +0300 | [diff] [blame] | 286 | return parent::_release_lock(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | } |
| 290 | |
| 291 | /* End of file Session_database_driver.php */ |
| 292 | /* Location: ./system/libraries/Session/drivers/Session_database_driver.php */ |