Subversion Repositories SmartDukaan

Rev

Rev 11419 | Rev 11540 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11537 lgm 1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
10582 lgm 2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP 5.1.6 or newer
6
 *
11537 lgm 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.
18
 *
10582 lgm 19
 * @package		CodeIgniter
11537 lgm 20
 * @author		EllisLab Dev Team
21
 * @copyright	Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
22
 * @license		http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
10582 lgm 23
 * @link		http://codeigniter.com
24
 * @since		Version 1.0
25
 * @filesource
26
 */
27
 
28
/**
29
 * Session Class
30
 *
31
 * @package		CodeIgniter
32
 * @subpackage	Libraries
33
 * @category	Sessions
11537 lgm 34
 * @author		EllisLab Dev Team
10582 lgm 35
 * @link		http://codeigniter.com/user_guide/libraries/sessions.html
36
 */
37
class CI_Session {
38
 
11537 lgm 39
	public $sess_encrypt_cookie		= FALSE;
40
	public $sess_use_database		= FALSE;
41
	public $sess_table_name			= '';
42
	public $sess_expiration			= 7200;
43
	public $sess_expire_on_close		= FALSE;
44
	public $sess_match_ip			= FALSE;
45
	public $sess_match_useragent		= TRUE;
46
	public $sess_cookie_name		= 'ci_session';
47
	public $cookie_prefix			= '';
48
	public $cookie_path			= '';
49
	public $cookie_domain			= '';
50
	public $cookie_secure			= FALSE;
51
	public $sess_time_to_update		= 300;
52
	public $encryption_key			= '';
53
	public $flashdata_key			= 'flash';
54
	public $time_reference			= 'time';
55
	public $gc_probability			= 5;
56
	public $userdata			= array();
57
	public $CI;
58
	public $now;
10582 lgm 59
 
60
	/**
61
	 * Session Constructor
62
	 *
63
	 * The constructor runs the session routines automatically
64
	 * whenever the class is instantiated.
65
	 */
66
	public function __construct($params = array())
67
	{
11537 lgm 68
		log_message('debug', 'Session Class Initialized');
10582 lgm 69
 
70
		// Set the super object to a local variable for use throughout the class
71
		$this->CI =& get_instance();
72
 
73
		// Set all the session preferences, which can either be set
74
		// manually via the $params array above or via the config file
75
		foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'cookie_secure', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key)
76
		{
77
			$this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
78
		}
79
 
80
		if ($this->encryption_key == '')
81
		{
82
			show_error('In order to use the Session class you are required to set an encryption key in your config file.');
83
		}
84
 
85
		// Load the string helper so we can use the strip_slashes() function
86
		$this->CI->load->helper('string');
87
 
88
		// Do we need encryption? If so, load the encryption class
89
		if ($this->sess_encrypt_cookie == TRUE)
90
		{
91
			$this->CI->load->library('encrypt');
92
		}
93
 
11537 lgm 94
		// Are we using a database? If so, load it
95
		if ($this->sess_use_database === TRUE && $this->sess_table_name != '')
10582 lgm 96
		{
97
			$this->CI->load->database();
98
		}
99
 
11537 lgm 100
		// Set the "now" time. Can either be GMT or server time, based on the
101
		// config prefs. We use this to set the "last activity" time
10582 lgm 102
		$this->now = $this->_get_time();
103
 
104
		// Set the session length. If the session expiration is
105
		// set to zero we'll set the expiration two years from now.
106
		if ($this->sess_expiration == 0)
107
		{
108
			$this->sess_expiration = (60*60*24*365*2);
109
		}
110
 
111
		// Set the cookie name
112
		$this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
113
 
114
		// Run the Session routine. If a session doesn't exist we'll
11537 lgm 115
		// create a new one. If it does, we'll update it.
10582 lgm 116
		if ( ! $this->sess_read())
117
		{
118
			$this->sess_create();
119
		}
120
		else
121
		{
122
			$this->sess_update();
123
		}
124
 
125
		// Delete 'old' flashdata (from last request)
126
		$this->_flashdata_sweep();
127
 
128
		// Mark all new flashdata as old (data will be deleted before next request)
129
		$this->_flashdata_mark();
130
 
131
		// Delete expired sessions if necessary
132
		$this->_sess_gc();
133
 
11537 lgm 134
		log_message('debug', 'Session routines successfully run');
10582 lgm 135
	}
