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