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 | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 125 | // Work-around for PHP bug #66827 (https://bugs.php.net/bug.php?id=66827) |
| 126 | // |
| 127 | // The session ID sanitizer doesn't check for the value type and blindly does |
| 128 | // an implicit cast to string, which triggers an 'Array to string' E_NOTICE. |
| 129 | if (isset($_COOKIE[$this->_cookie_name]) && ! is_string($_COOKIE[$this->_cookie_name])) |
| 130 | { |
| 131 | unset($_COOKIE[$this->_cookie_name]); |
| 132 | } |
| 133 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 134 | session_start(); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 135 | |
Andrey Andreev | ff37ffe | 2014-11-04 12:28:57 +0200 | [diff] [blame] | 136 | // Is session ID auto-regeneration configured? (ignoring ajax requests) |
| 137 | if ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) |
| 138 | && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest' |
Andrey Andreev | de5c246 | 2014-11-04 12:31:03 +0200 | [diff] [blame] | 139 | && ($regenerate_time = config_item('sess_time_to_update')) > 0 |
Andrey Andreev | ff37ffe | 2014-11-04 12:28:57 +0200 | [diff] [blame] | 140 | ) |
Andrey Andreev | 8e60b9a | 2014-11-04 11:08:06 +0200 | [diff] [blame] | 141 | { |
| 142 | if ( ! isset($_SESSION['__ci_last_regenerate'])) |
| 143 | { |
| 144 | $_SESSION['__ci_last_regenerate'] = time(); |
| 145 | } |
| 146 | elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time)) |
| 147 | { |
| 148 | $this->sess_regenerate(FALSE); |
| 149 | } |
| 150 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 151 | // Another work-around ... PHP doesn't seem to send the session cookie |
| 152 | // unless it is being currently created or regenerated |
Andrey Andreev | 8e60b9a | 2014-11-04 11:08:06 +0200 | [diff] [blame] | 153 | 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] | 154 | { |
| 155 | setcookie( |
| 156 | $this->_config['cookie_name'], |
| 157 | session_id(), |
| 158 | (empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']), |
| 159 | $this->_config['cookie_path'], |
| 160 | $this->_config['cookie_domain'], |
| 161 | $this->_config['cookie_secure'], |
| 162 | TRUE |
| 163 | ); |
| 164 | } |
| 165 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 166 | $this->_ci_init_vars(); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 167 | /* |
| 168 | Need to test if this is necessary for a custom driver or if it's only |
| 169 | relevant to PHP's own files handler. |
| 170 | |
| 171 | https://bugs.php.net/bug.php?id=65475 |
| 172 | do this after session is started: |
| 173 | if (is_php('5.5.2') && ! is_php('5.5.4')) |
| 174 | { |
| 175 | $session_id = session_id(); |
| 176 | if ($_COOKIE[$this->_cookie_name] !== $session_id && file_exists(teh file)) |
| 177 | { |
| 178 | unlink(<teh file>); |
| 179 | } |
| 180 | } |
| 181 | */ |
| 182 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 183 | log_message('debug', "Session: Class initialized using '".$this->_driver."' driver."); |
| 184 | } |
| 185 | |
| 186 | // ------------------------------------------------------------------------ |
| 187 | |
| 188 | protected function _ci_load_classes($driver) |
| 189 | { |
| 190 | // PHP 5.4 compatibility |
| 191 | interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php'); |
| 192 | |
| 193 | $prefix = config_item('subclass_prefix'); |
| 194 | |
| 195 | if ( ! class_exists('CI_Session_driver', FALSE)) |
| 196 | { |
Andrey Andreev | e86603f | 2014-06-11 14:03:36 +0300 | [diff] [blame] | 197 | require_once( |
| 198 | file_exists(APPPATH.'libraries/Session/Session_driver.php') |
| 199 | ? APPPATH.'libraries/Session/Session_driver.php' |
| 200 | : BASEPATH.'libraries/Session/Session_driver.php' |
| 201 | ); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 202 | |
| 203 | if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php')) |
| 204 | { |
| 205 | require_once($file_path); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | $class = 'Session_'.$driver.'_driver'; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 210 | if ( ! class_exists('CI_'.$class, FALSE)) |
| 211 | { |
| 212 | if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php')) |
| 213 | { |
| 214 | require_once($file_path); |
| 215 | } |
| 216 | |
| 217 | if ( ! class_exists('CI_'.$class, FALSE)) |
| 218 | { |
| 219 | log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting."); |
| 220 | return FALSE; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php')) |
| 225 | { |
| 226 | require_once($file_path); |
| 227 | if (class_exists($prefix.$class, FALSE)) |
| 228 | { |
| 229 | return $prefix.$class; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.'); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return 'CI_'.$class; |
| 238 | } |
| 239 | |
| 240 | // ------------------------------------------------------------------------ |
| 241 | |
| 242 | /** |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 243 | * Configuration |
| 244 | * |
| 245 | * Handle input parameters and configuration defaults |
| 246 | * |
| 247 | * @param array &$params Input parameters |
| 248 | * @return void |
| 249 | */ |
| 250 | protected function _configure(&$params) |
| 251 | { |
| 252 | $expiration = config_item('sess_expiration'); |
| 253 | |
| 254 | if (isset($params['cookie_lifetime'])) |
| 255 | { |
| 256 | $params['cookie_lifetime'] = (int) $params['cookie_lifetime']; |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close')) |
| 261 | ? 0 : (int) $expiration; |
| 262 | } |
| 263 | |
| 264 | isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name'); |
| 265 | if (empty($params['cookie_name'])) |
| 266 | { |
| 267 | $params['cookie_name'] = ini_get('session.name'); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | ini_set('session.name', $params['cookie_name']); |
| 272 | } |
| 273 | |
| 274 | isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path'); |
Andrey Andreev | 41b546d | 2014-10-06 03:01:22 +0300 | [diff] [blame] | 275 | isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain'); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 276 | isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure'); |
| 277 | |
| 278 | session_set_cookie_params( |
| 279 | $params['cookie_lifetime'], |
| 280 | $params['cookie_path'], |
| 281 | $params['cookie_domain'], |
| 282 | $params['cookie_secure'], |
| 283 | TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons |
| 284 | ); |
| 285 | |
| 286 | if (empty($expiration)) |
| 287 | { |
| 288 | $params['expiration'] = (int) ini_get('session.gc_maxlifetime'); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | $params['expiration'] = (int) $expiration; |
| 293 | ini_set('session.gc_maxlifetime', $expiration); |
| 294 | } |
| 295 | |
| 296 | $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip')); |
| 297 | |
| 298 | isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path'); |
| 299 | |
| 300 | $this->_config = $params; |
| 301 | |
| 302 | // Security is king |
| 303 | ini_set('session.use_trans_id', 0); |
| 304 | ini_set('session.use_strict_mode', 1); |
| 305 | ini_set('session.use_cookies', 1); |
| 306 | ini_set('session.use_only_cookies', 1); |
| 307 | ini_set('session.hash_function', 1); |
| 308 | ini_set('session.hash_bits_per_character', 4); |
| 309 | } |
| 310 | |
| 311 | // ------------------------------------------------------------------------ |
| 312 | |
| 313 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 314 | * Handle temporary variables |
| 315 | * |
| 316 | * Clears old "flash" data, marks the new one for deletion and handles |
| 317 | * "temp" data deletion. |
| 318 | * |
| 319 | * @return void |
| 320 | */ |
| 321 | protected function _ci_init_vars() |
| 322 | { |
| 323 | if ( ! empty($_SESSION['__ci_vars'])) |
| 324 | { |
| 325 | $current_time = time(); |
| 326 | |
| 327 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
| 328 | { |
| 329 | if ($value === 'new') |
| 330 | { |
| 331 | $_SESSION['__ci_vars'][$key] = 'old'; |
| 332 | } |
| 333 | // Hacky, but 'old' will (implicitly) always be less than time() ;) |
| 334 | // DO NOT move this above the 'new' check! |
| 335 | elseif ($value < $current_time) |
| 336 | { |
| 337 | unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (empty($_SESSION['__ci_vars'])) |
| 342 | { |
| 343 | unset($_SESSION['__ci_vars']); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 348 | // ------------------------------------------------------------------------ |
| 349 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 350 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 351 | * Mark as flash |
| 352 | * |
| 353 | * @param mixed $key Session data key(s) |
| 354 | * @return bool |
| 355 | */ |
| 356 | public function mark_as_flash($key) |
| 357 | { |
| 358 | if (is_array($key)) |
| 359 | { |
| 360 | for ($i = 0, $c = count($key); $i < $c; $i++) |
| 361 | { |
| 362 | if ( ! isset($_SESSION[$key[$i]])) |
| 363 | { |
| 364 | return FALSE; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | $new = array_fill_keys($key, 'new'); |
| 369 | |
| 370 | $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) |
| 371 | ? array_merge($_SESSION['__ci_vars'], $new) |
| 372 | : $new; |
| 373 | |
| 374 | return TRUE; |
| 375 | } |
| 376 | |
| 377 | if ( ! isset($_SESSION[$key])) |
| 378 | { |
| 379 | return FALSE; |
| 380 | } |
| 381 | |
| 382 | $_SESSION['__ci_vars'][$key] = 'new'; |
| 383 | return TRUE; |
| 384 | } |
| 385 | |
| 386 | // ------------------------------------------------------------------------ |
| 387 | |
| 388 | /** |
| 389 | * Get flash keys |
| 390 | * |
| 391 | * @return array |
| 392 | */ |
| 393 | public function get_flash_keys() |
| 394 | { |
| 395 | if ( ! isset($_SESSION['__ci_vars'])) |
| 396 | { |
| 397 | return array(); |
| 398 | } |
| 399 | |
| 400 | $keys = array(); |
| 401 | foreach (array_keys($_SESSION['__ci_vars']) as $key) |
| 402 | { |
| 403 | is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key; |
| 404 | } |
| 405 | |
| 406 | return $keys; |
| 407 | } |
| 408 | |
| 409 | // ------------------------------------------------------------------------ |
| 410 | |
| 411 | /** |
| 412 | * Unmark flash |
| 413 | * |
| 414 | * @param mixed $key Session data key(s) |
| 415 | * @return void |
| 416 | */ |
| 417 | public function unmark_flash($key) |
| 418 | { |
| 419 | if (empty($_SESSION['__ci_vars'])) |
| 420 | { |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | is_array($key) OR $key = array($key); |
| 425 | |
| 426 | foreach ($key as $k) |
| 427 | { |
| 428 | if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k])) |
| 429 | { |
| 430 | unset($_SESSION['__ci_vars'][$k]); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (empty($_SESSION['__ci_vars'])) |
| 435 | { |
| 436 | unset($_SESSION['__ci_vars']); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // ------------------------------------------------------------------------ |
| 441 | |
| 442 | /** |
| 443 | * Mark as temp |
| 444 | * |
| 445 | * @param mixed $key Session data key(s) |
| 446 | * @param int $ttl Time-to-live in seconds |
| 447 | * @return bool |
| 448 | */ |
| 449 | public function mark_as_temp($key, $ttl = 300) |
| 450 | { |
| 451 | $ttl += time(); |
| 452 | |
| 453 | if (is_array($key)) |
| 454 | { |
| 455 | $temp = array(); |
| 456 | |
| 457 | foreach ($key as $k => $v) |
| 458 | { |
| 459 | // Do we have a key => ttl pair, or just a key? |
| 460 | if (is_int($k)) |
| 461 | { |
| 462 | $k = $v; |
| 463 | $v = $ttl; |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | $v += time(); |
| 468 | } |
| 469 | |
| 470 | if ( ! isset($_SESSION[$k])) |
| 471 | { |
| 472 | return FALSE; |
| 473 | } |
| 474 | |
| 475 | $temp[$k] = $ts; |
| 476 | } |
| 477 | |
| 478 | $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) |
| 479 | ? array_merge($_SESSION['__ci_vars'], $temp) |
| 480 | : $temp; |
| 481 | |
| 482 | return TRUE; |
| 483 | } |
| 484 | |
| 485 | if ( ! isset($_SESSION[$key])) |
| 486 | { |
| 487 | return FALSE; |
| 488 | } |
| 489 | |
| 490 | $_SESSION['__ci_vars'][$key] = $ttl; |
| 491 | return TRUE; |
| 492 | } |
| 493 | |
| 494 | // ------------------------------------------------------------------------ |
| 495 | |
| 496 | /** |
| 497 | * Get temp keys |
| 498 | * |
| 499 | * @return array |
| 500 | */ |
| 501 | public function get_temp_keys() |
| 502 | { |
| 503 | if ( ! isset($_SESSION['__ci_vars'])) |
| 504 | { |
| 505 | return array(); |
| 506 | } |
| 507 | |
| 508 | $keys = array(); |
| 509 | foreach (array_keys($_SESSION['__ci_vars']) as $key) |
| 510 | { |
| 511 | is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key; |
| 512 | } |
| 513 | |
| 514 | return $keys; |
| 515 | } |
| 516 | |
| 517 | // ------------------------------------------------------------------------ |
| 518 | |
| 519 | /** |
| 520 | * Unmark flash |
| 521 | * |
| 522 | * @param mixed $key Session data key(s) |
| 523 | * @return void |
| 524 | */ |
| 525 | public function unmark_temp($key) |
| 526 | { |
| 527 | if (empty($_SESSION['__ci_vars'])) |
| 528 | { |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | is_array($key) OR $key = array($key); |
| 533 | |
| 534 | foreach ($key as $k) |
| 535 | { |
| 536 | if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k])) |
| 537 | { |
| 538 | unset($_SESSION['__ci_vars'][$k]); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (empty($_SESSION['__ci_vars'])) |
| 543 | { |
| 544 | unset($_SESSION['__ci_vars']); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // ------------------------------------------------------------------------ |
| 549 | |
| 550 | /** |
| 551 | * __get() |
| 552 | * |
| 553 | * @param string $key 'session_id' or a session data key |
| 554 | * @return mixed |
| 555 | */ |
| 556 | public function __get($key) |
| 557 | { |
| 558 | // Note: Keep this order the same, just in case somebody wants to |
| 559 | // use 'session_id' as a session data key, for whatever reason |
| 560 | if (isset($_SESSION[$key])) |
| 561 | { |
| 562 | return $_SESSION[$key]; |
| 563 | } |
| 564 | elseif ($key === 'session_id') |
| 565 | { |
| 566 | return session_id(); |
| 567 | } |
| 568 | |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | // ------------------------------------------------------------------------ |
| 573 | |
| 574 | /** |
| 575 | * __set() |
| 576 | * |
| 577 | * @param string $key Session data key |
| 578 | * @param mixed $value Session data value |
| 579 | * @return void |
| 580 | */ |
| 581 | public function __set($key, $value) |
| 582 | { |
| 583 | $_SESSION[$key] = $value; |
| 584 | } |
| 585 | |
| 586 | // ------------------------------------------------------------------------ |
| 587 | |
| 588 | /** |
| 589 | * Session destroy |
| 590 | * |
| 591 | * Legacy CI_Session compatibility method |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 592 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 593 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 594 | */ |
| 595 | public function sess_destroy() |
| 596 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 597 | session_destroy(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 598 | } |
| 599 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 600 | // ------------------------------------------------------------------------ |
| 601 | |
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 | * Session regenerate |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 604 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 605 | * Legacy CI_Session compatibility method |
| 606 | * |
| 607 | * @param bool $destroy Destroy old session data flag |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 608 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 609 | */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 610 | public function sess_regenerate($destroy = FALSE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 611 | { |
Andrey Andreev | 8e60b9a | 2014-11-04 11:08:06 +0200 | [diff] [blame] | 612 | $_SESSION['__ci_last_regenerate'] = time(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 613 | session_regenerate_id($destroy); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 614 | } |
| 615 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 616 | // ------------------------------------------------------------------------ |
| 617 | |
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 | * Get userdata reference |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 620 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 621 | * Legacy CI_Session compatibility method |
| 622 | * |
| 623 | * @returns array |
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 | public function &get_userdata() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 626 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 627 | return $_SESSION; |
| 628 | } |
| 629 | |
| 630 | // ------------------------------------------------------------------------ |
| 631 | |
| 632 | /** |
| 633 | * Userdata (fetch) |
| 634 | * |
| 635 | * Legacy CI_Session compatibility method |
| 636 | * |
| 637 | * @param string $key Session data key |
| 638 | * @return mixed Session data value or NULL if not found |
| 639 | */ |
| 640 | public function userdata($key = NULL) |
| 641 | { |
| 642 | if (isset($key)) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 643 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 644 | return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL; |
| 645 | } |
| 646 | elseif (empty($_SESSION)) |
| 647 | { |
| 648 | return array(); |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 651 | $userdata = array(); |
| 652 | $_exclude = array_merge( |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 653 | array('__ci_vars'), |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 654 | $this->get_flash_keys(), |
| 655 | $this->get_temp_keys() |
| 656 | ); |
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 | foreach (array_keys($_SESSION) as $key) |
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 | if ( ! in_array($key, $_exclude, TRUE)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 661 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 662 | $userdata[$key] = $_SESSION[$key]; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 666 | return $userdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 667 | } |
| 668 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 669 | // ------------------------------------------------------------------------ |
| 670 | |
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 | * Set userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 673 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 674 | * Legacy CI_Session compatibility method |
| 675 | * |
| 676 | * @param mixed $data Session data key or an associative array |
| 677 | * @param mixed $value Value to store |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 678 | * @return void |
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 | public function set_userdata($data, $value = NULL) |
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 | if (is_array($data)) |
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 | foreach ($data as $key => &$value) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 685 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 686 | $_SESSION[$key] = $value; |
Johnathan Croom | 8d8543d | 2012-11-25 10:36:57 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | return; |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 690 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 691 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 692 | $_SESSION[$data] = $value; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 693 | } |
| 694 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 695 | // ------------------------------------------------------------------------ |
| 696 | |
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 | * Unset userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 699 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 700 | * Legacy CI_Session compatibility method |
| 701 | * |
| 702 | * @param mixed $data Session data key(s) |
| 703 | * @return void |
| 704 | */ |
| 705 | public function unset_userdata($key) |
| 706 | { |
| 707 | if (is_array($key)) |
| 708 | { |
| 709 | foreach ($key as $k) |
| 710 | { |
Andrey Andreev | d069b9b | 2014-09-16 10:18:16 +0300 | [diff] [blame] | 711 | unset($_SESSION[$k]); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | unset($_SESSION[$key]); |
| 718 | } |
| 719 | |
| 720 | // ------------------------------------------------------------------------ |
| 721 | |
| 722 | /** |
| 723 | * All userdata (fetch) |
| 724 | * |
| 725 | * Legacy CI_Session compatibility method |
| 726 | * |
| 727 | * @return array $_SESSION, excluding flash data items |
| 728 | */ |
| 729 | public function all_userdata() |
| 730 | { |
| 731 | return $this->userdata(); |
| 732 | } |
| 733 | |
| 734 | // ------------------------------------------------------------------------ |
| 735 | |
| 736 | /** |
| 737 | * Has userdata |
| 738 | * |
| 739 | * Legacy CI_Session compatibility method |
| 740 | * |
| 741 | * @param string $key Session data key |
| 742 | * @return bool |
| 743 | */ |
| 744 | public function has_userdata($key) |
| 745 | { |
| 746 | return isset($_SESSION[$key]); |
| 747 | } |
| 748 | |
| 749 | // ------------------------------------------------------------------------ |
| 750 | |
| 751 | /** |
| 752 | * Flashdata (fetch) |
| 753 | * |
| 754 | * Legacy CI_Session compatibility method |
| 755 | * |
| 756 | * @param string $key Session data key |
| 757 | * @return mixed Session data value or NULL if not found |
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 | public function flashdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 760 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 761 | if (isset($key)) |
| 762 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 763 | 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] | 764 | ? $_SESSION[$key] |
| 765 | : NULL; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 768 | $flashdata = array(); |
| 769 | |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 770 | if ( ! empty($_SESSION['__ci_vars'])) |
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 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 773 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 774 | is_int($value) OR $flashdata[$key] = $_SESSION[$key]; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 778 | return $flashdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 779 | } |
| 780 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 781 | // ------------------------------------------------------------------------ |
| 782 | |
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 | * Set flashdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 785 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 786 | * Legacy CI_Session compatibiliy method |
| 787 | * |
| 788 | * @param mixed $data Session data key or an associative array |
| 789 | * @param mixed $value Value to store |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 790 | * @return void |
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 | public function set_flashdata($data, $value = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 793 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 794 | $this->set_userdata($data, $value); |
Andrey Andreev | c6e5098 | 2014-10-26 21:27:28 +0200 | [diff] [blame] | 795 | $this->mark_as_flash(is_array($data) ? array_keys($data) : $data); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 796 | } |
| 797 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 798 | // ------------------------------------------------------------------------ |
| 799 | |
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 | * Keep flashdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 802 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 803 | * Legacy CI_Session compatibility method |
| 804 | * |
| 805 | * @param mixed $key Session data key(s) |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 806 | * @return void |
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 | public function keep_flashdata($key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 809 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 810 | $this->mark_as_flash($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 811 | } |
| 812 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 813 | // ------------------------------------------------------------------------ |
| 814 | |
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 | * Temp data (fetch) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 817 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 818 | * Legacy CI_Session compatibility method |
| 819 | * |
| 820 | * @param string $key Session data key |
| 821 | * @return mixed Session data value or NULL if not found |
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 | public function tempdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 824 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 825 | if (isset($key)) |
| 826 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 827 | 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] | 828 | ? $_SESSION[$key] |
| 829 | : NULL; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 832 | $tempdata = array(); |
| 833 | |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 834 | if ( ! empty($_SESSION['__ci_vars'])) |
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 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 837 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 838 | is_int($value) && $tempdata[$key] = $_SESSION[$key]; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 842 | return $tempdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 843 | } |
| 844 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 845 | // ------------------------------------------------------------------------ |
| 846 | |
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 | * Set tempdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 849 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 850 | * Legacy CI_Session compatibility method |
| 851 | * |
| 852 | * @param mixed $data Session data key or an associative array of items |
| 853 | * @param mixed $value Value to store |
| 854 | * @param int $ttl Time-to-live in seconds |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 855 | * @return void |
| 856 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 857 | public function set_tempdata($data, $value = NULL, $ttl = 300) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 858 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 859 | $this->set_userdata($data, $value); |
| 860 | $this->mark_as_temp($data, $ttl); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 861 | } |
| 862 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 863 | // ------------------------------------------------------------------------ |
| 864 | |
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 | * Unset tempdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 867 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 868 | * Legacy CI_Session compatibility method |
| 869 | * |
| 870 | * @param mixed $data Session data key(s) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 871 | * @return void |
| 872 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 873 | public function unset_tempdata($key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 874 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 875 | $this->unmark_temp($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 876 | } |
| 877 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 878 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 879 | |
| 880 | /* End of file Session.php */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 881 | /* Location: ./system/libraries/Session/Session.php */ |