blob: 1d93cb1c8ded11c2fab9a0f92d9e766eecc13231 [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 *
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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 Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @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 Andreev47a47fb2014-05-31 16:08:30 +030051 protected $_driver = 'files';
Andrey Andreevdfb39be2014-10-06 01:50:14 +030052 protected $_config;
Darren Hillc4e266b2011-08-30 15:40:27 -040053
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020054 // ------------------------------------------------------------------------
55
Darren Hillc4e266b2011-08-30 15:40:27 -040056 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +030057 * Class constructor
Darren Hillc4e266b2011-08-30 15:40:27 -040058 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +030059 * @param array $params Configuration parameters
Andrey Andreev2e3e2302012-10-09 15:52:34 +030060 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -040061 */
62 public function __construct(array $params = array())
63 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030064 // No sessions under CLI
Andrey Andreevf964b162013-11-12 17:04:55 +020065 if (is_cli())
Andrey Andreev2e3e2302012-10-09 15:52:34 +030066 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +030067 log_message('debug', 'Session: Initialization under CLI aborted.');
68 return;
69 }
70 elseif ((bool) ini_get('session.auto_start'))
71 {
72 log_message('error', 'Session: session.auto_start is enabled in php.ini. Aborting.');
73 return;
74 }
75 elseif ( ! empty($params['driver']))
76 {
77 $this->_driver = $params['driver'];
78 unset($params['driver']);
79 }
Andrey Andreevdfb39be2014-10-06 01:50:14 +030080 elseif ($driver = config_item('sess_driver'))
Andrey Andreev34b1ef52014-05-31 21:23:41 +030081 {
82 $this->_driver = $driver;
83 }
Andrey Andreevac4f4722014-06-02 11:16:32 +030084 // Note: BC workaround
85 elseif (config_item('sess_use_database'))
86 {
87 $this->_driver = 'database';
88 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +030089
90 if (($class = $this->_ci_load_classes($this->_driver)) === FALSE)
91 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030092 return;
93 }
94
Andrey Andreevdfb39be2014-10-06 01:50:14 +030095 // Configuration ...
96 $this->_configure($params);
97
98 $class = new $class($this->_config);
Andrey Andreev47a47fb2014-05-31 16:08:30 +030099 if ($class instanceof SessionHandlerInterface)
Darren Hillc4e266b2011-08-30 15:40:27 -0400100 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300101 if (is_php('5.4'))
Darren Hillc4e266b2011-08-30 15:40:27 -0400102 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300103 session_set_save_handler($class, TRUE);
Darren Hillc4e266b2011-08-30 15:40:27 -0400104 }
105 else
106 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300107 session_set_save_handler(
108 array($class, 'open'),
109 array($class, 'close'),
110 array($class, 'read'),
111 array($class, 'write'),
112 array($class, 'destroy'),
113 array($class, 'gc')
114 );
115
116 register_shutdown_function('session_write_close');
117 }
118 }
119 else
120 {
121 log_message('error', "Session: Driver '".$this->_driver."' doesn't implement SessionHandlerInterface. Aborting.");
122 return;
123 }
124
Andrey Andreev562e39b2014-11-12 15:38:58 +0200125 // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
126 if (isset($_COOKIE[$this->_config['cookie_name']])
127 && (
128 ! is_string($_COOKIE[$this->_config['cookie_name']])
129 OR ! preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']])
130 )
131 )
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300132 {
Andrey Andreev562e39b2014-11-12 15:38:58 +0200133 unset($_COOKIE[$this->_config['cookie_name']]);
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300134 }
135
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300136 session_start();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300137
Andrey Andreevff37ffe2014-11-04 12:28:57 +0200138 // Is session ID auto-regeneration configured? (ignoring ajax requests)
139 if ( ! empty($_SERVER['HTTP_X_REQUESTED_WITH'])
140 && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'
Andrey Andreevde5c2462014-11-04 12:31:03 +0200141 && ($regenerate_time = config_item('sess_time_to_update')) > 0
Andrey Andreevff37ffe2014-11-04 12:28:57 +0200142 )
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200143 {
144 if ( ! isset($_SESSION['__ci_last_regenerate']))
145 {
146 $_SESSION['__ci_last_regenerate'] = time();
147 }
148 elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time))
149 {
150 $this->sess_regenerate(FALSE);
151 }
152 }
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300153 // Another work-around ... PHP doesn't seem to send the session cookie
154 // unless it is being currently created or regenerated
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200155 elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300156 {
157 setcookie(
158 $this->_config['cookie_name'],
159 session_id(),
160 (empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']),
161 $this->_config['cookie_path'],
162 $this->_config['cookie_domain'],
163 $this->_config['cookie_secure'],
164 TRUE
165 );
166 }
167
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300168 $this->_ci_init_vars();
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300169
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300170 log_message('debug', "Session: Class initialized using '".$this->_driver."' driver.");
171 }
172
173 // ------------------------------------------------------------------------
174
175 protected function _ci_load_classes($driver)
176 {
177 // PHP 5.4 compatibility
178 interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php');
179
180 $prefix = config_item('subclass_prefix');
181
182 if ( ! class_exists('CI_Session_driver', FALSE))
183 {
Andrey Andreeve86603f2014-06-11 14:03:36 +0300184 require_once(
185 file_exists(APPPATH.'libraries/Session/Session_driver.php')
186 ? APPPATH.'libraries/Session/Session_driver.php'
187 : BASEPATH.'libraries/Session/Session_driver.php'
188 );
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300189
190 if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php'))
191 {
192 require_once($file_path);
193 }
194 }
195
196 $class = 'Session_'.$driver.'_driver';
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300197 if ( ! class_exists('CI_'.$class, FALSE))
198 {
199 if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php'))
200 {
201 require_once($file_path);
202 }
203
204 if ( ! class_exists('CI_'.$class, FALSE))
205 {
206 log_message('error', "Session: Configured driver '".$driver."' was not found. Aborting.");
207 return FALSE;
208 }
209 }
210
211 if ( ! class_exists($prefix.$class) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
212 {
213 require_once($file_path);
214 if (class_exists($prefix.$class, FALSE))
215 {
216 return $prefix.$class;
217 }
218 else
219 {
220 log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.');
221 }
222 }
223
224 return 'CI_'.$class;
225 }
226
227 // ------------------------------------------------------------------------
228
229 /**
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300230 * Configuration
231 *
232 * Handle input parameters and configuration defaults
233 *
234 * @param array &$params Input parameters
235 * @return void
236 */
237 protected function _configure(&$params)
238 {
239 $expiration = config_item('sess_expiration');
240
241 if (isset($params['cookie_lifetime']))
242 {
243 $params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
244 }
245 else
246 {
247 $params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close'))
248 ? 0 : (int) $expiration;
249 }
250
251 isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name');
252 if (empty($params['cookie_name']))
253 {
254 $params['cookie_name'] = ini_get('session.name');
255 }
256 else
257 {
258 ini_set('session.name', $params['cookie_name']);
259 }
260
261 isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path');
Andrey Andreev41b546d2014-10-06 03:01:22 +0300262 isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300263 isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure');
264
265 session_set_cookie_params(
266 $params['cookie_lifetime'],
267 $params['cookie_path'],
268 $params['cookie_domain'],
269 $params['cookie_secure'],
270 TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons
271 );
272
273 if (empty($expiration))
274 {
275 $params['expiration'] = (int) ini_get('session.gc_maxlifetime');
276 }
277 else
278 {
279 $params['expiration'] = (int) $expiration;
280 ini_set('session.gc_maxlifetime', $expiration);
281 }
282
283 $params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip'));
284
285 isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path');
286
287 $this->_config = $params;
288
289 // Security is king
290 ini_set('session.use_trans_id', 0);
291 ini_set('session.use_strict_mode', 1);
292 ini_set('session.use_cookies', 1);
293 ini_set('session.use_only_cookies', 1);
294 ini_set('session.hash_function', 1);
295 ini_set('session.hash_bits_per_character', 4);
296 }
297
298 // ------------------------------------------------------------------------
299
300 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300301 * Handle temporary variables
302 *
303 * Clears old "flash" data, marks the new one for deletion and handles
304 * "temp" data deletion.
305 *
306 * @return void
307 */
308 protected function _ci_init_vars()
309 {
310 if ( ! empty($_SESSION['__ci_vars']))
311 {
312 $current_time = time();
313
314 foreach ($_SESSION['__ci_vars'] as $key => &$value)
315 {
316 if ($value === 'new')
317 {
318 $_SESSION['__ci_vars'][$key] = 'old';
319 }
320 // Hacky, but 'old' will (implicitly) always be less than time() ;)
321 // DO NOT move this above the 'new' check!
322 elseif ($value < $current_time)
323 {
324 unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
325 }
326 }
327
328 if (empty($_SESSION['__ci_vars']))
329 {
330 unset($_SESSION['__ci_vars']);
Darren Hillc4e266b2011-08-30 15:40:27 -0400331 }
332 }
333 }
334
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300335 // ------------------------------------------------------------------------
336
Darren Hillc4e266b2011-08-30 15:40:27 -0400337 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300338 * Mark as flash
339 *
340 * @param mixed $key Session data key(s)
341 * @return bool
342 */
343 public function mark_as_flash($key)
344 {
345 if (is_array($key))
346 {
347 for ($i = 0, $c = count($key); $i < $c; $i++)
348 {
349 if ( ! isset($_SESSION[$key[$i]]))
350 {
351 return FALSE;
352 }
353 }
354
355 $new = array_fill_keys($key, 'new');
356
357 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
358 ? array_merge($_SESSION['__ci_vars'], $new)
359 : $new;
360
361 return TRUE;
362 }
363
364 if ( ! isset($_SESSION[$key]))
365 {
366 return FALSE;
367 }
368
369 $_SESSION['__ci_vars'][$key] = 'new';
370 return TRUE;
371 }
372
373 // ------------------------------------------------------------------------
374
375 /**
376 * Get flash keys
377 *
378 * @return array
379 */
380 public function get_flash_keys()
381 {
382 if ( ! isset($_SESSION['__ci_vars']))
383 {
384 return array();
385 }
386
387 $keys = array();
388 foreach (array_keys($_SESSION['__ci_vars']) as $key)
389 {
390 is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key;
391 }
392
393 return $keys;
394 }
395
396 // ------------------------------------------------------------------------
397
398 /**
399 * Unmark flash
400 *
401 * @param mixed $key Session data key(s)
402 * @return void
403 */
404 public function unmark_flash($key)
405 {
406 if (empty($_SESSION['__ci_vars']))
407 {
408 return;
409 }
410
411 is_array($key) OR $key = array($key);
412
413 foreach ($key as $k)
414 {
415 if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k]))
416 {
417 unset($_SESSION['__ci_vars'][$k]);
418 }
419 }
420
421 if (empty($_SESSION['__ci_vars']))
422 {
423 unset($_SESSION['__ci_vars']);
424 }
425 }
426
427 // ------------------------------------------------------------------------
428
429 /**
430 * Mark as temp
431 *
432 * @param mixed $key Session data key(s)
433 * @param int $ttl Time-to-live in seconds
434 * @return bool
435 */
436 public function mark_as_temp($key, $ttl = 300)
437 {
438 $ttl += time();
439
440 if (is_array($key))
441 {
442 $temp = array();
443
444 foreach ($key as $k => $v)
445 {
446 // Do we have a key => ttl pair, or just a key?
447 if (is_int($k))
448 {
449 $k = $v;
450 $v = $ttl;
451 }
452 else
453 {
454 $v += time();
455 }
456
457 if ( ! isset($_SESSION[$k]))
458 {
459 return FALSE;
460 }
461
462 $temp[$k] = $ts;
463 }
464
465 $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
466 ? array_merge($_SESSION['__ci_vars'], $temp)
467 : $temp;
468
469 return TRUE;
470 }
471
472 if ( ! isset($_SESSION[$key]))
473 {
474 return FALSE;
475 }
476
477 $_SESSION['__ci_vars'][$key] = $ttl;
478 return TRUE;
479 }
480
481 // ------------------------------------------------------------------------
482
483 /**
484 * Get temp keys
485 *
486 * @return array
487 */
488 public function get_temp_keys()
489 {
490 if ( ! isset($_SESSION['__ci_vars']))
491 {
492 return array();
493 }
494
495 $keys = array();
496 foreach (array_keys($_SESSION['__ci_vars']) as $key)
497 {
498 is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key;
499 }
500
501 return $keys;
502 }
503
504 // ------------------------------------------------------------------------
505
506 /**
507 * Unmark flash
508 *
509 * @param mixed $key Session data key(s)
510 * @return void
511 */
512 public function unmark_temp($key)
513 {
514 if (empty($_SESSION['__ci_vars']))
515 {
516 return;
517 }
518
519 is_array($key) OR $key = array($key);
520
521 foreach ($key as $k)
522 {
523 if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k]))
524 {
525 unset($_SESSION['__ci_vars'][$k]);
526 }
527 }
528
529 if (empty($_SESSION['__ci_vars']))
530 {
531 unset($_SESSION['__ci_vars']);
532 }
533 }
534
535 // ------------------------------------------------------------------------
536
537 /**
538 * __get()
539 *
540 * @param string $key 'session_id' or a session data key
541 * @return mixed
542 */
543 public function __get($key)
544 {
545 // Note: Keep this order the same, just in case somebody wants to
546 // use 'session_id' as a session data key, for whatever reason
547 if (isset($_SESSION[$key]))
548 {
549 return $_SESSION[$key];
550 }
551 elseif ($key === 'session_id')
552 {
553 return session_id();
554 }
555
556 return NULL;
557 }
558
559 // ------------------------------------------------------------------------
560
561 /**
562 * __set()
563 *
564 * @param string $key Session data key
565 * @param mixed $value Session data value
566 * @return void
567 */
568 public function __set($key, $value)
569 {
570 $_SESSION[$key] = $value;
571 }
572
573 // ------------------------------------------------------------------------
574
575 /**
576 * Session destroy
577 *
578 * Legacy CI_Session compatibility method
Darren Hillc4e266b2011-08-30 15:40:27 -0400579 *
Darren Hill5073a372011-08-31 13:54:19 -0400580 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400581 */
582 public function sess_destroy()
583 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300584 session_destroy();
Darren Hillc4e266b2011-08-30 15:40:27 -0400585 }
586
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300587 // ------------------------------------------------------------------------
588
Darren Hillc4e266b2011-08-30 15:40:27 -0400589 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300590 * Session regenerate
Darren Hillc4e266b2011-08-30 15:40:27 -0400591 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300592 * Legacy CI_Session compatibility method
593 *
594 * @param bool $destroy Destroy old session data flag
Darren Hill5073a372011-08-31 13:54:19 -0400595 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400596 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300597 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400598 {
Andrey Andreev8e60b9a2014-11-04 11:08:06 +0200599 $_SESSION['__ci_last_regenerate'] = time();
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300600 session_regenerate_id($destroy);
Darren Hillc4e266b2011-08-30 15:40:27 -0400601 }
602
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300603 // ------------------------------------------------------------------------
604
Darren Hillc4e266b2011-08-30 15:40:27 -0400605 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300606 * Get userdata reference
Darren Hillc4e266b2011-08-30 15:40:27 -0400607 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300608 * Legacy CI_Session compatibility method
609 *
610 * @returns array
Darren Hillc4e266b2011-08-30 15:40:27 -0400611 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300612 public function &get_userdata()
Darren Hillc4e266b2011-08-30 15:40:27 -0400613 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300614 return $_SESSION;
615 }
616
617 // ------------------------------------------------------------------------
618
619 /**
620 * Userdata (fetch)
621 *
622 * Legacy CI_Session compatibility method
623 *
624 * @param string $key Session data key
625 * @return mixed Session data value or NULL if not found
626 */
627 public function userdata($key = NULL)
628 {
629 if (isset($key))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200630 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300631 return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
632 }
633 elseif (empty($_SESSION))
634 {
635 return array();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200636 }
637
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300638 $userdata = array();
639 $_exclude = array_merge(
Andrey Andreevef417862014-06-04 21:28:13 +0300640 array('__ci_vars'),
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300641 $this->get_flash_keys(),
642 $this->get_temp_keys()
643 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400644
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300645 foreach (array_keys($_SESSION) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400646 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300647 if ( ! in_array($key, $_exclude, TRUE))
Darren Hillc4e266b2011-08-30 15:40:27 -0400648 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300649 $userdata[$key] = $_SESSION[$key];
Darren Hillc4e266b2011-08-30 15:40:27 -0400650 }
651 }
652
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300653 return $userdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400654 }
655
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300656 // ------------------------------------------------------------------------
657
Darren Hillc4e266b2011-08-30 15:40:27 -0400658 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300659 * Set userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400660 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300661 * Legacy CI_Session compatibility method
662 *
663 * @param mixed $data Session data key or an associative array
664 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400665 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400666 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300667 public function set_userdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400668 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300669 if (is_array($data))
Darren Hillc4e266b2011-08-30 15:40:27 -0400670 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300671 foreach ($data as $key => &$value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400672 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300673 $_SESSION[$key] = $value;
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700674 }
675
676 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700677 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400678
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300679 $_SESSION[$data] = $value;
Darren Hillc4e266b2011-08-30 15:40:27 -0400680 }
681
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300682 // ------------------------------------------------------------------------
683
Darren Hillc4e266b2011-08-30 15:40:27 -0400684 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300685 * Unset userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400686 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300687 * Legacy CI_Session compatibility method
688 *
689 * @param mixed $data Session data key(s)
690 * @return void
691 */
692 public function unset_userdata($key)
693 {
694 if (is_array($key))
695 {
696 foreach ($key as $k)
697 {
Andrey Andreevd069b9b2014-09-16 10:18:16 +0300698 unset($_SESSION[$k]);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300699 }
700
701 return;
702 }
703
704 unset($_SESSION[$key]);
705 }
706
707 // ------------------------------------------------------------------------
708
709 /**
710 * All userdata (fetch)
711 *
712 * Legacy CI_Session compatibility method
713 *
714 * @return array $_SESSION, excluding flash data items
715 */
716 public function all_userdata()
717 {
718 return $this->userdata();
719 }
720
721 // ------------------------------------------------------------------------
722
723 /**
724 * Has userdata
725 *
726 * Legacy CI_Session compatibility method
727 *
728 * @param string $key Session data key
729 * @return bool
730 */
731 public function has_userdata($key)
732 {
733 return isset($_SESSION[$key]);
734 }
735
736 // ------------------------------------------------------------------------
737
738 /**
739 * Flashdata (fetch)
740 *
741 * Legacy CI_Session compatibility method
742 *
743 * @param string $key Session data key
744 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400745 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200746 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400747 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200748 if (isset($key))
749 {
Andrey Andreevef417862014-06-04 21:28:13 +0300750 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300751 ? $_SESSION[$key]
752 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200753 }
754
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300755 $flashdata = array();
756
Andrey Andreevef417862014-06-04 21:28:13 +0300757 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200758 {
Andrey Andreevef417862014-06-04 21:28:13 +0300759 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200760 {
Andrey Andreevef417862014-06-04 21:28:13 +0300761 is_int($value) OR $flashdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200762 }
763 }
764
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300765 return $flashdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400766 }
767
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300768 // ------------------------------------------------------------------------
769
Darren Hillc4e266b2011-08-30 15:40:27 -0400770 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300771 * Set flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400772 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300773 * Legacy CI_Session compatibiliy method
774 *
775 * @param mixed $data Session data key or an associative array
776 * @param mixed $value Value to store
Darren Hill5073a372011-08-31 13:54:19 -0400777 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400778 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300779 public function set_flashdata($data, $value = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400780 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300781 $this->set_userdata($data, $value);
Andrey Andreevc6e50982014-10-26 21:27:28 +0200782 $this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
Darren Hillc4e266b2011-08-30 15:40:27 -0400783 }
784
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300785 // ------------------------------------------------------------------------
786
Darren Hillc4e266b2011-08-30 15:40:27 -0400787 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300788 * Keep flashdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400789 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300790 * Legacy CI_Session compatibility method
791 *
792 * @param mixed $key Session data key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400793 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400794 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300795 public function keep_flashdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400796 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300797 $this->mark_as_flash($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400798 }
799
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300800 // ------------------------------------------------------------------------
801
Darren Hillc4e266b2011-08-30 15:40:27 -0400802 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300803 * Temp data (fetch)
Darren Hillc4e266b2011-08-30 15:40:27 -0400804 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300805 * Legacy CI_Session compatibility method
806 *
807 * @param string $key Session data key
808 * @return mixed Session data value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400809 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200810 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400811 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200812 if (isset($key))
813 {
Andrey Andreevef417862014-06-04 21:28:13 +0300814 return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300815 ? $_SESSION[$key]
816 : NULL;
Andrey Andreevecc260e2014-01-24 14:20:13 +0200817 }
818
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300819 $tempdata = array();
820
Andrey Andreevef417862014-06-04 21:28:13 +0300821 if ( ! empty($_SESSION['__ci_vars']))
Andrey Andreevecc260e2014-01-24 14:20:13 +0200822 {
Andrey Andreevef417862014-06-04 21:28:13 +0300823 foreach ($_SESSION['__ci_vars'] as $key => &$value)
Andrey Andreevecc260e2014-01-24 14:20:13 +0200824 {
Andrey Andreevef417862014-06-04 21:28:13 +0300825 is_int($value) && $tempdata[$key] = $_SESSION[$key];
Andrey Andreevecc260e2014-01-24 14:20:13 +0200826 }
827 }
828
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300829 return $tempdata;
Darren Hillc4e266b2011-08-30 15:40:27 -0400830 }
831
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300832 // ------------------------------------------------------------------------
833
Darren Hillc4e266b2011-08-30 15:40:27 -0400834 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300835 * Set tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400836 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300837 * Legacy CI_Session compatibility method
838 *
839 * @param mixed $data Session data key or an associative array of items
840 * @param mixed $value Value to store
841 * @param int $ttl Time-to-live in seconds
Darren Hillc4e266b2011-08-30 15:40:27 -0400842 * @return void
843 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300844 public function set_tempdata($data, $value = NULL, $ttl = 300)
Darren Hillc4e266b2011-08-30 15:40:27 -0400845 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300846 $this->set_userdata($data, $value);
847 $this->mark_as_temp($data, $ttl);
Darren Hillc4e266b2011-08-30 15:40:27 -0400848 }
849
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300850 // ------------------------------------------------------------------------
851
Darren Hillc4e266b2011-08-30 15:40:27 -0400852 /**
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300853 * Unset tempdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400854 *
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300855 * Legacy CI_Session compatibility method
856 *
857 * @param mixed $data Session data key(s)
Darren Hillc4e266b2011-08-30 15:40:27 -0400858 * @return void
859 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300860 public function unset_tempdata($key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400861 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300862 $this->unmark_temp($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400863 }
864
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300865}
Darren Hillc4e266b2011-08-30 15:40:27 -0400866
867/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300868/* Location: ./system/libraries/Session/Session.php */