blob: 905352bb3e6ed297fbb5d5c4cf45c5ce3f32ec22 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Darren Hillc4e266b2011-08-30 15:40:27 -04002/**
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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreev9ffcee62012-09-05 16:25:16 +030022 * @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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Darren Hillc4e266b2011-08-30 15:40:27 -040028
Darren Hillc4e266b2011-08-30 15:40:27 -040029/**
Andrey Andreev9ffcee62012-09-05 16:25:16 +030030 * CodeIgniter Session Class
Darren Hillc4e266b2011-08-30 15:40:27 -040031 *
32 * The user interface defined by EllisLabs, now with puggable drivers to manage different storage mechanisms.
dchill420e884082012-08-11 20:10:17 -040033 * By default, the cookie session driver will load, but the 'sess_driver' config/param item (see above) can be
34 * used to specify the 'native' driver, or any other you might create.
Darren Hillc4e266b2011-08-30 15:40:27 -040035 * Once loaded, this driver setup is a drop-in replacement for the former CI_Session library, taking its place as the
36 * 'session' member of the global controller framework (e.g.: $CI->session or $this->session).
37 * 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 -040038 * 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 -040039 * Ideally, one driver is loaded and all calls go directly through the main library interface. However, any methods
40 * called through the specific driver will switch the "current" driver to itself before invoking the library method
41 * (which will then call back into the driver for low-level operations). So, alternation between two drivers can be
42 * achieved by specifying which driver to use for each call (e.g.: $this->session->native->set_userdata('foo', 'bar');
43 * $this->session->cookie->userdata('foo'); $this->session->native->unset_userdata('foo');). Notice in the previous
44 * example that the _native_ userdata value 'foo' would be set to 'bar', which would NOT be returned by the call for
45 * the _cookie_ userdata 'foo', nor would the _cookie_ value be unset by the call to unset the _native_ 'foo' value.
46 *
47 * @package CodeIgniter
48 * @subpackage Libraries
49 * @category Sessions
Andrey Andreev9ffcee62012-09-05 16:25:16 +030050 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -040051 * @link http://codeigniter.com/user_guide/libraries/sessions.html
52 */
Darren Hilla2ae6572011-09-01 07:36:26 -040053class CI_Session extends CI_Driver_Library {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030054
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020055 /**
56 * Initialization parameters
57 *
58 * @var array
59 */
Darren Hillc4e266b2011-08-30 15:40:27 -040060 public $params = array();
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020061
62 /**
Andrey Andreevc958eeb2013-07-31 14:28:50 +030063 * Valid drivers list
64 *
65 * @var array
66 */
67 public $valid_drivers = array('native', 'cookie');
68
69 /**
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020070 * Current driver in use
71 *
72 * @var string
73 */
Andrey Andreevc958eeb2013-07-31 14:28:50 +030074 public $current = NULL;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020075
76 /**
77 * User data
78 *
79 * @var array
80 */
Darren Hilla2ae6572011-09-01 07:36:26 -040081 protected $userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -040082
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020083 // ------------------------------------------------------------------------
84
Darren Hillc4e266b2011-08-30 15:40:27 -040085 const FLASHDATA_KEY = 'flash';
86 const FLASHDATA_NEW = ':new:';
87 const FLASHDATA_OLD = ':old:';
88 const FLASHDATA_EXP = ':exp:';
89 const EXPIRATION_KEY = '__expirations';
90 const TEMP_EXP_DEF = 300;
91
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020092 // ------------------------------------------------------------------------
93
Darren Hillc4e266b2011-08-30 15:40:27 -040094 /**
Darren Hill5073a372011-08-31 13:54:19 -040095 * CI_Session constructor
Darren Hillc4e266b2011-08-30 15:40:27 -040096 *
97 * The constructor loads the configured driver ('sess_driver' in config.php or as a parameter), running
98 * routines in its constructor, and manages flashdata aging.
99 *
Darren Hill5073a372011-08-31 13:54:19 -0400100 * @param array Configuration parameters
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300101 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400102 */
103 public function __construct(array $params = array())
104 {
Andrey Andreevf964b162013-11-12 17:04:55 +0200105 $_config =& get_instance()->config;
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300106
107 // No sessions under CLI
Andrey Andreevf964b162013-11-12 17:04:55 +0200108 if (is_cli())
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300109 {
110 return;
111 }
112
Darren Hill5073a372011-08-31 13:54:19 -0400113 log_message('debug', 'CI_Session Class Initialized');
Darren Hillc4e266b2011-08-30 15:40:27 -0400114
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300115 // Add possible extra entries to our valid drivers list
Andrey Andreevf964b162013-11-12 17:04:55 +0200116 $drivers = isset($params['sess_valid_drivers']) ? $params['sess_valid_drivers'] : $_config->item('sess_valid_drivers');
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300117 if ( ! empty($drivers))
Darren Hillc4e266b2011-08-30 15:40:27 -0400118 {
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300119 $drivers = array_map('strtolower', (array) $drivers);
120 $this->valid_drivers = array_merge($this->valid_drivers, array_diff($drivers, $this->valid_drivers));
Darren Hillc4e266b2011-08-30 15:40:27 -0400121 }
122
123 // Get driver to load
Andrey Andreevf964b162013-11-12 17:04:55 +0200124 $driver = isset($params['sess_driver']) ? $params['sess_driver'] : $_config->item('sess_driver');
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300125 if ( ! $driver)
126 {
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300127 log_message('debug', "Session: No driver name is configured, defaulting to 'cookie'.");
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300128 $driver = 'cookie';
129 }
130
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300131 if ( ! in_array($driver, $this->valid_drivers))
Darren Hillc4e266b2011-08-30 15:40:27 -0400132 {
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300133 log_message('error', 'Session: Configured driver name is not valid, aborting.');
134 return;
Darren Hillc4e266b2011-08-30 15:40:27 -0400135 }
136
137 // Save a copy of parameters in case drivers need access
138 $this->params = $params;
139
140 // Load driver and get array reference
141 $this->load_driver($driver);
Darren Hillc4e266b2011-08-30 15:40:27 -0400142
143 // Delete 'old' flashdata (from last request)
144 $this->_flashdata_sweep();
145
146 // Mark all new flashdata as old (data will be deleted before next request)
147 $this->_flashdata_mark();
148
149 // Delete expired tempdata
150 $this->_tempdata_sweep();
151
Darren Hill5073a372011-08-31 13:54:19 -0400152 log_message('debug', 'CI_Session routines successfully run');
Darren Hillc4e266b2011-08-30 15:40:27 -0400153 }
154
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300155 // ------------------------------------------------------------------------
156
Darren Hillc4e266b2011-08-30 15:40:27 -0400157 /**
158 * Loads session storage driver
159 *
Darren Hill5073a372011-08-31 13:54:19 -0400160 * @param string Driver classname
161 * @return object Loaded driver object
Darren Hillc4e266b2011-08-30 15:40:27 -0400162 */
163 public function load_driver($driver)
164 {
dchill4226429202012-07-31 10:55:07 -0400165 // Save reference to most recently loaded driver as library default and sync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400166 $this->current = parent::load_driver($driver);
dchill42b1855372012-07-31 09:32:23 -0400167 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400168 return $this->current;
169 }
170
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300171 // ------------------------------------------------------------------------
172
Darren Hillc4e266b2011-08-30 15:40:27 -0400173 /**
174 * Select default session storage driver
175 *
dchill426262d052012-11-24 18:41:13 -0500176 * @param string Driver name
Darren Hill5073a372011-08-31 13:54:19 -0400177 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400178 */
179 public function select_driver($driver)
180 {
181 // Validate driver name
dchill426262d052012-11-24 18:41:13 -0500182 $prefix = (string) get_instance()->config->item('subclass_prefix');
183 $child = strtolower(str_replace(array('CI_', $prefix, $this->lib_name.'_'), '', $driver));
184 if (in_array($child, array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400185 {
dchill42b1855372012-07-31 09:32:23 -0400186 // See if driver is loaded
dchill42b1855372012-07-31 09:32:23 -0400187 if (isset($this->$child))
Darren Hillc4e266b2011-08-30 15:40:27 -0400188 {
dchill42aee92652012-08-26 21:45:35 -0400189 // See if driver is already current
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300190 if ($this->$child !== $this->current)
191 {
dchill42aee92652012-08-26 21:45:35 -0400192 // Make driver current and sync userdata
193 $this->current = $this->$child;
194 $this->userdata =& $this->current->get_userdata();
195 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400196 }
197 else
198 {
dchill4226429202012-07-31 10:55:07 -0400199 // Load new driver
dchill42aee92652012-08-26 21:45:35 -0400200 $this->load_driver($child);
Darren Hillc4e266b2011-08-30 15:40:27 -0400201 }
202 }
203 }
204
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300205 // ------------------------------------------------------------------------
206
Darren Hillc4e266b2011-08-30 15:40:27 -0400207 /**
208 * Destroy the current session
209 *
Darren Hill5073a372011-08-31 13:54:19 -0400210 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400211 */
212 public function sess_destroy()
213 {
214 // Just call destroy on driver
215 $this->current->sess_destroy();
216 }
217
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300218 // ------------------------------------------------------------------------
219
Darren Hillc4e266b2011-08-30 15:40:27 -0400220 /**
221 * Regenerate the current session
222 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300223 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400224 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400225 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300226 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400227 {
dchill4226429202012-07-31 10:55:07 -0400228 // Call regenerate on driver and resync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400229 $this->current->sess_regenerate($destroy);
dchill4226429202012-07-31 10:55:07 -0400230 $this->userdata =& $this->current->get_userdata();
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 /**
236 * Fetch a specific item from the session array
237 *
Darren Hill5073a372011-08-31 13:54:19 -0400238 * @param string Item key
dchill42c5872252012-07-30 14:53:11 -0400239 * @return string Item value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400240 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200241 public function userdata($item = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400242 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200243 if (isset($item))
244 {
245 return isset($this->userdata[$item]) ? $this->userdata[$item] : NULL;
246 }
247
248 return isset($this->userdata) ? $this->userdata : array();
Darren Hillc4e266b2011-08-30 15:40:27 -0400249 }
250
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300251 // ------------------------------------------------------------------------
252
Darren Hillc4e266b2011-08-30 15:40:27 -0400253 /**
254 * Fetch all session data
255 *
Andrey Andreevecc260e2014-01-24 14:20:13 +0200256 * @deprecated 3.0.0 Use userdata() with no parameters instead
Darren Hillc4e266b2011-08-30 15:40:27 -0400257 * @return array User data array
258 */
259 public function all_userdata()
260 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200261 return isset($this->userdata) ? $this->userdata : array();
dchill42c5079de2012-07-23 10:53:47 -0400262 }
263
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300264 // ------------------------------------------------------------------------
265
dchill42c5079de2012-07-23 10:53:47 -0400266 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400267 * Add or change data in the "userdata" array
268 *
Darren Hill5073a372011-08-31 13:54:19 -0400269 * @param mixed Item name or array of items
270 * @param string Item value or empty string
271 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400272 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200273 public function set_userdata($newdata, $newval = '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400274 {
275 // Wrap params as array if singular
276 if (is_string($newdata))
277 {
278 $newdata = array($newdata => $newval);
279 }
280
281 // Set each name/value pair
282 if (count($newdata) > 0)
283 {
284 foreach ($newdata as $key => $val)
285 {
286 $this->userdata[$key] = $val;
287 }
288 }
289
290 // Tell driver data changed
291 $this->current->sess_save();
292 }
293
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300294 // ------------------------------------------------------------------------
295
Darren Hillc4e266b2011-08-30 15:40:27 -0400296 /**
297 * Delete a session variable from the "userdata" array
298 *
Darren Hill5073a372011-08-31 13:54:19 -0400299 * @param mixed Item name or array of item names
300 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400301 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200302 public function unset_userdata($newdata)
Darren Hillc4e266b2011-08-30 15:40:27 -0400303 {
304 // Wrap single name as array
305 if (is_string($newdata))
306 {
307 $newdata = array($newdata => '');
308 }
309
310 // Unset each item name
311 if (count($newdata) > 0)
312 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300313 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400314 {
315 unset($this->userdata[$key]);
316 }
317 }
318
319 // Tell driver data changed
320 $this->current->sess_save();
321 }
322
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300323 // ------------------------------------------------------------------------
324
Darren Hillc4e266b2011-08-30 15:40:27 -0400325 /**
326 * Determine if an item exists
327 *
Darren Hill5073a372011-08-31 13:54:19 -0400328 * @param string Item name
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300329 * @return bool
Darren Hillc4e266b2011-08-30 15:40:27 -0400330 */
331 public function has_userdata($item)
332 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400333 return isset($this->userdata[$item]);
334 }
335
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300336 // ------------------------------------------------------------------------
337
Darren Hillc4e266b2011-08-30 15:40:27 -0400338 /**
339 * Add or change flashdata, only available until the next request
340 *
Darren Hill5073a372011-08-31 13:54:19 -0400341 * @param mixed Item name or array of items
342 * @param string Item value or empty string
343 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400344 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200345 public function set_flashdata($newdata, $newval = '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400346 {
347 // Wrap item as array if singular
348 if (is_string($newdata))
349 {
350 $newdata = array($newdata => $newval);
351 }
352
353 // Prepend each key name and set value
354 if (count($newdata) > 0)
355 {
356 foreach ($newdata as $key => $val)
357 {
358 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
359 $this->set_userdata($flashdata_key, $val);
360 }
361 }
362 }
363
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300364 // ------------------------------------------------------------------------
365
Darren Hillc4e266b2011-08-30 15:40:27 -0400366 /**
367 * Keeps existing flashdata available to next request.
368 *
Johnathan Croom9d9849b2012-11-24 13:03:13 -0700369 * @param mixed Item key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400370 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400371 */
372 public function keep_flashdata($key)
373 {
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700374
375 if (is_array($key))
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700376 {
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700377 foreach ($key as $k)
378 {
379 $this->keep_flashdata($k);
380 }
381
382 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700383 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400384
Darren Hillc4e266b2011-08-30 15:40:27 -0400385 // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep()
386 // Note the function will return NULL if the $key provided cannot be found
387 $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
388 $value = $this->userdata($old_flashdata_key);
389
Darren Hill5073a372011-08-31 13:54:19 -0400390 $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
391 $this->set_userdata($new_flashdata_key, $value);
392 }
393
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300394 // ------------------------------------------------------------------------
395
Darren Hillc4e266b2011-08-30 15:40:27 -0400396 /**
397 * Fetch a specific flashdata item from the session array
398 *
399 * @param string Item key
400 * @return string
401 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200402 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400403 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200404 if (isset($key))
405 {
406 return $this->userdata(self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key);
407 }
408
409 // Get our flashdata items from userdata
410 $out = array();
411 foreach ($this->userdata() as $key => $val)
412 {
413 if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE)
414 {
415 $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key);
416 $out[$key] = $val;
417 }
418 }
419
420 return $out;
Darren Hillc4e266b2011-08-30 15:40:27 -0400421 }
422
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300423 // ------------------------------------------------------------------------
424
Darren Hillc4e266b2011-08-30 15:40:27 -0400425 /**
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300426 * Add or change tempdata, only available until expiration
Darren Hillc4e266b2011-08-30 15:40:27 -0400427 *
428 * @param mixed Item name or array of items
429 * @param string Item value or empty string
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300430 * @param int Item lifetime in seconds or 0 for default
Darren Hillc4e266b2011-08-30 15:40:27 -0400431 * @return void
432 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200433 public function set_tempdata($newdata, $newval = '', $expire = 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400434 {
435 // Set expiration time
436 $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF);
437
438 // Wrap item as array if singular
439 if (is_string($newdata))
440 {
441 $newdata = array($newdata => $newval);
442 }
443
444 // Get or create expiration list
445 $expirations = $this->userdata(self::EXPIRATION_KEY);
dchill4277ee3fd2012-07-24 11:50:01 -0400446 if ( ! $expirations)
Darren Hillc4e266b2011-08-30 15:40:27 -0400447 {
448 $expirations = array();
449 }
450
451 // Prepend each key name and set value
452 if (count($newdata) > 0)
453 {
454 foreach ($newdata as $key => $val)
455 {
456 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
457 $expirations[$tempdata_key] = $expire;
458 $this->set_userdata($tempdata_key, $val);
459 }
460 }
461
462 // Update expiration list
463 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
464 }
465
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300466 // ------------------------------------------------------------------------
467
Darren Hillc4e266b2011-08-30 15:40:27 -0400468 /**
469 * Delete a temporary session variable from the "userdata" array
470 *
Darren Hill5073a372011-08-31 13:54:19 -0400471 * @param mixed Item name or array of item names
472 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400473 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200474 public function unset_tempdata($newdata)
Darren Hillc4e266b2011-08-30 15:40:27 -0400475 {
476 // Get expirations list
477 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300478 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400479 {
480 // Nothing to do
481 return;
482 }
483
484 // Wrap single name as array
485 if (is_string($newdata))
486 {
487 $newdata = array($newdata => '');
488 }
489
490 // Prepend each item name and unset
491 if (count($newdata) > 0)
492 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300493 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400494 {
495 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
496 unset($expirations[$tempdata_key]);
497 $this->unset_userdata($tempdata_key);
498 }
499 }
500
501 // Update expiration list
502 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
503 }
504
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300505 // ------------------------------------------------------------------------
506
Darren Hillc4e266b2011-08-30 15:40:27 -0400507 /**
508 * Fetch a specific tempdata item from the session array
509 *
Darren Hill5073a372011-08-31 13:54:19 -0400510 * @param string Item key
511 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400512 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200513 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400514 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200515 if (isset($key))
516 {
517 return $this->userdata(self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key);
518 }
519
520 // Get our tempdata items from userdata
521 $out = array();
522 foreach ($this->userdata() as $key => $val)
523 {
524 if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_EXP) !== FALSE)
525 {
526 $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_EXP, '', $key);
527 $out[$key] = $val;
528 }
529 }
530
531 return $out;
Darren Hillc4e266b2011-08-30 15:40:27 -0400532 }
533
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300534 // ------------------------------------------------------------------------
535
Darren Hillc4e266b2011-08-30 15:40:27 -0400536 /**
537 * Identifies flashdata as 'old' for removal
538 * when _flashdata_sweep() runs.
539 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400540 * @return void
541 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400542 protected function _flashdata_mark()
Darren Hillc4e266b2011-08-30 15:40:27 -0400543 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200544 foreach ($this->userdata() as $name => $value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400545 {
546 $parts = explode(self::FLASHDATA_NEW, $name);
Andrey Andreeve24eed72012-11-02 23:33:45 +0200547 if (count($parts) === 2)
Darren Hillc4e266b2011-08-30 15:40:27 -0400548 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200549 $this->set_userdata(self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1], $value);
Darren Hillc4e266b2011-08-30 15:40:27 -0400550 $this->unset_userdata($name);
551 }
552 }
553 }
554
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300555 // ------------------------------------------------------------------------
556
Darren Hillc4e266b2011-08-30 15:40:27 -0400557 /**
558 * Removes all flashdata marked as 'old'
559 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400560 * @return void
561 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400562 protected function _flashdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400563 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200564 $userdata = $this->userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300565 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400566 {
567 if (strpos($key, self::FLASHDATA_OLD))
568 {
569 $this->unset_userdata($key);
570 }
571 }
572 }
573
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300574 // ------------------------------------------------------------------------
575
Darren Hillc4e266b2011-08-30 15:40:27 -0400576 /**
577 * Removes all expired tempdata
578 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400579 * @return void
580 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400581 protected function _tempdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400582 {
583 // Get expirations list
584 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300585 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400586 {
587 // Nothing to do
588 return;
589 }
590
591 // Unset expired elements
592 $now = time();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200593 $userdata = $this->userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300594 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400595 {
596 if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now)
597 {
598 unset($expirations[$key]);
599 $this->unset_userdata($key);
600 }
601 }
602
603 // Update expiration list
604 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
605 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400606
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300607}
608
609// ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400610
611/**
Darren Hill5073a372011-08-31 13:54:19 -0400612 * CI_Session_driver Class
Darren Hillc4e266b2011-08-30 15:40:27 -0400613 *
Darren Hill5073a372011-08-31 13:54:19 -0400614 * Extend this class to make a new CI_Session driver.
615 * A CI_Session driver basically manages an array of name/value pairs with some sort of storage mechanism.
616 * 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 -0400617 * session data. Then implement a save handler to write changed data to storage (sess_save), a destroy handler
618 * to remove deleted data (sess_destroy), and an access handler to expose the data (get_userdata).
Darren Hill5073a372011-08-31 13:54:19 -0400619 * Put your driver in the libraries/Session/drivers folder anywhere in the loader paths. This includes the
620 * application directory, the system directory, or any path you add with $CI->load->add_package_path().
621 * Your driver must be named CI_Session_<name>, and your filename must be Session_<name>.php,
622 * preferably also capitalized. (e.g.: CI_Session_foo in libraries/Session/drivers/Session_foo.php)
623 * 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 -0400624 * object. (e.g.: $config['sess_driver'] = 'foo'; OR $CI->load->driver('session', array('sess_driver' => 'foo')); )
625 * Already provided are the Native driver, which manages the native PHP $_SESSION array, and
626 * the Cookie driver, which manages the data in a browser cookie, with optional extra storage in a database table.
627 *
Darren Hill5073a372011-08-31 13:54:19 -0400628 * @package CodeIgniter
629 * @subpackage Libraries
Darren Hillc4e266b2011-08-30 15:40:27 -0400630 * @category Sessions
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300631 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -0400632 */
Darren Hill5073a372011-08-31 13:54:19 -0400633abstract class CI_Session_driver extends CI_Driver {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300634
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200635 /**
636 * CI Singleton
637 *
638 * @see get_instance()
639 * @var object
640 */
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300641 protected $CI;
642
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200643 // ------------------------------------------------------------------------
644
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300645 /**
646 * Constructor
647 *
648 * Gets the CI singleton, so that individual drivers
649 * don't have to do it separately.
650 *
651 * @return void
652 */
653 public function __construct()
654 {
655 $this->CI =& get_instance();
656 }
657
658 // ------------------------------------------------------------------------
659
Darren Hillc4e266b2011-08-30 15:40:27 -0400660 /**
661 * Decorate
662 *
663 * Decorates the child with the parent driver lib's methods and properties
664 *
665 * @param object Parent library object
666 * @return void
667 */
668 public function decorate($parent)
669 {
670 // Call base class decorate first
671 parent::decorate($parent);
672
dchill42c5872252012-07-30 14:53:11 -0400673 // Call initialize method now that driver has access to $this->_parent
Darren Hillc4e266b2011-08-30 15:40:27 -0400674 $this->initialize();
675 }
676
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300677 // ------------------------------------------------------------------------
678
Darren Hillc4e266b2011-08-30 15:40:27 -0400679 /**
680 * __call magic method
681 *
682 * Handles access to the parent driver library's methods
683 *
Darren Hill5073a372011-08-31 13:54:19 -0400684 * @param string Library method name
685 * @param array Method arguments (default: none)
Darren Hillc4e266b2011-08-30 15:40:27 -0400686 * @return mixed
687 */
688 public function __call($method, $args = array())
689 {
690 // Make sure the parent library uses this driver
dchill42c5872252012-07-30 14:53:11 -0400691 $this->_parent->select_driver(get_class($this));
Darren Hillc4e266b2011-08-30 15:40:27 -0400692 return parent::__call($method, $args);
693 }
694
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300695 // ------------------------------------------------------------------------
696
Darren Hillc4e266b2011-08-30 15:40:27 -0400697 /**
698 * Initialize driver
699 *
Darren Hill5073a372011-08-31 13:54:19 -0400700 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400701 */
702 protected function initialize()
703 {
704 // Overload this method to implement initialization
705 }
706
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300707 // ------------------------------------------------------------------------
708
Darren Hillc4e266b2011-08-30 15:40:27 -0400709 /**
710 * Save the session data
711 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300712 * Data in the array has changed - perform any storage synchronization
713 * necessary. The child class MUST implement this abstract method!
Darren Hillc4e266b2011-08-30 15:40:27 -0400714 *
Darren Hill5073a372011-08-31 13:54:19 -0400715 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400716 */
717 abstract public function sess_save();
718
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300719 // ------------------------------------------------------------------------
720
Darren Hillc4e266b2011-08-30 15:40:27 -0400721 /**
722 * Destroy the current session
723 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300724 * Clean up storage for this session - it has been terminated.
Darren Hillc4e266b2011-08-30 15:40:27 -0400725 * The child class MUST implement this abstract method!
726 *
Darren Hill5073a372011-08-31 13:54:19 -0400727 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400728 */
729 abstract public function sess_destroy();
730
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300731 // ------------------------------------------------------------------------
732
Darren Hillc4e266b2011-08-30 15:40:27 -0400733 /**
734 * Regenerate the current session
735 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300736 * Regenerate the session ID.
Darren Hillc4e266b2011-08-30 15:40:27 -0400737 * The child class MUST implement this abstract method!
738 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300739 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400740 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400741 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300742 abstract public function sess_regenerate($destroy = FALSE);
743
744 // ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400745
746 /**
747 * Get a reference to user data array
748 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300749 * Give array access to the main CI_Session object.
Darren Hillc4e266b2011-08-30 15:40:27 -0400750 * The child class MUST implement this abstract method!
751 *
Darren Hill5073a372011-08-31 13:54:19 -0400752 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400753 */
754 abstract public function &get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400755
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300756}
Darren Hillc4e266b2011-08-30 15:40:27 -0400757
758/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300759/* Location: ./system/libraries/Session/Session.php */