blob: 19ccd417d6e9c738a83df6ef17f972340d8d19a0 [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 *
31 * This is the CI_Session functionality, as written by EllisLab, abstracted out to a driver.
32 * I have done a little updating for PHP5, and made minor changes to extract this functionality from
33 * the public interface (now in the Session Library), but effectively this code is unchanged.
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category Sessions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/sessions.html
Darren Hillc4e266b2011-08-30 15:40:27 -040040 */
Darren Hill5073a372011-08-31 13:54:19 -040041class CI_Session_cookie extends CI_Session_driver {
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 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020075 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 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020089 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 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200110 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 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200159 public $userdata = array();
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200160
Timothy Warren68f09812012-04-27 10:38:32 -0400161 /**
162 * Reference to CodeIgniter instance
163 *
164 * @var object
165 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200166 public $CI;
Michiel Vugteveen4c316b62012-05-04 11:32:48 +0200167
Timothy Warren68f09812012-04-27 10:38:32 -0400168 /**
169 * Current time
170 *
171 * @var int
172 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200173 public $now;
Darren Hillc4e266b2011-08-30 15:40:27 -0400174
175 /**
176 * Initialize session driver object
177 *
178 * @access protected
179 * @return void
180 */
181 protected function initialize()
182 {
183 // Set the super object to a local variable for use throughout the class
184 $this->CI =& get_instance();
185
186 // Set all the session preferences, which can either be set
dchill4242b77a92012-07-23 11:28:42 -0400187 // manually via the $params array or via the config file
Darren Hillc4e266b2011-08-30 15:40:27 -0400188 foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration',
189 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path',
dchill42c5079de2012-07-23 10:53:47 -0400190 'cookie_domain', 'cookie_secure', 'cookie_httponly', 'sess_time_to_update', 'time_reference', 'cookie_prefix',
191 'encryption_key') as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400192 {
dchill42c5872252012-07-30 14:53:11 -0400193 $this->$key = isset($this->_parent->params[$key]) ? $this->_parent->params[$key] :
dchill42c5079de2012-07-23 10:53:47 -0400194 $this->CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400195 }
196
Alex Bilbied261b1e2012-06-02 11:12:16 +0100197 if ($this->encryption_key === '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400198 {
dchill42c5872252012-07-30 14:53:11 -0400199 show_error('In order to use the Cookie Session driver you are required to set an encryption key '.
Darren Hillc4e266b2011-08-30 15:40:27 -0400200 'in your config file.');
201 }
202
203 // Load the string helper so we can use the strip_slashes() function
204 $this->CI->load->helper('string');
205
206 // Do we need encryption? If so, load the encryption class
Alex Bilbied261b1e2012-06-02 11:12:16 +0100207 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400208 {
209 $this->CI->load->library('encrypt');
210 }
211
212 // Are we using a database? If so, load it
Alex Bilbied261b1e2012-06-02 11:12:16 +0100213 if ($this->sess_use_database === TRUE && $this->sess_table_name !== '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400214 {
215 $this->CI->load->database();
216 }
217
218 // Set the "now" time. Can either be GMT or server time, based on the config prefs.
219 // We use this to set the "last activity" time
220 $this->now = $this->_get_time();
221
222 // Set the session length. If the session expiration is
223 // set to zero we'll set the expiration two years from now.
Alex Bilbied261b1e2012-06-02 11:12:16 +0100224 if ($this->sess_expiration === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400225 {
226 $this->sess_expiration = (60*60*24*365*2);
227 }
228
229 // Set the cookie name
230 $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
231
232 // Run the Session routine. If a session doesn't exist we'll
233 // create a new one. If it does, we'll update it.
234 if ( ! $this->_sess_read())
235 {
236 $this->_sess_create();
237 }
238 else
239 {
240 $this->_sess_update();
241 }
242
243 // Delete expired sessions if necessary
244 $this->_sess_gc();
245 }
246
247 /**
248 * Write the session data
249 *
250 * @return void
251 */
252 public function sess_save()
253 {
254 // Are we saving custom data to the DB? If not, all we do is update the cookie
255 if ($this->sess_use_database === FALSE)
256 {
257 $this->_set_cookie();
258 return;
259 }
260
261 // set the custom userdata, the session data we will set in a second
262 $custom_userdata = $this->all_userdata();
263 $cookie_userdata = array();
264
265 // Before continuing, we need to determine if there is any custom data to deal with.
266 // Let's determine this by removing the default indexes to see if there's anything left in the array
267 // and set the session data while we're at it
268 foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
269 {
270 unset($custom_userdata[$val]);
dchill42c5079de2012-07-23 10:53:47 -0400271 $cookie_userdata[$val] = $this->userdata[$val];
Darren Hillc4e266b2011-08-30 15:40:27 -0400272 }
273
274 // Did we find any custom data? If not, we turn the empty array into a string
275 // since there's no reason to serialize and store an empty array in the DB
276 if (count($custom_userdata) === 0)
277 {
278 $custom_userdata = '';
279 }
280 else
281 {
282 // Serialize the custom data array so we can store it
283 $custom_userdata = $this->_serialize($custom_userdata);
284 }
285
286 // Run the update query
dchill42c5079de2012-07-23 10:53:47 -0400287 $this->CI->db->where('session_id', $this->userdata['session_id']);
Darren Hillc4e266b2011-08-30 15:40:27 -0400288 $this->CI->db->update($this->sess_table_name,
dchill42c5079de2012-07-23 10:53:47 -0400289 array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
Darren Hillc4e266b2011-08-30 15:40:27 -0400290
291 // Write the cookie. Notice that we manually pass the cookie data array to the
292 // _set_cookie() function. Normally that function will store $this->userdata, but
293 // in this case that array contains custom data, which we do not want in the cookie.
294 $this->_set_cookie($cookie_userdata);
295 }
296
297 /**
298 * Destroy the current session
299 *
300 * @return void
301 */
302 public function sess_destroy()
303 {
304 // Kill the session DB row
305 if ($this->sess_use_database === TRUE && $this->has_userdata('session_id'))
306 {
307 $this->CI->db->where('session_id', $this->userdata['session_id']);
308 $this->CI->db->delete($this->sess_table_name);
309 }
310
311 // Kill the cookie
dchill42c5872252012-07-30 14:53:11 -0400312 $this->_setcookie($this->sess_cookie_name, addslashes(serialize(array())), ($this->now - 31500000),
Darren Hillc4e266b2011-08-30 15:40:27 -0400313 $this->cookie_path, $this->cookie_domain, 0);
dchill42c5079de2012-07-23 10:53:47 -0400314
315 // Kill session data
316 $this->userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -0400317 }
318
319 /**
320 * Regenerate the current session
321 *
322 * Regenerate the session id
323 *
324 * @param boolean Destroy session data flag (default: false)
325 * @return void
326 */
327 public function sess_regenerate($destroy = false)
328 {
329 // Check destroy flag
330 if ($destroy)
331 {
332 // Destroy old session and create new one
333 $this->sess_destroy();
334 $this->_sess_create();
335 }
336 else
337 {
338 // Just force an update to recreate the id
339 $this->_sess_update(true);
340 }
341 }
342
343 /**
344 * Get a reference to user data array
345 *
346 * @return array - Reference to userdata
347 */
348 public function &get_userdata()
349 {
350 // Return reference to array
351 return $this->userdata;
352 }
353
354 /**
355 * Fetch the current session data if it exists
356 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400357 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400358 * @return bool
359 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400360 protected function _sess_read()
Darren Hillc4e266b2011-08-30 15:40:27 -0400361 {
362 // Fetch the cookie
363 $session = $this->CI->input->cookie($this->sess_cookie_name);
364
365 // No cookie? Goodbye cruel world!...
Phil Sturgeon55a6ddb2012-05-23 18:37:24 +0100366 if ($session === NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400367 {
368 log_message('debug', 'A session cookie was not found.');
369 return FALSE;
370 }
371
372 // Decrypt the cookie data
Alex Bilbied261b1e2012-06-02 11:12:16 +0100373 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400374 {
375 $session = $this->CI->encrypt->decode($session);
376 }
377 else
378 {
379 // encryption was not used, so we need to check the md5 hash
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 $hash = substr($session, strlen($session)-32); // get last 32 chars
Darren Hillc4e266b2011-08-30 15:40:27 -0400381 $session = substr($session, 0, strlen($session)-32);
382
383 // Does the md5 hash match? This is to prevent manipulation of session data in userspace
384 if ($hash !== md5($session.$this->encryption_key))
385 {
386 log_message('error', 'The session cookie data did not match what was expected. '.
387 'This could be a possible hacking attempt.');
388 $this->sess_destroy();
389 return FALSE;
390 }
391 }
392
393 // Unserialize the session array
394 $session = $this->_unserialize($session);
395
396 // Is the session data we unserialized an array with the correct format?
dchill42c5079de2012-07-23 10:53:47 -0400397 if ( ! is_array($session) || ! isset($session['session_id'], $session['ip_address'], $session['user_agent'],
398 $session['last_activity']))
Darren Hillc4e266b2011-08-30 15:40:27 -0400399 {
400 $this->sess_destroy();
401 return FALSE;
402 }
403
404 // Is the session current?
Andrey Andreev7e087f52012-01-20 11:46:27 +0200405 if (($session['last_activity'] + $this->sess_expiration) < $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400406 {
407 $this->sess_destroy();
408 return FALSE;
409 }
410
Andrey Andreev7e087f52012-01-20 11:46:27 +0200411 // Does the IP match?
Alex Bilbied261b1e2012-06-02 11:12:16 +0100412 if ($this->sess_match_ip === TRUE && $session['ip_address'] !== $this->CI->input->ip_address())
Darren Hillc4e266b2011-08-30 15:40:27 -0400413 {
414 $this->sess_destroy();
415 return FALSE;
416 }
417
418 // Does the User Agent Match?
dchill42c5079de2012-07-23 10:53:47 -0400419 if ($this->sess_match_useragent === TRUE &&
420 trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400421 {
422 $this->sess_destroy();
423 return FALSE;
424 }
425
426 // Is there a corresponding session in the DB?
427 if ($this->sess_use_database === TRUE)
428 {
429 $this->CI->db->where('session_id', $session['session_id']);
430
Alex Bilbied261b1e2012-06-02 11:12:16 +0100431 if ($this->sess_match_ip === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400432 {
433 $this->CI->db->where('ip_address', $session['ip_address']);
434 }
435
Alex Bilbied261b1e2012-06-02 11:12:16 +0100436 if ($this->sess_match_useragent === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400437 {
438 $this->CI->db->where('user_agent', $session['user_agent']);
439 }
440
Timothy Warrenf1421922012-03-02 11:51:42 -0500441 $query = $this->CI->db->limit(1)->get($this->sess_table_name);
Darren Hillc4e266b2011-08-30 15:40:27 -0400442
443 // No result? Kill it!
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200444 if ($query->num_rows() === 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400445 {
446 $this->sess_destroy();
447 return FALSE;
448 }
449
450 // Is there custom data? If so, add it to the main session array
451 $row = $query->row();
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300452 if ( ! empty($row->user_data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400453 {
454 $custom_data = $this->_unserialize($row->user_data);
455
456 if (is_array($custom_data))
457 {
458 foreach ($custom_data as $key => $val)
459 {
460 $session[$key] = $val;
461 }
462 }
463 }
464 }
465
466 // Session is valid!
467 $this->userdata = $session;
468 unset($session);
469
470 return TRUE;
471 }
472
473 /**
474 * Create a new session
475 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400476 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400477 * @return void
478 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400479 protected function _sess_create()
Darren Hillc4e266b2011-08-30 15:40:27 -0400480 {
481 $sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200482 do
Darren Hillc4e266b2011-08-30 15:40:27 -0400483 {
484 $sessid .= mt_rand(0, mt_getrandmax());
485 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200486 while (strlen($sessid) < 32);
Darren Hillc4e266b2011-08-30 15:40:27 -0400487
488 // To make the session ID even more secure we'll combine it with the user's IP
489 $sessid .= $this->CI->input->ip_address();
490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 $this->userdata = array(
dchill42c5079de2012-07-23 10:53:47 -0400492 'session_id' => md5(uniqid($sessid, TRUE)),
493 'ip_address' => $this->CI->input->ip_address(),
494 'user_agent' => substr($this->CI->input->user_agent(), 0, 120),
495 'last_activity' => $this->now,
496 'user_data' => ''
497 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400498
499 // Save the data to the DB if needed
500 if ($this->sess_use_database === TRUE)
501 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
Darren Hillc4e266b2011-08-30 15:40:27 -0400503 }
504
505 // Write the cookie
506 $this->_set_cookie();
507 }
508
509 /**
510 * Update an existing session
511 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400512 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400513 * @param boolean Force update flag (default: false)
514 * @return void
515 */
dchill42c5079de2012-07-23 10:53:47 -0400516 protected function _sess_update($force = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400517 {
518 // We only update the session every five minutes by default (unless forced)
dchill4277ee3fd2012-07-24 11:50:01 -0400519 if ( ! $force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
Darren Hillc4e266b2011-08-30 15:40:27 -0400520 {
521 return;
522 }
523
Andrey Andreev9c622f32012-01-19 14:12:54 +0200524 // _set_cookie() will handle this for us if we aren't using database sessions
525 // by pushing all userdata to the cookie.
526 $cookie_data = NULL;
527
dchill4242b77a92012-07-23 11:28:42 -0400528 // Changing the session ID during an AJAX call causes problems, so we'll only update our last_activity
Andrey Andreev9c622f32012-01-19 14:12:54 +0200529 if ($this->CI->input->is_ajax_request())
530 {
531 $this->userdata['last_activity'] = $this->now;
532
533 // Update the session ID and last_activity field in the DB if needed
534 if ($this->sess_use_database === TRUE)
535 {
536 // set cookie explicitly to only have our session data
537 $cookie_data = array();
538 foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
539 {
540 $cookie_data[$val] = $this->userdata[$val];
541 }
542
543 $this->CI->db->query($this->CI->db->update_string($this->sess_table_name,
544 array('last_activity' => $this->userdata['last_activity']),
545 array('session_id' => $this->userdata['session_id'])));
546 }
547
548 return $this->_set_cookie($cookie_data);
549 }
550
Darren Hillc4e266b2011-08-30 15:40:27 -0400551 // Save the old session id so we know which record to
552 // update in the database if we need it
553 $old_sessid = $this->userdata['session_id'];
554 $new_sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200555 do
Darren Hillc4e266b2011-08-30 15:40:27 -0400556 {
557 $new_sessid .= mt_rand(0, mt_getrandmax());
558 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200559 while (strlen($new_sessid) < 32);
Darren Hillc4e266b2011-08-30 15:40:27 -0400560
561 // To make the session ID even more secure we'll combine it with the user's IP
562 $new_sessid .= $this->CI->input->ip_address();
563
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200564 // Turn it into a hash and update the session data array
565 $this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 $this->userdata['last_activity'] = $this->now;
Darren Hillc4e266b2011-08-30 15:40:27 -0400567
568 // Update the session ID and last_activity field in the DB if needed
569 if ($this->sess_use_database === TRUE)
570 {
571 // set cookie explicitly to only have our session data
572 $cookie_data = array();
573 foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
574 {
575 $cookie_data[$val] = $this->userdata[$val];
576 }
577
578 $this->CI->db->query($this->CI->db->update_string($this->sess_table_name,
dchill42c5079de2012-07-23 10:53:47 -0400579 array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid)));
Darren Hillc4e266b2011-08-30 15:40:27 -0400580 }
581
582 // Write the cookie
583 $this->_set_cookie($cookie_data);
584 }
585
586 /**
587 * Get the "now" time
588 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400589 * @access protected
dchill42c5079de2012-07-23 10:53:47 -0400590 * @return int Time
Darren Hillc4e266b2011-08-30 15:40:27 -0400591 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400592 protected function _get_time()
Darren Hillc4e266b2011-08-30 15:40:27 -0400593 {
dchill42c5079de2012-07-23 10:53:47 -0400594 if ($this->time_reference === 'local' || $this->time_reference === date_default_timezone_get())
Darren Hillc4e266b2011-08-30 15:40:27 -0400595 {
Iban Eguiafeb14da2012-06-12 16:09:36 +0200596 return time();
Darren Hillc4e266b2011-08-30 15:40:27 -0400597 }
598
Andrey Andreevd163e0b2012-06-14 03:09:53 +0300599 $datetime = new DateTime('now', new DateTimeZone($this->time_reference));
Iban Eguiafeb14da2012-06-12 16:09:36 +0200600 sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
601
602 return mktime($hour, $minute, $second, $month, $day, $year);
Darren Hillc4e266b2011-08-30 15:40:27 -0400603 }
604
605 /**
606 * Write the session cookie
607 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400608 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400609 * @param array Cookie name/value pairs
610 * @return void
611 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400612 protected function _set_cookie(array $cookie_data = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400613 {
614 if (is_null($cookie_data))
615 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 $cookie_data = $this->userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400617 }
618
619 // Serialize the userdata for the cookie
620 $cookie_data = $this->_serialize($cookie_data);
621
Alex Bilbied261b1e2012-06-02 11:12:16 +0100622 if ($this->sess_encrypt_cookie === TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400623 {
624 $cookie_data = $this->CI->encrypt->encode($cookie_data);
625 }
626 else
627 {
628 // if encryption is not used, we provide an md5 hash to prevent userside tampering
629 $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
630 }
631
632 $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
633
634 // Set the cookie
dchill42c5872252012-07-30 14:53:11 -0400635 $this->_setcookie($this->sess_cookie_name, $cookie_data, $expire, $this->cookie_path, $this->cookie_domain,
dchill42c5079de2012-07-23 10:53:47 -0400636 $this->cookie_secure, $this->cookie_httponly);
Darren Hillc4e266b2011-08-30 15:40:27 -0400637 }
638
639 /**
dchill42c5872252012-07-30 14:53:11 -0400640 * Set a cookie with the system
641 *
642 * This abstraction of the setcookie call allows overriding for unit testing
643 *
644 * @access protected
645 * @param string Cookie name
646 * @param string Cookie value
647 * @param int Expiration time
648 * @param string Cookie path
649 * @param string Cookie domain
650 * @param bool Secure connection flag
651 * @param bool HTTP protocol only flag
652 * @return void
653 */
654 protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = false,
655 $httponly = false)
656 {
657 // Set the cookie
658 setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
659 }
660
661 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400662 * Serialize an array
663 *
664 * This function first converts any slashes found in the array to a temporary
665 * marker, so when it gets unserialized the slashes will be preserved
666 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400667 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400668 * @param mixed Data to serialize
dchill42c5079de2012-07-23 10:53:47 -0400669 * @return string Serialized data
Darren Hillc4e266b2011-08-30 15:40:27 -0400670 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400671 protected function _serialize($data)
Darren Hillc4e266b2011-08-30 15:40:27 -0400672 {
673 if (is_array($data))
674 {
Chris Muench95933492011-10-16 14:14:04 -0400675 array_walk_recursive($data, array(&$this, '_escape_slashes'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400676 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200677 elseif (is_string($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400678 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200679 $data = str_replace('\\', '{{slash}}', $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400680 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400681 return serialize($data);
682 }
683
684 /**
Chris Muench95933492011-10-16 14:14:04 -0400685 * Escape slashes
686 *
687 * This function converts any slashes found into a temporary marker
688 *
dchill42c5079de2012-07-23 10:53:47 -0400689 * @access protected
690 * @param string Value
691 * @param string Key
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200692 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400693 */
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200694 protected function _escape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400695 {
696 if (is_string($val))
697 {
698 $val = str_replace('\\', '{{slash}}', $val);
699 }
700 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400703 * Unserialize
704 *
705 * This function unserializes a data string, then converts any
706 * temporary slash markers back to actual slashes
707 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400708 * @access protected
dchill42c5079de2012-07-23 10:53:47 -0400709 * @param mixed Data to unserialize
710 * @return mixed Unserialized data
Darren Hillc4e266b2011-08-30 15:40:27 -0400711 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400712 protected function _unserialize($data)
Darren Hillc4e266b2011-08-30 15:40:27 -0400713 {
Andrey Andreev6b831232012-03-06 11:16:57 +0200714 $data = @unserialize(strip_slashes(trim($data)));
Darren Hillc4e266b2011-08-30 15:40:27 -0400715
716 if (is_array($data))
717 {
Chris Muench95933492011-10-16 14:14:04 -0400718 array_walk_recursive($data, array(&$this, '_unescape_slashes'));
Darren Hillc4e266b2011-08-30 15:40:27 -0400719 return $data;
720 }
721
Andrey Andreev6b831232012-03-06 11:16:57 +0200722 return is_string($data) ? str_replace('{{slash}}', '\\', $data) : $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200724
Chris Muench95933492011-10-16 14:14:04 -0400725 /**
726 * Unescape slashes
727 *
728 * This function converts any slash markers back into actual slashes
729 *
dchill42c5079de2012-07-23 10:53:47 -0400730 * @access protected
731 * @param string Value
732 * @param string Key
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200733 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400734 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200735 protected function _unescape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400736 {
Chris Muench3e414f92011-10-16 23:03:55 -0400737 if (is_string($val))
738 {
739 $val= str_replace('{{slash}}', '\\', $val);
740 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400741 }
742
743 /**
744 * Garbage collection
745 *
746 * This deletes expired session rows from database
747 * if the probability percentage is met
748 *
Darren Hilla2ae6572011-09-01 07:36:26 -0400749 * @access protected
Darren Hillc4e266b2011-08-30 15:40:27 -0400750 * @return void
751 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400752 protected function _sess_gc()
Darren Hillc4e266b2011-08-30 15:40:27 -0400753 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100754 if ($this->sess_use_database !== TRUE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400755 {
756 return;
757 }
758
Christopher Guiney7a142862012-06-29 20:34:28 -0700759 $probability = ini_get('session.gc_probability');
760 $divisor = ini_get('session.gc_divisor');
761
Darren Hillc4e266b2011-08-30 15:40:27 -0400762 srand(time());
Christopher Guiney7a142862012-06-29 20:34:28 -0700763 if ((mt_rand(0, $divisor) / $divisor) < $probability)
Darren Hillc4e266b2011-08-30 15:40:27 -0400764 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 $expire = $this->now - $this->sess_expiration;
Darren Hillc4e266b2011-08-30 15:40:27 -0400766
767 $this->CI->db->where('last_activity < '.$expire);
768 $this->CI->db->delete($this->sess_table_name);
769
770 log_message('debug', 'Session garbage collection performed.');
771 }
772 }
773}
Darren Hillc4e266b2011-08-30 15:40:27 -0400774
775/* End of file Session_cookie.php */
Darren Hill5073a372011-08-31 13:54:19 -0400776/* Location: ./system/libraries/Session/drivers/Session_cookie.php */