136
 
137
	// --------------------------------------------------------------------
138
 
139
	/**
140
	 * Fetch the current session data if it exists
141
	 *
142
	 * @return	bool
143
	 */
11537 lgm 144
	public function sess_read()
10582 lgm 145
	{
146
		// Fetch the cookie
147
		$session = $this->CI->input->cookie($this->sess_cookie_name);
148
 
149
		// No cookie?  Goodbye cruel world!...
150
		if ($session === FALSE)
151
		{
152
			log_message('debug', 'A session cookie was not found.');
153
			return FALSE;
154
		}
155
 
156
		// Decrypt the cookie data
157
		if ($this->sess_encrypt_cookie == TRUE)
158
		{
159
			$session = $this->CI->encrypt->decode($session);
160
		}
161
		else
162
		{
163
			// encryption was not used, so we need to check the md5 hash
164
			$hash	 = substr($session, strlen($session)-32); // get last 32 chars
165
			$session = substr($session, 0, strlen($session)-32);
166
 
11537 lgm 167
			// Does the md5 hash match? This is to prevent manipulation of session data in userspace
10582 lgm 168
			if ($hash !==  md5($session.$this->encryption_key))
169
			{
170
				log_message('error', 'The session cookie data did not match what was expected. This could be a possible hacking attempt.');
171
				$this->sess_destroy();
172
				return FALSE;
173
			}
174
		}
175
 
176
		// Unserialize the session array
177
		$session = $this->_unserialize($session);
178
 
179
		// Is the session data we unserialized an array with the correct format?
11537 lgm 180
		if ( ! is_array($session) OR ! isset($session['session_id'], $session['ip_address'], $session['user_agent'], $session['last_activity']))
10582 lgm 181
		{
11537 lgm 182
			// $this->sess_destroy();
183
			// return FALSE;
10582 lgm 184
		}
185
 
186
		// Is the session current?
187
		if (($session['last_activity'] + $this->sess_expiration) < $this->now)
188
		{
189
			$this->sess_destroy();
190
			return FALSE;
191
		}
192
 
11537 lgm 193
		// Does the IP match?
194
		if ($this->sess_match_ip == TRUE && $session['ip_address'] !== $this->CI->input->ip_address())
10582 lgm 195
		{
11537 lgm 196
			// $this->sess_destroy();
197
			// return FALSE;
10582 lgm 198
		}
199
 
200
		// Does the User Agent Match?
11537 lgm 201
		if ($this->sess_match_useragent == TRUE && trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120)))
10582 lgm 202
		{
11537 lgm 203
			// $this->sess_destroy();
204
			// return FALSE;
10582 lgm 205
		}
206
 
207
		// Is there a corresponding session in the DB?
208
		if ($this->sess_use_database === TRUE)
209
		{
210
			$this->CI->db->where('session_id', $session['session_id']);
211
 
212
			if ($this->sess_match_ip == TRUE)
213
			{
214
				$this->CI->db->where('ip_address', $session['ip_address']);
215
			}
216
 
217
			if ($this->sess_match_useragent == TRUE)
218
			{
219
				$this->CI->db->where('user_agent', $session['user_agent']);
220
			}
221
 
222
			$query = $this->CI->db->get($this->sess_table_name);
223
 
11537 lgm 224
			// No result? Kill it!
225
			if ($query->num_rows() === 0)
10582 lgm 226
			{
11537 lgm 227
				$this->sess_destroy();
228
				return FALSE;
10582 lgm 229
			}
230
 
231
			// Is there custom data?  If so, add it to the main session array
232
			$row = $query->row();
11537 lgm 233
			if (isset($row->user_data) && $row->user_data != '')
10582 lgm 234
			{
235
				$custom_data = $this->_unserialize($row->user_data);
236
 
237
				if (is_array($custom_data))
238
				{
239
					foreach ($custom_data as $key => $val)
240
					{
241
						$session[$key] = $val;
242
					}
243
				}
244
			}
245
		}
246
 
247
		// Session is valid!
248
		$this->userdata = $session;
