blob: c8dfad6c9accd2d24d4ea6eeb68ed0d0f1d6f7db [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
Andrey Andreevc5536aa2012-11-01 17:33:58 +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.
Darren Hillc4e266b2011-08-30 15:40:27 -040018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Darren Hillc4e266b2011-08-30 15:40:27 -040028
Darren Hillc4e266b2011-08-30 15:40:27 -040029/**
30 * Cookie-based session management driver
31 *
dchill423cecd822012-08-28 21:37:27 -040032 * This is the classic CI_Session functionality, as written by EllisLab, abstracted out to a driver.
Darren Hillc4e266b2011-08-30 15:40:27 -040033 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Sessions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/sessions.html
Darren Hillc4e266b2011-08-30 15:40:27 -040039 */
Darren Hill5073a372011-08-31 13:54:19 -040040class CI_Session_cookie extends CI_Session_driver {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030041
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020042 /**
Timothy Warren68f09812012-04-27 10:38:32 -040043 * Whether to encrypt the session cookie
44 *
45 * @var bool
46 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020047 public $sess_encrypt_cookie = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020048
Timothy Warren68f09812012-04-27 10:38:32 -040049 /**
50 * Whether to use to the database for session storage
51 *
52 * @var bool
53 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020054 public $sess_use_database = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020055
Timothy Warren68f09812012-04-27 10:38:32 -040056 /**
57 * Name of the database table in which to store sessions
58 *
59 * @var string
60 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020061 public $sess_table_name = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020062
Timothy Warren68f09812012-04-27 10:38:32 -040063 /**
64 * Length of time (in seconds) for sessions to expire
65 *
66 * @var int
67 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020068 public $sess_expiration = 7200;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020069
Timothy Warren68f09812012-04-27 10:38:32 -040070 /**
71 * Whether to kill session on close of browser window
72 *
73 * @var bool
74 */
dchill4226429202012-07-31 10:55:07 -040075 public $sess_expire_on_close = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020076
Timothy Warren68f09812012-04-27 10:38:32 -040077 /**
78 * Whether to match session on ip address
79 *
80 * @var bool
81 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020082 public $sess_match_ip = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020083
Timothy Warren68f09812012-04-27 10:38:32 -040084 /**
85 * Whether to match session on user-agent
86 *
87 * @var bool
88 */
dchill4226429202012-07-31 10:55:07 -040089 public $sess_match_useragent = TRUE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020090
Timothy Warren68f09812012-04-27 10:38:32 -040091 /**
92 * Name of session cookie
93 *
94 * @var string
95 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020096 public $sess_cookie_name = 'ci_session';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +020097
Timothy Warren68f09812012-04-27 10:38:32 -040098 /**
99 * Session cookie prefix
100 *
101 * @var string
102 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200103 public $cookie_prefix = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200104
Timothy Warren68f09812012-04-27 10:38:32 -0400105 /**
106 * Session cookie path
107 *
108 * @var string
109 */
dchill4226429202012-07-31 10:55:07 -0400110 public $cookie_path = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200111
Timothy Warren68f09812012-04-27 10:38:32 -0400112 /**
113 * Session cookie domain
114 *
115 * @var string
116 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200117 public $cookie_domain = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200118
Timothy Warren68f09812012-04-27 10:38:32 -0400119 /**
120 * Whether to set the cookie only on HTTPS connections
121 *
122 * @var bool
123 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200124 public $cookie_secure = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200125
Timothy Warren68f09812012-04-27 10:38:32 -0400126 /**
127 * Whether cookie should be allowed only to be sent by the server
128 *
129 * @var bool
130 */
freewil4ad0fd82012-03-13 22:37:42 -0400131 public $cookie_httponly = FALSE;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200132
Timothy Warren68f09812012-04-27 10:38:32 -0400133 /**
134 * Interval at which to update session
135 *
136 * @var int
137 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200138 public $sess_time_to_update = 300;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200139
Timothy Warren68f09812012-04-27 10:38:32 -0400140 /**
141 * Key with which to encrypt the session cookie
142 *
143 * @var string
144 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200145 public $encryption_key = '';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200146
Timothy Warren68f09812012-04-27 10:38:32 -0400147 /**
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300148 * Timezone to use for the current time
Timothy Warren68f09812012-04-27 10:38:32 -0400149 *
150 * @var string
151 */
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300152 public $time_reference = 'local';
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200153
Timothy Warren68f09812012-04-27 10:38:32 -0400154 /**
155 * Session data
156 *
157 * @var array
158 */
dchill4226429202012-07-31 10:55:07 -0400159 public $userdata = array();
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200160
Timothy Warren68f09812012-04-27 10:38:32 -0400161 /**
Timothy Warren68f09812012-04-27 10:38:32 -0400162 * Current time
163 *
164 * @var int
165 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200166 public $now;
Darren Hillc4e266b2011-08-30 15:40:27 -0400167
Andrey Andreevbfb635b2014-01-08 18:32:05 +0200168 // ------------------------------------------------------------------------
169
Darren Hillc4e266b2011-08-30 15:40:27 -0400170 /**
dchill423cecd822012-08-28 21:37:27 -0400171 * Default userdata keys
172 *
173 * @var array
174 */
175 protected $defaults = array(
dchill4288b636b2012-08-29 08:47:05 -0400176 'session_id' => NULL,
177 'ip_address' => NULL,
178 'user_agent' => NULL,
179 'last_activity' => NULL
dchill423cecd822012-08-28 21:37:27 -0400180 );
181
182 /**
183 * Data needs DB update flag
184 *
185 * @var bool
186 */
187 protected $data_dirty = FALSE;
188
189 /**
Andrey Andreevbfb635b2014-01-08 18:32:05 +0200190 * Standardize newlines flag
191 *
192 * @var bool
193 */
194 protected $_standardize_newlines;
195
196 // ------------------------------------------------------------------------
197
198 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400199 * Initialize session driver object
200 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400201 * @return void
202 */
203 protected function initialize()
204 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400205 // Set all the session preferences, which can either be set
dchill4242b77a92012-07-23 11:28:42 -0400206 // manually via the $params array or via the config file
dchill4226429202012-07-31 10:55:07 -0400207 $prefs = array(
208 'sess_encrypt_cookie',
209 'sess_use_database',
210 'sess_table_name',
211 'sess_expiration',
212 'sess_expire_on_close',
213 'sess_match_ip',
214 'sess_match_useragent',
215 'sess_cookie_name',
216 'cookie_path',
217 'cookie_domain',
218 'cookie_secure',
219 'cookie_httponly',
220 'sess_time_to_update',
221 'time_reference',
222 'cookie_prefix',
Andrey Andreevbfb635b2014-01-08 18:32:05 +0200223 'encryption_key',
dchill4226429202012-07-31 10:55:07 -0400224 );
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300225
Andrey Andreev4ea76cc2014-01-08 21:49:23 +0200226 $this->_standardize_newlines = (bool) config_item('standardize_newlines');
Andrey Andreevbfb635b2014-01-08 18:32:05 +0200227
dchill4226429202012-07-31 10:55:07 -0400228 foreach ($prefs as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400229 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300230 $this->$key = isset($this->_parent->params[$key])
231 ? $this->_parent->params[$key]
232 : $this->CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400233 }
234
Andrey Andreev4abd0942012-11-26 12:13:59 +0200235 if (empty($this->encryption_key))
Darren Hillc4e266b2011-08-30 15:40:27 -0400236 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300237 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 -0400238 }
239
Darren Hillc4e266b2011-08-30 15:40:27 -0400240 // Do we need encryption? If so, load the encryption class
Alex Bilbied261b1e2012-06-02 11:12:16 +0100241 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400242 {
243 $this->CI->load->library('encrypt');
244 }
245
dchill423cecd822012-08-28 21:37:27 -0400246 // Check for database
Alex Bilbied261b1e2012-06-02 11:12:16 +0100247 if ($this->sess_use_database === TRUE && $this->sess_table_name !== '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400248 {
dchill423cecd822012-08-28 21:37:27 -0400249 // Load database driver
Darren Hillc4e266b2011-08-30 15:40:27 -0400250 $this->CI->load->database();
dchill423cecd822012-08-28 21:37:27 -0400251
252 // Register shutdown function
253 register_shutdown_function(array($this, '_update_db'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400254 }
255
256 // Set the "now" time. Can either be GMT or server time, based on the config prefs.
257 // We use this to set the "last activity" time
258 $this->now = $this->_get_time();
259
260 // Set the session length. If the session expiration is
261 // set to zero we'll set the expiration two years from now.
Alex Bilbied261b1e2012-06-02 11:12:16 +0100262 if ($this->sess_expiration === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400263 {
264 $this->sess_expiration = (60*60*24*365*2);
265 }
266
267 // Set the cookie name
268 $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
269
270 // Run the Session routine. If a session doesn't exist we'll
271 // create a new one. If it does, we'll update it.
272 if ( ! $this->_sess_read())
273 {
274 $this->_sess_create();
275 }
276 else
277 {
278 $this->_sess_update();
279 }
280
281 // Delete expired sessions if necessary
282 $this->_sess_gc();
283 }
284
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300285 // ------------------------------------------------------------------------
286
Darren Hillc4e266b2011-08-30 15:40:27 -0400287 /**
288 * Write the session data
289 *
290 * @return void
291 */
292 public function sess_save()
293 {
dchill423cecd822012-08-28 21:37:27 -0400294 // Check for database
dchill4288b636b2012-08-29 08:47:05 -0400295 if ($this->sess_use_database === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400296 {
dchill423cecd822012-08-28 21:37:27 -0400297 // Mark custom data as dirty so we know to update the DB
298 $this->data_dirty = TRUE;
Darren Hillc4e266b2011-08-30 15:40:27 -0400299 }
300
dchill423cecd822012-08-28 21:37:27 -0400301 // Write the cookie
302 $this->_set_cookie();
Darren Hillc4e266b2011-08-30 15:40:27 -0400303 }
304
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300305 // ------------------------------------------------------------------------
306
Darren Hillc4e266b2011-08-30 15:40:27 -0400307 /**
308 * Destroy the current session
309 *
310 * @return void
311 */
312 public function sess_destroy()
313 {
314 // Kill the session DB row
dchill42aee92652012-08-26 21:45:35 -0400315 if ($this->sess_use_database === TRUE && isset($this->userdata['session_id']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400316 {
dchill423cecd822012-08-28 21:37:27 -0400317 $this->CI->db->delete($this->sess_table_name, array('session_id' => $this->userdata['session_id']));
dchill4297b0d832012-09-04 10:09:00 -0400318 $this->data_dirty = FALSE;
Darren Hillc4e266b2011-08-30 15:40:27 -0400319 }
320
321 // Kill the cookie
Andrey Andreevcf264e02012-10-18 16:14:51 +0300322 $this->_setcookie($this->sess_cookie_name, '', ($this->now - 31500000),
Darren Hillc4e266b2011-08-30 15:40:27 -0400323 $this->cookie_path, $this->cookie_domain, 0);
dchill42c5079de2012-07-23 10:53:47 -0400324
325 // Kill session data
326 $this->userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -0400327 }
328
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300329 // ------------------------------------------------------------------------
330
Darren Hillc4e266b2011-08-30 15:40:27 -0400331 /**
332 * Regenerate the current session
333 *
334 * Regenerate the session id
335 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300336 * @param bool Destroy session data flag (default: false)
Darren Hillc4e266b2011-08-30 15:40:27 -0400337 * @return void
338 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300339 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400340 {
341 // Check destroy flag
342 if ($destroy)
343 {
344 // Destroy old session and create new one
345 $this->sess_destroy();
346 $this->_sess_create();
347 }
348 else
349 {
350 // Just force an update to recreate the id
Andrey Andreev3f3f1352012-09-05 16:39:28 +0300351 $this->_sess_update(TRUE);
Darren Hillc4e266b2011-08-30 15:40:27 -0400352 }
353 }
354
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300355 // ------------------------------------------------------------------------
356
Darren Hillc4e266b2011-08-30 15:40:27 -0400357 /**
358 * Get a reference to user data array
359 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300360 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400361 */
362 public function &get_userdata()
363 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400364 return $this->userdata;
365 }
366
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300367 // ------------------------------------------------------------------------
368
Darren Hillc4e266b2011-08-30 15:40:27 -0400369 /**
370 * Fetch the current session data if it exists
371 *
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
Pascal Krietef69f0e82012-10-16 11:54:49 -0400386 $len = strlen($session) - 40;
387
388 if ($len < 0)
389 {
390 log_message('debug', 'The session cookie was not signed.');
391 return FALSE;
392 }
393
394 // Check cookie authentication
395 $hmac = substr($session, $len);
396 $session = substr($session, 0, $len);
397
Andrey Andreev3aa781a2014-02-06 05:34:19 +0200398 // Time-attack-safe comparison
399 $hmac_check = hash_hmac('sha1', $session, $this->encryption_key);
400 $diff = 0;
401 for ($i = 0; $i < 40; $i++)
402 {
403 $diff |= ord($hmac[$i]) ^ ord($hmac_check[$i]);
404 }
405
406 if ($diff !== 0)
Pascal Krietef69f0e82012-10-16 11:54:49 -0400407 {
Pascal Kriete28dc2022012-10-17 11:27:29 -0400408 log_message('error', 'The session cookie data did not match what was expected.');
Pascal Krietef69f0e82012-10-16 11:54:49 -0400409 $this->sess_destroy();
410 return FALSE;
411 }
412
dchill423cecd822012-08-28 21:37:27 -0400413 // Check for encryption
Alex Bilbied261b1e2012-06-02 11:12:16 +0100414 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400415 {
dchill423cecd822012-08-28 21:37:27 -0400416 // Decrypt the cookie data
Darren Hillc4e266b2011-08-30 15:40:27 -0400417 $session = $this->CI->encrypt->decode($session);
418 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400419
420 // Unserialize the session array
Jordan Eldredge5306cad2013-12-23 11:10:51 -0800421 $session = @unserialize($session);
Darren Hillc4e266b2011-08-30 15:40:27 -0400422
423 // Is the session data we unserialized an array with the correct format?
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300424 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 -0400425 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300426 log_message('debug', 'Session: Wrong cookie data format');
Darren Hillc4e266b2011-08-30 15:40:27 -0400427 $this->sess_destroy();
428 return FALSE;
429 }
430
431 // Is the session current?
Andrey Andreev02117682012-10-15 11:12:37 +0300432 if (($session['last_activity'] + $this->sess_expiration) < $this->now OR $session['last_activity'] > $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400433 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300434 log_message('debug', 'Session: Expired');
Darren Hillc4e266b2011-08-30 15:40:27 -0400435 $this->sess_destroy();
436 return FALSE;
437 }
438
Andrey Andreev7e087f52012-01-20 11:46:27 +0200439 // Does the IP match?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100440 if ($this->sess_match_ip === TRUE && $session['ip_address'] !== $this->CI->input->ip_address())
Darren Hillc4e266b2011-08-30 15:40:27 -0400441 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300442 log_message('debug', 'Session: IP address mismatch');
Darren Hillc4e266b2011-08-30 15:40:27 -0400443 $this->sess_destroy();
444 return FALSE;
445 }
446
447 // Does the User Agent Match?
dchill42c5079de2012-07-23 10:53:47 -0400448 if ($this->sess_match_useragent === TRUE &&
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300449 trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400450 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300451 log_message('debug', 'Session: User Agent string mismatch');
Darren Hillc4e266b2011-08-30 15:40:27 -0400452 $this->sess_destroy();
453 return FALSE;
454 }
455
456 // Is there a corresponding session in the DB?
457 if ($this->sess_use_database === TRUE)
458 {
459 $this->CI->db->where('session_id', $session['session_id']);
460
Alex Bilbied261b1e2012-06-02 11:12:16 +0100461 if ($this->sess_match_ip === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400462 {
463 $this->CI->db->where('ip_address', $session['ip_address']);
464 }
465
Alex Bilbied261b1e2012-06-02 11:12:16 +0100466 if ($this->sess_match_useragent === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400467 {
468 $this->CI->db->where('user_agent', $session['user_agent']);
469 }
470
dchill42cd436e92012-09-04 10:15:14 -0400471 // Is caching in effect? Turn it off
472 $db_cache = $this->CI->db->cache_on;
473 $this->CI->db->cache_off();
474
Timothy Warrenf1421922012-03-02 11:51:42 -0500475 $query = $this->CI->db->limit(1)->get($this->sess_table_name);
Darren Hillc4e266b2011-08-30 15:40:27 -0400476
dchill42cd436e92012-09-04 10:15:14 -0400477 // Was caching in effect?
478 if ($db_cache)
479 {
480 // Turn it back on
481 $this->CI->db->cache_on();
482 }
483
Darren Hillc4e266b2011-08-30 15:40:27 -0400484 // No result? Kill it!
Andrey Andreeva8e34ac2012-12-17 10:39:32 +0200485 if (empty($query) OR $query->num_rows() === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400486 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300487 log_message('debug', 'Session: No match found in our database');
Darren Hillc4e266b2011-08-30 15:40:27 -0400488 $this->sess_destroy();
489 return FALSE;
490 }
491
492 // Is there custom data? If so, add it to the main session array
493 $row = $query->row();
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300494 if ( ! empty($row->user_data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400495 {
Jordan Eldredge5306cad2013-12-23 11:10:51 -0800496 $custom_data = unserialize(trim($row->user_data));
Darren Hillc4e266b2011-08-30 15:40:27 -0400497
498 if (is_array($custom_data))
499 {
dchill423cecd822012-08-28 21:37:27 -0400500 $session = $session + $custom_data;
Darren Hillc4e266b2011-08-30 15:40:27 -0400501 }
502 }
503 }
504
505 // Session is valid!
506 $this->userdata = $session;
Darren Hillc4e266b2011-08-30 15:40:27 -0400507 return TRUE;
508 }
509
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300510 // ------------------------------------------------------------------------
511
Darren Hillc4e266b2011-08-30 15:40:27 -0400512 /**
513 * Create a new session
514 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400515 * @return void
516 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400517 protected function _sess_create()
Darren Hillc4e266b2011-08-30 15:40:27 -0400518 {
dchill423cecd822012-08-28 21:37:27 -0400519 // Initialize userdata
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 $this->userdata = array(
dchill423cecd822012-08-28 21:37:27 -0400521 'session_id' => $this->_make_sess_id(),
dchill42c5079de2012-07-23 10:53:47 -0400522 'ip_address' => $this->CI->input->ip_address(),
Daniel Robbins930d8ef2013-03-01 21:36:48 -0500523 'user_agent' => trim(substr($this->CI->input->user_agent(), 0, 120)),
dchill42c5079de2012-07-23 10:53:47 -0400524 'last_activity' => $this->now,
dchill42c5079de2012-07-23 10:53:47 -0400525 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400526
Andrey Andreeve18de502013-07-17 19:59:20 +0300527 log_message('debug', 'Session: Creating new session ('.$this->userdata['session_id'].')');
528
dchill423cecd822012-08-28 21:37:27 -0400529 // Check for database
Darren Hillc4e266b2011-08-30 15:40:27 -0400530 if ($this->sess_use_database === TRUE)
531 {
dchill423cecd822012-08-28 21:37:27 -0400532 // Add empty user_data field and save the data to the DB
533 $this->CI->db->set('user_data', '')->insert($this->sess_table_name, $this->userdata);
Darren Hillc4e266b2011-08-30 15:40:27 -0400534 }
535
536 // Write the cookie
537 $this->_set_cookie();
538 }
539
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300540 // ------------------------------------------------------------------------
541
Darren Hillc4e266b2011-08-30 15:40:27 -0400542 /**
543 * Update an existing session
544 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300545 * @param bool Force update flag (default: false)
Darren Hillc4e266b2011-08-30 15:40:27 -0400546 * @return void
547 */
dchill42c5079de2012-07-23 10:53:47 -0400548 protected function _sess_update($force = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400549 {
550 // We only update the session every five minutes by default (unless forced)
dchill4277ee3fd2012-07-24 11:50:01 -0400551 if ( ! $force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400552 {
553 return;
554 }
555
dchill423cecd822012-08-28 21:37:27 -0400556 // Update last activity to now
557 $this->userdata['last_activity'] = $this->now;
Andrey Andreev9c622f32012-01-19 14:12:54 +0200558
dchill423cecd822012-08-28 21:37:27 -0400559 // Save the old session id so we know which DB record to update
560 $old_sessid = $this->userdata['session_id'];
561
562 // Changing the session ID during an AJAX call causes problems
563 if ( ! $this->CI->input->is_ajax_request())
Andrey Andreev9c622f32012-01-19 14:12:54 +0200564 {
dchill423cecd822012-08-28 21:37:27 -0400565 // Get new id
566 $this->userdata['session_id'] = $this->_make_sess_id();
Andrey Andreeve18de502013-07-17 19:59:20 +0300567
568 log_message('debug', 'Session: Regenerate ID');
Andrey Andreev9c622f32012-01-19 14:12:54 +0200569 }
570
dchill423cecd822012-08-28 21:37:27 -0400571 // Check for database
572 if ($this->sess_use_database === TRUE)
573 {
Andrey Andreeve2afc882012-11-01 01:35:34 +0200574 $this->CI->db->where('session_id', $old_sessid);
575
576 if ($this->sess_match_ip === TRUE)
577 {
578 $this->CI->db->where('ip_address', $this->CI->input->ip_address());
579 }
580
581 if ($this->sess_match_useragent === TRUE)
582 {
583 $this->CI->db->where('user_agent', trim(substr($this->CI->input->user_agent(), 0, 120)));
584 }
585
dchill423cecd822012-08-28 21:37:27 -0400586 // Update the session ID and last_activity field in the DB
Andrey Andreeve2afc882012-11-01 01:35:34 +0200587 $this->CI->db->update($this->sess_table_name,
588 array(
589 'last_activity' => $this->now,
590 'session_id' => $this->userdata['session_id']
591 )
592 );
dchill423cecd822012-08-28 21:37:27 -0400593 }
594
595 // Write the cookie
596 $this->_set_cookie();
597 }
598
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300599 // ------------------------------------------------------------------------
600
dchill423cecd822012-08-28 21:37:27 -0400601 /**
602 * Update database with current data
603 *
604 * This gets called from the shutdown function and also
605 * registered with PHP to run at the end of the request
606 * so it's guaranteed to update even when a fatal error
607 * occurs. The first call makes the update and clears the
608 * dirty flag so it won't happen twice.
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300609 *
610 * @return void
dchill423cecd822012-08-28 21:37:27 -0400611 */
612 public function _update_db()
613 {
614 // Check for database and dirty flag and unsaved
615 if ($this->sess_use_database === TRUE && $this->data_dirty === TRUE)
616 {
617 // Set up activity and data fields to be set
618 // If we don't find custom data, user_data will remain an empty string
619 $set = array(
620 'last_activity' => $this->userdata['last_activity'],
621 'user_data' => ''
622 );
623
624 // Get the custom userdata, leaving out the defaults
625 // (which get stored in the cookie)
626 $userdata = array_diff_key($this->userdata, $this->defaults);
627
628 // Did we find any custom data?
629 if ( ! empty($userdata))
630 {
631 // Serialize the custom data array so we can store it
Jordan Eldredge5306cad2013-12-23 11:10:51 -0800632 $set['user_data'] = serialize($userdata);
dchill423cecd822012-08-28 21:37:27 -0400633 }
634
Dionysis Arvanitisf8e2d0e2013-02-19 23:27:16 +0200635 // Reset query builder values.
636 $this->CI->db->reset_query();
637
dchill423cecd822012-08-28 21:37:27 -0400638 // Run the update query
639 // Any time we change the session id, it gets updated immediately,
640 // so our where clause below is always safe
Andrey Andreeve2afc882012-11-01 01:35:34 +0200641 $this->CI->db->where('session_id', $this->userdata['session_id']);
642
643 if ($this->sess_match_ip === TRUE)
644 {
645 $this->CI->db->where('ip_address', $this->CI->input->ip_address());
646 }
647
648 if ($this->sess_match_useragent === TRUE)
649 {
650 $this->CI->db->where('user_agent', trim(substr($this->CI->input->user_agent(), 0, 120)));
651 }
652
653 $this->CI->db->update($this->sess_table_name, $set);
dchill423cecd822012-08-28 21:37:27 -0400654
655 // Clear dirty flag to prevent double updates
656 $this->data_dirty = FALSE;
657
658 log_message('debug', 'CI_Session Data Saved To DB');
659 }
660 }
661
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300662 // ------------------------------------------------------------------------
663
dchill423cecd822012-08-28 21:37:27 -0400664 /**
665 * Generate a new session id
666 *
667 * @return string Hashed session id
668 */
669 protected function _make_sess_id()
670 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400671 $new_sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200672 do
Darren Hillc4e266b2011-08-30 15:40:27 -0400673 {
vlakoff06127562013-03-30 00:06:39 +0100674 $new_sessid .= mt_rand();
Darren Hillc4e266b2011-08-30 15:40:27 -0400675 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200676 while (strlen($new_sessid) < 32);
Darren Hillc4e266b2011-08-30 15:40:27 -0400677
678 // To make the session ID even more secure we'll combine it with the user's IP
679 $new_sessid .= $this->CI->input->ip_address();
680
dchill423cecd822012-08-28 21:37:27 -0400681 // Turn it into a hash and return
682 return md5(uniqid($new_sessid, TRUE));
Darren Hillc4e266b2011-08-30 15:40:27 -0400683 }
684
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300685 // ------------------------------------------------------------------------
686
Darren Hillc4e266b2011-08-30 15:40:27 -0400687 /**
688 * Get the "now" time
689 *
dchill42c5079de2012-07-23 10:53:47 -0400690 * @return int Time
Darren Hillc4e266b2011-08-30 15:40:27 -0400691 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400692 protected function _get_time()
Darren Hillc4e266b2011-08-30 15:40:27 -0400693 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300694 if ($this->time_reference === 'local' OR $this->time_reference === date_default_timezone_get())
Darren Hillc4e266b2011-08-30 15:40:27 -0400695 {
Iban Eguiafeb14da2012-06-12 16:09:36 +0200696 return time();
Darren Hillc4e266b2011-08-30 15:40:27 -0400697 }
698
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300699 $datetime = new DateTime('now', new DateTimeZone($this->time_reference));
Iban Eguiafeb14da2012-06-12 16:09:36 +0200700 sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
701
702 return mktime($hour, $minute, $second, $month, $day, $year);
Darren Hillc4e266b2011-08-30 15:40:27 -0400703 }
704
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300705 // ------------------------------------------------------------------------
706
Darren Hillc4e266b2011-08-30 15:40:27 -0400707 /**
708 * Write the session cookie
709 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400710 * @return void
711 */
dchill423cecd822012-08-28 21:37:27 -0400712 protected function _set_cookie()
Darren Hillc4e266b2011-08-30 15:40:27 -0400713 {
dchill423cecd822012-08-28 21:37:27 -0400714 // Get userdata (only defaults if database)
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300715 $cookie_data = ($this->sess_use_database === TRUE)
716 ? array_intersect_key($this->userdata, $this->defaults)
717 : $this->userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400718
Andrey Andreevbfb635b2014-01-08 18:32:05 +0200719 // The Input class will do this and since we use HMAC verification,
720 // unless we standardize here as well, the hash won't match.
721 if ($this->_standardize_newlines)
722 {
723 foreach (array_keys($this->userdata) as $key)
724 {
725 $this->userdata[$key] = preg_replace('/(?:\r\n|[\r\n])/', PHP_EOL, $this->userdata[$key]);
726 }
727 }
728
Darren Hillc4e266b2011-08-30 15:40:27 -0400729 // Serialize the userdata for the cookie
Jordan Eldredge5306cad2013-12-23 11:10:51 -0800730 $cookie_data = serialize($cookie_data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400731
Pascal Krietef69f0e82012-10-16 11:54:49 -0400732 if ($this->sess_encrypt_cookie === TRUE)
733 {
Andrey Andreevcf264e02012-10-18 16:14:51 +0300734 $cookie_data = $this->CI->encrypt->encode($cookie_data);
Pascal Krietef69f0e82012-10-16 11:54:49 -0400735 }
736
737 // Require message authentication
738 $cookie_data .= hash_hmac('sha1', $cookie_data, $this->encryption_key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400739
740 $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
741
742 // Set the cookie
dchill42c5872252012-07-30 14:53:11 -0400743 $this->_setcookie($this->sess_cookie_name, $cookie_data, $expire, $this->cookie_path, $this->cookie_domain,
dchill42c5079de2012-07-23 10:53:47 -0400744 $this->cookie_secure, $this->cookie_httponly);
Darren Hillc4e266b2011-08-30 15:40:27 -0400745 }
746
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300747 // ------------------------------------------------------------------------
748
Darren Hillc4e266b2011-08-30 15:40:27 -0400749 /**
dchill42c5872252012-07-30 14:53:11 -0400750 * Set a cookie with the system
751 *
752 * This abstraction of the setcookie call allows overriding for unit testing
753 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300754 * @param string Cookie name
755 * @param string Cookie value
756 * @param int Expiration time
757 * @param string Cookie path
758 * @param string Cookie domain
759 * @param bool Secure connection flag
760 * @param bool HTTP protocol only flag
761 * @return void
dchill42c5872252012-07-30 14:53:11 -0400762 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300763 protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = FALSE, $httponly = FALSE)
dchill42c5872252012-07-30 14:53:11 -0400764 {
dchill42c5872252012-07-30 14:53:11 -0400765 setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
766 }
767
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300768 // ------------------------------------------------------------------------
769
dchill42c5872252012-07-30 14:53:11 -0400770 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400771 * Garbage collection
772 *
773 * This deletes expired session rows from database
774 * if the probability percentage is met
775 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400776 * @return void
777 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400778 protected function _sess_gc()
Darren Hillc4e266b2011-08-30 15:40:27 -0400779 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100780 if ($this->sess_use_database !== TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400781 {
782 return;
783 }
784
Christopher Guiney7a142862012-06-29 20:34:28 -0700785 $probability = ini_get('session.gc_probability');
786 $divisor = ini_get('session.gc_divisor');
787
Tyler Brownell74c5f262013-12-13 00:23:12 -0500788 if (mt_rand(1, $divisor) <= $probability)
Darren Hillc4e266b2011-08-30 15:40:27 -0400789 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 $expire = $this->now - $this->sess_expiration;
dchill423cecd822012-08-28 21:37:27 -0400791 $this->CI->db->delete($this->sess_table_name, 'last_activity < '.$expire);
Darren Hillc4e266b2011-08-30 15:40:27 -0400792
793 log_message('debug', 'Session garbage collection performed.');
794 }
795 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300796
Darren Hillc4e266b2011-08-30 15:40:27 -0400797}
Darren Hillc4e266b2011-08-30 15:40:27 -0400798
799/* End of file Session_cookie.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300800/* Location: ./system/libraries/Session/drivers/Session_cookie.php */