blob: 2551e54e9ffb425dbedeced3a8b9ef2d4f6f4756 [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
97 if (($class = $this->_ci_load_classes($this->_driver)) === FALSE)
98 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030099 return;
100 }
101
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300102 // Configuration ...
103 $this->_configure($params);
104
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']])
136 OR ! preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']])
137 )
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)
146 if ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH'])
147 && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'
Andrey Andreevde5c2462014-11-04 12:31:03 +0200148 && ($regenerate_time = config_item('sess_time_to_update')) > 0
Andrey Andreevff37ffe2014-11-04 12:28:57 +0200149 )
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200150 {
151 if ( ! isset($_SESSION['__ci_last_regenerate']))
152 {
153 $_SESSION['__ci_last_regenerate'] = time();
154 }
155 elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time))
156 {
157 $this->sess_regenerate(FALSE);
158 }
159 }
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300160 // Another work-around ... PHP doesn't seem to send the session cookie
161 // unless it is being currently created or regenerated
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200162 elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300163 {
164 setcookie(
165 $this->_config['cookie_name'],
166 session_id(),
167 (empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']),
168 $this->_config['cookie_path'],
169 $this->_config['cookie_domain'],
170 $this->_config['cookie_secure'],
171 TRUE
172 );
173 }
174
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300175 $this->_ci_init_vars();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300176
Andrey Andreev90726b82015-01-20 12:39:22 +0200177 log_message('info', "Session: Class initialized using '".$this->_driver."' driver.");
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300178 }
179
180 // ------------------------------------------------------------------------
181
Andrey Andreev10411fc2015-01-19 13:54:53 +0200182 /**
183 * CI Load Classes
184 *
185 * An internal method to load all possible dependency and extension
186 * classes. It kind of emulates the CI_Driver library, but is
187 * self-sufficient.
188 *
189 * @param string $driver Driver name
190 * @return string Driver class name
191 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300192 protected function _ci_load_classes($driver)
193 {
194 // PHP 5.4 compatibility
195 interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php');
196
197 $prefix = config_item('subclass_prefix');
198
199 if ( ! class_exists('CI_Session_driver', FALSE))
200 {
Andrey Andreeve86603f2014-06-11 14:03:36 +0300201 require_once(
202 file_exists(APPPATH.'libraries/Session/Session_driver.php')
203 ? APPPATH.'libraries/Session/Session_driver.php'
204 : BASEPATH.'libraries/Session/Session_driver.php'
205 );
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300206
207 if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php'))
208 {
209 require_once($file_path);
210 }
211 }
212
213 $class = 'Session_'.$driver.'_driver';
Andrey Andreevcd94dd72014-12-09 17:38:56 +0200214
215 // Allow custom drivers without the CI_ or MY_ prefix
216 if ( ! class_exists($class, FALSE) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php'))
217 {
218 require_once($file_path);
219 if (class_exists($class, FALSE))
220 {
221 return $class;
222 }
223 }
224
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300225 if ( ! class_exists('CI_'.$class, FALSE))
226 {
227 if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php'))
228 {
229 require_once($file_path);
230 }
231
Andrey Andreevcd94dd72014-12-09 17:38:56 +0200232 if ( ! class_exists('CI_'.$class, FALSE) && ! class_exists($class, FALSE))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300233 {
234 log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting.");
235 return FALSE;
236 }
237 }
238
239 if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
240 {
241 require_once($file_path);
242 if (class_exists($prefix.$class, FALSE))
243 {
244 return $prefix.$class;
245 }
246 else
247 {
248 log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.');
249 }
250 }
251
252 return 'CI_'.$class;
253 }
254
255 // ------------------------------------------------------------------------
256
257 /**
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300258 * Configuration
259 *
260 * Handle input parameters and configuration defaults
261 *
262 * @param array &$params Input parameters
263 * @return void
264 */
265 protected function _configure(&$params)
266 {
267 $expiration = config_item('sess_expiration');
268
269 if (isset($params['cookie_lifetime']))
270 {
271 $params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
272 }
273 else
274 {
275 $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close'))
276 ? 0 : (int) $expiration;
277 }
278
279 isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name');
280 if (empty($params['cookie_name']))
281 {
282 $params['cookie_name'] = ini_get('session.name');
283 }
284 else
285 {
286 ini_set('session.name', $params['cookie_name']);
287 }
288
289 isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path');
Andrey Andreev41b546d2014-10-06 03:01:22 +0300290 isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300291 isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure');
292
293 session_set_cookie_params(
294 $params['cookie_lifetime'],
295 $params['cookie_path'],
296 $params['cookie_domain'],
297 $params['cookie_secure'],
298 TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons
299 );
300
301 if (empty($expiration))
302 {
303 $params['expiration'] = (int) ini_get('session.gc_maxlifetime');
304 }
305 else
306 {
307 $params['expiration'] = (int) $expiration;
308 ini_set('session.gc_maxlifetime', $expiration);
309 }
310
311 $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip'));
312
313 isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path');
314
315 $this->_config = $params;
316
317 // Security is king
318 ini_set('session.use_trans_id', 0);
319 ini_set('session.use_strict_mode', 1);
320 ini_set('session.use_cookies', 1);
321 ini_set('session.use_only_cookies', 1);
322 ini_set('session.hash_function', 1);
323 ini_set('session.hash_bits_per_character', 4);
324 }
325
326 // ------------------------------------------------------------------------
327
328 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300329 * Handle temporary variables
330 *
331 * Clears old "flash" data, marks the new one for deletion and handles
332 * "temp" data deletion.
333 *
334 * @return void
335 */
336 protected function _ci_init_vars()
337 {
338 if ( ! empty($_SESSION['__ci_vars']))
339 {
340 $current_time = time();
341
342 foreach ($_SESSION['__ci_vars'] as $key => &$value)
343 {
344 if ($value === 'new')
345 {
346 $_SESSION['__ci_vars'][$key] = 'old';
347 }
348 // Hacky, but 'old' will (implicitly) always be less than time() ;)
349 // DO NOT move this above the 'new' check!
350 elseif ($value < $current_time)
351 {
352 unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
353 }
354 }
355
356 if (empty($_SESSION['__ci_vars']))
357 {
358 unset($_SESSION['__ci_vars']);
Darren Hillc4e266b2011-08-30 15:40:27 -0400359 }
360 }
Andrey Andreevb4b215e2015-01-19 11:59:11 +0200361
362 $this->userdata =& $_SESSION;
Darren Hillc4e266b2011-08-30 15:40:27 -0400363 }
364
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300365 // ------------------------------------------------------------------------
366
Darren Hillc4e266b2011-08-30 15:40:27 -0400367 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300368 * Mark as flash
369 *
370 * @param mixed $key Session data key(s)
371 * @return bool
372 */
373 public function mark_as_flash($key)
374 {
375 if (is_array($key))
376 {
377 for ($i = 0, $c = count($key); $i < $c; $i++)
378 {
379 if ( ! isset($_SESSION[$key[$i]]))
380 {
381 return FALSE;
382 }
383 }
384
385 $new = array_fill_keys($key, 'new');
386
387 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
388 ? array_merge($_SESSION['__ci_vars'], $new)
389 : $new;
390
391 return TRUE;
392 }
393
394 if ( ! isset($_SESSION[$key]))
395 {
396 return FALSE;
397 }
398
399 $_SESSION['__ci_vars'][$key] = 'new';
400 return TRUE;
401 }
402
403 // ------------------------------------------------------------------------
404
405 /**
406 * Get flash keys
407 *
408 * @return array
409 */
410 public function get_flash_keys()
411 {
412 if ( ! isset($_SESSION['__ci_vars']))
413 {
414 return array();
415 }
416
417 $keys = array();
418 foreach (array_keys($_SESSION['__ci_vars']) as $key)
419 {
420 is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key;
421 }
422
423 return $keys;
424 }
425
426 // ------------------------------------------------------------------------
427
428 /**
429 * Unmark flash
430 *
431 * @param mixed $key Session data key(s)
432 * @return void
433 */
434 public function unmark_flash($key)
435 {
436 if (empty($_SESSION['__ci_vars']))
437 {
438 return;
439 }
440
441 is_array($key) OR $key = array($key);
442
443 foreach ($key as $k)
444 {
445 if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k]))
446 {
447 unset($_SESSION['__ci_vars'][$k]);
448 }
449 }
450
451 if (empty($_SESSION['__ci_vars']))
452 {
453 unset($_SESSION['__ci_vars']);
454 }
455 }
456
457 // ------------------------------------------------------------------------
458
459 /**
460 * Mark as temp
461 *
462 * @param mixed $key Session data key(s)
463 * @param int $ttl Time-to-live in seconds
464 * @return bool
465 */
466 public function mark_as_temp($key, $ttl = 300)
467 {
468 $ttl += time();
469
470 if (is_array($key))
471 {
472 $temp = array();
473
474 foreach ($key as $k => $v)
475 {
476 // Do we have a key => ttl pair, or just a key?
477 if (is_int($k))
478 {
479 $k = $v;
480 $v = $ttl;
481 }
482 else
483 {
484 $v += time();
485 }
486
487 if ( ! isset($_SESSION[$k]))
488 {
489 return FALSE;
490 }
491
Andrey Andreev43df7bd2015-02-02 23:22:29 +0200492 $temp[$k] = $v;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300493 }
494
495 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
496 ? array_merge($_SESSION['__ci_vars'], $temp)
497 : $temp;
498
499 return TRUE;
500 }
501
502 if ( ! isset($_SESSION[$key]))
503 {
504 return FALSE;
505 }
506
507 $_SESSION['__ci_vars'][$key] = $ttl;
508 return TRUE;
509 }
510
511 // ------------------------------------------------------------------------
512
513 /**
514 * Get temp keys
515 *
516 * @return array
517 */
518 public function get_temp_keys()
519 {
520 if ( ! isset($_SESSION['__ci_vars']))
521 {
522 return array();
523 }
524
525 $keys = array();
526 foreach (array_keys($_SESSION['__ci_vars']) as $key)
527 {
528 is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key;
529 }
530
531 return $keys;
532 }
533
534 // ------------------------------------------------------------------------
535
536 /**
537 * Unmark flash
538 *
539 * @param mixed $key Session data key(s)
540 * @return void
541 */
542 public function unmark_temp($key)
543 {
544 if (empty($_SESSION['__ci_vars']))
545 {
546 return;
547 }
548
549 is_array($key) OR $key = array($key);
550
551 foreach ($key as $k)
552 {
553 if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k]))
554 {
555 unset($_SESSION['__ci_vars'][$k]);
556 }
557 }
558
559 if (empty($_SESSION['__ci_vars']))
560 {
561 unset($_SESSION['__ci_vars']);
562 }
563 }
564
565 // ------------------------------------------------------------------------
566
567 /**
568 * __get()
569 *
570 * @param string $key 'session_id' or a session data key
571 * @return mixed
572 */
573 public function __get($key)
574 {
575 // Note: Keep this order the same, just in case somebody wants to
576 // use 'session_id' as a session data key, for whatever reason
577 if (isset($_SESSION[$key]))
578 {
579 return $_SESSION[$key];
580 }
581 elseif ($key === 'session_id')
582 {
583 return session_id();
584 }
585
586 return NULL;
587 }
588
589 // ------------------------------------------------------------------------
590
591 /**
592 * __set()
593 *
594 * @param string $key Session data key
595 * @param mixed $value Session data value
596 * @return void
597 */
598 public function __set($key, $value)
599 {
600 $_SESSION[$key] = $value;
601 }
602
603 // ------------------------------------------------------------------------
604
605 /**
606 * Session destroy
607 *
608 * Legacy CI_Session compatibility method
Darren Hillc4e266b2011-08-30 15:40:27 -0400609 *
Darren Hill5073a372011-08-31 13:54:19 -0400610 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400611 */
612 public function sess_destroy()
613 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300614 session_destroy();
Darren Hillc4e266b2011-08-30 15:40:27 -0400615 }
616
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300617 // ------------------------------------------------------------------------
618
Darren Hillc4e266b2011-08-30 15:40:27 -0400619 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300620 * Session regenerate
Darren Hillc4e266b2011-08-30 15:40:27 -0400621 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300622 * Legacy CI_Session compatibility method
623 *
624 * @param bool $destroy Destroy old session data flag
Darren Hill5073a372011-08-31 13:54:19 -0400625 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400626 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300627 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400628 {
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200629 $_SESSION['__ci_last_regenerate'] = time();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300630 session_regenerate_id($destroy);
Darren Hillc4e266b2011-08-30 15:40:27 -0400631 }
632
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300633 // ------------------------------------------------------------------------
634
Darren Hillc4e266b2011-08-30 15:40:27 -0400635 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300636 * Get userdata reference
Darren Hillc4e266b2011-08-30 15:40:27 -0400637 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300638 * Legacy CI_Session compatibility method
639 *
640 * @returns array
Darren Hillc4e266b2011-08-30 15:40:27 -0400641 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300642 public function &get_userdata()
Darren Hillc4e266b2011-08-30 15:40:27 -0400643 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300644 return $_SESSION;
645 }
646
647 // ------------------------------------------------------------------------
648
649 /**
650 * Userdata (fetch)
651 *
652 * Legacy CI_Session compatibility method
653 *
654 * @param string $key Session data key
655 * @return mixed Session data value or NULL if not found
656 */
657 public function userdata($key = NULL)
658 {
659 if (isset($key))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200660 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300661 return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
662 }
663 elseif (empty($_SESSION))
664 {
665 return array();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200666 }
667
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300668 $userdata = array();
669 $_exclude = array_merge(
Andrey Andreevef417862014-06-04 21:28:13 +0300670 array('__ci_vars'),
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300671 $this->get_flash_keys(),
672 $this->get_temp_keys()
673 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400674
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300675 foreach (array_keys($_SESSION) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400676 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300677 if ( ! in_array($key, $_exclude, TRUE))
Darren Hillc4e266b2011-08-30 15:40:27 -0400678 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300679 $userdata[$key] = $_SESSION[$key];
Darren Hillc4e266b2011-08-30 15:40:27 -0400680 }
681 }
682
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300683 return $userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400684 }
685
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300686 // ------------------------------------------------------------------------
687
Darren Hillc4e266b2011-08-30 15:40:27 -0400688 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300689 * Set userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400690 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300691 * Legacy CI_Session compatibility method
692 *
693 * @param mixed $data Session data key or an associative array
694 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400695 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400696 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300697 public function set_userdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400698 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300699 if (is_array($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400700 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300701 foreach ($data as $key => &$value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400702 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300703 $_SESSION[$key] = $value;
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700704 }
705
706 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700707 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400708
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300709 $_SESSION[$data] = $value;
Darren Hillc4e266b2011-08-30 15:40:27 -0400710 }
711
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300712 // ------------------------------------------------------------------------
713
Darren Hillc4e266b2011-08-30 15:40:27 -0400714 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300715 * Unset userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400716 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300717 * Legacy CI_Session compatibility method
718 *
719 * @param mixed $data Session data key(s)
720 * @return void
721 */
722 public function unset_userdata($key)
723 {
724 if (is_array($key))
725 {
726 foreach ($key as $k)
727 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300728 unset($_SESSION[$k]);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300729 }
730
731 return;
732 }
733
734 unset($_SESSION[$key]);
735 }
736
737 // ------------------------------------------------------------------------
738
739 /**
740 * All userdata (fetch)
741 *
742 * Legacy CI_Session compatibility method
743 *
744 * @return array $_SESSION, excluding flash data items
745 */
746 public function all_userdata()
747 {
748 return $this->userdata();
749 }
750
751 // ------------------------------------------------------------------------
752
753 /**
754 * Has userdata
755 *
756 * Legacy CI_Session compatibility method
757 *
758 * @param string $key Session data key
759 * @return bool
760 */
761 public function has_userdata($key)
762 {
763 return isset($_SESSION[$key]);
764 }
765
766 // ------------------------------------------------------------------------
767
768 /**
769 * Flashdata (fetch)
770 *
771 * Legacy CI_Session compatibility method
772 *
773 * @param string $key Session data key
774 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400775 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200776 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400777 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200778 if (isset($key))
779 {
Andrey Andreevef417862014-06-04 21:28:13 +0300780 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300781 ? $_SESSION[$key]
782 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200783 }
784
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300785 $flashdata = array();
786
Andrey Andreevef417862014-06-04 21:28:13 +0300787 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200788 {
Andrey Andreevef417862014-06-04 21:28:13 +0300789 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200790 {
Andrey Andreevef417862014-06-04 21:28:13 +0300791 is_int($value) OR $flashdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200792 }
793 }
794
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300795 return $flashdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400796 }
797
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300798 // ------------------------------------------------------------------------
799
Darren Hillc4e266b2011-08-30 15:40:27 -0400800 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300801 * Set flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400802 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300803 * Legacy CI_Session compatibiliy method
804 *
805 * @param mixed $data Session data key or an associative array
806 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400807 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400808 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300809 public function set_flashdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400810 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300811 $this->set_userdata($data, $value);
Andrey Andreevc6e50982014-10-26 21:27:28 +0200812 $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400813 }
814
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300815 // ------------------------------------------------------------------------
816
Darren Hillc4e266b2011-08-30 15:40:27 -0400817 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300818 * Keep flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400819 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300820 * Legacy CI_Session compatibility method
821 *
822 * @param mixed $key Session data key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400823 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400824 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300825 public function keep_flashdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400826 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300827 $this->mark_as_flash($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400828 }
829
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300830 // ------------------------------------------------------------------------
831
Darren Hillc4e266b2011-08-30 15:40:27 -0400832 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300833 * Temp data (fetch)
Darren Hillc4e266b2011-08-30 15:40:27 -0400834 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300835 * Legacy CI_Session compatibility method
836 *
837 * @param string $key Session data key
838 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400839 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200840 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400841 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200842 if (isset($key))
843 {
Andrey Andreevef417862014-06-04 21:28:13 +0300844 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300845 ? $_SESSION[$key]
846 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200847 }
848
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300849 $tempdata = array();
850
Andrey Andreevef417862014-06-04 21:28:13 +0300851 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200852 {
Andrey Andreevef417862014-06-04 21:28:13 +0300853 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200854 {
Andrey Andreevef417862014-06-04 21:28:13 +0300855 is_int($value) && $tempdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200856 }
857 }
858
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300859 return $tempdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400860 }
861
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300862 // ------------------------------------------------------------------------
863
Darren Hillc4e266b2011-08-30 15:40:27 -0400864 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300865 * Set tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400866 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300867 * Legacy CI_Session compatibility method
868 *
869 * @param mixed $data Session data key or an associative array of items
870 * @param mixed $value Value to store
871 * @param int $ttl Time-to-live in seconds
Darren Hillc4e266b2011-08-30 15:40:27 -0400872 * @return void
873 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300874 public function set_tempdata($data, $value = NULL, $ttl = 300)
Darren Hillc4e266b2011-08-30 15:40:27 -0400875 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300876 $this->set_userdata($data, $value);
877 $this->mark_as_temp($data, $ttl);
Darren Hillc4e266b2011-08-30 15:40:27 -0400878 }
879
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300880 // ------------------------------------------------------------------------
881
Darren Hillc4e266b2011-08-30 15:40:27 -0400882 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300883 * Unset tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400884 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300885 * Legacy CI_Session compatibility method
886 *
887 * @param mixed $data Session data key(s)
Darren Hillc4e266b2011-08-30 15:40:27 -0400888 * @return void
889 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300890 public function unset_tempdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400891 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300892 $this->unmark_temp($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400893 }
894
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300895}