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