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