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