249
		unset($session);
250
 
251
		return TRUE;
252
	}
253
 
254
	// --------------------------------------------------------------------
255
 
256
	/**
257
	 * Write the session data
258
	 *
259
	 * @return	void
260
	 */
11537 lgm 261
	public function sess_write()
10582 lgm 262
	{
263
		// Are we saving custom data to the DB?  If not, all we do is update the cookie
264
		if ($this->sess_use_database === FALSE)
265
		{
266
			$this->_set_cookie();
267
			return;
268
		}
269
 
270
		// set the custom userdata, the session data we will set in a second
271
		$custom_userdata = $this->userdata;
272
		$cookie_userdata = array();
273
 
274
		// Before continuing, we need to determine if there is any custom data to deal with.
275
		// Let's determine this by removing the default indexes to see if there's anything left in the array
276
		// and set the session data while we're at it
277
		foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
278
		{
279
			unset($custom_userdata[$val]);
280
			$cookie_userdata[$val] = $this->userdata[$val];
281
		}
282
 
11537 lgm 283
		// Did we find any custom data? If not, we turn the empty array into a string
10582 lgm 284
		// since there's no reason to serialize and store an empty array in the DB
285
		if (count($custom_userdata) === 0)
286
		{
287
			$custom_userdata = '';
288
		}
289
		else
290
		{
291
			// Serialize the custom data array so we can store it
292
			$custom_userdata = $this->_serialize($custom_userdata);
293
		}
294
 
295
		// Run the update query
296
		$this->CI->db->where('session_id', $this->userdata['session_id']);
297
		$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
298
 
11537 lgm 299
		// Write the cookie. Notice that we manually pass the cookie data array to the
10582 lgm 300
		// _set_cookie() function. Normally that function will store $this->userdata, but
301
		// in this case that array contains custom data, which we do not want in the cookie.
302
		$this->_set_cookie($cookie_userdata);
303
	}
304
 
305
	// --------------------------------------------------------------------
306
 
307
	/**
308
	 * Create a new session
309
	 *
310
	 * @return	void
311
	 */
11537 lgm 312
	public function sess_create()
10582 lgm 313
	{
314
		$sessid = '';
11537 lgm 315
		do
10582 lgm 316
		{
317
			$sessid .= mt_rand(0, mt_getrandmax());
318
		}
11537 lgm 319
		while (strlen($sessid) < 32);
10582 lgm 320
 
321
		// To make the session ID even more secure we'll combine it with the user's IP
322
		$sessid .= $this->CI->input->ip_address();
323
 
324
		$this->userdata = array(
11537 lgm 325
					'session_id'	=> md5(uniqid($sessid, TRUE)),
326
					'ip_address'	=> $this->CI->input->ip_address(),
327
					'user_agent'	=> substr($this->CI->input->user_agent(), 0, 120),
328
					'last_activity'	=> $this->now,
329
					'user_data'	=> ''
330
				);
10582 lgm 331
 
332
		// Save the data to the DB if needed
333
		if ($this->sess_use_database === TRUE)
334
		{
335
			$this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
336
		}
337
 
338
		// Write the cookie
339
		$this->_set_cookie();
340
	}
341
 
342
	// --------------------------------------------------------------------
343
 
344
	/**
345
	 * Update an existing session
346
	 *
347
	 * @return	void
348
	 */
11537 lgm 349
	public function sess_update()
