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