Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 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. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Session Class |
| 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 |
| 38 | */ |
| 39 | class CI_Session { |
| 40 | |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 41 | public $sess_encrypt_cookie = FALSE; |
| 42 | public $sess_use_database = FALSE; |
| 43 | public $sess_table_name = ''; |
| 44 | public $sess_expiration = 7200; |
| 45 | public $sess_expire_on_close = FALSE; |
| 46 | public $sess_match_ip = FALSE; |
| 47 | public $sess_match_useragent = TRUE; |
| 48 | public $sess_cookie_name = 'ci_session'; |
| 49 | public $cookie_prefix = ''; |
| 50 | public $cookie_path = ''; |
| 51 | public $cookie_domain = ''; |
| 52 | public $cookie_secure = FALSE; |
| 53 | public $sess_time_to_update = 300; |
| 54 | public $encryption_key = ''; |
| 55 | public $flashdata_key = 'flash'; |
| 56 | public $time_reference = 'time'; |
| 57 | public $gc_probability = 5; |
| 58 | public $userdata = array(); |
| 59 | public $CI; |
| 60 | public $now; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Session Constructor |
| 64 | * |
| 65 | * The constructor runs the session routines automatically |
| 66 | * whenever the class is instantiated. |
| 67 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 68 | public function __construct($params = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | { |
| 70 | log_message('debug', "Session Class Initialized"); |
| 71 | |
| 72 | // Set the super object to a local variable for use throughout the class |
| 73 | $this->CI =& get_instance(); |
| 74 | |
| 75 | // Set all the session preferences, which can either be set |
| 76 | // manually via the $params array above or via the config file |
tobiasbg | ba6432c | 2011-02-18 21:58:48 +0100 | [diff] [blame] | 77 | foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'cookie_secure', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | { |
| 79 | $this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key); |
| 80 | } |
| 81 | |
Derek Jones | 5485db5 | 2010-08-30 21:31:08 -0500 | [diff] [blame] | 82 | if ($this->encryption_key == '') |
| 83 | { |
| 84 | show_error('In order to use the Session class you are required to set an encryption key in your config file.'); |
| 85 | } |
| 86 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | // Load the string helper so we can use the strip_slashes() function |
| 88 | $this->CI->load->helper('string'); |
| 89 | |
| 90 | // Do we need encryption? If so, load the encryption class |
| 91 | if ($this->sess_encrypt_cookie == TRUE) |
| 92 | { |
| 93 | $this->CI->load->library('encrypt'); |
| 94 | } |
| 95 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 96 | // Are we using a database? If so, load it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | if ($this->sess_use_database === TRUE AND $this->sess_table_name != '') |
| 98 | { |
| 99 | $this->CI->load->database(); |
| 100 | } |
| 101 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 102 | // Set the "now" time. Can either be GMT or server time, based on the |
| 103 | // config prefs. We use this to set the "last activity" time |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | $this->now = $this->_get_time(); |
| 105 | |
| 106 | // Set the session length. If the session expiration is |
| 107 | // set to zero we'll set the expiration two years from now. |
| 108 | if ($this->sess_expiration == 0) |
| 109 | { |
| 110 | $this->sess_expiration = (60*60*24*365*2); |
| 111 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 112 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | // Set the cookie name |
| 114 | $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name; |
| 115 | |
| 116 | // Run the Session routine. If a session doesn't exist we'll |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 117 | // create a new one. If it does, we'll update it. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | if ( ! $this->sess_read()) |
| 119 | { |
| 120 | $this->sess_create(); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | $this->sess_update(); |
| 125 | } |
| 126 | |
| 127 | // Delete 'old' flashdata (from last request) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | $this->_flashdata_sweep(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | |
| 130 | // Mark all new flashdata as old (data will be deleted before next request) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 131 | $this->_flashdata_mark(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | |
| 133 | // Delete expired sessions if necessary |
| 134 | $this->_sess_gc(); |
| 135 | |
| 136 | log_message('debug', "Session routines successfully run"); |
| 137 | } |
| 138 | |
| 139 | // -------------------------------------------------------------------- |
| 140 | |
| 141 | /** |
| 142 | * Fetch the current session data if it exists |
| 143 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | * @return bool |
| 145 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 146 | public function sess_read() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
| 148 | // Fetch the cookie |
| 149 | $session = $this->CI->input->cookie($this->sess_cookie_name); |
| 150 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 151 | // No cookie? Goodbye cruel world!... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | if ($session === FALSE) |
| 153 | { |
| 154 | log_message('debug', 'A session cookie was not found.'); |
| 155 | return FALSE; |
| 156 | } |
| 157 | |
| 158 | // Decrypt the cookie data |
| 159 | if ($this->sess_encrypt_cookie == TRUE) |
| 160 | { |
| 161 | $session = $this->CI->encrypt->decode($session); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | // encryption was not used, so we need to check the md5 hash |
| 166 | $hash = substr($session, strlen($session)-32); // get last 32 chars |
| 167 | $session = substr($session, 0, strlen($session)-32); |
| 168 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 169 | // Does the md5 hash match? This is to prevent manipulation of session data in userspace |
| 170 | if ($hash !== md5($session.$this->encryption_key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | { |
| 172 | log_message('error', 'The session cookie data did not match what was expected. This could be a possible hacking attempt.'); |
| 173 | $this->sess_destroy(); |
| 174 | return FALSE; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Unserialize the session array |
| 179 | $session = $this->_unserialize($session); |
| 180 | |
| 181 | // Is the session data we unserialized an array with the correct format? |
| 182 | if ( ! is_array($session) OR ! isset($session['session_id']) OR ! isset($session['ip_address']) OR ! isset($session['user_agent']) OR ! isset($session['last_activity'])) |
| 183 | { |
| 184 | $this->sess_destroy(); |
| 185 | return FALSE; |
| 186 | } |
| 187 | |
| 188 | // Is the session current? |
| 189 | if (($session['last_activity'] + $this->sess_expiration) < $this->now) |
| 190 | { |
| 191 | $this->sess_destroy(); |
| 192 | return FALSE; |
| 193 | } |
| 194 | |
| 195 | // Does the IP Match? |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 196 | if ($this->sess_match_ip == TRUE AND $session['ip_address'] !== $this->CI->input->ip_address()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | { |
| 198 | $this->sess_destroy(); |
| 199 | return FALSE; |
| 200 | } |
| 201 | |
| 202 | // Does the User Agent Match? |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 203 | if ($this->sess_match_useragent == TRUE AND trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | { |
| 205 | $this->sess_destroy(); |
| 206 | return FALSE; |
| 207 | } |
| 208 | |
| 209 | // Is there a corresponding session in the DB? |
| 210 | if ($this->sess_use_database === TRUE) |
| 211 | { |
| 212 | $this->CI->db->where('session_id', $session['session_id']); |
| 213 | |
| 214 | if ($this->sess_match_ip == TRUE) |
| 215 | { |
| 216 | $this->CI->db->where('ip_address', $session['ip_address']); |
| 217 | } |
| 218 | |
| 219 | if ($this->sess_match_useragent == TRUE) |
| 220 | { |
| 221 | $this->CI->db->where('user_agent', $session['user_agent']); |
| 222 | } |
| 223 | |
| 224 | $query = $this->CI->db->get($this->sess_table_name); |
| 225 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 226 | // No result? Kill it! |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 227 | if ($query->num_rows() === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | { |
| 229 | $this->sess_destroy(); |
| 230 | return FALSE; |
| 231 | } |
| 232 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 233 | // Is there custom data? If so, add it to the main session array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | $row = $query->row(); |
| 235 | if (isset($row->user_data) AND $row->user_data != '') |
| 236 | { |
| 237 | $custom_data = $this->_unserialize($row->user_data); |
| 238 | |
| 239 | if (is_array($custom_data)) |
| 240 | { |
| 241 | foreach ($custom_data as $key => $val) |
| 242 | { |
| 243 | $session[$key] = $val; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Session is valid! |
| 250 | $this->userdata = $session; |
| 251 | unset($session); |
| 252 | |
| 253 | return TRUE; |
| 254 | } |
| 255 | |
| 256 | // -------------------------------------------------------------------- |
| 257 | |
| 258 | /** |
| 259 | * Write the session data |
| 260 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | * @return void |
| 262 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 263 | public function sess_write() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 265 | // Are we saving custom data to the DB? If not, all we do is update the cookie |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | if ($this->sess_use_database === FALSE) |
| 267 | { |
| 268 | $this->_set_cookie(); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | // set the custom userdata, the session data we will set in a second |
| 273 | $custom_userdata = $this->userdata; |
| 274 | $cookie_userdata = array(); |
| 275 | |
| 276 | // Before continuing, we need to determine if there is any custom data to deal with. |
| 277 | // Let's determine this by removing the default indexes to see if there's anything left in the array |
| 278 | // and set the session data while we're at it |
| 279 | foreach (array('session_id','ip_address','user_agent','last_activity') as $val) |
| 280 | { |
| 281 | unset($custom_userdata[$val]); |
| 282 | $cookie_userdata[$val] = $this->userdata[$val]; |
| 283 | } |
| 284 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 285 | // Did we find any custom data? If not, we turn the empty array into a string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | // since there's no reason to serialize and store an empty array in the DB |
| 287 | if (count($custom_userdata) === 0) |
| 288 | { |
| 289 | $custom_userdata = ''; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | // Serialize the custom data array so we can store it |
| 294 | $custom_userdata = $this->_serialize($custom_userdata); |
| 295 | } |
| 296 | |
| 297 | // Run the update query |
| 298 | $this->CI->db->where('session_id', $this->userdata['session_id']); |
| 299 | $this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata)); |
| 300 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 301 | // Write the cookie. Notice that we manually pass the cookie data array to the |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | // _set_cookie() function. Normally that function will store $this->userdata, but |
| 303 | // in this case that array contains custom data, which we do not want in the cookie. |
| 304 | $this->_set_cookie($cookie_userdata); |
| 305 | } |
| 306 | |
| 307 | // -------------------------------------------------------------------- |
| 308 | |
| 309 | /** |
| 310 | * Create a new session |
| 311 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | * @return void |
| 313 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 314 | public function sess_create() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | { |
| 316 | $sessid = ''; |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 317 | do |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | { |
| 319 | $sessid .= mt_rand(0, mt_getrandmax()); |
| 320 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 321 | while (strlen($sessid) < 32); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | |
| 323 | // To make the session ID even more secure we'll combine it with the user's IP |
| 324 | $sessid .= $this->CI->input->ip_address(); |
| 325 | |
| 326 | $this->userdata = array( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 327 | 'session_id' => md5(uniqid($sessid, TRUE)), |
| 328 | 'ip_address' => $this->CI->input->ip_address(), |
Greg Aker | 50671cf | 2011-04-20 11:36:45 -0500 | [diff] [blame] | 329 | 'user_agent' => substr($this->CI->input->user_agent(), 0, 120), |
Kyle Farris | f57a46b | 2011-08-29 23:26:07 -0300 | [diff] [blame] | 330 | 'last_activity' => $this->now, |
| 331 | 'user_data' => '' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | ); |
| 333 | |
| 334 | |
| 335 | // Save the data to the DB if needed |
| 336 | if ($this->sess_use_database === TRUE) |
| 337 | { |
| 338 | $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata)); |
| 339 | } |
| 340 | |
| 341 | // Write the cookie |
| 342 | $this->_set_cookie(); |
| 343 | } |
| 344 | |
| 345 | // -------------------------------------------------------------------- |
| 346 | |
| 347 | /** |
| 348 | * Update an existing session |
| 349 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | * @return void |
| 351 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 352 | public function sess_update() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
| 354 | // We only update the session every five minutes by default |
| 355 | if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) |
| 356 | { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | // Save the old session id so we know which record to |
| 361 | // update in the database if we need it |
| 362 | $old_sessid = $this->userdata['session_id']; |
| 363 | $new_sessid = ''; |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 364 | do |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | { |
| 366 | $new_sessid .= mt_rand(0, mt_getrandmax()); |
| 367 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 368 | while (strlen($new_sessid) < 32); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | |
| 370 | // To make the session ID even more secure we'll combine it with the user's IP |
| 371 | $new_sessid .= $this->CI->input->ip_address(); |
| 372 | |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 373 | // Turn it into a hash and update the session data array |
| 374 | $this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | $this->userdata['last_activity'] = $this->now; |
| 376 | |
| 377 | // _set_cookie() will handle this for us if we aren't using database sessions |
| 378 | // by pushing all userdata to the cookie. |
| 379 | $cookie_data = NULL; |
| 380 | |
| 381 | // Update the session ID and last_activity field in the DB if needed |
| 382 | if ($this->sess_use_database === TRUE) |
| 383 | { |
| 384 | // set cookie explicitly to only have our session data |
| 385 | $cookie_data = array(); |
| 386 | foreach (array('session_id','ip_address','user_agent','last_activity') as $val) |
| 387 | { |
| 388 | $cookie_data[$val] = $this->userdata[$val]; |
| 389 | } |
| 390 | |
| 391 | $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid))); |
| 392 | } |
| 393 | |
| 394 | // Write the cookie |
| 395 | $this->_set_cookie($cookie_data); |
| 396 | } |
| 397 | |
| 398 | // -------------------------------------------------------------------- |
| 399 | |
| 400 | /** |
| 401 | * Destroy the current session |
| 402 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | * @return void |
| 404 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 405 | public function sess_destroy() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | { |
| 407 | // Kill the session DB row |
| 408 | if ($this->sess_use_database === TRUE AND isset($this->userdata['session_id'])) |
| 409 | { |
| 410 | $this->CI->db->where('session_id', $this->userdata['session_id']); |
| 411 | $this->CI->db->delete($this->sess_table_name); |
| 412 | } |
| 413 | |
| 414 | // Kill the cookie |
| 415 | setcookie( |
| 416 | $this->sess_cookie_name, |
| 417 | addslashes(serialize(array())), |
| 418 | ($this->now - 31500000), |
| 419 | $this->cookie_path, |
| 420 | $this->cookie_domain, |
| 421 | 0 |
| 422 | ); |
| 423 | } |
| 424 | |
| 425 | // -------------------------------------------------------------------- |
| 426 | |
| 427 | /** |
| 428 | * Fetch a specific item from the session array |
| 429 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | * @param string |
| 431 | * @return string |
| 432 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 433 | public function userdata($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | { |
| 435 | return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item]; |
| 436 | } |
| 437 | |
| 438 | // -------------------------------------------------------------------- |
| 439 | |
| 440 | /** |
| 441 | * Fetch all session data |
| 442 | * |
Greg Aker | 3403366 | 2011-04-18 11:18:09 -0500 | [diff] [blame] | 443 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 445 | public function all_userdata() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | { |
Greg Aker | 3403366 | 2011-04-18 11:18:09 -0500 | [diff] [blame] | 447 | return $this->userdata; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // -------------------------------------------------------------------- |
| 451 | |
| 452 | /** |
| 453 | * Add or change data in the "userdata" array |
| 454 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | * @param mixed |
| 456 | * @param string |
| 457 | * @return void |
| 458 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 459 | public function set_userdata($newdata = array(), $newval = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | { |
| 461 | if (is_string($newdata)) |
| 462 | { |
| 463 | $newdata = array($newdata => $newval); |
| 464 | } |
| 465 | |
| 466 | if (count($newdata) > 0) |
| 467 | { |
| 468 | foreach ($newdata as $key => $val) |
| 469 | { |
| 470 | $this->userdata[$key] = $val; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | $this->sess_write(); |
| 475 | } |
| 476 | |
| 477 | // -------------------------------------------------------------------- |
| 478 | |
| 479 | /** |
| 480 | * Delete a session variable from the "userdata" array |
| 481 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | * @return void |
| 483 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 484 | public function unset_userdata($newdata = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | { |
| 486 | if (is_string($newdata)) |
| 487 | { |
| 488 | $newdata = array($newdata => ''); |
| 489 | } |
| 490 | |
| 491 | if (count($newdata) > 0) |
| 492 | { |
| 493 | foreach ($newdata as $key => $val) |
| 494 | { |
| 495 | unset($this->userdata[$key]); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | $this->sess_write(); |
| 500 | } |
| 501 | |
| 502 | // ------------------------------------------------------------------------ |
| 503 | |
| 504 | /** |
| 505 | * Add or change flashdata, only available |
| 506 | * until the next request |
| 507 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | * @param mixed |
| 509 | * @param string |
| 510 | * @return void |
| 511 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 512 | public function set_flashdata($newdata = array(), $newval = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 513 | { |
| 514 | if (is_string($newdata)) |
| 515 | { |
| 516 | $newdata = array($newdata => $newval); |
| 517 | } |
| 518 | |
| 519 | if (count($newdata) > 0) |
| 520 | { |
| 521 | foreach ($newdata as $key => $val) |
| 522 | { |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 523 | $this->set_userdata($this->flashdata_key.':new:'.$key, $val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // ------------------------------------------------------------------------ |
| 529 | |
| 530 | /** |
| 531 | * Keeps existing flashdata available to next request. |
| 532 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | * @param string |
| 534 | * @return void |
| 535 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 536 | public function keep_flashdata($key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 538 | // 'old' flashdata gets removed. Here we mark all |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | // flashdata as 'new' to preserve it from _flashdata_sweep() |
| 540 | // Note the function will return FALSE if the $key |
| 541 | // provided cannot be found |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 542 | $value = $this->userdata($this->flashdata_key.':old:'.$key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 544 | $this->set_userdata($this->flashdata_key.':new:'.$key, $value); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // ------------------------------------------------------------------------ |
| 548 | |
| 549 | /** |
| 550 | * Fetch a specific flashdata item from the session array |
| 551 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | * @param string |
| 553 | * @return string |
| 554 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 555 | public function flashdata($key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | { |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 557 | return $this->userdata($this->flashdata_key.':old:'.$key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | // ------------------------------------------------------------------------ |
| 561 | |
| 562 | /** |
| 563 | * Identifies flashdata as 'old' for removal |
| 564 | * when _flashdata_sweep() runs. |
| 565 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | * @return void |
| 567 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 568 | protected function _flashdata_mark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 569 | { |
| 570 | $userdata = $this->all_userdata(); |
| 571 | foreach ($userdata as $name => $value) |
| 572 | { |
| 573 | $parts = explode(':new:', $name); |
| 574 | if (is_array($parts) && count($parts) === 2) |
| 575 | { |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 576 | $this->set_userdata($this->flashdata_key.':old:'.$parts[1], $value); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 577 | $this->unset_userdata($name); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // ------------------------------------------------------------------------ |
| 583 | |
| 584 | /** |
| 585 | * Removes all flashdata marked as 'old' |
| 586 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | * @return void |
| 588 | */ |
| 589 | |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 590 | protected function _flashdata_sweep() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | { |
| 592 | $userdata = $this->all_userdata(); |
| 593 | foreach ($userdata as $key => $value) |
| 594 | { |
| 595 | if (strpos($key, ':old:')) |
| 596 | { |
| 597 | $this->unset_userdata($key); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | } |
| 602 | |
| 603 | // -------------------------------------------------------------------- |
| 604 | |
| 605 | /** |
| 606 | * Get the "now" time |
| 607 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 608 | * @return string |
| 609 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 610 | protected function _get_time() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 611 | { |
Andrey Andreev | 85f018f | 2011-12-27 02:52:36 +0200 | [diff] [blame] | 612 | if (strtolower($this->time_reference) === 'gmt') |
| 613 | { |
| 614 | $now = time(); |
| 615 | return mktime(gmdate('H', $now), gmdate('i', $now), gmdate('s', $now), gmdate('m', $now), gmdate('d', $now), gmdate('Y', $now)); |
| 616 | } |
| 617 | |
| 618 | return time(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | // -------------------------------------------------------------------- |
| 622 | |
| 623 | /** |
| 624 | * Write the session cookie |
| 625 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | * @return void |
| 627 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 628 | protected function _set_cookie($cookie_data = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 629 | { |
| 630 | if (is_null($cookie_data)) |
| 631 | { |
| 632 | $cookie_data = $this->userdata; |
| 633 | } |
| 634 | |
| 635 | // Serialize the userdata for the cookie |
| 636 | $cookie_data = $this->_serialize($cookie_data); |
| 637 | |
| 638 | if ($this->sess_encrypt_cookie == TRUE) |
| 639 | { |
| 640 | $cookie_data = $this->CI->encrypt->encode($cookie_data); |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | // if encryption is not used, we provide an md5 hash to prevent userside tampering |
| 645 | $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key); |
| 646 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 647 | |
Derek Jones | eaa71ba | 2010-09-02 10:32:07 -0500 | [diff] [blame] | 648 | $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 649 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 650 | // Set the cookie |
| 651 | setcookie( |
| 652 | $this->sess_cookie_name, |
| 653 | $cookie_data, |
Derek Jones | eaa71ba | 2010-09-02 10:32:07 -0500 | [diff] [blame] | 654 | $expire, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | $this->cookie_path, |
| 656 | $this->cookie_domain, |
tobiasbg | ba6432c | 2011-02-18 21:58:48 +0100 | [diff] [blame] | 657 | $this->cookie_secure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 658 | ); |
| 659 | } |
| 660 | |
| 661 | // -------------------------------------------------------------------- |
| 662 | |
| 663 | /** |
| 664 | * Serialize an array |
| 665 | * |
| 666 | * This function first converts any slashes found in the array to a temporary |
| 667 | * marker, so when it gets unserialized the slashes will be preserved |
| 668 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 669 | * @param array |
| 670 | * @return string |
| 671 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 672 | protected function _serialize($data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 673 | { |
| 674 | if (is_array($data)) |
| 675 | { |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 676 | array_walk_recursive($data, array(&$this, '_escape_slashes')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 677 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 678 | elseif (is_string($data)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 679 | { |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 680 | $data = str_replace('\\', '{{slash}}', $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 681 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | return serialize($data); |
| 683 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 684 | |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 685 | /** |
| 686 | * Escape slashes |
| 687 | * |
| 688 | * This function converts any slashes found into a temporary marker |
| 689 | * |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 690 | */ |
| 691 | function _escape_slashes(&$val, $key) |
| 692 | { |
| 693 | if (is_string($val)) |
| 694 | { |
| 695 | $val = str_replace('\\', '{{slash}}', $val); |
| 696 | } |
| 697 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 698 | |
| 699 | // -------------------------------------------------------------------- |
| 700 | |
| 701 | /** |
| 702 | * Unserialize |
| 703 | * |
| 704 | * This function unserializes a data string, then converts any |
| 705 | * temporary slash markers back to actual slashes |
| 706 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 707 | * @param array |
| 708 | * @return string |
| 709 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 710 | protected function _unserialize($data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 711 | { |
| 712 | $data = @unserialize(strip_slashes($data)); |
| 713 | |
| 714 | if (is_array($data)) |
| 715 | { |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 716 | array_walk_recursive($data, array(&$this, '_unescape_slashes')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 717 | return $data; |
| 718 | } |
| 719 | |
Derek Jones | 133e666 | 2010-03-29 11:36:42 -0500 | [diff] [blame] | 720 | return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 721 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 722 | |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 723 | /** |
| 724 | * Unescape slashes |
| 725 | * |
| 726 | * This function converts any slash markers back into actual slashes |
| 727 | * |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 728 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 729 | protected function _unescape_slashes(&$val, $key) |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 730 | { |
Chris Muench | 3e414f9 | 2011-10-16 23:03:55 -0400 | [diff] [blame] | 731 | if (is_string($val)) |
| 732 | { |
| 733 | $val= str_replace('{{slash}}', '\\', $val); |
| 734 | } |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 735 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 736 | |
| 737 | // -------------------------------------------------------------------- |
| 738 | |
| 739 | /** |
| 740 | * Garbage collection |
| 741 | * |
| 742 | * This deletes expired session rows from database |
| 743 | * if the probability percentage is met |
| 744 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 745 | * @return void |
| 746 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 747 | protected function _sess_gc() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 748 | { |
| 749 | if ($this->sess_use_database != TRUE) |
| 750 | { |
| 751 | return; |
| 752 | } |
| 753 | |
| 754 | srand(time()); |
| 755 | if ((rand() % 100) < $this->gc_probability) |
| 756 | { |
| 757 | $expire = $this->now - $this->sess_expiration; |
| 758 | |
| 759 | $this->CI->db->where("last_activity < {$expire}"); |
| 760 | $this->CI->db->delete($this->sess_table_name); |
| 761 | |
| 762 | log_message('debug', 'Session garbage collection performed.'); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | |
| 767 | } |
| 768 | // END Session Class |
| 769 | |
| 770 | /* End of file Session.php */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 771 | /* Location: ./system/libraries/Session.php */ |