blob: df3282cee43e72bfcb169f811a233491195cd4cb [file] [log] [blame]
Andrey Andreev57ffbbb2011-12-25 04:48:47 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Darren Hillc4e266b2011-08-30 15:40:27 -04002/**
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
dchill42c5079de2012-07-23 10:53:47 -040012 * 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.
Darren Hillc4e266b2011-08-30 15:40:27 -040018 *
19 * @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)
Darren Hillc4e266b2011-08-30 15:40:27 -040023 * @link http://codeigniter.com
Derek Allard2067d1a2008-11-13 22:59:24 +000024 * @since Version 1.0
Darren Hillc4e266b2011-08-30 15:40:27 -040025 * @filesource
26 */
27
Darren Hillc4e266b2011-08-30 15:40:27 -040028/**
29 * Cookie-based session management driver
30 *
dchill423cecd822012-08-28 21:37:27 -040031 * This is the classic CI_Session functionality, as written by EllisLab, abstracted out to a driver.
Darren Hillc4e266b2011-08-30 15:40:27 -040032 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Sessions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/sessions.html
Darren Hillc4e266b2011-08-30 15:40:27 -040038 */
Darren Hill5073a372011-08-31 13:54:19 -040039class CI_Session_cookie extends CI_Session_driver {
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020040 /**
Timothy Warren68f09812012-04-27 10:38:32 -040041 * Whether to encrypt the session cookie
42 *
43 * @var bool
44 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020045 public $sess_encrypt_cookie = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020046
Timothy Warren68f09812012-04-27 10:38:32 -040047 /**
48 * Whether to use to the database for session storage
49 *
50 * @var bool
51 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020052 public $sess_use_database = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020053
Timothy Warren68f09812012-04-27 10:38:32 -040054 /**
55 * Name of the database table in which to store sessions
56 *
57 * @var string
58 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020059 public $sess_table_name = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020060
Timothy Warren68f09812012-04-27 10:38:32 -040061 /**
62 * Length of time (in seconds) for sessions to expire
63 *
64 * @var int
65 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020066 public $sess_expiration = 7200;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020067
Timothy Warren68f09812012-04-27 10:38:32 -040068 /**
69 * Whether to kill session on close of browser window
70 *
71 * @var bool
72 */
dchill4226429202012-07-31 10:55:07 -040073 public $sess_expire_on_close = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020074
Timothy Warren68f09812012-04-27 10:38:32 -040075 /**
76 * Whether to match session on ip address
77 *
78 * @var bool
79 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020080 public $sess_match_ip = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020081
Timothy Warren68f09812012-04-27 10:38:32 -040082 /**
83 * Whether to match session on user-agent
84 *
85 * @var bool
86 */
dchill4226429202012-07-31 10:55:07 -040087 public $sess_match_useragent = TRUE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020088
Timothy Warren68f09812012-04-27 10:38:32 -040089 /**
90 * Name of session cookie
91 *
92 * @var string
93 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020094 public $sess_cookie_name = 'ci_session';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020095
Timothy Warren68f09812012-04-27 10:38:32 -040096 /**
97 * Session cookie prefix
98 *
99 * @var string
100 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200101 public $cookie_prefix = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200102
Timothy Warren68f09812012-04-27 10:38:32 -0400103 /**
104 * Session cookie path
105 *
106 * @var string
107 */
dchill4226429202012-07-31 10:55:07 -0400108 public $cookie_path = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200109
Timothy Warren68f09812012-04-27 10:38:32 -0400110 /**
111 * Session cookie domain
112 *
113 * @var string
114 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200115 public $cookie_domain = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200116
Timothy Warren68f09812012-04-27 10:38:32 -0400117 /**
118 * Whether to set the cookie only on HTTPS connections
119 *
120 * @var bool
121 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200122 public $cookie_secure = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200123
Timothy Warren68f09812012-04-27 10:38:32 -0400124 /**
125 * Whether cookie should be allowed only to be sent by the server
126 *
127 * @var bool
128 */
freewil4ad0fd82012-03-13 22:37:42 -0400129 public $cookie_httponly = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200130
Timothy Warren68f09812012-04-27 10:38:32 -0400131 /**
132 * Interval at which to update session
133 *
134 * @var int
135 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200136 public $sess_time_to_update = 300;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200137
Timothy Warren68f09812012-04-27 10:38:32 -0400138 /**
139 * Key with which to encrypt the session cookie
140 *
141 * @var string
142 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200143 public $encryption_key = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200144
Timothy Warren68f09812012-04-27 10:38:32 -0400145 /**
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300146 * Timezone to use for the current time
Timothy Warren68f09812012-04-27 10:38:32 -0400147 *
148 * @var string
149 */
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300150 public $time_reference = 'local';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200151
Timothy Warren68f09812012-04-27 10:38:32 -0400152 /**
153 * Session data
154 *
155 * @var array
156 */
dchill4226429202012-07-31 10:55:07 -0400157 public $userdata = array();
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200158
Timothy Warren68f09812012-04-27 10:38:32 -0400159 /**
160 * Reference to CodeIgniter instance
161 *
162 * @var object
163 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200164 public $CI;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200165
Timothy Warren68f09812012-04-27 10:38:32 -0400166 /**
167 * Current time
168 *
169 * @var int
170 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200171 public $now;
Darren Hillc4e266b2011-08-30 15:40:27 -0400172
173 /**
dchill423cecd822012-08-28 21:37:27 -0400174 * Default userdata keys
175 *
176 * @var array
177 */
178 protected $defaults = array(
179 'session_id',
180 'ip_address',
181 'user_agent',
182 'last_activity'
183 );
184
185 /**
186 * Data needs DB update flag
187 *
188 * @var bool
189 */
190 protected $data_dirty = FALSE;
191
192 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400193 * Initialize session driver object
194 *
195 * @access protected
196 * @return void
197 */
198 protected function initialize()
199 {
200 // Set the super object to a local variable for use throughout the class
201 $this->CI =& get_instance();
202
203 // Set all the session preferences, which can either be set
dchill4242b77a92012-07-23 11:28:42 -0400204 // manually via the $params array or via the config file
dchill4226429202012-07-31 10:55:07 -0400205 $prefs = array(
206 'sess_encrypt_cookie',
207 'sess_use_database',
208 'sess_table_name',
209 'sess_expiration',
210 'sess_expire_on_close',
211 'sess_match_ip',
212 'sess_match_useragent',
213 'sess_cookie_name',
214 'cookie_path',
215 'cookie_domain',
216 'cookie_secure',
217 'cookie_httponly',
218 'sess_time_to_update',
219 'time_reference',
220 'cookie_prefix',
221 'encryption_key'
222 );
223 foreach ($prefs as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400224 {
dchill42c5872252012-07-30 14:53:11 -0400225 $this->$key = isset($this->_parent->params[$key]) ? $this->_parent->params[$key] :
dchill42c5079de2012-07-23 10:53:47 -0400226 $this->CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400227 }
228
Alex Bilbied261b1e2012-06-02 11:12:16 +0100229 if ($this->encryption_key === '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400230 {
dchill42c5872252012-07-30 14:53:11 -0400231 show_error('In order to use the Cookie Session driver you are required to set an encryption key '.
Darren Hillc4e266b2011-08-30 15:40:27 -0400232 'in your config file.');
233 }
234
235 // Load the string helper so we can use the strip_slashes() function
236 $this->CI->load->helper('string');
237
238 // Do we need encryption? If so, load the encryption class
Alex Bilbied261b1e2012-06-02 11:12:16 +0100239 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400240 {
241 $this->CI->load->library('encrypt');
242 }
243
dchill423cecd822012-08-28 21:37:27 -0400244 // Check for database
Alex Bilbied261b1e2012-06-02 11:12:16 +0100245 if ($this->sess_use_database === TRUE && $this->sess_table_name !== '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400246 {
dchill423cecd822012-08-28 21:37:27 -0400247 // Load database driver
Darren Hillc4e266b2011-08-30 15:40:27 -0400248 $this->CI->load->database();
dchill423cecd822012-08-28 21:37:27 -0400249
250 // Register shutdown function
251 register_shutdown_function(array($this, '_update_db'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400252 }
253
254 // Set the "now" time. Can either be GMT or server time, based on the config prefs.
255 // We use this to set the "last activity" time
256 $this->now = $this->_get_time();
257
258 // Set the session length. If the session expiration is
259 // set to zero we'll set the expiration two years from now.
Alex Bilbied261b1e2012-06-02 11:12:16 +0100260 if ($this->sess_expiration === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400261 {
262 $this->sess_expiration = (60*60*24*365*2);
263 }
264
265 // Set the cookie name
266 $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
267
268 // Run the Session routine. If a session doesn't exist we'll
269 // create a new one. If it does, we'll update it.
270 if ( ! $this->_sess_read())
271 {
272 $this->_sess_create();
273 }
274 else
275 {
276 $this->_sess_update();
277 }
278
279 // Delete expired sessions if necessary
280 $this->_sess_gc();
281 }
282
283 /**
dchill423cecd822012-08-28 21:37:27 -0400284 * Shutdown session driver object
285 *
286 * @return void
287 */
288 public function shutdown()
289 {
290 // Just update the DB
291 $this->_update_db();
292 }
293
294 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400295 * Write the session data
296 *
297 * @return void
298 */
299 public function sess_save()
300 {
dchill423cecd822012-08-28 21:37:27 -0400301 // Check for database
Darren Hillc4e266b2011-08-30 15:40:27 -0400302 if ($this->sess_use_database === FALSE)
303 {
dchill423cecd822012-08-28 21:37:27 -0400304 // Mark custom data as dirty so we know to update the DB
305 $this->data_dirty = TRUE;
Darren Hillc4e266b2011-08-30 15:40:27 -0400306 }
307
dchill423cecd822012-08-28 21:37:27 -0400308 // Write the cookie
309 $this->_set_cookie();
Darren Hillc4e266b2011-08-30 15:40:27 -0400310 }
311
312 /**
313 * Destroy the current session
314 *
315 * @return void
316 */
317 public function sess_destroy()
318 {
319 // Kill the session DB row
dchill42aee92652012-08-26 21:45:35 -0400320 if ($this->sess_use_database === TRUE && isset($this->userdata['session_id']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400321 {
dchill423cecd822012-08-28 21:37:27 -0400322 $this->CI->db->delete($this->sess_table_name, array('session_id' => $this->userdata['session_id']));
Darren Hillc4e266b2011-08-30 15:40:27 -0400323 }
324
325 // Kill the cookie
dchill42c5872252012-07-30 14:53:11 -0400326 $this->_setcookie($this->sess_cookie_name, addslashes(serialize(array())), ($this->now - 31500000),
Darren Hillc4e266b2011-08-30 15:40:27 -0400327 $this->cookie_path, $this->cookie_domain, 0);
dchill42c5079de2012-07-23 10:53:47 -0400328
329 // Kill session data
330 $this->userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -0400331 }
332
333 /**
334 * Regenerate the current session
335 *
336 * Regenerate the session id
337 *
338 * @param boolean Destroy session data flag (default: false)
339 * @return void
340 */
341 public function sess_regenerate($destroy = false)
342 {
343 // Check destroy flag
344 if ($destroy)
345 {
346 // Destroy old session and create new one
347 $this->sess_destroy();
348 $this->_sess_create();
349 }
350 else
351 {
352 // Just force an update to recreate the id
353 $this->_sess_update(true);
354 }
355 }
356
357 /**
358 * Get a reference to user data array
359 *
360 * @return array - Reference to userdata
361 */
362 public function &get_userdata()
363 {
364 // Return reference to array
365 return $this->userdata;
366 }
367
368 /**
369 * Fetch the current session data if it exists
370 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400371 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400372 * @return bool
373 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400374 protected function _sess_read()
Darren Hillc4e266b2011-08-30 15:40:27 -0400375 {
376 // Fetch the cookie
377 $session = $this->CI->input->cookie($this->sess_cookie_name);
378
379 // No cookie? Goodbye cruel world!...
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100380 if ($session === NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400381 {
382 log_message('debug', 'A session cookie was not found.');
383 return FALSE;
384 }
385
dchill423cecd822012-08-28 21:37:27 -0400386 // Check for encryption
Alex Bilbied261b1e2012-06-02 11:12:16 +0100387 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400388 {
dchill423cecd822012-08-28 21:37:27 -0400389 // Decrypt the cookie data
Darren Hillc4e266b2011-08-30 15:40:27 -0400390 $session = $this->CI->encrypt->decode($session);
391 }
392 else
393 {
dchill423cecd822012-08-28 21:37:27 -0400394 // Encryption was not used, so we need to check the md5 hash in the last 32 chars
395 $len = strlen($session)-32;
396 $hash = substr($session, $len);
397 $session = substr($session, 0, $len);
Darren Hillc4e266b2011-08-30 15:40:27 -0400398
399 // Does the md5 hash match? This is to prevent manipulation of session data in userspace
400 if ($hash !== md5($session.$this->encryption_key))
401 {
402 log_message('error', 'The session cookie data did not match what was expected. '.
403 'This could be a possible hacking attempt.');
404 $this->sess_destroy();
405 return FALSE;
406 }
407 }
408
409 // Unserialize the session array
410 $session = $this->_unserialize($session);
411
412 // Is the session data we unserialized an array with the correct format?
dchill42c5079de2012-07-23 10:53:47 -0400413 if ( ! is_array($session) || ! isset($session['session_id'], $session['ip_address'], $session['user_agent'],
414 $session['last_activity']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400415 {
416 $this->sess_destroy();
417 return FALSE;
418 }
419
420 // Is the session current?
Andrey Andreev7e087f52012-01-20 11:46:27 +0200421 if (($session['last_activity'] + $this->sess_expiration) < $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400422 {
423 $this->sess_destroy();
424 return FALSE;
425 }
426
Andrey Andreev7e087f52012-01-20 11:46:27 +0200427 // Does the IP match?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100428 if ($this->sess_match_ip === TRUE && $session['ip_address'] !== $this->CI->input->ip_address())
Darren Hillc4e266b2011-08-30 15:40:27 -0400429 {
430 $this->sess_destroy();
431 return FALSE;
432 }
433
434 // Does the User Agent Match?
dchill42c5079de2012-07-23 10:53:47 -0400435 if ($this->sess_match_useragent === TRUE &&
436 trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400437 {
438 $this->sess_destroy();
439 return FALSE;
440 }
441
442 // Is there a corresponding session in the DB?
443 if ($this->sess_use_database === TRUE)
444 {
445 $this->CI->db->where('session_id', $session['session_id']);
446
Alex Bilbied261b1e2012-06-02 11:12:16 +0100447 if ($this->sess_match_ip === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400448 {
449 $this->CI->db->where('ip_address', $session['ip_address']);
450 }
451
Alex Bilbied261b1e2012-06-02 11:12:16 +0100452 if ($this->sess_match_useragent === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400453 {
454 $this->CI->db->where('user_agent', $session['user_agent']);
455 }
456
Timothy Warrenf1421922012-03-02 11:51:42 -0500457 $query = $this->CI->db->limit(1)->get($this->sess_table_name);
Darren Hillc4e266b2011-08-30 15:40:27 -0400458
459 // No result? Kill it!
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200460 if ($query->num_rows() === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400461 {
462 $this->sess_destroy();
463 return FALSE;
464 }
465
466 // Is there custom data? If so, add it to the main session array
467 $row = $query->row();
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300468 if ( ! empty($row->user_data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400469 {
470 $custom_data = $this->_unserialize($row->user_data);
471
472 if (is_array($custom_data))
473 {
dchill423cecd822012-08-28 21:37:27 -0400474 $session = $session + $custom_data;
Darren Hillc4e266b2011-08-30 15:40:27 -0400475 }
476 }
477 }
478
479 // Session is valid!
480 $this->userdata = $session;
Darren Hillc4e266b2011-08-30 15:40:27 -0400481 return TRUE;
482 }
483
484 /**
485 * Create a new session
486 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400487 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400488 * @return void
489 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400490 protected function _sess_create()
Darren Hillc4e266b2011-08-30 15:40:27 -0400491 {
dchill423cecd822012-08-28 21:37:27 -0400492 // Initialize userdata
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 $this->userdata = array(
dchill423cecd822012-08-28 21:37:27 -0400494 'session_id' => $this->_make_sess_id(),
dchill42c5079de2012-07-23 10:53:47 -0400495 'ip_address' => $this->CI->input->ip_address(),
496 'user_agent' => substr($this->CI->input->user_agent(), 0, 120),
497 'last_activity' => $this->now,
dchill42c5079de2012-07-23 10:53:47 -0400498 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400499
dchill423cecd822012-08-28 21:37:27 -0400500 // Check for database
Darren Hillc4e266b2011-08-30 15:40:27 -0400501 if ($this->sess_use_database === TRUE)
502 {
dchill423cecd822012-08-28 21:37:27 -0400503 // Add empty user_data field and save the data to the DB
504 $this->CI->db->set('user_data', '')->insert($this->sess_table_name, $this->userdata);
Darren Hillc4e266b2011-08-30 15:40:27 -0400505 }
506
507 // Write the cookie
508 $this->_set_cookie();
509 }
510
511 /**
512 * Update an existing session
513 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400514 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400515 * @param boolean Force update flag (default: false)
516 * @return void
517 */
dchill42c5079de2012-07-23 10:53:47 -0400518 protected function _sess_update($force = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400519 {
520 // We only update the session every five minutes by default (unless forced)
dchill4277ee3fd2012-07-24 11:50:01 -0400521 if ( ! $force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400522 {
523 return;
524 }
525
dchill423cecd822012-08-28 21:37:27 -0400526 // Update last activity to now
527 $this->userdata['last_activity'] = $this->now;
Andrey Andreev9c622f32012-01-19 14:12:54 +0200528
dchill423cecd822012-08-28 21:37:27 -0400529 // Save the old session id so we know which DB record to update
530 $old_sessid = $this->userdata['session_id'];
531
532 // Changing the session ID during an AJAX call causes problems
533 if ( ! $this->CI->input->is_ajax_request())
Andrey Andreev9c622f32012-01-19 14:12:54 +0200534 {
dchill423cecd822012-08-28 21:37:27 -0400535 // Get new id
536 $this->userdata['session_id'] = $this->_make_sess_id();
Andrey Andreev9c622f32012-01-19 14:12:54 +0200537 }
538
dchill423cecd822012-08-28 21:37:27 -0400539 // Check for database
540 if ($this->sess_use_database === TRUE)
541 {
542 // Update the session ID and last_activity field in the DB
543 $this->CI->db->update($this->sess_table_name, array(
544 'last_activity' => $this->now,
545 'session_id' => $this->userdata['session_id']
546 ), array('session_id' => $old_sessid));
547 }
548
549 // Write the cookie
550 $this->_set_cookie();
551 }
552
553 /**
554 * Update database with current data
555 *
556 * This gets called from the shutdown function and also
557 * registered with PHP to run at the end of the request
558 * so it's guaranteed to update even when a fatal error
559 * occurs. The first call makes the update and clears the
560 * dirty flag so it won't happen twice.
561 */
562 public function _update_db()
563 {
564 // Check for database and dirty flag and unsaved
565 if ($this->sess_use_database === TRUE && $this->data_dirty === TRUE)
566 {
567 // Set up activity and data fields to be set
568 // If we don't find custom data, user_data will remain an empty string
569 $set = array(
570 'last_activity' => $this->userdata['last_activity'],
571 'user_data' => ''
572 );
573
574 // Get the custom userdata, leaving out the defaults
575 // (which get stored in the cookie)
576 $userdata = array_diff_key($this->userdata, $this->defaults);
577
578 // Did we find any custom data?
579 if ( ! empty($userdata))
580 {
581 // Serialize the custom data array so we can store it
582 $set['user_data'] = $this->_serialize($userdata);
583 }
584
585 // Run the update query
586 // Any time we change the session id, it gets updated immediately,
587 // so our where clause below is always safe
588 $this->CI->db->update($this->sess_table_name, $set, array('session_id' => $this->userdata['session_id']));
589
590 // Clear dirty flag to prevent double updates
591 $this->data_dirty = FALSE;
592
593 log_message('debug', 'CI_Session Data Saved To DB');
594 }
595 }
596
597 /**
598 * Generate a new session id
599 *
600 * @return string Hashed session id
601 */
602 protected function _make_sess_id()
603 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400604 $new_sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200605 do
Darren Hillc4e266b2011-08-30 15:40:27 -0400606 {
607 $new_sessid .= mt_rand(0, mt_getrandmax());
608 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200609 while (strlen($new_sessid) < 32);
Darren Hillc4e266b2011-08-30 15:40:27 -0400610
611 // To make the session ID even more secure we'll combine it with the user's IP
612 $new_sessid .= $this->CI->input->ip_address();
613
dchill423cecd822012-08-28 21:37:27 -0400614 // Turn it into a hash and return
615 return md5(uniqid($new_sessid, TRUE));
Darren Hillc4e266b2011-08-30 15:40:27 -0400616 }
617
618 /**
619 * Get the "now" time
620 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400621 * @access protected
dchill42c5079de2012-07-23 10:53:47 -0400622 * @return int Time
Darren Hillc4e266b2011-08-30 15:40:27 -0400623 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400624 protected function _get_time()
Darren Hillc4e266b2011-08-30 15:40:27 -0400625 {
dchill42c5079de2012-07-23 10:53:47 -0400626 if ($this->time_reference === 'local' || $this->time_reference === date_default_timezone_get())
Darren Hillc4e266b2011-08-30 15:40:27 -0400627 {
Iban Eguiafeb14da2012-06-12 16:09:36 +0200628 return time();
Darren Hillc4e266b2011-08-30 15:40:27 -0400629 }
630
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300631 $datetime = new DateTime('now', new DateTimeZone($this->time_reference));
Iban Eguiafeb14da2012-06-12 16:09:36 +0200632 sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
633
634 return mktime($hour, $minute, $second, $month, $day, $year);
Darren Hillc4e266b2011-08-30 15:40:27 -0400635 }
636
637 /**
638 * Write the session cookie
639 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400640 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400641 * @return void
642 */
dchill423cecd822012-08-28 21:37:27 -0400643 protected function _set_cookie()
Darren Hillc4e266b2011-08-30 15:40:27 -0400644 {
dchill423cecd822012-08-28 21:37:27 -0400645 // Get userdata (only defaults if database)
646 if ($this->sess_use_database === TRUE)
647 {
648 $cookie_data = array_intersect_key($this->userdata, $this->defaults);
649 }
650 else
Darren Hillc4e266b2011-08-30 15:40:27 -0400651 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 $cookie_data = $this->userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400653 }
654
655 // Serialize the userdata for the cookie
656 $cookie_data = $this->_serialize($cookie_data);
657
Alex Bilbied261b1e2012-06-02 11:12:16 +0100658 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400659 {
660 $cookie_data = $this->CI->encrypt->encode($cookie_data);
661 }
662 else
663 {
664 // if encryption is not used, we provide an md5 hash to prevent userside tampering
665 $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
666 }
667
668 $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
669
670 // Set the cookie
dchill42c5872252012-07-30 14:53:11 -0400671 $this->_setcookie($this->sess_cookie_name, $cookie_data, $expire, $this->cookie_path, $this->cookie_domain,
dchill42c5079de2012-07-23 10:53:47 -0400672 $this->cookie_secure, $this->cookie_httponly);
Darren Hillc4e266b2011-08-30 15:40:27 -0400673 }
674
675 /**
dchill42c5872252012-07-30 14:53:11 -0400676 * Set a cookie with the system
677 *
678 * This abstraction of the setcookie call allows overriding for unit testing
679 *
680 * @access protected
681 * @param string Cookie name
682 * @param string Cookie value
683 * @param int Expiration time
684 * @param string Cookie path
685 * @param string Cookie domain
686 * @param bool Secure connection flag
687 * @param bool HTTP protocol only flag
688 * @return void
689 */
690 protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = false,
691 $httponly = false)
692 {
693 // Set the cookie
694 setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
695 }
696
697 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400698 * Serialize an array
699 *
700 * This function first converts any slashes found in the array to a temporary
701 * marker, so when it gets unserialized the slashes will be preserved
702 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400703 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400704 * @param mixed Data to serialize
dchill42c5079de2012-07-23 10:53:47 -0400705 * @return string Serialized data
Darren Hillc4e266b2011-08-30 15:40:27 -0400706 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400707 protected function _serialize($data)
Darren Hillc4e266b2011-08-30 15:40:27 -0400708 {
709 if (is_array($data))
710 {
Chris Muench95933492011-10-16 14:14:04 -0400711 array_walk_recursive($data, array(&$this, '_escape_slashes'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400712 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200713 elseif (is_string($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400714 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200715 $data = str_replace('\\', '{{slash}}', $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400716 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400717 return serialize($data);
718 }
719
720 /**
Chris Muench95933492011-10-16 14:14:04 -0400721 * Escape slashes
722 *
723 * This function converts any slashes found into a temporary marker
724 *
dchill42c5079de2012-07-23 10:53:47 -0400725 * @access protected
726 * @param string Value
727 * @param string Key
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200728 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400729 */
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200730 protected function _escape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400731 {
732 if (is_string($val))
733 {
734 $val = str_replace('\\', '{{slash}}', $val);
735 }
736 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000737
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400739 * Unserialize
740 *
741 * This function unserializes a data string, then converts any
742 * temporary slash markers back to actual slashes
743 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400744 * @access protected
dchill42c5079de2012-07-23 10:53:47 -0400745 * @param mixed Data to unserialize
746 * @return mixed Unserialized data
Darren Hillc4e266b2011-08-30 15:40:27 -0400747 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400748 protected function _unserialize($data)
Darren Hillc4e266b2011-08-30 15:40:27 -0400749 {
Andrey Andreev6b831232012-03-06 11:16:57 +0200750 $data = @unserialize(strip_slashes(trim($data)));
Darren Hillc4e266b2011-08-30 15:40:27 -0400751
752 if (is_array($data))
753 {
Chris Muench95933492011-10-16 14:14:04 -0400754 array_walk_recursive($data, array(&$this, '_unescape_slashes'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400755 return $data;
756 }
757
Andrey Andreev6b831232012-03-06 11:16:57 +0200758 return is_string($data) ? str_replace('{{slash}}', '\\', $data) : $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200760
Chris Muench95933492011-10-16 14:14:04 -0400761 /**
762 * Unescape slashes
763 *
764 * This function converts any slash markers back into actual slashes
765 *
dchill42c5079de2012-07-23 10:53:47 -0400766 * @access protected
767 * @param string Value
768 * @param string Key
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200769 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400770 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200771 protected function _unescape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400772 {
Chris Muench3e414f92011-10-16 23:03:55 -0400773 if (is_string($val))
774 {
775 $val= str_replace('{{slash}}', '\\', $val);
776 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400777 }
778
779 /**
780 * Garbage collection
781 *
782 * This deletes expired session rows from database
783 * if the probability percentage is met
784 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400785 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400786 * @return void
787 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400788 protected function _sess_gc()
Darren Hillc4e266b2011-08-30 15:40:27 -0400789 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100790 if ($this->sess_use_database !== TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400791 {
792 return;
793 }
794
Christopher Guiney7a142862012-06-29 20:34:28 -0700795 $probability = ini_get('session.gc_probability');
796 $divisor = ini_get('session.gc_divisor');
797
Darren Hillc4e266b2011-08-30 15:40:27 -0400798 srand(time());
Christopher Guiney7a142862012-06-29 20:34:28 -0700799 if ((mt_rand(0, $divisor) / $divisor) < $probability)
Darren Hillc4e266b2011-08-30 15:40:27 -0400800 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 $expire = $this->now - $this->sess_expiration;
dchill423cecd822012-08-28 21:37:27 -0400802 $this->CI->db->delete($this->sess_table_name, 'last_activity < '.$expire);
Darren Hillc4e266b2011-08-30 15:40:27 -0400803
804 log_message('debug', 'Session garbage collection performed.');
805 }
806 }
807}
Darren Hillc4e266b2011-08-30 15:40:27 -0400808
809/* End of file Session_cookie.php */
Darren Hill5073a372011-08-31 13:54:19 -0400810/* Location: ./system/libraries/Session/drivers/Session_cookie.php */