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 | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, 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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, 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 |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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 |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/sessions.html |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 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; |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 60 | protected $_sid_regexp; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 61 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 62 | // ------------------------------------------------------------------------ |
| 63 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 64 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 65 | * Class constructor |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 66 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 67 | * @param array $params Configuration parameters |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 68 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 69 | */ |
| 70 | public function __construct(array $params = array()) |
| 71 | { |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 72 | // No sessions under CLI |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 73 | if (is_cli()) |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 74 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 75 | log_message('debug', 'Session: Initialization under CLI aborted.'); |
| 76 | return; |
| 77 | } |
| 78 | elseif ((bool) ini_get('session.auto_start')) |
| 79 | { |
| 80 | log_message('error', 'Session: session.auto_start is enabled in php.ini. Aborting.'); |
| 81 | return; |
| 82 | } |
| 83 | elseif ( ! empty($params['driver'])) |
| 84 | { |
| 85 | $this->_driver = $params['driver']; |
| 86 | unset($params['driver']); |
| 87 | } |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 88 | elseif ($driver = config_item('sess_driver')) |
Andrey Andreev | 34b1ef5 | 2014-05-31 21:23:41 +0300 | [diff] [blame] | 89 | { |
| 90 | $this->_driver = $driver; |
| 91 | } |
Andrey Andreev | ac4f472 | 2014-06-02 11:16:32 +0300 | [diff] [blame] | 92 | // Note: BC workaround |
| 93 | elseif (config_item('sess_use_database')) |
| 94 | { |
Andrey Andreev | 85dfc2a | 2016-04-01 22:54:15 +0300 | [diff] [blame] | 95 | log_message('debug', 'Session: "sess_driver" is empty; using BC fallback to "sess_use_database".'); |
Andrey Andreev | ac4f472 | 2014-06-02 11:16:32 +0300 | [diff] [blame] | 96 | $this->_driver = 'database'; |
| 97 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 98 | |
Andrey Andreev | 34b92c6 | 2015-03-12 12:42:00 +0200 | [diff] [blame] | 99 | $class = $this->_ci_load_classes($this->_driver); |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 100 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 101 | // Configuration ... |
| 102 | $this->_configure($params); |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 103 | $this->_config['_sid_regexp'] = $this->_sid_regexp; |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 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']]) |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 136 | OR ! preg_match('#\A'.$this->_sid_regexp.'\z#', $_COOKIE[$this->_config['cookie_name']]) |
Andrey Andreev | 562e39b | 2014-11-12 15:38:58 +0200 | [diff] [blame] | 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 | { |
Andrey Andreev | 1d19520 | 2015-03-13 11:25:29 +0200 | [diff] [blame] | 233 | throw new UnexpectedValueException("Session: Configured driver '".$driver."' was not found. Aborting."); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Andrey Andreev | 738b9e3 | 2016-02-24 12:14:10 +0200 | [diff] [blame] | 237 | if ( ! class_exists($prefix.$class, FALSE) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php')) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 238 | { |
| 239 | require_once($file_path); |
| 240 | if (class_exists($prefix.$class, FALSE)) |
| 241 | { |
| 242 | return $prefix.$class; |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.'); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return 'CI_'.$class; |
| 251 | } |
| 252 | |
| 253 | // ------------------------------------------------------------------------ |
| 254 | |
| 255 | /** |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 256 | * Configuration |
| 257 | * |
| 258 | * Handle input parameters and configuration defaults |
| 259 | * |
| 260 | * @param array &$params Input parameters |
| 261 | * @return void |
| 262 | */ |
| 263 | protected function _configure(&$params) |
| 264 | { |
| 265 | $expiration = config_item('sess_expiration'); |
| 266 | |
| 267 | if (isset($params['cookie_lifetime'])) |
| 268 | { |
| 269 | $params['cookie_lifetime'] = (int) $params['cookie_lifetime']; |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close')) |
| 274 | ? 0 : (int) $expiration; |
| 275 | } |
| 276 | |
| 277 | isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name'); |
| 278 | if (empty($params['cookie_name'])) |
| 279 | { |
| 280 | $params['cookie_name'] = ini_get('session.name'); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | ini_set('session.name', $params['cookie_name']); |
| 285 | } |
| 286 | |
| 287 | isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path'); |
Andrey Andreev | 41b546d | 2014-10-06 03:01:22 +0300 | [diff] [blame] | 288 | isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain'); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 289 | isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure'); |
| 290 | |
| 291 | session_set_cookie_params( |
| 292 | $params['cookie_lifetime'], |
| 293 | $params['cookie_path'], |
| 294 | $params['cookie_domain'], |
| 295 | $params['cookie_secure'], |
| 296 | TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons |
| 297 | ); |
| 298 | |
| 299 | if (empty($expiration)) |
| 300 | { |
| 301 | $params['expiration'] = (int) ini_get('session.gc_maxlifetime'); |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | $params['expiration'] = (int) $expiration; |
| 306 | ini_set('session.gc_maxlifetime', $expiration); |
| 307 | } |
| 308 | |
| 309 | $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip')); |
| 310 | |
| 311 | isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path'); |
| 312 | |
| 313 | $this->_config = $params; |
| 314 | |
| 315 | // Security is king |
Andrey Andreev | c02952d | 2015-02-13 13:04:38 +0200 | [diff] [blame] | 316 | ini_set('session.use_trans_sid', 0); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 317 | ini_set('session.use_strict_mode', 1); |
| 318 | ini_set('session.use_cookies', 1); |
| 319 | ini_set('session.use_only_cookies', 1); |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 320 | |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 321 | $this->_configure_sid_length(); |
| 322 | } |
| 323 | |
| 324 | // ------------------------------------------------------------------------ |
| 325 | |
| 326 | /** |
| 327 | * Configure session ID length |
| 328 | * |
| 329 | * To make life easier, we used to force SHA-1 and 4 bits per |
| 330 | * character on everyone. And of course, someone was unhappy. |
| 331 | * |
| 332 | * Then PHP 7.1 broke backwards-compatibility because ext/session |
| 333 | * is such a mess that nobody wants to touch it with a pole stick, |
| 334 | * and the one guy who does, nobody has the energy to argue with. |
| 335 | * |
| 336 | * So we were forced to make changes, and OF COURSE something was |
| 337 | * going to break and now we have this pile of shit. -- Narf |
| 338 | * |
| 339 | * @return void |
| 340 | */ |
| 341 | protected function _configure_sid_length() |
| 342 | { |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 343 | if (PHP_VERSION_ID < 70100) |
| 344 | { |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 345 | $hash_function = ini_get('session.hash_function'); |
| 346 | if (ctype_digit($hash_function)) |
| 347 | { |
| 348 | if ($hash_function !== '1') |
| 349 | { |
| 350 | ini_set('session.hash_function', 1); |
| 351 | $bits = 160; |
| 352 | } |
| 353 | } |
| 354 | elseif ( ! in_array($hash_function, hash_algos(), TRUE)) |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 355 | { |
| 356 | ini_set('session.hash_function', 1); |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 357 | $bits = 160; |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 358 | } |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 359 | elseif (($bits = strlen(hash($hash_function, 'dummy', false)) * 4) < 160) |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 360 | { |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 361 | ini_set('session.hash_function', 1); |
| 362 | $bits = 160; |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 363 | } |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 364 | |
| 365 | $bits_per_character = (int) ini_get('session.hash_bits_per_character'); |
| 366 | $sid_length = $bits * $bits_per_character; |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 367 | } |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 368 | else |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 369 | { |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 370 | $bits_per_character = (int) ini_get('session.sid_bits_per_character'); |
| 371 | $sid_length = (int) ini_get('session.sid_length'); |
| 372 | if (($bits = $sid_length * $bits_per_character) < 160) |
| 373 | { |
| 374 | // Add as many more characters as necessary to reach at least 160 bits |
| 375 | $sid_length += (int) ceil((160 % $bits) / $bits_per_character); |
| 376 | ini_set('session.sid_length', $sid_length); |
| 377 | } |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 378 | } |
| 379 | |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 380 | // Yes, 4,5,6 are the only known possible values as of 2016-10-27 |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 381 | switch ($bits_per_character) |
| 382 | { |
| 383 | case 4: |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 384 | $this->_sid_regexp = '[0-9a-f]'; |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 385 | break; |
| 386 | case 5: |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 387 | $this->_sid_regexp = '[0-9a-v]'; |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 388 | break; |
| 389 | case 6: |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 390 | $this->_sid_regexp = '[0-9a-zA-Z,-]'; |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 391 | break; |
| 392 | } |
Andrey Andreev | 2f76087 | 2016-10-27 16:39:12 +0300 | [diff] [blame^] | 393 | |
| 394 | $this->_sid_regexp .= '{'.$sid_length.'}'; |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // ------------------------------------------------------------------------ |
| 398 | |
| 399 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 400 | * Handle temporary variables |
| 401 | * |
| 402 | * Clears old "flash" data, marks the new one for deletion and handles |
| 403 | * "temp" data deletion. |
| 404 | * |
| 405 | * @return void |
| 406 | */ |
| 407 | protected function _ci_init_vars() |
| 408 | { |
| 409 | if ( ! empty($_SESSION['__ci_vars'])) |
| 410 | { |
| 411 | $current_time = time(); |
| 412 | |
| 413 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
| 414 | { |
| 415 | if ($value === 'new') |
| 416 | { |
| 417 | $_SESSION['__ci_vars'][$key] = 'old'; |
| 418 | } |
| 419 | // Hacky, but 'old' will (implicitly) always be less than time() ;) |
| 420 | // DO NOT move this above the 'new' check! |
| 421 | elseif ($value < $current_time) |
| 422 | { |
| 423 | unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if (empty($_SESSION['__ci_vars'])) |
| 428 | { |
| 429 | unset($_SESSION['__ci_vars']); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 430 | } |
| 431 | } |
Andrey Andreev | b4b215e | 2015-01-19 11:59:11 +0200 | [diff] [blame] | 432 | |
| 433 | $this->userdata =& $_SESSION; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 436 | // ------------------------------------------------------------------------ |
| 437 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 438 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 439 | * Mark as flash |
| 440 | * |
| 441 | * @param mixed $key Session data key(s) |
| 442 | * @return bool |
| 443 | */ |
| 444 | public function mark_as_flash($key) |
| 445 | { |
| 446 | if (is_array($key)) |
| 447 | { |
| 448 | for ($i = 0, $c = count($key); $i < $c; $i++) |
| 449 | { |
| 450 | if ( ! isset($_SESSION[$key[$i]])) |
| 451 | { |
| 452 | return FALSE; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | $new = array_fill_keys($key, 'new'); |
| 457 | |
| 458 | $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) |
| 459 | ? array_merge($_SESSION['__ci_vars'], $new) |
| 460 | : $new; |
| 461 | |
| 462 | return TRUE; |
| 463 | } |
| 464 | |
| 465 | if ( ! isset($_SESSION[$key])) |
| 466 | { |
| 467 | return FALSE; |
| 468 | } |
| 469 | |
| 470 | $_SESSION['__ci_vars'][$key] = 'new'; |
| 471 | return TRUE; |
| 472 | } |
| 473 | |
| 474 | // ------------------------------------------------------------------------ |
| 475 | |
| 476 | /** |
| 477 | * Get flash keys |
| 478 | * |
| 479 | * @return array |
| 480 | */ |
| 481 | public function get_flash_keys() |
| 482 | { |
| 483 | if ( ! isset($_SESSION['__ci_vars'])) |
| 484 | { |
| 485 | return array(); |
| 486 | } |
| 487 | |
| 488 | $keys = array(); |
| 489 | foreach (array_keys($_SESSION['__ci_vars']) as $key) |
| 490 | { |
| 491 | is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key; |
| 492 | } |
| 493 | |
| 494 | return $keys; |
| 495 | } |
| 496 | |
| 497 | // ------------------------------------------------------------------------ |
| 498 | |
| 499 | /** |
| 500 | * Unmark flash |
| 501 | * |
| 502 | * @param mixed $key Session data key(s) |
| 503 | * @return void |
| 504 | */ |
| 505 | public function unmark_flash($key) |
| 506 | { |
| 507 | if (empty($_SESSION['__ci_vars'])) |
| 508 | { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | is_array($key) OR $key = array($key); |
| 513 | |
| 514 | foreach ($key as $k) |
| 515 | { |
| 516 | if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k])) |
| 517 | { |
| 518 | unset($_SESSION['__ci_vars'][$k]); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | if (empty($_SESSION['__ci_vars'])) |
| 523 | { |
| 524 | unset($_SESSION['__ci_vars']); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // ------------------------------------------------------------------------ |
| 529 | |
| 530 | /** |
| 531 | * Mark as temp |
| 532 | * |
| 533 | * @param mixed $key Session data key(s) |
| 534 | * @param int $ttl Time-to-live in seconds |
| 535 | * @return bool |
| 536 | */ |
| 537 | public function mark_as_temp($key, $ttl = 300) |
| 538 | { |
| 539 | $ttl += time(); |
| 540 | |
| 541 | if (is_array($key)) |
| 542 | { |
| 543 | $temp = array(); |
| 544 | |
| 545 | foreach ($key as $k => $v) |
| 546 | { |
| 547 | // Do we have a key => ttl pair, or just a key? |
| 548 | if (is_int($k)) |
| 549 | { |
| 550 | $k = $v; |
| 551 | $v = $ttl; |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | $v += time(); |
| 556 | } |
| 557 | |
| 558 | if ( ! isset($_SESSION[$k])) |
| 559 | { |
| 560 | return FALSE; |
| 561 | } |
| 562 | |
Andrey Andreev | 43df7bd | 2015-02-02 23:22:29 +0200 | [diff] [blame] | 563 | $temp[$k] = $v; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) |
| 567 | ? array_merge($_SESSION['__ci_vars'], $temp) |
| 568 | : $temp; |
| 569 | |
| 570 | return TRUE; |
| 571 | } |
| 572 | |
| 573 | if ( ! isset($_SESSION[$key])) |
| 574 | { |
| 575 | return FALSE; |
| 576 | } |
| 577 | |
| 578 | $_SESSION['__ci_vars'][$key] = $ttl; |
| 579 | return TRUE; |
| 580 | } |
| 581 | |
| 582 | // ------------------------------------------------------------------------ |
| 583 | |
| 584 | /** |
| 585 | * Get temp keys |
| 586 | * |
| 587 | * @return array |
| 588 | */ |
| 589 | public function get_temp_keys() |
| 590 | { |
| 591 | if ( ! isset($_SESSION['__ci_vars'])) |
| 592 | { |
| 593 | return array(); |
| 594 | } |
| 595 | |
| 596 | $keys = array(); |
| 597 | foreach (array_keys($_SESSION['__ci_vars']) as $key) |
| 598 | { |
| 599 | is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key; |
| 600 | } |
| 601 | |
| 602 | return $keys; |
| 603 | } |
| 604 | |
| 605 | // ------------------------------------------------------------------------ |
| 606 | |
| 607 | /** |
| 608 | * Unmark flash |
| 609 | * |
| 610 | * @param mixed $key Session data key(s) |
| 611 | * @return void |
| 612 | */ |
| 613 | public function unmark_temp($key) |
| 614 | { |
| 615 | if (empty($_SESSION['__ci_vars'])) |
| 616 | { |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | is_array($key) OR $key = array($key); |
| 621 | |
| 622 | foreach ($key as $k) |
| 623 | { |
| 624 | if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k])) |
| 625 | { |
| 626 | unset($_SESSION['__ci_vars'][$k]); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (empty($_SESSION['__ci_vars'])) |
| 631 | { |
| 632 | unset($_SESSION['__ci_vars']); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | // ------------------------------------------------------------------------ |
| 637 | |
| 638 | /** |
| 639 | * __get() |
| 640 | * |
| 641 | * @param string $key 'session_id' or a session data key |
| 642 | * @return mixed |
| 643 | */ |
| 644 | public function __get($key) |
| 645 | { |
| 646 | // Note: Keep this order the same, just in case somebody wants to |
| 647 | // use 'session_id' as a session data key, for whatever reason |
| 648 | if (isset($_SESSION[$key])) |
| 649 | { |
| 650 | return $_SESSION[$key]; |
| 651 | } |
| 652 | elseif ($key === 'session_id') |
| 653 | { |
| 654 | return session_id(); |
| 655 | } |
| 656 | |
| 657 | return NULL; |
| 658 | } |
| 659 | |
| 660 | // ------------------------------------------------------------------------ |
| 661 | |
| 662 | /** |
Andrey Andreev | 2c10f60 | 2016-03-15 14:39:02 +0200 | [diff] [blame] | 663 | * __isset() |
| 664 | * |
| 665 | * @param string $key 'session_id' or a session data key |
| 666 | * @return bool |
| 667 | */ |
| 668 | public function __isset($key) |
| 669 | { |
| 670 | if ($key === 'session_id') |
| 671 | { |
| 672 | return (session_status() === PHP_SESSION_ACTIVE); |
| 673 | } |
| 674 | |
| 675 | return isset($_SESSION[$key]); |
| 676 | } |
| 677 | |
| 678 | // ------------------------------------------------------------------------ |
| 679 | |
| 680 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 681 | * __set() |
| 682 | * |
| 683 | * @param string $key Session data key |
| 684 | * @param mixed $value Session data value |
| 685 | * @return void |
| 686 | */ |
| 687 | public function __set($key, $value) |
| 688 | { |
| 689 | $_SESSION[$key] = $value; |
| 690 | } |
| 691 | |
| 692 | // ------------------------------------------------------------------------ |
| 693 | |
| 694 | /** |
| 695 | * Session destroy |
| 696 | * |
| 697 | * Legacy CI_Session compatibility method |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 698 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 699 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 700 | */ |
| 701 | public function sess_destroy() |
| 702 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 703 | session_destroy(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 704 | } |
| 705 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 706 | // ------------------------------------------------------------------------ |
| 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 regenerate |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 710 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 711 | * Legacy CI_Session compatibility method |
| 712 | * |
| 713 | * @param bool $destroy Destroy old session data flag |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 714 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 715 | */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 716 | public function sess_regenerate($destroy = FALSE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 717 | { |
Andrey Andreev | 8e60b9a | 2014-11-04 11:08:06 +0200 | [diff] [blame] | 718 | $_SESSION['__ci_last_regenerate'] = time(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 719 | session_regenerate_id($destroy); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 720 | } |
| 721 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 722 | // ------------------------------------------------------------------------ |
| 723 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 724 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 725 | * Get userdata reference |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 726 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 727 | * Legacy CI_Session compatibility method |
| 728 | * |
| 729 | * @returns array |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 730 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 731 | public function &get_userdata() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 732 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 733 | return $_SESSION; |
| 734 | } |
| 735 | |
| 736 | // ------------------------------------------------------------------------ |
| 737 | |
| 738 | /** |
| 739 | * Userdata (fetch) |
| 740 | * |
| 741 | * Legacy CI_Session compatibility method |
| 742 | * |
| 743 | * @param string $key Session data key |
| 744 | * @return mixed Session data value or NULL if not found |
| 745 | */ |
| 746 | public function userdata($key = NULL) |
| 747 | { |
| 748 | if (isset($key)) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 749 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 750 | return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL; |
| 751 | } |
| 752 | elseif (empty($_SESSION)) |
| 753 | { |
| 754 | return array(); |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 755 | } |
| 756 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 757 | $userdata = array(); |
| 758 | $_exclude = array_merge( |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 759 | array('__ci_vars'), |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 760 | $this->get_flash_keys(), |
| 761 | $this->get_temp_keys() |
| 762 | ); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 763 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 764 | foreach (array_keys($_SESSION) as $key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 765 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 766 | if ( ! in_array($key, $_exclude, TRUE)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 767 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 768 | $userdata[$key] = $_SESSION[$key]; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 772 | return $userdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 773 | } |
| 774 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 775 | // ------------------------------------------------------------------------ |
| 776 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 777 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 778 | * Set userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 779 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 780 | * Legacy CI_Session compatibility method |
| 781 | * |
| 782 | * @param mixed $data Session data key or an associative array |
| 783 | * @param mixed $value Value to store |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 784 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 785 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 786 | public function set_userdata($data, $value = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 787 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 788 | if (is_array($data)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 789 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 790 | foreach ($data as $key => &$value) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 791 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 792 | $_SESSION[$key] = $value; |
Johnathan Croom | 8d8543d | 2012-11-25 10:36:57 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return; |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 796 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 797 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 798 | $_SESSION[$data] = $value; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 799 | } |
| 800 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 801 | // ------------------------------------------------------------------------ |
| 802 | |
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 | * Unset userdata |
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 | * Legacy CI_Session compatibility method |
| 807 | * |
Andrey Andreev | e13fa9f | 2016-05-20 17:30:07 +0300 | [diff] [blame] | 808 | * @param mixed $key Session data key(s) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 809 | * @return void |
| 810 | */ |
| 811 | public function unset_userdata($key) |
| 812 | { |
| 813 | if (is_array($key)) |
| 814 | { |
| 815 | foreach ($key as $k) |
| 816 | { |
Andrey Andreev | d069b9b | 2014-09-16 10:18:16 +0300 | [diff] [blame] | 817 | unset($_SESSION[$k]); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | return; |
| 821 | } |
| 822 | |
| 823 | unset($_SESSION[$key]); |
| 824 | } |
| 825 | |
| 826 | // ------------------------------------------------------------------------ |
| 827 | |
| 828 | /** |
| 829 | * All userdata (fetch) |
| 830 | * |
| 831 | * Legacy CI_Session compatibility method |
| 832 | * |
| 833 | * @return array $_SESSION, excluding flash data items |
| 834 | */ |
| 835 | public function all_userdata() |
| 836 | { |
| 837 | return $this->userdata(); |
| 838 | } |
| 839 | |
| 840 | // ------------------------------------------------------------------------ |
| 841 | |
| 842 | /** |
| 843 | * Has userdata |
| 844 | * |
| 845 | * Legacy CI_Session compatibility method |
| 846 | * |
| 847 | * @param string $key Session data key |
| 848 | * @return bool |
| 849 | */ |
| 850 | public function has_userdata($key) |
| 851 | { |
| 852 | return isset($_SESSION[$key]); |
| 853 | } |
| 854 | |
| 855 | // ------------------------------------------------------------------------ |
| 856 | |
| 857 | /** |
| 858 | * Flashdata (fetch) |
| 859 | * |
| 860 | * Legacy CI_Session compatibility method |
| 861 | * |
| 862 | * @param string $key Session data key |
| 863 | * @return mixed Session data value or NULL if not found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 864 | */ |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 865 | public function flashdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 866 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 867 | if (isset($key)) |
| 868 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 869 | 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] | 870 | ? $_SESSION[$key] |
| 871 | : NULL; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 872 | } |
| 873 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 874 | $flashdata = array(); |
| 875 | |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 876 | if ( ! empty($_SESSION['__ci_vars'])) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 877 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 878 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 879 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 880 | is_int($value) OR $flashdata[$key] = $_SESSION[$key]; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 884 | return $flashdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 885 | } |
| 886 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 887 | // ------------------------------------------------------------------------ |
| 888 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 889 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 890 | * Set flashdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 891 | * |
Calvin Tam | 55bc505 | 2015-07-24 02:27:24 -0700 | [diff] [blame] | 892 | * Legacy CI_Session compatibility method |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 893 | * |
| 894 | * @param mixed $data Session data key or an associative array |
| 895 | * @param mixed $value Value to store |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 896 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 897 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 898 | public function set_flashdata($data, $value = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 899 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 900 | $this->set_userdata($data, $value); |
Andrey Andreev | c6e5098 | 2014-10-26 21:27:28 +0200 | [diff] [blame] | 901 | $this->mark_as_flash(is_array($data) ? array_keys($data) : $data); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 902 | } |
| 903 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 904 | // ------------------------------------------------------------------------ |
| 905 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 906 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 907 | * Keep flashdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 908 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 909 | * Legacy CI_Session compatibility method |
| 910 | * |
| 911 | * @param mixed $key Session data key(s) |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 912 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 913 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 914 | public function keep_flashdata($key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 915 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 916 | $this->mark_as_flash($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 917 | } |
| 918 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 919 | // ------------------------------------------------------------------------ |
| 920 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 921 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 922 | * Temp data (fetch) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 923 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 924 | * Legacy CI_Session compatibility method |
| 925 | * |
| 926 | * @param string $key Session data key |
| 927 | * @return mixed Session data value or NULL if not found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 928 | */ |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 929 | public function tempdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 930 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 931 | if (isset($key)) |
| 932 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 933 | 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] | 934 | ? $_SESSION[$key] |
| 935 | : NULL; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 936 | } |
| 937 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 938 | $tempdata = array(); |
| 939 | |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 940 | if ( ! empty($_SESSION['__ci_vars'])) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 941 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 942 | foreach ($_SESSION['__ci_vars'] as $key => &$value) |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 943 | { |
Andrey Andreev | ef41786 | 2014-06-04 21:28:13 +0300 | [diff] [blame] | 944 | is_int($value) && $tempdata[$key] = $_SESSION[$key]; |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 948 | return $tempdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 949 | } |
| 950 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 951 | // ------------------------------------------------------------------------ |
| 952 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 953 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 954 | * Set tempdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 955 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 956 | * Legacy CI_Session compatibility method |
| 957 | * |
| 958 | * @param mixed $data Session data key or an associative array of items |
| 959 | * @param mixed $value Value to store |
| 960 | * @param int $ttl Time-to-live in seconds |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 961 | * @return void |
| 962 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 963 | public function set_tempdata($data, $value = NULL, $ttl = 300) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 964 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 965 | $this->set_userdata($data, $value); |
Andrey Andreev | fd31057 | 2015-03-30 17:19:26 +0300 | [diff] [blame] | 966 | $this->mark_as_temp(is_array($data) ? array_keys($data) : $data, $ttl); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 967 | } |
| 968 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 969 | // ------------------------------------------------------------------------ |
| 970 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 971 | /** |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 972 | * Unset tempdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 973 | * |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 974 | * Legacy CI_Session compatibility method |
| 975 | * |
| 976 | * @param mixed $data Session data key(s) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 977 | * @return void |
| 978 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 979 | public function unset_tempdata($key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 980 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 981 | $this->unmark_temp($key); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 982 | } |
| 983 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 984 | } |