blob: bf11cd181d9a21efc7758d21a24c4e190a29169b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Darren Hillc4e266b2011-08-30 15:40:27 -04002/**
3 * CodeIgniter
4 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +03005 * An open source application development framework for PHP 5.2.4 or newer
6 *
7 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
Darren Hillc4e266b2011-08-30 15:40:27 -040018 *
19 * @package CodeIgniter
Andrey Andreev9ffcee62012-09-05 16:25:16 +030020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreev9ffcee62012-09-05 16:25:16 +030022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Darren Hillc4e266b2011-08-30 15:40:27 -040023 * @link http://codeigniter.com
24 * @since Version 2.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Darren Hillc4e266b2011-08-30 15:40:27 -040028
Darren Hillc4e266b2011-08-30 15:40:27 -040029/**
Andrey Andreev9ffcee62012-09-05 16:25:16 +030030 * CodeIgniter Session Class
Darren Hillc4e266b2011-08-30 15:40:27 -040031 *
Darren Hillc4e266b2011-08-30 15:40:27 -040032 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Sessions
Andrey Andreev47a47fb2014-05-31 16:08:30 +030035 * @author Andrey Andreev
Darren Hillc4e266b2011-08-30 15:40:27 -040036 * @link http://codeigniter.com/user_guide/libraries/sessions.html
37 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +030038class CI_Session {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030039
Andrey Andreev47a47fb2014-05-31 16:08:30 +030040 protected $_driver = 'files';
Andrey Andreevdfb39be2014-10-06 01:50:14 +030041 protected $_config;
Darren Hillc4e266b2011-08-30 15:40:27 -040042
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020043 // ------------------------------------------------------------------------
44
Darren Hillc4e266b2011-08-30 15:40:27 -040045 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +030046 * Class constructor
Darren Hillc4e266b2011-08-30 15:40:27 -040047 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +030048 * @param array $params Configuration parameters
Andrey Andreev2e3e2302012-10-09 15:52:34 +030049 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -040050 */
51 public function __construct(array $params = array())
52 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030053 // No sessions under CLI
Andrey Andreevf964b162013-11-12 17:04:55 +020054 if (is_cli())
Andrey Andreev2e3e2302012-10-09 15:52:34 +030055 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030056 log_message('debug', 'Session: Initialization under CLI aborted.');
57 return;
58 }
59 elseif ((bool) ini_get('session.auto_start'))
60 {
61 log_message('error', 'Session: session.auto_start is enabled in php.ini. Aborting.');
62 return;
63 }
64 elseif ( ! empty($params['driver']))
65 {
66 $this->_driver = $params['driver'];
67 unset($params['driver']);
68 }
Andrey Andreevdfb39be2014-10-06 01:50:14 +030069 elseif ($driver = config_item('sess_driver'))
Andrey Andreev34b1ef52014-05-31 21:23:41 +030070 {
71 $this->_driver = $driver;
72 }
Andrey Andreevac4f4722014-06-02 11:16:32 +030073 // Note: BC workaround
74 elseif (config_item('sess_use_database'))
75 {
76 $this->_driver = 'database';
77 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +030078
79 if (($class = $this->_ci_load_classes($this->_driver)) === FALSE)
80 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030081 return;
82 }
83
Andrey Andreevdfb39be2014-10-06 01:50:14 +030084 // Configuration ...
85 $this->_configure($params);
86
87 $class = new $class($this->_config);
Andrey Andreev47a47fb2014-05-31 16:08:30 +030088 if ($class instanceof SessionHandlerInterface)
Darren Hillc4e266b2011-08-30 15:40:27 -040089 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030090 if (is_php('5.4'))
Darren Hillc4e266b2011-08-30 15:40:27 -040091 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030092 session_set_save_handler($class, TRUE);
Darren Hillc4e266b2011-08-30 15:40:27 -040093 }
94 else
95 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030096 session_set_save_handler(
97 array($class, 'open'),
98 array($class, 'close'),
99 array($class, 'read'),
100 array($class, 'write'),
101 array($class, 'destroy'),
102 array($class, 'gc')
103 );
104
105 register_shutdown_function('session_write_close');
106 }
107 }
108 else
109 {
110 log_message('error', "Session: Driver '".$this->_driver."' doesn't implement SessionHandlerInterface. Aborting.");
111 return;
112 }
113
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300114 // Work-around for PHP bug #66827 (https://bugs.php.net/bug.php?id=66827)
115 //
116 // The session ID sanitizer doesn't check for the value type and blindly does
117 // an implicit cast to string, which triggers an 'Array to string' E_NOTICE.
118 if (isset($_COOKIE[$this->_cookie_name]) && ! is_string($_COOKIE[$this->_cookie_name]))
119 {
120 unset($_COOKIE[$this->_cookie_name]);
121 }
122
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300123 session_start();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300124
Andrey Andreevff37ffe2014-11-04 12:28:57 +0200125 // Is session ID auto-regeneration configured? (ignoring ajax requests)
126 if ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH'])
127 && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'
128 && $regenerate_time = config_item('sess_time_to_update')) > 0
129 )
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200130 {
131 if ( ! isset($_SESSION['__ci_last_regenerate']))
132 {
133 $_SESSION['__ci_last_regenerate'] = time();
134 }
135 elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time))
136 {
137 $this->sess_regenerate(FALSE);
138 }
139 }
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300140 // Another work-around ... PHP doesn't seem to send the session cookie
141 // unless it is being currently created or regenerated
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200142 elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300143 {
144 setcookie(
145 $this->_config['cookie_name'],
146 session_id(),
147 (empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']),
148 $this->_config['cookie_path'],
149 $this->_config['cookie_domain'],
150 $this->_config['cookie_secure'],
151 TRUE
152 );
153 }
154
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300155 $this->_ci_init_vars();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300156/*
157 Need to test if this is necessary for a custom driver or if it's only
158 relevant to PHP's own files handler.
159
160 https://bugs.php.net/bug.php?id=65475
161 do this after session is started:
162 if (is_php('5.5.2') && ! is_php('5.5.4'))
163 {
164 $session_id = session_id();
165 if ($_COOKIE[$this->_cookie_name] !== $session_id && file_exists(teh file))
166 {
167 unlink(<teh file>);
168 }
169 }
170*/
171
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300172 log_message('debug', "Session: Class initialized using '".$this->_driver."' driver.");
173 }
174
175 // ------------------------------------------------------------------------
176
177 protected function _ci_load_classes($driver)
178 {
179 // PHP 5.4 compatibility
180 interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php');
181
182 $prefix = config_item('subclass_prefix');
183
184 if ( ! class_exists('CI_Session_driver', FALSE))
185 {
Andrey Andreeve86603f2014-06-11 14:03:36 +0300186 require_once(
187 file_exists(APPPATH.'libraries/Session/Session_driver.php')
188 ? APPPATH.'libraries/Session/Session_driver.php'
189 : BASEPATH.'libraries/Session/Session_driver.php'
190 );
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300191
192 if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php'))
193 {
194 require_once($file_path);
195 }
196 }
197
198 $class = 'Session_'.$driver.'_driver';
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300199 if ( ! class_exists('CI_'.$class, FALSE))
200 {
201 if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php'))
202 {
203 require_once($file_path);
204 }
205
206 if ( ! class_exists('CI_'.$class, FALSE))
207 {
208 log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting.");
209 return FALSE;
210 }
211 }
212
213 if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
214 {
215 require_once($file_path);
216 if (class_exists($prefix.$class, FALSE))
217 {
218 return $prefix.$class;
219 }
220 else
221 {
222 log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.');
223 }
224 }
225
226 return 'CI_'.$class;
227 }
228
229 // ------------------------------------------------------------------------
230
231 /**
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300232 * Configuration
233 *
234 * Handle input parameters and configuration defaults
235 *
236 * @param array &$params Input parameters
237 * @return void
238 */
239 protected function _configure(&$params)
240 {
241 $expiration = config_item('sess_expiration');
242
243 if (isset($params['cookie_lifetime']))
244 {
245 $params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
246 }
247 else
248 {
249 $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close'))
250 ? 0 : (int) $expiration;
251 }
252
253 isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name');
254 if (empty($params['cookie_name']))
255 {
256 $params['cookie_name'] = ini_get('session.name');
257 }
258 else
259 {
260 ini_set('session.name', $params['cookie_name']);
261 }
262
263 isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path');
Andrey Andreev41b546d2014-10-06 03:01:22 +0300264 isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300265 isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure');
266
267 session_set_cookie_params(
268 $params['cookie_lifetime'],
269 $params['cookie_path'],
270 $params['cookie_domain'],
271 $params['cookie_secure'],
272 TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons
273 );
274
275 if (empty($expiration))
276 {
277 $params['expiration'] = (int) ini_get('session.gc_maxlifetime');
278 }
279 else
280 {
281 $params['expiration'] = (int) $expiration;
282 ini_set('session.gc_maxlifetime', $expiration);
283 }
284
285 $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip'));
286
287 isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path');
288
289 $this->_config = $params;
290
291 // Security is king
292 ini_set('session.use_trans_id', 0);
293 ini_set('session.use_strict_mode', 1);
294 ini_set('session.use_cookies', 1);
295 ini_set('session.use_only_cookies', 1);
296 ini_set('session.hash_function', 1);
297 ini_set('session.hash_bits_per_character', 4);
298 }
299
300 // ------------------------------------------------------------------------
301
302 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300303 * Handle temporary variables
304 *
305 * Clears old "flash" data, marks the new one for deletion and handles
306 * "temp" data deletion.
307 *
308 * @return void
309 */
310 protected function _ci_init_vars()
311 {
312 if ( ! empty($_SESSION['__ci_vars']))
313 {
314 $current_time = time();
315
316 foreach ($_SESSION['__ci_vars'] as $key => &$value)
317 {
318 if ($value === 'new')
319 {
320 $_SESSION['__ci_vars'][$key] = 'old';
321 }
322 // Hacky, but 'old' will (implicitly) always be less than time() ;)
323 // DO NOT move this above the 'new' check!
324 elseif ($value < $current_time)
325 {
326 unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
327 }
328 }
329
330 if (empty($_SESSION['__ci_vars']))
331 {
332 unset($_SESSION['__ci_vars']);
Darren Hillc4e266b2011-08-30 15:40:27 -0400333 }
334 }
335 }
336
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300337 // ------------------------------------------------------------------------
338
Darren Hillc4e266b2011-08-30 15:40:27 -0400339 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300340 * Mark as flash
341 *
342 * @param mixed $key Session data key(s)
343 * @return bool
344 */
345 public function mark_as_flash($key)
346 {
347 if (is_array($key))
348 {
349 for ($i = 0, $c = count($key); $i < $c; $i++)
350 {
351 if ( ! isset($_SESSION[$key[$i]]))
352 {
353 return FALSE;
354 }
355 }
356
357 $new = array_fill_keys($key, 'new');
358
359 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
360 ? array_merge($_SESSION['__ci_vars'], $new)
361 : $new;
362
363 return TRUE;
364 }
365
366 if ( ! isset($_SESSION[$key]))
367 {
368 return FALSE;
369 }
370
371 $_SESSION['__ci_vars'][$key] = 'new';
372 return TRUE;
373 }
374
375 // ------------------------------------------------------------------------
376
377 /**
378 * Get flash keys
379 *
380 * @return array
381 */
382 public function get_flash_keys()
383 {
384 if ( ! isset($_SESSION['__ci_vars']))
385 {
386 return array();
387 }
388
389 $keys = array();
390 foreach (array_keys($_SESSION['__ci_vars']) as $key)
391 {
392 is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key;
393 }
394
395 return $keys;
396 }
397
398 // ------------------------------------------------------------------------
399
400 /**
401 * Unmark flash
402 *
403 * @param mixed $key Session data key(s)
404 * @return void
405 */
406 public function unmark_flash($key)
407 {
408 if (empty($_SESSION['__ci_vars']))
409 {
410 return;
411 }
412
413 is_array($key) OR $key = array($key);
414
415 foreach ($key as $k)
416 {
417 if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k]))
418 {
419 unset($_SESSION['__ci_vars'][$k]);
420 }
421 }
422
423 if (empty($_SESSION['__ci_vars']))
424 {
425 unset($_SESSION['__ci_vars']);
426 }
427 }
428
429 // ------------------------------------------------------------------------
430
431 /**
432 * Mark as temp
433 *
434 * @param mixed $key Session data key(s)
435 * @param int $ttl Time-to-live in seconds
436 * @return bool
437 */
438 public function mark_as_temp($key, $ttl = 300)
439 {
440 $ttl += time();
441
442 if (is_array($key))
443 {
444 $temp = array();
445
446 foreach ($key as $k => $v)
447 {
448 // Do we have a key => ttl pair, or just a key?
449 if (is_int($k))
450 {
451 $k = $v;
452 $v = $ttl;
453 }
454 else
455 {
456 $v += time();
457 }
458
459 if ( ! isset($_SESSION[$k]))
460 {
461 return FALSE;
462 }
463
464 $temp[$k] = $ts;
465 }
466
467 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
468 ? array_merge($_SESSION['__ci_vars'], $temp)
469 : $temp;
470
471 return TRUE;
472 }
473
474 if ( ! isset($_SESSION[$key]))
475 {
476 return FALSE;
477 }
478
479 $_SESSION['__ci_vars'][$key] = $ttl;
480 return TRUE;
481 }
482
483 // ------------------------------------------------------------------------
484
485 /**
486 * Get temp keys
487 *
488 * @return array
489 */
490 public function get_temp_keys()
491 {
492 if ( ! isset($_SESSION['__ci_vars']))
493 {
494 return array();
495 }
496
497 $keys = array();
498 foreach (array_keys($_SESSION['__ci_vars']) as $key)
499 {
500 is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key;
501 }
502
503 return $keys;
504 }
505
506 // ------------------------------------------------------------------------
507
508 /**
509 * Unmark flash
510 *
511 * @param mixed $key Session data key(s)
512 * @return void
513 */
514 public function unmark_temp($key)
515 {
516 if (empty($_SESSION['__ci_vars']))
517 {
518 return;
519 }
520
521 is_array($key) OR $key = array($key);
522
523 foreach ($key as $k)
524 {
525 if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k]))
526 {
527 unset($_SESSION['__ci_vars'][$k]);
528 }
529 }
530
531 if (empty($_SESSION['__ci_vars']))
532 {
533 unset($_SESSION['__ci_vars']);
534 }
535 }
536
537 // ------------------------------------------------------------------------
538
539 /**
540 * __get()
541 *
542 * @param string $key 'session_id' or a session data key
543 * @return mixed
544 */
545 public function __get($key)
546 {
547 // Note: Keep this order the same, just in case somebody wants to
548 // use 'session_id' as a session data key, for whatever reason
549 if (isset($_SESSION[$key]))
550 {
551 return $_SESSION[$key];
552 }
553 elseif ($key === 'session_id')
554 {
555 return session_id();
556 }
557
558 return NULL;
559 }
560
561 // ------------------------------------------------------------------------
562
563 /**
564 * __set()
565 *
566 * @param string $key Session data key
567 * @param mixed $value Session data value
568 * @return void
569 */
570 public function __set($key, $value)
571 {
572 $_SESSION[$key] = $value;
573 }
574
575 // ------------------------------------------------------------------------
576
577 /**
578 * Session destroy
579 *
580 * Legacy CI_Session compatibility method
Darren Hillc4e266b2011-08-30 15:40:27 -0400581 *
Darren Hill5073a372011-08-31 13:54:19 -0400582 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400583 */
584 public function sess_destroy()
585 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300586 session_destroy();
Darren Hillc4e266b2011-08-30 15:40:27 -0400587 }
588
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300589 // ------------------------------------------------------------------------
590
Darren Hillc4e266b2011-08-30 15:40:27 -0400591 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300592 * Session regenerate
Darren Hillc4e266b2011-08-30 15:40:27 -0400593 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300594 * Legacy CI_Session compatibility method
595 *
596 * @param bool $destroy Destroy old session data flag
Darren Hill5073a372011-08-31 13:54:19 -0400597 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400598 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300599 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400600 {
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200601 $_SESSION['__ci_last_regenerate'] = time();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300602 session_regenerate_id($destroy);
Darren Hillc4e266b2011-08-30 15:40:27 -0400603 }
604
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300605 // ------------------------------------------------------------------------
606
Darren Hillc4e266b2011-08-30 15:40:27 -0400607 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300608 * Get userdata reference
Darren Hillc4e266b2011-08-30 15:40:27 -0400609 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300610 * Legacy CI_Session compatibility method
611 *
612 * @returns array
Darren Hillc4e266b2011-08-30 15:40:27 -0400613 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300614 public function &get_userdata()
Darren Hillc4e266b2011-08-30 15:40:27 -0400615 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300616 return $_SESSION;
617 }
618
619 // ------------------------------------------------------------------------
620
621 /**
622 * Userdata (fetch)
623 *
624 * Legacy CI_Session compatibility method
625 *
626 * @param string $key Session data key
627 * @return mixed Session data value or NULL if not found
628 */
629 public function userdata($key = NULL)
630 {
631 if (isset($key))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200632 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300633 return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
634 }
635 elseif (empty($_SESSION))
636 {
637 return array();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200638 }
639
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300640 $userdata = array();
641 $_exclude = array_merge(
Andrey Andreevef417862014-06-04 21:28:13 +0300642 array('__ci_vars'),
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300643 $this->get_flash_keys(),
644 $this->get_temp_keys()
645 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400646
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300647 foreach (array_keys($_SESSION) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400648 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300649 if ( ! in_array($key, $_exclude, TRUE))
Darren Hillc4e266b2011-08-30 15:40:27 -0400650 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300651 $userdata[$key] = $_SESSION[$key];
Darren Hillc4e266b2011-08-30 15:40:27 -0400652 }
653 }
654
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300655 return $userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400656 }
657
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300658 // ------------------------------------------------------------------------
659
Darren Hillc4e266b2011-08-30 15:40:27 -0400660 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300661 * Set userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400662 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300663 * Legacy CI_Session compatibility method
664 *
665 * @param mixed $data Session data key or an associative array
666 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400667 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400668 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300669 public function set_userdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400670 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300671 if (is_array($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400672 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300673 foreach ($data as $key => &$value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400674 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300675 $_SESSION[$key] = $value;
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700676 }
677
678 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700679 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400680
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300681 $_SESSION[$data] = $value;
Darren Hillc4e266b2011-08-30 15:40:27 -0400682 }
683
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300684 // ------------------------------------------------------------------------
685
Darren Hillc4e266b2011-08-30 15:40:27 -0400686 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300687 * Unset userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400688 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300689 * Legacy CI_Session compatibility method
690 *
691 * @param mixed $data Session data key(s)
692 * @return void
693 */
694 public function unset_userdata($key)
695 {
696 if (is_array($key))
697 {
698 foreach ($key as $k)
699 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300700 unset($_SESSION[$k]);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300701 }
702
703 return;
704 }
705
706 unset($_SESSION[$key]);
707 }
708
709 // ------------------------------------------------------------------------
710
711 /**
712 * All userdata (fetch)
713 *
714 * Legacy CI_Session compatibility method
715 *
716 * @return array $_SESSION, excluding flash data items
717 */
718 public function all_userdata()
719 {
720 return $this->userdata();
721 }
722
723 // ------------------------------------------------------------------------
724
725 /**
726 * Has userdata
727 *
728 * Legacy CI_Session compatibility method
729 *
730 * @param string $key Session data key
731 * @return bool
732 */
733 public function has_userdata($key)
734 {
735 return isset($_SESSION[$key]);
736 }
737
738 // ------------------------------------------------------------------------
739
740 /**
741 * Flashdata (fetch)
742 *
743 * Legacy CI_Session compatibility method
744 *
745 * @param string $key Session data key
746 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400747 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200748 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400749 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200750 if (isset($key))
751 {
Andrey Andreevef417862014-06-04 21:28:13 +0300752 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300753 ? $_SESSION[$key]
754 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200755 }
756
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300757 $flashdata = array();
758
Andrey Andreevef417862014-06-04 21:28:13 +0300759 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200760 {
Andrey Andreevef417862014-06-04 21:28:13 +0300761 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200762 {
Andrey Andreevef417862014-06-04 21:28:13 +0300763 is_int($value) OR $flashdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200764 }
765 }
766
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300767 return $flashdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400768 }
769
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300770 // ------------------------------------------------------------------------
771
Darren Hillc4e266b2011-08-30 15:40:27 -0400772 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300773 * Set flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400774 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300775 * Legacy CI_Session compatibiliy method
776 *
777 * @param mixed $data Session data key or an associative array
778 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400779 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400780 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300781 public function set_flashdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400782 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300783 $this->set_userdata($data, $value);
Andrey Andreevc6e50982014-10-26 21:27:28 +0200784 $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400785 }
786
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300787 // ------------------------------------------------------------------------
788
Darren Hillc4e266b2011-08-30 15:40:27 -0400789 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300790 * Keep flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400791 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300792 * Legacy CI_Session compatibility method
793 *
794 * @param mixed $key Session data key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400795 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400796 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300797 public function keep_flashdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400798 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300799 $this->mark_as_flash($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400800 }
801
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300802 // ------------------------------------------------------------------------
803
Darren Hillc4e266b2011-08-30 15:40:27 -0400804 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300805 * Temp data (fetch)
Darren Hillc4e266b2011-08-30 15:40:27 -0400806 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300807 * Legacy CI_Session compatibility method
808 *
809 * @param string $key Session data key
810 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400811 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200812 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400813 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200814 if (isset($key))
815 {
Andrey Andreevef417862014-06-04 21:28:13 +0300816 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300817 ? $_SESSION[$key]
818 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200819 }
820
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300821 $tempdata = array();
822
Andrey Andreevef417862014-06-04 21:28:13 +0300823 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200824 {
Andrey Andreevef417862014-06-04 21:28:13 +0300825 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200826 {
Andrey Andreevef417862014-06-04 21:28:13 +0300827 is_int($value) && $tempdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200828 }
829 }
830
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300831 return $tempdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400832 }
833
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300834 // ------------------------------------------------------------------------
835
Darren Hillc4e266b2011-08-30 15:40:27 -0400836 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300837 * Set tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400838 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300839 * Legacy CI_Session compatibility method
840 *
841 * @param mixed $data Session data key or an associative array of items
842 * @param mixed $value Value to store
843 * @param int $ttl Time-to-live in seconds
Darren Hillc4e266b2011-08-30 15:40:27 -0400844 * @return void
845 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300846 public function set_tempdata($data, $value = NULL, $ttl = 300)
Darren Hillc4e266b2011-08-30 15:40:27 -0400847 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300848 $this->set_userdata($data, $value);
849 $this->mark_as_temp($data, $ttl);
Darren Hillc4e266b2011-08-30 15:40:27 -0400850 }
851
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300852 // ------------------------------------------------------------------------
853
Darren Hillc4e266b2011-08-30 15:40:27 -0400854 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300855 * Unset tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400856 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300857 * Legacy CI_Session compatibility method
858 *
859 * @param mixed $data Session data key(s)
Darren Hillc4e266b2011-08-30 15:40:27 -0400860 * @return void
861 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300862 public function unset_tempdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400863 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300864 $this->unmark_temp($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400865 }
866
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300867}
Darren Hillc4e266b2011-08-30 15:40:27 -0400868
869/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300870/* Location: ./system/libraries/Session/Session.php */