blob: ea7853108eb1cee70a77e223e217d38987799e48 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Darren Hillc4e266b2011-08-30 15:40:27 -04002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Andrey Andreev9ffcee62012-09-05 16:25:16 +03006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev9ffcee62012-09-05 16:25:16 +03008 *
Andrey Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
Andrey Andreev9ffcee62012-09-05 16:25:16 +030010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Hillc4e266b2011-08-30 15:40:27 -040017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 2.0.0
Darren Hillc4e266b2011-08-30 15:40:27 -040036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Darren Hillc4e266b2011-08-30 15:40:27 -040039
Darren Hillc4e266b2011-08-30 15:40:27 -040040/**
Andrey Andreev9ffcee62012-09-05 16:25:16 +030041 * CodeIgniter Session Class
Darren Hillc4e266b2011-08-30 15:40:27 -040042 *
Darren Hillc4e266b2011-08-30 15:40:27 -040043 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Sessions
Andrey Andreev47a47fb2014-05-31 16:08:30 +030046 * @author Andrey Andreev
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/sessions.html
Darren Hillc4e266b2011-08-30 15:40:27 -040048 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +030049class CI_Session {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030050
Andrey Andreevb4b215e2015-01-19 11:59:11 +020051 /**
52 * Userdata array
53 *
54 * Just a reference to $_SESSION, for BC purposes.
55 */
56 public $userdata;
57
Andrey Andreev47a47fb2014-05-31 16:08:30 +030058 protected $_driver = 'files';
Andrey Andreevdfb39be2014-10-06 01:50:14 +030059 protected $_config;
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +030060 protected $_sid_regexp;
Darren Hillc4e266b2011-08-30 15:40:27 -040061
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020062 // ------------------------------------------------------------------------
63
Darren Hillc4e266b2011-08-30 15:40:27 -040064 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +030065 * Class constructor
Darren Hillc4e266b2011-08-30 15:40:27 -040066 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +030067 * @param array $params Configuration parameters
Andrey Andreev2e3e2302012-10-09 15:52:34 +030068 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -040069 */
70 public function __construct(array $params = array())
71 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030072 // No sessions under CLI
Andrey Andreevf964b162013-11-12 17:04:55 +020073 if (is_cli())
Andrey Andreev2e3e2302012-10-09 15:52:34 +030074 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030075 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 Andreevdfb39be2014-10-06 01:50:14 +030088 elseif ($driver = config_item('sess_driver'))
Andrey Andreev34b1ef52014-05-31 21:23:41 +030089 {
90 $this->_driver = $driver;
91 }
Andrey Andreevac4f4722014-06-02 11:16:32 +030092 // Note: BC workaround
93 elseif (config_item('sess_use_database'))
94 {
Andrey Andreev85dfc2a2016-04-01 22:54:15 +030095 log_message('debug', 'Session: "sess_driver" is empty; using BC fallback to "sess_use_database".');
Andrey Andreevac4f4722014-06-02 11:16:32 +030096 $this->_driver = 'database';
97 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +030098
Andrey Andreev34b92c62015-03-12 12:42:00 +020099 $class = $this->_ci_load_classes($this->_driver);
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300100
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300101 // Configuration ...
102 $this->_configure($params);
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300103 $this->_config['_sid_regexp'] = $this->_sid_regexp;
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300104
105 $class = new $class($this->_config);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300106 if ($class instanceof SessionHandlerInterface)
Darren Hillc4e266b2011-08-30 15:40:27 -0400107 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300108 if (is_php('5.4'))
Darren Hillc4e266b2011-08-30 15:40:27 -0400109 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300110 session_set_save_handler($class, TRUE);
Darren Hillc4e266b2011-08-30 15:40:27 -0400111 }
112 else
113 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300114 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 Andreev562e39b2014-11-12 15:38:58 +0200132 // 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 Andreev6c6ee1a2016-10-22 16:33:06 +0300136 OR ! preg_match('#\A'.$this->_sid_regexp.'\z#', $_COOKIE[$this->_config['cookie_name']])
Andrey Andreev562e39b2014-11-12 15:38:58 +0200137 )
138 )
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300139 {
Andrey Andreev562e39b2014-11-12 15:38:58 +0200140 unset($_COOKIE[$this->_config['cookie_name']]);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300141 }
142
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300143 session_start();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300144
Andrey Andreevff37ffe2014-11-04 12:28:57 +0200145 // Is session ID auto-regeneration configured? (ignoring ajax requests)
Andrey Andreev395f9282015-02-05 13:29:56 +0200146 if ((empty($_SERVER['HTTP_X_REQUESTED_WITH']) OR strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
Andrey Andreevde5c2462014-11-04 12:31:03 +0200147 && ($regenerate_time = config_item('sess_time_to_update')) > 0
Andrey Andreevff37ffe2014-11-04 12:28:57 +0200148 )
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200149 {
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 Andreev789b1fe2015-02-07 19:30:30 +0200156 $this->sess_regenerate((bool) config_item('sess_regenerate_destroy'));
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200157 }
158 }
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300159 // Another work-around ... PHP doesn't seem to send the session cookie
160 // unless it is being currently created or regenerated
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200161 elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300162 {
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 Andreev47a47fb2014-05-31 16:08:30 +0300174 $this->_ci_init_vars();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300175
Andrey Andreev90726b82015-01-20 12:39:22 +0200176 log_message('info', "Session: Class initialized using '".$this->_driver."' driver.");
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300177 }
178
179 // ------------------------------------------------------------------------
180
Andrey Andreev10411fc2015-01-19 13:54:53 +0200181 /**
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 Andreev47a47fb2014-05-31 16:08:30 +0300191 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 Andreeve86603f2014-06-11 14:03:36 +0300200 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 Andreev47a47fb2014-05-31 16:08:30 +0300205
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 Andreevcd94dd72014-12-09 17:38:56 +0200213
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 Andreev47a47fb2014-05-31 16:08:30 +0300224 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 Andreevcd94dd72014-12-09 17:38:56 +0200231 if ( ! class_exists('CI_'.$class, FALSE) && ! class_exists($class, FALSE))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300232 {
Andrey Andreev1d195202015-03-13 11:25:29 +0200233 throw new UnexpectedValueException("Session: Configured driver '".$driver."' was not found. Aborting.");
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300234 }
235 }
236
Andrey Andreev738b9e32016-02-24 12:14:10 +0200237 if ( ! class_exists($prefix.$class, FALSE) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300238 {
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 Andreevdfb39be2014-10-06 01:50:14 +0300256 * 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 Andreev41b546d2014-10-06 03:01:22 +0300288 isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300289 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 Andreevc02952d2015-02-13 13:04:38 +0200316 ini_set('session.use_trans_sid', 0);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300317 ini_set('session.use_strict_mode', 1);
318 ini_set('session.use_cookies', 1);
319 ini_set('session.use_only_cookies', 1);
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300320
Andrey Andreev2f760872016-10-27 16:39:12 +0300321 $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 Andreev6c6ee1a2016-10-22 16:33:06 +0300343 if (PHP_VERSION_ID < 70100)
344 {
Andrey Andreev2f760872016-10-27 16:39:12 +0300345 $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 Andreev6c6ee1a2016-10-22 16:33:06 +0300355 {
356 ini_set('session.hash_function', 1);
Andrey Andreev2f760872016-10-27 16:39:12 +0300357 $bits = 160;
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300358 }
Andrey Andreev2f760872016-10-27 16:39:12 +0300359 elseif (($bits = strlen(hash($hash_function, 'dummy', false)) * 4) < 160)
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300360 {
Andrey Andreev2f760872016-10-27 16:39:12 +0300361 ini_set('session.hash_function', 1);
362 $bits = 160;
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300363 }
Andrey Andreev2f760872016-10-27 16:39:12 +0300364
365 $bits_per_character = (int) ini_get('session.hash_bits_per_character');
366 $sid_length = $bits * $bits_per_character;
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300367 }
Andrey Andreev2f760872016-10-27 16:39:12 +0300368 else
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300369 {
Andrey Andreev2f760872016-10-27 16:39:12 +0300370 $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 Andreev6c6ee1a2016-10-22 16:33:06 +0300378 }
379
Andrey Andreev2f760872016-10-27 16:39:12 +0300380 // Yes, 4,5,6 are the only known possible values as of 2016-10-27
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300381 switch ($bits_per_character)
382 {
383 case 4:
Andrey Andreev2f760872016-10-27 16:39:12 +0300384 $this->_sid_regexp = '[0-9a-f]';
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300385 break;
386 case 5:
Andrey Andreev2f760872016-10-27 16:39:12 +0300387 $this->_sid_regexp = '[0-9a-v]';
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300388 break;
389 case 6:
Andrey Andreev2f760872016-10-27 16:39:12 +0300390 $this->_sid_regexp = '[0-9a-zA-Z,-]';
Andrey Andreev6c6ee1a2016-10-22 16:33:06 +0300391 break;
392 }
Andrey Andreev2f760872016-10-27 16:39:12 +0300393
394 $this->_sid_regexp .= '{'.$sid_length.'}';
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300395 }
396
397 // ------------------------------------------------------------------------
398
399 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300400 * 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 Hillc4e266b2011-08-30 15:40:27 -0400430 }
431 }
Andrey Andreevb4b215e2015-01-19 11:59:11 +0200432
433 $this->userdata =& $_SESSION;
Darren Hillc4e266b2011-08-30 15:40:27 -0400434 }
435
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300436 // ------------------------------------------------------------------------
437
Darren Hillc4e266b2011-08-30 15:40:27 -0400438 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300439 * 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 Andreev43df7bd2015-02-02 23:22:29 +0200563 $temp[$k] = $v;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300564 }
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 Andreev2c10f602016-03-15 14:39:02 +0200663 * __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 Andreev47a47fb2014-05-31 16:08:30 +0300681 * __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 Hillc4e266b2011-08-30 15:40:27 -0400698 *
Darren Hill5073a372011-08-31 13:54:19 -0400699 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400700 */
701 public function sess_destroy()
702 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300703 session_destroy();
Darren Hillc4e266b2011-08-30 15:40:27 -0400704 }
705
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300706 // ------------------------------------------------------------------------
707
Darren Hillc4e266b2011-08-30 15:40:27 -0400708 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300709 * Session regenerate
Darren Hillc4e266b2011-08-30 15:40:27 -0400710 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300711 * Legacy CI_Session compatibility method
712 *
713 * @param bool $destroy Destroy old session data flag
Darren Hill5073a372011-08-31 13:54:19 -0400714 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400715 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300716 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400717 {
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200718 $_SESSION['__ci_last_regenerate'] = time();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300719 session_regenerate_id($destroy);
Darren Hillc4e266b2011-08-30 15:40:27 -0400720 }
721
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300722 // ------------------------------------------------------------------------
723
Darren Hillc4e266b2011-08-30 15:40:27 -0400724 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300725 * Get userdata reference
Darren Hillc4e266b2011-08-30 15:40:27 -0400726 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300727 * Legacy CI_Session compatibility method
728 *
729 * @returns array
Darren Hillc4e266b2011-08-30 15:40:27 -0400730 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300731 public function &get_userdata()
Darren Hillc4e266b2011-08-30 15:40:27 -0400732 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300733 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 Andreevecc260e2014-01-24 14:20:13 +0200749 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300750 return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
751 }
752 elseif (empty($_SESSION))
753 {
754 return array();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200755 }
756
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300757 $userdata = array();
758 $_exclude = array_merge(
Andrey Andreevef417862014-06-04 21:28:13 +0300759 array('__ci_vars'),
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300760 $this->get_flash_keys(),
761 $this->get_temp_keys()
762 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400763
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300764 foreach (array_keys($_SESSION) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400765 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300766 if ( ! in_array($key, $_exclude, TRUE))
Darren Hillc4e266b2011-08-30 15:40:27 -0400767 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300768 $userdata[$key] = $_SESSION[$key];
Darren Hillc4e266b2011-08-30 15:40:27 -0400769 }
770 }
771
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300772 return $userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400773 }
774
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300775 // ------------------------------------------------------------------------
776
Darren Hillc4e266b2011-08-30 15:40:27 -0400777 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300778 * Set userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400779 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300780 * 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 Hill5073a372011-08-31 13:54:19 -0400784 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400785 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300786 public function set_userdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400787 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300788 if (is_array($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400789 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300790 foreach ($data as $key => &$value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400791 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300792 $_SESSION[$key] = $value;
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700793 }
794
795 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700796 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400797
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300798 $_SESSION[$data] = $value;
Darren Hillc4e266b2011-08-30 15:40:27 -0400799 }
800
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300801 // ------------------------------------------------------------------------
802
Darren Hillc4e266b2011-08-30 15:40:27 -0400803 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300804 * Unset userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400805 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300806 * Legacy CI_Session compatibility method
807 *
Andrey Andreeve13fa9f2016-05-20 17:30:07 +0300808 * @param mixed $key Session data key(s)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300809 * @return void
810 */
811 public function unset_userdata($key)
812 {
813 if (is_array($key))
814 {
815 foreach ($key as $k)
816 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300817 unset($_SESSION[$k]);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300818 }
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 Hillc4e266b2011-08-30 15:40:27 -0400864 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200865 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400866 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200867 if (isset($key))
868 {
Andrey Andreevef417862014-06-04 21:28:13 +0300869 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300870 ? $_SESSION[$key]
871 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200872 }
873
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300874 $flashdata = array();
875
Andrey Andreevef417862014-06-04 21:28:13 +0300876 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200877 {
Andrey Andreevef417862014-06-04 21:28:13 +0300878 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200879 {
Andrey Andreevef417862014-06-04 21:28:13 +0300880 is_int($value) OR $flashdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200881 }
882 }
883
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300884 return $flashdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400885 }
886
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300887 // ------------------------------------------------------------------------
888
Darren Hillc4e266b2011-08-30 15:40:27 -0400889 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300890 * Set flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400891 *
Calvin Tam55bc5052015-07-24 02:27:24 -0700892 * Legacy CI_Session compatibility method
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300893 *
894 * @param mixed $data Session data key or an associative array
895 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400896 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400897 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300898 public function set_flashdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400899 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300900 $this->set_userdata($data, $value);
Andrey Andreevc6e50982014-10-26 21:27:28 +0200901 $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400902 }
903
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300904 // ------------------------------------------------------------------------
905
Darren Hillc4e266b2011-08-30 15:40:27 -0400906 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300907 * Keep flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400908 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300909 * Legacy CI_Session compatibility method
910 *
911 * @param mixed $key Session data key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400912 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400913 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300914 public function keep_flashdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400915 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300916 $this->mark_as_flash($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400917 }
918
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300919 // ------------------------------------------------------------------------
920
Darren Hillc4e266b2011-08-30 15:40:27 -0400921 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300922 * Temp data (fetch)
Darren Hillc4e266b2011-08-30 15:40:27 -0400923 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300924 * 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 Hillc4e266b2011-08-30 15:40:27 -0400928 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200929 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400930 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200931 if (isset($key))
932 {
Andrey Andreevef417862014-06-04 21:28:13 +0300933 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300934 ? $_SESSION[$key]
935 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200936 }
937
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300938 $tempdata = array();
939
Andrey Andreevef417862014-06-04 21:28:13 +0300940 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200941 {
Andrey Andreevef417862014-06-04 21:28:13 +0300942 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200943 {
Andrey Andreevef417862014-06-04 21:28:13 +0300944 is_int($value) && $tempdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200945 }
946 }
947
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300948 return $tempdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400949 }
950
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300951 // ------------------------------------------------------------------------
952
Darren Hillc4e266b2011-08-30 15:40:27 -0400953 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300954 * Set tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400955 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300956 * 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 Hillc4e266b2011-08-30 15:40:27 -0400961 * @return void
962 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300963 public function set_tempdata($data, $value = NULL, $ttl = 300)
Darren Hillc4e266b2011-08-30 15:40:27 -0400964 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300965 $this->set_userdata($data, $value);
Andrey Andreevfd310572015-03-30 17:19:26 +0300966 $this->mark_as_temp(is_array($data) ? array_keys($data) : $data, $ttl);
Darren Hillc4e266b2011-08-30 15:40:27 -0400967 }
968
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300969 // ------------------------------------------------------------------------
970
Darren Hillc4e266b2011-08-30 15:40:27 -0400971 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300972 * Unset tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400973 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300974 * Legacy CI_Session compatibility method
975 *
976 * @param mixed $data Session data key(s)
Darren Hillc4e266b2011-08-30 15:40:27 -0400977 * @return void
978 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300979 public function unset_tempdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400980 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300981 $this->unmark_temp($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400982 }
983
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300984}