Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 5 | * 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 Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 18 | * |
| 19 | * @package CodeIgniter |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 2.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 28 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 29 | /** |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 30 | * CodeIgniter Session Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 31 | * |
| 32 | * The user interface defined by EllisLabs, now with puggable drivers to manage different storage mechanisms. |
dchill42 | 0e88408 | 2012-08-11 20:10:17 -0400 | [diff] [blame] | 33 | * 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 Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 35 | * 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 Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 38 | * The CI_Session library class keeps track of the most recently loaded driver as "current" to call for driver methods. |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 39 | * 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 Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 50 | * @author EllisLab Dev Team |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 51 | * @link http://codeigniter.com/user_guide/libraries/sessions.html |
| 52 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 53 | class CI_Session extends CI_Driver_Library { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 54 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 55 | /** |
| 56 | * Initialization parameters |
| 57 | * |
| 58 | * @var array |
| 59 | */ |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 60 | public $params = array(); |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 61 | |
| 62 | /** |
Andrey Andreev | c958eeb | 2013-07-31 14:28:50 +0300 | [diff] [blame] | 63 | * Valid drivers list |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
| 67 | public $valid_drivers = array('native', 'cookie'); |
| 68 | |
| 69 | /** |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 70 | * Current driver in use |
| 71 | * |
| 72 | * @var string |
| 73 | */ |
Andrey Andreev | c958eeb | 2013-07-31 14:28:50 +0300 | [diff] [blame] | 74 | public $current = NULL; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * User data |
| 78 | * |
| 79 | * @var array |
| 80 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 81 | protected $userdata = array(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 82 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 83 | // ------------------------------------------------------------------------ |
| 84 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 85 | const FLASHDATA_KEY = 'flash'; |
| 86 | const FLASHDATA_NEW = ':new:'; |
| 87 | const FLASHDATA_OLD = ':old:'; |
| 88 | const FLASHDATA_EXP = ':exp:'; |
| 89 | const EXPIRATION_KEY = '__expirations'; |
| 90 | const TEMP_EXP_DEF = 300; |
| 91 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 92 | // ------------------------------------------------------------------------ |
| 93 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 94 | /** |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 95 | * CI_Session constructor |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 96 | * |
| 97 | * The constructor loads the configured driver ('sess_driver' in config.php or as a parameter), running |
| 98 | * routines in its constructor, and manages flashdata aging. |
| 99 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 100 | * @param array Configuration parameters |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 101 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 102 | */ |
| 103 | public function __construct(array $params = array()) |
| 104 | { |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 105 | $_config =& get_instance()->config; |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 106 | |
| 107 | // No sessions under CLI |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 108 | if (is_cli()) |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 109 | { |
| 110 | return; |
| 111 | } |
| 112 | |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 113 | log_message('debug', 'CI_Session Class Initialized'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 114 | |
Andrey Andreev | c958eeb | 2013-07-31 14:28:50 +0300 | [diff] [blame] | 115 | // Add possible extra entries to our valid drivers list |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 116 | $drivers = isset($params['sess_valid_drivers']) ? $params['sess_valid_drivers'] : $_config->item('sess_valid_drivers'); |
Andrey Andreev | c958eeb | 2013-07-31 14:28:50 +0300 | [diff] [blame] | 117 | if ( ! empty($drivers)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 118 | { |
Andrey Andreev | c958eeb | 2013-07-31 14:28:50 +0300 | [diff] [blame] | 119 | $drivers = array_map('strtolower', (array) $drivers); |
| 120 | $this->valid_drivers = array_merge($this->valid_drivers, array_diff($drivers, $this->valid_drivers)); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Get driver to load |
Andrey Andreev | f964b16 | 2013-11-12 17:04:55 +0200 | [diff] [blame] | 124 | $driver = isset($params['sess_driver']) ? $params['sess_driver'] : $_config->item('sess_driver'); |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 125 | if ( ! $driver) |
| 126 | { |
Andrey Andreev | c958eeb | 2013-07-31 14:28:50 +0300 | [diff] [blame] | 127 | log_message('debug', "Session: No driver name is configured, defaulting to 'cookie'."); |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 128 | $driver = 'cookie'; |
| 129 | } |
| 130 | |
Andrey Andreev | c958eeb | 2013-07-31 14:28:50 +0300 | [diff] [blame] | 131 | if ( ! in_array($driver, $this->valid_drivers)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 132 | { |
Andrey Andreev | c958eeb | 2013-07-31 14:28:50 +0300 | [diff] [blame] | 133 | log_message('error', 'Session: Configured driver name is not valid, aborting.'); |
| 134 | return; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Save a copy of parameters in case drivers need access |
| 138 | $this->params = $params; |
| 139 | |
| 140 | // Load driver and get array reference |
| 141 | $this->load_driver($driver); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 142 | |
| 143 | // Delete 'old' flashdata (from last request) |
| 144 | $this->_flashdata_sweep(); |
| 145 | |
| 146 | // Mark all new flashdata as old (data will be deleted before next request) |
| 147 | $this->_flashdata_mark(); |
| 148 | |
| 149 | // Delete expired tempdata |
| 150 | $this->_tempdata_sweep(); |
| 151 | |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 152 | log_message('debug', 'CI_Session routines successfully run'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 155 | // ------------------------------------------------------------------------ |
| 156 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 157 | /** |
| 158 | * Loads session storage driver |
| 159 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 160 | * @param string Driver classname |
| 161 | * @return object Loaded driver object |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 162 | */ |
| 163 | public function load_driver($driver) |
| 164 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 165 | // Save reference to most recently loaded driver as library default and sync userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 166 | $this->current = parent::load_driver($driver); |
dchill42 | b185537 | 2012-07-31 09:32:23 -0400 | [diff] [blame] | 167 | $this->userdata =& $this->current->get_userdata(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 168 | return $this->current; |
| 169 | } |
| 170 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 171 | // ------------------------------------------------------------------------ |
| 172 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 173 | /** |
| 174 | * Select default session storage driver |
| 175 | * |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 176 | * @param string Driver name |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 177 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 178 | */ |
| 179 | public function select_driver($driver) |
| 180 | { |
| 181 | // Validate driver name |
dchill42 | 6262d05 | 2012-11-24 18:41:13 -0500 | [diff] [blame] | 182 | $prefix = (string) get_instance()->config->item('subclass_prefix'); |
| 183 | $child = strtolower(str_replace(array('CI_', $prefix, $this->lib_name.'_'), '', $driver)); |
| 184 | if (in_array($child, array_map('strtolower', $this->valid_drivers))) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 185 | { |
dchill42 | b185537 | 2012-07-31 09:32:23 -0400 | [diff] [blame] | 186 | // See if driver is loaded |
dchill42 | b185537 | 2012-07-31 09:32:23 -0400 | [diff] [blame] | 187 | if (isset($this->$child)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 188 | { |
dchill42 | aee9265 | 2012-08-26 21:45:35 -0400 | [diff] [blame] | 189 | // See if driver is already current |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 190 | if ($this->$child !== $this->current) |
| 191 | { |
dchill42 | aee9265 | 2012-08-26 21:45:35 -0400 | [diff] [blame] | 192 | // Make driver current and sync userdata |
| 193 | $this->current = $this->$child; |
| 194 | $this->userdata =& $this->current->get_userdata(); |
| 195 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 196 | } |
| 197 | else |
| 198 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 199 | // Load new driver |
dchill42 | aee9265 | 2012-08-26 21:45:35 -0400 | [diff] [blame] | 200 | $this->load_driver($child); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 205 | // ------------------------------------------------------------------------ |
| 206 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 207 | /** |
| 208 | * Destroy the current session |
| 209 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 210 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 211 | */ |
| 212 | public function sess_destroy() |
| 213 | { |
| 214 | // Just call destroy on driver |
| 215 | $this->current->sess_destroy(); |
| 216 | } |
| 217 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 218 | // ------------------------------------------------------------------------ |
| 219 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 220 | /** |
| 221 | * Regenerate the current session |
| 222 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 223 | * @param bool Destroy session data flag (default: false) |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 224 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 225 | */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 226 | public function sess_regenerate($destroy = FALSE) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 227 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 228 | // Call regenerate on driver and resync userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 229 | $this->current->sess_regenerate($destroy); |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 230 | $this->userdata =& $this->current->get_userdata(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 233 | // ------------------------------------------------------------------------ |
| 234 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 235 | /** |
| 236 | * Fetch a specific item from the session array |
| 237 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 238 | * @param string Item key |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 239 | * @return string Item value or NULL if not found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 240 | */ |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 241 | public function userdata($item = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 242 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 243 | if (isset($item)) |
| 244 | { |
| 245 | return isset($this->userdata[$item]) ? $this->userdata[$item] : NULL; |
| 246 | } |
| 247 | |
| 248 | return isset($this->userdata) ? $this->userdata : array(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 251 | // ------------------------------------------------------------------------ |
| 252 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 253 | /** |
| 254 | * Fetch all session data |
| 255 | * |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 256 | * @deprecated 3.0.0 Use userdata() with no parameters instead |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 257 | * @return array User data array |
| 258 | */ |
| 259 | public function all_userdata() |
| 260 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 261 | return isset($this->userdata) ? $this->userdata : array(); |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 262 | } |
| 263 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 264 | // ------------------------------------------------------------------------ |
| 265 | |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 266 | /** |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 267 | * Add or change data in the "userdata" array |
| 268 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 269 | * @param mixed Item name or array of items |
| 270 | * @param string Item value or empty string |
| 271 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 272 | */ |
Andrey Andreev | e6376aa | 2014-01-06 13:11:30 +0200 | [diff] [blame] | 273 | public function set_userdata($newdata, $newval = '') |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 274 | { |
| 275 | // Wrap params as array if singular |
| 276 | if (is_string($newdata)) |
| 277 | { |
| 278 | $newdata = array($newdata => $newval); |
| 279 | } |
| 280 | |
| 281 | // Set each name/value pair |
| 282 | if (count($newdata) > 0) |
| 283 | { |
| 284 | foreach ($newdata as $key => $val) |
| 285 | { |
| 286 | $this->userdata[$key] = $val; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // Tell driver data changed |
| 291 | $this->current->sess_save(); |
| 292 | } |
| 293 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 294 | // ------------------------------------------------------------------------ |
| 295 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 296 | /** |
| 297 | * Delete a session variable from the "userdata" array |
| 298 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 299 | * @param mixed Item name or array of item names |
| 300 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 301 | */ |
Andrey Andreev | e6376aa | 2014-01-06 13:11:30 +0200 | [diff] [blame] | 302 | public function unset_userdata($newdata) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 303 | { |
| 304 | // Wrap single name as array |
| 305 | if (is_string($newdata)) |
| 306 | { |
| 307 | $newdata = array($newdata => ''); |
| 308 | } |
| 309 | |
| 310 | // Unset each item name |
| 311 | if (count($newdata) > 0) |
| 312 | { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 313 | foreach (array_keys($newdata) as $key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 314 | { |
| 315 | unset($this->userdata[$key]); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // Tell driver data changed |
| 320 | $this->current->sess_save(); |
| 321 | } |
| 322 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 323 | // ------------------------------------------------------------------------ |
| 324 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 325 | /** |
| 326 | * Determine if an item exists |
| 327 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 328 | * @param string Item name |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 329 | * @return bool |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 330 | */ |
| 331 | public function has_userdata($item) |
| 332 | { |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 333 | return isset($this->userdata[$item]); |
| 334 | } |
| 335 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 336 | // ------------------------------------------------------------------------ |
| 337 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 338 | /** |
| 339 | * Add or change flashdata, only available until the next request |
| 340 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 341 | * @param mixed Item name or array of items |
| 342 | * @param string Item value or empty string |
| 343 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 344 | */ |
Andrey Andreev | e6376aa | 2014-01-06 13:11:30 +0200 | [diff] [blame] | 345 | public function set_flashdata($newdata, $newval = '') |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 346 | { |
| 347 | // Wrap item as array if singular |
| 348 | if (is_string($newdata)) |
| 349 | { |
| 350 | $newdata = array($newdata => $newval); |
| 351 | } |
| 352 | |
| 353 | // Prepend each key name and set value |
| 354 | if (count($newdata) > 0) |
| 355 | { |
| 356 | foreach ($newdata as $key => $val) |
| 357 | { |
| 358 | $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key; |
| 359 | $this->set_userdata($flashdata_key, $val); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 364 | // ------------------------------------------------------------------------ |
| 365 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 366 | /** |
| 367 | * Keeps existing flashdata available to next request. |
| 368 | * |
Johnathan Croom | 9d9849b | 2012-11-24 13:03:13 -0700 | [diff] [blame] | 369 | * @param mixed Item key(s) |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 370 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 371 | */ |
| 372 | public function keep_flashdata($key) |
| 373 | { |
Johnathan Croom | 8d8543d | 2012-11-25 10:36:57 -0700 | [diff] [blame] | 374 | |
| 375 | if (is_array($key)) |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 376 | { |
Johnathan Croom | 8d8543d | 2012-11-25 10:36:57 -0700 | [diff] [blame] | 377 | foreach ($key as $k) |
| 378 | { |
| 379 | $this->keep_flashdata($k); |
| 380 | } |
| 381 | |
| 382 | return; |
Johnathan Croom | 4beca5c | 2012-11-23 18:32:46 -0700 | [diff] [blame] | 383 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 384 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 385 | // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep() |
| 386 | // Note the function will return NULL if the $key provided cannot be found |
| 387 | $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key; |
| 388 | $value = $this->userdata($old_flashdata_key); |
| 389 | |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 390 | $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key; |
| 391 | $this->set_userdata($new_flashdata_key, $value); |
| 392 | } |
| 393 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 394 | // ------------------------------------------------------------------------ |
| 395 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 396 | /** |
| 397 | * Fetch a specific flashdata item from the session array |
| 398 | * |
| 399 | * @param string Item key |
| 400 | * @return string |
| 401 | */ |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 402 | public function flashdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 403 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 404 | if (isset($key)) |
| 405 | { |
| 406 | return $this->userdata(self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key); |
| 407 | } |
| 408 | |
| 409 | // Get our flashdata items from userdata |
| 410 | $out = array(); |
| 411 | foreach ($this->userdata() as $key => $val) |
| 412 | { |
| 413 | if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE) |
| 414 | { |
| 415 | $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key); |
| 416 | $out[$key] = $val; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return $out; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 423 | // ------------------------------------------------------------------------ |
| 424 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 425 | /** |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 426 | * Add or change tempdata, only available until expiration |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 427 | * |
| 428 | * @param mixed Item name or array of items |
| 429 | * @param string Item value or empty string |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 430 | * @param int Item lifetime in seconds or 0 for default |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 431 | * @return void |
| 432 | */ |
Andrey Andreev | e6376aa | 2014-01-06 13:11:30 +0200 | [diff] [blame] | 433 | public function set_tempdata($newdata, $newval = '', $expire = 0) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 434 | { |
| 435 | // Set expiration time |
| 436 | $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF); |
| 437 | |
| 438 | // Wrap item as array if singular |
| 439 | if (is_string($newdata)) |
| 440 | { |
| 441 | $newdata = array($newdata => $newval); |
| 442 | } |
| 443 | |
| 444 | // Get or create expiration list |
| 445 | $expirations = $this->userdata(self::EXPIRATION_KEY); |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 446 | if ( ! $expirations) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 447 | { |
| 448 | $expirations = array(); |
| 449 | } |
| 450 | |
| 451 | // Prepend each key name and set value |
| 452 | if (count($newdata) > 0) |
| 453 | { |
| 454 | foreach ($newdata as $key => $val) |
| 455 | { |
| 456 | $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key; |
| 457 | $expirations[$tempdata_key] = $expire; |
| 458 | $this->set_userdata($tempdata_key, $val); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Update expiration list |
| 463 | $this->set_userdata(self::EXPIRATION_KEY, $expirations); |
| 464 | } |
| 465 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 466 | // ------------------------------------------------------------------------ |
| 467 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 468 | /** |
| 469 | * Delete a temporary session variable from the "userdata" array |
| 470 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 471 | * @param mixed Item name or array of item names |
| 472 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 473 | */ |
Andrey Andreev | e6376aa | 2014-01-06 13:11:30 +0200 | [diff] [blame] | 474 | public function unset_tempdata($newdata) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 475 | { |
| 476 | // Get expirations list |
| 477 | $expirations = $this->userdata(self::EXPIRATION_KEY); |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 478 | if (empty($expirations)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 479 | { |
| 480 | // Nothing to do |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | // Wrap single name as array |
| 485 | if (is_string($newdata)) |
| 486 | { |
| 487 | $newdata = array($newdata => ''); |
| 488 | } |
| 489 | |
| 490 | // Prepend each item name and unset |
| 491 | if (count($newdata) > 0) |
| 492 | { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 493 | foreach (array_keys($newdata) as $key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 494 | { |
| 495 | $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key; |
| 496 | unset($expirations[$tempdata_key]); |
| 497 | $this->unset_userdata($tempdata_key); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // Update expiration list |
| 502 | $this->set_userdata(self::EXPIRATION_KEY, $expirations); |
| 503 | } |
| 504 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 505 | // ------------------------------------------------------------------------ |
| 506 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 507 | /** |
| 508 | * Fetch a specific tempdata item from the session array |
| 509 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 510 | * @param string Item key |
| 511 | * @return string |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 512 | */ |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 513 | public function tempdata($key = NULL) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 514 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 515 | if (isset($key)) |
| 516 | { |
| 517 | return $this->userdata(self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key); |
| 518 | } |
| 519 | |
| 520 | // Get our tempdata items from userdata |
| 521 | $out = array(); |
| 522 | foreach ($this->userdata() as $key => $val) |
| 523 | { |
| 524 | if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_EXP) !== FALSE) |
| 525 | { |
| 526 | $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_EXP, '', $key); |
| 527 | $out[$key] = $val; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | return $out; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 534 | // ------------------------------------------------------------------------ |
| 535 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 536 | /** |
| 537 | * Identifies flashdata as 'old' for removal |
| 538 | * when _flashdata_sweep() runs. |
| 539 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 540 | * @return void |
| 541 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 542 | protected function _flashdata_mark() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 543 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 544 | foreach ($this->userdata() as $name => $value) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 545 | { |
| 546 | $parts = explode(self::FLASHDATA_NEW, $name); |
Andrey Andreev | e24eed7 | 2012-11-02 23:33:45 +0200 | [diff] [blame] | 547 | if (count($parts) === 2) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 548 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 549 | $this->set_userdata(self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1], $value); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 550 | $this->unset_userdata($name); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 555 | // ------------------------------------------------------------------------ |
| 556 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 557 | /** |
| 558 | * Removes all flashdata marked as 'old' |
| 559 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 560 | * @return void |
| 561 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 562 | protected function _flashdata_sweep() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 563 | { |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 564 | $userdata = $this->userdata(); |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 565 | foreach (array_keys($userdata) as $key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 566 | { |
| 567 | if (strpos($key, self::FLASHDATA_OLD)) |
| 568 | { |
| 569 | $this->unset_userdata($key); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 574 | // ------------------------------------------------------------------------ |
| 575 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 576 | /** |
| 577 | * Removes all expired tempdata |
| 578 | * |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 579 | * @return void |
| 580 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 581 | protected function _tempdata_sweep() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 582 | { |
| 583 | // Get expirations list |
| 584 | $expirations = $this->userdata(self::EXPIRATION_KEY); |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 585 | if (empty($expirations)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 586 | { |
| 587 | // Nothing to do |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | // Unset expired elements |
| 592 | $now = time(); |
Andrey Andreev | ecc260e | 2014-01-24 14:20:13 +0200 | [diff] [blame] | 593 | $userdata = $this->userdata(); |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 594 | foreach (array_keys($userdata) as $key) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 595 | { |
| 596 | if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now) |
| 597 | { |
| 598 | unset($expirations[$key]); |
| 599 | $this->unset_userdata($key); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | // Update expiration list |
| 604 | $this->set_userdata(self::EXPIRATION_KEY, $expirations); |
| 605 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 606 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // ------------------------------------------------------------------------ |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 610 | |
| 611 | /** |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 612 | * CI_Session_driver Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 613 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 614 | * Extend this class to make a new CI_Session driver. |
| 615 | * A CI_Session driver basically manages an array of name/value pairs with some sort of storage mechanism. |
| 616 | * To make a new driver, derive from (extend) CI_Session_driver. Overload the initialize method and read or create |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 617 | * session data. Then implement a save handler to write changed data to storage (sess_save), a destroy handler |
| 618 | * to remove deleted data (sess_destroy), and an access handler to expose the data (get_userdata). |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 619 | * Put your driver in the libraries/Session/drivers folder anywhere in the loader paths. This includes the |
| 620 | * application directory, the system directory, or any path you add with $CI->load->add_package_path(). |
| 621 | * Your driver must be named CI_Session_<name>, and your filename must be Session_<name>.php, |
| 622 | * preferably also capitalized. (e.g.: CI_Session_foo in libraries/Session/drivers/Session_foo.php) |
| 623 | * Then specify the driver by setting 'sess_driver' in your config file or as a parameter when loading the CI_Session |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 624 | * object. (e.g.: $config['sess_driver'] = 'foo'; OR $CI->load->driver('session', array('sess_driver' => 'foo')); ) |
| 625 | * Already provided are the Native driver, which manages the native PHP $_SESSION array, and |
| 626 | * the Cookie driver, which manages the data in a browser cookie, with optional extra storage in a database table. |
| 627 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 628 | * @package CodeIgniter |
| 629 | * @subpackage Libraries |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 630 | * @category Sessions |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 631 | * @author EllisLab Dev Team |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 632 | */ |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 633 | abstract class CI_Session_driver extends CI_Driver { |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 634 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 635 | /** |
| 636 | * CI Singleton |
| 637 | * |
| 638 | * @see get_instance() |
| 639 | * @var object |
| 640 | */ |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 641 | protected $CI; |
| 642 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 643 | // ------------------------------------------------------------------------ |
| 644 | |
Andrey Andreev | 2e3e230 | 2012-10-09 15:52:34 +0300 | [diff] [blame] | 645 | /** |
| 646 | * Constructor |
| 647 | * |
| 648 | * Gets the CI singleton, so that individual drivers |
| 649 | * don't have to do it separately. |
| 650 | * |
| 651 | * @return void |
| 652 | */ |
| 653 | public function __construct() |
| 654 | { |
| 655 | $this->CI =& get_instance(); |
| 656 | } |
| 657 | |
| 658 | // ------------------------------------------------------------------------ |
| 659 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 660 | /** |
| 661 | * Decorate |
| 662 | * |
| 663 | * Decorates the child with the parent driver lib's methods and properties |
| 664 | * |
| 665 | * @param object Parent library object |
| 666 | * @return void |
| 667 | */ |
| 668 | public function decorate($parent) |
| 669 | { |
| 670 | // Call base class decorate first |
| 671 | parent::decorate($parent); |
| 672 | |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 673 | // Call initialize method now that driver has access to $this->_parent |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 674 | $this->initialize(); |
| 675 | } |
| 676 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 677 | // ------------------------------------------------------------------------ |
| 678 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 679 | /** |
| 680 | * __call magic method |
| 681 | * |
| 682 | * Handles access to the parent driver library's methods |
| 683 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 684 | * @param string Library method name |
| 685 | * @param array Method arguments (default: none) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 686 | * @return mixed |
| 687 | */ |
| 688 | public function __call($method, $args = array()) |
| 689 | { |
| 690 | // Make sure the parent library uses this driver |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 691 | $this->_parent->select_driver(get_class($this)); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 692 | return parent::__call($method, $args); |
| 693 | } |
| 694 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 695 | // ------------------------------------------------------------------------ |
| 696 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 697 | /** |
| 698 | * Initialize driver |
| 699 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 700 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 701 | */ |
| 702 | protected function initialize() |
| 703 | { |
| 704 | // Overload this method to implement initialization |
| 705 | } |
| 706 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 707 | // ------------------------------------------------------------------------ |
| 708 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 709 | /** |
| 710 | * Save the session data |
| 711 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 712 | * Data in the array has changed - perform any storage synchronization |
| 713 | * necessary. The child class MUST implement this abstract method! |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 714 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 715 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 716 | */ |
| 717 | abstract public function sess_save(); |
| 718 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 719 | // ------------------------------------------------------------------------ |
| 720 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 721 | /** |
| 722 | * Destroy the current session |
| 723 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 724 | * Clean up storage for this session - it has been terminated. |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 725 | * The child class MUST implement this abstract method! |
| 726 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 727 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 728 | */ |
| 729 | abstract public function sess_destroy(); |
| 730 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 731 | // ------------------------------------------------------------------------ |
| 732 | |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 733 | /** |
| 734 | * Regenerate the current session |
| 735 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 736 | * Regenerate the session ID. |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 737 | * The child class MUST implement this abstract method! |
| 738 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 739 | * @param bool Destroy session data flag (default: false) |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 740 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 741 | */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 742 | abstract public function sess_regenerate($destroy = FALSE); |
| 743 | |
| 744 | // ------------------------------------------------------------------------ |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 745 | |
| 746 | /** |
| 747 | * Get a reference to user data array |
| 748 | * |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 749 | * Give array access to the main CI_Session object. |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 750 | * The child class MUST implement this abstract method! |
| 751 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 752 | * @return array Reference to userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 753 | */ |
| 754 | abstract public function &get_userdata(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 755 | |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 756 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 757 | |
| 758 | /* End of file Session.php */ |
Andrey Andreev | 9ffcee6 | 2012-09-05 16:25:16 +0300 | [diff] [blame] | 759 | /* Location: ./system/libraries/Session/Session.php */ |