blob: d3d22d03a19c4cc59f9a9f5e6abb0f93cd408db4 [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
168 /**
dchill423cecd822012-08-28 21:37:27 -0400169 * Default userdata keys
170 *
171 * @var array
172 */
173 protected $defaults = array(
dchill4288b636b2012-08-29 08:47:05 -0400174 'session_id' => NULL,
175 'ip_address' => NULL,
176 'user_agent' => NULL,
177 'last_activity' => NULL
dchill423cecd822012-08-28 21:37:27 -0400178 );
179
180 /**
181 * Data needs DB update flag
182 *
183 * @var bool
184 */
185 protected $data_dirty = FALSE;
186
187 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400188 * Initialize session driver object
189 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400190 * @return void
191 */
192 protected function initialize()
193 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400194 // Set all the session preferences, which can either be set
dchill4242b77a92012-07-23 11:28:42 -0400195 // manually via the $params array or via the config file
dchill4226429202012-07-31 10:55:07 -0400196 $prefs = array(
197 'sess_encrypt_cookie',
198 'sess_use_database',
199 'sess_table_name',
200 'sess_expiration',
201 'sess_expire_on_close',
202 'sess_match_ip',
203 'sess_match_useragent',
204 'sess_cookie_name',
205 'cookie_path',
206 'cookie_domain',
207 'cookie_secure',
208 'cookie_httponly',
209 'sess_time_to_update',
210 'time_reference',
211 'cookie_prefix',
212 'encryption_key'
213 );
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300214
dchill4226429202012-07-31 10:55:07 -0400215 foreach ($prefs as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400216 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300217 $this->$key = isset($this->_parent->params[$key])
218 ? $this->_parent->params[$key]
219 : $this->CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400220 }
221
Andrey Andreev4abd0942012-11-26 12:13:59 +0200222 if (empty($this->encryption_key))
Darren Hillc4e266b2011-08-30 15:40:27 -0400223 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300224 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 -0400225 }
226
Darren Hillc4e266b2011-08-30 15:40:27 -0400227 // Do we need encryption? If so, load the encryption class
Alex Bilbied261b1e2012-06-02 11:12:16 +0100228 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400229 {
230 $this->CI->load->library('encrypt');
231 }
232
dchill423cecd822012-08-28 21:37:27 -0400233 // Check for database
Alex Bilbied261b1e2012-06-02 11:12:16 +0100234 if ($this->sess_use_database === TRUE && $this->sess_table_name !== '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400235 {
dchill423cecd822012-08-28 21:37:27 -0400236 // Load database driver
Darren Hillc4e266b2011-08-30 15:40:27 -0400237 $this->CI->load->database();
dchill423cecd822012-08-28 21:37:27 -0400238
239 // Register shutdown function
240 register_shutdown_function(array($this, '_update_db'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400241 }
242
243 // Set the "now" time. Can either be GMT or server time, based on the config prefs.
244 // We use this to set the "last activity" time
245 $this->now = $this->_get_time();
246
247 // Set the session length. If the session expiration is
248 // set to zero we'll set the expiration two years from now.
Alex Bilbied261b1e2012-06-02 11:12:16 +0100249 if ($this->sess_expiration === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400250 {
251 $this->sess_expiration = (60*60*24*365*2);
252 }
253
254 // Set the cookie name
255 $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
256
257 // Run the Session routine. If a session doesn't exist we'll
258 // create a new one. If it does, we'll update it.
259 if ( ! $this->_sess_read())
260 {
261 $this->_sess_create();
262 }
263 else
264 {
265 $this->_sess_update();
266 }
267
268 // Delete expired sessions if necessary
269 $this->_sess_gc();
270 }
271
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300272 // ------------------------------------------------------------------------
273
Darren Hillc4e266b2011-08-30 15:40:27 -0400274 /**
275 * Write the session data
276 *
277 * @return void
278 */
279 public function sess_save()
280 {
dchill423cecd822012-08-28 21:37:27 -0400281 // Check for database
dchill4288b636b2012-08-29 08:47:05 -0400282 if ($this->sess_use_database === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400283 {
dchill423cecd822012-08-28 21:37:27 -0400284 // Mark custom data as dirty so we know to update the DB
285 $this->data_dirty = TRUE;
Darren Hillc4e266b2011-08-30 15:40:27 -0400286 }
287
dchill423cecd822012-08-28 21:37:27 -0400288 // Write the cookie
289 $this->_set_cookie();
Darren Hillc4e266b2011-08-30 15:40:27 -0400290 }
291
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300292 // ------------------------------------------------------------------------
293
Darren Hillc4e266b2011-08-30 15:40:27 -0400294 /**
295 * Destroy the current session
296 *
297 * @return void
298 */
299 public function sess_destroy()
300 {
301 // Kill the session DB row
dchill42aee92652012-08-26 21:45:35 -0400302 if ($this->sess_use_database === TRUE && isset($this->userdata['session_id']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400303 {
dchill423cecd822012-08-28 21:37:27 -0400304 $this->CI->db->delete($this->sess_table_name, array('session_id' => $this->userdata['session_id']));
dchill4297b0d832012-09-04 10:09:00 -0400305 $this->data_dirty = FALSE;
Darren Hillc4e266b2011-08-30 15:40:27 -0400306 }
307
308 // Kill the cookie
Andrey Andreevcf264e02012-10-18 16:14:51 +0300309 $this->_setcookie($this->sess_cookie_name, '', ($this->now - 31500000),
Darren Hillc4e266b2011-08-30 15:40:27 -0400310 $this->cookie_path, $this->cookie_domain, 0);
dchill42c5079de2012-07-23 10:53:47 -0400311
312 // Kill session data
313 $this->userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -0400314 }
315
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300316 // ------------------------------------------------------------------------
317
Darren Hillc4e266b2011-08-30 15:40:27 -0400318 /**
319 * Regenerate the current session
320 *
321 * Regenerate the session id
322 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300323 * @param bool Destroy session data flag (default: false)
Darren Hillc4e266b2011-08-30 15:40:27 -0400324 * @return void
325 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300326 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400327 {
328 // Check destroy flag
329 if ($destroy)
330 {
331 // Destroy old session and create new one
332 $this->sess_destroy();
333 $this->_sess_create();
334 }
335 else
336 {
337 // Just force an update to recreate the id
Andrey Andreev3f3f1352012-09-05 16:39:28 +0300338 $this->_sess_update(TRUE);
Darren Hillc4e266b2011-08-30 15:40:27 -0400339 }
340 }
341
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300342 // ------------------------------------------------------------------------
343
Darren Hillc4e266b2011-08-30 15:40:27 -0400344 /**
345 * Get a reference to user data array
346 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300347 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400348 */
349 public function &get_userdata()
350 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400351 return $this->userdata;
352 }
353
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300354 // ------------------------------------------------------------------------
355
Darren Hillc4e266b2011-08-30 15:40:27 -0400356 /**
357 * Fetch the current session data if it exists
358 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400359 * @return bool
360 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400361 protected function _sess_read()
Darren Hillc4e266b2011-08-30 15:40:27 -0400362 {
363 // Fetch the cookie
364 $session = $this->CI->input->cookie($this->sess_cookie_name);
365
366 // No cookie? Goodbye cruel world!...
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100367 if ($session === NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400368 {
369 log_message('debug', 'A session cookie was not found.');
370 return FALSE;
371 }
372
Pascal Krietef69f0e82012-10-16 11:54:49 -0400373 $len = strlen($session) - 40;
374
375 if ($len < 0)
376 {
377 log_message('debug', 'The session cookie was not signed.');
378 return FALSE;
379 }
380
381 // Check cookie authentication
382 $hmac = substr($session, $len);
383 $session = substr($session, 0, $len);
384
385 if ($hmac !== hash_hmac('sha1', $session, $this->encryption_key))
386 {
Pascal Kriete28dc2022012-10-17 11:27:29 -0400387 log_message('error', 'The session cookie data did not match what was expected.');
Pascal Krietef69f0e82012-10-16 11:54:49 -0400388 $this->sess_destroy();
389 return FALSE;
390 }
391
dchill423cecd822012-08-28 21:37:27 -0400392 // Check for encryption
Alex Bilbied261b1e2012-06-02 11:12:16 +0100393 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400394 {
dchill423cecd822012-08-28 21:37:27 -0400395 // Decrypt the cookie data
Darren Hillc4e266b2011-08-30 15:40:27 -0400396 $session = $this->CI->encrypt->decode($session);
397 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400398
399 // Unserialize the session array
400 $session = $this->_unserialize($session);
401
402 // Is the session data we unserialized an array with the correct format?
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300403 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 -0400404 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300405 log_message('debug', 'Session: Wrong cookie data format');
Darren Hillc4e266b2011-08-30 15:40:27 -0400406 $this->sess_destroy();
407 return FALSE;
408 }
409
410 // Is the session current?
Andrey Andreev02117682012-10-15 11:12:37 +0300411 if (($session['last_activity'] + $this->sess_expiration) < $this->now OR $session['last_activity'] > $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400412 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300413 log_message('debug', 'Session: Expired');
Darren Hillc4e266b2011-08-30 15:40:27 -0400414 $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 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300421 log_message('debug', 'Session: IP address mismatch');
Darren Hillc4e266b2011-08-30 15:40:27 -0400422 $this->sess_destroy();
423 return FALSE;
424 }
425
426 // Does the User Agent Match?
dchill42c5079de2012-07-23 10:53:47 -0400427 if ($this->sess_match_useragent === TRUE &&
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300428 trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400429 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300430 log_message('debug', 'Session: User Agent string mismatch');
Darren Hillc4e266b2011-08-30 15:40:27 -0400431 $this->sess_destroy();
432 return FALSE;
433 }
434
435 // Is there a corresponding session in the DB?
436 if ($this->sess_use_database === TRUE)
437 {
438 $this->CI->db->where('session_id', $session['session_id']);
439
Alex Bilbied261b1e2012-06-02 11:12:16 +0100440 if ($this->sess_match_ip === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400441 {
442 $this->CI->db->where('ip_address', $session['ip_address']);
443 }
444
Alex Bilbied261b1e2012-06-02 11:12:16 +0100445 if ($this->sess_match_useragent === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400446 {
447 $this->CI->db->where('user_agent', $session['user_agent']);
448 }
449
dchill42cd436e92012-09-04 10:15:14 -0400450 // Is caching in effect? Turn it off
451 $db_cache = $this->CI->db->cache_on;
452 $this->CI->db->cache_off();
453
Timothy Warrenf1421922012-03-02 11:51:42 -0500454 $query = $this->CI->db->limit(1)->get($this->sess_table_name);
Darren Hillc4e266b2011-08-30 15:40:27 -0400455
dchill42cd436e92012-09-04 10:15:14 -0400456 // Was caching in effect?
457 if ($db_cache)
458 {
459 // Turn it back on
460 $this->CI->db->cache_on();
461 }
462
Darren Hillc4e266b2011-08-30 15:40:27 -0400463 // No result? Kill it!
Andrey Andreeva8e34ac2012-12-17 10:39:32 +0200464 if (empty($query) OR $query->num_rows() === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400465 {
Andrey Andreeve18de502013-07-17 19:59:20 +0300466 log_message('debug', 'Session: No match found in our database');
Darren Hillc4e266b2011-08-30 15:40:27 -0400467 $this->sess_destroy();
468 return FALSE;
469 }
470
471 // Is there custom data? If so, add it to the main session array
472 $row = $query->row();
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300473 if ( ! empty($row->user_data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400474 {
475 $custom_data = $this->_unserialize($row->user_data);
476
477 if (is_array($custom_data))
478 {
dchill423cecd822012-08-28 21:37:27 -0400479 $session = $session + $custom_data;
Darren Hillc4e266b2011-08-30 15:40:27 -0400480 }
481 }
482 }
483
484 // Session is valid!
485 $this->userdata = $session;
Darren Hillc4e266b2011-08-30 15:40:27 -0400486 return TRUE;
487 }
488
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300489 // ------------------------------------------------------------------------
490
Darren Hillc4e266b2011-08-30 15:40:27 -0400491 /**
492 * Create a new session
493 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400494 * @return void
495 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400496 protected function _sess_create()
Darren Hillc4e266b2011-08-30 15:40:27 -0400497 {
dchill423cecd822012-08-28 21:37:27 -0400498 // Initialize userdata
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 $this->userdata = array(
dchill423cecd822012-08-28 21:37:27 -0400500 'session_id' => $this->_make_sess_id(),
dchill42c5079de2012-07-23 10:53:47 -0400501 'ip_address' => $this->CI->input->ip_address(),
Daniel Robbins930d8ef2013-03-01 21:36:48 -0500502 'user_agent' => trim(substr($this->CI->input->user_agent(), 0, 120)),
dchill42c5079de2012-07-23 10:53:47 -0400503 'last_activity' => $this->now,
dchill42c5079de2012-07-23 10:53:47 -0400504 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400505
Andrey Andreeve18de502013-07-17 19:59:20 +0300506 log_message('debug', 'Session: Creating new session ('.$this->userdata['session_id'].')');
507
dchill423cecd822012-08-28 21:37:27 -0400508 // Check for database
Darren Hillc4e266b2011-08-30 15:40:27 -0400509 if ($this->sess_use_database === TRUE)
510 {
dchill423cecd822012-08-28 21:37:27 -0400511 // Add empty user_data field and save the data to the DB
512 $this->CI->db->set('user_data', '')->insert($this->sess_table_name, $this->userdata);
Darren Hillc4e266b2011-08-30 15:40:27 -0400513 }
514
515 // Write the cookie
516 $this->_set_cookie();
517 }
518
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300519 // ------------------------------------------------------------------------
520
Darren Hillc4e266b2011-08-30 15:40:27 -0400521 /**
522 * Update an existing session
523 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300524 * @param bool Force update flag (default: false)
Darren Hillc4e266b2011-08-30 15:40:27 -0400525 * @return void
526 */
dchill42c5079de2012-07-23 10:53:47 -0400527 protected function _sess_update($force = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400528 {
529 // We only update the session every five minutes by default (unless forced)
dchill4277ee3fd2012-07-24 11:50:01 -0400530 if ( ! $force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400531 {
532 return;
533 }
534
dchill423cecd822012-08-28 21:37:27 -0400535 // Update last activity to now
536 $this->userdata['last_activity'] = $this->now;
Andrey Andreev9c622f32012-01-19 14:12:54 +0200537
dchill423cecd822012-08-28 21:37:27 -0400538 // Save the old session id so we know which DB record to update
539 $old_sessid = $this->userdata['session_id'];
540
541 // Changing the session ID during an AJAX call causes problems
542 if ( ! $this->CI->input->is_ajax_request())
Andrey Andreev9c622f32012-01-19 14:12:54 +0200543 {
dchill423cecd822012-08-28 21:37:27 -0400544 // Get new id
545 $this->userdata['session_id'] = $this->_make_sess_id();
Andrey Andreeve18de502013-07-17 19:59:20 +0300546
547 log_message('debug', 'Session: Regenerate ID');
Andrey Andreev9c622f32012-01-19 14:12:54 +0200548 }
549
dchill423cecd822012-08-28 21:37:27 -0400550 // Check for database
551 if ($this->sess_use_database === TRUE)
552 {
Andrey Andreeve2afc882012-11-01 01:35:34 +0200553 $this->CI->db->where('session_id', $old_sessid);
554
555 if ($this->sess_match_ip === TRUE)
556 {
557 $this->CI->db->where('ip_address', $this->CI->input->ip_address());
558 }
559
560 if ($this->sess_match_useragent === TRUE)
561 {
562 $this->CI->db->where('user_agent', trim(substr($this->CI->input->user_agent(), 0, 120)));
563 }
564
dchill423cecd822012-08-28 21:37:27 -0400565 // Update the session ID and last_activity field in the DB
Andrey Andreeve2afc882012-11-01 01:35:34 +0200566 $this->CI->db->update($this->sess_table_name,
567 array(
568 'last_activity' => $this->now,
569 'session_id' => $this->userdata['session_id']
570 )
571 );
dchill423cecd822012-08-28 21:37:27 -0400572 }
573
574 // Write the cookie
575 $this->_set_cookie();
576 }
577
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300578 // ------------------------------------------------------------------------
579
dchill423cecd822012-08-28 21:37:27 -0400580 /**
581 * Update database with current data
582 *
583 * This gets called from the shutdown function and also
584 * registered with PHP to run at the end of the request
585 * so it's guaranteed to update even when a fatal error
586 * occurs. The first call makes the update and clears the
587 * dirty flag so it won't happen twice.
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300588 *
589 * @return void
dchill423cecd822012-08-28 21:37:27 -0400590 */
591 public function _update_db()
592 {
593 // Check for database and dirty flag and unsaved
594 if ($this->sess_use_database === TRUE && $this->data_dirty === TRUE)
595 {
596 // Set up activity and data fields to be set
597 // If we don't find custom data, user_data will remain an empty string
598 $set = array(
599 'last_activity' => $this->userdata['last_activity'],
600 'user_data' => ''
601 );
602
603 // Get the custom userdata, leaving out the defaults
604 // (which get stored in the cookie)
605 $userdata = array_diff_key($this->userdata, $this->defaults);
606
607 // Did we find any custom data?
608 if ( ! empty($userdata))
609 {
610 // Serialize the custom data array so we can store it
611 $set['user_data'] = $this->_serialize($userdata);
612 }
613
Dionysis Arvanitisf8e2d0e2013-02-19 23:27:16 +0200614 // Reset query builder values.
615 $this->CI->db->reset_query();
616
dchill423cecd822012-08-28 21:37:27 -0400617 // Run the update query
618 // Any time we change the session id, it gets updated immediately,
619 // so our where clause below is always safe
Andrey Andreeve2afc882012-11-01 01:35:34 +0200620 $this->CI->db->where('session_id', $this->userdata['session_id']);
621
622 if ($this->sess_match_ip === TRUE)
623 {
624 $this->CI->db->where('ip_address', $this->CI->input->ip_address());
625 }
626
627 if ($this->sess_match_useragent === TRUE)
628 {
629 $this->CI->db->where('user_agent', trim(substr($this->CI->input->user_agent(), 0, 120)));
630 }
631
632 $this->CI->db->update($this->sess_table_name, $set);
dchill423cecd822012-08-28 21:37:27 -0400633
634 // Clear dirty flag to prevent double updates
635 $this->data_dirty = FALSE;
636
637 log_message('debug', 'CI_Session Data Saved To DB');
638 }
639 }
640
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300641 // ------------------------------------------------------------------------
642
dchill423cecd822012-08-28 21:37:27 -0400643 /**
644 * Generate a new session id
645 *
646 * @return string Hashed session id
647 */
648 protected function _make_sess_id()
649 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400650 $new_sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200651 do
Darren Hillc4e266b2011-08-30 15:40:27 -0400652 {
vlakoff06127562013-03-30 00:06:39 +0100653 $new_sessid .= mt_rand();
Darren Hillc4e266b2011-08-30 15:40:27 -0400654 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200655 while (strlen($new_sessid) < 32);
Darren Hillc4e266b2011-08-30 15:40:27 -0400656
657 // To make the session ID even more secure we'll combine it with the user's IP
658 $new_sessid .= $this->CI->input->ip_address();
659
dchill423cecd822012-08-28 21:37:27 -0400660 // Turn it into a hash and return
661 return md5(uniqid($new_sessid, TRUE));
Darren Hillc4e266b2011-08-30 15:40:27 -0400662 }
663
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300664 // ------------------------------------------------------------------------
665
Darren Hillc4e266b2011-08-30 15:40:27 -0400666 /**
667 * Get the "now" time
668 *
dchill42c5079de2012-07-23 10:53:47 -0400669 * @return int Time
Darren Hillc4e266b2011-08-30 15:40:27 -0400670 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400671 protected function _get_time()
Darren Hillc4e266b2011-08-30 15:40:27 -0400672 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300673 if ($this->time_reference === 'local' OR $this->time_reference === date_default_timezone_get())
Darren Hillc4e266b2011-08-30 15:40:27 -0400674 {
Iban Eguiafeb14da2012-06-12 16:09:36 +0200675 return time();
Darren Hillc4e266b2011-08-30 15:40:27 -0400676 }
677
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300678 $datetime = new DateTime('now', new DateTimeZone($this->time_reference));
Iban Eguiafeb14da2012-06-12 16:09:36 +0200679 sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
680
681 return mktime($hour, $minute, $second, $month, $day, $year);
Darren Hillc4e266b2011-08-30 15:40:27 -0400682 }
683
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300684 // ------------------------------------------------------------------------
685
Darren Hillc4e266b2011-08-30 15:40:27 -0400686 /**
687 * Write the session cookie
688 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400689 * @return void
690 */
dchill423cecd822012-08-28 21:37:27 -0400691 protected function _set_cookie()
Darren Hillc4e266b2011-08-30 15:40:27 -0400692 {
dchill423cecd822012-08-28 21:37:27 -0400693 // Get userdata (only defaults if database)
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300694 $cookie_data = ($this->sess_use_database === TRUE)
695 ? array_intersect_key($this->userdata, $this->defaults)
696 : $this->userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400697
698 // Serialize the userdata for the cookie
699 $cookie_data = $this->_serialize($cookie_data);
700
Pascal Krietef69f0e82012-10-16 11:54:49 -0400701 if ($this->sess_encrypt_cookie === TRUE)
702 {
Andrey Andreevcf264e02012-10-18 16:14:51 +0300703 $cookie_data = $this->CI->encrypt->encode($cookie_data);
Pascal Krietef69f0e82012-10-16 11:54:49 -0400704 }
705
706 // Require message authentication
707 $cookie_data .= hash_hmac('sha1', $cookie_data, $this->encryption_key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400708
709 $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
710
711 // Set the cookie
dchill42c5872252012-07-30 14:53:11 -0400712 $this->_setcookie($this->sess_cookie_name, $cookie_data, $expire, $this->cookie_path, $this->cookie_domain,
dchill42c5079de2012-07-23 10:53:47 -0400713 $this->cookie_secure, $this->cookie_httponly);
Darren Hillc4e266b2011-08-30 15:40:27 -0400714 }
715
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300716 // ------------------------------------------------------------------------
717
Darren Hillc4e266b2011-08-30 15:40:27 -0400718 /**
dchill42c5872252012-07-30 14:53:11 -0400719 * Set a cookie with the system
720 *
721 * This abstraction of the setcookie call allows overriding for unit testing
722 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300723 * @param string Cookie name
724 * @param string Cookie value
725 * @param int Expiration time
726 * @param string Cookie path
727 * @param string Cookie domain
728 * @param bool Secure connection flag
729 * @param bool HTTP protocol only flag
730 * @return void
dchill42c5872252012-07-30 14:53:11 -0400731 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300732 protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = FALSE, $httponly = FALSE)
dchill42c5872252012-07-30 14:53:11 -0400733 {
dchill42c5872252012-07-30 14:53:11 -0400734 setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
735 }
736
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300737 // ------------------------------------------------------------------------
738
dchill42c5872252012-07-30 14:53:11 -0400739 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400740 * Serialize an array
741 *
742 * This function first converts any slashes found in the array to a temporary
743 * marker, so when it gets unserialized the slashes will be preserved
744 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400745 * @param mixed Data to serialize
dchill42c5079de2012-07-23 10:53:47 -0400746 * @return string Serialized data
Darren Hillc4e266b2011-08-30 15:40:27 -0400747 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400748 protected function _serialize($data)
Darren Hillc4e266b2011-08-30 15:40:27 -0400749 {
750 if (is_array($data))
751 {
Chris Muench95933492011-10-16 14:14:04 -0400752 array_walk_recursive($data, array(&$this, '_escape_slashes'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400753 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200754 elseif (is_string($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400755 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200756 $data = str_replace('\\', '{{slash}}', $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400757 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300758
Darren Hillc4e266b2011-08-30 15:40:27 -0400759 return serialize($data);
760 }
761
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300762 // ------------------------------------------------------------------------
763
Darren Hillc4e266b2011-08-30 15:40:27 -0400764 /**
Chris Muench95933492011-10-16 14:14:04 -0400765 * Escape slashes
766 *
767 * This function converts any slashes found into a temporary marker
768 *
dchill42c5079de2012-07-23 10:53:47 -0400769 * @param string Value
770 * @param string Key
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200771 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400772 */
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200773 protected function _escape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400774 {
775 if (is_string($val))
776 {
777 $val = str_replace('\\', '{{slash}}', $val);
778 }
779 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000780
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300781 // ------------------------------------------------------------------------
782
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400784 * Unserialize
785 *
786 * This function unserializes a data string, then converts any
787 * temporary slash markers back to actual slashes
788 *
dchill42c5079de2012-07-23 10:53:47 -0400789 * @param mixed Data to unserialize
790 * @return mixed Unserialized data
Darren Hillc4e266b2011-08-30 15:40:27 -0400791 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400792 protected function _unserialize($data)
Darren Hillc4e266b2011-08-30 15:40:27 -0400793 {
Andrey Andreevca20d842012-10-27 03:02:38 +0300794 $data = @unserialize(trim($data));
Darren Hillc4e266b2011-08-30 15:40:27 -0400795
796 if (is_array($data))
797 {
Chris Muench95933492011-10-16 14:14:04 -0400798 array_walk_recursive($data, array(&$this, '_unescape_slashes'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400799 return $data;
800 }
801
Andrey Andreev6b831232012-03-06 11:16:57 +0200802 return is_string($data) ? str_replace('{{slash}}', '\\', $data) : $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200804
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300805 // ------------------------------------------------------------------------
806
Chris Muench95933492011-10-16 14:14:04 -0400807 /**
808 * Unescape slashes
809 *
810 * This function converts any slash markers back into actual slashes
811 *
dchill42c5079de2012-07-23 10:53:47 -0400812 * @param string Value
813 * @param string Key
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200814 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400815 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200816 protected function _unescape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400817 {
Chris Muench3e414f92011-10-16 23:03:55 -0400818 if (is_string($val))
819 {
Andrey Andreevcc221dc2013-02-08 21:57:42 +0200820 $val = str_replace('{{slash}}', '\\', $val);
Chris Muench3e414f92011-10-16 23:03:55 -0400821 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400822 }
823
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300824 // ------------------------------------------------------------------------
825
Darren Hillc4e266b2011-08-30 15:40:27 -0400826 /**
827 * Garbage collection
828 *
829 * This deletes expired session rows from database
830 * if the probability percentage is met
831 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400832 * @return void
833 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400834 protected function _sess_gc()
Darren Hillc4e266b2011-08-30 15:40:27 -0400835 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100836 if ($this->sess_use_database !== TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400837 {
838 return;
839 }
840
Christopher Guiney7a142862012-06-29 20:34:28 -0700841 $probability = ini_get('session.gc_probability');
842 $divisor = ini_get('session.gc_divisor');
843
Christopher Guiney7a142862012-06-29 20:34:28 -0700844 if ((mt_rand(0, $divisor) / $divisor) < $probability)
Darren Hillc4e266b2011-08-30 15:40:27 -0400845 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 $expire = $this->now - $this->sess_expiration;
dchill423cecd822012-08-28 21:37:27 -0400847 $this->CI->db->delete($this->sess_table_name, 'last_activity < '.$expire);
Darren Hillc4e266b2011-08-30 15:40:27 -0400848
849 log_message('debug', 'Session garbage collection performed.');
850 }
851 }
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300852
Darren Hillc4e266b2011-08-30 15:40:27 -0400853}
Darren Hillc4e266b2011-08-30 15:40:27 -0400854
855/* End of file Session_cookie.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300856/* Location: ./system/libraries/Session/drivers/Session_cookie.php */