Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014, British Columbia Institute of Technology |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +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: |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/) |
| 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 1.0.0 |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 39 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 40 | /** |
| 41 | * Cookie-based session management driver |
| 42 | * |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 43 | * This is the classic CI_Session functionality, as written by EllisLab, abstracted out to a driver. |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 44 | * |
| 45 | * @package CodeIgniter |
| 46 | * @subpackage Libraries |
| 47 | * @category Sessions |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | * @link http://codeigniter.com/user_guide/libraries/sessions.html |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 50 | */ |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 51 | class CI_Session_cookie extends CI_Session_driver { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 52 | |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 53 | /** |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 54 | * Whether to encrypt the session cookie |
| 55 | * |
| 56 | * @var bool |
| 57 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 58 | public $sess_encrypt_cookie = FALSE; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 59 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 60 | /** |
| 61 | * Whether to use to the database for session storage |
| 62 | * |
| 63 | * @var bool |
| 64 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 65 | public $sess_use_database = FALSE; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 66 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 67 | /** |
| 68 | * Name of the database table in which to store sessions |
| 69 | * |
| 70 | * @var string |
| 71 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 72 | public $sess_table_name = ''; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 73 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 74 | /** |
| 75 | * Length of time (in seconds) for sessions to expire |
| 76 | * |
| 77 | * @var int |
| 78 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 79 | public $sess_expiration = 7200; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 80 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 81 | /** |
| 82 | * Whether to kill session on close of browser window |
| 83 | * |
| 84 | * @var bool |
| 85 | */ |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 86 | public $sess_expire_on_close = FALSE; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 87 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 88 | /** |
| 89 | * Whether to match session on ip address |
| 90 | * |
| 91 | * @var bool |
| 92 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 93 | public $sess_match_ip = FALSE; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 94 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 95 | /** |
| 96 | * Whether to match session on user-agent |
| 97 | * |
| 98 | * @var bool |
| 99 | */ |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 100 | public $sess_match_useragent = TRUE; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 101 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 102 | /** |
| 103 | * Name of session cookie |
| 104 | * |
| 105 | * @var string |
| 106 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 107 | public $sess_cookie_name = 'ci_session'; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 108 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 109 | /** |
| 110 | * Session cookie prefix |
| 111 | * |
| 112 | * @var string |
| 113 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 114 | public $cookie_prefix = ''; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 115 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 116 | /** |
| 117 | * Session cookie path |
| 118 | * |
| 119 | * @var string |
| 120 | */ |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 121 | public $cookie_path = ''; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 122 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 123 | /** |
| 124 | * Session cookie domain |
| 125 | * |
| 126 | * @var string |
| 127 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 128 | public $cookie_domain = ''; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 129 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 130 | /** |
| 131 | * Whether to set the cookie only on HTTPS connections |
| 132 | * |
| 133 | * @var bool |
| 134 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 135 | public $cookie_secure = FALSE; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 136 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 137 | /** |
| 138 | * Whether cookie should be allowed only to be sent by the server |
| 139 | * |
| 140 | * @var bool |
| 141 | */ |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 142 | public $cookie_httponly = FALSE; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 143 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 144 | /** |
| 145 | * Interval at which to update session |
| 146 | * |
| 147 | * @var int |
| 148 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 149 | public $sess_time_to_update = 300; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 150 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 151 | /** |
| 152 | * Key with which to encrypt the session cookie |
| 153 | * |
| 154 | * @var string |
| 155 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 156 | public $encryption_key = ''; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 157 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 158 | /** |
Andrey Andreev | d163e0b | 2012-06-14 03:09:53 +0300 | [diff] [blame] | 159 | * Timezone to use for the current time |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 160 | * |
| 161 | * @var string |
| 162 | */ |
Andrey Andreev | d163e0b | 2012-06-14 03:09:53 +0300 | [diff] [blame] | 163 | public $time_reference = 'local'; |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 164 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 165 | /** |
| 166 | * Session data |
| 167 | * |
| 168 | * @var array |
| 169 | */ |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 170 | public $userdata = array(); |
Michiel Vugteveen | 4c316b6 | 2012-05-04 11:32:48 +0200 | [diff] [blame] | 171 | |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 172 | /** |
Timothy Warren | 68f0981 | 2012-04-27 10:38:32 -0400 | [diff] [blame] | 173 | * Current time |
| 174 | * |
| 175 | * @var int |
| 176 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 177 | public $now; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 178 | |
Andrey Andreev | bfb635b | 2014-01-08 18:32:05 +0200 | [diff] [blame] | 179 | // ------------------------------------------------------------------------ |
| 180 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 181 | /** |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 182 | * Default userdata keys |
| 183 | * |
| 184 | * @var array |
| 185 | */ |
| 186 | protected $defaults = array( |
dchill42 | 88b636b | 2012-08-29 08:47:05 -0400 | [diff] [blame] | 187 | 'session_id' => NULL, |
| 188 | 'ip_address' => NULL, |
| 189 | 'user_agent' => NULL, |
| 190 | 'last_activity' => NULL |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 191 | ); |
| 192 | |
| 193 | /** |
| 194 | * Data needs DB update flag |
| 195 | * |
| 196 | * @var bool |
| 197 | */ |
| 198 | protected $data_dirty = FALSE; |
| 199 | |
| 200 | /** |
Andrey Andreev | bfb635b | 2014-01-08 18:32:05 +0200 | [diff] [blame] | 201 | * Standardize newlines flag |
| 202 | * |
| 203 | * @var bool |
| 204 | */ |
| 205 | protected $_standardize_newlines; |
| 206 | |
| 207 | // ------------------------------------------------------------------------ |
| 208 | |
| 209 | /** |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 210 | * Initialize session driver object |
| 211 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 212 | * @return void |
| 213 | */ |
| 214 | protected function initialize() |
| 215 | { |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 216 | // Set all the session preferences, which can either be set |
dchill42 | 42b77a9 | 2012-07-23 11:28:42 -0400 | [diff] [blame] | 217 | // manually via the $params array or via the config file |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 218 | $prefs = array( |
| 219 | 'sess_encrypt_cookie', |
| 220 | 'sess_use_database', |
| 221 | 'sess_table_name', |
| 222 | 'sess_expiration', |
| 223 | 'sess_expire_on_close', |
| 224 | 'sess_match_ip', |
| 225 | 'sess_match_useragent', |
| 226 | 'sess_cookie_name', |
| 227 | 'cookie_path', |
| 228 | 'cookie_domain', |
| 229 | 'cookie_secure', |
| 230 | 'cookie_httponly', |
| 231 | 'sess_time_to_update', |
| 232 | 'time_reference', |
| 233 | 'cookie_prefix', |
Andrey Andreev | bfb635b | 2014-01-08 18:32:05 +0200 | [diff] [blame] | 234 | 'encryption_key', |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 235 | ); |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 236 | |
Andrey Andreev | 4ea76cc | 2014-01-08 21:49:23 +0200 | [diff] [blame] | 237 | $this->_standardize_newlines = (bool) config_item('standardize_newlines'); |
Andrey Andreev | bfb635b | 2014-01-08 18:32:05 +0200 | [diff] [blame] | 238 | |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 239 | foreach ($prefs as $key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 240 | { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 241 | $this->$key = isset($this->_parent->params[$key]) |
| 242 | ? $this->_parent->params[$key] |
| 243 | : $this->CI->config->item($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Andrey Andreev | 4abd094 | 2012-11-26 12:13:59 +0200 | [diff] [blame] | 246 | if (empty($this->encryption_key)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 247 | { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 248 | show_error('In order to use the Cookie Session driver you are required to set an encryption key in your config file.'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 251 | // Do we need encryption? If so, load the encryption class |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 252 | if ($this->sess_encrypt_cookie === TRUE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 253 | { |
Andrey Andreev | 4a2918a | 2014-02-05 01:03:46 +0200 | [diff] [blame] | 254 | $this->CI->load->library('encryption'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 255 | } |
| 256 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 257 | // Check for database |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 258 | if ($this->sess_use_database === TRUE && $this->sess_table_name !== '') |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 259 | { |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 260 | // Load database driver |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 261 | $this->CI->load->database(); |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 262 | |
| 263 | // Register shutdown function |
| 264 | register_shutdown_function(array($this, '_update_db')); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Set the "now" time. Can either be GMT or server time, based on the config prefs. |
| 268 | // We use this to set the "last activity" time |
| 269 | $this->now = $this->_get_time(); |
| 270 | |
| 271 | // Set the session length. If the session expiration is |
| 272 | // set to zero we'll set the expiration two years from now. |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 273 | if ($this->sess_expiration === 0) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 274 | { |
| 275 | $this->sess_expiration = (60*60*24*365*2); |
| 276 | } |
| 277 | |
| 278 | // Set the cookie name |
| 279 | $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name; |
| 280 | |
| 281 | // Run the Session routine. If a session doesn't exist we'll |
| 282 | // create a new one. If it does, we'll update it. |
| 283 | if ( ! $this->_sess_read()) |
| 284 | { |
| 285 | $this->_sess_create(); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | $this->_sess_update(); |
| 290 | } |
| 291 | |
| 292 | // Delete expired sessions if necessary |
| 293 | $this->_sess_gc(); |
| 294 | } |
| 295 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 296 | // ------------------------------------------------------------------------ |
| 297 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 298 | /** |
| 299 | * Write the session data |
| 300 | * |
| 301 | * @return void |
| 302 | */ |
| 303 | public function sess_save() |
| 304 | { |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 305 | // Check for database |
dchill42 | 88b636b | 2012-08-29 08:47:05 -0400 | [diff] [blame] | 306 | if ($this->sess_use_database === TRUE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 307 | { |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 308 | // Mark custom data as dirty so we know to update the DB |
| 309 | $this->data_dirty = TRUE; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 310 | } |
| 311 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 312 | // Write the cookie |
| 313 | $this->_set_cookie(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 316 | // ------------------------------------------------------------------------ |
| 317 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 318 | /** |
| 319 | * Destroy the current session |
| 320 | * |
| 321 | * @return void |
| 322 | */ |
| 323 | public function sess_destroy() |
| 324 | { |
| 325 | // Kill the session DB row |
dchill42 | aee9265 | 2012-08-26 21:45:35 -0400 | [diff] [blame] | 326 | if ($this->sess_use_database === TRUE && isset($this->userdata['session_id'])) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 327 | { |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 328 | $this->CI->db->delete($this->sess_table_name, array('session_id' => $this->userdata['session_id'])); |
dchill42 | 97b0d83 | 2012-09-04 10:09:00 -0400 | [diff] [blame] | 329 | $this->data_dirty = FALSE; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Kill the cookie |
Andrey Andreev | cf264e0 | 2012-10-18 16:14:51 +0300 | [diff] [blame] | 333 | $this->_setcookie($this->sess_cookie_name, '', ($this->now - 31500000), |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 334 | $this->cookie_path, $this->cookie_domain, 0); |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 335 | |
| 336 | // Kill session data |
| 337 | $this->userdata = array(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 338 | } |
| 339 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 340 | // ------------------------------------------------------------------------ |
| 341 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 342 | /** |
| 343 | * Regenerate the current session |
| 344 | * |
| 345 | * Regenerate the session id |
| 346 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 347 | * @param bool Destroy session data flag (default: false) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 348 | * @return void |
| 349 | */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 350 | public function sess_regenerate($destroy = FALSE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 351 | { |
| 352 | // Check destroy flag |
| 353 | if ($destroy) |
| 354 | { |
| 355 | // Destroy old session and create new one |
| 356 | $this->sess_destroy(); |
| 357 | $this->_sess_create(); |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | // Just force an update to recreate the id |
Andrey Andreev | 3f3f135 | 2012-09-05 16:39:28 +0300 | [diff] [blame] | 362 | $this->_sess_update(TRUE); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 366 | // ------------------------------------------------------------------------ |
| 367 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 368 | /** |
| 369 | * Get a reference to user data array |
| 370 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 371 | * @return array Reference to userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 372 | */ |
| 373 | public function &get_userdata() |
| 374 | { |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 375 | return $this->userdata; |
| 376 | } |
| 377 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 378 | // ------------------------------------------------------------------------ |
| 379 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 380 | /** |
| 381 | * Fetch the current session data if it exists |
| 382 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 383 | * @return bool |
| 384 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 385 | protected function _sess_read() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 386 | { |
| 387 | // Fetch the cookie |
| 388 | $session = $this->CI->input->cookie($this->sess_cookie_name); |
| 389 | |
| 390 | // No cookie? Goodbye cruel world!... |
Phil Sturgeon | 55a6ddb | 2012-05-23 18:37:24 +0100 | [diff] [blame] | 391 | if ($session === NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 392 | { |
| 393 | log_message('debug', 'A session cookie was not found.'); |
| 394 | return FALSE; |
| 395 | } |
| 396 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 397 | if ($this->sess_encrypt_cookie === TRUE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 398 | { |
Andrey Andreev | 4a2918a | 2014-02-05 01:03:46 +0200 | [diff] [blame] | 399 | $session = $this->CI->encryption->decrypt($session); |
| 400 | if ($session === FALSE) |
| 401 | { |
| 402 | log_message('error', 'Session: Unable to decrypt the session cookie, possibly due to a HMAC mismatch.'); |
| 403 | return FALSE; |
| 404 | } |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | if (($len = strlen($session) - 40) <= 0) |
| 409 | { |
| 410 | log_message('error', 'Session: The session cookie was not signed.'); |
| 411 | return FALSE; |
| 412 | } |
| 413 | |
| 414 | // Check cookie authentication |
| 415 | $hmac = substr($session, $len); |
| 416 | $session = substr($session, 0, $len); |
| 417 | |
Andrey Andreev | df7a696 | 2014-02-06 05:36:47 +0200 | [diff] [blame] | 418 | // Time-attack-safe comparison |
| 419 | $hmac_check = hash_hmac('sha1', $session, $this->encryption_key); |
| 420 | $diff = 0; |
| 421 | for ($i = 0; $i < 40; $i++) |
| 422 | { |
| 423 | $diff |= ord($hmac[$i]) ^ ord($hmac_check[$i]); |
| 424 | } |
| 425 | |
| 426 | if ($diff !== 0) |
Andrey Andreev | 4a2918a | 2014-02-05 01:03:46 +0200 | [diff] [blame] | 427 | { |
| 428 | log_message('error', 'Session: HMAC mismatch. The session cookie data did not match what was expected.'); |
| 429 | $this->sess_destroy(); |
| 430 | return FALSE; |
| 431 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 432 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 433 | |
| 434 | // Unserialize the session array |
Jordan Eldredge | 5306cad | 2013-12-23 11:10:51 -0800 | [diff] [blame] | 435 | $session = @unserialize($session); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 436 | |
| 437 | // Is the session data we unserialized an array with the correct format? |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 438 | if ( ! is_array($session) OR ! isset($session['session_id'], $session['ip_address'], $session['user_agent'], $session['last_activity'])) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 439 | { |
Andrey Andreev | e18de50 | 2013-07-17 19:59:20 +0300 | [diff] [blame] | 440 | log_message('debug', 'Session: Wrong cookie data format'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 441 | $this->sess_destroy(); |
| 442 | return FALSE; |
| 443 | } |
| 444 | |
| 445 | // Is the session current? |
Andrey Andreev | 0211768 | 2012-10-15 11:12:37 +0300 | [diff] [blame] | 446 | if (($session['last_activity'] + $this->sess_expiration) < $this->now OR $session['last_activity'] > $this->now) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 447 | { |
Andrey Andreev | e18de50 | 2013-07-17 19:59:20 +0300 | [diff] [blame] | 448 | log_message('debug', 'Session: Expired'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 449 | $this->sess_destroy(); |
| 450 | return FALSE; |
| 451 | } |
| 452 | |
Andrey Andreev | 7e087f5 | 2012-01-20 11:46:27 +0200 | [diff] [blame] | 453 | // Does the IP match? |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 454 | if ($this->sess_match_ip === TRUE && $session['ip_address'] !== $this->CI->input->ip_address()) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 455 | { |
Andrey Andreev | e18de50 | 2013-07-17 19:59:20 +0300 | [diff] [blame] | 456 | log_message('debug', 'Session: IP address mismatch'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 457 | $this->sess_destroy(); |
| 458 | return FALSE; |
| 459 | } |
| 460 | |
| 461 | // Does the User Agent Match? |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 462 | if ($this->sess_match_useragent === TRUE && |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 463 | trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120))) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 464 | { |
Andrey Andreev | e18de50 | 2013-07-17 19:59:20 +0300 | [diff] [blame] | 465 | log_message('debug', 'Session: User Agent string mismatch'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 466 | $this->sess_destroy(); |
| 467 | return FALSE; |
| 468 | } |
| 469 | |
| 470 | // Is there a corresponding session in the DB? |
| 471 | if ($this->sess_use_database === TRUE) |
| 472 | { |
| 473 | $this->CI->db->where('session_id', $session['session_id']); |
| 474 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 475 | if ($this->sess_match_ip === TRUE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 476 | { |
| 477 | $this->CI->db->where('ip_address', $session['ip_address']); |
| 478 | } |
| 479 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 480 | if ($this->sess_match_useragent === TRUE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 481 | { |
| 482 | $this->CI->db->where('user_agent', $session['user_agent']); |
| 483 | } |
| 484 | |
dchill42 | cd436e9 | 2012-09-04 10:15:14 -0400 | [diff] [blame] | 485 | // Is caching in effect? Turn it off |
| 486 | $db_cache = $this->CI->db->cache_on; |
| 487 | $this->CI->db->cache_off(); |
| 488 | |
Andrey Andreev | ed3fc51 | 2014-11-18 11:12:35 +0200 | [diff] [blame] | 489 | $query = $this->CI->db->get($this->sess_table_name); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 490 | |
dchill42 | cd436e9 | 2012-09-04 10:15:14 -0400 | [diff] [blame] | 491 | // Was caching in effect? |
| 492 | if ($db_cache) |
| 493 | { |
| 494 | // Turn it back on |
| 495 | $this->CI->db->cache_on(); |
| 496 | } |
| 497 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 498 | // No result? Kill it! |
Andrey Andreev | a8e34ac | 2012-12-17 10:39:32 +0200 | [diff] [blame] | 499 | if (empty($query) OR $query->num_rows() === 0) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 500 | { |
Andrey Andreev | e18de50 | 2013-07-17 19:59:20 +0300 | [diff] [blame] | 501 | log_message('debug', 'Session: No match found in our database'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 502 | $this->sess_destroy(); |
| 503 | return FALSE; |
| 504 | } |
| 505 | |
| 506 | // Is there custom data? If so, add it to the main session array |
| 507 | $row = $query->row(); |
Andrey Andreev | 5036c9c | 2012-06-04 15:34:56 +0300 | [diff] [blame] | 508 | if ( ! empty($row->user_data)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 509 | { |
Jordan Eldredge | 5306cad | 2013-12-23 11:10:51 -0800 | [diff] [blame] | 510 | $custom_data = unserialize(trim($row->user_data)); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 511 | |
| 512 | if (is_array($custom_data)) |
| 513 | { |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 514 | $session = $session + $custom_data; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // Session is valid! |
| 520 | $this->userdata = $session; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 521 | return TRUE; |
| 522 | } |
| 523 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 524 | // ------------------------------------------------------------------------ |
| 525 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 526 | /** |
| 527 | * Create a new session |
| 528 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 529 | * @return void |
| 530 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 531 | protected function _sess_create() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 532 | { |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 533 | // Initialize userdata |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 534 | $this->userdata = array( |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 535 | 'session_id' => $this->_make_sess_id(), |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 536 | 'ip_address' => $this->CI->input->ip_address(), |
Daniel Robbins | 930d8ef | 2013-03-01 21:36:48 -0500 | [diff] [blame] | 537 | 'user_agent' => trim(substr($this->CI->input->user_agent(), 0, 120)), |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 538 | 'last_activity' => $this->now, |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 539 | ); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 540 | |
Andrey Andreev | e18de50 | 2013-07-17 19:59:20 +0300 | [diff] [blame] | 541 | log_message('debug', 'Session: Creating new session ('.$this->userdata['session_id'].')'); |
| 542 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 543 | // Check for database |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 544 | if ($this->sess_use_database === TRUE) |
| 545 | { |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 546 | // Add empty user_data field and save the data to the DB |
| 547 | $this->CI->db->set('user_data', '')->insert($this->sess_table_name, $this->userdata); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | // Write the cookie |
| 551 | $this->_set_cookie(); |
| 552 | } |
| 553 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 554 | // ------------------------------------------------------------------------ |
| 555 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 556 | /** |
| 557 | * Update an existing session |
| 558 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 559 | * @param bool Force update flag (default: false) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 560 | * @return void |
| 561 | */ |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 562 | protected function _sess_update($force = FALSE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 563 | { |
| 564 | // We only update the session every five minutes by default (unless forced) |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 565 | if ( ! $force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 566 | { |
| 567 | return; |
| 568 | } |
| 569 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 570 | // Update last activity to now |
| 571 | $this->userdata['last_activity'] = $this->now; |
Andrey Andreev | 9c622f3 | 2012-01-19 14:12:54 +0200 | [diff] [blame] | 572 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 573 | // Save the old session id so we know which DB record to update |
| 574 | $old_sessid = $this->userdata['session_id']; |
| 575 | |
| 576 | // Changing the session ID during an AJAX call causes problems |
| 577 | if ( ! $this->CI->input->is_ajax_request()) |
Andrey Andreev | 9c622f3 | 2012-01-19 14:12:54 +0200 | [diff] [blame] | 578 | { |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 579 | // Get new id |
| 580 | $this->userdata['session_id'] = $this->_make_sess_id(); |
Andrey Andreev | e18de50 | 2013-07-17 19:59:20 +0300 | [diff] [blame] | 581 | |
| 582 | log_message('debug', 'Session: Regenerate ID'); |
Andrey Andreev | 9c622f3 | 2012-01-19 14:12:54 +0200 | [diff] [blame] | 583 | } |
| 584 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 585 | // Check for database |
| 586 | if ($this->sess_use_database === TRUE) |
| 587 | { |
Andrey Andreev | e2afc88 | 2012-11-01 01:35:34 +0200 | [diff] [blame] | 588 | $this->CI->db->where('session_id', $old_sessid); |
| 589 | |
| 590 | if ($this->sess_match_ip === TRUE) |
| 591 | { |
| 592 | $this->CI->db->where('ip_address', $this->CI->input->ip_address()); |
| 593 | } |
| 594 | |
| 595 | if ($this->sess_match_useragent === TRUE) |
| 596 | { |
| 597 | $this->CI->db->where('user_agent', trim(substr($this->CI->input->user_agent(), 0, 120))); |
| 598 | } |
| 599 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 600 | // Update the session ID and last_activity field in the DB |
Andrey Andreev | e2afc88 | 2012-11-01 01:35:34 +0200 | [diff] [blame] | 601 | $this->CI->db->update($this->sess_table_name, |
| 602 | array( |
| 603 | 'last_activity' => $this->now, |
| 604 | 'session_id' => $this->userdata['session_id'] |
| 605 | ) |
| 606 | ); |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // Write the cookie |
| 610 | $this->_set_cookie(); |
| 611 | } |
| 612 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 613 | // ------------------------------------------------------------------------ |
| 614 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 615 | /** |
| 616 | * Update database with current data |
| 617 | * |
| 618 | * This gets called from the shutdown function and also |
| 619 | * registered with PHP to run at the end of the request |
| 620 | * so it's guaranteed to update even when a fatal error |
| 621 | * occurs. The first call makes the update and clears the |
| 622 | * dirty flag so it won't happen twice. |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 623 | * |
| 624 | * @return void |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 625 | */ |
| 626 | public function _update_db() |
| 627 | { |
| 628 | // Check for database and dirty flag and unsaved |
| 629 | if ($this->sess_use_database === TRUE && $this->data_dirty === TRUE) |
| 630 | { |
| 631 | // Set up activity and data fields to be set |
| 632 | // If we don't find custom data, user_data will remain an empty string |
| 633 | $set = array( |
| 634 | 'last_activity' => $this->userdata['last_activity'], |
| 635 | 'user_data' => '' |
| 636 | ); |
| 637 | |
| 638 | // Get the custom userdata, leaving out the defaults |
| 639 | // (which get stored in the cookie) |
| 640 | $userdata = array_diff_key($this->userdata, $this->defaults); |
| 641 | |
| 642 | // Did we find any custom data? |
| 643 | if ( ! empty($userdata)) |
| 644 | { |
| 645 | // Serialize the custom data array so we can store it |
Jordan Eldredge | 5306cad | 2013-12-23 11:10:51 -0800 | [diff] [blame] | 646 | $set['user_data'] = serialize($userdata); |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 647 | } |
| 648 | |
Dionysis Arvanitis | f8e2d0e | 2013-02-19 23:27:16 +0200 | [diff] [blame] | 649 | // Reset query builder values. |
| 650 | $this->CI->db->reset_query(); |
| 651 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 652 | // Run the update query |
| 653 | // Any time we change the session id, it gets updated immediately, |
| 654 | // so our where clause below is always safe |
Andrey Andreev | e2afc88 | 2012-11-01 01:35:34 +0200 | [diff] [blame] | 655 | $this->CI->db->where('session_id', $this->userdata['session_id']); |
| 656 | |
| 657 | if ($this->sess_match_ip === TRUE) |
| 658 | { |
| 659 | $this->CI->db->where('ip_address', $this->CI->input->ip_address()); |
| 660 | } |
| 661 | |
| 662 | if ($this->sess_match_useragent === TRUE) |
| 663 | { |
| 664 | $this->CI->db->where('user_agent', trim(substr($this->CI->input->user_agent(), 0, 120))); |
| 665 | } |
| 666 | |
| 667 | $this->CI->db->update($this->sess_table_name, $set); |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 668 | |
| 669 | // Clear dirty flag to prevent double updates |
| 670 | $this->data_dirty = FALSE; |
| 671 | |
| 672 | log_message('debug', 'CI_Session Data Saved To DB'); |
| 673 | } |
| 674 | } |
| 675 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 676 | // ------------------------------------------------------------------------ |
| 677 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 678 | /** |
| 679 | * Generate a new session id |
| 680 | * |
| 681 | * @return string Hashed session id |
| 682 | */ |
| 683 | protected function _make_sess_id() |
| 684 | { |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 685 | $new_sessid = ''; |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 686 | do |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 687 | { |
vlakoff | 0612756 | 2013-03-30 00:06:39 +0100 | [diff] [blame] | 688 | $new_sessid .= mt_rand(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 689 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 690 | while (strlen($new_sessid) < 32); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 691 | |
| 692 | // To make the session ID even more secure we'll combine it with the user's IP |
| 693 | $new_sessid .= $this->CI->input->ip_address(); |
| 694 | |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 695 | // Turn it into a hash and return |
| 696 | return md5(uniqid($new_sessid, TRUE)); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 697 | } |
| 698 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 699 | // ------------------------------------------------------------------------ |
| 700 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 701 | /** |
| 702 | * Get the "now" time |
| 703 | * |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 704 | * @return int Time |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 705 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 706 | protected function _get_time() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 707 | { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 708 | if ($this->time_reference === 'local' OR $this->time_reference === date_default_timezone_get()) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 709 | { |
Iban Eguia | feb14da | 2012-06-12 16:09:36 +0200 | [diff] [blame] | 710 | return time(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 711 | } |
| 712 | |
Andrey Andreev | d163e0b | 2012-06-14 03:09:53 +0300 | [diff] [blame] | 713 | $datetime = new DateTime('now', new DateTimeZone($this->time_reference)); |
Iban Eguia | feb14da | 2012-06-12 16:09:36 +0200 | [diff] [blame] | 714 | sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second); |
| 715 | |
| 716 | return mktime($hour, $minute, $second, $month, $day, $year); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 717 | } |
| 718 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 719 | // ------------------------------------------------------------------------ |
| 720 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 721 | /** |
| 722 | * Write the session cookie |
| 723 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 724 | * @return void |
| 725 | */ |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 726 | protected function _set_cookie() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 727 | { |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 728 | // Get userdata (only defaults if database) |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 729 | $cookie_data = ($this->sess_use_database === TRUE) |
| 730 | ? array_intersect_key($this->userdata, $this->defaults) |
| 731 | : $this->userdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 732 | |
Andrey Andreev | bfb635b | 2014-01-08 18:32:05 +0200 | [diff] [blame] | 733 | // The Input class will do this and since we use HMAC verification, |
| 734 | // unless we standardize here as well, the hash won't match. |
| 735 | if ($this->_standardize_newlines) |
| 736 | { |
| 737 | foreach (array_keys($this->userdata) as $key) |
| 738 | { |
| 739 | $this->userdata[$key] = preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $this->userdata[$key]); |
| 740 | } |
| 741 | } |
| 742 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 743 | // Serialize the userdata for the cookie |
Jordan Eldredge | 5306cad | 2013-12-23 11:10:51 -0800 | [diff] [blame] | 744 | $cookie_data = serialize($cookie_data); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 745 | |
Pascal Kriete | f69f0e8 | 2012-10-16 11:54:49 -0400 | [diff] [blame] | 746 | if ($this->sess_encrypt_cookie === TRUE) |
| 747 | { |
Andrey Andreev | 4a2918a | 2014-02-05 01:03:46 +0200 | [diff] [blame] | 748 | $cookie_data = $this->CI->encryption->encrypt($cookie_data); |
Pascal Kriete | f69f0e8 | 2012-10-16 11:54:49 -0400 | [diff] [blame] | 749 | } |
Andrey Andreev | 4a2918a | 2014-02-05 01:03:46 +0200 | [diff] [blame] | 750 | else |
| 751 | { |
| 752 | // Require message authentication |
| 753 | $cookie_data .= hash_hmac('sha1', $cookie_data, $this->encryption_key); |
| 754 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 755 | |
| 756 | $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time(); |
| 757 | |
| 758 | // Set the cookie |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 759 | $this->_setcookie($this->sess_cookie_name, $cookie_data, $expire, $this->cookie_path, $this->cookie_domain, |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 760 | $this->cookie_secure, $this->cookie_httponly); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 761 | } |
| 762 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 763 | // ------------------------------------------------------------------------ |
| 764 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 765 | /** |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 766 | * Set a cookie with the system |
| 767 | * |
| 768 | * This abstraction of the setcookie call allows overriding for unit testing |
| 769 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 770 | * @param string Cookie name |
| 771 | * @param string Cookie value |
| 772 | * @param int Expiration time |
| 773 | * @param string Cookie path |
| 774 | * @param string Cookie domain |
| 775 | * @param bool Secure connection flag |
| 776 | * @param bool HTTP protocol only flag |
| 777 | * @return void |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 778 | */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 779 | protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = FALSE, $httponly = FALSE) |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 780 | { |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 781 | setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); |
| 782 | } |
| 783 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 784 | // ------------------------------------------------------------------------ |
| 785 | |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 786 | /** |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 787 | * Garbage collection |
| 788 | * |
| 789 | * This deletes expired session rows from database |
| 790 | * if the probability percentage is met |
| 791 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 792 | * @return void |
| 793 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 794 | protected function _sess_gc() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 795 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 796 | if ($this->sess_use_database !== TRUE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 797 | { |
| 798 | return; |
| 799 | } |
| 800 | |
Christopher Guiney | 7a14286 | 2012-06-29 20:34:28 -0700 | [diff] [blame] | 801 | $probability = ini_get('session.gc_probability'); |
| 802 | $divisor = ini_get('session.gc_divisor'); |
| 803 | |
Tyler Brownell | 74c5f26 | 2013-12-13 00:23:12 -0500 | [diff] [blame] | 804 | if (mt_rand(1, $divisor) <= $probability) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 805 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 806 | $expire = $this->now - $this->sess_expiration; |
dchill42 | 3cecd82 | 2012-08-28 21:37:27 -0400 | [diff] [blame] | 807 | $this->CI->db->delete($this->sess_table_name, 'last_activity < '.$expire); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 808 | |
| 809 | log_message('debug', 'Session garbage collection performed.'); |
| 810 | } |
| 811 | } |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 812 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 813 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 814 | |
| 815 | /* End of file Session_cookie.php */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 816 | /* Location: ./system/libraries/Session/drivers/Session_cookie.php */ |