Subversion-Projekte lars-tiefland.ci

Revision

Revision 2242 | Revision 2257 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
2254 lars 9
 * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
68 lars 10
 *
11
 * 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:
17
 *
18
 * 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
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
2254 lars 32
 * @copyright	Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
68 lars 33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
35
 * @since	Version 2.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * CodeIgniter Session Class
42
 *
43
 * @package		CodeIgniter
44
 * @subpackage	Libraries
45
 * @category	Sessions
46
 * @author		Andrey Andreev
47
 * @link		https://codeigniter.com/user_guide/libraries/sessions.html
48
 */
49
class CI_Session {
50
 
51
	/**
52
	 * Userdata array
53
	 *
54
	 * Just a reference to $_SESSION, for BC purposes.
55
	 */
56
	public $userdata;
57
 
58
	protected $_driver = 'files';
59
	protected $_config;
1257 lars 60
	protected $_sid_regexp;
68 lars 61
 
62
	// ------------------------------------------------------------------------
63
 
64
	/**
65
	 * Class constructor
66
	 *
67
	 * @param	array	$params	Configuration parameters
68
	 * @return	void
69
	 */
70
	public function __construct(array $params = array())
71
	{
72
		// No sessions under CLI
73
		if (is_cli())
74
		{
75
			log_message('debug', 'Session: Initialization under CLI aborted.');
76
			return;
77
		}
78
		elseif ((bool) ini_get('session.auto_start'))
79
		{
80
			log_message('error', 'Session: session.auto_start is enabled in php.ini. Aborting.');
81
			return;
82
		}
83
		elseif ( ! empty($params['driver']))
84
		{
85
			$this->_driver = $params['driver'];
86
			unset($params['driver']);
87
		}
88
		elseif ($driver = config_item('sess_driver'))
89
		{
90
			$this->_driver = $driver;
91
		}
92
		// Note: BC workaround
93
		elseif (config_item('sess_use_database'))
94
		{
95
			log_message('debug', 'Session: "sess_driver" is empty; using BC fallback to "sess_use_database".');
96
			$this->_driver = 'database';
97
		}
98
 
99
		$class = $this->_ci_load_classes($this->_driver);
100
 
101
		// Configuration ...
102
		$this->_configure($params);
1257 lars 103
		$this->_config['_sid_regexp'] = $this->_sid_regexp;
68 lars 104
 
105
		$class = new $class($this->_config);
106
		if ($class instanceof SessionHandlerInterface)
107
		{
108
			if (is_php('5.4'))
109
			{
110
				session_set_save_handler($class, TRUE);
111
			}
112
			else
113
			{
114
				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
 
132
		// 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']])
1257 lars 136
				OR ! preg_match('#\A'.$this->_sid_regexp.'\z#', $_COOKIE[$this->_config['cookie_name']])
68 lars 137
			)
138
		)
139
		{
140
			unset($_COOKIE[$this->_config['cookie_name']]);
141
		}
142
 
143
		session_start();
144
 
145
		// Is session ID auto-regeneration configured? (ignoring ajax requests)
146
		if ((empty($_SERVER['HTTP_X_REQUESTED_WITH']) OR strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
147
			&& ($regenerate_time = config_item('sess_time_to_update')) > 0
148
		)
149
		{
150
			if ( ! isset($_SESSION['__ci_last_regenerate']))
151
			{
152
				$_SESSION['__ci_last_regenerate'] = time();
153
			}
154
			elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time))
155
			{
156
				$this->sess_regenerate((bool) config_item('sess_regenerate_destroy'));
157
			}
158
		}
159
		// Another work-around ... PHP doesn't seem to send the session cookie
160
		// unless it is being currently created or regenerated
161
		elseif (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
162
		{
163
			setcookie(
164
				$this->_config['cookie_name'],
165
				session_id(),
166
				(empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']),
167
				$this->_config['cookie_path'],
168
				$this->_config['cookie_domain'],
169
				$this->_config['cookie_secure'],
170
				TRUE
171
			);
172
		}
173
 
174
		$this->_ci_init_vars();
175
 
176
		log_message('info', "Session: Class initialized using '".$this->_driver."' driver.");
177
	}
178
 
179
	// ------------------------------------------------------------------------
180
 
181
	/**
182
	 * CI Load Classes
183
	 *
184
	 * An internal method to load all possible dependency and extension
185
	 * classes. It kind of emulates the CI_Driver library, but is
186
	 * self-sufficient.
187
	 *
188
	 * @param	string	$driver	Driver name
189
	 * @return	string	Driver class name
190
	 */
191
	protected function _ci_load_classes($driver)
192
	{
193
		// PHP 5.4 compatibility
194
		interface_exists('SessionHandlerInterface', FALSE) OR require_once(BASEPATH.'libraries/Session/SessionHandlerInterface.php');
195
 
196
		$prefix = config_item('subclass_prefix');
197
 
198
		if ( ! class_exists('CI_Session_driver', FALSE))
199
		{
200
			require_once(
201
				file_exists(APPPATH.'libraries/Session/Session_driver.php')
202
					? APPPATH.'libraries/Session/Session_driver.php'
203
					: BASEPATH.'libraries/Session/Session_driver.php'
204
			);
205
 
206
			if (file_exists($file_path = APPPATH.'libraries/Session/'.$prefix.'Session_driver.php'))
207
			{
208
				require_once($file_path);
209
			}
210
		}
211
 
212
		$class = 'Session_'.$driver.'_driver';
213
 
214
		// Allow custom drivers without the CI_ or MY_ prefix
215
		if ( ! class_exists($class, FALSE) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php'))
216
		{
217
			require_once($file_path);
218
			if (class_exists($class, FALSE))
219
			{
220
				return $class;
221
			}
222
		}
223
 
224
		if ( ! class_exists('CI_'.$class, FALSE))
225
		{
226
			if (file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$class.'.php') OR file_exists($file_path = BASEPATH.'libraries/Session/drivers/'.$class.'.php'))
227
			{
228
				require_once($file_path);
229
			}
230
 
231
			if ( ! class_exists('CI_'.$class, FALSE) && ! class_exists($class, FALSE))
232
			{
233
				throw new UnexpectedValueException("Session: Configured driver '".$driver."' was not found. Aborting.");
234
			}
235
		}
236
 
237
		if ( ! class_exists($prefix.$class, FALSE) && file_exists($file_path = APPPATH.'libraries/Session/drivers/'.$prefix.$class.'.php'))
238
		{
239
			require_once($file_path);
240
			if (class_exists($prefix.$class, FALSE))
241
			{
242
				return $prefix.$class;
243
			}
2254 lars 244
			else
245
			{
246
				log_message('debug', 'Session: '.$prefix.$class.".php found but it doesn't declare class ".$prefix.$class.'.');
247
			}
68 lars 248
		}
249
 
250
		return 'CI_'.$class;
251
	}
252
 
253
	// ------------------------------------------------------------------------
254
 
255
	/**
256
	 * Configuration
257
	 *
258
	 * Handle input parameters and configuration defaults
259
	 *
260
	 * @param	array	&$params	Input parameters
261
	 * @return	void
262
	 */
263
	protected function _configure(&$params)
264
	{
265
		$expiration = config_item('sess_expiration');
266
 
267
		if (isset($params['cookie_lifetime']))
268
		{
269
			$params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
270
		}
271
		else
272
		{
273
			$params['cookie_lifetime'] = ( ! isset($expiration) && config_item('sess_expire_on_close'))
274
				? 0 : (int) $expiration;
275
		}
276
 
277
		isset($params['cookie_name']) OR $params['cookie_name'] = config_item('sess_cookie_name');
278
		if (empty($params['cookie_name']))
279
		{
280
			$params['cookie_name'] = ini_get('session.name');
281
		}
282
		else
283
		{
284
			ini_set('session.name', $params['cookie_name']);
285
		}
286
 
287
		isset($params['cookie_path']) OR $params['cookie_path'] = config_item('cookie_path');
288
		isset($params['cookie_domain']) OR $params['cookie_domain'] = config_item('cookie_domain');
289
		isset($params['cookie_secure']) OR $params['cookie_secure'] = (bool) config_item('cookie_secure');
290
 
291
		session_set_cookie_params(
292
			$params['cookie_lifetime'],
293
			$params['cookie_path'],
294
			$params['cookie_domain'],
295
			$params['cookie_secure'],
296
			TRUE // HttpOnly; Yes, this is intentional and not configurable for security reasons
297
		);
298
 
299
		if (empty($expiration))
300
		{
301
			$params['expiration'] = (int) ini_get('session.gc_maxlifetime');
302
		}
303
		else
304
		{
305
			$params['expiration'] = (int) $expiration;
306
			ini_set('session.gc_maxlifetime', $expiration);
307
		}
308
 
309
		$params['match_ip'] = (bool) (isset($params['match_ip']) ? $params['match_ip'] : config_item('sess_match_ip'));
310
 
311
		isset($params['save_path']) OR $params['save_path'] = config_item('sess_save_path');
312
 
313
		$this->_config = $params;
314
 
315
		// Security is king
316
		ini_set('session.use_trans_sid', 0);
317
		ini_set('session.use_strict_mode', 1);
318
		ini_set('session.use_cookies', 1);
319
		ini_set('session.use_only_cookies', 1);
1257 lars 320
 
321
		$this->_configure_sid_length();
68 lars 322
	}
323
 
324
	// ------------------------------------------------------------------------
325
 
326
	/**
1257 lars 327
	 * Configure session ID length
328
	 *
329
	 * To make life easier, we used to force SHA-1 and 4 bits per
330
	 * character on everyone. And of course, someone was unhappy.
331
	 *
332
	 * Then PHP 7.1 broke backwards-compatibility because ext/session
333
	 * is such a mess that nobody wants to touch it with a pole stick,
334
	 * and the one guy who does, nobody has the energy to argue with.
335
	 *
336
	 * So we were forced to make changes, and OF COURSE something was
337
	 * going to break and now we have this pile of shit. -- Narf
338
	 *
339
	 * @return	void
340
	 */
341
	protected function _configure_sid_length()
342
	{
343
		if (PHP_VERSION_ID < 70100)
344
		{
345
			$hash_function = ini_get('session.hash_function');
346
			if (ctype_digit($hash_function))
347
			{
348
				if ($hash_function !== '1')
349
				{
350
					ini_set('session.hash_function', 1);
351
				}
352
 
353
				$bits = 160;
354
			}
355
			elseif ( ! in_array($hash_function, hash_algos(), TRUE))
356
			{
357
				ini_set('session.hash_function', 1);
358
				$bits = 160;
359
			}
360
			elseif (($bits = strlen(hash($hash_function, 'dummy', false)) * 4) < 160)
361
			{
362
				ini_set('session.hash_function', 1);
363
				$bits = 160;
364
			}
365
 
366
			$bits_per_character = (int) ini_get('session.hash_bits_per_character');
367
			$sid_length         = (int) ceil($bits / $bits_per_character);
368
		}
369
		else
370
		{
371
			$bits_per_character = (int) ini_get('session.sid_bits_per_character');
372
			$sid_length         = (int) ini_get('session.sid_length');
373
			if (($bits = $sid_length * $bits_per_character) < 160)
374
			{
375
				// Add as many more characters as necessary to reach at least 160 bits
376
				$sid_length += (int) ceil((160 % $bits) / $bits_per_character);
377
				ini_set('session.sid_length', $sid_length);
378
			}
379
		}
380
 
381
		// Yes, 4,5,6 are the only known possible values as of 2016-10-27
382
		switch ($bits_per_character)
383
		{
384
			case 4:
385
				$this->_sid_regexp = '[0-9a-f]';
386
				break;
387
			case 5:
388
				$this->_sid_regexp = '[0-9a-v]';
389
				break;
390
			case 6:
391
				$this->_sid_regexp = '[0-9a-zA-Z,-]';
392
				break;
393
		}
394
 
395
		$this->_sid_regexp .= '{'.$sid_length.'}';
396
	}
397
 
398
	// ------------------------------------------------------------------------
399
 
400
	/**
68 lars 401
	 * Handle temporary variables
402
	 *
403
	 * Clears old "flash" data, marks the new one for deletion and handles
404
	 * "temp" data deletion.
405
	 *
406
	 * @return	void
407
	 */
408
	protected function _ci_init_vars()
409
	{
410
		if ( ! empty($_SESSION['__ci_vars']))
411
		{
412
			$current_time = time();
413
 
414
			foreach ($_SESSION['__ci_vars'] as $key => &$value)
415
			{
416
				if ($value === 'new')
417
				{
418
					$_SESSION['__ci_vars'][$key] = 'old';
419
				}
420
				// Hacky, but 'old' will (implicitly) always be less than time() ;)
421
				// DO NOT move this above the 'new' check!
422
				elseif ($value < $current_time)
423
				{
424
					unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
425
				}
426
			}
427
 
428
			if (empty($_SESSION['__ci_vars']))
429
			{
430
				unset($_SESSION['__ci_vars']);
431
			}
432
		}
433
 
434
		$this->userdata =& $_SESSION;
435
	}
436
 
437
	// ------------------------------------------------------------------------
438
 
439
	/**
440
	 * Mark as flash
441
	 *
442
	 * @param	mixed	$key	Session data key(s)
443
	 * @return	bool
444
	 */
445
	public function mark_as_flash($key)
446
	{
447
		if (is_array($key))
448
		{
449
			for ($i = 0, $c = count($key); $i < $c; $i++)
450
			{
451
				if ( ! isset($_SESSION[$key[$i]]))
452
				{
453
					return FALSE;
454
				}
455
			}
456
 
457
			$new = array_fill_keys($key, 'new');
458
 
459
			$_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
460
				? array_merge($_SESSION['__ci_vars'], $new)
461
				: $new;
462
 
463
			return TRUE;
464
		}
465
 
466
		if ( ! isset($_SESSION[$key]))
467
		{
468
			return FALSE;
469
		}
470
 
471
		$_SESSION['__ci_vars'][$key] = 'new';
472
		return TRUE;
473
	}
474
 
475
	// ------------------------------------------------------------------------
476
 
477
	/**
478
	 * Get flash keys
479
	 *
480
	 * @return	array
481
	 */
482
	public function get_flash_keys()
483
	{
484
		if ( ! isset($_SESSION['__ci_vars']))
485
		{
486
			return array();
487
		}
488
 
489
		$keys = array();
490
		foreach (array_keys($_SESSION['__ci_vars']) as $key)
491
		{
492
			is_int($_SESSION['__ci_vars'][$key]) OR $keys[] = $key;
493
		}
494
 
495
		return $keys;
496
	}
497
 
498
	// ------------------------------------------------------------------------
499
 
500
	/**
501
	 * Unmark flash
502
	 *
503
	 * @param	mixed	$key	Session data key(s)
504
	 * @return	void
505
	 */
506
	public function unmark_flash($key)
507
	{
508
		if (empty($_SESSION['__ci_vars']))
509
		{
510
			return;
511
		}
512
 
513
		is_array($key) OR $key = array($key);
514
 
515
		foreach ($key as $k)
516
		{
517
			if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k]))
518
			{
519
				unset($_SESSION['__ci_vars'][$k]);
520
			}
521
		}
