Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
| 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 8 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014, British Columbia Institute of Technology |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/) |
| 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 2.0.0 |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 39 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 40 | /** |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 41 | * CodeIgniter Session Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 42 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Sessions |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 46 | * @author Andrey Andreev |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 47 | * @link http://codeigniter.com/user_guide/libraries/sessions.html |
| 48 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 49 | class CI_Session { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 50 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 51 | protected $_driver = 'files'; |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 52 | protected $_config; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 53 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 54 | // ------------------------------------------------------------------------ |
| 55 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 56 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 57 | * Class constructor |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 58 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 59 | * @param array $params Configuration parameters |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 60 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 61 | */ |
| 62 | public function __construct(array $params = array()) |
| 63 | { |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 64 | // No sessions under CLI |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 65 | if (is_cli()) |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 66 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 67 | log_message('debug', 'Session: Initialization under CLI aborted.'); |
| 68 | return; |
| 69 | } |
| 70 | elseif ((bool) ini_get('session.auto_start')) |
| 71 | { |
| 72 | log_message('error', 'Session: session.auto_start is enabled in php.ini. Aborting.'); |
| 73 | return; |
| 74 | } |
| 75 | elseif ( ! empty($params['driver'])) |
| 76 | { |
| 77 | $this->_driver = $params['driver']; |
| 78 | unset($params['driver']); |
| 79 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 80 | elseif ($driver = config_item('sess_driver')) |
Andrey Andreev | 34b1ef5 | 2014-05-31 21:23:41 +0300 | [diff] [blame] | 81 | { |
| 82 | $this->_driver = $driver; |
| 83 | } |
Andrey Andreev | ac4f472 | 2014-06-02 11:16:32 +0300 | [diff] [blame] | 84 | // Note: BC workaround |
| 85 | elseif (config_item('sess_use_database')) |
| 86 | { |
| 87 | $this->_driver = 'database'; |
| 88 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 89 | |
| 90 | if (($class = $this->_ci_load_classes($this->_driver)) === FALSE) |
| 91 | { |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 95 | // Configuration ... |
| 96 | $this->_configure($params); |
| 97 | |
| 98 | $class = new $class($this->_config); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 99 | if ($class instanceof SessionHandlerInterface) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 100 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 101 | if (is_php('5.4')) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 102 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 103 | session_set_save_handler($class, TRUE); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 107 | session_set_save_handler( |
| 108 | array($class, 'open'), |
| 109 | array($class, 'close'), |
| 110 | array($class, 'read'), |
| 111 | array($class, 'write'), |
| 112 | array($class, 'destroy'), |
| 113 | array($class, 'gc') |
| 114 | ); |
| 115 | |
| 116 | register_shutdown_function('session_write_close'); |
| 117 | } |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | log_message('error', "Session: Driver '".$this->_driver."' doesn't implement SessionHandlerInterface. Aborting."); |
| 122 | return; |
| 123 | } |
| 124 | |
Andrey Andreev | 562e39b | 2014-11-12 15:38:58 +0200 | [diff] [blame] | 125 | // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers |
| 126 | if (isset($_COOKIE[$this->_config['cookie_name']]) |
| 127 | && ( |
| 128 | ! is_string($_COOKIE[$this->_config['cookie_name']]) |
| 129 | OR ! preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']]) |
| 130 | ) |
| 131 | ) |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 132 | { |
Andrey Andreev | 562e39b | 2014-11-12 15:38:58 +0200 | [diff] [blame] | 133 | unset($_COOKIE[$this->_config['cookie_name']]); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 134 | } |
| 135 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 136 | session_start(); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 137 | |
Andrey Andreev | ff37ffe | 2014-11-04 12:28:57 +0200 | [diff] [blame] | 138 | // Is session ID auto-regeneration configured? (ignoring ajax requests) |
| 139 | if ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) |
| 140 | && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest' |
Andrey Andreev | de5c246 | 2014-11-04 12:31:03 +0200 | [diff] [blame] | 141 | && ($regenerate_time = config_item('sess_time_to_update')) > 0 |
Andrey Andreev | ff37ffe | 2014-11-04 12:28:57 +0200 | [diff] [blame] | 142 | ) |
Andrey Andreev | 8e60b9a | 2014-11-04 11:08:06 +0200 | [diff] [blame] | 143 | { |
| 144 | if ( ! isset($_SESSION['__ci_last_regenerate'])) |
| 145 | { |
| 146 | $_SESSION['__ci_last_regenerate'] = time(); |
| 147 | } |
| 148 | elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time)) |
| 149 | { |
| 150 | $this->sess_regenerate(FALSE); |
| 151 | } |
| 152 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 153 | // Another work-around ... PHP doesn't seem to send the session cookie |
| 154 | // unless it is being currently created or regenerated |
Andrey Andreev | 8e60b9a | 2014-11-04 11:08:06 +0200 | [diff] [blame] | 155 | elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id()) |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 156 | { |
| 157 | setcookie( |
| 158 | $this->_config['cookie_name'], |
| 159 | session_id(), |
| 160 | (empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']), |
| 161 | $this->_config['cookie_path'], |
| 162 | $this->_config['cookie_domain'], |
| 163 | $this->_config['cookie_secure'], |
| 164 | TRUE |
| 165 | ); |
| 166 | } |
| 167 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 168 | $this->_ci_init_vars(); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 169 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 170 | log_message('debug', "Session: Class initialized using '".$this->_driver."' driver."); |
| 171 | } |
| 172 | |
| 173 | // ------------------------------------------------------------------------ |
| 174 | |
| 175 | protected function _ci_load_classes($driver) |
| 176 | { |
| 177 | // PHP 5.4 compatibility |
| 178 | interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php'); |
| 179 | |
| 180 | $prefix = config_item('subclass_prefix'); |
| 181 | |
| 182 | if ( ! class_exists('CI_Session_driver', FALSE)) |
| 183 | { |
Andrey Andreev | e86603f | 2014-06-11 14:03:36 +0300 | [diff] [blame] | 184 | require_once( |
| 185 | file_exists(APPPATH.'libraries/Session/Session_driver.php') |
| 186 | ? APPPATH.'libraries/Session/Session_driver.php' |
| 187 | : BASEPATH.'libraries/Session/Session_driver.php' |
| 188 | ); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 189 | |
| 190 | if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php')) |
| 191 | { |
| 192 | require_once($file_path); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | $class = 'Session_'.$driver.'_driver'; |
Andrey Andreev | cd94dd7 | 2014-12-09 17:38:56 +0200 | [diff] [blame^] | 197 | |
| 198 | // Allow custom drivers without the CI_ or MY_ prefix |
| 199 | if ( ! class_exists($class, FALSE) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php')) |
| 200 | { |
| 201 | require_once($file_path); |
| 202 | if (class_exists($class, FALSE)) |
| 203 | { |
| 204 | return $class; |
| 205 | } |
| 206 | } |
| 207 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 208 | if ( ! class_exists('CI_'.$class, FALSE)) |
| 209 | { |
| 210 | if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php')) |
| 211 | { |
| 212 | require_once($file_path); |
| 213 | } |
| 214 | |
Andrey Andreev | cd94dd7 | 2014-12-09 17:38:56 +0200 | [diff] [blame^] | 215 | if ( ! class_exists('CI_'.$class, FALSE) && ! class_exists($class, FALSE)) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 216 | { |
| 217 | log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting."); |
| 218 | return FALSE; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php')) |
| 223 | { |
| 224 | require_once($file_path); |
| 225 | if (class_exists($prefix.$class, FALSE)) |
| 226 | { |
| 227 | return $prefix.$class; |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.'); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return 'CI_'.$class; |
| 236 | } |
| 237 | |
| 238 | // ------------------------------------------------------------------------ |
| 239 | |
| 240 | /** |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 241 | * Configuration |
| 242 | * |
| 243 | * Handle input parameters and configuration defaults |
| 244 | * |
| 245 | * @param array &$params Input parameters |
| 246 | * @return void |
| 247 | */ |
| 248 | protected function _configure(&$params) |
| 249 | { |
| 250 | $expiration = config_item('sess_expiration'); |
| 251 | |
| 252 | if (isset($params['cookie_lifetime'])) |
| 253 | { |
| 254 | $params['cookie_lifetime'] = (int) $params['cookie_lifetime']; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close')) |
| 259 | ? 0 : (int) $expiration; |
| 260 | } |
| 261 | |
| 262 | isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name'); |
| 263 | if (empty($params['cookie_name'])) |
| 264 | { |
| 265 | $params['cookie_name'] = ini_get('session.name'); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | ini_set('session.name', $params['cookie_name']); |
| 270 | } |
| 271 | |
| 272 | isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path'); |
Andrey Andreev | 41b546d | 2014-10-06 03:01:22 +0300 | [diff] [blame] | 273 | isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain'); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 274 | isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure'); |
| 275 | |
| 276 | session_set_cookie_params( |
| 277 | $params['cookie_lifetime'], |
| 278 | $params['cookie_path'], |
| 279 | $params['cookie_domain'], |
| 280 | $params['cookie_secure'], |
| 281 | TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons |
| 282 | ); |
| 283 | |
| 284 | if (empty($expiration)) |
| 285 | { |
| 286 | $params['expiration'] = (int) ini_get('session.gc_maxlifetime'); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | $params['expiration'] = (int) $expiration; |
| 291 | ini_set('session.gc_maxlifetime', $expiration); |
| 292 | } |
| 293 | |
| 294 | $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip')); |
| 295 | |
| 296 | isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path'); |
| 297 | |
| 298 | $this->_config = $params; |
| 299 | |
| 300 | // Security is king |
| 301 | ini_set('session.use_trans_id', 0); |
| 302 | ini_set('session.use_strict_mode', 1); |
| 303 | ini_set('session.use_cookies', 1); |
| 304 | ini_set('session.use_only_cookies', 1); |
| 305 | ini_set('session.hash_function', 1); |
| 306 | ini_set('session.hash_bits_per_character', 4); |
| 307 | } |
| 308 | |
| 309 | // ------------------------------------------------------------------------ |
| 310 | |
| 311 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 312 | * Handle temporary variables |
| 313 | * |
| 314 | * Clears old "flash" data, marks the new one for deletion and handles |
| 315 | * "temp" data deletion. |
| 316 | * |
| 317 | * @return void |
| 318 | */ |
| 319 | protected function _ci_init_vars() |
| 320 | { |
| 321 | if ( ! empty($_SESSION['__ci_vars'])) |
| 322 | { |
| 323 | $current_time = time(); |
| 324 | |
| 325 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
| 326 | { |
| 327 | if ($value === 'new') |
| 328 | { |
| 329 | $_SESSION['__ci_vars'][$key] = 'old'; |
| 330 | } |
| 331 | // Hacky, but 'old' will (implicitly) always be less than time() ;) |
| 332 | // DO NOT move this above the 'new' check! |
| 333 | elseif ($value < $current_time) |
| 334 | { |
| 335 | unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (empty($_SESSION['__ci_vars'])) |
| 340 | { |
| 341 | unset($_SESSION['__ci_vars']); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 346 | // ------------------------------------------------------------------------ |
| 347 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 348 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 349 | * Mark as flash |
| 350 | * |
| 351 | * @param mixed $key Session data key(s) |
| 352 | * @return bool |
| 353 | */ |
| 354 | public function mark_as_flash($key) |
| 355 | { |
| 356 | if (is_array($key)) |
| 357 | { |
| 358 | for ($i = 0, $c = count($key); $i < $c; $i++) |
| 359 | { |
| 360 | if ( ! isset($_SESSION[$key[$i]])) |
| 361 | { |
| 362 | return FALSE; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | $new = array_fill_keys($key, 'new'); |
| 367 | |
| 368 | $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) |
| 369 | ? array_merge($_SESSION['__ci_vars'], $new) |
| 370 | : $new; |
| 371 | |
| 372 | return TRUE; |
| 373 | } |
| 374 | |
| 375 | if ( ! isset($_SESSION[$key])) |
| 376 | { |
| 377 | return FALSE; |
| 378 | } |
| 379 | |
| 380 | $_SESSION['__ci_vars'][$key] = 'new'; |
| 381 | return TRUE; |
| 382 | } |
| 383 | |
| 384 | // ------------------------------------------------------------------------ |
| 385 | |
| 386 | /** |
| 387 | * Get flash keys |
| 388 | * |
| 389 | * @return array |
| 390 | */ |
| 391 | public function get_flash_keys() |
| 392 | { |
| 393 | if ( ! isset($_SESSION['__ci_vars'])) |
| 394 | { |
| 395 | return array(); |
| 396 | } |
| 397 | |
| 398 | $keys = array(); |
| 399 | foreach (array_keys($_SESSION['__ci_vars']) as $key) |
| 400 | { |
| 401 | is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key; |
| 402 | } |
| 403 | |
| 404 | return $keys; |
| 405 | } |
| 406 | |
| 407 | // ------------------------------------------------------------------------ |
| 408 | |
| 409 | /** |
| 410 | * Unmark flash |
| 411 | * |
| 412 | * @param mixed $key Session data key(s) |
| 413 | * @return void |
| 414 | */ |
| 415 | public function unmark_flash($key) |
| 416 | { |
| 417 | if (empty($_SESSION['__ci_vars'])) |
| 418 | { |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | is_array($key) OR $key = array($key); |
| 423 | |
| 424 | foreach ($key as $k) |
| 425 | { |
| 426 | if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k])) |
| 427 | { |
| 428 | unset($_SESSION['__ci_vars'][$k]); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (empty($_SESSION['__ci_vars'])) |
| 433 | { |
| 434 | unset($_SESSION['__ci_vars']); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // ------------------------------------------------------------------------ |
| 439 | |
| 440 | /** |
| 441 | * Mark as temp |
| 442 | * |
| 443 | * @param mixed $key Session data key(s) |
| 444 | * @param int $ttl Time-to-live in seconds |
| 445 | * @return bool |
| 446 | */ |
| 447 | public function mark_as_temp($key, $ttl = 300) |
| 448 | { |
| 449 | $ttl += time(); |
| 450 | |
| 451 | if (is_array($key)) |
| 452 | { |
| 453 | $temp = array(); |
| 454 | |
| 455 | foreach ($key as $k => $v) |
| 456 | { |
| 457 | // Do we have a key => ttl pair, or just a key? |
| 458 | if (is_int($k)) |
| 459 | { |
| 460 | $k = $v; |
| 461 | $v = $ttl; |
| 462 | } |
| 463 | else |
| 464 | { |
| 465 | $v += time(); |
| 466 | } |
| 467 | |
| 468 | if ( ! isset($_SESSION[$k])) |
| 469 | { |
| 470 | return FALSE; |
| 471 | } |
| 472 | |
| 473 | $temp[$k] = $ts; |
| 474 | } |
| 475 | |
| 476 | $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) |
| 477 | ? array_merge($_SESSION['__ci_vars'], $temp) |
| 478 | : $temp; |
| 479 | |
| 480 | return TRUE; |
| 481 | } |
| 482 | |
| 483 | if ( ! isset($_SESSION[$key])) |
| 484 | { |
| 485 | return FALSE; |
| 486 | } |
| 487 | |
| 488 | $_SESSION['__ci_vars'][$key] = $ttl; |
| 489 | return TRUE; |
| 490 | } |
| 491 | |
| 492 | // ------------------------------------------------------------------------ |
| 493 | |
| 494 | /** |
| 495 | * Get temp keys |
| 496 | * |
| 497 | * @return array |
| 498 | */ |
| 499 | public function get_temp_keys() |
| 500 | { |
| 501 | if ( ! isset($_SESSION['__ci_vars'])) |
| 502 | { |
| 503 | return array(); |
| 504 | } |
| 505 | |
| 506 | $keys = array(); |
| 507 | foreach (array_keys($_SESSION['__ci_vars']) as $key) |
| 508 | { |
| 509 | is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key; |
| 510 | } |
| 511 | |
| 512 | return $keys; |
| 513 | } |
| 514 | |
| 515 | // ------------------------------------------------------------------------ |
| 516 | |
| 517 | /** |
| 518 | * Unmark flash |
| 519 | * |
| 520 | * @param mixed $key Session data key(s) |
| 521 | * @return void |
| 522 | */ |
| 523 | public function unmark_temp($key) |
| 524 | { |
| 525 | if (empty($_SESSION['__ci_vars'])) |
| 526 | { |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | is_array($key) OR $key = array($key); |
| 531 | |
| 532 | foreach ($key as $k) |
| 533 | { |
| 534 | if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k])) |
| 535 | { |
| 536 | unset($_SESSION['__ci_vars'][$k]); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | if (empty($_SESSION['__ci_vars'])) |
| 541 | { |
| 542 | unset($_SESSION['__ci_vars']); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // ------------------------------------------------------------------------ |
| 547 | |
| 548 | /** |
| 549 | * __get() |
| 550 | * |
| 551 | * @param string $key 'session_id' or a session data key |
| 552 | * @return mixed |
| 553 | */ |
| 554 | public function __get($key) |
| 555 | { |
| 556 | // Note: Keep this order the same, just in case somebody wants to |
| 557 | // use 'session_id' as a session data key, for whatever reason |
| 558 | if (isset($_SESSION[$key])) |
| 559 | { |
| 560 | return $_SESSION[$key]; |
| 561 | } |
| 562 | elseif ($key === 'session_id') |
| 563 | { |
| 564 | return session_id(); |
| 565 | } |
| 566 | |
| 567 | return NULL; |
| 568 | } |
| 569 | |
| 570 | // ------------------------------------------------------------------------ |
| 571 | |
| 572 | /** |
| 573 | * __set() |
| 574 | * |
| 575 | * @param string $key Session data key |
| 576 | * @param mixed $value Session data value |
| 577 | * @return void |
| 578 | */ |
| 579 | public function __set($key, $value) |
| 580 | { |
| 581 | $_SESSION[$key] = $value; |
| 582 | } |
| 583 | |
| 584 | // ------------------------------------------------------------------------ |
| 585 | |
| 586 | /** |
| 587 | * Session destroy |
| 588 | * |
| 589 | * Legacy CI_Session compatibility method |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 590 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 591 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 592 | */ |
| 593 | public function sess_destroy() |
| 594 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 595 | session_destroy(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 596 | } |
| 597 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 598 | // ------------------------------------------------------------------------ |
| 599 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 600 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 601 | * Session regenerate |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 602 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 603 | * Legacy CI_Session compatibility method |
| 604 | * |
| 605 | * @param bool $destroy Destroy old session data flag |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 606 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 607 | */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 608 | public function sess_regenerate($destroy = FALSE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 609 | { |
Andrey Andreev | 8e60b9a | 2014-11-04 11:08:06 +0200 | [diff] [blame] | 610 | $_SESSION['__ci_last_regenerate'] = time(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 611 | session_regenerate_id($destroy); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 612 | } |
| 613 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 614 | // ------------------------------------------------------------------------ |
| 615 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 616 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 617 | * Get userdata reference |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 618 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 619 | * Legacy CI_Session compatibility method |
| 620 | * |
| 621 | * @returns array |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 622 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 623 | public function &get_userdata() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 624 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 625 | return $_SESSION; |
| 626 | } |
| 627 | |
| 628 | // ------------------------------------------------------------------------ |
| 629 | |
| 630 | /** |
| 631 | * Userdata (fetch) |
| 632 | * |
| 633 | * Legacy CI_Session compatibility method |
| 634 | * |
| 635 | * @param string $key Session data key |
| 636 | * @return mixed Session data value or NULL if not found |
| 637 | */ |
| 638 | public function userdata($key = NULL) |
| 639 | { |
| 640 | if (isset($key)) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 641 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 642 | return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL; |
| 643 | } |
| 644 | elseif (empty($_SESSION)) |
| 645 | { |
| 646 | return array(); |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 647 | } |
| 648 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 649 | $userdata = array(); |
| 650 | $_exclude = array_merge( |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 651 | array('__ci_vars'), |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 652 | $this->get_flash_keys(), |
| 653 | $this->get_temp_keys() |
| 654 | ); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 655 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 656 | foreach (array_keys($_SESSION) as $key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 657 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 658 | if ( ! in_array($key, $_exclude, TRUE)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 659 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 660 | $userdata[$key] = $_SESSION[$key]; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 664 | return $userdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 665 | } |
| 666 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 667 | // ------------------------------------------------------------------------ |
| 668 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 669 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 670 | * Set userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 671 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 672 | * Legacy CI_Session compatibility method |
| 673 | * |
| 674 | * @param mixed $data Session data key or an associative array |
| 675 | * @param mixed $value Value to store |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 676 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 677 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 678 | public function set_userdata($data, $value = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 679 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 680 | if (is_array($data)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 681 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 682 | foreach ($data as $key => &$value) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 683 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 684 | $_SESSION[$key] = $value; |
Johnathan Croom | 8d8543d | 2012-11-25 10:36:57 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | return; |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 688 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 689 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 690 | $_SESSION[$data] = $value; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 691 | } |
| 692 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 693 | // ------------------------------------------------------------------------ |
| 694 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 695 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 696 | * Unset userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 697 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 698 | * Legacy CI_Session compatibility method |
| 699 | * |
| 700 | * @param mixed $data Session data key(s) |
| 701 | * @return void |
| 702 | */ |
| 703 | public function unset_userdata($key) |
| 704 | { |
| 705 | if (is_array($key)) |
| 706 | { |
| 707 | foreach ($key as $k) |
| 708 | { |
Andrey Andreev | d069b9b | 2014-09-16 10:18:16 +0300 | [diff] [blame] | 709 | unset($_SESSION[$k]); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | unset($_SESSION[$key]); |
| 716 | } |
| 717 | |
| 718 | // ------------------------------------------------------------------------ |
| 719 | |
| 720 | /** |
| 721 | * All userdata (fetch) |
| 722 | * |
| 723 | * Legacy CI_Session compatibility method |
| 724 | * |
| 725 | * @return array $_SESSION, excluding flash data items |
| 726 | */ |
| 727 | public function all_userdata() |
| 728 | { |
| 729 | return $this->userdata(); |
| 730 | } |
| 731 | |
| 732 | // ------------------------------------------------------------------------ |
| 733 | |
| 734 | /** |
| 735 | * Has userdata |
| 736 | * |
| 737 | * Legacy CI_Session compatibility method |
| 738 | * |
| 739 | * @param string $key Session data key |
| 740 | * @return bool |
| 741 | */ |
| 742 | public function has_userdata($key) |
| 743 | { |
| 744 | return isset($_SESSION[$key]); |
| 745 | } |
| 746 | |
| 747 | // ------------------------------------------------------------------------ |
| 748 | |
| 749 | /** |
| 750 | * Flashdata (fetch) |
| 751 | * |
| 752 | * Legacy CI_Session compatibility method |
| 753 | * |
| 754 | * @param string $key Session data key |
| 755 | * @return mixed Session data value or NULL if not found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 756 | */ |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 757 | public function flashdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 758 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 759 | if (isset($key)) |
| 760 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 761 | return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key])) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 762 | ? $_SESSION[$key] |
| 763 | : NULL; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 766 | $flashdata = array(); |
| 767 | |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 768 | if ( ! empty($_SESSION['__ci_vars'])) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 769 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 770 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 771 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 772 | is_int($value) OR $flashdata[$key] = $_SESSION[$key]; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 776 | return $flashdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 777 | } |
| 778 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 779 | // ------------------------------------------------------------------------ |
| 780 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 781 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 782 | * Set flashdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 783 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 784 | * Legacy CI_Session compatibiliy method |
| 785 | * |
| 786 | * @param mixed $data Session data key or an associative array |
| 787 | * @param mixed $value Value to store |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 788 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 789 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 790 | public function set_flashdata($data, $value = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 791 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 792 | $this->set_userdata($data, $value); |
Andrey Andreev | c6e5098 | 2014-10-26 21:27:28 +0200 | [diff] [blame] | 793 | $this->mark_as_flash(is_array($data) ? array_keys($data) : $data); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 794 | } |
| 795 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 796 | // ------------------------------------------------------------------------ |
| 797 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 798 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 799 | * Keep flashdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 800 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 801 | * Legacy CI_Session compatibility method |
| 802 | * |
| 803 | * @param mixed $key Session data key(s) |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 804 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 805 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 806 | public function keep_flashdata($key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 807 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 808 | $this->mark_as_flash($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 809 | } |
| 810 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 811 | // ------------------------------------------------------------------------ |
| 812 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 813 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 814 | * Temp data (fetch) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 815 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 816 | * Legacy CI_Session compatibility method |
| 817 | * |
| 818 | * @param string $key Session data key |
| 819 | * @return mixed Session data value or NULL if not found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 820 | */ |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 821 | public function tempdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 822 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 823 | if (isset($key)) |
| 824 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 825 | return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key])) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 826 | ? $_SESSION[$key] |
| 827 | : NULL; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 830 | $tempdata = array(); |
| 831 | |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 832 | if ( ! empty($_SESSION['__ci_vars'])) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 833 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 834 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 835 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 836 | is_int($value) && $tempdata[$key] = $_SESSION[$key]; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 840 | return $tempdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 841 | } |
| 842 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 843 | // ------------------------------------------------------------------------ |
| 844 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 845 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 846 | * Set tempdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 847 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 848 | * Legacy CI_Session compatibility method |
| 849 | * |
| 850 | * @param mixed $data Session data key or an associative array of items |
| 851 | * @param mixed $value Value to store |
| 852 | * @param int $ttl Time-to-live in seconds |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 853 | * @return void |
| 854 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 855 | public function set_tempdata($data, $value = NULL, $ttl = 300) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 856 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 857 | $this->set_userdata($data, $value); |
| 858 | $this->mark_as_temp($data, $ttl); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 859 | } |
| 860 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 861 | // ------------------------------------------------------------------------ |
| 862 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 863 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 864 | * Unset tempdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 865 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 866 | * Legacy CI_Session compatibility method |
| 867 | * |
| 868 | * @param mixed $data Session data key(s) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 869 | * @return void |
| 870 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 871 | public function unset_tempdata($key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 872 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 873 | $this->unmark_temp($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 874 | } |
| 875 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 876 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 877 | |
| 878 | /* End of file Session.php */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 879 | /* Location: ./system/libraries/Session/Session.php */ |