blob: fb5b9fdd3652d1b05abd7a04689d63615864304e [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
Darren Hillc4e266b2011-08-30 15:40:27 -040055 public $params = array();
Andrey Andreev9ffcee62012-09-05 16:25:16 +030056 protected $current = NULL;
Darren Hilla2ae6572011-09-01 07:36:26 -040057 protected $userdata = array();
Darren Hillc4e266b2011-08-30 15:40:27 -040058
59 const FLASHDATA_KEY = 'flash';
60 const FLASHDATA_NEW = ':new:';
61 const FLASHDATA_OLD = ':old:';
62 const FLASHDATA_EXP = ':exp:';
63 const EXPIRATION_KEY = '__expirations';
64 const TEMP_EXP_DEF = 300;
65
66 /**
Darren Hill5073a372011-08-31 13:54:19 -040067 * CI_Session constructor
Darren Hillc4e266b2011-08-30 15:40:27 -040068 *
69 * The constructor loads the configured driver ('sess_driver' in config.php or as a parameter), running
70 * routines in its constructor, and manages flashdata aging.
71 *
Darren Hill5073a372011-08-31 13:54:19 -040072 * @param array Configuration parameters
Andrey Andreev2e3e2302012-10-09 15:52:34 +030073 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -040074 */
75 public function __construct(array $params = array())
76 {
Andrey Andreev2e3e2302012-10-09 15:52:34 +030077 $CI =& get_instance();
78
79 // No sessions under CLI
80 if ($CI->input->is_cli_request())
81 {
82 return;
83 }
84
Darren Hill5073a372011-08-31 13:54:19 -040085 log_message('debug', 'CI_Session Class Initialized');
Darren Hillc4e266b2011-08-30 15:40:27 -040086
87 // Get valid drivers list
dchill4226429202012-07-31 10:55:07 -040088 $this->valid_drivers = array(
89 'Session_native',
vkeranov3bb40292012-10-27 18:47:26 +030090 'Session_cookie'
dchill4226429202012-07-31 10:55:07 -040091 );
Darren Hillc4e266b2011-08-30 15:40:27 -040092 $key = 'sess_valid_drivers';
Andrey Andreev9ffcee62012-09-05 16:25:16 +030093 $drivers = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
Darren Hillc4e266b2011-08-30 15:40:27 -040094 if ($drivers)
95 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +030096 is_array($drivers) OR $drivers = array($drivers);
Darren Hillc4e266b2011-08-30 15:40:27 -040097
98 // Add driver names to valid list
99 foreach ($drivers as $driver)
100 {
dchill4277ee3fd2012-07-24 11:50:01 -0400101 if ( ! in_array(strtolower($driver), array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400102 {
103 $this->valid_drivers[] = $driver;
104 }
105 }
106 }
107
108 // Get driver to load
109 $key = 'sess_driver';
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300110 $driver = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
111 if ( ! $driver)
112 {
113 $driver = 'cookie';
114 }
115
dchill4277ee3fd2012-07-24 11:50:01 -0400116 if ( ! in_array('session_'.strtolower($driver), array_map('strtolower', $this->valid_drivers)))
Darren Hillc4e266b2011-08-30 15:40:27 -0400117 {
118 $this->valid_drivers[] = 'Session_'.$driver;
119 }
120
121 // Save a copy of parameters in case drivers need access
122 $this->params = $params;
123
124 // Load driver and get array reference
125 $this->load_driver($driver);
Darren Hillc4e266b2011-08-30 15:40:27 -0400126
127 // Delete 'old' flashdata (from last request)
128 $this->_flashdata_sweep();
129
130 // Mark all new flashdata as old (data will be deleted before next request)
131 $this->_flashdata_mark();
132
133 // Delete expired tempdata
134 $this->_tempdata_sweep();
135
Darren Hill5073a372011-08-31 13:54:19 -0400136 log_message('debug', 'CI_Session routines successfully run');
Darren Hillc4e266b2011-08-30 15:40:27 -0400137 }
138
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300139 // ------------------------------------------------------------------------
140
Darren Hillc4e266b2011-08-30 15:40:27 -0400141 /**
142 * Loads session storage driver
143 *
Darren Hill5073a372011-08-31 13:54:19 -0400144 * @param string Driver classname
145 * @return object Loaded driver object
Darren Hillc4e266b2011-08-30 15:40:27 -0400146 */
147 public function load_driver($driver)
148 {
dchill4226429202012-07-31 10:55:07 -0400149 // Save reference to most recently loaded driver as library default and sync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400150 $this->current = parent::load_driver($driver);
dchill42b1855372012-07-31 09:32:23 -0400151 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400152 return $this->current;
153 }
154
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300155 // ------------------------------------------------------------------------
156
Darren Hillc4e266b2011-08-30 15:40:27 -0400157 /**
158 * Select default session storage driver
159 *
Darren Hill5073a372011-08-31 13:54:19 -0400160 * @param string Driver classname
161 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400162 */
163 public function select_driver($driver)
164 {
165 // Validate driver name
dchill42c5872252012-07-30 14:53:11 -0400166 $lowername = strtolower(str_replace('CI_', '', $driver));
Darren Hillc4e266b2011-08-30 15:40:27 -0400167 if (in_array($lowername, array_map('strtolower', $this->valid_drivers)))
168 {
dchill42b1855372012-07-31 09:32:23 -0400169 // See if driver is loaded
170 $child = str_replace($this->lib_name.'_', '', $driver);
171 if (isset($this->$child))
Darren Hillc4e266b2011-08-30 15:40:27 -0400172 {
dchill42aee92652012-08-26 21:45:35 -0400173 // See if driver is already current
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300174 if ($this->$child !== $this->current)
175 {
dchill42aee92652012-08-26 21:45:35 -0400176 // Make driver current and sync userdata
177 $this->current = $this->$child;
178 $this->userdata =& $this->current->get_userdata();
179 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400180 }
181 else
182 {
dchill4226429202012-07-31 10:55:07 -0400183 // Load new driver
dchill42aee92652012-08-26 21:45:35 -0400184 $this->load_driver($child);
Darren Hillc4e266b2011-08-30 15:40:27 -0400185 }
186 }
187 }
188
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300189 // ------------------------------------------------------------------------
190
Darren Hillc4e266b2011-08-30 15:40:27 -0400191 /**
192 * Destroy the current session
193 *
Darren Hill5073a372011-08-31 13:54:19 -0400194 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400195 */
196 public function sess_destroy()
197 {
198 // Just call destroy on driver
199 $this->current->sess_destroy();
200 }
201
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300202 // ------------------------------------------------------------------------
203
Darren Hillc4e266b2011-08-30 15:40:27 -0400204 /**
205 * Regenerate the current session
206 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300207 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400208 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400209 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300210 public function sess_regenerate($destroy = FALSE)
Darren Hillc4e266b2011-08-30 15:40:27 -0400211 {
dchill4226429202012-07-31 10:55:07 -0400212 // Call regenerate on driver and resync userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400213 $this->current->sess_regenerate($destroy);
dchill4226429202012-07-31 10:55:07 -0400214 $this->userdata =& $this->current->get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400215 }
216
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300217 // ------------------------------------------------------------------------
218
Darren Hillc4e266b2011-08-30 15:40:27 -0400219 /**
220 * Fetch a specific item from the session array
221 *
Darren Hill5073a372011-08-31 13:54:19 -0400222 * @param string Item key
dchill42c5872252012-07-30 14:53:11 -0400223 * @return string Item value or NULL if not found
Darren Hillc4e266b2011-08-30 15:40:27 -0400224 */
225 public function userdata($item)
226 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300227 return isset($this->userdata[$item]) ? $this->userdata[$item] : NULL;
Darren Hillc4e266b2011-08-30 15:40:27 -0400228 }
229
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300230 // ------------------------------------------------------------------------
231
Darren Hillc4e266b2011-08-30 15:40:27 -0400232 /**
233 * Fetch all session data
234 *
235 * @return array User data array
236 */
237 public function all_userdata()
238 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300239 return isset($this->userdata) ? $this->userdata : NULL;
Darren Hillc4e266b2011-08-30 15:40:27 -0400240 }
241
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300242 // ------------------------------------------------------------------------
243
Darren Hillc4e266b2011-08-30 15:40:27 -0400244 /**
dchill42c5079de2012-07-23 10:53:47 -0400245 * Fetch all flashdata
246 *
vkeranov3bb40292012-10-27 18:47:26 +0300247 * @return array Flash data array
dchill42c5079de2012-07-23 10:53:47 -0400248 */
249 public function all_flashdata()
250 {
251 $out = array();
252
253 // loop through all userdata
254 foreach ($this->all_userdata() as $key => $val)
255 {
dchill42c5872252012-07-30 14:53:11 -0400256 // if it contains flashdata, add it
dchill42c5079de2012-07-23 10:53:47 -0400257 if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE)
258 {
dchill4226429202012-07-31 10:55:07 -0400259 $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key);
dchill42c5079de2012-07-23 10:53:47 -0400260 $out[$key] = $val;
261 }
262 }
263 return $out;
264 }
265
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300266 // ------------------------------------------------------------------------
267
dchill42c5079de2012-07-23 10:53:47 -0400268 /**
Darren Hillc4e266b2011-08-30 15:40:27 -0400269 * Add or change data in the "userdata" array
270 *
Darren Hill5073a372011-08-31 13:54:19 -0400271 * @param mixed Item name or array of items
272 * @param string Item value or empty string
273 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400274 */
275 public function set_userdata($newdata = array(), $newval = '')
276 {
277 // Wrap params as array if singular
278 if (is_string($newdata))
279 {
280 $newdata = array($newdata => $newval);
281 }
282
283 // Set each name/value pair
284 if (count($newdata) > 0)
285 {
286 foreach ($newdata as $key => $val)
287 {
288 $this->userdata[$key] = $val;
289 }
290 }
291
292 // Tell driver data changed
293 $this->current->sess_save();
294 }
295
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300296 // ------------------------------------------------------------------------
297
Darren Hillc4e266b2011-08-30 15:40:27 -0400298 /**
299 * Delete a session variable from the "userdata" array
300 *
Darren Hill5073a372011-08-31 13:54:19 -0400301 * @param mixed Item name or array of item names
302 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400303 */
304 public function unset_userdata($newdata = array())
305 {
306 // Wrap single name as array
307 if (is_string($newdata))
308 {
309 $newdata = array($newdata => '');
310 }
311
312 // Unset each item name
313 if (count($newdata) > 0)
314 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300315 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400316 {
317 unset($this->userdata[$key]);
318 }
319 }
320
321 // Tell driver data changed
322 $this->current->sess_save();
323 }
324
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300325 // ------------------------------------------------------------------------
326
Darren Hillc4e266b2011-08-30 15:40:27 -0400327 /**
328 * Determine if an item exists
329 *
Darren Hill5073a372011-08-31 13:54:19 -0400330 * @param string Item name
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300331 * @return bool
Darren Hillc4e266b2011-08-30 15:40:27 -0400332 */
333 public function has_userdata($item)
334 {
Darren Hillc4e266b2011-08-30 15:40:27 -0400335 return isset($this->userdata[$item]);
336 }
337
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300338 // ------------------------------------------------------------------------
339
Darren Hillc4e266b2011-08-30 15:40:27 -0400340 /**
341 * Add or change flashdata, only available until the next request
342 *
Darren Hill5073a372011-08-31 13:54:19 -0400343 * @param mixed Item name or array of items
344 * @param string Item value or empty string
345 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400346 */
347 public function set_flashdata($newdata = array(), $newval = '')
348 {
349 // Wrap item as array if singular
350 if (is_string($newdata))
351 {
352 $newdata = array($newdata => $newval);
353 }
354
355 // Prepend each key name and set value
356 if (count($newdata) > 0)
357 {
358 foreach ($newdata as $key => $val)
359 {
360 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
361 $this->set_userdata($flashdata_key, $val);
362 }
363 }
364 }
365
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300366 // ------------------------------------------------------------------------
367
Darren Hillc4e266b2011-08-30 15:40:27 -0400368 /**
369 * Keeps existing flashdata available to next request.
370 *
Darren Hill5073a372011-08-31 13:54:19 -0400371 * @param string Item key
372 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400373 */
374 public function keep_flashdata($key)
375 {
dchill42c5079de2012-07-23 10:53:47 -0400376 // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep()
377 // Note the function will return NULL if the $key provided cannot be found
Darren Hillc4e266b2011-08-30 15:40:27 -0400378 $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
379 $value = $this->userdata($old_flashdata_key);
380
381 $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
382 $this->set_userdata($new_flashdata_key, $value);
383 }
384
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300385 // ------------------------------------------------------------------------
386
Darren Hillc4e266b2011-08-30 15:40:27 -0400387 /**
388 * Fetch a specific flashdata item from the session array
389 *
Darren Hill5073a372011-08-31 13:54:19 -0400390 * @param string Item key
391 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400392 */
393 public function flashdata($key)
394 {
395 // Prepend key and retrieve value
396 $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
397 return $this->userdata($flashdata_key);
398 }
399
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300400 // ------------------------------------------------------------------------
401
Darren Hillc4e266b2011-08-30 15:40:27 -0400402 /**
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300403 * Add or change tempdata, only available until expiration
Darren Hillc4e266b2011-08-30 15:40:27 -0400404 *
Darren Hill5073a372011-08-31 13:54:19 -0400405 * @param mixed Item name or array of items
406 * @param string Item value or empty string
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300407 * @param int Item lifetime in seconds or 0 for default
Darren Hill5073a372011-08-31 13:54:19 -0400408 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400409 */
410 public function set_tempdata($newdata = array(), $newval = '', $expire = 0)
411 {
412 // Set expiration time
413 $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF);
414
415 // Wrap item as array if singular
416 if (is_string($newdata))
417 {
418 $newdata = array($newdata => $newval);
419 }
420
421 // Get or create expiration list
422 $expirations = $this->userdata(self::EXPIRATION_KEY);
dchill4277ee3fd2012-07-24 11:50:01 -0400423 if ( ! $expirations)
Darren Hillc4e266b2011-08-30 15:40:27 -0400424 {
425 $expirations = array();
426 }
427
428 // Prepend each key name and set value
429 if (count($newdata) > 0)
430 {
431 foreach ($newdata as $key => $val)
432 {
433 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
434 $expirations[$tempdata_key] = $expire;
435 $this->set_userdata($tempdata_key, $val);
436 }
437 }
438
439 // Update expiration list
440 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
441 }
442
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300443 // ------------------------------------------------------------------------
444
Darren Hillc4e266b2011-08-30 15:40:27 -0400445 /**
446 * Delete a temporary session variable from the "userdata" array
447 *
Darren Hill5073a372011-08-31 13:54:19 -0400448 * @param mixed Item name or array of item names
449 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400450 */
451 public function unset_tempdata($newdata = array())
452 {
453 // Get expirations list
454 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300455 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400456 {
457 // Nothing to do
458 return;
459 }
460
461 // Wrap single name as array
462 if (is_string($newdata))
463 {
464 $newdata = array($newdata => '');
465 }
466
467 // Prepend each item name and unset
468 if (count($newdata) > 0)
469 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300470 foreach (array_keys($newdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400471 {
472 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
473 unset($expirations[$tempdata_key]);
474 $this->unset_userdata($tempdata_key);
475 }
476 }
477
478 // Update expiration list
479 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
480 }
481
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300482 // ------------------------------------------------------------------------
483
Darren Hillc4e266b2011-08-30 15:40:27 -0400484 /**
485 * Fetch a specific tempdata item from the session array
486 *
Darren Hill5073a372011-08-31 13:54:19 -0400487 * @param string Item key
488 * @return string
Darren Hillc4e266b2011-08-30 15:40:27 -0400489 */
490 public function tempdata($key)
491 {
492 // Prepend key and return value
493 $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
494 return $this->userdata($tempdata_key);
495 }
496
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300497 // ------------------------------------------------------------------------
498
Darren Hillc4e266b2011-08-30 15:40:27 -0400499 /**
500 * Identifies flashdata as 'old' for removal
501 * when _flashdata_sweep() runs.
502 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400503 * @return void
504 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400505 protected function _flashdata_mark()
Darren Hillc4e266b2011-08-30 15:40:27 -0400506 {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300507 foreach ($this->all_userdata() as $name => $value)
Darren Hillc4e266b2011-08-30 15:40:27 -0400508 {
509 $parts = explode(self::FLASHDATA_NEW, $name);
510 if (is_array($parts) && count($parts) === 2)
511 {
512 $new_name = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1];
513 $this->set_userdata($new_name, $value);
514 $this->unset_userdata($name);
515 }
516 }
517 }
518
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300519 // ------------------------------------------------------------------------
520
Darren Hillc4e266b2011-08-30 15:40:27 -0400521 /**
522 * Removes all flashdata marked as 'old'
523 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400524 * @return void
525 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400526 protected function _flashdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400527 {
528 $userdata = $this->all_userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300529 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400530 {
531 if (strpos($key, self::FLASHDATA_OLD))
532 {
533 $this->unset_userdata($key);
534 }
535 }
536 }
537
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300538 // ------------------------------------------------------------------------
539
Darren Hillc4e266b2011-08-30 15:40:27 -0400540 /**
541 * Removes all expired tempdata
542 *
Darren Hillc4e266b2011-08-30 15:40:27 -0400543 * @return void
544 */
Darren Hilla2ae6572011-09-01 07:36:26 -0400545 protected function _tempdata_sweep()
Darren Hillc4e266b2011-08-30 15:40:27 -0400546 {
547 // Get expirations list
548 $expirations = $this->userdata(self::EXPIRATION_KEY);
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300549 if (empty($expirations))
Darren Hillc4e266b2011-08-30 15:40:27 -0400550 {
551 // Nothing to do
552 return;
553 }
554
555 // Unset expired elements
556 $now = time();
557 $userdata = $this->all_userdata();
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300558 foreach (array_keys($userdata) as $key)
Darren Hillc4e266b2011-08-30 15:40:27 -0400559 {
560 if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now)
561 {
562 unset($expirations[$key]);
563 $this->unset_userdata($key);
564 }
565 }
566
567 // Update expiration list
568 $this->set_userdata(self::EXPIRATION_KEY, $expirations);
569 }
Darren Hillc4e266b2011-08-30 15:40:27 -0400570
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300571}
572
573// ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400574
575/**
Darren Hill5073a372011-08-31 13:54:19 -0400576 * CI_Session_driver Class
Darren Hillc4e266b2011-08-30 15:40:27 -0400577 *
Darren Hill5073a372011-08-31 13:54:19 -0400578 * Extend this class to make a new CI_Session driver.
579 * A CI_Session driver basically manages an array of name/value pairs with some sort of storage mechanism.
580 * 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 -0400581 * session data. Then implement a save handler to write changed data to storage (sess_save), a destroy handler
582 * to remove deleted data (sess_destroy), and an access handler to expose the data (get_userdata).
Darren Hill5073a372011-08-31 13:54:19 -0400583 * Put your driver in the libraries/Session/drivers folder anywhere in the loader paths. This includes the
584 * application directory, the system directory, or any path you add with $CI->load->add_package_path().
585 * Your driver must be named CI_Session_<name>, and your filename must be Session_<name>.php,
586 * preferably also capitalized. (e.g.: CI_Session_foo in libraries/Session/drivers/Session_foo.php)
587 * 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 -0400588 * object. (e.g.: $config['sess_driver'] = 'foo'; OR $CI->load->driver('session', array('sess_driver' => 'foo')); )
589 * Already provided are the Native driver, which manages the native PHP $_SESSION array, and
590 * the Cookie driver, which manages the data in a browser cookie, with optional extra storage in a database table.
591 *
Darren Hill5073a372011-08-31 13:54:19 -0400592 * @package CodeIgniter
593 * @subpackage Libraries
Darren Hillc4e266b2011-08-30 15:40:27 -0400594 * @category Sessions
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300595 * @author EllisLab Dev Team
Darren Hillc4e266b2011-08-30 15:40:27 -0400596 */
Darren Hill5073a372011-08-31 13:54:19 -0400597abstract class CI_Session_driver extends CI_Driver {
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300598
Andrey Andreev2e3e2302012-10-09 15:52:34 +0300599 protected $CI;
600
601 /**
602 * Constructor
603 *
604 * Gets the CI singleton, so that individual drivers
605 * don't have to do it separately.
606 *
607 * @return void
608 */
609 public function __construct()
610 {
611 $this->CI =& get_instance();
612 }
613
614 // ------------------------------------------------------------------------
615
Darren Hillc4e266b2011-08-30 15:40:27 -0400616 /**
617 * Decorate
618 *
619 * Decorates the child with the parent driver lib's methods and properties
620 *
621 * @param object Parent library object
622 * @return void
623 */
624 public function decorate($parent)
625 {
626 // Call base class decorate first
627 parent::decorate($parent);
628
dchill42c5872252012-07-30 14:53:11 -0400629 // Call initialize method now that driver has access to $this->_parent
Darren Hillc4e266b2011-08-30 15:40:27 -0400630 $this->initialize();
631 }
632
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300633 // ------------------------------------------------------------------------
634
Darren Hillc4e266b2011-08-30 15:40:27 -0400635 /**
636 * __call magic method
637 *
638 * Handles access to the parent driver library's methods
639 *
Darren Hill5073a372011-08-31 13:54:19 -0400640 * @param string Library method name
641 * @param array Method arguments (default: none)
Darren Hillc4e266b2011-08-30 15:40:27 -0400642 * @return mixed
643 */
644 public function __call($method, $args = array())
645 {
646 // Make sure the parent library uses this driver
dchill42c5872252012-07-30 14:53:11 -0400647 $this->_parent->select_driver(get_class($this));
Darren Hillc4e266b2011-08-30 15:40:27 -0400648 return parent::__call($method, $args);
649 }
650
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300651 // ------------------------------------------------------------------------
652
Darren Hillc4e266b2011-08-30 15:40:27 -0400653 /**
654 * Initialize driver
655 *
Darren Hill5073a372011-08-31 13:54:19 -0400656 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400657 */
658 protected function initialize()
659 {
660 // Overload this method to implement initialization
661 }
662
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300663 // ------------------------------------------------------------------------
664
Darren Hillc4e266b2011-08-30 15:40:27 -0400665 /**
666 * Save the session data
667 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300668 * Data in the array has changed - perform any storage synchronization
669 * necessary. The child class MUST implement this abstract method!
Darren Hillc4e266b2011-08-30 15:40:27 -0400670 *
Darren Hill5073a372011-08-31 13:54:19 -0400671 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400672 */
673 abstract public function sess_save();
674
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300675 // ------------------------------------------------------------------------
676
Darren Hillc4e266b2011-08-30 15:40:27 -0400677 /**
678 * Destroy the current session
679 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300680 * Clean up storage for this session - it has been terminated.
Darren Hillc4e266b2011-08-30 15:40:27 -0400681 * The child class MUST implement this abstract method!
682 *
Darren Hill5073a372011-08-31 13:54:19 -0400683 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400684 */
685 abstract public function sess_destroy();
686
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300687 // ------------------------------------------------------------------------
688
Darren Hillc4e266b2011-08-30 15:40:27 -0400689 /**
690 * Regenerate the current session
691 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300692 * Regenerate the session ID.
Darren Hillc4e266b2011-08-30 15:40:27 -0400693 * The child class MUST implement this abstract method!
694 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300695 * @param bool Destroy session data flag (default: false)
Darren Hill5073a372011-08-31 13:54:19 -0400696 * @return void
Darren Hillc4e266b2011-08-30 15:40:27 -0400697 */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300698 abstract public function sess_regenerate($destroy = FALSE);
699
700 // ------------------------------------------------------------------------
Darren Hillc4e266b2011-08-30 15:40:27 -0400701
702 /**
703 * Get a reference to user data array
704 *
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300705 * Give array access to the main CI_Session object.
Darren Hillc4e266b2011-08-30 15:40:27 -0400706 * The child class MUST implement this abstract method!
707 *
Darren Hill5073a372011-08-31 13:54:19 -0400708 * @return array Reference to userdata
Darren Hillc4e266b2011-08-30 15:40:27 -0400709 */
710 abstract public function &get_userdata();
Darren Hillc4e266b2011-08-30 15:40:27 -0400711
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300712}
Darren Hillc4e266b2011-08-30 15:40:27 -0400713
714/* End of file Session.php */
Andrey Andreev9ffcee62012-09-05 16:25:16 +0300715/* Location: ./system/libraries/Session/Session.php */