blob: 3b26a2f170800811b722372a220056f086674873 [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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev9ffcee62012-09-05 16:25:16 +03008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreev9ffcee62012-09-05 16:25:16 +030010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Darren Hillc4e266b2011-08-30 15:40:27 -040017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 2.0.0
Darren Hillc4e266b2011-08-30 15:40:27 -040036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Darren Hillc4e266b2011-08-30 15:40:27 -040039
Darren Hillc4e266b2011-08-30 15:40:27 -040040/**
Andrey Andreev9ffcee62012-09-05 16:25:16 +030041 * CodeIgniter Session Class
Darren Hillc4e266b2011-08-30 15:40:27 -040042 *
43 * The user interface defined by EllisLabs, now with puggable drivers to manage different storage mechanisms.
dchill420e884082012-08-11 20:10:17 -040044 * By default, the cookie session driver will load, but the 'sess_driver' config/param item (see above) can be
45 * used to specify the 'native' driver, or any other you might create.
Darren Hillc4e266b2011-08-30 15:40:27 -040046 * Once loaded, this driver setup is a drop-in replacement for the former CI_Session library, taking its place as the
47 * 'session' member of the global controller framework (e.g.: $CI->session or $this->session).
48 * 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 -040049 * 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 -040050 * Ideally, one driver is loaded and all calls go directly through the main library interface. However, any methods
51 * called through the specific driver will switch the "current" driver to itself before invoking the library method
52 * (which will then call back into the driver for low-level operations). So, alternation between two drivers can be
53 * achieved by specifying which driver to use for each call (e.g.: $this->session->native->set_userdata('foo', 'bar');
54 * $this->session->cookie->userdata('foo'); $this->session->native->unset_userdata('foo');). Notice in the previous
55 * example that the _native_ userdata value 'foo' would be set to 'bar', which would NOT be returned by the call for
56 * the _cookie_ userdata 'foo', nor would the _cookie_ value be unset by the call to unset the _native_ 'foo' value.
57 *
58 * @package CodeIgniter
59 * @subpackage Libraries
60 * @category Sessions
Andrey Andreev9ffcee62012-09-05 16:25:16 +030061 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -040062 * @link http://codeigniter.com/user_guide/libraries/sessions.html
63 */
Darren Hilla2ae6572011-09-01 07:36:26 -040064class CI_Session extends CI_Driver_Library {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030065
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020066 /**
67 * Initialization parameters
68 *
69 * @var array
70 */
Darren Hillc4e266b2011-08-30 15:40:27 -040071 public $params = array();
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020072
73 /**
Andrey Andreevc958eeb2013-07-31 14:28:50 +030074 * Valid drivers list
75 *
76 * @var array
77 */
78 public $valid_drivers = array('native', 'cookie');
79
80 /**
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020081 * Current driver in use
82 *
83 * @var string
84 */
Andrey Andreevc958eeb2013-07-31 14:28:50 +030085 public $current = NULL;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020086
87 /**
88 * User data
89 *
90 * @var array
91 */
Darren Hilla2ae6572011-09-01 07:36:26 -040092 protected $userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -040093
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020094 // ------------------------------------------------------------------------
95
Darren Hillc4e266b2011-08-30 15:40:27 -040096 const FLASHDATA_KEY = 'flash';
97 const FLASHDATA_NEW = ':new:';
98 const FLASHDATA_OLD = ':old:';
99 const FLASHDATA_EXP = ':exp:';
100 const EXPIRATION_KEY = '__expirations';
101 const TEMP_EXP_DEF = 300;
102
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200103 // ------------------------------------------------------------------------
104
Darren Hillc4e266b2011-08-30 15:40:27 -0400105 /**
Darren Hill5073a372011-08-31 13:54:19 -0400106 * CI_Session constructor
Darren Hillc4e266b2011-08-30 15:40:27 -0400107 *
108 * The constructor loads the configured driver ('sess_driver' in config.php or as a parameter), running
109 * routines in its constructor, and manages flashdata aging.
110 *
Darren Hill5073a372011-08-31 13:54:19 -0400111 * @param array Configuration parameters
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300112 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400113 */
114 public function __construct(array $params = array())
115 {
Andrey Andreevf964b162013-11-12 17:04:55 +0200116 $_config =& get_instance()->config;
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300117
118 // No sessions under CLI
Andrey Andreevf964b162013-11-12 17:04:55 +0200119 if (is_cli())
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300120 {
121 return;
122 }
123
Darren Hill5073a372011-08-31 13:54:19 -0400124 log_message('debug', 'CI_Session Class Initialized');
Darren Hillc4e266b2011-08-30 15:40:27 -0400125
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300126 // Add possible extra entries to our valid drivers list
Andrey Andreevf964b162013-11-12 17:04:55 +0200127 $drivers = isset($params['sess_valid_drivers']) ? $params['sess_valid_drivers'] : $_config->item('sess_valid_drivers');
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300128 if ( ! empty($drivers))
Darren Hillc4e266b2011-08-30 15:40:27 -0400129 {
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300130 $drivers = array_map('strtolower', (array) $drivers);
131 $this->valid_drivers = array_merge($this->valid_drivers, array_diff($drivers, $this->valid_drivers));
Darren Hillc4e266b2011-08-30 15:40:27 -0400132 }
133
134 // Get driver to load
Andrey Andreevf964b162013-11-12 17:04:55 +0200135 $driver = isset($params['sess_driver']) ? $params['sess_driver'] : $_config->item('sess_driver');
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300136 if ( ! $driver)
137 {
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300138 log_message('debug', "Session: No driver name is configured, defaulting to 'cookie'.");
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300139 $driver = 'cookie';
140 }
141
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300142 if ( ! in_array($driver, $this->valid_drivers))
Darren Hillc4e266b2011-08-30 15:40:27 -0400143 {
Andrey Andreevc958eeb2013-07-31 14:28:50 +0300144 log_message('error', 'Session: Configured driver name is not valid, aborting.');
145 return;
Darren Hillc4e266b2011-08-30 15:40:27 -0400146 }
147
148 // Save a copy of parameters in case drivers need access
149 $this->params = $params;
150
151 // Load driver and get array reference
152 $this->load_driver($driver);
Darren Hillc4e266b2011-08-30 15:40:27 -0400153
154 // Delete 'old' flashdata (from last request)
155 $this->_flashdata_sweep();
156
157 // Mark all new flashdata as old (data will be deleted before next request)
158 $this->_flashdata_mark();
159
160 // Delete expired tempdata
161 $this->_tempdata_sweep();
162
Darren Hill5073a372011-08-31 13:54:19 -0400163 log_message('debug', 'CI_Session routines successfully run');
Darren Hillc4e266b2011-08-30 15:40:27 -0400164 }
165
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300166 // ------------------------------------------------------------------------
167
Darren Hillc4e266b2011-08-30 15:40:27 -0400168 /**
169 * Loads session storage driver
170 *
Darren Hill5073a372011-08-31 13:54:19 -0400171 * @param string Driver classname
172 * @return object Loaded driver object
Darren Hillc4e266b2011-08-30 15:40:27 -0400173 */
174 public function load_driver($driver)
175 {
dchill4226429202012-07-31 10:55:07 -0400176 // Save reference to most recently loaded driver as library default and sync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400177 $this->current = parent::load_driver($driver);
dchill42b1855372012-07-31 09:32:23 -0400178 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400179 return $this->current;
180 }
181
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300182 // ------------------------------------------------------------------------
183
Darren Hillc4e266b2011-08-30 15:40:27 -0400184 /**
185 * Select default session storage driver
186 *
dchill426262d052012-11-24 18:41:13 -0500187 * @param string Driver name
Darren Hill5073a372011-08-31 13:54:19 -0400188 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400189 */
190 public function select_driver($driver)
191 {
192 // Validate driver name
dchill426262d052012-11-24 18:41:13 -0500193 $prefix = (string) get_instance()->config->item('subclass_prefix');
194 $child = strtolower(str_replace(array('CI_', $prefix, $this->lib_name.'_'), '', $driver));
195 if (in_array($child, array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400196 {
dchill42b1855372012-07-31 09:32:23 -0400197 // See if driver is loaded
dchill42b1855372012-07-31 09:32:23 -0400198 if (isset($this->$child))
Darren Hillc4e266b2011-08-30 15:40:27 -0400199 {
dchill42aee92652012-08-26 21:45:35 -0400200 // See if driver is already current
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300201 if ($this->$child !== $this->current)
202 {
dchill42aee92652012-08-26 21:45:35 -0400203 // Make driver current and sync userdata
204 $this->current = $this->$child;
205 $this->userdata =& $this->current->get_userdata();
206 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400207 }
208 else
209 {
dchill4226429202012-07-31 10:55:07 -0400210 // Load new driver
dchill42aee92652012-08-26 21:45:35 -0400211 $this->load_driver($child);
Darren Hillc4e266b2011-08-30 15:40:27 -0400212 }
213 }
214 }
215
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300216 // ------------------------------------------------------------------------
217
Darren Hillc4e266b2011-08-30 15:40:27 -0400218 /**
219 * Destroy the current session
220 *
Darren Hill5073a372011-08-31 13:54:19 -0400221 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400222 */
223 public function sess_destroy()
224 {
225 // Just call destroy on driver
226 $this->current->sess_destroy();
227 }
228
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300229 // ------------------------------------------------------------------------
230
Darren Hillc4e266b2011-08-30 15:40:27 -0400231 /**
232 * Regenerate the current session
233 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300234 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400235 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400236 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300237 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400238 {
dchill4226429202012-07-31 10:55:07 -0400239 // Call regenerate on driver and resync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400240 $this->current->sess_regenerate($destroy);
dchill4226429202012-07-31 10:55:07 -0400241 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400242 }
243
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300244 // ------------------------------------------------------------------------
245
Darren Hillc4e266b2011-08-30 15:40:27 -0400246 /**
247 * Fetch a specific item from the session array
248 *
Darren Hill5073a372011-08-31 13:54:19 -0400249 * @param string Item key
dchill42c5872252012-07-30 14:53:11 -0400250 * @return string Item value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400251 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200252 public function userdata($item = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400253 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200254 if (isset($item))
255 {
256 return isset($this->userdata[$item]) ? $this->userdata[$item] : NULL;
257 }
258
259 return isset($this->userdata) ? $this->userdata : array();
Darren Hillc4e266b2011-08-30 15:40:27 -0400260 }
261
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300262 // ------------------------------------------------------------------------
263
Darren Hillc4e266b2011-08-30 15:40:27 -0400264 /**
265 * Fetch all session data
266 *
Andrey Andreevecc260e2014-01-24 14:20:13 +0200267 * @deprecated 3.0.0 Use userdata() with no parameters instead
Darren Hillc4e266b2011-08-30 15:40:27 -0400268 * @return array User data array
269 */
270 public function all_userdata()
271 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200272 return isset($this->userdata) ? $this->userdata : array();
dchill42c5079de2012-07-23 10:53:47 -0400273 }
274
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300275 // ------------------------------------------------------------------------
276
dchill42c5079de2012-07-23 10:53:47 -0400277 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400278 * Add or change data in the "userdata" array
279 *
Darren Hill5073a372011-08-31 13:54:19 -0400280 * @param mixed Item name or array of items
281 * @param string Item value or empty string
282 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400283 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200284 public function set_userdata($newdata, $newval = '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400285 {
286 // Wrap params as array if singular
287 if (is_string($newdata))
288 {
289 $newdata = array($newdata => $newval);
290 }
291
292 // Set each name/value pair
293 if (count($newdata) > 0)
294 {
295 foreach ($newdata as $key => $val)
296 {
297 $this->userdata[$key] = $val;
298 }
299 }
300
301 // Tell driver data changed
302 $this->current->sess_save();
303 }
304
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300305 // ------------------------------------------------------------------------
306
Darren Hillc4e266b2011-08-30 15:40:27 -0400307 /**
308 * Delete a session variable from the "userdata" array
309 *
Darren Hill5073a372011-08-31 13:54:19 -0400310 * @param mixed Item name or array of item names
311 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400312 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200313 public function unset_userdata($newdata)
Darren Hillc4e266b2011-08-30 15:40:27 -0400314 {
315 // Wrap single name as array
316 if (is_string($newdata))
317 {
318 $newdata = array($newdata => '');
319 }
320
321 // Unset each item name
322 if (count($newdata) > 0)
323 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300324 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400325 {
326 unset($this->userdata[$key]);
327 }
328 }
329
330 // Tell driver data changed
331 $this->current->sess_save();
332 }
333
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300334 // ------------------------------------------------------------------------
335
Darren Hillc4e266b2011-08-30 15:40:27 -0400336 /**
337 * Determine if an item exists
338 *
Darren Hill5073a372011-08-31 13:54:19 -0400339 * @param string Item name
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300340 * @return bool
Darren Hillc4e266b2011-08-30 15:40:27 -0400341 */
342 public function has_userdata($item)
343 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400344 return isset($this->userdata[$item]);
345 }
346
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300347 // ------------------------------------------------------------------------
348
Darren Hillc4e266b2011-08-30 15:40:27 -0400349 /**
350 * Add or change flashdata, only available until the next request
351 *
Darren Hill5073a372011-08-31 13:54:19 -0400352 * @param mixed Item name or array of items
353 * @param string Item value or empty string
354 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400355 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200356 public function set_flashdata($newdata, $newval = '')
Darren Hillc4e266b2011-08-30 15:40:27 -0400357 {
358 // Wrap item as array if singular
359 if (is_string($newdata))
360 {
361 $newdata = array($newdata => $newval);
362 }
363
364 // Prepend each key name and set value
365 if (count($newdata) > 0)
366 {
367 foreach ($newdata as $key => $val)
368 {
369 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
370 $this->set_userdata($flashdata_key, $val);
371 }
372 }
373 }
374
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300375 // ------------------------------------------------------------------------
376
Darren Hillc4e266b2011-08-30 15:40:27 -0400377 /**
378 * Keeps existing flashdata available to next request.
379 *
Johnathan Croom9d9849b2012-11-24 13:03:13 -0700380 * @param mixed Item key(s)
Darren Hill5073a372011-08-31 13:54:19 -0400381 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400382 */
383 public function keep_flashdata($key)
384 {
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700385
386 if (is_array($key))
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700387 {
Johnathan Croom8d8543d2012-11-25 10:36:57 -0700388 foreach ($key as $k)
389 {
390 $this->keep_flashdata($k);
391 }
392
393 return;
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700394 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400395
Darren Hillc4e266b2011-08-30 15:40:27 -0400396 // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep()
397 // Note the function will return NULL if the $key provided cannot be found
398 $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
399 $value = $this->userdata($old_flashdata_key);
400
Darren Hill5073a372011-08-31 13:54:19 -0400401 $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
402 $this->set_userdata($new_flashdata_key, $value);
403 }
404
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300405 // ------------------------------------------------------------------------
406
Darren Hillc4e266b2011-08-30 15:40:27 -0400407 /**
408 * Fetch a specific flashdata item from the session array
409 *
410 * @param string Item key
411 * @return string
412 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200413 public function flashdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400414 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200415 if (isset($key))
416 {
417 return $this->userdata(self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key);
418 }
419
420 // Get our flashdata items from userdata
421 $out = array();
422 foreach ($this->userdata() as $key => $val)
423 {
424 if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE)
425 {
426 $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key);
427 $out[$key] = $val;
428 }
429 }
430
431 return $out;
Darren Hillc4e266b2011-08-30 15:40:27 -0400432 }
433
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300434 // ------------------------------------------------------------------------
435
Darren Hillc4e266b2011-08-30 15:40:27 -0400436 /**
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300437 * Add or change tempdata, only available until expiration
Darren Hillc4e266b2011-08-30 15:40:27 -0400438 *
439 * @param mixed Item name or array of items
440 * @param string Item value or empty string
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300441 * @param int Item lifetime in seconds or 0 for default
Darren Hillc4e266b2011-08-30 15:40:27 -0400442 * @return void
443 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200444 public function set_tempdata($newdata, $newval = '', $expire = 0)
Darren Hillc4e266b2011-08-30 15:40:27 -0400445 {
446 // Set expiration time
447 $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF);
448
449 // Wrap item as array if singular
450 if (is_string($newdata))
451 {
452 $newdata = array($newdata => $newval);
453 }
454
455 // Get or create expiration list
456 $expirations = $this->userdata(self::EXPIRATION_KEY);
dchill4277ee3fd2012-07-24 11:50:01 -0400457 if ( ! $expirations)
Darren Hillc4e266b2011-08-30 15:40:27 -0400458 {
459 $expirations = array();
460 }
461
462 // Prepend each key name and set value
463 if (count($newdata) > 0)
464 {
465 foreach ($newdata as $key => $val)
466 {
467 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
468 $expirations[$tempdata_key] = $expire;
469 $this->set_userdata($tempdata_key, $val);
470 }
471 }
472
473 // Update expiration list
474 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
475 }
476
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300477 // ------------------------------------------------------------------------
478
Darren Hillc4e266b2011-08-30 15:40:27 -0400479 /**
480 * Delete a temporary session variable from the "userdata" array
481 *
Darren Hill5073a372011-08-31 13:54:19 -0400482 * @param mixed Item name or array of item names
483 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400484 */
Andrey Andreeve6376aa2014-01-06 13:11:30 +0200485 public function unset_tempdata($newdata)
Darren Hillc4e266b2011-08-30 15:40:27 -0400486 {
487 // Get expirations list
488 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300489 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400490 {
491 // Nothing to do
492 return;
493 }
494
495 // Wrap single name as array
496 if (is_string($newdata))
497 {
498 $newdata = array($newdata => '');
499 }
500
501 // Prepend each item name and unset
502 if (count($newdata) > 0)
503 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300504 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400505 {
506 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
507 unset($expirations[$tempdata_key]);
508 $this->unset_userdata($tempdata_key);
509 }
510 }
511
512 // Update expiration list
513 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
514 }
515
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300516 // ------------------------------------------------------------------------
517
Darren Hillc4e266b2011-08-30 15:40:27 -0400518 /**
519 * Fetch a specific tempdata item from the session array
520 *
Darren Hill5073a372011-08-31 13:54:19 -0400521 * @param string Item key
522 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400523 */
Andrey Andreevecc260e2014-01-24 14:20:13 +0200524 public function tempdata($key = NULL)
Darren Hillc4e266b2011-08-30 15:40:27 -0400525 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200526 if (isset($key))
527 {
528 return $this->userdata(self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key);
529 }
530
531 // Get our tempdata items from userdata
532 $out = array();
533 foreach ($this->userdata() as $key => $val)
534 {
535 if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_EXP) !== FALSE)
536 {
537 $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_EXP, '', $key);
538 $out[$key] = $val;
539 }
540 }
541
542 return $out;
Darren Hillc4e266b2011-08-30 15:40:27 -0400543 }
544
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300545 // ------------------------------------------------------------------------
546
Darren Hillc4e266b2011-08-30 15:40:27 -0400547 /**
548 * Identifies flashdata as 'old' for removal
549 * when _flashdata_sweep() runs.
550 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400551 * @return void
552 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400553 protected function _flashdata_mark()
Darren Hillc4e266b2011-08-30 15:40:27 -0400554 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200555 foreach ($this->userdata() as $name => $value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400556 {
557 $parts = explode(self::FLASHDATA_NEW, $name);
Andrey Andreeve24eed72012-11-02 23:33:45 +0200558 if (count($parts) === 2)
Darren Hillc4e266b2011-08-30 15:40:27 -0400559 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200560 $this->set_userdata(self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1], $value);
Darren Hillc4e266b2011-08-30 15:40:27 -0400561 $this->unset_userdata($name);
562 }
563 }
564 }
565
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300566 // ------------------------------------------------------------------------
567
Darren Hillc4e266b2011-08-30 15:40:27 -0400568 /**
569 * Removes all flashdata marked as 'old'
570 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400571 * @return void
572 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400573 protected function _flashdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400574 {
Andrey Andreevecc260e2014-01-24 14:20:13 +0200575 $userdata = $this->userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300576 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400577 {
578 if (strpos($key, self::FLASHDATA_OLD))
579 {
580 $this->unset_userdata($key);
581 }
582 }
583 }
584
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300585 // ------------------------------------------------------------------------
586
Darren Hillc4e266b2011-08-30 15:40:27 -0400587 /**
588 * Removes all expired tempdata
589 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400590 * @return void
591 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400592 protected function _tempdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400593 {
594 // Get expirations list
595 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300596 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400597 {
598 // Nothing to do
599 return;
600 }
601
602 // Unset expired elements
603 $now = time();
Andrey Andreevecc260e2014-01-24 14:20:13 +0200604 $userdata = $this->userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300605 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400606 {
607 if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now)
608 {
609 unset($expirations[$key]);
610 $this->unset_userdata($key);
611 }
612 }
613
614 // Update expiration list
615 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
616 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400617
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300618}
619
620// ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400621
622/**
Darren Hill5073a372011-08-31 13:54:19 -0400623 * CI_Session_driver Class
Darren Hillc4e266b2011-08-30 15:40:27 -0400624 *
Darren Hill5073a372011-08-31 13:54:19 -0400625 * Extend this class to make a new CI_Session driver.
626 * A CI_Session driver basically manages an array of name/value pairs with some sort of storage mechanism.
627 * 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 -0400628 * session data. Then implement a save handler to write changed data to storage (sess_save), a destroy handler
629 * to remove deleted data (sess_destroy), and an access handler to expose the data (get_userdata).
Darren Hill5073a372011-08-31 13:54:19 -0400630 * Put your driver in the libraries/Session/drivers folder anywhere in the loader paths. This includes the
631 * application directory, the system directory, or any path you add with $CI->load->add_package_path().
632 * Your driver must be named CI_Session_<name>, and your filename must be Session_<name>.php,
633 * preferably also capitalized. (e.g.: CI_Session_foo in libraries/Session/drivers/Session_foo.php)
634 * 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 -0400635 * object. (e.g.: $config['sess_driver'] = 'foo'; OR $CI->load->driver('session', array('sess_driver' => 'foo')); )
636 * Already provided are the Native driver, which manages the native PHP $_SESSION array, and
637 * the Cookie driver, which manages the data in a browser cookie, with optional extra storage in a database table.
638 *
Darren Hill5073a372011-08-31 13:54:19 -0400639 * @package CodeIgniter
640 * @subpackage Libraries
Darren Hillc4e266b2011-08-30 15:40:27 -0400641 * @category Sessions
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300642 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -0400643 */
Darren Hill5073a372011-08-31 13:54:19 -0400644abstract class CI_Session_driver extends CI_Driver {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300645
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200646 /**
647 * CI Singleton
648 *
649 * @see get_instance()
650 * @var object
651 */
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300652 protected $CI;
653
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200654 // ------------------------------------------------------------------------
655
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300656 /**
657 * Constructor
658 *
659 * Gets the CI singleton, so that individual drivers
660 * don't have to do it separately.
661 *
662 * @return void
663 */
664 public function __construct()
665 {
666 $this->CI =& get_instance();
667 }
668
669 // ------------------------------------------------------------------------
670
Darren Hillc4e266b2011-08-30 15:40:27 -0400671 /**
672 * Decorate
673 *
674 * Decorates the child with the parent driver lib's methods and properties
675 *
676 * @param object Parent library object
677 * @return void
678 */
679 public function decorate($parent)
680 {
681 // Call base class decorate first
682 parent::decorate($parent);
683
dchill42c5872252012-07-30 14:53:11 -0400684 // Call initialize method now that driver has access to $this->_parent
Darren Hillc4e266b2011-08-30 15:40:27 -0400685 $this->initialize();
686 }
687
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300688 // ------------------------------------------------------------------------
689
Darren Hillc4e266b2011-08-30 15:40:27 -0400690 /**
691 * __call magic method
692 *
693 * Handles access to the parent driver library's methods
694 *
Darren Hill5073a372011-08-31 13:54:19 -0400695 * @param string Library method name
696 * @param array Method arguments (default: none)
Darren Hillc4e266b2011-08-30 15:40:27 -0400697 * @return mixed
698 */
699 public function __call($method, $args = array())
700 {
701 // Make sure the parent library uses this driver
dchill42c5872252012-07-30 14:53:11 -0400702 $this->_parent->select_driver(get_class($this));
Darren Hillc4e266b2011-08-30 15:40:27 -0400703 return parent::__call($method, $args);
704 }
705
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300706 // ------------------------------------------------------------------------
707
Darren Hillc4e266b2011-08-30 15:40:27 -0400708 /**
709 * Initialize driver
710 *
Darren Hill5073a372011-08-31 13:54:19 -0400711 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400712 */
713 protected function initialize()
714 {
715 // Overload this method to implement initialization
716 }
717
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300718 // ------------------------------------------------------------------------
719
Darren Hillc4e266b2011-08-30 15:40:27 -0400720 /**
721 * Save the session data
722 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300723 * Data in the array has changed - perform any storage synchronization
724 * necessary. The child class MUST implement this abstract method!
Darren Hillc4e266b2011-08-30 15:40:27 -0400725 *
Darren Hill5073a372011-08-31 13:54:19 -0400726 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400727 */
728 abstract public function sess_save();
729
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300730 // ------------------------------------------------------------------------
731
Darren Hillc4e266b2011-08-30 15:40:27 -0400732 /**
733 * Destroy the current session
734 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300735 * Clean up storage for this session - it has been terminated.
Darren Hillc4e266b2011-08-30 15:40:27 -0400736 * The child class MUST implement this abstract method!
737 *
Darren Hill5073a372011-08-31 13:54:19 -0400738 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400739 */
740 abstract public function sess_destroy();
741
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300742 // ------------------------------------------------------------------------
743
Darren Hillc4e266b2011-08-30 15:40:27 -0400744 /**
745 * Regenerate the current session
746 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300747 * Regenerate the session ID.
Darren Hillc4e266b2011-08-30 15:40:27 -0400748 * The child class MUST implement this abstract method!
749 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300750 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400751 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400752 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300753 abstract public function sess_regenerate($destroy = FALSE);
754
755 // ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400756
757 /**
758 * Get a reference to user data array
759 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300760 * Give array access to the main CI_Session object.
Darren Hillc4e266b2011-08-30 15:40:27 -0400761 * The child class MUST implement this abstract method!
762 *
Darren Hill5073a372011-08-31 13:54:19 -0400763 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400764 */
765 abstract public function &get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400766
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300767}
Darren Hillc4e266b2011-08-30 15:40:27 -0400768
769/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300770/* Location: ./system/libraries/Session/Session.php */