blob: 4a96aa6b1dda5d3b521dd0d5fc9f1f8f272151d4 [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 Andreev47a47fb2014-05-31 16:08:30 +0300177 log_message('debug', "Session: Class initialized using '".$this->_driver."' driver.");
178 }
179
180 // ------------------------------------------------------------------------
181
182 protected function _ci_load_classes($driver)
183 {
184 // PHP 5.4 compatibility
185 interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php');
186
187 $prefix = config_item('subclass_prefix');
188
189 if ( ! class_exists('CI_Session_driver', FALSE))
190 {
Andrey Andreeve86603f2014-06-11 14:03:36 +0300191 require_once(
192 file_exists(APPPATH.'libraries/Session/Session_driver.php')
193 ? APPPATH.'libraries/Session/Session_driver.php'
194 : BASEPATH.'libraries/Session/Session_driver.php'
195 );
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300196
197 if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php'))
198 {
199 require_once($file_path);
200 }
201 }
202
203 $class = 'Session_'.$driver.'_driver';
Andrey Andreevcd94dd72014-12-09 17:38:56 +0200204
205 // Allow custom drivers without the CI_ or MY_ prefix
206 if ( ! class_exists($class, FALSE) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php'))
207 {
208 require_once($file_path);
209 if (class_exists($class, FALSE))
210 {
211 return $class;
212 }
213 }
214
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300215 if ( ! class_exists('CI_'.$class, FALSE))
216 {
217 if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php'))
218 {
219 require_once($file_path);
220 }
221
Andrey Andreevcd94dd72014-12-09 17:38:56 +0200222 if ( ! class_exists('CI_'.$class, FALSE) && ! class_exists($class, FALSE))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300223 {
224 log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting.");
225 return FALSE;
226 }
227 }
228
229 if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
230 {
231 require_once($file_path);
232 if (class_exists($prefix.$class, FALSE))
233 {
234 return $prefix.$class;
235 }
236 else
237 {
238 log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.');
239 }
240 }
241
242 return 'CI_'.$class;
243 }
244
245 // ------------------------------------------------------------------------
246
247 /**
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300248 * Configuration
249 *
250 * Handle input parameters and configuration defaults
251 *
252 * @param array &$params Input parameters
253 * @return void
254 */
255 protected function _configure(&$params)
256 {
257 $expiration = config_item('sess_expiration');
258
259 if (isset($params['cookie_lifetime']))
260 {
261 $params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
262 }
263 else
264 {
265 $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close'))
266 ? 0 : (int) $expiration;
267 }
268
269 isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name');
270 if (empty($params['cookie_name']))
271 {
272 $params['cookie_name'] = ini_get('session.name');
273 }
274 else
275 {
276 ini_set('session.name', $params['cookie_name']);
277 }
278
279 isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path');
Andrey Andreev41b546d2014-10-06 03:01:22 +0300280 isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300281 isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure');
282
283 session_set_cookie_params(
284 $params['cookie_lifetime'],
285 $params['cookie_path'],
286 $params['cookie_domain'],
287 $params['cookie_secure'],
288 TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons
289 );
290
291 if (empty($expiration))
292 {
293 $params['expiration'] = (int) ini_get('session.gc_maxlifetime');
294 }
295 else
296 {
297 $params['expiration'] = (int) $expiration;
298 ini_set('session.gc_maxlifetime', $expiration);
299 }
300
301 $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip'));
302
303 isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path');
304
305 $this->_config = $params;
306
307 // Security is king
308 ini_set('session.use_trans_id', 0);
309 ini_set('session.use_strict_mode', 1);
310 ini_set('session.use_cookies', 1);
311 ini_set('session.use_only_cookies', 1);
312 ini_set('session.hash_function', 1);
313 ini_set('session.hash_bits_per_character', 4);
314 }
315
316 // ------------------------------------------------------------------------
317
318 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300319 * Handle temporary variables
320 *
321 * Clears old "flash" data, marks the new one for deletion and handles
322 * "temp" data deletion.
323 *
324 * @return void
325 */
326 protected function _ci_init_vars()
327 {
328 if ( ! empty($_SESSION['__ci_vars']))
329 {
330 $current_time = time();
331
332 foreach ($_SESSION['__ci_vars'] as $key => &$value)
333 {
334 if ($value === 'new')
335 {
336 $_SESSION['__ci_vars'][$key] = 'old';
337 }
338 // Hacky, but 'old' will (implicitly) always be less than time() ;)
339 // DO NOT move this above the 'new' check!
340 elseif ($value < $current_time)
341 {
342 unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
343 }
344 }
345
346 if (empty($_SESSION['__ci_vars']))
347 {
348 unset($_SESSION['__ci_vars']);
Darren Hillc4e266b2011-08-30 15:40:27 -0400349 }
350 }
Andrey Andreevb4b215e2015-01-19 11:59:11 +0200351
352 $this->userdata =& $_SESSION;
Darren Hillc4e266b2011-08-30 15:40:27 -0400353 }
354
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300355 // ------------------------------------------------------------------------
356
Darren Hillc4e266b2011-08-30 15:40:27 -0400357 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300358 * Mark as flash
359 *
360 * @param mixed $key Session data key(s)
361 * @return bool
362 */
363 public function mark_as_flash($key)
364 {
365 if (is_array($key))
366 {
367 for ($i = 0, $c = count($key); $i < $c; $i++)
368 {
369 if ( ! isset($_SESSION[$key[$i]]))
370 {
371 return FALSE;
372 }
373 }
374
375 $new = array_fill_keys($key, 'new');
376
377 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
378 ? array_merge($_SESSION['__ci_vars'], $new)
379 : $new;
380
381 return TRUE;
382 }
383
384 if ( ! isset($_SESSION[$key]))
385 {
386 return FALSE;
387 }
388
389 $_SESSION['__ci_vars'][$key] = 'new';
390 return TRUE;
391 }
392
393 // ------------------------------------------------------------------------
394
395 /**
396 * Get flash keys
397 *
398 * @return array
399 */
400 public function get_flash_keys()
401 {
402 if ( ! isset($_SESSION['__ci_vars']))
403 {
404 return array();
405 }
406
407 $keys = array();
408 foreach (array_keys($_SESSION['__ci_vars']) as $key)
409 {
410 is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key;
411 }
412
413 return $keys;
414 }
415
416 // ------------------------------------------------------------------------
417
418 /**
419 * Unmark flash
420 *
421 * @param mixed $key Session data key(s)
422 * @return void
423 */
424 public function unmark_flash($key)
425 {
426 if (empty($_SESSION['__ci_vars']))
427 {
428 return;
429 }
430
431 is_array($key) OR $key = array($key);
432
433 foreach ($key as $k)
434 {
435 if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k]))
436 {
437 unset($_SESSION['__ci_vars'][$k]);
438 }
439 }
440
441 if (empty($_SESSION['__ci_vars']))
442 {
443 unset($_SESSION['__ci_vars']);
444 }
445 }
446
447 // ------------------------------------------------------------------------
448
449 /**
450 * Mark as temp
451 *
452 * @param mixed $key Session data key(s)
453 * @param int $ttl Time-to-live in seconds
454 * @return bool
455 */
456 public function mark_as_temp($key, $ttl = 300)
457 {
458 $ttl += time();
459
460 if (is_array($key))
461 {
462 $temp = array();
463
464 foreach ($key as $k => $v)
465 {
466 // Do we have a key => ttl pair, or just a key?
467 if (is_int($k))
468 {
469 $k = $v;
470 $v = $ttl;
471 }
472 else
473 {
474 $v += time();
475 }
476
477 if ( ! isset($_SESSION[$k]))
478 {
479 return FALSE;
480 }
481
482 $temp[$k] = $ts;
483 }
484
485 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
486 ? array_merge($_SESSION['__ci_vars'], $temp)
487 : $temp;
488
489 return TRUE;
490 }
491
492 if ( ! isset($_SESSION[$key]))
493 {
494 return FALSE;
495 }
496
497 $_SESSION['__ci_vars'][$key] = $ttl;
498 return TRUE;
499 }
500
501 // ------------------------------------------------------------------------
502
503 /**
504 * Get temp keys
505 *
506 * @return array
507 */
508 public function get_temp_keys()
509 {
510 if ( ! isset($_SESSION['__ci_vars']))
511 {
512 return array();
513 }
514
515 $keys = array();
516 foreach (array_keys($_SESSION['__ci_vars']) as $key)
517 {
518 is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key;
519 }
520
521 return $keys;
522 }
523
524 // ------------------------------------------------------------------------
525
526 /**
527 * Unmark flash
528 *
529 * @param mixed $key Session data key(s)
530 * @return void
531 */
532 public function unmark_temp($key)
533 {
534 if (empty($_SESSION['__ci_vars']))
535 {
536 return;
537 }
538
539 is_array($key) OR $key = array($key);
540
541 foreach ($key as $k)
542 {
543 if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k]))
544 {
545 unset($_SESSION['__ci_vars'][$k]);
546 }
547 }
548
549 if (empty($_SESSION['__ci_vars']))
550 {
551 unset($_SESSION['__ci_vars']);
552 }
553 }
554
555 // ------------------------------------------------------------------------
556
557 /**
558 * __get()
559 *
560 * @param string $key 'session_id' or a session data key
561 * @return mixed
562 */
563 public function __get($key)
564 {
565 // Note: Keep this order the same, just in case somebody wants to
566 // use 'session_id' as a session data key, for whatever reason
567 if (isset($_SESSION[$key]))
568 {
569 return $_SESSION[$key];
570 }
571 elseif ($key === 'session_id')
572 {
573 return session_id();
574 }
575
576 return NULL;
577 }
578
579 // ------------------------------------------------------------------------
580
581 /**
582 * __set()
583 *
584 * @param string $key Session data key
585 * @param mixed $value Session data value
586 * @return void
587 */
588 public function __set($key, $value)
589 {
590 $_SESSION[$key] = $value;
591 }
592
593 // ------------------------------------------------------------------------
594
595 /**
596 * Session destroy
597 *
598 * Legacy CI_Session compatibility method
Darren Hillc4e266b2011-08-30 15:40:27 -0400599 *
Darren Hill5073a372011-08-31 13:54:19 -0400600 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400601 */
602 public function sess_destroy()
603 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300604 session_destroy();
Darren Hillc4e266b2011-08-30 15:40:27 -0400605 }
606
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300607 // ------------------------------------------------------------------------
608
Darren Hillc4e266b2011-08-30 15:40:27 -0400609 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300610 * Session regenerate
Darren Hillc4e266b2011-08-30 15:40:27 -0400611 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300612 * Legacy CI_Session compatibility method
613 *
614 * @param bool $destroy Destroy old session data flag
Darren Hill5073a372011-08-31 13:54:19 -0400615 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400616 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300617 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400618 {
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200619 $_SESSION['__ci_last_regenerate'] = time();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300620 session_regenerate_id($destroy);
Darren Hillc4e266b2011-08-30 15:40:27 -0400621 }
622
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300623 // ------------------------------------------------------------------------
624
Darren Hillc4e266b2011-08-30 15:40:27 -0400625 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300626 * Get userdata reference
Darren Hillc4e266b2011-08-30 15:40:27 -0400627 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300628 * Legacy CI_Session compatibility method
629 *
630 * @returns array
Darren Hillc4e266b2011-08-30 15:40:27 -0400631 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300632 public function &get_userdata()
Darren Hillc4e266b2011-08-30 15:40:27 -0400633 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300634 return $_SESSION;
635 }
636
637 // ------------------------------------------------------------------------
638
639 /**
640 * Userdata (fetch)
641 *
642 * Legacy CI_Session compatibility method
643 *
644 * @param string $key Session data key
645 * @return mixed Session data value or NULL if not found
646 */
647 public function userdata($key = NULL)
648 {
649 if (isset($key))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200650 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300651 return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
652 }
653 elseif (empty($_SESSION))
654 {
655 return array();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200656 }
657
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300658 $userdata = array();
659 $_exclude = array_merge(
Andrey Andreevef417862014-06-04 21:28:13 +0300660 array('__ci_vars'),
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300661 $this->get_flash_keys(),
662 $this->get_temp_keys()
663 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400664
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300665 foreach (array_keys($_SESSION) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400666 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300667 if ( ! in_array($key, $_exclude, TRUE))
Darren Hillc4e266b2011-08-30 15:40:27 -0400668 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300669 $userdata[$key] = $_SESSION[$key];
Darren Hillc4e266b2011-08-30 15:40:27 -0400670 }
671 }
672
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300673 return $userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400674 }
675
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300676 // ------------------------------------------------------------------------
677
Darren Hillc4e266b2011-08-30 15:40:27 -0400678 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300679 * Set userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400680 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300681 * Legacy CI_Session compatibility method
682 *
683 * @param mixed $data Session data key or an associative array
684 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400685 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400686 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300687 public function set_userdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400688 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300689 if (is_array($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400690 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300691 foreach ($data as $key => &$value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400692 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300693 $_SESSION[$key] = $value;
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700694 }
695
696 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700697 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400698
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300699 $_SESSION[$data] = $value;
Darren Hillc4e266b2011-08-30 15:40:27 -0400700 }
701
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300702 // ------------------------------------------------------------------------
703
Darren Hillc4e266b2011-08-30 15:40:27 -0400704 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300705 * Unset userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400706 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300707 * Legacy CI_Session compatibility method
708 *
709 * @param mixed $data Session data key(s)
710 * @return void
711 */
712 public function unset_userdata($key)
713 {
714 if (is_array($key))
715 {
716 foreach ($key as $k)
717 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300718 unset($_SESSION[$k]);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300719 }
720
721 return;
722 }
723
724 unset($_SESSION[$key]);
725 }
726
727 // ------------------------------------------------------------------------
728
729 /**
730 * All userdata (fetch)
731 *
732 * Legacy CI_Session compatibility method
733 *
734 * @return array $_SESSION, excluding flash data items
735 */
736 public function all_userdata()
737 {
738 return $this->userdata();
739 }
740
741 // ------------------------------------------------------------------------
742
743 /**
744 * Has userdata
745 *
746 * Legacy CI_Session compatibility method
747 *
748 * @param string $key Session data key
749 * @return bool
750 */
751 public function has_userdata($key)
752 {
753 return isset($_SESSION[$key]);
754 }
755
756 // ------------------------------------------------------------------------
757
758 /**
759 * Flashdata (fetch)
760 *
761 * Legacy CI_Session compatibility method
762 *
763 * @param string $key Session data key
764 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400765 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200766 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400767 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200768 if (isset($key))
769 {
Andrey Andreevef417862014-06-04 21:28:13 +0300770 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300771 ? $_SESSION[$key]
772 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200773 }
774
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300775 $flashdata = array();
776
Andrey Andreevef417862014-06-04 21:28:13 +0300777 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200778 {
Andrey Andreevef417862014-06-04 21:28:13 +0300779 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200780 {
Andrey Andreevef417862014-06-04 21:28:13 +0300781 is_int($value) OR $flashdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200782 }
783 }
784
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300785 return $flashdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400786 }
787
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300788 // ------------------------------------------------------------------------
789
Darren Hillc4e266b2011-08-30 15:40:27 -0400790 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300791 * Set flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400792 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300793 * Legacy CI_Session compatibiliy method
794 *
795 * @param mixed $data Session data key or an associative array
796 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400797 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400798 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300799 public function set_flashdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400800 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300801 $this->set_userdata($data, $value);
Andrey Andreevc6e50982014-10-26 21:27:28 +0200802 $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400803 }
804
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300805 // ------------------------------------------------------------------------
806
Darren Hillc4e266b2011-08-30 15:40:27 -0400807 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300808 * Keep flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400809 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300810 * Legacy CI_Session compatibility method
811 *
812 * @param mixed $key Session data key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400813 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400814 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300815 public function keep_flashdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400816 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300817 $this->mark_as_flash($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400818 }
819
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300820 // ------------------------------------------------------------------------
821
Darren Hillc4e266b2011-08-30 15:40:27 -0400822 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300823 * Temp data (fetch)
Darren Hillc4e266b2011-08-30 15:40:27 -0400824 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300825 * Legacy CI_Session compatibility method
826 *
827 * @param string $key Session data key
828 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400829 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200830 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400831 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200832 if (isset($key))
833 {
Andrey Andreevef417862014-06-04 21:28:13 +0300834 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300835 ? $_SESSION[$key]
836 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200837 }
838
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300839 $tempdata = array();
840
Andrey Andreevef417862014-06-04 21:28:13 +0300841 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200842 {
Andrey Andreevef417862014-06-04 21:28:13 +0300843 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200844 {
Andrey Andreevef417862014-06-04 21:28:13 +0300845 is_int($value) && $tempdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200846 }
847 }
848
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300849 return $tempdata;
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 * Set 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 or an associative array of items
860 * @param mixed $value Value to store
861 * @param int $ttl Time-to-live in seconds
Darren Hillc4e266b2011-08-30 15:40:27 -0400862 * @return void
863 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300864 public function set_tempdata($data, $value = NULL, $ttl = 300)
Darren Hillc4e266b2011-08-30 15:40:27 -0400865 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300866 $this->set_userdata($data, $value);
867 $this->mark_as_temp($data, $ttl);
Darren Hillc4e266b2011-08-30 15:40:27 -0400868 }
869
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300870 // ------------------------------------------------------------------------
871
Darren Hillc4e266b2011-08-30 15:40:27 -0400872 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300873 * Unset tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400874 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300875 * Legacy CI_Session compatibility method
876 *
877 * @param mixed $data Session data key(s)
Darren Hillc4e266b2011-08-30 15:40:27 -0400878 * @return void
879 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300880 public function unset_tempdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400881 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300882 $this->unmark_temp($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400883 }
884
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300885}
Darren Hillc4e266b2011-08-30 15:40:27 -0400886
887/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300888/* Location: ./system/libraries/Session/Session.php */