10582 lgm 350
	{
351
		// We only update the session every five minutes by default
352
		if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
353
		{
354
			return;
355
		}
356
 
11537 lgm 357
		// _set_cookie() will handle this for us if we aren't using database sessions
358
		// by pushing all userdata to the cookie.
359
		$cookie_data = NULL;
360
 
361
		/* Changing the session ID during an AJAX call causes problems,
362
		 * so we'll only update our last_activity
363
		 */
364
		if ($this->CI->input->is_ajax_request())
365
		{
366
			$this->userdata['last_activity'] = $this->now;
367
 
368
			// Update the session ID and last_activity field in the DB if needed
369
			if ($this->sess_use_database === TRUE)
370
			{
371
				// set cookie explicitly to only have our session data
372
				$cookie_data = array();
373
				foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
374
				{
375
					$cookie_data[$val] = $this->userdata[$val];
376
				}
377
 
378
				$this->CI->db->query($this->CI->db->update_string($this->sess_table_name,
379
											array('last_activity' => $this->userdata['last_activity']),
380
											array('session_id' => $this->userdata['session_id'])));
381
			}
382
 
383
			return $this->_set_cookie($cookie_data);
384
		}
385
 
10582 lgm 386
		// Save the old session id so we know which record to
387
		// update in the database if we need it
388
		$old_sessid = $this->userdata['session_id'];
389
		$new_sessid = '';
11537 lgm 390
		do
10582 lgm 391
		{
392
			$new_sessid .= mt_rand(0, mt_getrandmax());
393
		}
11537 lgm 394
		while (strlen($new_sessid) < 32);
10582 lgm 395
 
396
		// To make the session ID even more secure we'll combine it with the user's IP
397
		$new_sessid .= $this->CI->input->ip_address();
398
 
11537 lgm 399
		// Turn it into a hash and update the session data array
400
		$this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE));
10582 lgm 401
		$this->userdata['last_activity'] = $this->now;
402
 
403
		// Update the session ID and last_activity field in the DB if needed
404
		if ($this->sess_use_database === TRUE)
405
		{
406
			// set cookie explicitly to only have our session data
407
			$cookie_data = array();
408
			foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
409
			{
410
				$cookie_data[$val] = $this->userdata[$val];
411
			}
412
 
413
			$this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid)));
414
		}
415
 
416
		// Write the cookie
417
		$this->_set_cookie($cookie_data);
418
	}
419
 
420
	// --------------------------------------------------------------------
421
 
422
	/**
423
	 * Destroy the current session
424
	 *
425
	 * @return	void
426
	 */
11537 lgm 427
	public function sess_destroy()
10582 lgm 428
	{
429
		// Kill the session DB row
430
		if ($this->sess_use_database === TRUE && isset($this->userdata['session_id']))
431
		{
432
			$this->CI->db->where('session_id', $this->userdata['session_id']);
433
			$this->CI->db->delete($this->sess_table_name);
434
		}
435
 
436
		// Kill the cookie
437
		setcookie(
11537 lgm 438
				$this->sess_cookie_name,
439
				addslashes(serialize(array())),
440
				($this->now - 31500000),
441
				$this->cookie_path,
442
				$this->cookie_domain,
443
 
444
			);
10582 lgm 445
	}
446
 
447
	// --------------------------------------------------------------------
448
 
449
	/**
450
	 * Fetch a specific item from the session array
451
	 *
452
	 * @param	string
453
	 * @return	string
454
	 */
11537 lgm 455
	public function userdata($item)
10582 lgm 456
	{
457
		return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item];
458
	}
459
 
460
	// --------------------------------------------------------------------
461
 
462
	/**
463
	 * Fetch all session data
464
	 *
465
	 * @return	array
466
	 */
11537 lgm 467
	public function all_userdata()
10582 lgm 468
	{
469
		return $this->userdata;
470
	}
471
 
472
	// --------------------------------------------------------------------
473
 
474
	/**
475
	 * Add or change data in the "userdata" array
476
	 *
477
	 * @param	mixed
478
	 * @param	string
479
	 * @return	void
480
	 */
11537 lgm 481
	public function set_userdata($newdata = array(), $newval = '')
10582 lgm 482
	{
483
		if (is_string($newdata))
484
		{
485
			$newdata = array($newdata => $newval);
486
		}
487
 
488
		if (count($newdata) > 0)
489
		{
490
			foreach ($newdata as $key => $val)
491
			{
492
				$this->userdata[$key] = $val;
493
			}
494
		}
495
 
496
		$this->sess_write();
497
	}
498
 
499
	// --------------------------------------------------------------------
500
 
501
	/**
502
	 * Delete a session variable from the "userdata" array
503
	 *
504
	 * @return	void
505
	 */
11537 lgm 506
	public function unset_userdata($newdata = array())
10582 lgm 507
	{
508
		if (is_string($newdata))
509
		{
510
			$newdata = array($newdata => '');
511
		}
512
 
513
		if (count($newdata) > 0)
514
		{
515
			foreach ($newdata as $key => $val)
516
			{
517
				unset($this->userdata[$key]);
518
			}
519
		}
520
 
521
		$this->sess_write();
522
	}
