blob: f250c3d6403792a805fd42c8f655a6f295f614b8 [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 Andreev8e60b9a2014-11-04 11:08:06 +0200125 if (($regenerate_time = config_item('sess_time_to_update')) > 0)
126 {
127 if ( ! isset($_SESSION['__ci_last_regenerate']))
128 {
129 $_SESSION['__ci_last_regenerate'] = time();
130 }
131 elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time))
132 {
133 $this->sess_regenerate(FALSE);
134 }
135 }
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300136 // Another work-around ... PHP doesn't seem to send the session cookie
137 // unless it is being currently created or regenerated
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200138 elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300139 {
140 setcookie(
141 $this->_config['cookie_name'],
142 session_id(),
143 (empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']),
144 $this->_config['cookie_path'],
145 $this->_config['cookie_domain'],
146 $this->_config['cookie_secure'],
147 TRUE
148 );
149 }
150
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300151 $this->_ci_init_vars();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300152/*
153 Need to test if this is necessary for a custom driver or if it's only
154 relevant to PHP's own files handler.
155
156 https://bugs.php.net/bug.php?id=65475
157 do this after session is started:
158 if (is_php('5.5.2') && ! is_php('5.5.4'))
159 {
160 $session_id = session_id();
161 if ($_COOKIE[$this->_cookie_name] !== $session_id && file_exists(teh file))
162 {
163 unlink(<teh file>);
164 }
165 }
166*/
167
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300168 log_message('debug', "Session: Class initialized using '".$this->_driver."' driver.");
169 }
170
171 // ------------------------------------------------------------------------
172
173 protected function _ci_load_classes($driver)
174 {
175 // PHP 5.4 compatibility
176 interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php');
177
178 $prefix = config_item('subclass_prefix');
179
180 if ( ! class_exists('CI_Session_driver', FALSE))
181 {
Andrey Andreeve86603f2014-06-11 14:03:36 +0300182 require_once(
183 file_exists(APPPATH.'libraries/Session/Session_driver.php')
184 ? APPPATH.'libraries/Session/Session_driver.php'
185 : BASEPATH.'libraries/Session/Session_driver.php'
186 );
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300187
188 if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php'))
189 {
190 require_once($file_path);
191 }
192 }
193
194 $class = 'Session_'.$driver.'_driver';
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300195 if ( ! class_exists('CI_'.$class, FALSE))
196 {
197 if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php'))
198 {
199 require_once($file_path);
200 }
201
202 if ( ! class_exists('CI_'.$class, FALSE))
203 {
204 log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting.");
205 return FALSE;
206 }
207 }
208
209 if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
210 {
211 require_once($file_path);
212 if (class_exists($prefix.$class, FALSE))
213 {
214 return $prefix.$class;
215 }
216 else
217 {
218 log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.');
219 }
220 }
221
222 return 'CI_'.$class;
223 }
224
225 // ------------------------------------------------------------------------
226
227 /**
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300228 * Configuration
229 *
230 * Handle input parameters and configuration defaults
231 *
232 * @param array &$params Input parameters
233 * @return void
234 */
235 protected function _configure(&$params)
236 {
237 $expiration = config_item('sess_expiration');
238
239 if (isset($params['cookie_lifetime']))
240 {
241 $params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
242 }
243 else
244 {
245 $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close'))
246 ? 0 : (int) $expiration;
247 }
248
249 isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name');
250 if (empty($params['cookie_name']))
251 {
252 $params['cookie_name'] = ini_get('session.name');
253 }
254 else
255 {
256 ini_set('session.name', $params['cookie_name']);
257 }
258
259 isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path');
Andrey Andreev41b546d2014-10-06 03:01:22 +0300260 isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300261 isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure');
262
263 session_set_cookie_params(
264 $params['cookie_lifetime'],
265 $params['cookie_path'],
266 $params['cookie_domain'],
267 $params['cookie_secure'],
268 TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons
269 );
270
271 if (empty($expiration))
272 {
273 $params['expiration'] = (int) ini_get('session.gc_maxlifetime');
274 }
275 else
276 {
277 $params['expiration'] = (int) $expiration;
278 ini_set('session.gc_maxlifetime', $expiration);
279 }
280
281 $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip'));
282
283 isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path');
284
285 $this->_config = $params;
286
287 // Security is king
288 ini_set('session.use_trans_id', 0);
289 ini_set('session.use_strict_mode', 1);
290 ini_set('session.use_cookies', 1);
291 ini_set('session.use_only_cookies', 1);
292 ini_set('session.hash_function', 1);
293 ini_set('session.hash_bits_per_character', 4);
294 }
295
296 // ------------------------------------------------------------------------
297
298 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300299 * Handle temporary variables
300 *
301 * Clears old "flash" data, marks the new one for deletion and handles
302 * "temp" data deletion.
303 *
304 * @return void
305 */
306 protected function _ci_init_vars()
307 {
308 if ( ! empty($_SESSION['__ci_vars']))
309 {
310 $current_time = time();
311
312 foreach ($_SESSION['__ci_vars'] as $key => &$value)
313 {
314 if ($value === 'new')
315 {
316 $_SESSION['__ci_vars'][$key] = 'old';
317 }
318 // Hacky, but 'old' will (implicitly) always be less than time() ;)
319 // DO NOT move this above the 'new' check!
320 elseif ($value < $current_time)
321 {
322 unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
323 }
324 }
325
326 if (empty($_SESSION['__ci_vars']))
327 {
328 unset($_SESSION['__ci_vars']);
Darren Hillc4e266b2011-08-30 15:40:27 -0400329 }
330 }
331 }
332
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300333 // ------------------------------------------------------------------------
334
Darren Hillc4e266b2011-08-30 15:40:27 -0400335 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300336 * Mark as flash
337 *
338 * @param mixed $key Session data key(s)
339 * @return bool
340 */
341 public function mark_as_flash($key)
342 {
343 if (is_array($key))
344 {
345 for ($i = 0, $c = count($key); $i < $c; $i++)
346 {
347 if ( ! isset($_SESSION[$key[$i]]))
348 {
349 return FALSE;
350 }
351 }
352
353 $new = array_fill_keys($key, 'new');
354
355 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
356 ? array_merge($_SESSION['__ci_vars'], $new)
357 : $new;
358
359 return TRUE;
360 }
361
362 if ( ! isset($_SESSION[$key]))
363 {
364 return FALSE;
365 }
366
367 $_SESSION['__ci_vars'][$key] = 'new';
368 return TRUE;
369 }
370
371 // ------------------------------------------------------------------------
372
373 /**
374 * Get flash keys
375 *
376 * @return array
377 */
378 public function get_flash_keys()
379 {
380 if ( ! isset($_SESSION['__ci_vars']))
381 {
382 return array();
383 }
384
385 $keys = array();
386 foreach (array_keys($_SESSION['__ci_vars']) as $key)
387 {
388 is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key;
389 }
390
391 return $keys;
392 }
393
394 // ------------------------------------------------------------------------
395
396 /**
397 * Unmark flash
398 *
399 * @param mixed $key Session data key(s)
400 * @return void
401 */
402 public function unmark_flash($key)
403 {
404 if (empty($_SESSION['__ci_vars']))
405 {
406 return;
407 }
408
409 is_array($key) OR $key = array($key);
410
411 foreach ($key as $k)
412 {
413 if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k]))
414 {
415 unset($_SESSION['__ci_vars'][$k]);
416 }
417 }
418
419 if (empty($_SESSION['__ci_vars']))
420 {
421 unset($_SESSION['__ci_vars']);
422 }
423 }
424
425 // ------------------------------------------------------------------------
426
427 /**
428 * Mark as temp
429 *
430 * @param mixed $key Session data key(s)
431 * @param int $ttl Time-to-live in seconds
432 * @return bool
433 */
434 public function mark_as_temp($key, $ttl = 300)
435 {
436 $ttl += time();
437
438 if (is_array($key))
439 {
440 $temp = array();
441
442 foreach ($key as $k => $v)
443 {
444 // Do we have a key => ttl pair, or just a key?
445 if (is_int($k))
446 {
447 $k = $v;
448 $v = $ttl;
449 }
450 else
451 {
452 $v += time();
453 }
454
455 if ( ! isset($_SESSION[$k]))
456 {
457 return FALSE;
458 }
459
460 $temp[$k] = $ts;
461 }
462
463 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
464 ? array_merge($_SESSION['__ci_vars'], $temp)
465 : $temp;
466
467 return TRUE;
468 }
469
470 if ( ! isset($_SESSION[$key]))
471 {
472 return FALSE;
473 }
474
475 $_SESSION['__ci_vars'][$key] = $ttl;
476 return TRUE;
477 }
478
479 // ------------------------------------------------------------------------
480
481 /**
482 * Get temp keys
483 *
484 * @return array
485 */
486 public function get_temp_keys()
487 {
488 if ( ! isset($_SESSION['__ci_vars']))
489 {
490 return array();
491 }
492
493 $keys = array();
494 foreach (array_keys($_SESSION['__ci_vars']) as $key)
495 {
496 is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key;
497 }
498
499 return $keys;
500 }
501
502 // ------------------------------------------------------------------------
503
504 /**
505 * Unmark flash
506 *
507 * @param mixed $key Session data key(s)
508 * @return void
509 */
510 public function unmark_temp($key)
511 {
512 if (empty($_SESSION['__ci_vars']))
513 {
514 return;
515 }
516
517 is_array($key) OR $key = array($key);
518
519 foreach ($key as $k)
520 {
521 if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k]))
522 {
523 unset($_SESSION['__ci_vars'][$k]);
524 }
525 }
526
527 if (empty($_SESSION['__ci_vars']))
528 {
529 unset($_SESSION['__ci_vars']);
530 }
531 }
532
533 // ------------------------------------------------------------------------
534
535 /**
536 * __get()
537 *
538 * @param string $key 'session_id' or a session data key
539 * @return mixed
540 */
541 public function __get($key)
542 {
543 // Note: Keep this order the same, just in case somebody wants to
544 // use 'session_id' as a session data key, for whatever reason
545 if (isset($_SESSION[$key]))
546 {
547 return $_SESSION[$key];
548 }
549 elseif ($key === 'session_id')
550 {
551 return session_id();
552 }
553
554 return NULL;
555 }
556
557 // ------------------------------------------------------------------------
558
559 /**
560 * __set()
561 *
562 * @param string $key Session data key
563 * @param mixed $value Session data value
564 * @return void
565 */
566 public function __set($key, $value)
567 {
568 $_SESSION[$key] = $value;
569 }
570
571 // ------------------------------------------------------------------------
572
573 /**
574 * Session destroy
575 *
576 * Legacy CI_Session compatibility method
Darren Hillc4e266b2011-08-30 15:40:27 -0400577 *
Darren Hill5073a372011-08-31 13:54:19 -0400578 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400579 */
580 public function sess_destroy()
581 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300582 session_destroy();
Darren Hillc4e266b2011-08-30 15:40:27 -0400583 }
584
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300585 // ------------------------------------------------------------------------
586
Darren Hillc4e266b2011-08-30 15:40:27 -0400587 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300588 * Session regenerate
Darren Hillc4e266b2011-08-30 15:40:27 -0400589 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300590 * Legacy CI_Session compatibility method
591 *
592 * @param bool $destroy Destroy old session data flag
Darren Hill5073a372011-08-31 13:54:19 -0400593 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400594 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300595 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400596 {
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200597 $_SESSION['__ci_last_regenerate'] = time();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300598 session_regenerate_id($destroy);
Darren Hillc4e266b2011-08-30 15:40:27 -0400599 }
600
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300601 // ------------------------------------------------------------------------
602
Darren Hillc4e266b2011-08-30 15:40:27 -0400603 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300604 * Get userdata reference
Darren Hillc4e266b2011-08-30 15:40:27 -0400605 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300606 * Legacy CI_Session compatibility method
607 *
608 * @returns array
Darren Hillc4e266b2011-08-30 15:40:27 -0400609 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300610 public function &get_userdata()
Darren Hillc4e266b2011-08-30 15:40:27 -0400611 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300612 return $_SESSION;
613 }
614
615 // ------------------------------------------------------------------------
616
617 /**
618 * Userdata (fetch)
619 *
620 * Legacy CI_Session compatibility method
621 *
622 * @param string $key Session data key
623 * @return mixed Session data value or NULL if not found
624 */
625 public function userdata($key = NULL)
626 {
627 if (isset($key))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200628 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300629 return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
630 }
631 elseif (empty($_SESSION))
632 {
633 return array();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200634 }
635
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300636 $userdata = array();
637 $_exclude = array_merge(
Andrey Andreevef417862014-06-04 21:28:13 +0300638 array('__ci_vars'),
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300639 $this->get_flash_keys(),
640 $this->get_temp_keys()
641 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400642
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300643 foreach (array_keys($_SESSION) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400644 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300645 if ( ! in_array($key, $_exclude, TRUE))
Darren Hillc4e266b2011-08-30 15:40:27 -0400646 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300647 $userdata[$key] = $_SESSION[$key];
Darren Hillc4e266b2011-08-30 15:40:27 -0400648 }
649 }
650
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300651 return $userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400652 }
653
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300654 // ------------------------------------------------------------------------
655
Darren Hillc4e266b2011-08-30 15:40:27 -0400656 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300657 * Set userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400658 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300659 * Legacy CI_Session compatibility method
660 *
661 * @param mixed $data Session data key or an associative array
662 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400663 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400664 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300665 public function set_userdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400666 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300667 if (is_array($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400668 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300669 foreach ($data as $key => &$value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400670 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300671 $_SESSION[$key] = $value;
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700672 }
673
674 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700675 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400676
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300677 $_SESSION[$data] = $value;
Darren Hillc4e266b2011-08-30 15:40:27 -0400678 }
679
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300680 // ------------------------------------------------------------------------
681
Darren Hillc4e266b2011-08-30 15:40:27 -0400682 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300683 * Unset userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400684 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300685 * Legacy CI_Session compatibility method
686 *
687 * @param mixed $data Session data key(s)
688 * @return void
689 */
690 public function unset_userdata($key)
691 {
692 if (is_array($key))
693 {
694 foreach ($key as $k)
695 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300696 unset($_SESSION[$k]);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300697 }
698
699 return;
700 }
701
702 unset($_SESSION[$key]);
703 }
704
705 // ------------------------------------------------------------------------
706
707 /**
708 * All userdata (fetch)
709 *
710 * Legacy CI_Session compatibility method
711 *
712 * @return array $_SESSION, excluding flash data items
713 */
714 public function all_userdata()
715 {
716 return $this->userdata();
717 }
718
719 // ------------------------------------------------------------------------
720
721 /**
722 * Has userdata
723 *
724 * Legacy CI_Session compatibility method
725 *
726 * @param string $key Session data key
727 * @return bool
728 */
729 public function has_userdata($key)
730 {
731 return isset($_SESSION[$key]);
732 }
733
734 // ------------------------------------------------------------------------
735
736 /**
737 * Flashdata (fetch)
738 *
739 * Legacy CI_Session compatibility method
740 *
741 * @param string $key Session data key
742 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400743 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200744 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400745 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200746 if (isset($key))
747 {
Andrey Andreevef417862014-06-04 21:28:13 +0300748 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300749 ? $_SESSION[$key]
750 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200751 }
752
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300753 $flashdata = array();
754
Andrey Andreevef417862014-06-04 21:28:13 +0300755 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200756 {
Andrey Andreevef417862014-06-04 21:28:13 +0300757 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200758 {
Andrey Andreevef417862014-06-04 21:28:13 +0300759 is_int($value) OR $flashdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200760 }
761 }
762
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300763 return $flashdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400764 }
765
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300766 // ------------------------------------------------------------------------
767
Darren Hillc4e266b2011-08-30 15:40:27 -0400768 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300769 * Set flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400770 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300771 * Legacy CI_Session compatibiliy method
772 *
773 * @param mixed $data Session data key or an associative array
774 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400775 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400776 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300777 public function set_flashdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400778 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300779 $this->set_userdata($data, $value);
Andrey Andreevc6e50982014-10-26 21:27:28 +0200780 $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400781 }
782
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300783 // ------------------------------------------------------------------------
784
Darren Hillc4e266b2011-08-30 15:40:27 -0400785 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300786 * Keep flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400787 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300788 * Legacy CI_Session compatibility method
789 *
790 * @param mixed $key Session data key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400791 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400792 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300793 public function keep_flashdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400794 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300795 $this->mark_as_flash($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400796 }
797
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300798 // ------------------------------------------------------------------------
799
Darren Hillc4e266b2011-08-30 15:40:27 -0400800 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300801 * Temp data (fetch)
Darren Hillc4e266b2011-08-30 15:40:27 -0400802 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300803 * Legacy CI_Session compatibility method
804 *
805 * @param string $key Session data key
806 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400807 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200808 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400809 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200810 if (isset($key))
811 {
Andrey Andreevef417862014-06-04 21:28:13 +0300812 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300813 ? $_SESSION[$key]
814 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200815 }
816
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300817 $tempdata = array();
818
Andrey Andreevef417862014-06-04 21:28:13 +0300819 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200820 {
Andrey Andreevef417862014-06-04 21:28:13 +0300821 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200822 {
Andrey Andreevef417862014-06-04 21:28:13 +0300823 is_int($value) && $tempdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200824 }
825 }
826
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300827 return $tempdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400828 }
829
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300830 // ------------------------------------------------------------------------
831
Darren Hillc4e266b2011-08-30 15:40:27 -0400832 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300833 * Set tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400834 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300835 * Legacy CI_Session compatibility method
836 *
837 * @param mixed $data Session data key or an associative array of items
838 * @param mixed $value Value to store
839 * @param int $ttl Time-to-live in seconds
Darren Hillc4e266b2011-08-30 15:40:27 -0400840 * @return void
841 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300842 public function set_tempdata($data, $value = NULL, $ttl = 300)
Darren Hillc4e266b2011-08-30 15:40:27 -0400843 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300844 $this->set_userdata($data, $value);
845 $this->mark_as_temp($data, $ttl);
Darren Hillc4e266b2011-08-30 15:40:27 -0400846 }
847
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300848 // ------------------------------------------------------------------------
849
Darren Hillc4e266b2011-08-30 15:40:27 -0400850 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300851 * Unset tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400852 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300853 * Legacy CI_Session compatibility method
854 *
855 * @param mixed $data Session data key(s)
Darren Hillc4e266b2011-08-30 15:40:27 -0400856 * @return void
857 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300858 public function unset_tempdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400859 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300860 $this->unmark_temp($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400861 }
862
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300863}
Darren Hillc4e266b2011-08-30 15:40:27 -0400864
865/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300866/* Location: ./system/libraries/Session/Session.php */