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