blob: 81d63c304f71f13f640a290fe866b0389efeadf7 [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
Andrey Andreevc5536aa2012-11-01 17:33:58 +020021 * @copyright Copyright (c) 2008 - 2012, 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 /**
63 * Current driver in use
64 *
65 * @var string
66 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +030067 protected $current = NULL;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020068
69 /**
70 * User data
71 *
72 * @var array
73 */
Darren Hilla2ae6572011-09-01 07:36:26 -040074 protected $userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -040075
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020076 // ------------------------------------------------------------------------
77
Darren Hillc4e266b2011-08-30 15:40:27 -040078 const FLASHDATA_KEY = 'flash';
79 const FLASHDATA_NEW = ':new:';
80 const FLASHDATA_OLD = ':old:';
81 const FLASHDATA_EXP = ':exp:';
82 const EXPIRATION_KEY = '__expirations';
83 const TEMP_EXP_DEF = 300;
84
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020085 // ------------------------------------------------------------------------
86
Darren Hillc4e266b2011-08-30 15:40:27 -040087 /**
Darren Hill5073a372011-08-31 13:54:19 -040088 * CI_Session constructor
Darren Hillc4e266b2011-08-30 15:40:27 -040089 *
90 * The constructor loads the configured driver ('sess_driver' in config.php or as a parameter), running
91 * routines in its constructor, and manages flashdata aging.
92 *
Darren Hill5073a372011-08-31 13:54:19 -040093 * @param array Configuration parameters
Andrey Andreev2e3e2302012-10-09 15:52:34 +030094 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -040095 */
96 public function __construct(array $params = array())
97 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030098 $CI =& get_instance();
99
100 // No sessions under CLI
101 if ($CI->input->is_cli_request())
102 {
103 return;
104 }
105
Darren Hill5073a372011-08-31 13:54:19 -0400106 log_message('debug', 'CI_Session Class Initialized');
Darren Hillc4e266b2011-08-30 15:40:27 -0400107
108 // Get valid drivers list
dchill4226429202012-07-31 10:55:07 -0400109 $this->valid_drivers = array(
110 'Session_native',
vkeranov3bb40292012-10-27 18:47:26 +0300111 'Session_cookie'
dchill4226429202012-07-31 10:55:07 -0400112 );
Darren Hillc4e266b2011-08-30 15:40:27 -0400113 $key = 'sess_valid_drivers';
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300114 $drivers = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -0400115 if ($drivers)
116 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300117 is_array($drivers) OR $drivers = array($drivers);
Darren Hillc4e266b2011-08-30 15:40:27 -0400118
119 // Add driver names to valid list
120 foreach ($drivers as $driver)
121 {
dchill4277ee3fd2012-07-24 11:50:01 -0400122 if ( ! in_array(strtolower($driver), array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400123 {
124 $this->valid_drivers[] = $driver;
125 }
126 }
127 }
128
129 // Get driver to load
130 $key = 'sess_driver';
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300131 $driver = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
132 if ( ! $driver)
133 {
134 $driver = 'cookie';
135 }
136
dchill4277ee3fd2012-07-24 11:50:01 -0400137 if ( ! in_array('session_'.strtolower($driver), array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400138 {
139 $this->valid_drivers[] = 'Session_'.$driver;
140 }
141
142 // Save a copy of parameters in case drivers need access
143 $this->params = $params;
144
145 // Load driver and get array reference
146 $this->load_driver($driver);
Darren Hillc4e266b2011-08-30 15:40:27 -0400147
148 // Delete 'old' flashdata (from last request)
149 $this->_flashdata_sweep();
150
151 // Mark all new flashdata as old (data will be deleted before next request)
152 $this->_flashdata_mark();
153
154 // Delete expired tempdata
155 $this->_tempdata_sweep();
156
Darren Hill5073a372011-08-31 13:54:19 -0400157 log_message('debug', 'CI_Session routines successfully run');
Darren Hillc4e266b2011-08-30 15:40:27 -0400158 }
159
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300160 // ------------------------------------------------------------------------
161
Darren Hillc4e266b2011-08-30 15:40:27 -0400162 /**
163 * Loads session storage driver
164 *
Darren Hill5073a372011-08-31 13:54:19 -0400165 * @param string Driver classname
166 * @return object Loaded driver object
Darren Hillc4e266b2011-08-30 15:40:27 -0400167 */
168 public function load_driver($driver)
169 {
dchill4226429202012-07-31 10:55:07 -0400170 // Save reference to most recently loaded driver as library default and sync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400171 $this->current = parent::load_driver($driver);
dchill42b1855372012-07-31 09:32:23 -0400172 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400173 return $this->current;
174 }
175
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300176 // ------------------------------------------------------------------------
177
Darren Hillc4e266b2011-08-30 15:40:27 -0400178 /**
179 * Select default session storage driver
180 *
Darren Hill5073a372011-08-31 13:54:19 -0400181 * @param string Driver classname
182 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400183 */
184 public function select_driver($driver)
185 {
186 // Validate driver name
dchill42c5872252012-07-30 14:53:11 -0400187 $lowername = strtolower(str_replace('CI_', '', $driver));
Darren Hillc4e266b2011-08-30 15:40:27 -0400188 if (in_array($lowername, array_map('strtolower', $this->valid_drivers)))
189 {
dchill42b1855372012-07-31 09:32:23 -0400190 // See if driver is loaded
191 $child = str_replace($this->lib_name.'_', '', $driver);
192 if (isset($this->$child))
Darren Hillc4e266b2011-08-30 15:40:27 -0400193 {
dchill42aee92652012-08-26 21:45:35 -0400194 // See if driver is already current
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300195 if ($this->$child !== $this->current)
196 {
dchill42aee92652012-08-26 21:45:35 -0400197 // Make driver current and sync userdata
198 $this->current = $this->$child;
199 $this->userdata =& $this->current->get_userdata();
200 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400201 }
202 else
203 {
dchill4226429202012-07-31 10:55:07 -0400204 // Load new driver
dchill42aee92652012-08-26 21:45:35 -0400205 $this->load_driver($child);
Darren Hillc4e266b2011-08-30 15:40:27 -0400206 }
207 }
208 }
209
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300210 // ------------------------------------------------------------------------
211
Darren Hillc4e266b2011-08-30 15:40:27 -0400212 /**
213 * Destroy the current session
214 *
Darren Hill5073a372011-08-31 13:54:19 -0400215 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400216 */
217 public function sess_destroy()
218 {
219 // Just call destroy on driver
220 $this->current->sess_destroy();
221 }
222
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300223 // ------------------------------------------------------------------------
224
Darren Hillc4e266b2011-08-30 15:40:27 -0400225 /**
226 * Regenerate the current session
227 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300228 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400229 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400230 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300231 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400232 {
dchill4226429202012-07-31 10:55:07 -0400233 // Call regenerate on driver and resync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400234 $this->current->sess_regenerate($destroy);
dchill4226429202012-07-31 10:55:07 -0400235 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400236 }
237
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300238 // ------------------------------------------------------------------------
239
Darren Hillc4e266b2011-08-30 15:40:27 -0400240 /**
241 * Fetch a specific item from the session array
242 *
Darren Hill5073a372011-08-31 13:54:19 -0400243 * @param string Item key
dchill42c5872252012-07-30 14:53:11 -0400244 * @return string Item value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400245 */
246 public function userdata($item)
247 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300248 return isset($this->userdata[$item]) ? $this->userdata[$item] : NULL;
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 *
256 * @return array User data array
257 */
258 public function all_userdata()
259 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300260 return isset($this->userdata) ? $this->userdata : NULL;
Darren Hillc4e266b2011-08-30 15:40:27 -0400261 }
262
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300263 // ------------------------------------------------------------------------
264
Darren Hillc4e266b2011-08-30 15:40:27 -0400265 /**
dchill42c5079de2012-07-23 10:53:47 -0400266 * Fetch all flashdata
267 *
vkeranov3bb40292012-10-27 18:47:26 +0300268 * @return array Flash data array
dchill42c5079de2012-07-23 10:53:47 -0400269 */
270 public function all_flashdata()
271 {
272 $out = array();
273
274 // loop through all userdata
275 foreach ($this->all_userdata() as $key => $val)
276 {
dchill42c5872252012-07-30 14:53:11 -0400277 // if it contains flashdata, add it
dchill42c5079de2012-07-23 10:53:47 -0400278 if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE)
279 {
dchill4226429202012-07-31 10:55:07 -0400280 $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key);
dchill42c5079de2012-07-23 10:53:47 -0400281 $out[$key] = $val;
282 }
283 }
284 return $out;
285 }
286
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300287 // ------------------------------------------------------------------------
288
dchill42c5079de2012-07-23 10:53:47 -0400289 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400290 * Add or change data in the "userdata" array
291 *
Darren Hill5073a372011-08-31 13:54:19 -0400292 * @param mixed Item name or array of items
293 * @param string Item value or empty string
294 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400295 */
296 public function set_userdata($newdata = array(), $newval = '')
297 {
298 // Wrap params as array if singular
299 if (is_string($newdata))
300 {
301 $newdata = array($newdata => $newval);
302 }
303
304 // Set each name/value pair
305 if (count($newdata) > 0)
306 {
307 foreach ($newdata as $key => $val)
308 {
309 $this->userdata[$key] = $val;
310 }
311 }
312
313 // Tell driver data changed
314 $this->current->sess_save();
315 }
316
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300317 // ------------------------------------------------------------------------
318
Darren Hillc4e266b2011-08-30 15:40:27 -0400319 /**
320 * Delete a session variable from the "userdata" array
321 *
Darren Hill5073a372011-08-31 13:54:19 -0400322 * @param mixed Item name or array of item names
323 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400324 */
325 public function unset_userdata($newdata = array())
326 {
327 // Wrap single name as array
328 if (is_string($newdata))
329 {
330 $newdata = array($newdata => '');
331 }
332
333 // Unset each item name
334 if (count($newdata) > 0)
335 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300336 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400337 {
338 unset($this->userdata[$key]);
339 }
340 }
341
342 // Tell driver data changed
343 $this->current->sess_save();
344 }
345
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300346 // ------------------------------------------------------------------------
347
Darren Hillc4e266b2011-08-30 15:40:27 -0400348 /**
349 * Determine if an item exists
350 *
Darren Hill5073a372011-08-31 13:54:19 -0400351 * @param string Item name
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300352 * @return bool
Darren Hillc4e266b2011-08-30 15:40:27 -0400353 */
354 public function has_userdata($item)
355 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400356 return isset($this->userdata[$item]);
357 }
358
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300359 // ------------------------------------------------------------------------
360
Darren Hillc4e266b2011-08-30 15:40:27 -0400361 /**
362 * Add or change flashdata, only available until the next request
363 *
Darren Hill5073a372011-08-31 13:54:19 -0400364 * @param mixed Item name or array of items
365 * @param string Item value or empty string
366 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400367 */
368 public function set_flashdata($newdata = array(), $newval = '')
369 {
370 // Wrap item as array if singular
371 if (is_string($newdata))
372 {
373 $newdata = array($newdata => $newval);
374 }
375
376 // Prepend each key name and set value
377 if (count($newdata) > 0)
378 {
379 foreach ($newdata as $key => $val)
380 {
381 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
382 $this->set_userdata($flashdata_key, $val);
383 }
384 }
385 }
386
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300387 // ------------------------------------------------------------------------
388
Darren Hillc4e266b2011-08-30 15:40:27 -0400389 /**
390 * Keeps existing flashdata available to next request.
391 *
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700392 * @param mixed Item key
Darren Hill5073a372011-08-31 13:54:19 -0400393 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400394 */
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700395 public function keep_flashdata($data)
Darren Hillc4e266b2011-08-30 15:40:27 -0400396 {
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700397 // Wrap item as array if singular
398 if (is_string($data))
399 {
400 $data = array($data);
401 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400402
Johnathan Croom4beca5c2012-11-23 18:32:46 -0700403 foreach($data as $key)
404 {
405 // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep()
406 // Note the function will return NULL if the $key provided cannot be found
407 $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
408 $value = $this->userdata($old_flashdata_key);
409
410 $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
411 $this->set_userdata($new_flashdata_key, $value);
412 }
413
Darren Hillc4e266b2011-08-30 15:40:27 -0400414 }
415
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300416 // ------------------------------------------------------------------------
417
Darren Hillc4e266b2011-08-30 15:40:27 -0400418 /**
419 * Fetch a specific flashdata item from the session array
420 *
Darren Hill5073a372011-08-31 13:54:19 -0400421 * @param string Item key
422 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400423 */
424 public function flashdata($key)
425 {
426 // Prepend key and retrieve value
427 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
428 return $this->userdata($flashdata_key);
429 }
430
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300431 // ------------------------------------------------------------------------
432
Darren Hillc4e266b2011-08-30 15:40:27 -0400433 /**
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300434 * Add or change tempdata, only available until expiration
Darren Hillc4e266b2011-08-30 15:40:27 -0400435 *
Darren Hill5073a372011-08-31 13:54:19 -0400436 * @param mixed Item name or array of items
437 * @param string Item value or empty string
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300438 * @param int Item lifetime in seconds or 0 for default
Darren Hill5073a372011-08-31 13:54:19 -0400439 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400440 */
441 public function set_tempdata($newdata = array(), $newval = '', $expire = 0)
442 {
443 // Set expiration time
444 $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF);
445
446 // Wrap item as array if singular
447 if (is_string($newdata))
448 {
449 $newdata = array($newdata => $newval);
450 }
451
452 // Get or create expiration list
453 $expirations = $this->userdata(self::EXPIRATION_KEY);
dchill4277ee3fd2012-07-24 11:50:01 -0400454 if ( ! $expirations)
Darren Hillc4e266b2011-08-30 15:40:27 -0400455 {
456 $expirations = array();
457 }
458
459 // Prepend each key name and set value
460 if (count($newdata) > 0)
461 {
462 foreach ($newdata as $key => $val)
463 {
464 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
465 $expirations[$tempdata_key] = $expire;
466 $this->set_userdata($tempdata_key, $val);
467 }
468 }
469
470 // Update expiration list
471 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
472 }
473
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300474 // ------------------------------------------------------------------------
475
Darren Hillc4e266b2011-08-30 15:40:27 -0400476 /**
477 * Delete a temporary session variable from the "userdata" array
478 *
Darren Hill5073a372011-08-31 13:54:19 -0400479 * @param mixed Item name or array of item names
480 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400481 */
482 public function unset_tempdata($newdata = array())
483 {
484 // Get expirations list
485 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300486 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400487 {
488 // Nothing to do
489 return;
490 }
491
492 // Wrap single name as array
493 if (is_string($newdata))
494 {
495 $newdata = array($newdata => '');
496 }
497
498 // Prepend each item name and unset
499 if (count($newdata) > 0)
500 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300501 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400502 {
503 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
504 unset($expirations[$tempdata_key]);
505 $this->unset_userdata($tempdata_key);
506 }
507 }
508
509 // Update expiration list
510 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
511 }
512
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300513 // ------------------------------------------------------------------------
514
Darren Hillc4e266b2011-08-30 15:40:27 -0400515 /**
516 * Fetch a specific tempdata item from the session array
517 *
Darren Hill5073a372011-08-31 13:54:19 -0400518 * @param string Item key
519 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400520 */
521 public function tempdata($key)
522 {
523 // Prepend key and return value
524 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
525 return $this->userdata($tempdata_key);
526 }
527
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300528 // ------------------------------------------------------------------------
529
Darren Hillc4e266b2011-08-30 15:40:27 -0400530 /**
531 * Identifies flashdata as 'old' for removal
532 * when _flashdata_sweep() runs.
533 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400534 * @return void
535 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400536 protected function _flashdata_mark()
Darren Hillc4e266b2011-08-30 15:40:27 -0400537 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300538 foreach ($this->all_userdata() as $name => $value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400539 {
540 $parts = explode(self::FLASHDATA_NEW, $name);
Andrey Andreeve24eed72012-11-02 23:33:45 +0200541 if (count($parts) === 2)
Darren Hillc4e266b2011-08-30 15:40:27 -0400542 {
543 $new_name = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1];
544 $this->set_userdata($new_name, $value);
545 $this->unset_userdata($name);
546 }
547 }
548 }
549
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300550 // ------------------------------------------------------------------------
551
Darren Hillc4e266b2011-08-30 15:40:27 -0400552 /**
553 * Removes all flashdata marked as 'old'
554 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400555 * @return void
556 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400557 protected function _flashdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400558 {
559 $userdata = $this->all_userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300560 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400561 {
562 if (strpos($key, self::FLASHDATA_OLD))
563 {
564 $this->unset_userdata($key);
565 }
566 }
567 }
568
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300569 // ------------------------------------------------------------------------
570
Darren Hillc4e266b2011-08-30 15:40:27 -0400571 /**
572 * Removes all expired tempdata
573 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400574 * @return void
575 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400576 protected function _tempdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400577 {
578 // Get expirations list
579 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300580 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400581 {
582 // Nothing to do
583 return;
584 }
585
586 // Unset expired elements
587 $now = time();
588 $userdata = $this->all_userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300589 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400590 {
591 if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now)
592 {
593 unset($expirations[$key]);
594 $this->unset_userdata($key);
595 }
596 }
597
598 // Update expiration list
599 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
600 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400601
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300602}
603
604// ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400605
606/**
Darren Hill5073a372011-08-31 13:54:19 -0400607 * CI_Session_driver Class
Darren Hillc4e266b2011-08-30 15:40:27 -0400608 *
Darren Hill5073a372011-08-31 13:54:19 -0400609 * Extend this class to make a new CI_Session driver.
610 * A CI_Session driver basically manages an array of name/value pairs with some sort of storage mechanism.
611 * 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 -0400612 * session data. Then implement a save handler to write changed data to storage (sess_save), a destroy handler
613 * to remove deleted data (sess_destroy), and an access handler to expose the data (get_userdata).
Darren Hill5073a372011-08-31 13:54:19 -0400614 * Put your driver in the libraries/Session/drivers folder anywhere in the loader paths. This includes the
615 * application directory, the system directory, or any path you add with $CI->load->add_package_path().
616 * Your driver must be named CI_Session_<name>, and your filename must be Session_<name>.php,
617 * preferably also capitalized. (e.g.: CI_Session_foo in libraries/Session/drivers/Session_foo.php)
618 * 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 -0400619 * object. (e.g.: $config['sess_driver'] = 'foo'; OR $CI->load->driver('session', array('sess_driver' => 'foo')); )
620 * Already provided are the Native driver, which manages the native PHP $_SESSION array, and
621 * the Cookie driver, which manages the data in a browser cookie, with optional extra storage in a database table.
622 *
Darren Hill5073a372011-08-31 13:54:19 -0400623 * @package CodeIgniter
624 * @subpackage Libraries
Darren Hillc4e266b2011-08-30 15:40:27 -0400625 * @category Sessions
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300626 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -0400627 */
Darren Hill5073a372011-08-31 13:54:19 -0400628abstract class CI_Session_driver extends CI_Driver {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300629
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200630 /**
631 * CI Singleton
632 *
633 * @see get_instance()
634 * @var object
635 */
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300636 protected $CI;
637
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200638 // ------------------------------------------------------------------------
639
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300640 /**
641 * Constructor
642 *
643 * Gets the CI singleton, so that individual drivers
644 * don't have to do it separately.
645 *
646 * @return void
647 */
648 public function __construct()
649 {
650 $this->CI =& get_instance();
651 }
652
653 // ------------------------------------------------------------------------
654
Darren Hillc4e266b2011-08-30 15:40:27 -0400655 /**
656 * Decorate
657 *
658 * Decorates the child with the parent driver lib's methods and properties
659 *
660 * @param object Parent library object
661 * @return void
662 */
663 public function decorate($parent)
664 {
665 // Call base class decorate first
666 parent::decorate($parent);
667
dchill42c5872252012-07-30 14:53:11 -0400668 // Call initialize method now that driver has access to $this->_parent
Darren Hillc4e266b2011-08-30 15:40:27 -0400669 $this->initialize();
670 }
671
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300672 // ------------------------------------------------------------------------
673
Darren Hillc4e266b2011-08-30 15:40:27 -0400674 /**
675 * __call magic method
676 *
677 * Handles access to the parent driver library's methods
678 *
Darren Hill5073a372011-08-31 13:54:19 -0400679 * @param string Library method name
680 * @param array Method arguments (default: none)
Darren Hillc4e266b2011-08-30 15:40:27 -0400681 * @return mixed
682 */
683 public function __call($method, $args = array())
684 {
685 // Make sure the parent library uses this driver
dchill42c5872252012-07-30 14:53:11 -0400686 $this->_parent->select_driver(get_class($this));
Darren Hillc4e266b2011-08-30 15:40:27 -0400687 return parent::__call($method, $args);
688 }
689
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300690 // ------------------------------------------------------------------------
691
Darren Hillc4e266b2011-08-30 15:40:27 -0400692 /**
693 * Initialize driver
694 *
Darren Hill5073a372011-08-31 13:54:19 -0400695 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400696 */
697 protected function initialize()
698 {
699 // Overload this method to implement initialization
700 }
701
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300702 // ------------------------------------------------------------------------
703
Darren Hillc4e266b2011-08-30 15:40:27 -0400704 /**
705 * Save the session data
706 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300707 * Data in the array has changed - perform any storage synchronization
708 * necessary. The child class MUST implement this abstract method!
Darren Hillc4e266b2011-08-30 15:40:27 -0400709 *
Darren Hill5073a372011-08-31 13:54:19 -0400710 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400711 */
712 abstract public function sess_save();
713
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300714 // ------------------------------------------------------------------------
715
Darren Hillc4e266b2011-08-30 15:40:27 -0400716 /**
717 * Destroy the current session
718 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300719 * Clean up storage for this session - it has been terminated.
Darren Hillc4e266b2011-08-30 15:40:27 -0400720 * The child class MUST implement this abstract method!
721 *
Darren Hill5073a372011-08-31 13:54:19 -0400722 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400723 */
724 abstract public function sess_destroy();
725
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300726 // ------------------------------------------------------------------------
727
Darren Hillc4e266b2011-08-30 15:40:27 -0400728 /**
729 * Regenerate the current session
730 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300731 * Regenerate the session ID.
Darren Hillc4e266b2011-08-30 15:40:27 -0400732 * The child class MUST implement this abstract method!
733 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300734 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400735 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400736 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300737 abstract public function sess_regenerate($destroy = FALSE);
738
739 // ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400740
741 /**
742 * Get a reference to user data array
743 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300744 * Give array access to the main CI_Session object.
Darren Hillc4e266b2011-08-30 15:40:27 -0400745 * The child class MUST implement this abstract method!
746 *
Darren Hill5073a372011-08-31 13:54:19 -0400747 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400748 */
749 abstract public function &get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400750
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300751}
Darren Hillc4e266b2011-08-30 15:40:27 -0400752
753/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300754/* Location: ./system/libraries/Session/Session.php */