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