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 |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 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 | { |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 70 | log_message('debug', 'Session Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [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 | |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [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 |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [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 | |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 136 | log_message('debug', 'Session routines successfully run'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 169 | // Does the md5 hash match? This is to prevent manipulation of session data in userspace |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 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? |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 182 | if ( ! is_array($session) OR ! isset($session['session_id'], $session['ip_address'], $session['user_agent'], $session['last_activity']) |
| 183 | OR ($session['last_activity'] + $this->sess_expiration) < $this->now // Is the session current? |
| 184 | OR ($this->sess_match_ip == TRUE && $session['ip_address'] !== $this->CI->input->ip_address()) // Does the IP match? |
| 185 | OR ($this->sess_match_useragent == TRUE && trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120))) // Does the User Agent Match? |
| 186 | ) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | { |
| 188 | $this->sess_destroy(); |
| 189 | return FALSE; |
| 190 | } |
| 191 | |
| 192 | // Is there a corresponding session in the DB? |
| 193 | if ($this->sess_use_database === TRUE) |
| 194 | { |
| 195 | $this->CI->db->where('session_id', $session['session_id']); |
| 196 | |
| 197 | if ($this->sess_match_ip == TRUE) |
| 198 | { |
| 199 | $this->CI->db->where('ip_address', $session['ip_address']); |
| 200 | } |
| 201 | |
| 202 | if ($this->sess_match_useragent == TRUE) |
| 203 | { |
| 204 | $this->CI->db->where('user_agent', $session['user_agent']); |
| 205 | } |
| 206 | |
| 207 | $query = $this->CI->db->get($this->sess_table_name); |
| 208 | |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 209 | // No result? Kill it! |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 210 | if ($query->num_rows() === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | { |
| 212 | $this->sess_destroy(); |
| 213 | return FALSE; |
| 214 | } |
| 215 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 216 | // 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] | 217 | $row = $query->row(); |
| 218 | if (isset($row->user_data) AND $row->user_data != '') |
| 219 | { |
| 220 | $custom_data = $this->_unserialize($row->user_data); |
| 221 | |
| 222 | if (is_array($custom_data)) |
| 223 | { |
| 224 | foreach ($custom_data as $key => $val) |
| 225 | { |
| 226 | $session[$key] = $val; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // Session is valid! |
| 233 | $this->userdata = $session; |
| 234 | unset($session); |
| 235 | |
| 236 | return TRUE; |
| 237 | } |
| 238 | |
| 239 | // -------------------------------------------------------------------- |
| 240 | |
| 241 | /** |
| 242 | * Write the session data |
| 243 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | * @return void |
| 245 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 246 | public function sess_write() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 248 | // 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] | 249 | if ($this->sess_use_database === FALSE) |
| 250 | { |
| 251 | $this->_set_cookie(); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | // set the custom userdata, the session data we will set in a second |
| 256 | $custom_userdata = $this->userdata; |
| 257 | $cookie_userdata = array(); |
| 258 | |
| 259 | // Before continuing, we need to determine if there is any custom data to deal with. |
| 260 | // Let's determine this by removing the default indexes to see if there's anything left in the array |
| 261 | // and set the session data while we're at it |
| 262 | foreach (array('session_id','ip_address','user_agent','last_activity') as $val) |
| 263 | { |
| 264 | unset($custom_userdata[$val]); |
| 265 | $cookie_userdata[$val] = $this->userdata[$val]; |
| 266 | } |
| 267 | |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 268 | // 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] | 269 | // since there's no reason to serialize and store an empty array in the DB |
| 270 | if (count($custom_userdata) === 0) |
| 271 | { |
| 272 | $custom_userdata = ''; |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | // Serialize the custom data array so we can store it |
| 277 | $custom_userdata = $this->_serialize($custom_userdata); |
| 278 | } |
| 279 | |
| 280 | // Run the update query |
| 281 | $this->CI->db->where('session_id', $this->userdata['session_id']); |
| 282 | $this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata)); |
| 283 | |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 284 | // 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] | 285 | // _set_cookie() function. Normally that function will store $this->userdata, but |
| 286 | // in this case that array contains custom data, which we do not want in the cookie. |
| 287 | $this->_set_cookie($cookie_userdata); |
| 288 | } |
| 289 | |
| 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
| 293 | * Create a new session |
| 294 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | * @return void |
| 296 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 297 | public function sess_create() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | { |
| 299 | $sessid = ''; |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 300 | do |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | { |
| 302 | $sessid .= mt_rand(0, mt_getrandmax()); |
| 303 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 304 | while (strlen($sessid) < 32); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | |
| 306 | // To make the session ID even more secure we'll combine it with the user's IP |
| 307 | $sessid .= $this->CI->input->ip_address(); |
| 308 | |
| 309 | $this->userdata = array( |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 310 | 'session_id' => md5(uniqid($sessid, TRUE)), |
| 311 | 'ip_address' => $this->CI->input->ip_address(), |
| 312 | 'user_agent' => substr($this->CI->input->user_agent(), 0, 120), |
| 313 | 'last_activity' => $this->now, |
| 314 | 'user_data' => '' |
| 315 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | |
| 317 | // Save the data to the DB if needed |
| 318 | if ($this->sess_use_database === TRUE) |
| 319 | { |
| 320 | $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata)); |
| 321 | } |
| 322 | |
| 323 | // Write the cookie |
| 324 | $this->_set_cookie(); |
| 325 | } |
| 326 | |
| 327 | // -------------------------------------------------------------------- |
| 328 | |
| 329 | /** |
| 330 | * Update an existing session |
| 331 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | * @return void |
| 333 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 334 | public function sess_update() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | { |
| 336 | // We only update the session every five minutes by default |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 337 | if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now |
| 338 | OR $this->CI->input->is_ajax_request()) // Changing the session ID during an AJAX call causes problems |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | // Save the old session id so we know which record to |
| 344 | // update in the database if we need it |
| 345 | $old_sessid = $this->userdata['session_id']; |
| 346 | $new_sessid = ''; |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 347 | do |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | { |
| 349 | $new_sessid .= mt_rand(0, mt_getrandmax()); |
| 350 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 351 | while (strlen($new_sessid) < 32); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | |
| 353 | // To make the session ID even more secure we'll combine it with the user's IP |
| 354 | $new_sessid .= $this->CI->input->ip_address(); |
| 355 | |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 356 | // Turn it into a hash and update the session data array |
| 357 | $this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | $this->userdata['last_activity'] = $this->now; |
| 359 | |
| 360 | // _set_cookie() will handle this for us if we aren't using database sessions |
| 361 | // by pushing all userdata to the cookie. |
| 362 | $cookie_data = NULL; |
| 363 | |
| 364 | // Update the session ID and last_activity field in the DB if needed |
| 365 | if ($this->sess_use_database === TRUE) |
| 366 | { |
| 367 | // set cookie explicitly to only have our session data |
| 368 | $cookie_data = array(); |
| 369 | foreach (array('session_id','ip_address','user_agent','last_activity') as $val) |
| 370 | { |
| 371 | $cookie_data[$val] = $this->userdata[$val]; |
| 372 | } |
| 373 | |
| 374 | $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))); |
| 375 | } |
| 376 | |
| 377 | // Write the cookie |
| 378 | $this->_set_cookie($cookie_data); |
| 379 | } |
| 380 | |
| 381 | // -------------------------------------------------------------------- |
| 382 | |
| 383 | /** |
| 384 | * Destroy the current session |
| 385 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | * @return void |
| 387 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 388 | public function sess_destroy() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | { |
| 390 | // Kill the session DB row |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 391 | if ($this->sess_use_database === TRUE && isset($this->userdata['session_id'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | { |
| 393 | $this->CI->db->where('session_id', $this->userdata['session_id']); |
| 394 | $this->CI->db->delete($this->sess_table_name); |
| 395 | } |
| 396 | |
| 397 | // Kill the cookie |
| 398 | setcookie( |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 399 | $this->sess_cookie_name, |
| 400 | addslashes(serialize(array())), |
| 401 | ($this->now - 31500000), |
| 402 | $this->cookie_path, |
| 403 | $this->cookie_domain, |
| 404 | 0 |
| 405 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // -------------------------------------------------------------------- |
| 409 | |
| 410 | /** |
| 411 | * Fetch a specific item from the session array |
| 412 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | * @param string |
| 414 | * @return string |
| 415 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 416 | public function userdata($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | { |
| 418 | return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item]; |
| 419 | } |
| 420 | |
| 421 | // -------------------------------------------------------------------- |
| 422 | |
| 423 | /** |
| 424 | * Fetch all session data |
| 425 | * |
Greg Aker | 3403366 | 2011-04-18 11:18:09 -0500 | [diff] [blame] | 426 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 428 | public function all_userdata() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | { |
Greg Aker | 3403366 | 2011-04-18 11:18:09 -0500 | [diff] [blame] | 430 | return $this->userdata; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // -------------------------------------------------------------------- |
| 434 | |
| 435 | /** |
| 436 | * Add or change data in the "userdata" array |
| 437 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 438 | * @param mixed |
| 439 | * @param string |
| 440 | * @return void |
| 441 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 442 | public function set_userdata($newdata = array(), $newval = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | { |
| 444 | if (is_string($newdata)) |
| 445 | { |
| 446 | $newdata = array($newdata => $newval); |
| 447 | } |
| 448 | |
| 449 | if (count($newdata) > 0) |
| 450 | { |
| 451 | foreach ($newdata as $key => $val) |
| 452 | { |
| 453 | $this->userdata[$key] = $val; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | $this->sess_write(); |
| 458 | } |
| 459 | |
| 460 | // -------------------------------------------------------------------- |
| 461 | |
| 462 | /** |
| 463 | * Delete a session variable from the "userdata" array |
| 464 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | * @return void |
| 466 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 467 | public function unset_userdata($newdata = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | { |
| 469 | if (is_string($newdata)) |
| 470 | { |
| 471 | $newdata = array($newdata => ''); |
| 472 | } |
| 473 | |
| 474 | if (count($newdata) > 0) |
| 475 | { |
| 476 | foreach ($newdata as $key => $val) |
| 477 | { |
| 478 | unset($this->userdata[$key]); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | $this->sess_write(); |
| 483 | } |
| 484 | |
| 485 | // ------------------------------------------------------------------------ |
| 486 | |
| 487 | /** |
| 488 | * Add or change flashdata, only available |
| 489 | * until the next request |
| 490 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | * @param mixed |
| 492 | * @param string |
| 493 | * @return void |
| 494 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 495 | public function set_flashdata($newdata = array(), $newval = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | { |
| 497 | if (is_string($newdata)) |
| 498 | { |
| 499 | $newdata = array($newdata => $newval); |
| 500 | } |
| 501 | |
| 502 | if (count($newdata) > 0) |
| 503 | { |
| 504 | foreach ($newdata as $key => $val) |
| 505 | { |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 506 | $this->set_userdata($this->flashdata_key.':new:'.$key, $val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // ------------------------------------------------------------------------ |
| 512 | |
| 513 | /** |
| 514 | * Keeps existing flashdata available to next request. |
| 515 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 516 | * @param string |
| 517 | * @return void |
| 518 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 519 | public function keep_flashdata($key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | { |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 521 | // 'old' flashdata gets removed. Here we mark all |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | // flashdata as 'new' to preserve it from _flashdata_sweep() |
| 523 | // Note the function will return FALSE if the $key |
| 524 | // provided cannot be found |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 525 | $value = $this->userdata($this->flashdata_key.':old:'.$key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 526 | |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 527 | $this->set_userdata($this->flashdata_key.':new:'.$key, $value); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | // ------------------------------------------------------------------------ |
| 531 | |
| 532 | /** |
| 533 | * Fetch a specific flashdata item from the session array |
| 534 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | * @param string |
| 536 | * @return string |
| 537 | */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 538 | public function flashdata($key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | { |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 540 | return $this->userdata($this->flashdata_key.':old:'.$key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | // ------------------------------------------------------------------------ |
| 544 | |
| 545 | /** |
| 546 | * Identifies flashdata as 'old' for removal |
| 547 | * when _flashdata_sweep() runs. |
| 548 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 549 | * @return void |
| 550 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 551 | protected function _flashdata_mark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | { |
| 553 | $userdata = $this->all_userdata(); |
| 554 | foreach ($userdata as $name => $value) |
| 555 | { |
| 556 | $parts = explode(':new:', $name); |
| 557 | if (is_array($parts) && count($parts) === 2) |
| 558 | { |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 559 | $this->set_userdata($this->flashdata_key.':old:'.$parts[1], $value); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | $this->unset_userdata($name); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | // ------------------------------------------------------------------------ |
| 566 | |
| 567 | /** |
| 568 | * Removes all flashdata marked as 'old' |
| 569 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | * @return void |
| 571 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 572 | protected function _flashdata_sweep() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 573 | { |
| 574 | $userdata = $this->all_userdata(); |
| 575 | foreach ($userdata as $key => $value) |
| 576 | { |
| 577 | if (strpos($key, ':old:')) |
| 578 | { |
| 579 | $this->unset_userdata($key); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | } |
| 584 | |
| 585 | // -------------------------------------------------------------------- |
| 586 | |
| 587 | /** |
| 588 | * Get the "now" time |
| 589 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 590 | * @return string |
| 591 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 592 | protected function _get_time() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | { |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 594 | return (strtolower($this->time_reference) === 'gmt') |
| 595 | ? mktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y')) |
| 596 | : time(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // -------------------------------------------------------------------- |
| 600 | |
| 601 | /** |
| 602 | * Write the session cookie |
| 603 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | * @return void |
| 605 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 606 | protected function _set_cookie($cookie_data = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | { |
| 608 | if (is_null($cookie_data)) |
| 609 | { |
| 610 | $cookie_data = $this->userdata; |
| 611 | } |
| 612 | |
| 613 | // Serialize the userdata for the cookie |
| 614 | $cookie_data = $this->_serialize($cookie_data); |
| 615 | |
| 616 | if ($this->sess_encrypt_cookie == TRUE) |
| 617 | { |
| 618 | $cookie_data = $this->CI->encrypt->encode($cookie_data); |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | // if encryption is not used, we provide an md5 hash to prevent userside tampering |
| 623 | $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key); |
| 624 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 625 | |
Derek Jones | eaa71ba | 2010-09-02 10:32:07 -0500 | [diff] [blame] | 626 | $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 627 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 628 | // Set the cookie |
| 629 | setcookie( |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 630 | $this->sess_cookie_name, |
| 631 | $cookie_data, |
| 632 | $expire, |
| 633 | $this->cookie_path, |
| 634 | $this->cookie_domain, |
| 635 | $this->cookie_secure |
| 636 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | // -------------------------------------------------------------------- |
| 640 | |
| 641 | /** |
| 642 | * Serialize an array |
| 643 | * |
| 644 | * This function first converts any slashes found in the array to a temporary |
| 645 | * marker, so when it gets unserialized the slashes will be preserved |
| 646 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 647 | * @param array |
| 648 | * @return string |
| 649 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 650 | protected function _serialize($data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 651 | { |
| 652 | if (is_array($data)) |
| 653 | { |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 654 | array_walk_recursive($data, array(&$this, '_escape_slashes')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 656 | elseif (is_string($data)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 657 | { |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 658 | $data = str_replace('\\', '{{slash}}', $data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 659 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 660 | return serialize($data); |
| 661 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 662 | |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 663 | /** |
| 664 | * Escape slashes |
| 665 | * |
| 666 | * This function converts any slashes found into a temporary marker |
| 667 | * |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 668 | * @param string |
| 669 | * @param string |
| 670 | * @return void |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 671 | */ |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 672 | protected function _escape_slashes(&$val, $key) |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 673 | { |
| 674 | if (is_string($val)) |
| 675 | { |
| 676 | $val = str_replace('\\', '{{slash}}', $val); |
| 677 | } |
| 678 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 679 | |
| 680 | // -------------------------------------------------------------------- |
| 681 | |
| 682 | /** |
| 683 | * Unserialize |
| 684 | * |
| 685 | * This function unserializes a data string, then converts any |
| 686 | * temporary slash markers back to actual slashes |
| 687 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 688 | * @param array |
| 689 | * @return string |
| 690 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 691 | protected function _unserialize($data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 692 | { |
| 693 | $data = @unserialize(strip_slashes($data)); |
| 694 | |
| 695 | if (is_array($data)) |
| 696 | { |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 697 | array_walk_recursive($data, array(&$this, '_unescape_slashes')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 698 | return $data; |
| 699 | } |
| 700 | |
Derek Jones | 133e666 | 2010-03-29 11:36:42 -0500 | [diff] [blame] | 701 | return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 702 | } |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 703 | |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 704 | /** |
| 705 | * Unescape slashes |
| 706 | * |
| 707 | * This function converts any slash markers back into actual slashes |
| 708 | * |
Andrey Andreev | eea2ff5 | 2012-01-19 13:21:53 +0200 | [diff] [blame^] | 709 | * @param string |
| 710 | * @param string |
| 711 | * @return void |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 712 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 713 | protected function _unescape_slashes(&$val, $key) |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 714 | { |
Chris Muench | 3e414f9 | 2011-10-16 23:03:55 -0400 | [diff] [blame] | 715 | if (is_string($val)) |
| 716 | { |
| 717 | $val= str_replace('{{slash}}', '\\', $val); |
| 718 | } |
Chris Muench | 9593349 | 2011-10-16 14:14:04 -0400 | [diff] [blame] | 719 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 720 | |
| 721 | // -------------------------------------------------------------------- |
| 722 | |
| 723 | /** |
| 724 | * Garbage collection |
| 725 | * |
| 726 | * This deletes expired session rows from database |
| 727 | * if the probability percentage is met |
| 728 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 729 | * @return void |
| 730 | */ |
Andrey Andreev | 2c79b76 | 2011-12-26 16:54:44 +0200 | [diff] [blame] | 731 | protected function _sess_gc() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 732 | { |
| 733 | if ($this->sess_use_database != TRUE) |
| 734 | { |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | srand(time()); |
| 739 | if ((rand() % 100) < $this->gc_probability) |
| 740 | { |
| 741 | $expire = $this->now - $this->sess_expiration; |
| 742 | |
| 743 | $this->CI->db->where("last_activity < {$expire}"); |
| 744 | $this->CI->db->delete($this->sess_table_name); |
| 745 | |
| 746 | log_message('debug', 'Session garbage collection performed.'); |
| 747 | } |
| 748 | } |
| 749 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 750 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 751 | |
| 752 | /* End of file Session.php */ |
Andrey Andreev | 57ffbbb | 2011-12-25 04:48:47 +0200 | [diff] [blame] | 753 | /* Location: ./system/libraries/Session.php */ |