blob: 42ff96b7cdb5d8662bea9a3b181570c9c764b609 [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();
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
97 $db_driver = $this->_db->dbdriver.(empty($this->_db->subdriver) ? '' : '_'.$this->_db->subdriver);
98 if (strpos($db_driver, 'mysql') !== FALSE)
99 {
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200100 $this->_platform = 'mysql';
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300101 }
102 elseif (in_array($db_driver, array('postgre', 'pdo_pgsql'), TRUE))
103 {
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200104 $this->_platform = 'postgre';
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300105 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300106
Andrey Andreev7f8eb362015-01-15 18:01:41 +0200107 // Note: BC work-around for the old 'sess_table_name' setting, should be removed in the future.
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300108 isset($this->_config['save_path']) OR $this->_config['save_path'] = config_item('sess_table_name');
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300109 }
110
111 // ------------------------------------------------------------------------
112
113 public function open($save_path, $name)
114 {
115 return empty($this->_db->conn_id)
116 ? ( ! $this->_db->autoinit && $this->_db->db_connect())
117 : TRUE;
118 }
119
120 // ------------------------------------------------------------------------
121
122 public function read($session_id)
123 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300124 if ($this->_get_lock($session_id) !== FALSE)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300125 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200126 // Needed by write() to detect session_regenerate_id() calls
127 $this->_session_id = $session_id;
128
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300129 $this->_db
130 ->select('data')
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300131 ->from($this->_config['save_path'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300132 ->where('id', $session_id);
133
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300134 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300135 {
136 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
137 }
138
139 if (($result = $this->_db->get()->row()) === NULL)
140 {
141 $this->_fingerprint = md5('');
142 return '';
143 }
144
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200145 // PostgreSQL's variant of a BLOB datatype is Bytea, which is a
146 // PITA to work with, so we use base64-encoded data in a TEXT
147 // field instead.
148 if ($this->_platform === 'postgre')
149 {
150 $result = base64_decode(rtrim($result->data));
151 }
152
153 $this->_fingerprint = md5(rtrim($result));
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300154 $this->_row_exists = TRUE;
155 return $result->data;
156 }
157
158 $this->_fingerprint = md5('');
159 return '';
160 }
161
162 public function write($session_id, $session_data)
163 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200164 // Was the ID regenerated?
Shakespeare2000305186d2014-11-02 11:28:47 +0100165 if ($session_id !== $this->_session_id)
Andrey Andreev7474a672014-10-31 23:35:32 +0200166 {
167 if ( ! $this->_release_lock() OR ! $this->_get_lock($session_id))
168 {
169 return FALSE;
170 }
171
172 $this->_row_exists = FALSE;
173 $this->_session_id = $session_id;
174 }
Shakespeare2000305186d2014-11-02 11:28:47 +0100175 elseif ($this->_lock === FALSE)
176 {
177 return FALSE;
178 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300179
180 if ($this->_row_exists === FALSE)
181 {
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200182 if ($this->_db->insert($this->_config['save_path'], array('id' => $session_id, 'ip_address' => $_SERVER['REMOTE_ADDR'], 'timestamp' => time(), 'data' => base64_encode($session_data))))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300183 {
184 $this->_fingerprint = md5($session_data);
185 return $this->_row_exists = TRUE;
186 }
187
188 return FALSE;
189 }
190
191 $this->_db->where('id', $session_id);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300192 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300193 {
194 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
195 }
196
197 $update_data = ($this->_fingerprint === md5($session_data))
198 ? array('timestamp' => time())
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200199 : array('timestamp' => time(), 'data' => base64_encode($session_data));
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300200
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300201 if ($this->_db->update($this->_config['save_path'], $update_data))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300202 {
203 $this->_fingerprint = md5($session_data);
204 return TRUE;
205 }
206
207 return FALSE;
208 }
209
210 // ------------------------------------------------------------------------
211
212 public function close()
213 {
214 return ($this->_lock)
215 ? $this->_release_lock()
216 : TRUE;
217 }
218
219 // ------------------------------------------------------------------------
220
221 public function destroy($session_id)
222 {
223 if ($this->_lock)
224 {
225 $this->_db->where('id', $session_id);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300226 if ($this->_config['match_ip'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300227 {
228 $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
229 }
230
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300231 return $this->_db->delete($this->_config['save_path'])
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300232 ? ($this->close() && $this->_cookie_destroy())
233 : FALSE;
234 }
235
236 return ($this->close() && $this->_cookie_destroy());
237 }
238
239 // ------------------------------------------------------------------------
240
241 public function gc($maxlifetime)
242 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300243 return $this->_db->delete($this->_config['save_path'], 'timestamp < '.(time() - $maxlifetime));
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300244 }
245
246 // ------------------------------------------------------------------------
247
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300248 protected function _get_lock($session_id)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300249 {
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200250 if ($this->_platform === 'mysql')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300251 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300252 $arg = $session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : '');
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300253 if ($this->_db->query("SELECT GET_LOCK('".$arg."', 10) AS ci_session_lock")->row()->ci_session_lock)
254 {
255 $this->_lock = $arg;
256 return TRUE;
257 }
258
259 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300260 }
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200261 elseif ($this->_platform === 'postgre')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300262 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300263 $arg = "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" : '');
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300264 if ($this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')'))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300265 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300266 $this->_lock = $arg;
267 return TRUE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300268 }
269
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300270 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300271 }
272
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300273 return parent::_get_lock($session_id);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300274 }
275
276 // ------------------------------------------------------------------------
277
278 protected function _release_lock()
279 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300280 if ( ! $this->_lock)
281 {
282 return TRUE;
283 }
284
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200285 if ($this->_platform === 'mysql')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300286 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300287 if ($this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock)
288 {
289 $this->_lock = FALSE;
290 return TRUE;
291 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300292
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300293 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300294 }
Andrey Andreeve9ca0122015-01-15 17:42:17 +0200295 elseif ($this->_platform === 'postgre')
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300296 {
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300297 if ($this->_db->simple_query('SELECT pg_advisory_unlock('.$this->_lock.')'))
298 {
299 $this->_lock = FALSE;
300 return TRUE;
301 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300302
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300303 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300304 }
305
Andrey Andreev93d9fa72014-08-27 22:14:36 +0300306 return parent::_release_lock();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300307 }
308
309}
310
311/* End of file Session_database_driver.php */
312/* Location: ./system/libraries/Session/drivers/Session_database_driver.php */