523
 
524
	// ------------------------------------------------------------------------
525
 
526
	/**
527
	 * Add or change flashdata, only available
528
	 * until the next request
529
	 *
530
	 * @param	mixed
531
	 * @param	string
532
	 * @return	void
533
	 */
11537 lgm 534
	public function set_flashdata($newdata = array(), $newval = '')
10582 lgm 535
	{
536
		if (is_string($newdata))
537
		{
538
			$newdata = array($newdata => $newval);
539
		}
540
 
541
		if (count($newdata) > 0)
542
		{
543
			foreach ($newdata as $key => $val)
544
			{
11537 lgm 545
				$this->set_userdata($this->flashdata_key.':new:'.$key, $val);
10582 lgm 546
			}
547
		}
548
	}
549
 
550
	// ------------------------------------------------------------------------
551
 
552
	/**
553
	 * Keeps existing flashdata available to next request.
554
	 *
555
	 * @param	string
556
	 * @return	void
557
	 */
11537 lgm 558
	public function keep_flashdata($key)
10582 lgm 559
	{
11537 lgm 560
		// 'old' flashdata gets removed. Here we mark all
10582 lgm 561
		// flashdata as 'new' to preserve it from _flashdata_sweep()
562
		// Note the function will return FALSE if the $key
563
		// provided cannot be found
11537 lgm 564
		$value = $this->userdata($this->flashdata_key.':old:'.$key);
10582 lgm 565
 
11537 lgm 566
		$this->set_userdata($this->flashdata_key.':new:'.$key, $value);
10582 lgm 567
	}
568
 
569
	// ------------------------------------------------------------------------
570
 
571
	/**
572
	 * Fetch a specific flashdata item from the session array
573
	 *
574
	 * @param	string
575
	 * @return	string
576
	 */
11537 lgm 577
	public function flashdata($key)
10582 lgm 578
	{
11537 lgm 579
		return $this->userdata($this->flashdata_key.':old:'.$key);
10582 lgm 580
	}
581
 
582
	// ------------------------------------------------------------------------
583
 
584
	/**
585
	 * Identifies flashdata as 'old' for removal
586
	 * when _flashdata_sweep() runs.
587
	 *
588
	 * @return	void
589
	 */
11537 lgm 590
	protected function _flashdata_mark()
10582 lgm 591
	{
592
		$userdata = $this->all_userdata();
593
		foreach ($userdata as $name => $value)
594
		{
595
			$parts = explode(':new:', $name);
596
			if (is_array($parts) && count($parts) === 2)
597
			{
11537 lgm 598
				$this->set_userdata($this->flashdata_key.':old:'.$parts[1], $value);
10582 lgm 599
				$this->unset_userdata($name);
600
			}
601
		}
602
	}
603
 
604
	// ------------------------------------------------------------------------
605
 
606
	/**
607
	 * Removes all flashdata marked as 'old'
608
	 *
609
	 * @return	void
610
	 */
11537 lgm 611
	protected function _flashdata_sweep()
10582 lgm 612
	{
613
		$userdata = $this->all_userdata();
614
		foreach ($userdata as $key => $value)
615
		{
616
			if (strpos($key, ':old:'))
617
			{
618
				$this->unset_userdata($key);
619
			}
620
		}
621
 
622
	}
623
 
624
	// --------------------------------------------------------------------
625
 
626
	/**
627
	 * Get the "now" time
628
	 *
629
	 * @return	string
630
	 */
11537 lgm 631
	protected function _get_time()
10582 lgm 632
	{
11537 lgm 633
		return (strtolower($this->time_reference) === 'gmt')
634
			? mktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y'))
635
			: time();
10582 lgm 636
	}
637
 
638
	// --------------------------------------------------------------------
639
 
640
	/**
641
	 * Write the session cookie
642
	 *
643
	 * @return	void
644
	 */
11537 lgm 645
	protected function _set_cookie($cookie_data = NULL)
