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