blob: 978506062e92fdf328f551cfda8d88b69445a294 [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
Andrey Andreev2e3e2302012-10-09 15:52:34 +030072 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -040073 */
74 public function __construct(array $params = array())
75 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030076 $CI =& get_instance();
77
78 // No sessions under CLI
79 if ($CI->input->is_cli_request())
80 {
81 return;
82 }
83
Darren Hill5073a372011-08-31 13:54:19 -040084 log_message('debug', 'CI_Session Class Initialized');
Darren Hillc4e266b2011-08-30 15:40:27 -040085
86 // Get valid drivers list
dchill4226429202012-07-31 10:55:07 -040087 $this->valid_drivers = array(
88 'Session_native',
89 'Session_cookie'
90 );
Darren Hillc4e266b2011-08-30 15:40:27 -040091 $key = 'sess_valid_drivers';
Andrey Andreev9ffcee62012-09-05 16:25:16 +030092 $drivers = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -040093 if ($drivers)
94 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030095 is_array($drivers) OR $drivers = array($drivers);
Darren Hillc4e266b2011-08-30 15:40:27 -040096
97 // Add driver names to valid list
98 foreach ($drivers as $driver)
99 {
dchill4277ee3fd2012-07-24 11:50:01 -0400100 if ( ! in_array(strtolower($driver), array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400101 {
102 $this->valid_drivers[] = $driver;
103 }
104 }
105 }
106
107 // Get driver to load
108 $key = 'sess_driver';
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300109 $driver = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
110 if ( ! $driver)
111 {
112 $driver = 'cookie';
113 }
114
dchill4277ee3fd2012-07-24 11:50:01 -0400115 if ( ! in_array('session_'.strtolower($driver), array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400116 {
117 $this->valid_drivers[] = 'Session_'.$driver;
118 }
119
120 // Save a copy of parameters in case drivers need access
121 $this->params = $params;
122
123 // Load driver and get array reference
124 $this->load_driver($driver);
Darren Hillc4e266b2011-08-30 15:40:27 -0400125
126 // Delete 'old' flashdata (from last request)
127 $this->_flashdata_sweep();
128
129 // Mark all new flashdata as old (data will be deleted before next request)
130 $this->_flashdata_mark();
131
132 // Delete expired tempdata
133 $this->_tempdata_sweep();
134
Darren Hill5073a372011-08-31 13:54:19 -0400135 log_message('debug', 'CI_Session routines successfully run');
Darren Hillc4e266b2011-08-30 15:40:27 -0400136 }
137
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300138 // ------------------------------------------------------------------------
139
Darren Hillc4e266b2011-08-30 15:40:27 -0400140 /**
141 * Loads session storage driver
142 *
Darren Hill5073a372011-08-31 13:54:19 -0400143 * @param string Driver classname
144 * @return object Loaded driver object
Darren Hillc4e266b2011-08-30 15:40:27 -0400145 */
146 public function load_driver($driver)
147 {
dchill4226429202012-07-31 10:55:07 -0400148 // Save reference to most recently loaded driver as library default and sync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400149 $this->current = parent::load_driver($driver);
dchill42b1855372012-07-31 09:32:23 -0400150 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400151 return $this->current;
152 }
153
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300154 // ------------------------------------------------------------------------
155
Darren Hillc4e266b2011-08-30 15:40:27 -0400156 /**
157 * Select default session storage driver
158 *
Darren Hill5073a372011-08-31 13:54:19 -0400159 * @param string Driver classname
160 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400161 */
162 public function select_driver($driver)
163 {
164 // Validate driver name
dchill42c5872252012-07-30 14:53:11 -0400165 $lowername = strtolower(str_replace('CI_', '', $driver));
Darren Hillc4e266b2011-08-30 15:40:27 -0400166 if (in_array($lowername, array_map('strtolower', $this->valid_drivers)))
167 {
dchill42b1855372012-07-31 09:32:23 -0400168 // See if driver is loaded
169 $child = str_replace($this->lib_name.'_', '', $driver);
170 if (isset($this->$child))
Darren Hillc4e266b2011-08-30 15:40:27 -0400171 {
dchill42aee92652012-08-26 21:45:35 -0400172 // See if driver is already current
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300173 if ($this->$child !== $this->current)
174 {
dchill42aee92652012-08-26 21:45:35 -0400175 // Make driver current and sync userdata
176 $this->current = $this->$child;
177 $this->userdata =& $this->current->get_userdata();
178 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400179 }
180 else
181 {
dchill4226429202012-07-31 10:55:07 -0400182 // Load new driver
dchill42aee92652012-08-26 21:45:35 -0400183 $this->load_driver($child);
Darren Hillc4e266b2011-08-30 15:40:27 -0400184 }
185 }
186 }
187
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300188 // ------------------------------------------------------------------------
189
Darren Hillc4e266b2011-08-30 15:40:27 -0400190 /**
191 * Destroy the current session
192 *
Darren Hill5073a372011-08-31 13:54:19 -0400193 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400194 */
195 public function sess_destroy()
196 {
197 // Just call destroy on driver
198 $this->current->sess_destroy();
199 }
200
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300201 // ------------------------------------------------------------------------
202
Darren Hillc4e266b2011-08-30 15:40:27 -0400203 /**
204 * Regenerate the current session
205 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300206 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400207 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400208 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300209 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400210 {
dchill4226429202012-07-31 10:55:07 -0400211 // Call regenerate on driver and resync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400212 $this->current->sess_regenerate($destroy);
dchill4226429202012-07-31 10:55:07 -0400213 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400214 }
215
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300216 // ------------------------------------------------------------------------
217
Darren Hillc4e266b2011-08-30 15:40:27 -0400218 /**
219 * Fetch a specific item from the session array
220 *
Darren Hill5073a372011-08-31 13:54:19 -0400221 * @param string Item key
dchill42c5872252012-07-30 14:53:11 -0400222 * @return string Item value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400223 */
224 public function userdata($item)
225 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300226 return isset($this->userdata[$item]) ? $this->userdata[$item] : NULL;
Darren Hillc4e266b2011-08-30 15:40:27 -0400227 }
228
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300229 // ------------------------------------------------------------------------
230
Darren Hillc4e266b2011-08-30 15:40:27 -0400231 /**
232 * Fetch all session data
233 *
234 * @return array User data array
235 */
236 public function all_userdata()
237 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300238 return isset($this->userdata) ? $this->userdata : NULL;
Darren Hillc4e266b2011-08-30 15:40:27 -0400239 }
240
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300241 // ------------------------------------------------------------------------
242
Darren Hillc4e266b2011-08-30 15:40:27 -0400243 /**
dchill42c5079de2012-07-23 10:53:47 -0400244 * Fetch all flashdata
245 *
246 * @return array Flash data array
247 */
248 public function all_flashdata()
249 {
250 $out = array();
251
252 // loop through all userdata
253 foreach ($this->all_userdata() as $key => $val)
254 {
dchill42c5872252012-07-30 14:53:11 -0400255 // if it contains flashdata, add it
dchill42c5079de2012-07-23 10:53:47 -0400256 if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE)
257 {
dchill4226429202012-07-31 10:55:07 -0400258 $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key);
dchill42c5079de2012-07-23 10:53:47 -0400259 $out[$key] = $val;
260 }
261 }
262 return $out;
263 }
264
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300265 // ------------------------------------------------------------------------
266
dchill42c5079de2012-07-23 10:53:47 -0400267 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400268 * Add or change data in the "userdata" array
269 *
Darren Hill5073a372011-08-31 13:54:19 -0400270 * @param mixed Item name or array of items
271 * @param string Item value or empty string
272 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400273 */
274 public function set_userdata($newdata = array(), $newval = '')
275 {
276 // Wrap params as array if singular
277 if (is_string($newdata))
278 {
279 $newdata = array($newdata => $newval);
280 }
281
282 // Set each name/value pair
283 if (count($newdata) > 0)
284 {
285 foreach ($newdata as $key => $val)
286 {
287 $this->userdata[$key] = $val;
288 }
289 }
290
291 // Tell driver data changed
292 $this->current->sess_save();
293 }
294
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300295 // ------------------------------------------------------------------------
296
Darren Hillc4e266b2011-08-30 15:40:27 -0400297 /**
298 * Delete a session variable from the "userdata" array
299 *
Darren Hill5073a372011-08-31 13:54:19 -0400300 * @param mixed Item name or array of item names
301 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400302 */
303 public function unset_userdata($newdata = array())
304 {
305 // Wrap single name as array
306 if (is_string($newdata))
307 {
308 $newdata = array($newdata => '');
309 }
310
311 // Unset each item name
312 if (count($newdata) > 0)
313 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300314 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400315 {
316 unset($this->userdata[$key]);
317 }
318 }
319
320 // Tell driver data changed
321 $this->current->sess_save();
322 }
323
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300324 // ------------------------------------------------------------------------
325
Darren Hillc4e266b2011-08-30 15:40:27 -0400326 /**
327 * Determine if an item exists
328 *
Darren Hill5073a372011-08-31 13:54:19 -0400329 * @param string Item name
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300330 * @return bool
Darren Hillc4e266b2011-08-30 15:40:27 -0400331 */
332 public function has_userdata($item)
333 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400334 return isset($this->userdata[$item]);
335 }
336
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300337 // ------------------------------------------------------------------------
338
Darren Hillc4e266b2011-08-30 15:40:27 -0400339 /**
340 * Add or change flashdata, only available until the next request
341 *
Darren Hill5073a372011-08-31 13:54:19 -0400342 * @param mixed Item name or array of items
343 * @param string Item value or empty string
344 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400345 */
346 public function set_flashdata($newdata = array(), $newval = '')
347 {
348 // Wrap item as array if singular
349 if (is_string($newdata))
350 {
351 $newdata = array($newdata => $newval);
352 }
353
354 // Prepend each key name and set value
355 if (count($newdata) > 0)
356 {
357 foreach ($newdata as $key => $val)
358 {
359 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
360 $this->set_userdata($flashdata_key, $val);
361 }
362 }
363 }
364
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300365 // ------------------------------------------------------------------------
366
Darren Hillc4e266b2011-08-30 15:40:27 -0400367 /**
368 * Keeps existing flashdata available to next request.
369 *
Darren Hill5073a372011-08-31 13:54:19 -0400370 * @param string Item key
371 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400372 */
373 public function keep_flashdata($key)
374 {
dchill42c5079de2012-07-23 10:53:47 -0400375 // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep()
376 // Note the function will return NULL if the $key provided cannot be found
Darren Hillc4e266b2011-08-30 15:40:27 -0400377 $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
378 $value = $this->userdata($old_flashdata_key);
379
380 $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
381 $this->set_userdata($new_flashdata_key, $value);
382 }
383
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300384 // ------------------------------------------------------------------------
385
Darren Hillc4e266b2011-08-30 15:40:27 -0400386 /**
387 * Fetch a specific flashdata item from the session array
388 *
Darren Hill5073a372011-08-31 13:54:19 -0400389 * @param string Item key
390 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400391 */
392 public function flashdata($key)
393 {
394 // Prepend key and retrieve value
395 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
396 return $this->userdata($flashdata_key);
397 }
398
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300399 // ------------------------------------------------------------------------
400
Darren Hillc4e266b2011-08-30 15:40:27 -0400401 /**
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300402 * Add or change tempdata, only available until expiration
Darren Hillc4e266b2011-08-30 15:40:27 -0400403 *
Darren Hill5073a372011-08-31 13:54:19 -0400404 * @param mixed Item name or array of items
405 * @param string Item value or empty string
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300406 * @param int Item lifetime in seconds or 0 for default
Darren Hill5073a372011-08-31 13:54:19 -0400407 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400408 */
409 public function set_tempdata($newdata = array(), $newval = '', $expire = 0)
410 {
411 // Set expiration time
412 $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF);
413
414 // Wrap item as array if singular
415 if (is_string($newdata))
416 {
417 $newdata = array($newdata => $newval);
418 }
419
420 // Get or create expiration list
421 $expirations = $this->userdata(self::EXPIRATION_KEY);
dchill4277ee3fd2012-07-24 11:50:01 -0400422 if ( ! $expirations)
Darren Hillc4e266b2011-08-30 15:40:27 -0400423 {
424 $expirations = array();
425 }
426
427 // Prepend each key name and set value
428 if (count($newdata) > 0)
429 {
430 foreach ($newdata as $key => $val)
431 {
432 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
433 $expirations[$tempdata_key] = $expire;
434 $this->set_userdata($tempdata_key, $val);
435 }
436 }
437
438 // Update expiration list
439 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
440 }
441
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300442 // ------------------------------------------------------------------------
443
Darren Hillc4e266b2011-08-30 15:40:27 -0400444 /**
445 * Delete a temporary session variable from the "userdata" array
446 *
Darren Hill5073a372011-08-31 13:54:19 -0400447 * @param mixed Item name or array of item names
448 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400449 */
450 public function unset_tempdata($newdata = array())
451 {
452 // Get expirations list
453 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300454 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400455 {
456 // Nothing to do
457 return;
458 }
459
460 // Wrap single name as array
461 if (is_string($newdata))
462 {
463 $newdata = array($newdata => '');
464 }
465
466 // Prepend each item name and unset
467 if (count($newdata) > 0)
468 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300469 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400470 {
471 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
472 unset($expirations[$tempdata_key]);
473 $this->unset_userdata($tempdata_key);
474 }
475 }
476
477 // Update expiration list
478 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
479 }
480
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300481 // ------------------------------------------------------------------------
482
Darren Hillc4e266b2011-08-30 15:40:27 -0400483 /**
484 * Fetch a specific tempdata item from the session array
485 *
Darren Hill5073a372011-08-31 13:54:19 -0400486 * @param string Item key
487 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400488 */
489 public function tempdata($key)
490 {
491 // Prepend key and return value
492 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
493 return $this->userdata($tempdata_key);
494 }
495
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300496 // ------------------------------------------------------------------------
497
Darren Hillc4e266b2011-08-30 15:40:27 -0400498 /**
499 * Identifies flashdata as 'old' for removal
500 * when _flashdata_sweep() runs.
501 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400502 * @return void
503 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400504 protected function _flashdata_mark()
Darren Hillc4e266b2011-08-30 15:40:27 -0400505 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300506 foreach ($this->all_userdata() as $name => $value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400507 {
508 $parts = explode(self::FLASHDATA_NEW, $name);
509 if (is_array($parts) && count($parts) === 2)
510 {
511 $new_name = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1];
512 $this->set_userdata($new_name, $value);
513 $this->unset_userdata($name);
514 }
515 }
516 }
517
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300518 // ------------------------------------------------------------------------
519
Darren Hillc4e266b2011-08-30 15:40:27 -0400520 /**
521 * Removes all flashdata marked as 'old'
522 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400523 * @return void
524 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400525 protected function _flashdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400526 {
527 $userdata = $this->all_userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300528 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400529 {
530 if (strpos($key, self::FLASHDATA_OLD))
531 {
532 $this->unset_userdata($key);
533 }
534 }
535 }
536
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300537 // ------------------------------------------------------------------------
538
Darren Hillc4e266b2011-08-30 15:40:27 -0400539 /**
540 * Removes all expired tempdata
541 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400542 * @return void
543 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400544 protected function _tempdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400545 {
546 // Get expirations list
547 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300548 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400549 {
550 // Nothing to do
551 return;
552 }
553
554 // Unset expired elements
555 $now = time();
556 $userdata = $this->all_userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300557 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400558 {
559 if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now)
560 {
561 unset($expirations[$key]);
562 $this->unset_userdata($key);
563 }
564 }
565
566 // Update expiration list
567 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
568 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400569
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300570}
571
572// ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400573
574/**
Darren Hill5073a372011-08-31 13:54:19 -0400575 * CI_Session_driver Class
Darren Hillc4e266b2011-08-30 15:40:27 -0400576 *
Darren Hill5073a372011-08-31 13:54:19 -0400577 * Extend this class to make a new CI_Session driver.
578 * A CI_Session driver basically manages an array of name/value pairs with some sort of storage mechanism.
579 * 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 -0400580 * session data. Then implement a save handler to write changed data to storage (sess_save), a destroy handler
581 * to remove deleted data (sess_destroy), and an access handler to expose the data (get_userdata).
Darren Hill5073a372011-08-31 13:54:19 -0400582 * Put your driver in the libraries/Session/drivers folder anywhere in the loader paths. This includes the
583 * application directory, the system directory, or any path you add with $CI->load->add_package_path().
584 * Your driver must be named CI_Session_<name>, and your filename must be Session_<name>.php,
585 * preferably also capitalized. (e.g.: CI_Session_foo in libraries/Session/drivers/Session_foo.php)
586 * 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 -0400587 * object. (e.g.: $config['sess_driver'] = 'foo'; OR $CI->load->driver('session', array('sess_driver' => 'foo')); )
588 * Already provided are the Native driver, which manages the native PHP $_SESSION array, and
589 * the Cookie driver, which manages the data in a browser cookie, with optional extra storage in a database table.
590 *
Darren Hill5073a372011-08-31 13:54:19 -0400591 * @package CodeIgniter
592 * @subpackage Libraries
Darren Hillc4e266b2011-08-30 15:40:27 -0400593 * @category Sessions
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300594 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -0400595 */
Darren Hill5073a372011-08-31 13:54:19 -0400596abstract class CI_Session_driver extends CI_Driver {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300597
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300598 protected $CI;
599
600 /**
601 * Constructor
602 *
603 * Gets the CI singleton, so that individual drivers
604 * don't have to do it separately.
605 *
606 * @return void
607 */
608 public function __construct()
609 {
610 $this->CI =& get_instance();
611 }
612
613 // ------------------------------------------------------------------------
614
Darren Hillc4e266b2011-08-30 15:40:27 -0400615 /**
616 * Decorate
617 *
618 * Decorates the child with the parent driver lib's methods and properties
619 *
620 * @param object Parent library object
621 * @return void
622 */
623 public function decorate($parent)
624 {
625 // Call base class decorate first
626 parent::decorate($parent);
627
dchill42c5872252012-07-30 14:53:11 -0400628 // Call initialize method now that driver has access to $this->_parent
Darren Hillc4e266b2011-08-30 15:40:27 -0400629 $this->initialize();
630 }
631
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300632 // ------------------------------------------------------------------------
633
Darren Hillc4e266b2011-08-30 15:40:27 -0400634 /**
635 * __call magic method
636 *
637 * Handles access to the parent driver library's methods
638 *
Darren Hill5073a372011-08-31 13:54:19 -0400639 * @param string Library method name
640 * @param array Method arguments (default: none)
Darren Hillc4e266b2011-08-30 15:40:27 -0400641 * @return mixed
642 */
643 public function __call($method, $args = array())
644 {
645 // Make sure the parent library uses this driver
dchill42c5872252012-07-30 14:53:11 -0400646 $this->_parent->select_driver(get_class($this));
Darren Hillc4e266b2011-08-30 15:40:27 -0400647 return parent::__call($method, $args);
648 }
649
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300650 // ------------------------------------------------------------------------
651
Darren Hillc4e266b2011-08-30 15:40:27 -0400652 /**
653 * Initialize driver
654 *
Darren Hill5073a372011-08-31 13:54:19 -0400655 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400656 */
657 protected function initialize()
658 {
659 // Overload this method to implement initialization
660 }
661
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300662 // ------------------------------------------------------------------------
663
Darren Hillc4e266b2011-08-30 15:40:27 -0400664 /**
665 * Save the session data
666 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300667 * Data in the array has changed - perform any storage synchronization
668 * necessary. The child class MUST implement this abstract method!
Darren Hillc4e266b2011-08-30 15:40:27 -0400669 *
Darren Hill5073a372011-08-31 13:54:19 -0400670 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400671 */
672 abstract public function sess_save();
673
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300674 // ------------------------------------------------------------------------
675
Darren Hillc4e266b2011-08-30 15:40:27 -0400676 /**
677 * Destroy the current session
678 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300679 * Clean up storage for this session - it has been terminated.
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 void
Darren Hillc4e266b2011-08-30 15:40:27 -0400683 */
684 abstract public function sess_destroy();
685
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300686 // ------------------------------------------------------------------------
687
Darren Hillc4e266b2011-08-30 15:40:27 -0400688 /**
689 * Regenerate the current session
690 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300691 * Regenerate the session ID.
Darren Hillc4e266b2011-08-30 15:40:27 -0400692 * The child class MUST implement this abstract method!
693 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300694 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400695 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400696 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300697 abstract public function sess_regenerate($destroy = FALSE);
698
699 // ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400700
701 /**
702 * Get a reference to user data array
703 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300704 * Give array access to the main CI_Session object.
Darren Hillc4e266b2011-08-30 15:40:27 -0400705 * The child class MUST implement this abstract method!
706 *
Darren Hill5073a372011-08-31 13:54:19 -0400707 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400708 */
709 abstract public function &get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400710
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300711}
Darren Hillc4e266b2011-08-30 15:40:27 -0400712
713/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300714/* Location: ./system/libraries/Session/Session.php */