522
 
523
		if (empty($_SESSION['__ci_vars']))
524
		{
525
			unset($_SESSION['__ci_vars']);
526
		}
527
	}
528
 
529
	// ------------------------------------------------------------------------
530
 
531
	/**
532
	 * Mark as temp
533
	 *
534
	 * @param	mixed	$key	Session data key(s)
535
	 * @param	int	$ttl	Time-to-live in seconds
536
	 * @return	bool
537
	 */
538
	public function mark_as_temp($key, $ttl = 300)
539
	{
540
		$ttl += time();
541
 
542
		if (is_array($key))
543
		{
544
			$temp = array();
545
 
546
			foreach ($key as $k => $v)
547
			{
548
				// Do we have a key => ttl pair, or just a key?
549
				if (is_int($k))
550
				{
551
					$k = $v;
552
					$v = $ttl;
553
				}
554
				else
555
				{
556
					$v += time();
557
				}
558
 
559
				if ( ! isset($_SESSION[$k]))
560
				{
561
					return FALSE;
562
				}
563
 
564
				$temp[$k] = $v;
565
			}
566
 
567
			$_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars'])
568
				? array_merge($_SESSION['__ci_vars'], $temp)
569
				: $temp;
570
 
571
			return TRUE;
572
		}
573
 
574
		if ( ! isset($_SESSION[$key]))
575
		{
576
			return FALSE;
577
		}
578
 
579
		$_SESSION['__ci_vars'][$key] = $ttl;
580
		return TRUE;
581
	}
