blob: f3b819af94416df29037e234c6bce2d3a73d416e [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)
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 {
233 log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting.");
234 return FALSE;
235 }
236 }
237
238 if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
239 {
240 require_once($file_path);
241 if (class_exists($prefix.$class, FALSE))
242 {
243 return $prefix.$class;
244 }
245 else
246 {
247 log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.');
248 }
249 }
250
251 return 'CI_'.$class;
252 }
253
254 // ------------------------------------------------------------------------
255
256 /**
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300257 * Configuration
258 *
259 * Handle input parameters and configuration defaults
260 *
261 * @param array &$params Input parameters
262 * @return void
263 */
264 protected function _configure(&$params)
265 {
266 $expiration = config_item('sess_expiration');
267
268 if (isset($params['cookie_lifetime']))
269 {
270 $params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
271 }
272 else
273 {
274 $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close'))
275 ? 0 : (int) $expiration;
276 }
277
278 isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name');
279 if (empty($params['cookie_name']))
280 {
281 $params['cookie_name'] = ini_get('session.name');
282 }
283 else
284 {
285 ini_set('session.name', $params['cookie_name']);
286 }
287
288 isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path');
Andrey Andreev41b546d2014-10-06 03:01:22 +0300289 isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300290 isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure');
291
292 session_set_cookie_params(
293 $params['cookie_lifetime'],
294 $params['cookie_path'],
295 $params['cookie_domain'],
296 $params['cookie_secure'],
297 TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons
298 );
299
300 if (empty($expiration))
301 {
302 $params['expiration'] = (int) ini_get('session.gc_maxlifetime');
303 }
304 else
305 {
306 $params['expiration'] = (int) $expiration;
307 ini_set('session.gc_maxlifetime', $expiration);
308 }
309
310 $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip'));
311
312 isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path');
313
314 $this->_config = $params;
315
316 // Security is king
Andrey Andreevc02952d2015-02-13 13:04:38 +0200317 ini_set('session.use_trans_sid', 0);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300318 ini_set('session.use_strict_mode', 1);
319 ini_set('session.use_cookies', 1);
320 ini_set('session.use_only_cookies', 1);
321 ini_set('session.hash_function', 1);
322 ini_set('session.hash_bits_per_character', 4);
323 }
324
325 // ------------------------------------------------------------------------
326
327 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300328 * Handle temporary variables
329 *
330 * Clears old "flash" data, marks the new one for deletion and handles
331 * "temp" data deletion.
332 *
333 * @return void
334 */
335 protected function _ci_init_vars()
336 {
337 if ( ! empty($_SESSION['__ci_vars']))
338 {
339 $current_time = time();
340
341 foreach ($_SESSION['__ci_vars'] as $key => &$value)
342 {
343 if ($value === 'new')
344 {
345 $_SESSION['__ci_vars'][$key] = 'old';
346 }
347 // Hacky, but 'old' will (implicitly) always be less than time() ;)
348 // DO NOT move this above the 'new' check!
349 elseif ($value < $current_time)
350 {
351 unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
352 }
353 }
354
355 if (empty($_SESSION['__ci_vars']))
356 {
357 unset($_SESSION['__ci_vars']);
Darren Hillc4e266b2011-08-30 15:40:27 -0400358 }
359 }
Andrey Andreevb4b215e2015-01-19 11:59:11 +0200360
361 $this->userdata =& $_SESSION;
Darren Hillc4e266b2011-08-30 15:40:27 -0400362 }
363
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300364 // ------------------------------------------------------------------------
365
Darren Hillc4e266b2011-08-30 15:40:27 -0400366 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300367 * Mark as flash
368 *
369 * @param mixed $key Session data key(s)
370 * @return bool
371 */
372 public function mark_as_flash($key)
373 {
374 if (is_array($key))
375 {
376 for ($i = 0, $c = count($key); $i < $c; $i++)
377 {
378 if ( ! isset($_SESSION[$key[$i]]))
379 {
380 return FALSE;
381 }
382 }
383
384 $new = array_fill_keys($key, 'new');
385
386 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
387 ? array_merge($_SESSION['__ci_vars'], $new)
388 : $new;
389
390 return TRUE;
391 }
392
393 if ( ! isset($_SESSION[$key]))
394 {
395 return FALSE;
396 }
397
398 $_SESSION['__ci_vars'][$key] = 'new';
399 return TRUE;
400 }
401
402 // ------------------------------------------------------------------------
403
404 /**
405 * Get flash keys
406 *
407 * @return array
408 */
409 public function get_flash_keys()
410 {
411 if ( ! isset($_SESSION['__ci_vars']))
412 {
413 return array();
414 }
415
416 $keys = array();
417 foreach (array_keys($_SESSION['__ci_vars']) as $key)
418 {
419 is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key;
420 }
421
422 return $keys;
423 }
424
425 // ------------------------------------------------------------------------
426
427 /**
428 * Unmark flash
429 *
430 * @param mixed $key Session data key(s)
431 * @return void
432 */
433 public function unmark_flash($key)
434 {
435 if (empty($_SESSION['__ci_vars']))
436 {
437 return;
438 }
439
440 is_array($key) OR $key = array($key);
441
442 foreach ($key as $k)
443 {
444 if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k]))
445 {
446 unset($_SESSION['__ci_vars'][$k]);
447 }
448 }
449
450 if (empty($_SESSION['__ci_vars']))
451 {
452 unset($_SESSION['__ci_vars']);
453 }
454 }
455
456 // ------------------------------------------------------------------------
457
458 /**
459 * Mark as temp
460 *
461 * @param mixed $key Session data key(s)
462 * @param int $ttl Time-to-live in seconds
463 * @return bool
464 */
465 public function mark_as_temp($key, $ttl = 300)
466 {
467 $ttl += time();
468
469 if (is_array($key))
470 {
471 $temp = array();
472
473 foreach ($key as $k => $v)
474 {
475 // Do we have a key => ttl pair, or just a key?
476 if (is_int($k))
477 {
478 $k = $v;
479 $v = $ttl;
480 }
481 else
482 {
483 $v += time();
484 }
485
486 if ( ! isset($_SESSION[$k]))
487 {
488 return FALSE;
489 }
490
Andrey Andreev43df7bd2015-02-02 23:22:29 +0200491 $temp[$k] = $v;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300492 }
493
494 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
495 ? array_merge($_SESSION['__ci_vars'], $temp)
496 : $temp;
497
498 return TRUE;
499 }
500
501 if ( ! isset($_SESSION[$key]))
502 {
503 return FALSE;
504 }
505
506 $_SESSION['__ci_vars'][$key] = $ttl;
507 return TRUE;
508 }
509
510 // ------------------------------------------------------------------------
511
512 /**
513 * Get temp keys
514 *
515 * @return array
516 */
517 public function get_temp_keys()
518 {
519 if ( ! isset($_SESSION['__ci_vars']))
520 {
521 return array();
522 }
523
524 $keys = array();
525 foreach (array_keys($_SESSION['__ci_vars']) as $key)
526 {
527 is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key;
528 }
529
530 return $keys;
531 }
532
533 // ------------------------------------------------------------------------
534
535 /**
536 * Unmark flash
537 *
538 * @param mixed $key Session data key(s)
539 * @return void
540 */
541 public function unmark_temp($key)
542 {
543 if (empty($_SESSION['__ci_vars']))
544 {
545 return;
546 }
547
548 is_array($key) OR $key = array($key);
549
550 foreach ($key as $k)
551 {
552 if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k]))
553 {
554 unset($_SESSION['__ci_vars'][$k]);
555 }
556 }
557
558 if (empty($_SESSION['__ci_vars']))
559 {
560 unset($_SESSION['__ci_vars']);
561 }
562 }
563
564 // ------------------------------------------------------------------------
565
566 /**
567 * __get()
568 *
569 * @param string $key 'session_id' or a session data key
570 * @return mixed
571 */
572 public function __get($key)
573 {
574 // Note: Keep this order the same, just in case somebody wants to
575 // use 'session_id' as a session data key, for whatever reason
576 if (isset($_SESSION[$key]))
577 {
578 return $_SESSION[$key];
579 }
580 elseif ($key === 'session_id')
581 {
582 return session_id();
583 }
584
585 return NULL;
586 }
587
588 // ------------------------------------------------------------------------
589
590 /**
591 * __set()
592 *
593 * @param string $key Session data key
594 * @param mixed $value Session data value
595 * @return void
596 */
597 public function __set($key, $value)
598 {
599 $_SESSION[$key] = $value;
600 }
601
602 // ------------------------------------------------------------------------
603
604 /**
605 * Session destroy
606 *
607 * Legacy CI_Session compatibility method
Darren Hillc4e266b2011-08-30 15:40:27 -0400608 *
Darren Hill5073a372011-08-31 13:54:19 -0400609 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400610 */
611 public function sess_destroy()
612 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300613 session_destroy();
Darren Hillc4e266b2011-08-30 15:40:27 -0400614 }
615
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300616 // ------------------------------------------------------------------------
617
Darren Hillc4e266b2011-08-30 15:40:27 -0400618 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300619 * Session regenerate
Darren Hillc4e266b2011-08-30 15:40:27 -0400620 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300621 * Legacy CI_Session compatibility method
622 *
623 * @param bool $destroy Destroy old session data flag
Darren Hill5073a372011-08-31 13:54:19 -0400624 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400625 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300626 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400627 {
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200628 $_SESSION['__ci_last_regenerate'] = time();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300629 session_regenerate_id($destroy);
Darren Hillc4e266b2011-08-30 15:40:27 -0400630 }
631
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300632 // ------------------------------------------------------------------------
633
Darren Hillc4e266b2011-08-30 15:40:27 -0400634 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300635 * Get userdata reference
Darren Hillc4e266b2011-08-30 15:40:27 -0400636 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300637 * Legacy CI_Session compatibility method
638 *
639 * @returns array
Darren Hillc4e266b2011-08-30 15:40:27 -0400640 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300641 public function &get_userdata()
Darren Hillc4e266b2011-08-30 15:40:27 -0400642 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300643 return $_SESSION;
644 }
645
646 // ------------------------------------------------------------------------
647
648 /**
649 * Userdata (fetch)
650 *
651 * Legacy CI_Session compatibility method
652 *
653 * @param string $key Session data key
654 * @return mixed Session data value or NULL if not found
655 */
656 public function userdata($key = NULL)
657 {
658 if (isset($key))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200659 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300660 return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
661 }
662 elseif (empty($_SESSION))
663 {
664 return array();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200665 }
666
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300667 $userdata = array();
668 $_exclude = array_merge(
Andrey Andreevef417862014-06-04 21:28:13 +0300669 array('__ci_vars'),
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300670 $this->get_flash_keys(),
671 $this->get_temp_keys()
672 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400673
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300674 foreach (array_keys($_SESSION) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400675 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300676 if ( ! in_array($key, $_exclude, TRUE))
Darren Hillc4e266b2011-08-30 15:40:27 -0400677 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300678 $userdata[$key] = $_SESSION[$key];
Darren Hillc4e266b2011-08-30 15:40:27 -0400679 }
680 }
681
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300682 return $userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400683 }
684
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300685 // ------------------------------------------------------------------------
686
Darren Hillc4e266b2011-08-30 15:40:27 -0400687 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300688 * Set userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400689 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300690 * Legacy CI_Session compatibility method
691 *
692 * @param mixed $data Session data key or an associative array
693 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400694 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400695 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300696 public function set_userdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400697 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300698 if (is_array($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400699 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300700 foreach ($data as $key => &$value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400701 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300702 $_SESSION[$key] = $value;
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700703 }
704
705 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700706 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400707
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300708 $_SESSION[$data] = $value;
Darren Hillc4e266b2011-08-30 15:40:27 -0400709 }
710
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300711 // ------------------------------------------------------------------------
712
Darren Hillc4e266b2011-08-30 15:40:27 -0400713 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300714 * Unset userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400715 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300716 * Legacy CI_Session compatibility method
717 *
718 * @param mixed $data Session data key(s)
719 * @return void
720 */
721 public function unset_userdata($key)
722 {
723 if (is_array($key))
724 {
725 foreach ($key as $k)
726 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300727 unset($_SESSION[$k]);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300728 }
729
730 return;
731 }
732
733 unset($_SESSION[$key]);
734 }
735
736 // ------------------------------------------------------------------------
737
738 /**
739 * All userdata (fetch)
740 *
741 * Legacy CI_Session compatibility method
742 *
743 * @return array $_SESSION, excluding flash data items
744 */
745 public function all_userdata()
746 {
747 return $this->userdata();
748 }
749
750 // ------------------------------------------------------------------------
751
752 /**
753 * Has userdata
754 *
755 * Legacy CI_Session compatibility method
756 *
757 * @param string $key Session data key
758 * @return bool
759 */
760 public function has_userdata($key)
761 {
762 return isset($_SESSION[$key]);
763 }
764
765 // ------------------------------------------------------------------------
766
767 /**
768 * Flashdata (fetch)
769 *
770 * Legacy CI_Session compatibility method
771 *
772 * @param string $key Session data key
773 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400774 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200775 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400776 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200777 if (isset($key))
778 {
Andrey Andreevef417862014-06-04 21:28:13 +0300779 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300780 ? $_SESSION[$key]
781 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200782 }
783
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300784 $flashdata = array();
785
Andrey Andreevef417862014-06-04 21:28:13 +0300786 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200787 {
Andrey Andreevef417862014-06-04 21:28:13 +0300788 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200789 {
Andrey Andreevef417862014-06-04 21:28:13 +0300790 is_int($value) OR $flashdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200791 }
792 }
793
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300794 return $flashdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400795 }
796
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300797 // ------------------------------------------------------------------------
798
Darren Hillc4e266b2011-08-30 15:40:27 -0400799 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300800 * Set flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400801 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300802 * Legacy CI_Session compatibiliy method
803 *
804 * @param mixed $data Session data key or an associative array
805 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400806 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400807 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300808 public function set_flashdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400809 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300810 $this->set_userdata($data, $value);
Andrey Andreevc6e50982014-10-26 21:27:28 +0200811 $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400812 }
813
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300814 // ------------------------------------------------------------------------
815
Darren Hillc4e266b2011-08-30 15:40:27 -0400816 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300817 * Keep flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400818 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300819 * Legacy CI_Session compatibility method
820 *
821 * @param mixed $key Session data key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400822 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400823 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300824 public function keep_flashdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400825 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300826 $this->mark_as_flash($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400827 }
828
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300829 // ------------------------------------------------------------------------
830
Darren Hillc4e266b2011-08-30 15:40:27 -0400831 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300832 * Temp data (fetch)
Darren Hillc4e266b2011-08-30 15:40:27 -0400833 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300834 * Legacy CI_Session compatibility method
835 *
836 * @param string $key Session data key
837 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400838 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200839 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400840 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200841 if (isset($key))
842 {
Andrey Andreevef417862014-06-04 21:28:13 +0300843 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300844 ? $_SESSION[$key]
845 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200846 }
847
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300848 $tempdata = array();
849
Andrey Andreevef417862014-06-04 21:28:13 +0300850 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200851 {
Andrey Andreevef417862014-06-04 21:28:13 +0300852 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200853 {
Andrey Andreevef417862014-06-04 21:28:13 +0300854 is_int($value) && $tempdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200855 }
856 }
857
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300858 return $tempdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400859 }
860
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300861 // ------------------------------------------------------------------------
862
Darren Hillc4e266b2011-08-30 15:40:27 -0400863 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300864 * Set tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400865 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300866 * Legacy CI_Session compatibility method
867 *
868 * @param mixed $data Session data key or an associative array of items
869 * @param mixed $value Value to store
870 * @param int $ttl Time-to-live in seconds
Darren Hillc4e266b2011-08-30 15:40:27 -0400871 * @return void
872 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300873 public function set_tempdata($data, $value = NULL, $ttl = 300)
Darren Hillc4e266b2011-08-30 15:40:27 -0400874 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300875 $this->set_userdata($data, $value);
876 $this->mark_as_temp($data, $ttl);
Darren Hillc4e266b2011-08-30 15:40:27 -0400877 }
878
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300879 // ------------------------------------------------------------------------
880
Darren Hillc4e266b2011-08-30 15:40:27 -0400881 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300882 * Unset tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400883 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300884 * Legacy CI_Session compatibility method
885 *
886 * @param mixed $data Session data key(s)
Darren Hillc4e266b2011-08-30 15:40:27 -0400887 * @return void
888 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300889 public function unset_tempdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400890 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300891 $this->unmark_temp($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400892 }
893
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300894}