10582 lgm 646
	{
647
		if (is_null($cookie_data))
648
		{
649
			$cookie_data = $this->userdata;
650
		}
651
 
652
		// Serialize the userdata for the cookie
653
		$cookie_data = $this->_serialize($cookie_data);
654
 
655
		if ($this->sess_encrypt_cookie == TRUE)
656
		{
657
			$cookie_data = $this->CI->encrypt->encode($cookie_data);
658
		}
659
		else
660
		{
661
			// if encryption is not used, we provide an md5 hash to prevent userside tampering
662
			$cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
663
		}
664
 
665
		$expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
666
 
667
		// Set the cookie
668
		setcookie(
11537 lgm 669
				$this->sess_cookie_name,
670
				$cookie_data,
671
				$expire,
672
				$this->cookie_path,
673
				$this->cookie_domain,
674
				$this->cookie_secure
675
			);
10582 lgm 676
	}
677
 
678
	// --------------------------------------------------------------------
679
 
680
	/**
681
	 * Serialize an array
682
	 *
683
	 * This function first converts any slashes found in the array to a temporary
684
	 * marker, so when it gets unserialized the slashes will be preserved
685
	 *
686
	 * @param	array
687
	 * @return	string
688
	 */
11537 lgm 689
	protected function _serialize($data)
10582 lgm 690
	{
691
		if (is_array($data))
692
		{
11537 lgm 693
			array_walk_recursive($data, array(&$this, '_escape_slashes'));
10582 lgm 694
		}
11537 lgm 695
		elseif (is_string($data))
10582 lgm 696
		{
11537 lgm 697
			$data = str_replace('\\', '{{slash}}', $data);
10582 lgm 698
		}
699
		return serialize($data);
700
	}
701
 
11537 lgm 702
	/**
703
	 * Escape slashes
704
	 *
705
	 * This function converts any slashes found into a temporary marker
706
	 *
707
	 * @param	string
708
	 * @param	string
709
	 * @return	void
710
	 */
711
	protected function _escape_slashes(&$val, $key)
712
	{
713
		if (is_string($val))
714
		{
715
			$val = str_replace('\\', '{{slash}}', $val);
716
		}
717
	}
718
 
10582 lgm 719
	// --------------------------------------------------------------------
720
 
721
	/**
722
	 * Unserialize
723
	 *
724
	 * This function unserializes a data string, then converts any
725
	 * temporary slash markers back to actual slashes
726
	 *
727
	 * @param	array
728
	 * @return	string
729
	 */
11537 lgm 730
	protected function _unserialize($data)
10582 lgm 731
	{
732
		$data = @unserialize(strip_slashes($data));
733
 
734
		if (is_array($data))
735
		{
11537 lgm 736
			array_walk_recursive($data, array(&$this, '_unescape_slashes'));
10582 lgm 737
			return $data;
738
		}
739
 
740
		return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data;
741
	}
742
 
11537 lgm 743
	/**
744
	 * Unescape slashes
745
	 *
746
	 * This function converts any slash markers back into actual slashes
747
	 *
748
	 * @param	string
749
	 * @param	string
750
	 * @return	void
751
	 */
752
	protected function _unescape_slashes(&$val, $key)
753
	{
754
		if (is_string($val))
755
		{
756
	 		$val= str_replace('{{slash}}', '\\', $val);
757
		}
758
	}
759
 
10582 lgm 760
	// --------------------------------------------------------------------
761
 
762
	/**
763
	 * Garbage collection
764
	 *
765
	 * This deletes expired session rows from database
766
	 * if the probability percentage is met
767
	 *
768
	 * @return	void
769
	 */
11537 lgm 770
	protected function _sess_gc()
10582 lgm 771
	{
772
		if ($this->sess_use_database != TRUE)
773
		{
774
			return;
775
		}
776
 
777
		srand(time());
778
		if ((rand() % 100) < $this->gc_probability)
779
		{
780
			$expire = $this->now - $this->sess_expiration;
781
 
782
			$this->CI->db->where("last_activity < {$expire}");
783
			$this->CI->db->delete($this->sess_table_name);
784
 
785
			log_message('debug', 'Session garbage collection performed.');
786
		}
787
	}
788
 
789
}
790
 
791
/* End of file Session.php */
792
/* Location: ./system/libraries/Session.php */