582
 
583
	// ------------------------------------------------------------------------
584
 
585
	/**
586
	 * Get temp keys
587
	 *
588
	 * @return	array
589
	 */
590
	public function get_temp_keys()
591
	{
592
		if ( ! isset($_SESSION['__ci_vars']))
593
		{
594
			return array();
595
		}
596
 
597
		$keys = array();
598
		foreach (array_keys($_SESSION['__ci_vars']) as $key)
599
		{
600
			is_int($_SESSION['__ci_vars'][$key]) && $keys[] = $key;
601
		}
602
 
603
		return $keys;
604
	}
605
 
606
	// ------------------------------------------------------------------------
607
 
608
	/**
2254 lars 609
	 * Unmark flash
68 lars 610
	 *
611
	 * @param	mixed	$key	Session data key(s)
612
	 * @return	void
613
	 */
614
	public function unmark_temp($key)
615
	{
616
		if (empty($_SESSION['__ci_vars']))
617
		{
618
			return;
619
		}
620
 
621
		is_array($key) OR $key = array($key);
622
 
623
		foreach ($key as $k)
624
		{
625
			if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k]))
626
			{
627
				unset($_SESSION['__ci_vars'][$k]);
628
			}
629
		}
630
 
631
		if (empty($_SESSION['__ci_vars']))
