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