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