632
		{
633
			unset($_SESSION['__ci_vars']);
634
		}
635
	}
636
 
637
	// ------------------------------------------------------------------------
638
 
639
	/**
640
	 * __get()
641
	 *
642
	 * @param	string	$key	'session_id' or a session data key
643
	 * @return	mixed
644
	 */
645
	public function __get($key)
646
	{
647
		// Note: Keep this order the same, just in case somebody wants to
648
		//       use 'session_id' as a session data key, for whatever reason
649
		if (isset($_SESSION[$key]))
650
		{
651
			return $_SESSION[$key];
652
		}
653
		elseif ($key === 'session_id')
654
		{
655
			return session_id();
656
		}
657
 
658
		return NULL;
659
	}
660
 
661
	// ------------------------------------------------------------------------
662
 
663
	/**
664
	 * __isset()
665
	 *
666
	 * @param	string	$key	'session_id' or a session data key
667
	 * @return	bool
668
	 */
669
	public function __isset($key)
670
	{
671
		if ($key === 'session_id')
672
		{
673
			return (session_status() === PHP_SESSION_ACTIVE);
674
		}
675
 
676
		return isset($_SESSION[$key]);
677
	}
678
 
679
	// ------------------------------------------------------------------------
680
 
681
	/**
682
	 * __set()
683
	 *
684
	 * @param	string	$key	Session data key
685
	 * @param	mixed	$value	Session data value
686
	 * @return	void
687
	 */
