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