| 12694 |
anikendra |
1 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP 5.1.6 or newer
|
|
|
6 |
*
|
|
|
7 |
* NOTICE OF LICENSE
|
|
|
8 |
*
|
|
|
9 |
* Licensed under the Open Software License version 3.0
|
|
|
10 |
*
|
|
|
11 |
* This source file is subject to the Open Software License (OSL 3.0) that is
|
|
|
12 |
* bundled with this package in the files license.txt / license.rst. It is
|
|
|
13 |
* 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.
|
|
|
18 |
*
|
|
|
19 |
* @package CodeIgniter
|
|
|
20 |
* @author EllisLab Dev Team
|
|
|
21 |
* @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
|
|
|
22 |
* @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
|
|
|
23 |
* @link http://codeigniter.com
|
|
|
24 |
* @since Version 1.0
|
|
|
25 |
* @filesource
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Session Class
|
|
|
30 |
*
|
|
|
31 |
* @package CodeIgniter
|
|
|
32 |
* @subpackage Libraries
|
|
|
33 |
* @category Sessions
|
|
|
34 |
* @author EllisLab Dev Team
|
|
|
35 |
* @link http://codeigniter.com/user_guide/libraries/sessions.html
|
|
|
36 |
*/
|
|
|
37 |
class CI_Session {
|
|
|
38 |
|
|
|
39 |
public $sess_encrypt_cookie = FALSE;
|
|
|
40 |
public $sess_use_database = FALSE;
|
|
|
41 |
public $sess_table_name = '';
|
|
|
42 |
public $sess_expiration = 7200;
|
|
|
43 |
public $sess_expire_on_close = FALSE;
|
|
|
44 |
public $sess_match_ip = FALSE;
|
|
|
45 |
public $sess_match_useragent = TRUE;
|
|
|
46 |
public $sess_cookie_name = 'ci_session';
|
|
|
47 |
public $cookie_prefix = '';
|
|
|
48 |
public $cookie_path = '';
|
|
|
49 |
public $cookie_domain = '';
|
|
|
50 |
public $cookie_secure = FALSE;
|
|
|
51 |
public $sess_time_to_update = 300;
|
|
|
52 |
public $encryption_key = '';
|
|
|
53 |
public $flashdata_key = 'flash';
|
|
|
54 |
public $time_reference = 'time';
|
|
|
55 |
public $gc_probability = 5;
|
|
|
56 |
public $userdata = array();
|
|
|
57 |
public $CI;
|
|
|
58 |
public $now;
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Session Constructor
|
|
|
62 |
*
|
|
|
63 |
* The constructor runs the session routines automatically
|
|
|
64 |
* whenever the class is instantiated.
|
|
|
65 |
*/
|
|
|
66 |
public function __construct($params = array())
|
|
|
67 |
{
|
|
|
68 |
log_message('debug', 'Session Class Initialized');
|
|
|
69 |
|
|
|
70 |
// Set the super object to a local variable for use throughout the class
|
|
|
71 |
$this->CI =& get_instance();
|
|
|
72 |
|
|
|
73 |
// Set all the session preferences, which can either be set
|
|
|
74 |
// manually via the $params array above or via the config file
|
|
|
75 |
foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'cookie_secure', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key)
|
|
|
76 |
{
|
|
|
77 |
$this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
if ($this->encryption_key == '')
|
|
|
81 |
{
|
|
|
82 |
show_error('In order to use the Session class you are required to set an encryption key in your config file.');
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
// Load the string helper so we can use the strip_slashes() function
|
|
|
86 |
$this->CI->load->helper('string');
|
|
|
87 |
|
|
|
88 |
// Do we need encryption? If so, load the encryption class
|
|
|
89 |
if ($this->sess_encrypt_cookie == TRUE)
|
|
|
90 |
{
|
|
|
91 |
$this->CI->load->library('encrypt');
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
// Are we using a database? If so, load it
|
|
|
95 |
if ($this->sess_use_database === TRUE && $this->sess_table_name != '')
|
|
|
96 |
{
|
|
|
97 |
$this->CI->load->database();
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
// Set the "now" time. Can either be GMT or server time, based on the
|
|
|
101 |
// config prefs. We use this to set the "last activity" time
|
|
|
102 |
$this->now = $this->_get_time();
|
|
|
103 |
|
|
|
104 |
// Set the session length. If the session expiration is
|
|
|
105 |
// set to zero we'll set the expiration two years from now.
|
|
|
106 |
if ($this->sess_expiration == 0)
|
|
|
107 |
{
|
|
|
108 |
$this->sess_expiration = (60*60*24*365*2);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
// Set the cookie name
|
|
|
112 |
$this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
|
|
|
113 |
|
|
|
114 |
// Run the Session routine. If a session doesn't exist we'll
|
|
|
115 |
// create a new one. If it does, we'll update it.
|
|
|
116 |
if ( ! $this->sess_read())
|
|
|
117 |
{
|
|
|
118 |
$this->sess_create();
|
|
|
119 |
}
|
|
|
120 |
else
|
|
|
121 |
{
|
|
|
122 |
$this->sess_update();
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
// Delete 'old' flashdata (from last request)
|
|
|
126 |
$this->_flashdata_sweep();
|
|
|
127 |
|
|
|
128 |
// Mark all new flashdata as old (data will be deleted before next request)
|
|
|
129 |
$this->_flashdata_mark();
|
|
|
130 |
|
|
|
131 |
// Delete expired sessions if necessary
|
|
|
132 |
$this->_sess_gc();
|
|
|
133 |
|
|
|
134 |
log_message('debug', 'Session routines successfully run');
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// --------------------------------------------------------------------
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Fetch the current session data if it exists
|
|
|
141 |
*
|
|
|
142 |
* @return bool
|
|
|
143 |
*/
|
|
|
144 |
public function sess_read()
|
|
|
145 |
{
|
|
|
146 |
// Fetch the cookie
|
|
|
147 |
$session = $this->CI->input->cookie($this->sess_cookie_name);
|
|
|
148 |
|
|
|
149 |
// No cookie? Goodbye cruel world!...
|
|
|
150 |
if ($session === FALSE)
|
|
|
151 |
{
|
|
|
152 |
log_message('debug', 'A session cookie was not found.');
|
|
|
153 |
return FALSE;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
// Decrypt the cookie data
|
|
|
157 |
if ($this->sess_encrypt_cookie == TRUE)
|
|
|
158 |
{
|
|
|
159 |
$session = $this->CI->encrypt->decode($session);
|
|
|
160 |
}
|
|
|
161 |
else
|
|
|
162 |
{
|
|
|
163 |
// encryption was not used, so we need to check the md5 hash
|
|
|
164 |
$hash = substr($session, strlen($session)-32); // get last 32 chars
|
|
|
165 |
$session = substr($session, 0, strlen($session)-32);
|
|
|
166 |
|
|
|
167 |
// Does the md5 hash match? This is to prevent manipulation of session data in userspace
|
|
|
168 |
if ($hash !== md5($session.$this->encryption_key))
|
|
|
169 |
{
|
|
|
170 |
log_message('error', 'The session cookie data did not match what was expected. This could be a possible hacking attempt.');
|
|
|
171 |
$this->sess_destroy();
|
|
|
172 |
return FALSE;
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
// Unserialize the session array
|
|
|
177 |
$session = $this->_unserialize($session);
|
|
|
178 |
|
|
|
179 |
// Is the session data we unserialized an array with the correct format?
|
|
|
180 |
if ( ! is_array($session) OR ! isset($session['session_id'], $session['ip_address'], $session['user_agent'], $session['last_activity']))
|
|
|
181 |
{
|
|
|
182 |
// $this->sess_destroy();
|
|
|
183 |
// return FALSE;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
// Is the session current?
|
|
|
187 |
if (($session['last_activity'] + $this->sess_expiration) < $this->now)
|
|
|
188 |
{
|
|
|
189 |
// $this->sess_destroy();
|
|
|
190 |
// return FALSE;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
// Does the IP match?
|
|
|
194 |
if ($this->sess_match_ip == TRUE && $session['ip_address'] !== $this->CI->input->ip_address())
|
|
|
195 |
{
|
|
|
196 |
// $this->sess_destroy();
|
|
|
197 |
// return FALSE;
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
// Does the User Agent Match?
|
|
|
201 |
if ($this->sess_match_useragent == TRUE && trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))
|
|
|
202 |
{
|
|
|
203 |
// $this->sess_destroy();
|
|
|
204 |
// return FALSE;
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
// Is there a corresponding session in the DB?
|
|
|
208 |
if ($this->sess_use_database === TRUE)
|
|
|
209 |
{
|
|
|
210 |
$this->CI->db->where('session_id', $session['session_id']);
|
|
|
211 |
|
|
|
212 |
if ($this->sess_match_ip == TRUE)
|
|
|
213 |
{
|
|
|
214 |
$this->CI->db->where('ip_address', $session['ip_address']);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
if ($this->sess_match_useragent == TRUE)
|
|
|
218 |
{
|
|
|
219 |
$this->CI->db->where('user_agent', $session['user_agent']);
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
$query = $this->CI->db->get($this->sess_table_name);
|
|
|
223 |
|
|
|
224 |
// No result? Kill it!
|
|
|
225 |
if ($query->num_rows() === 0)
|
|
|
226 |
{
|
|
|
227 |
// $this->sess_destroy();
|
|
|
228 |
// return FALSE;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
// Is there custom data? If so, add it to the main session array
|
|
|
232 |
$row = $query->row();
|
|
|
233 |
if (isset($row->user_data) && $row->user_data != '')
|
|
|
234 |
{
|
|
|
235 |
$custom_data = $this->_unserialize($row->user_data);
|
|
|
236 |
|
|
|
237 |
if (is_array($custom_data))
|
|
|
238 |
{
|
|
|
239 |
foreach ($custom_data as $key => $val)
|
|
|
240 |
{
|
|
|
241 |
$session[$key] = $val;
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
// Session is valid!
|
|
|
248 |
$this->userdata = $session;
|
|
|
249 |
unset($session);
|
|
|
250 |
|
|
|
251 |
return TRUE;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
// --------------------------------------------------------------------
|
|
|
255 |
|
|
|
256 |
/**
|
|
|
257 |
* Write the session data
|
|
|
258 |
*
|
|
|
259 |
* @return void
|
|
|
260 |
*/
|
|
|
261 |
public function sess_write()
|
|
|
262 |
{
|
|
|
263 |
// Are we saving custom data to the DB? If not, all we do is update the cookie
|
|
|
264 |
if ($this->sess_use_database === FALSE)
|
|
|
265 |
{
|
|
|
266 |
$this->_set_cookie();
|
|
|
267 |
return;
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
// set the custom userdata, the session data we will set in a second
|
|
|
271 |
$custom_userdata = $this->userdata;
|
|
|
272 |
$cookie_userdata = array();
|
|
|
273 |
|
|
|
274 |
// Before continuing, we need to determine if there is any custom data to deal with.
|
|
|
275 |
// Let's determine this by removing the default indexes to see if there's anything left in the array
|
|
|
276 |
// and set the session data while we're at it
|
|
|
277 |
foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
|
|
|
278 |
{
|
|
|
279 |
unset($custom_userdata[$val]);
|
|
|
280 |
$cookie_userdata[$val] = $this->userdata[$val];
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
// Did we find any custom data? If not, we turn the empty array into a string
|
|
|
284 |
// since there's no reason to serialize and store an empty array in the DB
|
|
|
285 |
if (count($custom_userdata) === 0)
|
|
|
286 |
{
|
|
|
287 |
$custom_userdata = '';
|
|
|
288 |
}
|
|
|
289 |
else
|
|
|
290 |
{
|
|
|
291 |
// Serialize the custom data array so we can store it
|
|
|
292 |
$custom_userdata = $this->_serialize($custom_userdata);
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
// Run the update query
|
|
|
296 |
$this->CI->db->where('session_id', $this->userdata['session_id']);
|
|
|
297 |
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
|
|
|
298 |
|
|
|
299 |
// Write the cookie. Notice that we manually pass the cookie data array to the
|
|
|
300 |
// _set_cookie() function. Normally that function will store $this->userdata, but
|
|
|
301 |
// in this case that array contains custom data, which we do not want in the cookie.
|
|
|
302 |
$this->_set_cookie($cookie_userdata);
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
// --------------------------------------------------------------------
|
|
|
306 |
|
|
|
307 |
/**
|
|
|
308 |
* Create a new session
|
|
|
309 |
*
|
|
|
310 |
* @return void
|
|
|
311 |
*/
|
|
|
312 |
public function sess_create()
|
|
|
313 |
{
|
|
|
314 |
$sessid = '';
|
|
|
315 |
do
|
|
|
316 |
{
|
|
|
317 |
$sessid .= mt_rand(0, mt_getrandmax());
|
|
|
318 |
}
|
|
|
319 |
while (strlen($sessid) < 32);
|
|
|
320 |
|
|
|
321 |
// To make the session ID even more secure we'll combine it with the user's IP
|
|
|
322 |
$sessid .= $this->CI->input->ip_address();
|
|
|
323 |
|
|
|
324 |
$this->userdata = array(
|
|
|
325 |
'session_id' => md5(uniqid($sessid, TRUE)),
|
|
|
326 |
'ip_address' => $this->CI->input->ip_address(),
|
|
|
327 |
'user_agent' => substr($this->CI->input->user_agent(), 0, 120),
|
|
|
328 |
'last_activity' => $this->now,
|
|
|
329 |
'user_data' => ''
|
|
|
330 |
);
|
|
|
331 |
|
|
|
332 |
// Save the data to the DB if needed
|
|
|
333 |
if ($this->sess_use_database === TRUE)
|
|
|
334 |
{
|
|
|
335 |
$this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
// Write the cookie
|
|
|
339 |
$this->_set_cookie();
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
// --------------------------------------------------------------------
|
|
|
343 |
|
|
|
344 |
/**
|
|
|
345 |
* Update an existing session
|
|
|
346 |
*
|
|
|
347 |
* @return void
|
|
|
348 |
*/
|
|
|
349 |
public function sess_update()
|
|
|
350 |
{
|
|
|
351 |
// We only update the session every five minutes by default
|
|
|
352 |
if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
|
|
|
353 |
{
|
|
|
354 |
return;
|
|
|
355 |
}
|
|
|
356 |
if ($this->CI->input->is_ajax_request())
|
|
|
357 |
{
|
|
|
358 |
return;
|
|
|
359 |
}
|
|
|
360 |
// _set_cookie() will handle this for us if we aren't using database sessions
|
|
|
361 |
// by pushing all userdata to the cookie.
|
|
|
362 |
$cookie_data = NULL;
|
|
|
363 |
|
|
|
364 |
/* Changing the session ID during an AJAX call causes problems,
|
|
|
365 |
* so we'll only update our last_activity
|
|
|
366 |
*/
|
|
|
367 |
if ($this->CI->input->is_ajax_request())
|
|
|
368 |
{
|
|
|
369 |
$this->userdata['last_activity'] = $this->now;
|
|
|
370 |
|
|
|
371 |
// Update the session ID and last_activity field in the DB if needed
|
|
|
372 |
if ($this->sess_use_database === TRUE)
|
|
|
373 |
{
|
|
|
374 |
// set cookie explicitly to only have our session data
|
|
|
375 |
$cookie_data = array();
|
|
|
376 |
foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
|
|
|
377 |
{
|
|
|
378 |
$cookie_data[$val] = $this->userdata[$val];
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
$this->CI->db->query($this->CI->db->update_string($this->sess_table_name,
|
|
|
382 |
array('last_activity' => $this->userdata['last_activity']),
|
|
|
383 |
array('session_id' => $this->userdata['session_id'])));
|
|
|
384 |
}
|
|
|
385 |
|
|
|
386 |
return $this->_set_cookie($cookie_data);
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
// Save the old session id so we know which record to
|
|
|
390 |
// update in the database if we need it
|
|
|
391 |
$old_sessid = $this->userdata['session_id'];
|
|
|
392 |
$new_sessid = '';
|
|
|
393 |
do
|
|
|
394 |
{
|
|
|
395 |
$new_sessid .= mt_rand(0, mt_getrandmax());
|
|
|
396 |
}
|
|
|
397 |
while (strlen($new_sessid) < 32);
|
|
|
398 |
|
|
|
399 |
// To make the session ID even more secure we'll combine it with the user's IP
|
|
|
400 |
$new_sessid .= $this->CI->input->ip_address();
|
|
|
401 |
|
|
|
402 |
// Turn it into a hash and update the session data array
|
|
|
403 |
$this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE));
|
|
|
404 |
$this->userdata['last_activity'] = $this->now;
|
|
|
405 |
|
|
|
406 |
// Update the session ID and last_activity field in the DB if needed
|
|
|
407 |
if ($this->sess_use_database === TRUE)
|
|
|
408 |
{
|
|
|
409 |
// set cookie explicitly to only have our session data
|
|
|
410 |
$cookie_data = array();
|
|
|
411 |
foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
|
|
|
412 |
{
|
|
|
413 |
$cookie_data[$val] = $this->userdata[$val];
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
$this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid)));
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
// Write the cookie
|
|
|
420 |
$this->_set_cookie($cookie_data);
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
// --------------------------------------------------------------------
|
|
|
424 |
|
|
|
425 |
/**
|
|
|
426 |
* Destroy the current session
|
|
|
427 |
*
|
|
|
428 |
* @return void
|
|
|
429 |
*/
|
|
|
430 |
public function sess_destroy()
|
|
|
431 |
{
|
|
|
432 |
// Kill the session DB row
|
|
|
433 |
if ($this->sess_use_database === TRUE && isset($this->userdata['session_id']))
|
|
|
434 |
{
|
|
|
435 |
$this->CI->db->where('session_id', $this->userdata['session_id']);
|
|
|
436 |
$this->CI->db->delete($this->sess_table_name);
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
// Kill the cookie
|
|
|
440 |
setcookie(
|
|
|
441 |
$this->sess_cookie_name,
|
|
|
442 |
addslashes(serialize(array())),
|
|
|
443 |
($this->now - 31500000),
|
|
|
444 |
$this->cookie_path,
|
|
|
445 |
$this->cookie_domain,
|
|
|
446 |
|
|
|
447 |
);
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
// --------------------------------------------------------------------
|
|
|
451 |
|
|
|
452 |
/**
|
|
|
453 |
* Fetch a specific item from the session array
|
|
|
454 |
*
|
|
|
455 |
* @param string
|
|
|
456 |
* @return string
|
|
|
457 |
*/
|
|
|
458 |
public function userdata($item)
|
|
|
459 |
{
|
|
|
460 |
return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item];
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
// --------------------------------------------------------------------
|
|
|
464 |
|
|
|
465 |
/**
|
|
|
466 |
* Fetch all session data
|
|
|
467 |
*
|
|
|
468 |
* @return array
|
|
|
469 |
*/
|
|
|
470 |
public function all_userdata()
|
|
|
471 |
{
|
|
|
472 |
return $this->userdata;
|
|
|
473 |
}
|
|
|
474 |
|
|
|
475 |
// --------------------------------------------------------------------
|
|
|
476 |
|
|
|
477 |
/**
|
|
|
478 |
* Add or change data in the "userdata" array
|
|
|
479 |
*
|
|
|
480 |
* @param mixed
|
|
|
481 |
* @param string
|
|
|
482 |
* @return void
|
|
|
483 |
*/
|
|
|
484 |
public function set_userdata($newdata = array(), $newval = '')
|
|
|
485 |
{
|
|
|
486 |
if (is_string($newdata))
|
|
|
487 |
{
|
|
|
488 |
$newdata = array($newdata => $newval);
|
|
|
489 |
}
|
|
|
490 |
|
|
|
491 |
if (count($newdata) > 0)
|
|
|
492 |
{
|
|
|
493 |
foreach ($newdata as $key => $val)
|
|
|
494 |
{
|
|
|
495 |
$this->userdata[$key] = $val;
|
|
|
496 |
}
|
|
|
497 |
}
|
|
|
498 |
|
|
|
499 |
$this->sess_write();
|
|
|
500 |
}
|
|
|
501 |
|
|
|
502 |
// --------------------------------------------------------------------
|
|
|
503 |
|
|
|
504 |
/**
|
|
|
505 |
* Delete a session variable from the "userdata" array
|
|
|
506 |
*
|
|
|
507 |
* @return void
|
|
|
508 |
*/
|
|
|
509 |
public function unset_userdata($newdata = array())
|
|
|
510 |
{
|
|
|
511 |
if (is_string($newdata))
|
|
|
512 |
{
|
|
|
513 |
$newdata = array($newdata => '');
|
|
|
514 |
}
|
|
|
515 |
|
|
|
516 |
if (count($newdata) > 0)
|
|
|
517 |
{
|
|
|
518 |
foreach ($newdata as $key => $val)
|
|
|
519 |
{
|
|
|
520 |
unset($this->userdata[$key]);
|
|
|
521 |
}
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
$this->sess_write();
|
|
|
525 |
}
|
|
|
526 |
|
|
|
527 |
// ------------------------------------------------------------------------
|
|
|
528 |
|
|
|
529 |
/**
|
|
|
530 |
* Add or change flashdata, only available
|
|
|
531 |
* until the next request
|
|
|
532 |
*
|
|
|
533 |
* @param mixed
|
|
|
534 |
* @param string
|
|
|
535 |
* @return void
|
|
|
536 |
*/
|
|
|
537 |
public function set_flashdata($newdata = array(), $newval = '')
|
|
|
538 |
{
|
|
|
539 |
if (is_string($newdata))
|
|
|
540 |
{
|
|
|
541 |
$newdata = array($newdata => $newval);
|
|
|
542 |
}
|
|
|
543 |
|
|
|
544 |
if (count($newdata) > 0)
|
|
|
545 |
{
|
|
|
546 |
foreach ($newdata as $key => $val)
|
|
|
547 |
{
|
|
|
548 |
$this->set_userdata($this->flashdata_key.':new:'.$key, $val);
|
|
|
549 |
}
|
|
|
550 |
}
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
// ------------------------------------------------------------------------
|
|
|
554 |
|
|
|
555 |
/**
|
|
|
556 |
* Keeps existing flashdata available to next request.
|
|
|
557 |
*
|
|
|
558 |
* @param string
|
|
|
559 |
* @return void
|
|
|
560 |
*/
|
|
|
561 |
public function keep_flashdata($key)
|
|
|
562 |
{
|
|
|
563 |
// 'old' flashdata gets removed. Here we mark all
|
|
|
564 |
// flashdata as 'new' to preserve it from _flashdata_sweep()
|
|
|
565 |
// Note the function will return FALSE if the $key
|
|
|
566 |
// provided cannot be found
|
|
|
567 |
$value = $this->userdata($this->flashdata_key.':old:'.$key);
|
|
|
568 |
|
|
|
569 |
$this->set_userdata($this->flashdata_key.':new:'.$key, $value);
|
|
|
570 |
}
|
|
|
571 |
|
|
|
572 |
// ------------------------------------------------------------------------
|
|
|
573 |
|
|
|
574 |
/**
|
|
|
575 |
* Fetch a specific flashdata item from the session array
|
|
|
576 |
*
|
|
|
577 |
* @param string
|
|
|
578 |
* @return string
|
|
|
579 |
*/
|
|
|
580 |
public function flashdata($key)
|
|
|
581 |
{
|
|
|
582 |
return $this->userdata($this->flashdata_key.':old:'.$key);
|
|
|
583 |
}
|
|
|
584 |
|
|
|
585 |
// ------------------------------------------------------------------------
|
|
|
586 |
|
|
|
587 |
/**
|
|
|
588 |
* Identifies flashdata as 'old' for removal
|
|
|
589 |
* when _flashdata_sweep() runs.
|
|
|
590 |
*
|
|
|
591 |
* @return void
|
|
|
592 |
*/
|
|
|
593 |
protected function _flashdata_mark()
|
|
|
594 |
{
|
|
|
595 |
$userdata = $this->all_userdata();
|
|
|
596 |
foreach ($userdata as $name => $value)
|
|
|
597 |
{
|
|
|
598 |
$parts = explode(':new:', $name);
|
|
|
599 |
if (is_array($parts) && count($parts) === 2)
|
|
|
600 |
{
|
|
|
601 |
$this->set_userdata($this->flashdata_key.':old:'.$parts[1], $value);
|
|
|
602 |
$this->unset_userdata($name);
|
|
|
603 |
}
|
|
|
604 |
}
|
|
|
605 |
}
|
|
|
606 |
|
|
|
607 |
// ------------------------------------------------------------------------
|
|
|
608 |
|
|
|
609 |
/**
|
|
|
610 |
* Removes all flashdata marked as 'old'
|
|
|
611 |
*
|
|
|
612 |
* @return void
|
|
|
613 |
*/
|
|
|
614 |
protected function _flashdata_sweep()
|
|
|
615 |
{
|
|
|
616 |
$userdata = $this->all_userdata();
|
|
|
617 |
foreach ($userdata as $key => $value)
|
|
|
618 |
{
|
|
|
619 |
if (strpos($key, ':old:'))
|
|
|
620 |
{
|
|
|
621 |
$this->unset_userdata($key);
|
|
|
622 |
}
|
|
|
623 |
}
|
|
|
624 |
|
|
|
625 |
}
|
|
|
626 |
|
|
|
627 |
// --------------------------------------------------------------------
|
|
|
628 |
|
|
|
629 |
/**
|
|
|
630 |
* Get the "now" time
|
|
|
631 |
*
|
|
|
632 |
* @return string
|
|
|
633 |
*/
|
|
|
634 |
protected function _get_time()
|
|
|
635 |
{
|
|
|
636 |
return (strtolower($this->time_reference) === 'gmt')
|
|
|
637 |
? mktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y'))
|
|
|
638 |
: time();
|
|
|
639 |
}
|
|
|
640 |
|
|
|
641 |
// --------------------------------------------------------------------
|
|
|
642 |
|
|
|
643 |
/**
|
|
|
644 |
* Write the session cookie
|
|
|
645 |
*
|
|
|
646 |
* @return void
|
|
|
647 |
*/
|
|
|
648 |
protected function _set_cookie($cookie_data = NULL)
|
|
|
649 |
{
|
|
|
650 |
if (is_null($cookie_data))
|
|
|
651 |
{
|
|
|
652 |
$cookie_data = $this->userdata;
|
|
|
653 |
}
|
|
|
654 |
|
|
|
655 |
// Serialize the userdata for the cookie
|
|
|
656 |
$cookie_data = $this->_serialize($cookie_data);
|
|
|
657 |
|
|
|
658 |
if ($this->sess_encrypt_cookie == TRUE)
|
|
|
659 |
{
|
|
|
660 |
$cookie_data = $this->CI->encrypt->encode($cookie_data);
|
|
|
661 |
}
|
|
|
662 |
else
|
|
|
663 |
{
|
|
|
664 |
// if encryption is not used, we provide an md5 hash to prevent userside tampering
|
|
|
665 |
$cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
|
|
|
666 |
}
|
|
|
667 |
|
|
|
668 |
$expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
|
|
|
669 |
|
|
|
670 |
// Set the cookie
|
|
|
671 |
setcookie(
|
|
|
672 |
$this->sess_cookie_name,
|
|
|
673 |
$cookie_data,
|
|
|
674 |
$expire,
|
|
|
675 |
$this->cookie_path,
|
|
|
676 |
$this->cookie_domain,
|
|
|
677 |
$this->cookie_secure
|
|
|
678 |
);
|
|
|
679 |
}
|
|
|
680 |
|
|
|
681 |
// --------------------------------------------------------------------
|
|
|
682 |
|
|
|
683 |
/**
|
|
|
684 |
* Serialize an array
|
|
|
685 |
*
|
|
|
686 |
* This function first converts any slashes found in the array to a temporary
|
|
|
687 |
* marker, so when it gets unserialized the slashes will be preserved
|
|
|
688 |
*
|
|
|
689 |
* @param array
|
|
|
690 |
* @return string
|
|
|
691 |
*/
|
|
|
692 |
protected function _serialize($data)
|
|
|
693 |
{
|
|
|
694 |
if (is_array($data))
|
|
|
695 |
{
|
|
|
696 |
array_walk_recursive($data, array(&$this, '_escape_slashes'));
|
|
|
697 |
}
|
|
|
698 |
elseif (is_string($data))
|
|
|
699 |
{
|
|
|
700 |
$data = str_replace('\\', '{{slash}}', $data);
|
|
|
701 |
}
|
|
|
702 |
return serialize($data);
|
|
|
703 |
}
|
|
|
704 |
|
|
|
705 |
/**
|
|
|
706 |
* Escape slashes
|
|
|
707 |
*
|
|
|
708 |
* This function converts any slashes found into a temporary marker
|
|
|
709 |
*
|
|
|
710 |
* @param string
|
|
|
711 |
* @param string
|
|
|
712 |
* @return void
|
|
|
713 |
*/
|
|
|
714 |
protected function _escape_slashes(&$val, $key)
|
|
|
715 |
{
|
|
|
716 |
if (is_string($val))
|
|
|
717 |
{
|
|
|
718 |
$val = str_replace('\\', '{{slash}}', $val);
|
|
|
719 |
}
|
|
|
720 |
}
|
|
|
721 |
|
|
|
722 |
// --------------------------------------------------------------------
|
|
|
723 |
|
|
|
724 |
/**
|
|
|
725 |
* Unserialize
|
|
|
726 |
*
|
|
|
727 |
* This function unserializes a data string, then converts any
|
|
|
728 |
* temporary slash markers back to actual slashes
|
|
|
729 |
*
|
|
|
730 |
* @param array
|
|
|
731 |
* @return string
|
|
|
732 |
*/
|
|
|
733 |
protected function _unserialize($data)
|
|
|
734 |
{
|
|
|
735 |
$data = @unserialize(strip_slashes($data));
|
|
|
736 |
|
|
|
737 |
if (is_array($data))
|
|
|
738 |
{
|
|
|
739 |
array_walk_recursive($data, array(&$this, '_unescape_slashes'));
|
|
|
740 |
return $data;
|
|
|
741 |
}
|
|
|
742 |
|
|
|
743 |
return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data;
|
|
|
744 |
}
|
|
|
745 |
|
|
|
746 |
/**
|
|
|
747 |
* Unescape slashes
|
|
|
748 |
*
|
|
|
749 |
* This function converts any slash markers back into actual slashes
|
|
|
750 |
*
|
|
|
751 |
* @param string
|
|
|
752 |
* @param string
|
|
|
753 |
* @return void
|
|
|
754 |
*/
|
|
|
755 |
protected function _unescape_slashes(&$val, $key)
|
|
|
756 |
{
|
|
|
757 |
if (is_string($val))
|
|
|
758 |
{
|
|
|
759 |
$val= str_replace('{{slash}}', '\\', $val);
|
|
|
760 |
}
|
|
|
761 |
}
|
|
|
762 |
|
|
|
763 |
// --------------------------------------------------------------------
|
|
|
764 |
|
|
|
765 |
/**
|
|
|
766 |
* Garbage collection
|
|
|
767 |
*
|
|
|
768 |
* This deletes expired session rows from database
|
|
|
769 |
* if the probability percentage is met
|
|
|
770 |
*
|
|
|
771 |
* @return void
|
|
|
772 |
*/
|
|
|
773 |
protected function _sess_gc()
|
|
|
774 |
{
|
|
|
775 |
if ($this->sess_use_database != TRUE)
|
|
|
776 |
{
|
|
|
777 |
return;
|
|
|
778 |
}
|
|
|
779 |
|
|
|
780 |
srand(time());
|
|
|
781 |
if ((rand() % 100) < $this->gc_probability)
|
|
|
782 |
{
|
|
|
783 |
$expire = $this->now - $this->sess_expiration;
|
|
|
784 |
|
|
|
785 |
$this->CI->db->where("last_activity < {$expire}");
|
|
|
786 |
$this->CI->db->delete($this->sess_table_name);
|
|
|
787 |
|
|
|
788 |
log_message('debug', 'Session garbage collection performed.');
|
|
|
789 |
}
|
|
|
790 |
}
|
|
|
791 |
|
|
|
792 |
}
|
|
|
793 |
|
|
|
794 |
/* End of file Session.php */
|
|
|
795 |
/* Location: ./system/libraries/Session.php */
|