blob: c4f97e965d2eb03ed27f2c12d819ede1822b3955 [file] [log] [blame]
Andrey Andreev57ffbbb2011-12-25 04:48:47 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev57ffbbb2011-12-25 04:48:47 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreeveea2ff52012-01-19 13:21:53 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Session Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Sessions
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/sessions.html
38 */
39class CI_Session {
40
Andrey Andreev57ffbbb2011-12-25 04:48:47 +020041 public $sess_encrypt_cookie = FALSE;
42 public $sess_use_database = FALSE;
43 public $sess_table_name = '';
44 public $sess_expiration = 7200;
45 public $sess_expire_on_close = FALSE;
46 public $sess_match_ip = FALSE;
47 public $sess_match_useragent = TRUE;
48 public $sess_cookie_name = 'ci_session';
49 public $cookie_prefix = '';
50 public $cookie_path = '';
51 public $cookie_domain = '';
52 public $cookie_secure = FALSE;
53 public $sess_time_to_update = 300;
54 public $encryption_key = '';
55 public $flashdata_key = 'flash';
56 public $time_reference = 'time';
57 public $gc_probability = 5;
58 public $userdata = array();
59 public $CI;
60 public $now;
Derek Allard2067d1a2008-11-13 22:59:24 +000061
62 /**
63 * Session Constructor
64 *
65 * The constructor runs the session routines automatically
66 * whenever the class is instantiated.
67 */
Greg Akera9263282010-11-10 15:26:43 -060068 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
Andrey Andreeveea2ff52012-01-19 13:21:53 +020070 log_message('debug', 'Session Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000071
72 // Set the super object to a local variable for use throughout the class
73 $this->CI =& get_instance();
74
75 // Set all the session preferences, which can either be set
76 // manually via the $params array above or via the config file
tobiasbgba6432c2011-02-18 21:58:48 +010077 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)
Derek Allard2067d1a2008-11-13 22:59:24 +000078 {
79 $this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);
80 }
81
Derek Jones5485db52010-08-30 21:31:08 -050082 if ($this->encryption_key == '')
83 {
84 show_error('In order to use the Session class you are required to set an encryption key in your config file.');
85 }
86
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // Load the string helper so we can use the strip_slashes() function
88 $this->CI->load->helper('string');
89
90 // Do we need encryption? If so, load the encryption class
91 if ($this->sess_encrypt_cookie == TRUE)
92 {
93 $this->CI->load->library('encrypt');
94 }
95
Andrey Andreeveea2ff52012-01-19 13:21:53 +020096 // Are we using a database? If so, load it
Derek Allard2067d1a2008-11-13 22:59:24 +000097 if ($this->sess_use_database === TRUE AND $this->sess_table_name != '')
98 {
99 $this->CI->load->database();
100 }
101
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200102 // Set the "now" time. Can either be GMT or server time, based on the
103 // config prefs. We use this to set the "last activity" time
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 $this->now = $this->_get_time();
105
106 // Set the session length. If the session expiration is
107 // set to zero we'll set the expiration two years from now.
108 if ($this->sess_expiration == 0)
109 {
110 $this->sess_expiration = (60*60*24*365*2);
111 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 // Set the cookie name
114 $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name;
115
116 // Run the Session routine. If a session doesn't exist we'll
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200117 // create a new one. If it does, we'll update it.
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 if ( ! $this->sess_read())
119 {
120 $this->sess_create();
121 }
122 else
123 {
124 $this->sess_update();
125 }
126
127 // Delete 'old' flashdata (from last request)
Barry Mienydd671972010-10-04 16:33:58 +0200128 $this->_flashdata_sweep();
Derek Allard2067d1a2008-11-13 22:59:24 +0000129
130 // Mark all new flashdata as old (data will be deleted before next request)
Barry Mienydd671972010-10-04 16:33:58 +0200131 $this->_flashdata_mark();
Derek Allard2067d1a2008-11-13 22:59:24 +0000132
133 // Delete expired sessions if necessary
134 $this->_sess_gc();
135
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200136 log_message('debug', 'Session routines successfully run');
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Fetch the current session data if it exists
143 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * @return bool
145 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200146 public function sess_read()
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 // Fetch the cookie
149 $session = $this->CI->input->cookie($this->sess_cookie_name);
150
Derek Jones4b9c6292011-07-01 17:40:48 -0500151 // No cookie? Goodbye cruel world!...
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 if ($session === FALSE)
153 {
154 log_message('debug', 'A session cookie was not found.');
155 return FALSE;
156 }
157
158 // Decrypt the cookie data
159 if ($this->sess_encrypt_cookie == TRUE)
160 {
161 $session = $this->CI->encrypt->decode($session);
162 }
163 else
164 {
165 // encryption was not used, so we need to check the md5 hash
166 $hash = substr($session, strlen($session)-32); // get last 32 chars
167 $session = substr($session, 0, strlen($session)-32);
168
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200169 // Does the md5 hash match? This is to prevent manipulation of session data in userspace
Derek Jones4b9c6292011-07-01 17:40:48 -0500170 if ($hash !== md5($session.$this->encryption_key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
172 log_message('error', 'The session cookie data did not match what was expected. This could be a possible hacking attempt.');
173 $this->sess_destroy();
174 return FALSE;
175 }
176 }
177
178 // Unserialize the session array
179 $session = $this->_unserialize($session);
180
181 // Is the session data we unserialized an array with the correct format?
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200182 if ( ! is_array($session) OR ! isset($session['session_id'], $session['ip_address'], $session['user_agent'], $session['last_activity'])
183 OR ($session['last_activity'] + $this->sess_expiration) < $this->now // Is the session current?
184 OR ($this->sess_match_ip == TRUE && $session['ip_address'] !== $this->CI->input->ip_address()) // Does the IP match?
185 OR ($this->sess_match_useragent == TRUE && trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120))) // Does the User Agent Match?
186 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
188 $this->sess_destroy();
189 return FALSE;
190 }
191
192 // Is there a corresponding session in the DB?
193 if ($this->sess_use_database === TRUE)
194 {
195 $this->CI->db->where('session_id', $session['session_id']);
196
197 if ($this->sess_match_ip == TRUE)
198 {
199 $this->CI->db->where('ip_address', $session['ip_address']);
200 }
201
202 if ($this->sess_match_useragent == TRUE)
203 {
204 $this->CI->db->where('user_agent', $session['user_agent']);
205 }
206
207 $query = $this->CI->db->get($this->sess_table_name);
208
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200209 // No result? Kill it!
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200210 if ($query->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 $this->sess_destroy();
213 return FALSE;
214 }
215
Derek Jones4b9c6292011-07-01 17:40:48 -0500216 // Is there custom data? If so, add it to the main session array
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 $row = $query->row();
218 if (isset($row->user_data) AND $row->user_data != '')
219 {
220 $custom_data = $this->_unserialize($row->user_data);
221
222 if (is_array($custom_data))
223 {
224 foreach ($custom_data as $key => $val)
225 {
226 $session[$key] = $val;
227 }
228 }
229 }
230 }
231
232 // Session is valid!
233 $this->userdata = $session;
234 unset($session);
235
236 return TRUE;
237 }
238
239 // --------------------------------------------------------------------
240
241 /**
242 * Write the session data
243 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 * @return void
245 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200246 public function sess_write()
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500248 // Are we saving custom data to the DB? If not, all we do is update the cookie
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 if ($this->sess_use_database === FALSE)
250 {
251 $this->_set_cookie();
252 return;
253 }
254
255 // set the custom userdata, the session data we will set in a second
256 $custom_userdata = $this->userdata;
257 $cookie_userdata = array();
258
259 // Before continuing, we need to determine if there is any custom data to deal with.
260 // Let's determine this by removing the default indexes to see if there's anything left in the array
261 // and set the session data while we're at it
262 foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
263 {
264 unset($custom_userdata[$val]);
265 $cookie_userdata[$val] = $this->userdata[$val];
266 }
267
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200268 // Did we find any custom data? If not, we turn the empty array into a string
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // since there's no reason to serialize and store an empty array in the DB
270 if (count($custom_userdata) === 0)
271 {
272 $custom_userdata = '';
273 }
274 else
275 {
276 // Serialize the custom data array so we can store it
277 $custom_userdata = $this->_serialize($custom_userdata);
278 }
279
280 // Run the update query
281 $this->CI->db->where('session_id', $this->userdata['session_id']);
282 $this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
283
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200284 // Write the cookie. Notice that we manually pass the cookie data array to the
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // _set_cookie() function. Normally that function will store $this->userdata, but
286 // in this case that array contains custom data, which we do not want in the cookie.
287 $this->_set_cookie($cookie_userdata);
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * Create a new session
294 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * @return void
296 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200297 public function sess_create()
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
299 $sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200300 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
302 $sessid .= mt_rand(0, mt_getrandmax());
303 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200304 while (strlen($sessid) < 32);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
306 // To make the session ID even more secure we'll combine it with the user's IP
307 $sessid .= $this->CI->input->ip_address();
308
309 $this->userdata = array(
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200310 'session_id' => md5(uniqid($sessid, TRUE)),
311 'ip_address' => $this->CI->input->ip_address(),
312 'user_agent' => substr($this->CI->input->user_agent(), 0, 120),
313 'last_activity' => $this->now,
314 'user_data' => ''
315 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000316
317 // Save the data to the DB if needed
318 if ($this->sess_use_database === TRUE)
319 {
320 $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata));
321 }
322
323 // Write the cookie
324 $this->_set_cookie();
325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * Update an existing session
331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @return void
333 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200334 public function sess_update()
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 // We only update the session every five minutes by default
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200337 if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now
338 OR $this->CI->input->is_ajax_request()) // Changing the session ID during an AJAX call causes problems
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
340 return;
341 }
342
343 // Save the old session id so we know which record to
344 // update in the database if we need it
345 $old_sessid = $this->userdata['session_id'];
346 $new_sessid = '';
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200347 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 $new_sessid .= mt_rand(0, mt_getrandmax());
350 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200351 while (strlen($new_sessid) < 32);
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
353 // To make the session ID even more secure we'll combine it with the user's IP
354 $new_sessid .= $this->CI->input->ip_address();
355
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200356 // Turn it into a hash and update the session data array
357 $this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 $this->userdata['last_activity'] = $this->now;
359
360 // _set_cookie() will handle this for us if we aren't using database sessions
361 // by pushing all userdata to the cookie.
362 $cookie_data = NULL;
363
364 // Update the session ID and last_activity field in the DB if needed
365 if ($this->sess_use_database === TRUE)
366 {
367 // set cookie explicitly to only have our session data
368 $cookie_data = array();
369 foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
370 {
371 $cookie_data[$val] = $this->userdata[$val];
372 }
373
374 $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)));
375 }
376
377 // Write the cookie
378 $this->_set_cookie($cookie_data);
379 }
380
381 // --------------------------------------------------------------------
382
383 /**
384 * Destroy the current session
385 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * @return void
387 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200388 public function sess_destroy()
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 // Kill the session DB row
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200391 if ($this->sess_use_database === TRUE && isset($this->userdata['session_id']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 $this->CI->db->where('session_id', $this->userdata['session_id']);
394 $this->CI->db->delete($this->sess_table_name);
395 }
396
397 // Kill the cookie
398 setcookie(
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200399 $this->sess_cookie_name,
400 addslashes(serialize(array())),
401 ($this->now - 31500000),
402 $this->cookie_path,
403 $this->cookie_domain,
404 0
405 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * Fetch a specific item from the session array
412 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 * @param string
414 * @return string
415 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200416 public function userdata($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
418 return ( ! isset($this->userdata[$item])) ? FALSE : $this->userdata[$item];
419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * Fetch all session data
425 *
Greg Aker34033662011-04-18 11:18:09 -0500426 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200428 public function all_userdata()
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Greg Aker34033662011-04-18 11:18:09 -0500430 return $this->userdata;
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Add or change data in the "userdata" array
437 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @param mixed
439 * @param string
440 * @return void
441 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200442 public function set_userdata($newdata = array(), $newval = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
444 if (is_string($newdata))
445 {
446 $newdata = array($newdata => $newval);
447 }
448
449 if (count($newdata) > 0)
450 {
451 foreach ($newdata as $key => $val)
452 {
453 $this->userdata[$key] = $val;
454 }
455 }
456
457 $this->sess_write();
458 }
459
460 // --------------------------------------------------------------------
461
462 /**
463 * Delete a session variable from the "userdata" array
464 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 * @return void
466 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200467 public function unset_userdata($newdata = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 if (is_string($newdata))
470 {
471 $newdata = array($newdata => '');
472 }
473
474 if (count($newdata) > 0)
475 {
476 foreach ($newdata as $key => $val)
477 {
478 unset($this->userdata[$key]);
479 }
480 }
481
482 $this->sess_write();
483 }
484
485 // ------------------------------------------------------------------------
486
487 /**
488 * Add or change flashdata, only available
489 * until the next request
490 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 * @param mixed
492 * @param string
493 * @return void
494 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200495 public function set_flashdata($newdata = array(), $newval = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
497 if (is_string($newdata))
498 {
499 $newdata = array($newdata => $newval);
500 }
501
502 if (count($newdata) > 0)
503 {
504 foreach ($newdata as $key => $val)
505 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200506 $this->set_userdata($this->flashdata_key.':new:'.$key, $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508 }
509 }
510
511 // ------------------------------------------------------------------------
512
513 /**
514 * Keeps existing flashdata available to next request.
515 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 * @param string
517 * @return void
518 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200519 public function keep_flashdata($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200521 // 'old' flashdata gets removed. Here we mark all
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // flashdata as 'new' to preserve it from _flashdata_sweep()
523 // Note the function will return FALSE if the $key
524 // provided cannot be found
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200525 $value = $this->userdata($this->flashdata_key.':old:'.$key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000526
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200527 $this->set_userdata($this->flashdata_key.':new:'.$key, $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529
530 // ------------------------------------------------------------------------
531
532 /**
533 * Fetch a specific flashdata item from the session array
534 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 * @param string
536 * @return string
537 */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200538 public function flashdata($key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200540 return $this->userdata($this->flashdata_key.':old:'.$key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542
543 // ------------------------------------------------------------------------
544
545 /**
546 * Identifies flashdata as 'old' for removal
547 * when _flashdata_sweep() runs.
548 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 * @return void
550 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200551 protected function _flashdata_mark()
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
553 $userdata = $this->all_userdata();
554 foreach ($userdata as $name => $value)
555 {
556 $parts = explode(':new:', $name);
557 if (is_array($parts) && count($parts) === 2)
558 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200559 $this->set_userdata($this->flashdata_key.':old:'.$parts[1], $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 $this->unset_userdata($name);
561 }
562 }
563 }
564
565 // ------------------------------------------------------------------------
566
567 /**
568 * Removes all flashdata marked as 'old'
569 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 * @return void
571 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200572 protected function _flashdata_sweep()
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 {
574 $userdata = $this->all_userdata();
575 foreach ($userdata as $key => $value)
576 {
577 if (strpos($key, ':old:'))
578 {
579 $this->unset_userdata($key);
580 }
581 }
582
583 }
584
585 // --------------------------------------------------------------------
586
587 /**
588 * Get the "now" time
589 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @return string
591 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200592 protected function _get_time()
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200594 return (strtolower($this->time_reference) === 'gmt')
595 ? mktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('m'), gmdate('d'), gmdate('Y'))
596 : time();
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 }
598
599 // --------------------------------------------------------------------
600
601 /**
602 * Write the session cookie
603 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 * @return void
605 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200606 protected function _set_cookie($cookie_data = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
608 if (is_null($cookie_data))
609 {
610 $cookie_data = $this->userdata;
611 }
612
613 // Serialize the userdata for the cookie
614 $cookie_data = $this->_serialize($cookie_data);
615
616 if ($this->sess_encrypt_cookie == TRUE)
617 {
618 $cookie_data = $this->CI->encrypt->encode($cookie_data);
619 }
620 else
621 {
622 // if encryption is not used, we provide an md5 hash to prevent userside tampering
623 $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Joneseaa71ba2010-09-02 10:32:07 -0500626 $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 // Set the cookie
629 setcookie(
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200630 $this->sess_cookie_name,
631 $cookie_data,
632 $expire,
633 $this->cookie_path,
634 $this->cookie_domain,
635 $this->cookie_secure
636 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 }
638
639 // --------------------------------------------------------------------
640
641 /**
642 * Serialize an array
643 *
644 * This function first converts any slashes found in the array to a temporary
645 * marker, so when it gets unserialized the slashes will be preserved
646 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 * @param array
648 * @return string
649 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200650 protected function _serialize($data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
652 if (is_array($data))
653 {
Chris Muench95933492011-10-16 14:14:04 -0400654 array_walk_recursive($data, array(&$this, '_escape_slashes'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200656 elseif (is_string($data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 {
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200658 $data = str_replace('\\', '{{slash}}', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 return serialize($data);
661 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200662
Chris Muench95933492011-10-16 14:14:04 -0400663 /**
664 * Escape slashes
665 *
666 * This function converts any slashes found into a temporary marker
667 *
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200668 * @param string
669 * @param string
670 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400671 */
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200672 protected function _escape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400673 {
674 if (is_string($val))
675 {
676 $val = str_replace('\\', '{{slash}}', $val);
677 }
678 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000679
680 // --------------------------------------------------------------------
681
682 /**
683 * Unserialize
684 *
685 * This function unserializes a data string, then converts any
686 * temporary slash markers back to actual slashes
687 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @param array
689 * @return string
690 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200691 protected function _unserialize($data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 $data = @unserialize(strip_slashes($data));
694
695 if (is_array($data))
696 {
Chris Muench95933492011-10-16 14:14:04 -0400697 array_walk_recursive($data, array(&$this, '_unescape_slashes'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 return $data;
699 }
700
Derek Jones133e6662010-03-29 11:36:42 -0500701 return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200703
Chris Muench95933492011-10-16 14:14:04 -0400704 /**
705 * Unescape slashes
706 *
707 * This function converts any slash markers back into actual slashes
708 *
Andrey Andreeveea2ff52012-01-19 13:21:53 +0200709 * @param string
710 * @param string
711 * @return void
Chris Muench95933492011-10-16 14:14:04 -0400712 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200713 protected function _unescape_slashes(&$val, $key)
Chris Muench95933492011-10-16 14:14:04 -0400714 {
Chris Muench3e414f92011-10-16 23:03:55 -0400715 if (is_string($val))
716 {
717 $val= str_replace('{{slash}}', '\\', $val);
718 }
Chris Muench95933492011-10-16 14:14:04 -0400719 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000720
721 // --------------------------------------------------------------------
722
723 /**
724 * Garbage collection
725 *
726 * This deletes expired session rows from database
727 * if the probability percentage is met
728 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 * @return void
730 */
Andrey Andreev2c79b762011-12-26 16:54:44 +0200731 protected function _sess_gc()
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
733 if ($this->sess_use_database != TRUE)
734 {
735 return;
736 }
737
738 srand(time());
739 if ((rand() % 100) < $this->gc_probability)
740 {
741 $expire = $this->now - $this->sess_expiration;
742
743 $this->CI->db->where("last_activity < {$expire}");
744 $this->CI->db->delete($this->sess_table_name);
745
746 log_message('debug', 'Session garbage collection performed.');
747 }
748 }
749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750}
Derek Allard2067d1a2008-11-13 22:59:24 +0000751
752/* End of file Session.php */
Andrey Andreev57ffbbb2011-12-25 04:48:47 +0200753/* Location: ./system/libraries/Session.php */