688
	public function __set($key, $value)
689
	{
690
		$_SESSION[$key] = $value;
691
	}
692
 
693
	// ------------------------------------------------------------------------
694
 
695
	/**
696
	 * Session destroy
697
	 *
698
	 * Legacy CI_Session compatibility method
699
	 *
700
	 * @return	void
701
	 */
702
	public function sess_destroy()
703
	{
704
		session_destroy();
705
	}
706
 
707
	// ------------------------------------------------------------------------
708
 
709
	/**
710
	 * Session regenerate
711
	 *
712
	 * Legacy CI_Session compatibility method
713
	 *
714
	 * @param	bool	$destroy	Destroy old session data flag
715
	 * @return	void
716
	 */
717
	public function sess_regenerate($destroy = FALSE)
718
	{
719
		$_SESSION['__ci_last_regenerate'] = time();
720
		session_regenerate_id($destroy);
721
	}
722
 
723
	// ------------------------------------------------------------------------
724
 
725
	/**
726
	 * Get userdata reference
727
	 *
728
	 * Legacy CI_Session compatibility method
729
	 *
730
	 * @returns	array
731
	 */
732
	public function &get_userdata()
733
	{
734
		return $_SESSION;
735
	}
736
 
737
	// ------------------------------------------------------------------------
738
 
