blob: 783109a60e09084ac47a42f72eebe04c3d913a4f [file] [log] [blame]
Andrey Andreev57ffbbb2011-12-25 04:48:47 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev57ffbbb2011-12-25 04:48:47 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreevf863a022012-01-24 15:31:54 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Session Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Sessions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/sessions.html
36 */
37class CI_Session {
38
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020039 /**
Timothy Warren68f09812012-04-27 10:38:32 -040040 * Whether to encrypt the session cookie
41 *
42 * @var bool
43 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020044 public $sess_encrypt_cookie = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020045
Timothy Warren68f09812012-04-27 10:38:32 -040046 /**
47 * Whether to use to the database for session storage
48 *
49 * @var bool
50 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020051 public $sess_use_database = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020052
Timothy Warren68f09812012-04-27 10:38:32 -040053 /**
54 * Name of the database table in which to store sessions
55 *
56 * @var string
57 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020058 public $sess_table_name = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020059
Timothy Warren68f09812012-04-27 10:38:32 -040060 /**
61 * Length of time (in seconds) for sessions to expire
62 *
63 * @var int
64 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020065 public $sess_expiration = 7200;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020066
Timothy Warren68f09812012-04-27 10:38:32 -040067 /**
68 * Whether to kill session on close of browser window
69 *
70 * @var bool
71 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020072 public $sess_expire_on_close = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020073
Timothy Warren68f09812012-04-27 10:38:32 -040074 /**
75 * Whether to match session on ip address
76 *
77 * @var bool
78 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020079 public $sess_match_ip = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020080
Timothy Warren68f09812012-04-27 10:38:32 -040081 /**
82 * Whether to match session on user-agent
83 *
84 * @var bool
85 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020086 public $sess_match_useragent = TRUE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020087
Timothy Warren68f09812012-04-27 10:38:32 -040088 /**
89 * Name of session cookie
90 *
91 * @var string
92 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020093 public $sess_cookie_name = 'ci_session';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020094
Timothy Warren68f09812012-04-27 10:38:32 -040095 /**
96 * Session cookie prefix
97 *
98 * @var string
99 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200100 public $cookie_prefix = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200101
Timothy Warren68f09812012-04-27 10:38:32 -0400102 /**
103 * Session cookie path
104 *
105 * @var string
106 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200107 public $cookie_path = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200108
Timothy Warren68f09812012-04-27 10:38:32 -0400109 /**
110 * Session cookie domain
111 *
112 * @var string
113 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200114 public $cookie_domain = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200115
Timothy Warren68f09812012-04-27 10:38:32 -0400116 /**
117 * Whether to set the cookie only on HTTPS connections
118 *
119 * @var bool
120 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200121 public $cookie_secure = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200122
Timothy Warren68f09812012-04-27 10:38:32 -0400123 /**
124 * Whether cookie should be allowed only to be sent by the server
125 *
126 * @var bool
127 */
freewil4ad0fd82012-03-13 22:37:42 -0400128 public $cookie_httponly = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200129
Timothy Warren68f09812012-04-27 10:38:32 -0400130 /**
131 * Interval at which to update session
132 *
133 * @var int
134 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200135 public $sess_time_to_update = 300;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200136
Timothy Warren68f09812012-04-27 10:38:32 -0400137 /**
138 * Key with which to encrypt the session cookie
139 *
140 * @var string
141 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200142 public $encryption_key = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200143
Timothy Warren68f09812012-04-27 10:38:32 -0400144 /**
145 * String to indicate flash data cookies
146 *
147 * @var string
148 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200149 public $flashdata_key = 'flash';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200150
Timothy Warren68f09812012-04-27 10:38:32 -0400151 /**
152 * Function to use to get the current time
153 *
154 * @var string
155 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200156 public $time_reference = 'time';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200157
Timothy Warren68f09812012-04-27 10:38:32 -0400158 /**
159 * Probablity level of garbage collection of old sessions
160 *
161 * @var int
162 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200163 public $gc_probability = 5;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200164
Timothy Warren68f09812012-04-27 10:38:32 -0400165 /**
166 * Session data
167 *
168 * @var array
169 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200170 public $userdata = array();
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200171
Timothy Warren68f09812012-04-27 10:38:32 -0400172 /**
173 * Reference to CodeIgniter instance
174 *
175 * @var object
176 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200177 public $CI;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200178
Timothy Warren68f09812012-04-27 10:38:32 -0400179 /**
180 * Current time
181 *
182 * @var int
183 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200184 public $now;
Derek Allard2067d1a2008-11-13 22:59:24 +0000185
186 /**
187 * Session Constructor
188 *
189 * The constructor runs the session routines automatically
190 * whenever the class is instantiated.
Timothy Warren68f09812012-04-27 10:38:32 -0400191 *
192 * @param array
Andrey Andreev56454792012-05-17 14:32:19 +0300193 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 */
Greg Akera9263282010-11-10 15:26:43 -0600195 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200197 log_message('debug', 'Session Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000198
199 // Set the super object to a local variable for use throughout the class
200 $this->CI =& get_instance();
201
202 // Set all the session preferences, which can either be set
203 // manually via the $params array above or via the config file
freewil4ad0fd82012-03-13 22:37:42 -0400204 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', 'cookie_httponly', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 $this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
207 }
208
Derek Jones5485db52010-08-30 21:31:08 -0500209 if ($this->encryption_key == '')
210 {
211 show_error('In order to use the Session class you are required to set an encryption key in your config file.');
212 }
213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // Load the string helper so we can use the strip_slashes() function
215 $this->CI->load->helper('string');
216
217 // Do we need encryption? If so, load the encryption class
218 if ($this->sess_encrypt_cookie == TRUE)
219 {
220 $this->CI->load->library('encrypt');
221 }
222
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200223 // Are we using a database? If so, load it
Andrey Andreevf8868182012-01-20 13:14:53 +0200224 if ($this->sess_use_database === TRUE && $this->sess_table_name != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
226 $this->CI->load->database();
227 }
228
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200229 // Set the "now" time. Can either be GMT or server time, based on the
230 // config prefs. We use this to set the "last activity" time
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $this->now = $this->_get_time();
232
233 // Set the session length. If the session expiration is
234 // set to zero we'll set the expiration two years from now.
235 if ($this->sess_expiration == 0)
236 {
237 $this->sess_expiration = (60*60*24*365*2);
238 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // Set the cookie name
241 $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
242
243 // Run the Session routine. If a session doesn't exist we'll
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200244 // create a new one. If it does, we'll update it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 if ( ! $this->sess_read())
246 {
247 $this->sess_create();
248 }
249 else
250 {
251 $this->sess_update();
252 }
253
254 // Delete 'old' flashdata (from last request)
Barry Mienydd671972010-10-04 16:33:58 +0200255 $this->_flashdata_sweep();
Derek Allard2067d1a2008-11-13 22:59:24 +0000256
257 // Mark all new flashdata as old (data will be deleted before next request)
Barry Mienydd671972010-10-04 16:33:58 +0200258 $this->_flashdata_mark();
Derek Allard2067d1a2008-11-13 22:59:24 +0000259
260 // Delete expired sessions if necessary
261 $this->_sess_gc();
262
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200263 log_message('debug', 'Session routines successfully run');
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265
266 // --------------------------------------------------------------------
267
268 /**
269 * Fetch the current session data if it exists
270 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 * @return bool
272 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200273 public function sess_read()
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
275 // Fetch the cookie
276 $session = $this->CI->input->cookie($this->sess_cookie_name);
277
Derek Jones4b9c6292011-07-01 17:40:48 -0500278 // No cookie? Goodbye cruel world!...
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 if ($session === FALSE)
280 {
281 log_message('debug', 'A session cookie was not found.');
282 return FALSE;
283 }
284
285 // Decrypt the cookie data
286 if ($this->sess_encrypt_cookie == TRUE)
287 {
288 $session = $this->CI->encrypt->decode($session);
289 }
290 else
291 {
292 // encryption was not used, so we need to check the md5 hash
293 $hash = substr($session, strlen($session)-32); // get last 32 chars
294 $session = substr($session, 0, strlen($session)-32);
295
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200296 // Does the md5 hash match? This is to prevent manipulation of session data in userspace
Derek Jones4b9c6292011-07-01 17:40:48 -0500297 if ($hash !== md5($session.$this->encryption_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
299 log_message('error', 'The session cookie data did not match what was expected. This could be a possible hacking attempt.');
300 $this->sess_destroy();
301 return FALSE;
302 }
303 }
304
305 // Unserialize the session array
306 $session = $this->_unserialize($session);
307
308 // Is the session data we unserialized an array with the correct format?
Andrey Andreev7e087f52012-01-20 11:46:27 +0200309 if ( ! is_array($session) OR ! isset($session['session_id'], $session['ip_address'], $session['user_agent'], $session['last_activity']))
310 {
311 $this->sess_destroy();
312 return FALSE;
313 }
314
315 // Is the session current?
316 if (($session['last_activity'] + $this->sess_expiration) < $this->now)
317 {
318 $this->sess_destroy();
319 return FALSE;
320 }
321
322 // Does the IP match?
323 if ($this->sess_match_ip == TRUE && $session['ip_address'] !== $this->CI->input->ip_address())
324 {
325 $this->sess_destroy();
326 return FALSE;
327 }
328
329 // Does the User Agent Match?
330 if ($this->sess_match_useragent == TRUE && trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $this->sess_destroy();
333 return FALSE;
334 }
335
336 // Is there a corresponding session in the DB?
337 if ($this->sess_use_database === TRUE)
338 {
339 $this->CI->db->where('session_id', $session['session_id']);
340
341 if ($this->sess_match_ip == TRUE)
342 {
343 $this->CI->db->where('ip_address', $session['ip_address']);
344 }
345
346 if ($this->sess_match_useragent == TRUE)
347 {
348 $this->CI->db->where('user_agent', $session['user_agent']);
349 }
350
Timothy Warrenf1421922012-03-02 11:51:42 -0500351 $query = $this->CI->db->limit(1)->get($this->sess_table_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200353 // No result? Kill it!
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200354 if ($query->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 $this->sess_destroy();
357 return FALSE;
358 }
359
Derek Jones4b9c6292011-07-01 17:40:48 -0500360 // Is there custom data? If so, add it to the main session array
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 $row = $query->row();
Andrey Andreevf8868182012-01-20 13:14:53 +0200362 if (isset($row->user_data) && $row->user_data != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 $custom_data = $this->_unserialize($row->user_data);
365
366 if (is_array($custom_data))
367 {
368 foreach ($custom_data as $key => $val)
369 {
370 $session[$key] = $val;
371 }
372 }
373 }
374 }
375
376 // Session is valid!
377 $this->userdata = $session;
378 unset($session);
379
380 return TRUE;
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Write the session data
387 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @return void
389 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200390 public function sess_write()
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500392 // Are we saving custom data to the DB? If not, all we do is update the cookie
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 if ($this->sess_use_database === FALSE)
394 {
395 $this->_set_cookie();
396 return;
397 }
398
399 // set the custom userdata, the session data we will set in a second
400 $custom_userdata = $this->userdata;
401 $cookie_userdata = array();
402
403 // Before continuing, we need to determine if there is any custom data to deal with.
404 // Let's determine this by removing the default indexes to see if there's anything left in the array
405 // and set the session data while we're at it
406 foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
407 {
408 unset($custom_userdata[$val]);
409 $cookie_userdata[$val] = $this->userdata[$val];
410 }
411
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200412 // Did we find any custom data? If not, we turn the empty array into a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // since there's no reason to serialize and store an empty array in the DB
414 if (count($custom_userdata) === 0)
415 {
416 $custom_userdata = '';
417 }
418 else
419 {
420 // Serialize the custom data array so we can store it
421 $custom_userdata = $this->_serialize($custom_userdata);
422 }
423
424 // Run the update query
425 $this->CI->db->where('session_id', $this->userdata['session_id']);
426 $this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
427
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200428 // Write the cookie. Notice that we manually pass the cookie data array to the
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 // _set_cookie() function. Normally that function will store $this->userdata, but
430 // in this case that array contains custom data, which we do not want in the cookie.
431 $this->_set_cookie($cookie_userdata);
432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * Create a new session
438 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @return void
440 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200441 public function sess_create()
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 $sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200444 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
446 $sessid .= mt_rand(0, mt_getrandmax());
447 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200448 while (strlen($sessid) < 32);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449
450 // To make the session ID even more secure we'll combine it with the user's IP
451 $sessid .= $this->CI->input->ip_address();
452
453 $this->userdata = array(
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200454 'session_id' => md5(uniqid($sessid, TRUE)),
455 'ip_address' => $this->CI->input->ip_address(),
456 'user_agent' => substr($this->CI->input->user_agent(), 0, 120),
457 'last_activity' => $this->now,
458 'user_data' => ''
459 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000460
461 // Save the data to the DB if needed
462 if ($this->sess_use_database === TRUE)
463 {
464 $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
465 }
466
467 // Write the cookie
468 $this->_set_cookie();
469 }
470
471 // --------------------------------------------------------------------
472
473 /**
474 * Update an existing session
475 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @return void
477 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200478 public function sess_update()
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
480 // We only update the session every five minutes by default
Andrey Andreev9c622f32012-01-19 14:12:54 +0200481 if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 return;
484 }
485
Andrey Andreev9c622f32012-01-19 14:12:54 +0200486 // _set_cookie() will handle this for us if we aren't using database sessions
487 // by pushing all userdata to the cookie.
488 $cookie_data = NULL;
489
490 /* Changing the session ID during an AJAX call causes problems,
491 * so we'll only update our last_activity
492 */
493 if ($this->CI->input->is_ajax_request())
494 {
495 $this->userdata['last_activity'] = $this->now;
496
497 // Update the session ID and last_activity field in the DB if needed
498 if ($this->sess_use_database === TRUE)
499 {
500 // set cookie explicitly to only have our session data
501 $cookie_data = array();
502 foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
503 {
504 $cookie_data[$val] = $this->userdata[$val];
505 }
506
507 $this->CI->db->query($this->CI->db->update_string($this->sess_table_name,
508 array('last_activity' => $this->userdata['last_activity']),
509 array('session_id' => $this->userdata['session_id'])));
510 }
511
512 return $this->_set_cookie($cookie_data);
513 }
514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 // Save the old session id so we know which record to
516 // update in the database if we need it
517 $old_sessid = $this->userdata['session_id'];
518 $new_sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200519 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
521 $new_sessid .= mt_rand(0, mt_getrandmax());
522 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200523 while (strlen($new_sessid) < 32);
Derek Allard2067d1a2008-11-13 22:59:24 +0000524
525 // To make the session ID even more secure we'll combine it with the user's IP
526 $new_sessid .= $this->CI->input->ip_address();
527
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200528 // Turn it into a hash and update the session data array
529 $this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 $this->userdata['last_activity'] = $this->now;
531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 // Update the session ID and last_activity field in the DB if needed
533 if ($this->sess_use_database === TRUE)
534 {
535 // set cookie explicitly to only have our session data
536 $cookie_data = array();
537 foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
538 {
539 $cookie_data[$val] = $this->userdata[$val];
540 }
541
542 $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)));
543 }
544
545 // Write the cookie
546 $this->_set_cookie($cookie_data);
547 }
548
549 // --------------------------------------------------------------------
550
551 /**
552 * Destroy the current session
553 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @return void
555 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200556 public function sess_destroy()
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
558 // Kill the session DB row
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200559 if ($this->sess_use_database === TRUE && isset($this->userdata['session_id']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
561 $this->CI->db->where('session_id', $this->userdata['session_id']);
562 $this->CI->db->delete($this->sess_table_name);
563 }
564
565 // Kill the cookie
566 setcookie(
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200567 $this->sess_cookie_name,
568 addslashes(serialize(array())),
569 ($this->now - 31500000),
570 $this->cookie_path,
571 $this->cookie_domain,
572 0
573 );
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200574
575 // Kill session data
576 $this->userdata = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 }
578
579 // --------------------------------------------------------------------
580
581 /**
582 * Fetch a specific item from the session array
583 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @param string
585 * @return string
586 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200587 public function userdata($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
Andrey Andreev6b831232012-03-06 11:16:57 +0200589 return isset($this->userdata[$item]) ? $this->userdata[$item] : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 }
591
592 // --------------------------------------------------------------------
593
594 /**
595 * Fetch all session data
596 *
Greg Aker34033662011-04-18 11:18:09 -0500597 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200599 public function all_userdata()
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Greg Aker34033662011-04-18 11:18:09 -0500601 return $this->userdata;
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
Andrey Andreev38d0e932012-04-03 19:27:45 +0300603
Mike Funkc15e17c2012-02-23 14:56:18 -0500604 // --------------------------------------------------------------------------
Andrey Andreev38d0e932012-04-03 19:27:45 +0300605
Mike Funkc15e17c2012-02-23 14:56:18 -0500606 /**
607 * Fetch all flashdata
Andrey Andreev38d0e932012-04-03 19:27:45 +0300608 *
Mike Funkc91a66c2012-02-28 13:46:00 -0500609 * @return array
Mike Funkc15e17c2012-02-23 14:56:18 -0500610 */
611 public function all_flashdata()
612 {
613 $out = array();
Andrey Andreev38d0e932012-04-03 19:27:45 +0300614
Mike Funkc15e17c2012-02-23 14:56:18 -0500615 // loop through all userdata
616 foreach ($this->all_userdata() as $key => $val)
Andrey Andreev38d0e932012-04-03 19:27:45 +0300617 {
Mike Funkc15e17c2012-02-23 14:56:18 -0500618 // if it contains flashdata, add it
619 if (strpos($key, 'flash:old:') !== FALSE)
620 {
621 $out[$key] = $val;
622 }
623 }
624 return $out;
625 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000626
627 // --------------------------------------------------------------------
628
629 /**
630 * Add or change data in the "userdata" array
631 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 * @param mixed
633 * @param string
634 * @return void
635 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200636 public function set_userdata($newdata = array(), $newval = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
638 if (is_string($newdata))
639 {
640 $newdata = array($newdata => $newval);
641 }
642
643 if (count($newdata) > 0)
644 {
645 foreach ($newdata as $key => $val)
646 {
647 $this->userdata[$key] = $val;
648 }
649 }
650
651 $this->sess_write();
652 }
653
654 // --------------------------------------------------------------------
655
656 /**
657 * Delete a session variable from the "userdata" array
658 *
Timothy Warren68f09812012-04-27 10:38:32 -0400659 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 * @return void
661 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200662 public function unset_userdata($newdata = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
664 if (is_string($newdata))
665 {
666 $newdata = array($newdata => '');
667 }
668
669 if (count($newdata) > 0)
670 {
671 foreach ($newdata as $key => $val)
672 {
673 unset($this->userdata[$key]);
674 }
675 }
676
677 $this->sess_write();
678 }
679
680 // ------------------------------------------------------------------------
681
682 /**
683 * Add or change flashdata, only available
684 * until the next request
685 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * @param mixed
687 * @param string
688 * @return void
689 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200690 public function set_flashdata($newdata = array(), $newval = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
692 if (is_string($newdata))
693 {
694 $newdata = array($newdata => $newval);
695 }
696
697 if (count($newdata) > 0)
698 {
699 foreach ($newdata as $key => $val)
700 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200701 $this->set_userdata($this->flashdata_key.':new:'.$key, $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
703 }
704 }
705
706 // ------------------------------------------------------------------------
707
708 /**
709 * Keeps existing flashdata available to next request.
710 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 * @param string
712 * @return void
713 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200714 public function keep_flashdata($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 {
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200716 // 'old' flashdata gets removed. Here we mark all
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 // flashdata as 'new' to preserve it from _flashdata_sweep()
718 // Note the function will return FALSE if the $key
719 // provided cannot be found
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200720 $value = $this->userdata($this->flashdata_key.':old:'.$key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000721
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200722 $this->set_userdata($this->flashdata_key.':new:'.$key, $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
724
725 // ------------------------------------------------------------------------
726
727 /**
728 * Fetch a specific flashdata item from the session array
729 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 * @param string
731 * @return string
732 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200733 public function flashdata($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200735 return $this->userdata($this->flashdata_key.':old:'.$key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
737
738 // ------------------------------------------------------------------------
739
740 /**
741 * Identifies flashdata as 'old' for removal
742 * when _flashdata_sweep() runs.
743 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 * @return void
745 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200746 protected function _flashdata_mark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 {
748 $userdata = $this->all_userdata();
749 foreach ($userdata as $name => $value)
750 {
751 $parts = explode(':new:', $name);
752 if (is_array($parts) && count($parts) === 2)
753 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200754 $this->set_userdata($this->flashdata_key.':old:'.$parts[1], $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 $this->unset_userdata($name);
756 }
757 }
758 }
759
760 // ------------------------------------------------------------------------
761
762 /**
763 * Removes all flashdata marked as 'old'
764 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 * @return void
766 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200767 protected function _flashdata_sweep()
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
769 $userdata = $this->all_userdata();
770 foreach ($userdata as $key => $value)
771 {
772 if (strpos($key, ':old:'))
773 {
774 $this->unset_userdata($key);
775 }
776 }
777
778 }
779
780 // --------------------------------------------------------------------
781
782 /**
783 * Get the "now" time
784 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 * @return string
786 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200787 protected function _get_time()
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200789 return (strtolower($this->time_reference) === 'gmt')
790 ? mktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y'))
791 : time();
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 }
793
794 // --------------------------------------------------------------------
795
796 /**
797 * Write the session cookie
798 *
Timothy Warren68f09812012-04-27 10:38:32 -0400799 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 * @return void
801 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200802 protected function _set_cookie($cookie_data = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
804 if (is_null($cookie_data))
805 {
806 $cookie_data = $this->userdata;
807 }
808
809 // Serialize the userdata for the cookie
810 $cookie_data = $this->_serialize($cookie_data);
811
812 if ($this->sess_encrypt_cookie == TRUE)
813 {
814 $cookie_data = $this->CI->encrypt->encode($cookie_data);
815 }
816 else
817 {
818 // if encryption is not used, we provide an md5 hash to prevent userside tampering
819 $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
820 }
Barry Mienydd671972010-10-04 16:33:58 +0200821
Derek Joneseaa71ba2010-09-02 10:32:07 -0500822 $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 // Set the cookie
825 setcookie(
freewil4ad0fd82012-03-13 22:37:42 -0400826 $this->sess_cookie_name,
827 $cookie_data,
828 $expire,
829 $this->cookie_path,
830 $this->cookie_domain,
831 $this->cookie_secure,
832 $this->cookie_httponly
833 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 }
835
836 // --------------------------------------------------------------------
837
838 /**
839 * Serialize an array
840 *
841 * This function first converts any slashes found in the array to a temporary
842 * marker, so when it gets unserialized the slashes will be preserved
843 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 * @param array
845 * @return string
846 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200847 protected function _serialize($data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 {
849 if (is_array($data))
850 {
Chris Muench95933492011-10-16 14:14:04 -0400851 array_walk_recursive($data, array(&$this, '_escape_slashes'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200853 elseif (is_string($data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200855 $data = str_replace('\\', '{{slash}}', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 return serialize($data);
858 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200859
Chris Muench95933492011-10-16 14:14:04 -0400860 /**
861 * Escape slashes
862 *
863 * This function converts any slashes found into a temporary marker
864 *
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200865 * @param string
866 * @param string
867 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400868 */
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200869 protected function _escape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400870 {
871 if (is_string($val))
872 {
873 $val = str_replace('\\', '{{slash}}', $val);
874 }
875 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000876
877 // --------------------------------------------------------------------
878
879 /**
880 * Unserialize
881 *
882 * This function unserializes a data string, then converts any
883 * temporary slash markers back to actual slashes
884 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * @param array
886 * @return string
887 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200888 protected function _unserialize($data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
Andrey Andreev6b831232012-03-06 11:16:57 +0200890 $data = @unserialize(strip_slashes(trim($data)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000891
892 if (is_array($data))
893 {
Chris Muench95933492011-10-16 14:14:04 -0400894 array_walk_recursive($data, array(&$this, '_unescape_slashes'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 return $data;
896 }
897
Andrey Andreev6b831232012-03-06 11:16:57 +0200898 return is_string($data) ? str_replace('{{slash}}', '\\', $data) : $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200900
Andrey Andreev6b831232012-03-06 11:16:57 +0200901 // --------------------------------------------------------------------
902
Chris Muench95933492011-10-16 14:14:04 -0400903 /**
904 * Unescape slashes
905 *
906 * This function converts any slash markers back into actual slashes
907 *
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200908 * @param string
909 * @param string
910 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400911 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200912 protected function _unescape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400913 {
Chris Muench3e414f92011-10-16 23:03:55 -0400914 if (is_string($val))
915 {
916 $val= str_replace('{{slash}}', '\\', $val);
917 }
Chris Muench95933492011-10-16 14:14:04 -0400918 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000919
920 // --------------------------------------------------------------------
921
922 /**
923 * Garbage collection
924 *
925 * This deletes expired session rows from database
926 * if the probability percentage is met
927 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 * @return void
929 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200930 protected function _sess_gc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 if ($this->sess_use_database != TRUE)
933 {
934 return;
935 }
936
937 srand(time());
938 if ((rand() % 100) < $this->gc_probability)
939 {
940 $expire = $this->now - $this->sess_expiration;
941
Andrey Andreev6b831232012-03-06 11:16:57 +0200942 $this->CI->db->where('last_activity < '.$expire);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 $this->CI->db->delete($this->sess_table_name);
944
945 log_message('debug', 'Session garbage collection performed.');
946 }
947 }
948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949}
Derek Allard2067d1a2008-11-13 22:59:24 +0000950
951/* End of file Session.php */
Andrey Andreev38d0e932012-04-03 19:27:45 +0300952/* Location: ./system/libraries/Session.php */