blob: 54d31ee1a6bf2bf6d07ad363e666b545793f7b9a [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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Darren Hillc4e266b2011-08-30 15:40:27 -040047 * @link http://codeigniter.com/user_guide/libraries/sessions.html
48 */
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;
Darren Hillc4e266b2011-08-30 15:40:27 -040060
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020061 // ------------------------------------------------------------------------
62
Darren Hillc4e266b2011-08-30 15:40:27 -040063 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +030064 * Class constructor
Darren Hillc4e266b2011-08-30 15:40:27 -040065 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +030066 * @param array $params Configuration parameters
Andrey Andreev2e3e2302012-10-09 15:52:34 +030067 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -040068 */
69 public function __construct(array $params = array())
70 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030071 // No sessions under CLI
Andrey Andreevf964b162013-11-12 17:04:55 +020072 if (is_cli())
Andrey Andreev2e3e2302012-10-09 15:52:34 +030073 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030074 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 Andreevdfb39be2014-10-06 01:50:14 +030087 elseif ($driver = config_item('sess_driver'))
Andrey Andreev34b1ef52014-05-31 21:23:41 +030088 {
89 $this->_driver = $driver;
90 }
Andrey Andreevac4f4722014-06-02 11:16:32 +030091 // Note: BC workaround
92 elseif (config_item('sess_use_database'))
93 {
94 $this->_driver = 'database';
95 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +030096
Andrey Andreev34b92c62015-03-12 12:42:00 +020097 $class = $this->_ci_load_classes($this->_driver);
Andrey Andreev2e3e2302012-10-09 15:52:34 +030098
Andrey Andreevdfb39be2014-10-06 01:50:14 +030099 // Configuration ...
100 $this->_configure($params);
101
102 $class = new $class($this->_config);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300103 if ($class instanceof SessionHandlerInterface)
Darren Hillc4e266b2011-08-30 15:40:27 -0400104 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300105 if (is_php('5.4'))
Darren Hillc4e266b2011-08-30 15:40:27 -0400106 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300107 session_set_save_handler($class, TRUE);
Darren Hillc4e266b2011-08-30 15:40:27 -0400108 }
109 else
110 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300111 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 Andreev562e39b2014-11-12 15:38:58 +0200129 // 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 Andreevdfb39be2014-10-06 01:50:14 +0300136 {
Andrey Andreev562e39b2014-11-12 15:38:58 +0200137 unset($_COOKIE[$this->_config['cookie_name']]);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300138 }
139
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300140 session_start();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300141
Andrey Andreevff37ffe2014-11-04 12:28:57 +0200142 // Is session ID auto-regeneration configured? (ignoring ajax requests)
Andrey Andreev395f9282015-02-05 13:29:56 +0200143 if ((empty($_SERVER['HTTP_X_REQUESTED_WITH']) OR strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
Andrey Andreevde5c2462014-11-04 12:31:03 +0200144 && ($regenerate_time = config_item('sess_time_to_update')) > 0
Andrey Andreevff37ffe2014-11-04 12:28:57 +0200145 )
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200146 {
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 Andreev789b1fe2015-02-07 19:30:30 +0200153 $this->sess_regenerate((bool) config_item('sess_regenerate_destroy'));
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200154 }
155 }
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300156 // Another work-around ... PHP doesn't seem to send the session cookie
157 // unless it is being currently created or regenerated
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200158 elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300159 {
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 Andreev47a47fb2014-05-31 16:08:30 +0300171 $this->_ci_init_vars();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300172
Andrey Andreev90726b82015-01-20 12:39:22 +0200173 log_message('info', "Session: Class initialized using '".$this->_driver."' driver.");
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300174 }
175
176 // ------------------------------------------------------------------------
177
Andrey Andreev10411fc2015-01-19 13:54:53 +0200178 /**
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 Andreev47a47fb2014-05-31 16:08:30 +0300188 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 Andreeve86603f2014-06-11 14:03:36 +0300197 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 Andreev47a47fb2014-05-31 16:08:30 +0300202
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 Andreevcd94dd72014-12-09 17:38:56 +0200210
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 Andreev47a47fb2014-05-31 16:08:30 +0300221 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 Andreevcd94dd72014-12-09 17:38:56 +0200228 if ( ! class_exists('CI_'.$class, FALSE) && ! class_exists($class, FALSE))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300229 {
Andrey Andreev34b92c62015-03-12 12:42:00 +0200230 throw new \UnexpectedValueException("Session: Configured driver '".$driver."' was not found. Aborting.");
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300231 }
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 Andreevdfb39be2014-10-06 01:50:14 +0300253 * 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 Andreev41b546d2014-10-06 03:01:22 +0300285 isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300286 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 Andreevc02952d2015-02-13 13:04:38 +0200313 ini_set('session.use_trans_sid', 0);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300314 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 Andreev47a47fb2014-05-31 16:08:30 +0300324 * 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 Hillc4e266b2011-08-30 15:40:27 -0400354 }
355 }
Andrey Andreevb4b215e2015-01-19 11:59:11 +0200356
357 $this->userdata =& $_SESSION;
Darren Hillc4e266b2011-08-30 15:40:27 -0400358 }
359
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300360 // ------------------------------------------------------------------------
361
Darren Hillc4e266b2011-08-30 15:40:27 -0400362 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300363 * 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 Andreev43df7bd2015-02-02 23:22:29 +0200487 $temp[$k] = $v;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300488 }
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 Hillc4e266b2011-08-30 15:40:27 -0400604 *
Darren Hill5073a372011-08-31 13:54:19 -0400605 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400606 */
607 public function sess_destroy()
608 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300609 session_destroy();
Darren Hillc4e266b2011-08-30 15:40:27 -0400610 }
611
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300612 // ------------------------------------------------------------------------
613
Darren Hillc4e266b2011-08-30 15:40:27 -0400614 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300615 * Session regenerate
Darren Hillc4e266b2011-08-30 15:40:27 -0400616 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300617 * Legacy CI_Session compatibility method
618 *
619 * @param bool $destroy Destroy old session data flag
Darren Hill5073a372011-08-31 13:54:19 -0400620 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400621 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300622 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400623 {
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200624 $_SESSION['__ci_last_regenerate'] = time();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300625 session_regenerate_id($destroy);
Darren Hillc4e266b2011-08-30 15:40:27 -0400626 }
627
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300628 // ------------------------------------------------------------------------
629
Darren Hillc4e266b2011-08-30 15:40:27 -0400630 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300631 * Get userdata reference
Darren Hillc4e266b2011-08-30 15:40:27 -0400632 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300633 * Legacy CI_Session compatibility method
634 *
635 * @returns array
Darren Hillc4e266b2011-08-30 15:40:27 -0400636 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300637 public function &get_userdata()
Darren Hillc4e266b2011-08-30 15:40:27 -0400638 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300639 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 Andreevecc260e2014-01-24 14:20:13 +0200655 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300656 return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
657 }
658 elseif (empty($_SESSION))
659 {
660 return array();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200661 }
662
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300663 $userdata = array();
664 $_exclude = array_merge(
Andrey Andreevef417862014-06-04 21:28:13 +0300665 array('__ci_vars'),
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300666 $this->get_flash_keys(),
667 $this->get_temp_keys()
668 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400669
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300670 foreach (array_keys($_SESSION) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400671 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300672 if ( ! in_array($key, $_exclude, TRUE))
Darren Hillc4e266b2011-08-30 15:40:27 -0400673 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300674 $userdata[$key] = $_SESSION[$key];
Darren Hillc4e266b2011-08-30 15:40:27 -0400675 }
676 }
677
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300678 return $userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400679 }
680
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300681 // ------------------------------------------------------------------------
682
Darren Hillc4e266b2011-08-30 15:40:27 -0400683 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300684 * Set userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400685 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300686 * 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 Hill5073a372011-08-31 13:54:19 -0400690 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400691 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300692 public function set_userdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400693 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300694 if (is_array($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400695 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300696 foreach ($data as $key => &$value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400697 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300698 $_SESSION[$key] = $value;
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700699 }
700
701 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700702 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400703
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300704 $_SESSION[$data] = $value;
Darren Hillc4e266b2011-08-30 15:40:27 -0400705 }
706
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300707 // ------------------------------------------------------------------------
708
Darren Hillc4e266b2011-08-30 15:40:27 -0400709 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300710 * Unset userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400711 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300712 * 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 Andreevd069b9b2014-09-16 10:18:16 +0300723 unset($_SESSION[$k]);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300724 }
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 Hillc4e266b2011-08-30 15:40:27 -0400770 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200771 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400772 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200773 if (isset($key))
774 {
Andrey Andreevef417862014-06-04 21:28:13 +0300775 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300776 ? $_SESSION[$key]
777 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200778 }
779
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300780 $flashdata = array();
781
Andrey Andreevef417862014-06-04 21:28:13 +0300782 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200783 {
Andrey Andreevef417862014-06-04 21:28:13 +0300784 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200785 {
Andrey Andreevef417862014-06-04 21:28:13 +0300786 is_int($value) OR $flashdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200787 }
788 }
789
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300790 return $flashdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400791 }
792
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300793 // ------------------------------------------------------------------------
794
Darren Hillc4e266b2011-08-30 15:40:27 -0400795 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300796 * Set flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400797 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300798 * Legacy CI_Session compatibiliy method
799 *
800 * @param mixed $data Session data key or an associative array
801 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400802 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400803 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300804 public function set_flashdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400805 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300806 $this->set_userdata($data, $value);
Andrey Andreevc6e50982014-10-26 21:27:28 +0200807 $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400808 }
809
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300810 // ------------------------------------------------------------------------
811
Darren Hillc4e266b2011-08-30 15:40:27 -0400812 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300813 * Keep flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400814 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300815 * Legacy CI_Session compatibility method
816 *
817 * @param mixed $key Session data key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400818 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400819 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300820 public function keep_flashdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400821 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300822 $this->mark_as_flash($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400823 }
824
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300825 // ------------------------------------------------------------------------
826
Darren Hillc4e266b2011-08-30 15:40:27 -0400827 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300828 * Temp data (fetch)
Darren Hillc4e266b2011-08-30 15:40:27 -0400829 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300830 * 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 Hillc4e266b2011-08-30 15:40:27 -0400834 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200835 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400836 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200837 if (isset($key))
838 {
Andrey Andreevef417862014-06-04 21:28:13 +0300839 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300840 ? $_SESSION[$key]
841 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200842 }
843
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300844 $tempdata = array();
845
Andrey Andreevef417862014-06-04 21:28:13 +0300846 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200847 {
Andrey Andreevef417862014-06-04 21:28:13 +0300848 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200849 {
Andrey Andreevef417862014-06-04 21:28:13 +0300850 is_int($value) && $tempdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200851 }
852 }
853
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300854 return $tempdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400855 }
856
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300857 // ------------------------------------------------------------------------
858
Darren Hillc4e266b2011-08-30 15:40:27 -0400859 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300860 * Set tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400861 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300862 * 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 Hillc4e266b2011-08-30 15:40:27 -0400867 * @return void
868 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300869 public function set_tempdata($data, $value = NULL, $ttl = 300)
Darren Hillc4e266b2011-08-30 15:40:27 -0400870 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300871 $this->set_userdata($data, $value);
872 $this->mark_as_temp($data, $ttl);
Darren Hillc4e266b2011-08-30 15:40:27 -0400873 }
874
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300875 // ------------------------------------------------------------------------
876
Darren Hillc4e266b2011-08-30 15:40:27 -0400877 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300878 * Unset tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400879 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300880 * Legacy CI_Session compatibility method
881 *
882 * @param mixed $data Session data key(s)
Darren Hillc4e266b2011-08-30 15:40:27 -0400883 * @return void
884 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300885 public function unset_tempdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400886 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300887 $this->unmark_temp($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400888 }
889
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300890}