739
	/**
740
	 * Userdata (fetch)
741
	 *
742
	 * Legacy CI_Session compatibility method
743
	 *
744
	 * @param	string	$key	Session data key
745
	 * @return	mixed	Session data value or NULL if not found
746
	 */
747
	public function userdata($key = NULL)
748
	{
749
		if (isset($key))
750
		{
751
			return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
752
		}
753
		elseif (empty($_SESSION))
754
		{
755
			return array();
756
		}
757
 
758
		$userdata = array();
759
		$_exclude = array_merge(
760
			array('__ci_vars'),
761
			$this->get_flash_keys(),
762
			$this->get_temp_keys()
763
		);
764
 
765
		foreach (array_keys($_SESSION) as $key)
766
		{
767
			if ( ! in_array($key, $_exclude, TRUE))
768
			{
769
				$userdata[$key] = $_SESSION[$key];
770
			}
771
		}
772
 
773
		return $userdata;
774
	}
775
 
776
	// ------------------------------------------------------------------------
777
 
778
	/**
779
	 * Set userdata
780
	 *
781
	 * Legacy CI_Session compatibility method
782
	 *
783
	 * @param	mixed	$data	Session data key or an associative array
784
	 * @param	mixed	$value	Value to store
785
	 * @return	void
786
	 */
787
	public function set_userdata($data, $value = NULL)
788
	{
789
		if (is_array($data))
790
		{
791
			foreach ($data as $key => &$value)
792
			{
793
				$_SESSION[$key] = $value;
794
			}
795
 
796
			return;
797
		}
798
 
799
		$_SESSION[$data] = $value;
800
	}
801
 
802
	// ------------------------------------------------------------------------
803
 
804
	/**
805
	 * Unset userdata
806
	 *
807
	 * Legacy CI_Session compatibility method
808
	 *
809
	 * @param	mixed	$key	Session data key(s)
810
	 * @return	void
811
	 */
812
	public function unset_userdata($key)
813
	{
814
		if (is_array($key))
815
		{
816
			foreach ($key as $k)
817
			{
818
				unset($_SESSION[$k]);
819
			}
820
 
821
			return;
822
		}
823
 
824
		unset($_SESSION[$key]);
825
	}
826
 
827
	// ------------------------------------------------------------------------
828
 
829
	/**
830
	 * All userdata (fetch)
831
	 *
832
	 * Legacy CI_Session compatibility method
833
	 *
834
	 * @return	array	$_SESSION, excluding flash data items
835
	 */
836
	public function all_userdata()
837
	{
838
		return $this->userdata();
839
	}
840
 
841
	// ------------------------------------------------------------------------
842
 
843
	/**
844
	 * Has userdata
845
	 *
846
	 * Legacy CI_Session compatibility method
847
	 *
848
	 * @param	string	$key	Session data key
849
	 * @return	bool
850
	 */
851
	public function has_userdata($key)
