blob: e6f6050c0f4d36cddf60395171ccbeeea08b46bf [file] [log] [blame]
Darren Hillc4e266b2011-08-30 15:40:27 -04001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +03005 * An open source application development framework for PHP 5.2.4 or newer
6 *
7 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
Darren Hillc4e266b2011-08-30 15:40:27 -040018 *
19 * @package CodeIgniter
Andrey Andreev9ffcee62012-09-05 16:25:16 +030020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Darren Hillc4e266b2011-08-30 15:40:27 -040023 * @link http://codeigniter.com
24 * @since Version 2.0
25 * @filesource
26 */
27
Darren Hillc4e266b2011-08-30 15:40:27 -040028/**
Andrey Andreev9ffcee62012-09-05 16:25:16 +030029 * CodeIgniter Session Class
Darren Hillc4e266b2011-08-30 15:40:27 -040030 *
31 * The user interface defined by EllisLabs, now with puggable drivers to manage different storage mechanisms.
dchill420e884082012-08-11 20:10:17 -040032 * By default, the cookie session driver will load, but the 'sess_driver' config/param item (see above) can be
33 * used to specify the 'native' driver, or any other you might create.
Darren Hillc4e266b2011-08-30 15:40:27 -040034 * Once loaded, this driver setup is a drop-in replacement for the former CI_Session library, taking its place as the
35 * 'session' member of the global controller framework (e.g.: $CI->session or $this->session).
36 * In keeping with the CI_Driver methodology, multiple drivers may be loaded, although this might be a bit confusing.
Darren Hill5073a372011-08-31 13:54:19 -040037 * The CI_Session library class keeps track of the most recently loaded driver as "current" to call for driver methods.
Darren Hillc4e266b2011-08-30 15:40:27 -040038 * Ideally, one driver is loaded and all calls go directly through the main library interface. However, any methods
39 * called through the specific driver will switch the "current" driver to itself before invoking the library method
40 * (which will then call back into the driver for low-level operations). So, alternation between two drivers can be
41 * achieved by specifying which driver to use for each call (e.g.: $this->session->native->set_userdata('foo', 'bar');
42 * $this->session->cookie->userdata('foo'); $this->session->native->unset_userdata('foo');). Notice in the previous
43 * example that the _native_ userdata value 'foo' would be set to 'bar', which would NOT be returned by the call for
44 * the _cookie_ userdata 'foo', nor would the _cookie_ value be unset by the call to unset the _native_ 'foo' value.
45 *
46 * @package CodeIgniter
47 * @subpackage Libraries
48 * @category Sessions
Andrey Andreev9ffcee62012-09-05 16:25:16 +030049 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -040050 * @link http://codeigniter.com/user_guide/libraries/sessions.html
51 */
Darren Hilla2ae6572011-09-01 07:36:26 -040052class CI_Session extends CI_Driver_Library {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030053
Darren Hillc4e266b2011-08-30 15:40:27 -040054 public $params = array();
Andrey Andreev9ffcee62012-09-05 16:25:16 +030055 protected $current = NULL;
Darren Hilla2ae6572011-09-01 07:36:26 -040056 protected $userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -040057
58 const FLASHDATA_KEY = 'flash';
59 const FLASHDATA_NEW = ':new:';
60 const FLASHDATA_OLD = ':old:';
61 const FLASHDATA_EXP = ':exp:';
62 const EXPIRATION_KEY = '__expirations';
63 const TEMP_EXP_DEF = 300;
64
65 /**
Darren Hill5073a372011-08-31 13:54:19 -040066 * CI_Session constructor
Darren Hillc4e266b2011-08-30 15:40:27 -040067 *
68 * The constructor loads the configured driver ('sess_driver' in config.php or as a parameter), running
69 * routines in its constructor, and manages flashdata aging.
70 *
Darren Hill5073a372011-08-31 13:54:19 -040071 * @param array Configuration parameters
Darren Hillc4e266b2011-08-30 15:40:27 -040072 */
73 public function __construct(array $params = array())
74 {
Darren Hill5073a372011-08-31 13:54:19 -040075 log_message('debug', 'CI_Session Class Initialized');
Darren Hillc4e266b2011-08-30 15:40:27 -040076
77 // Get valid drivers list
78 $CI =& get_instance();
dchill4226429202012-07-31 10:55:07 -040079 $this->valid_drivers = array(
80 'Session_native',
81 'Session_cookie'
82 );
Darren Hillc4e266b2011-08-30 15:40:27 -040083 $key = 'sess_valid_drivers';
Andrey Andreev9ffcee62012-09-05 16:25:16 +030084 $drivers = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -040085 if ($drivers)
86 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030087 is_array($drivers) OR $drivers = array($drivers);
Darren Hillc4e266b2011-08-30 15:40:27 -040088
89 // Add driver names to valid list
90 foreach ($drivers as $driver)
91 {
dchill4277ee3fd2012-07-24 11:50:01 -040092 if ( ! in_array(strtolower($driver), array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -040093 {
94 $this->valid_drivers[] = $driver;
95 }
96 }
97 }
98
99 // Get driver to load
100 $key = 'sess_driver';
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300101 $driver = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
102 if ( ! $driver)
103 {
104 $driver = 'cookie';
105 }
106
dchill4277ee3fd2012-07-24 11:50:01 -0400107 if ( ! in_array('session_'.strtolower($driver), array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400108 {
109 $this->valid_drivers[] = 'Session_'.$driver;
110 }
111
112 // Save a copy of parameters in case drivers need access
113 $this->params = $params;
114
115 // Load driver and get array reference
116 $this->load_driver($driver);
Darren Hillc4e266b2011-08-30 15:40:27 -0400117
118 // Delete 'old' flashdata (from last request)
119 $this->_flashdata_sweep();
120
121 // Mark all new flashdata as old (data will be deleted before next request)
122 $this->_flashdata_mark();
123
124 // Delete expired tempdata
125 $this->_tempdata_sweep();
126
Darren Hill5073a372011-08-31 13:54:19 -0400127 log_message('debug', 'CI_Session routines successfully run');
Darren Hillc4e266b2011-08-30 15:40:27 -0400128 }
129
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300130 // ------------------------------------------------------------------------
131
Darren Hillc4e266b2011-08-30 15:40:27 -0400132 /**
133 * Loads session storage driver
134 *
Darren Hill5073a372011-08-31 13:54:19 -0400135 * @param string Driver classname
136 * @return object Loaded driver object
Darren Hillc4e266b2011-08-30 15:40:27 -0400137 */
138 public function load_driver($driver)
139 {
dchill4226429202012-07-31 10:55:07 -0400140 // Save reference to most recently loaded driver as library default and sync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400141 $this->current = parent::load_driver($driver);
dchill42b1855372012-07-31 09:32:23 -0400142 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400143 return $this->current;
144 }
145
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300146 // ------------------------------------------------------------------------
147
Darren Hillc4e266b2011-08-30 15:40:27 -0400148 /**
149 * Select default session storage driver
150 *
Darren Hill5073a372011-08-31 13:54:19 -0400151 * @param string Driver classname
152 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400153 */
154 public function select_driver($driver)
155 {
156 // Validate driver name
dchill42c5872252012-07-30 14:53:11 -0400157 $lowername = strtolower(str_replace('CI_', '', $driver));
Darren Hillc4e266b2011-08-30 15:40:27 -0400158 if (in_array($lowername, array_map('strtolower', $this->valid_drivers)))
159 {
dchill42b1855372012-07-31 09:32:23 -0400160 // See if driver is loaded
161 $child = str_replace($this->lib_name.'_', '', $driver);
162 if (isset($this->$child))
Darren Hillc4e266b2011-08-30 15:40:27 -0400163 {
dchill42aee92652012-08-26 21:45:35 -0400164 // See if driver is already current
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300165 if ($this->$child !== $this->current)
166 {
dchill42aee92652012-08-26 21:45:35 -0400167 // Make driver current and sync userdata
168 $this->current = $this->$child;
169 $this->userdata =& $this->current->get_userdata();
170 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400171 }
172 else
173 {
dchill4226429202012-07-31 10:55:07 -0400174 // Load new driver
dchill42aee92652012-08-26 21:45:35 -0400175 $this->load_driver($child);
Darren Hillc4e266b2011-08-30 15:40:27 -0400176 }
177 }
178 }
179
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300180 // ------------------------------------------------------------------------
181
Darren Hillc4e266b2011-08-30 15:40:27 -0400182 /**
183 * Destroy the current session
184 *
Darren Hill5073a372011-08-31 13:54:19 -0400185 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400186 */
187 public function sess_destroy()
188 {
189 // Just call destroy on driver
190 $this->current->sess_destroy();
191 }
192
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300193 // ------------------------------------------------------------------------
194
Darren Hillc4e266b2011-08-30 15:40:27 -0400195 /**
196 * Regenerate the current session
197 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300198 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400199 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400200 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300201 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400202 {
dchill4226429202012-07-31 10:55:07 -0400203 // Call regenerate on driver and resync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400204 $this->current->sess_regenerate($destroy);
dchill4226429202012-07-31 10:55:07 -0400205 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400206 }
207
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300208 // ------------------------------------------------------------------------
209
Darren Hillc4e266b2011-08-30 15:40:27 -0400210 /**
211 * Fetch a specific item from the session array
212 *
Darren Hill5073a372011-08-31 13:54:19 -0400213 * @param string Item key
dchill42c5872252012-07-30 14:53:11 -0400214 * @return string Item value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400215 */
216 public function userdata($item)
217 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300218 return isset($this->userdata[$item]) ? $this->userdata[$item] : NULL;
Darren Hillc4e266b2011-08-30 15:40:27 -0400219 }
220
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300221 // ------------------------------------------------------------------------
222
Darren Hillc4e266b2011-08-30 15:40:27 -0400223 /**
224 * Fetch all session data
225 *
226 * @return array User data array
227 */
228 public function all_userdata()
229 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300230 return isset($this->userdata) ? $this->userdata : NULL;
Darren Hillc4e266b2011-08-30 15:40:27 -0400231 }
232
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300233 // ------------------------------------------------------------------------
234
Darren Hillc4e266b2011-08-30 15:40:27 -0400235 /**
dchill42c5079de2012-07-23 10:53:47 -0400236 * Fetch all flashdata
237 *
238 * @return array Flash data array
239 */
240 public function all_flashdata()
241 {
242 $out = array();
243
244 // loop through all userdata
245 foreach ($this->all_userdata() as $key => $val)
246 {
dchill42c5872252012-07-30 14:53:11 -0400247 // if it contains flashdata, add it
dchill42c5079de2012-07-23 10:53:47 -0400248 if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE)
249 {
dchill4226429202012-07-31 10:55:07 -0400250 $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key);
dchill42c5079de2012-07-23 10:53:47 -0400251 $out[$key] = $val;
252 }
253 }
254 return $out;
255 }
256
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300257 // ------------------------------------------------------------------------
258
dchill42c5079de2012-07-23 10:53:47 -0400259 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400260 * Add or change data in the "userdata" array
261 *
Darren Hill5073a372011-08-31 13:54:19 -0400262 * @param mixed Item name or array of items
263 * @param string Item value or empty string
264 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400265 */
266 public function set_userdata($newdata = array(), $newval = '')
267 {
268 // Wrap params as array if singular
269 if (is_string($newdata))
270 {
271 $newdata = array($newdata => $newval);
272 }
273
274 // Set each name/value pair
275 if (count($newdata) > 0)
276 {
277 foreach ($newdata as $key => $val)
278 {
279 $this->userdata[$key] = $val;
280 }
281 }
282
283 // Tell driver data changed
284 $this->current->sess_save();
285 }
286
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300287 // ------------------------------------------------------------------------
288
Darren Hillc4e266b2011-08-30 15:40:27 -0400289 /**
290 * Delete a session variable from the "userdata" array
291 *
Darren Hill5073a372011-08-31 13:54:19 -0400292 * @param mixed Item name or array of item names
293 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400294 */
295 public function unset_userdata($newdata = array())
296 {
297 // Wrap single name as array
298 if (is_string($newdata))
299 {
300 $newdata = array($newdata => '');
301 }
302
303 // Unset each item name
304 if (count($newdata) > 0)
305 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300306 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400307 {
308 unset($this->userdata[$key]);
309 }
310 }
311
312 // Tell driver data changed
313 $this->current->sess_save();
314 }
315
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300316 // ------------------------------------------------------------------------
317
Darren Hillc4e266b2011-08-30 15:40:27 -0400318 /**
319 * Determine if an item exists
320 *
Darren Hill5073a372011-08-31 13:54:19 -0400321 * @param string Item name
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300322 * @return bool
Darren Hillc4e266b2011-08-30 15:40:27 -0400323 */
324 public function has_userdata($item)
325 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400326 return isset($this->userdata[$item]);
327 }
328
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300329 // ------------------------------------------------------------------------
330
Darren Hillc4e266b2011-08-30 15:40:27 -0400331 /**
332 * Add or change flashdata, only available until the next request
333 *
Darren Hill5073a372011-08-31 13:54:19 -0400334 * @param mixed Item name or array of items
335 * @param string Item value or empty string
336 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400337 */
338 public function set_flashdata($newdata = array(), $newval = '')
339 {
340 // Wrap item as array if singular
341 if (is_string($newdata))
342 {
343 $newdata = array($newdata => $newval);
344 }
345
346 // Prepend each key name and set value
347 if (count($newdata) > 0)
348 {
349 foreach ($newdata as $key => $val)
350 {
351 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
352 $this->set_userdata($flashdata_key, $val);
353 }
354 }
355 }
356
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300357 // ------------------------------------------------------------------------
358
Darren Hillc4e266b2011-08-30 15:40:27 -0400359 /**
360 * Keeps existing flashdata available to next request.
361 *
Darren Hill5073a372011-08-31 13:54:19 -0400362 * @param string Item key
363 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400364 */
365 public function keep_flashdata($key)
366 {
dchill42c5079de2012-07-23 10:53:47 -0400367 // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep()
368 // Note the function will return NULL if the $key provided cannot be found
Darren Hillc4e266b2011-08-30 15:40:27 -0400369 $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
370 $value = $this->userdata($old_flashdata_key);
371
372 $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
373 $this->set_userdata($new_flashdata_key, $value);
374 }
375
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300376 // ------------------------------------------------------------------------
377
Darren Hillc4e266b2011-08-30 15:40:27 -0400378 /**
379 * Fetch a specific flashdata item from the session array
380 *
Darren Hill5073a372011-08-31 13:54:19 -0400381 * @param string Item key
382 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400383 */
384 public function flashdata($key)
385 {
386 // Prepend key and retrieve value
387 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
388 return $this->userdata($flashdata_key);
389 }
390
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300391 // ------------------------------------------------------------------------
392
Darren Hillc4e266b2011-08-30 15:40:27 -0400393 /**
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300394 * Add or change tempdata, only available until expiration
Darren Hillc4e266b2011-08-30 15:40:27 -0400395 *
Darren Hill5073a372011-08-31 13:54:19 -0400396 * @param mixed Item name or array of items
397 * @param string Item value or empty string
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300398 * @param int Item lifetime in seconds or 0 for default
Darren Hill5073a372011-08-31 13:54:19 -0400399 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400400 */
401 public function set_tempdata($newdata = array(), $newval = '', $expire = 0)
402 {
403 // Set expiration time
404 $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF);
405
406 // Wrap item as array if singular
407 if (is_string($newdata))
408 {
409 $newdata = array($newdata => $newval);
410 }
411
412 // Get or create expiration list
413 $expirations = $this->userdata(self::EXPIRATION_KEY);
dchill4277ee3fd2012-07-24 11:50:01 -0400414 if ( ! $expirations)
Darren Hillc4e266b2011-08-30 15:40:27 -0400415 {
416 $expirations = array();
417 }
418
419 // Prepend each key name and set value
420 if (count($newdata) > 0)
421 {
422 foreach ($newdata as $key => $val)
423 {
424 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
425 $expirations[$tempdata_key] = $expire;
426 $this->set_userdata($tempdata_key, $val);
427 }
428 }
429
430 // Update expiration list
431 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
432 }
433
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300434 // ------------------------------------------------------------------------
435
Darren Hillc4e266b2011-08-30 15:40:27 -0400436 /**
437 * Delete a temporary session variable from the "userdata" array
438 *
Darren Hill5073a372011-08-31 13:54:19 -0400439 * @param mixed Item name or array of item names
440 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400441 */
442 public function unset_tempdata($newdata = array())
443 {
444 // Get expirations list
445 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300446 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400447 {
448 // Nothing to do
449 return;
450 }
451
452 // Wrap single name as array
453 if (is_string($newdata))
454 {
455 $newdata = array($newdata => '');
456 }
457
458 // Prepend each item name and unset
459 if (count($newdata) > 0)
460 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300461 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400462 {
463 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
464 unset($expirations[$tempdata_key]);
465 $this->unset_userdata($tempdata_key);
466 }
467 }
468
469 // Update expiration list
470 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
471 }
472
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300473 // ------------------------------------------------------------------------
474
Darren Hillc4e266b2011-08-30 15:40:27 -0400475 /**
476 * Fetch a specific tempdata item from the session array
477 *
Darren Hill5073a372011-08-31 13:54:19 -0400478 * @param string Item key
479 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400480 */
481 public function tempdata($key)
482 {
483 // Prepend key and return value
484 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
485 return $this->userdata($tempdata_key);
486 }
487
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300488 // ------------------------------------------------------------------------
489
Darren Hillc4e266b2011-08-30 15:40:27 -0400490 /**
491 * Identifies flashdata as 'old' for removal
492 * when _flashdata_sweep() runs.
493 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400494 * @return void
495 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400496 protected function _flashdata_mark()
Darren Hillc4e266b2011-08-30 15:40:27 -0400497 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300498 foreach ($this->all_userdata() as $name => $value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400499 {
500 $parts = explode(self::FLASHDATA_NEW, $name);
501 if (is_array($parts) && count($parts) === 2)
502 {
503 $new_name = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1];
504 $this->set_userdata($new_name, $value);
505 $this->unset_userdata($name);
506 }
507 }
508 }
509
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300510 // ------------------------------------------------------------------------
511
Darren Hillc4e266b2011-08-30 15:40:27 -0400512 /**
513 * Removes all flashdata marked as 'old'
514 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400515 * @return void
516 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400517 protected function _flashdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400518 {
519 $userdata = $this->all_userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300520 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400521 {
522 if (strpos($key, self::FLASHDATA_OLD))
523 {
524 $this->unset_userdata($key);
525 }
526 }
527 }
528
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300529 // ------------------------------------------------------------------------
530
Darren Hillc4e266b2011-08-30 15:40:27 -0400531 /**
532 * Removes all expired tempdata
533 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400534 * @return void
535 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400536 protected function _tempdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400537 {
538 // Get expirations list
539 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300540 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400541 {
542 // Nothing to do
543 return;
544 }
545
546 // Unset expired elements
547 $now = time();
548 $userdata = $this->all_userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300549 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400550 {
551 if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now)
552 {
553 unset($expirations[$key]);
554 $this->unset_userdata($key);
555 }
556 }
557
558 // Update expiration list
559 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
560 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400561
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300562}
563
564// ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400565
566/**
Darren Hill5073a372011-08-31 13:54:19 -0400567 * CI_Session_driver Class
Darren Hillc4e266b2011-08-30 15:40:27 -0400568 *
Darren Hill5073a372011-08-31 13:54:19 -0400569 * Extend this class to make a new CI_Session driver.
570 * A CI_Session driver basically manages an array of name/value pairs with some sort of storage mechanism.
571 * To make a new driver, derive from (extend) CI_Session_driver. Overload the initialize method and read or create
Darren Hillc4e266b2011-08-30 15:40:27 -0400572 * session data. Then implement a save handler to write changed data to storage (sess_save), a destroy handler
573 * to remove deleted data (sess_destroy), and an access handler to expose the data (get_userdata).
Darren Hill5073a372011-08-31 13:54:19 -0400574 * Put your driver in the libraries/Session/drivers folder anywhere in the loader paths. This includes the
575 * application directory, the system directory, or any path you add with $CI->load->add_package_path().
576 * Your driver must be named CI_Session_<name>, and your filename must be Session_<name>.php,
577 * preferably also capitalized. (e.g.: CI_Session_foo in libraries/Session/drivers/Session_foo.php)
578 * Then specify the driver by setting 'sess_driver' in your config file or as a parameter when loading the CI_Session
Darren Hillc4e266b2011-08-30 15:40:27 -0400579 * object. (e.g.: $config['sess_driver'] = 'foo'; OR $CI->load->driver('session', array('sess_driver' => 'foo')); )
580 * Already provided are the Native driver, which manages the native PHP $_SESSION array, and
581 * the Cookie driver, which manages the data in a browser cookie, with optional extra storage in a database table.
582 *
Darren Hill5073a372011-08-31 13:54:19 -0400583 * @package CodeIgniter
584 * @subpackage Libraries
Darren Hillc4e266b2011-08-30 15:40:27 -0400585 * @category Sessions
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300586 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -0400587 */
Darren Hill5073a372011-08-31 13:54:19 -0400588abstract class CI_Session_driver extends CI_Driver {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300589
Darren Hillc4e266b2011-08-30 15:40:27 -0400590 /**
591 * Decorate
592 *
593 * Decorates the child with the parent driver lib's methods and properties
594 *
595 * @param object Parent library object
596 * @return void
597 */
598 public function decorate($parent)
599 {
600 // Call base class decorate first
601 parent::decorate($parent);
602
dchill42c5872252012-07-30 14:53:11 -0400603 // Call initialize method now that driver has access to $this->_parent
Darren Hillc4e266b2011-08-30 15:40:27 -0400604 $this->initialize();
605 }
606
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300607 // ------------------------------------------------------------------------
608
Darren Hillc4e266b2011-08-30 15:40:27 -0400609 /**
610 * __call magic method
611 *
612 * Handles access to the parent driver library's methods
613 *
Darren Hill5073a372011-08-31 13:54:19 -0400614 * @param string Library method name
615 * @param array Method arguments (default: none)
Darren Hillc4e266b2011-08-30 15:40:27 -0400616 * @return mixed
617 */
618 public function __call($method, $args = array())
619 {
620 // Make sure the parent library uses this driver
dchill42c5872252012-07-30 14:53:11 -0400621 $this->_parent->select_driver(get_class($this));
Darren Hillc4e266b2011-08-30 15:40:27 -0400622 return parent::__call($method, $args);
623 }
624
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300625 // ------------------------------------------------------------------------
626
Darren Hillc4e266b2011-08-30 15:40:27 -0400627 /**
628 * Initialize driver
629 *
Darren Hill5073a372011-08-31 13:54:19 -0400630 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400631 */
632 protected function initialize()
633 {
634 // Overload this method to implement initialization
635 }
636
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300637 // ------------------------------------------------------------------------
638
Darren Hillc4e266b2011-08-30 15:40:27 -0400639 /**
640 * Save the session data
641 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300642 * Data in the array has changed - perform any storage synchronization
643 * necessary. The child class MUST implement this abstract method!
Darren Hillc4e266b2011-08-30 15:40:27 -0400644 *
Darren Hill5073a372011-08-31 13:54:19 -0400645 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400646 */
647 abstract public function sess_save();
648
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300649 // ------------------------------------------------------------------------
650
Darren Hillc4e266b2011-08-30 15:40:27 -0400651 /**
652 * Destroy the current session
653 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300654 * Clean up storage for this session - it has been terminated.
Darren Hillc4e266b2011-08-30 15:40:27 -0400655 * The child class MUST implement this abstract method!
656 *
Darren Hill5073a372011-08-31 13:54:19 -0400657 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400658 */
659 abstract public function sess_destroy();
660
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300661 // ------------------------------------------------------------------------
662
Darren Hillc4e266b2011-08-30 15:40:27 -0400663 /**
664 * Regenerate the current session
665 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300666 * Regenerate the session ID.
Darren Hillc4e266b2011-08-30 15:40:27 -0400667 * The child class MUST implement this abstract method!
668 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300669 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400670 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400671 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300672 abstract public function sess_regenerate($destroy = FALSE);
673
674 // ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400675
676 /**
677 * Get a reference to user data array
678 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300679 * Give array access to the main CI_Session object.
Darren Hillc4e266b2011-08-30 15:40:27 -0400680 * The child class MUST implement this abstract method!
681 *
Darren Hill5073a372011-08-31 13:54:19 -0400682 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400683 */
684 abstract public function &get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400685
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300686}
Darren Hillc4e266b2011-08-30 15:40:27 -0400687
688/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300689/* Location: ./system/libraries/Session/Session.php */