blob: 51d94da4e8b91e1e5962cc3681ab50df5ad13391 [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 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030040
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020041 /**
Timothy Warren68f09812012-04-27 10:38:32 -040042 * Whether to encrypt the session cookie
43 *
44 * @var bool
45 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020046 public $sess_encrypt_cookie = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020047
Timothy Warren68f09812012-04-27 10:38:32 -040048 /**
49 * Whether to use to the database for session storage
50 *
51 * @var bool
52 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020053 public $sess_use_database = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020054
Timothy Warren68f09812012-04-27 10:38:32 -040055 /**
56 * Name of the database table in which to store sessions
57 *
58 * @var string
59 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020060 public $sess_table_name = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020061
Timothy Warren68f09812012-04-27 10:38:32 -040062 /**
63 * Length of time (in seconds) for sessions to expire
64 *
65 * @var int
66 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020067 public $sess_expiration = 7200;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020068
Timothy Warren68f09812012-04-27 10:38:32 -040069 /**
70 * Whether to kill session on close of browser window
71 *
72 * @var bool
73 */
dchill4226429202012-07-31 10:55:07 -040074 public $sess_expire_on_close = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020075
Timothy Warren68f09812012-04-27 10:38:32 -040076 /**
77 * Whether to match session on ip address
78 *
79 * @var bool
80 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020081 public $sess_match_ip = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020082
Timothy Warren68f09812012-04-27 10:38:32 -040083 /**
84 * Whether to match session on user-agent
85 *
86 * @var bool
87 */
dchill4226429202012-07-31 10:55:07 -040088 public $sess_match_useragent = TRUE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020089
Timothy Warren68f09812012-04-27 10:38:32 -040090 /**
91 * Name of session cookie
92 *
93 * @var string
94 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020095 public $sess_cookie_name = 'ci_session';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020096
Timothy Warren68f09812012-04-27 10:38:32 -040097 /**
98 * Session cookie prefix
99 *
100 * @var string
101 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200102 public $cookie_prefix = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200103
Timothy Warren68f09812012-04-27 10:38:32 -0400104 /**
105 * Session cookie path
106 *
107 * @var string
108 */
dchill4226429202012-07-31 10:55:07 -0400109 public $cookie_path = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200110
Timothy Warren68f09812012-04-27 10:38:32 -0400111 /**
112 * Session cookie domain
113 *
114 * @var string
115 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200116 public $cookie_domain = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200117
Timothy Warren68f09812012-04-27 10:38:32 -0400118 /**
119 * Whether to set the cookie only on HTTPS connections
120 *
121 * @var bool
122 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200123 public $cookie_secure = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200124
Timothy Warren68f09812012-04-27 10:38:32 -0400125 /**
126 * Whether cookie should be allowed only to be sent by the server
127 *
128 * @var bool
129 */
freewil4ad0fd82012-03-13 22:37:42 -0400130 public $cookie_httponly = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200131
Timothy Warren68f09812012-04-27 10:38:32 -0400132 /**
133 * Interval at which to update session
134 *
135 * @var int
136 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200137 public $sess_time_to_update = 300;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200138
Timothy Warren68f09812012-04-27 10:38:32 -0400139 /**
140 * Key with which to encrypt the session cookie
141 *
142 * @var string
143 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200144 public $encryption_key = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200145
Timothy Warren68f09812012-04-27 10:38:32 -0400146 /**
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300147 * Timezone to use for the current time
Timothy Warren68f09812012-04-27 10:38:32 -0400148 *
149 * @var string
150 */
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300151 public $time_reference = 'local';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200152
Timothy Warren68f09812012-04-27 10:38:32 -0400153 /**
154 * Session data
155 *
156 * @var array
157 */
dchill4226429202012-07-31 10:55:07 -0400158 public $userdata = array();
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200159
Timothy Warren68f09812012-04-27 10:38:32 -0400160 /**
Timothy Warren68f09812012-04-27 10:38:32 -0400161 * Current time
162 *
163 * @var int
164 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200165 public $now;
Darren Hillc4e266b2011-08-30 15:40:27 -0400166
167 /**
dchill423cecd822012-08-28 21:37:27 -0400168 * Default userdata keys
169 *
170 * @var array
171 */
172 protected $defaults = array(
dchill4288b636b2012-08-29 08:47:05 -0400173 'session_id' => NULL,
174 'ip_address' => NULL,
175 'user_agent' => NULL,
176 'last_activity' => NULL
dchill423cecd822012-08-28 21:37:27 -0400177 );
178
179 /**
180 * Data needs DB update flag
181 *
182 * @var bool
183 */
184 protected $data_dirty = FALSE;
185
186 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400187 * Initialize session driver object
188 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400189 * @return void
190 */
191 protected function initialize()
192 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400193 // Set all the session preferences, which can either be set
dchill4242b77a92012-07-23 11:28:42 -0400194 // manually via the $params array or via the config file
dchill4226429202012-07-31 10:55:07 -0400195 $prefs = array(
196 'sess_encrypt_cookie',
197 'sess_use_database',
198 'sess_table_name',
199 'sess_expiration',
200 'sess_expire_on_close',
201 'sess_match_ip',
202 'sess_match_useragent',
203 'sess_cookie_name',
204 'cookie_path',
205 'cookie_domain',
206 'cookie_secure',
207 'cookie_httponly',
208 'sess_time_to_update',
209 'time_reference',
210 'cookie_prefix',
211 'encryption_key'
212 );
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300213
dchill4226429202012-07-31 10:55:07 -0400214 foreach ($prefs as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400215 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300216 $this->$key = isset($this->_parent->params[$key])
217 ? $this->_parent->params[$key]
218 : $this->CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400219 }
220
Alex Bilbied261b1e2012-06-02 11:12:16 +0100221 if ($this->encryption_key === '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400222 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300223 show_error('In order to use the Cookie Session driver you are required to set an encryption key in your config file.');
Darren Hillc4e266b2011-08-30 15:40:27 -0400224 }
225
226 // Load the string helper so we can use the strip_slashes() function
227 $this->CI->load->helper('string');
228
229 // Do we need encryption? If so, load the encryption class
Alex Bilbied261b1e2012-06-02 11:12:16 +0100230 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400231 {
232 $this->CI->load->library('encrypt');
233 }
234
dchill423cecd822012-08-28 21:37:27 -0400235 // Check for database
Alex Bilbied261b1e2012-06-02 11:12:16 +0100236 if ($this->sess_use_database === TRUE && $this->sess_table_name !== '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400237 {
dchill423cecd822012-08-28 21:37:27 -0400238 // Load database driver
Darren Hillc4e266b2011-08-30 15:40:27 -0400239 $this->CI->load->database();
dchill423cecd822012-08-28 21:37:27 -0400240
241 // Register shutdown function
242 register_shutdown_function(array($this, '_update_db'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400243 }
244
245 // Set the "now" time. Can either be GMT or server time, based on the config prefs.
246 // We use this to set the "last activity" time
247 $this->now = $this->_get_time();
248
249 // Set the session length. If the session expiration is
250 // set to zero we'll set the expiration two years from now.
Alex Bilbied261b1e2012-06-02 11:12:16 +0100251 if ($this->sess_expiration === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400252 {
253 $this->sess_expiration = (60*60*24*365*2);
254 }
255
256 // Set the cookie name
257 $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
258
259 // Run the Session routine. If a session doesn't exist we'll
260 // create a new one. If it does, we'll update it.
261 if ( ! $this->_sess_read())
262 {
263 $this->_sess_create();
264 }
265 else
266 {
267 $this->_sess_update();
268 }
269
270 // Delete expired sessions if necessary
271 $this->_sess_gc();
272 }
273
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300274 // ------------------------------------------------------------------------
275
Darren Hillc4e266b2011-08-30 15:40:27 -0400276 /**
277 * Write the session data
278 *
279 * @return void
280 */
281 public function sess_save()
282 {
dchill423cecd822012-08-28 21:37:27 -0400283 // Check for database
dchill4288b636b2012-08-29 08:47:05 -0400284 if ($this->sess_use_database === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400285 {
dchill423cecd822012-08-28 21:37:27 -0400286 // Mark custom data as dirty so we know to update the DB
287 $this->data_dirty = TRUE;
Darren Hillc4e266b2011-08-30 15:40:27 -0400288 }
289
dchill423cecd822012-08-28 21:37:27 -0400290 // Write the cookie
291 $this->_set_cookie();
Darren Hillc4e266b2011-08-30 15:40:27 -0400292 }
293
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300294 // ------------------------------------------------------------------------
295
Darren Hillc4e266b2011-08-30 15:40:27 -0400296 /**
297 * Destroy the current session
298 *
299 * @return void
300 */
301 public function sess_destroy()
302 {
303 // Kill the session DB row
dchill42aee92652012-08-26 21:45:35 -0400304 if ($this->sess_use_database === TRUE && isset($this->userdata['session_id']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400305 {
dchill423cecd822012-08-28 21:37:27 -0400306 $this->CI->db->delete($this->sess_table_name, array('session_id' => $this->userdata['session_id']));
dchill4297b0d832012-09-04 10:09:00 -0400307 $this->data_dirty = FALSE;
Darren Hillc4e266b2011-08-30 15:40:27 -0400308 }
309
310 // Kill the cookie
dchill42c5872252012-07-30 14:53:11 -0400311 $this->_setcookie($this->sess_cookie_name, addslashes(serialize(array())), ($this->now - 31500000),
Darren Hillc4e266b2011-08-30 15:40:27 -0400312 $this->cookie_path, $this->cookie_domain, 0);
dchill42c5079de2012-07-23 10:53:47 -0400313
314 // Kill session data
315 $this->userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -0400316 }
317
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300318 // ------------------------------------------------------------------------
319
Darren Hillc4e266b2011-08-30 15:40:27 -0400320 /**
321 * Regenerate the current session
322 *
323 * Regenerate the session id
324 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300325 * @param bool Destroy session data flag (default: false)
Darren Hillc4e266b2011-08-30 15:40:27 -0400326 * @return void
327 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300328 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400329 {
330 // Check destroy flag
331 if ($destroy)
332 {
333 // Destroy old session and create new one
334 $this->sess_destroy();
335 $this->_sess_create();
336 }
337 else
338 {
339 // Just force an update to recreate the id
Andrey Andreev3f3f1352012-09-05 16:39:28 +0300340 $this->_sess_update(TRUE);
Darren Hillc4e266b2011-08-30 15:40:27 -0400341 }
342 }
343
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300344 // ------------------------------------------------------------------------
345
Darren Hillc4e266b2011-08-30 15:40:27 -0400346 /**
347 * Get a reference to user data array
348 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300349 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400350 */
351 public function &get_userdata()
352 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400353 return $this->userdata;
354 }
355
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300356 // ------------------------------------------------------------------------
357
Darren Hillc4e266b2011-08-30 15:40:27 -0400358 /**
359 * Fetch the current session data if it exists
360 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400361 * @return bool
362 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400363 protected function _sess_read()
Darren Hillc4e266b2011-08-30 15:40:27 -0400364 {
365 // Fetch the cookie
366 $session = $this->CI->input->cookie($this->sess_cookie_name);
367
368 // No cookie? Goodbye cruel world!...
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100369 if ($session === NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400370 {
371 log_message('debug', 'A session cookie was not found.');
372 return FALSE;
373 }
374
Pascal Krietef69f0e82012-10-16 11:54:49 -0400375 $len = strlen($session) - 40;
376
377 if ($len < 0)
378 {
379 log_message('debug', 'The session cookie was not signed.');
380 return FALSE;
381 }
382
383 // Check cookie authentication
384 $hmac = substr($session, $len);
385 $session = substr($session, 0, $len);
386
387 if ($hmac !== hash_hmac('sha1', $session, $this->encryption_key))
388 {
Pascal Kriete28dc2022012-10-17 11:27:29 -0400389 log_message('error', 'The session cookie data did not match what was expected.');
Pascal Krietef69f0e82012-10-16 11:54:49 -0400390 $this->sess_destroy();
391 return FALSE;
392 }
393
dchill423cecd822012-08-28 21:37:27 -0400394 // Check for encryption
Alex Bilbied261b1e2012-06-02 11:12:16 +0100395 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400396 {
dchill423cecd822012-08-28 21:37:27 -0400397 // Decrypt the cookie data
Darren Hillc4e266b2011-08-30 15:40:27 -0400398 $session = $this->CI->encrypt->decode($session);
399 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400400
401 // Unserialize the session array
402 $session = $this->_unserialize($session);
403
404 // Is the session data we unserialized an array with the correct format?
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300405 if ( ! is_array($session) OR ! isset($session['session_id'], $session['ip_address'], $session['user_agent'], $session['last_activity']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400406 {
407 $this->sess_destroy();
408 return FALSE;
409 }
410
411 // Is the session current?
Andrey Andreev02117682012-10-15 11:12:37 +0300412 if (($session['last_activity'] + $this->sess_expiration) < $this->now OR $session['last_activity'] > $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400413 {
414 $this->sess_destroy();
415 return FALSE;
416 }
417
Andrey Andreev7e087f52012-01-20 11:46:27 +0200418 // Does the IP match?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100419 if ($this->sess_match_ip === TRUE && $session['ip_address'] !== $this->CI->input->ip_address())
Darren Hillc4e266b2011-08-30 15:40:27 -0400420 {
421 $this->sess_destroy();
422 return FALSE;
423 }
424
425 // Does the User Agent Match?
dchill42c5079de2012-07-23 10:53:47 -0400426 if ($this->sess_match_useragent === TRUE &&
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300427 trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400428 {
429 $this->sess_destroy();
430 return FALSE;
431 }
432
433 // Is there a corresponding session in the DB?
434 if ($this->sess_use_database === TRUE)
435 {
436 $this->CI->db->where('session_id', $session['session_id']);
437
Alex Bilbied261b1e2012-06-02 11:12:16 +0100438 if ($this->sess_match_ip === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400439 {
440 $this->CI->db->where('ip_address', $session['ip_address']);
441 }
442
Alex Bilbied261b1e2012-06-02 11:12:16 +0100443 if ($this->sess_match_useragent === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400444 {
445 $this->CI->db->where('user_agent', $session['user_agent']);
446 }
447
dchill42cd436e92012-09-04 10:15:14 -0400448 // Is caching in effect? Turn it off
449 $db_cache = $this->CI->db->cache_on;
450 $this->CI->db->cache_off();
451
Timothy Warrenf1421922012-03-02 11:51:42 -0500452 $query = $this->CI->db->limit(1)->get($this->sess_table_name);
Darren Hillc4e266b2011-08-30 15:40:27 -0400453
dchill42cd436e92012-09-04 10:15:14 -0400454 // Was caching in effect?
455 if ($db_cache)
456 {
457 // Turn it back on
458 $this->CI->db->cache_on();
459 }
460
Darren Hillc4e266b2011-08-30 15:40:27 -0400461 // No result? Kill it!
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200462 if ($query->num_rows() === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400463 {
464 $this->sess_destroy();
465 return FALSE;
466 }
467
468 // Is there custom data? If so, add it to the main session array
469 $row = $query->row();
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300470 if ( ! empty($row->user_data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400471 {
472 $custom_data = $this->_unserialize($row->user_data);
473
474 if (is_array($custom_data))
475 {
dchill423cecd822012-08-28 21:37:27 -0400476 $session = $session + $custom_data;
Darren Hillc4e266b2011-08-30 15:40:27 -0400477 }
478 }
479 }
480
481 // Session is valid!
482 $this->userdata = $session;
Darren Hillc4e266b2011-08-30 15:40:27 -0400483 return TRUE;
484 }
485
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300486 // ------------------------------------------------------------------------
487
Darren Hillc4e266b2011-08-30 15:40:27 -0400488 /**
489 * Create a new session
490 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400491 * @return void
492 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400493 protected function _sess_create()
Darren Hillc4e266b2011-08-30 15:40:27 -0400494 {
dchill423cecd822012-08-28 21:37:27 -0400495 // Initialize userdata
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 $this->userdata = array(
dchill423cecd822012-08-28 21:37:27 -0400497 'session_id' => $this->_make_sess_id(),
dchill42c5079de2012-07-23 10:53:47 -0400498 'ip_address' => $this->CI->input->ip_address(),
499 'user_agent' => substr($this->CI->input->user_agent(), 0, 120),
500 'last_activity' => $this->now,
dchill42c5079de2012-07-23 10:53:47 -0400501 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400502
dchill423cecd822012-08-28 21:37:27 -0400503 // Check for database
Darren Hillc4e266b2011-08-30 15:40:27 -0400504 if ($this->sess_use_database === TRUE)
505 {
dchill423cecd822012-08-28 21:37:27 -0400506 // Add empty user_data field and save the data to the DB
507 $this->CI->db->set('user_data', '')->insert($this->sess_table_name, $this->userdata);
Darren Hillc4e266b2011-08-30 15:40:27 -0400508 }
509
510 // Write the cookie
511 $this->_set_cookie();
512 }
513
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300514 // ------------------------------------------------------------------------
515
Darren Hillc4e266b2011-08-30 15:40:27 -0400516 /**
517 * Update an existing session
518 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300519 * @param bool Force update flag (default: false)
Darren Hillc4e266b2011-08-30 15:40:27 -0400520 * @return void
521 */
dchill42c5079de2012-07-23 10:53:47 -0400522 protected function _sess_update($force = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400523 {
524 // We only update the session every five minutes by default (unless forced)
dchill4277ee3fd2012-07-24 11:50:01 -0400525 if ( ! $force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400526 {
527 return;
528 }
529
dchill423cecd822012-08-28 21:37:27 -0400530 // Update last activity to now
531 $this->userdata['last_activity'] = $this->now;
Andrey Andreev9c622f32012-01-19 14:12:54 +0200532
dchill423cecd822012-08-28 21:37:27 -0400533 // Save the old session id so we know which DB record to update
534 $old_sessid = $this->userdata['session_id'];
535
536 // Changing the session ID during an AJAX call causes problems
537 if ( ! $this->CI->input->is_ajax_request())
Andrey Andreev9c622f32012-01-19 14:12:54 +0200538 {
dchill423cecd822012-08-28 21:37:27 -0400539 // Get new id
540 $this->userdata['session_id'] = $this->_make_sess_id();
Andrey Andreev9c622f32012-01-19 14:12:54 +0200541 }
542
dchill423cecd822012-08-28 21:37:27 -0400543 // Check for database
544 if ($this->sess_use_database === TRUE)
545 {
546 // Update the session ID and last_activity field in the DB
547 $this->CI->db->update($this->sess_table_name, array(
548 'last_activity' => $this->now,
549 'session_id' => $this->userdata['session_id']
550 ), array('session_id' => $old_sessid));
551 }
552
553 // Write the cookie
554 $this->_set_cookie();
555 }
556
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300557 // ------------------------------------------------------------------------
558
dchill423cecd822012-08-28 21:37:27 -0400559 /**
560 * Update database with current data
561 *
562 * This gets called from the shutdown function and also
563 * registered with PHP to run at the end of the request
564 * so it's guaranteed to update even when a fatal error
565 * occurs. The first call makes the update and clears the
566 * dirty flag so it won't happen twice.
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300567 *
568 * @return void
dchill423cecd822012-08-28 21:37:27 -0400569 */
570 public function _update_db()
571 {
572 // Check for database and dirty flag and unsaved
573 if ($this->sess_use_database === TRUE && $this->data_dirty === TRUE)
574 {
575 // Set up activity and data fields to be set
576 // If we don't find custom data, user_data will remain an empty string
577 $set = array(
578 'last_activity' => $this->userdata['last_activity'],
579 'user_data' => ''
580 );
581
582 // Get the custom userdata, leaving out the defaults
583 // (which get stored in the cookie)
584 $userdata = array_diff_key($this->userdata, $this->defaults);
585
586 // Did we find any custom data?
587 if ( ! empty($userdata))
588 {
589 // Serialize the custom data array so we can store it
590 $set['user_data'] = $this->_serialize($userdata);
591 }
592
593 // Run the update query
594 // Any time we change the session id, it gets updated immediately,
595 // so our where clause below is always safe
596 $this->CI->db->update($this->sess_table_name, $set, array('session_id' => $this->userdata['session_id']));
597
598 // Clear dirty flag to prevent double updates
599 $this->data_dirty = FALSE;
600
601 log_message('debug', 'CI_Session Data Saved To DB');
602 }
603 }
604
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300605 // ------------------------------------------------------------------------
606
dchill423cecd822012-08-28 21:37:27 -0400607 /**
608 * Generate a new session id
609 *
610 * @return string Hashed session id
611 */
612 protected function _make_sess_id()
613 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400614 $new_sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200615 do
Darren Hillc4e266b2011-08-30 15:40:27 -0400616 {
617 $new_sessid .= mt_rand(0, mt_getrandmax());
618 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200619 while (strlen($new_sessid) < 32);
Darren Hillc4e266b2011-08-30 15:40:27 -0400620
621 // To make the session ID even more secure we'll combine it with the user's IP
622 $new_sessid .= $this->CI->input->ip_address();
623
dchill423cecd822012-08-28 21:37:27 -0400624 // Turn it into a hash and return
625 return md5(uniqid($new_sessid, TRUE));
Darren Hillc4e266b2011-08-30 15:40:27 -0400626 }
627
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300628 // ------------------------------------------------------------------------
629
Darren Hillc4e266b2011-08-30 15:40:27 -0400630 /**
631 * Get the "now" time
632 *
dchill42c5079de2012-07-23 10:53:47 -0400633 * @return int Time
Darren Hillc4e266b2011-08-30 15:40:27 -0400634 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400635 protected function _get_time()
Darren Hillc4e266b2011-08-30 15:40:27 -0400636 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300637 if ($this->time_reference === 'local' OR $this->time_reference === date_default_timezone_get())
Darren Hillc4e266b2011-08-30 15:40:27 -0400638 {
Iban Eguiafeb14da2012-06-12 16:09:36 +0200639 return time();
Darren Hillc4e266b2011-08-30 15:40:27 -0400640 }
641
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300642 $datetime = new DateTime('now', new DateTimeZone($this->time_reference));
Iban Eguiafeb14da2012-06-12 16:09:36 +0200643 sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
644
645 return mktime($hour, $minute, $second, $month, $day, $year);
Darren Hillc4e266b2011-08-30 15:40:27 -0400646 }
647
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300648 // ------------------------------------------------------------------------
649
Darren Hillc4e266b2011-08-30 15:40:27 -0400650 /**
651 * Write the session cookie
652 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400653 * @return void
654 */
dchill423cecd822012-08-28 21:37:27 -0400655 protected function _set_cookie()
Darren Hillc4e266b2011-08-30 15:40:27 -0400656 {
dchill423cecd822012-08-28 21:37:27 -0400657 // Get userdata (only defaults if database)
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300658 $cookie_data = ($this->sess_use_database === TRUE)
659 ? array_intersect_key($this->userdata, $this->defaults)
660 : $this->userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400661
662 // Serialize the userdata for the cookie
663 $cookie_data = $this->_serialize($cookie_data);
664
Pascal Krietef69f0e82012-10-16 11:54:49 -0400665 if ($this->sess_encrypt_cookie === TRUE)
666 {
667 $this->CI->encrypt->encode($cookie_data);
668 }
669
670 // Require message authentication
671 $cookie_data .= hash_hmac('sha1', $cookie_data, $this->encryption_key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400672
673 $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
674
675 // Set the cookie
dchill42c5872252012-07-30 14:53:11 -0400676 $this->_setcookie($this->sess_cookie_name, $cookie_data, $expire, $this->cookie_path, $this->cookie_domain,
dchill42c5079de2012-07-23 10:53:47 -0400677 $this->cookie_secure, $this->cookie_httponly);
Darren Hillc4e266b2011-08-30 15:40:27 -0400678 }
679
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300680 // ------------------------------------------------------------------------
681
Darren Hillc4e266b2011-08-30 15:40:27 -0400682 /**
dchill42c5872252012-07-30 14:53:11 -0400683 * Set a cookie with the system
684 *
685 * This abstraction of the setcookie call allows overriding for unit testing
686 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300687 * @param string Cookie name
688 * @param string Cookie value
689 * @param int Expiration time
690 * @param string Cookie path
691 * @param string Cookie domain
692 * @param bool Secure connection flag
693 * @param bool HTTP protocol only flag
694 * @return void
dchill42c5872252012-07-30 14:53:11 -0400695 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300696 protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = FALSE, $httponly = FALSE)
dchill42c5872252012-07-30 14:53:11 -0400697 {
dchill42c5872252012-07-30 14:53:11 -0400698 setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
699 }
700
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300701 // ------------------------------------------------------------------------
702
dchill42c5872252012-07-30 14:53:11 -0400703 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400704 * Serialize an array
705 *
706 * This function first converts any slashes found in the array to a temporary
707 * marker, so when it gets unserialized the slashes will be preserved
708 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400709 * @param mixed Data to serialize
dchill42c5079de2012-07-23 10:53:47 -0400710 * @return string Serialized data
Darren Hillc4e266b2011-08-30 15:40:27 -0400711 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400712 protected function _serialize($data)
Darren Hillc4e266b2011-08-30 15:40:27 -0400713 {
714 if (is_array($data))
715 {
Chris Muench95933492011-10-16 14:14:04 -0400716 array_walk_recursive($data, array(&$this, '_escape_slashes'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400717 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200718 elseif (is_string($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400719 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200720 $data = str_replace('\\', '{{slash}}', $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400721 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300722
Darren Hillc4e266b2011-08-30 15:40:27 -0400723 return serialize($data);
724 }
725
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300726 // ------------------------------------------------------------------------
727
Darren Hillc4e266b2011-08-30 15:40:27 -0400728 /**
Chris Muench95933492011-10-16 14:14:04 -0400729 * Escape slashes
730 *
731 * This function converts any slashes found into a temporary marker
732 *
dchill42c5079de2012-07-23 10:53:47 -0400733 * @param string Value
734 * @param string Key
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200735 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400736 */
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200737 protected function _escape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400738 {
739 if (is_string($val))
740 {
741 $val = str_replace('\\', '{{slash}}', $val);
742 }
743 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000744
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300745 // ------------------------------------------------------------------------
746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400748 * Unserialize
749 *
750 * This function unserializes a data string, then converts any
751 * temporary slash markers back to actual slashes
752 *
dchill42c5079de2012-07-23 10:53:47 -0400753 * @param mixed Data to unserialize
754 * @return mixed Unserialized data
Darren Hillc4e266b2011-08-30 15:40:27 -0400755 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400756 protected function _unserialize($data)
Darren Hillc4e266b2011-08-30 15:40:27 -0400757 {
Andrey Andreev6b831232012-03-06 11:16:57 +0200758 $data = @unserialize(strip_slashes(trim($data)));
Darren Hillc4e266b2011-08-30 15:40:27 -0400759
760 if (is_array($data))
761 {
Chris Muench95933492011-10-16 14:14:04 -0400762 array_walk_recursive($data, array(&$this, '_unescape_slashes'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400763 return $data;
764 }
765
Andrey Andreev6b831232012-03-06 11:16:57 +0200766 return is_string($data) ? str_replace('{{slash}}', '\\', $data) : $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200768
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300769 // ------------------------------------------------------------------------
770
Chris Muench95933492011-10-16 14:14:04 -0400771 /**
772 * Unescape slashes
773 *
774 * This function converts any slash markers back into actual slashes
775 *
dchill42c5079de2012-07-23 10:53:47 -0400776 * @param string Value
777 * @param string Key
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200778 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400779 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200780 protected function _unescape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400781 {
Chris Muench3e414f92011-10-16 23:03:55 -0400782 if (is_string($val))
783 {
784 $val= str_replace('{{slash}}', '\\', $val);
785 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400786 }
787
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300788 // ------------------------------------------------------------------------
789
Darren Hillc4e266b2011-08-30 15:40:27 -0400790 /**
791 * Garbage collection
792 *
793 * This deletes expired session rows from database
794 * if the probability percentage is met
795 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400796 * @return void
797 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400798 protected function _sess_gc()
Darren Hillc4e266b2011-08-30 15:40:27 -0400799 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100800 if ($this->sess_use_database !== TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400801 {
802 return;
803 }
804
Christopher Guiney7a142862012-06-29 20:34:28 -0700805 $probability = ini_get('session.gc_probability');
806 $divisor = ini_get('session.gc_divisor');
807
Darren Hillc4e266b2011-08-30 15:40:27 -0400808 srand(time());
Christopher Guiney7a142862012-06-29 20:34:28 -0700809 if ((mt_rand(0, $divisor) / $divisor) < $probability)
Darren Hillc4e266b2011-08-30 15:40:27 -0400810 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 $expire = $this->now - $this->sess_expiration;
dchill423cecd822012-08-28 21:37:27 -0400812 $this->CI->db->delete($this->sess_table_name, 'last_activity < '.$expire);
Darren Hillc4e266b2011-08-30 15:40:27 -0400813
814 log_message('debug', 'Session garbage collection performed.');
815 }
816 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300817
Darren Hillc4e266b2011-08-30 15:40:27 -0400818}
Darren Hillc4e266b2011-08-30 15:40:27 -0400819
820/* End of file Session_cookie.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300821/* Location: ./system/libraries/Session/drivers/Session_cookie.php */