852
	{
853
		return isset($_SESSION[$key]);
854
	}
855
 
856
	// ------------------------------------------------------------------------
857
 
858
	/**
859
	 * Flashdata (fetch)
860
	 *
861
	 * Legacy CI_Session compatibility method
862
	 *
863
	 * @param	string	$key	Session data key
864
	 * @return	mixed	Session data value or NULL if not found
865
	 */
866
	public function flashdata($key = NULL)
867
	{
868
		if (isset($key))
869
		{
870
			return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && ! is_int($_SESSION['__ci_vars'][$key]))
871
				? $_SESSION[$key]
872
				: NULL;
873
		}
874
 
875
		$flashdata = array();
876
 
877
		if ( ! empty($_SESSION['__ci_vars']))
878
		{
879
			foreach ($_SESSION['__ci_vars'] as $key => &$value)
880
			{
881
				is_int($value) OR $flashdata[$key] = $_SESSION[$key];
882
			}
883
		}
884
 
885
		return $flashdata;
886
	}
887
 
888
	// ------------------------------------------------------------------------
889
 
890
	/**
891
	 * Set flashdata
892
	 *
893
	 * Legacy CI_Session compatibility method
894
	 *
895
	 * @param	mixed	$data	Session data key or an associative array
896
	 * @param	mixed	$value	Value to store
897
	 * @return	void
898
	 */
899
	public function set_flashdata($data, $value = NULL)
900
	{
901
		$this->set_userdata($data, $value);
902
		$this->mark_as_flash(is_array($data) ? array_keys($data) : $data);
903
	}
904
 
905
	// ------------------------------------------------------------------------
906
 
907
	/**
908
	 * Keep flashdata
909
	 *
910
	 * Legacy CI_Session compatibility method
911
	 *
912
	 * @param	mixed	$key	Session data key(s)
913
	 * @return	void
914
	 */
915
	public function keep_flashdata($key)
916
	{
917
		$this->mark_as_flash($key);
918
	}
919
 
920
	// ------------------------------------------------------------------------
921
 
922
	/**
923
	 * Temp data (fetch)
924
	 *
925
	 * Legacy CI_Session compatibility method
926
	 *
927
	 * @param	string	$key	Session data key
928
	 * @return	mixed	Session data value or NULL if not found
929
	 */
930
	public function tempdata($key = NULL)
931
	{
932
		if (isset($key))
933
		{
934
			return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key]) && is_int($_SESSION['__ci_vars'][$key]))
935
				? $_SESSION[$key]
936
				: NULL;
937
		}
938
 
939
		$tempdata = array();
940
 
941
		if ( ! empty($_SESSION['__ci_vars']))
942
		{
943
			foreach ($_SESSION['__ci_vars'] as $key => &$value)
944
			{
945
				is_int($value) && $tempdata[$key] = $_SESSION[$key];
946
			}
947
		}
948
 
949
		return $tempdata;
950
	}
951
 
952
	// ------------------------------------------------------------------------
953
 
954
	/**
955
	 * Set tempdata
956
	 *
957
	 * Legacy CI_Session compatibility method
958
	 *
959
	 * @param	mixed	$data	Session data key or an associative array of items
960
	 * @param	mixed	$value	Value to store
961
	 * @param	int	$ttl	Time-to-live in seconds
962
	 * @return	void
963
	 */
964
	public function set_tempdata($data, $value = NULL, $ttl = 300)
965
	{
966
		$this->set_userdata($data, $value);
967
		$this->mark_as_temp(is_array($data) ? array_keys($data) : $data, $ttl);
968
	}
969
 
970
	// ------------------------------------------------------------------------
971
 
972
	/**
973
	 * Unset tempdata
974
	 *
975
	 * Legacy CI_Session compatibility method
976
	 *
977
	 * @param	mixed	$data	Session data key(s)
978
	 * @return	void
979
	 */
980
	public function unset_tempdata($key)
981
	{
982
		$this->unmark_temp($key);
983
	}
984
 
985
}