Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
| 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
| 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 2.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | /** |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 18 | * CI_Session Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 19 | * |
| 20 | * 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] | 21 | * By default, the cookie session driver will load, but the 'sess_driver' config/param item (see above) can be |
| 22 | * used to specify the 'native' driver, or any other you might create. |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 23 | * Once loaded, this driver setup is a drop-in replacement for the former CI_Session library, taking its place as the |
| 24 | * 'session' member of the global controller framework (e.g.: $CI->session or $this->session). |
| 25 | * 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] | 26 | * 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] | 27 | * Ideally, one driver is loaded and all calls go directly through the main library interface. However, any methods |
| 28 | * called through the specific driver will switch the "current" driver to itself before invoking the library method |
| 29 | * (which will then call back into the driver for low-level operations). So, alternation between two drivers can be |
| 30 | * achieved by specifying which driver to use for each call (e.g.: $this->session->native->set_userdata('foo', 'bar'); |
| 31 | * $this->session->cookie->userdata('foo'); $this->session->native->unset_userdata('foo');). Notice in the previous |
| 32 | * example that the _native_ userdata value 'foo' would be set to 'bar', which would NOT be returned by the call for |
| 33 | * the _cookie_ userdata 'foo', nor would the _cookie_ value be unset by the call to unset the _native_ 'foo' value. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Libraries |
| 37 | * @category Sessions |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 38 | * @author ExpressionEngine Dev Team |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/libraries/sessions.html |
| 40 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 41 | class CI_Session extends CI_Driver_Library { |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 42 | public $params = array(); |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 43 | protected $current = null; |
| 44 | protected $userdata = array(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 45 | |
| 46 | const FLASHDATA_KEY = 'flash'; |
| 47 | const FLASHDATA_NEW = ':new:'; |
| 48 | const FLASHDATA_OLD = ':old:'; |
| 49 | const FLASHDATA_EXP = ':exp:'; |
| 50 | const EXPIRATION_KEY = '__expirations'; |
| 51 | const TEMP_EXP_DEF = 300; |
| 52 | |
| 53 | /** |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 54 | * CI_Session constructor |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 55 | * |
| 56 | * The constructor loads the configured driver ('sess_driver' in config.php or as a parameter), running |
| 57 | * routines in its constructor, and manages flashdata aging. |
| 58 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 59 | * @param array Configuration parameters |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 60 | */ |
| 61 | public function __construct(array $params = array()) |
| 62 | { |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 63 | log_message('debug', 'CI_Session Class Initialized'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 64 | |
| 65 | // Get valid drivers list |
| 66 | $CI =& get_instance(); |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 67 | $this->valid_drivers = array( |
| 68 | 'Session_native', |
| 69 | 'Session_cookie' |
| 70 | ); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 71 | $key = 'sess_valid_drivers'; |
| 72 | $drivers = (isset($params[$key])) ? $params[$key] : $CI->config->item($key); |
| 73 | if ($drivers) |
| 74 | { |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 75 | if ( ! is_array($drivers)) $drivers = array($drivers); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 76 | |
| 77 | // Add driver names to valid list |
| 78 | foreach ($drivers as $driver) |
| 79 | { |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 80 | if ( ! in_array(strtolower($driver), array_map('strtolower', $this->valid_drivers))) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 81 | { |
| 82 | $this->valid_drivers[] = $driver; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Get driver to load |
| 88 | $key = 'sess_driver'; |
| 89 | $driver = (isset($params[$key])) ? $params[$key] : $CI->config->item($key); |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 90 | if ( ! $driver) $driver = 'cookie'; |
| 91 | if ( ! in_array('session_'.strtolower($driver), array_map('strtolower', $this->valid_drivers))) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 92 | { |
| 93 | $this->valid_drivers[] = 'Session_'.$driver; |
| 94 | } |
| 95 | |
| 96 | // Save a copy of parameters in case drivers need access |
| 97 | $this->params = $params; |
| 98 | |
| 99 | // Load driver and get array reference |
| 100 | $this->load_driver($driver); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 101 | |
| 102 | // Delete 'old' flashdata (from last request) |
| 103 | $this->_flashdata_sweep(); |
| 104 | |
| 105 | // Mark all new flashdata as old (data will be deleted before next request) |
| 106 | $this->_flashdata_mark(); |
| 107 | |
| 108 | // Delete expired tempdata |
| 109 | $this->_tempdata_sweep(); |
| 110 | |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 111 | log_message('debug', 'CI_Session routines successfully run'); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Loads session storage driver |
| 116 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 117 | * @param string Driver classname |
| 118 | * @return object Loaded driver object |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 119 | */ |
| 120 | public function load_driver($driver) |
| 121 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 122 | // 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] | 123 | $this->current = parent::load_driver($driver); |
dchill42 | b185537 | 2012-07-31 09:32:23 -0400 | [diff] [blame] | 124 | $this->userdata =& $this->current->get_userdata(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 125 | return $this->current; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Select default session storage driver |
| 130 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 131 | * @param string Driver classname |
| 132 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 133 | */ |
| 134 | public function select_driver($driver) |
| 135 | { |
| 136 | // Validate driver name |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 137 | $lowername = strtolower(str_replace('CI_', '', $driver)); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 138 | if (in_array($lowername, array_map('strtolower', $this->valid_drivers))) |
| 139 | { |
dchill42 | b185537 | 2012-07-31 09:32:23 -0400 | [diff] [blame] | 140 | // See if driver is loaded |
| 141 | $child = str_replace($this->lib_name.'_', '', $driver); |
| 142 | if (isset($this->$child)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 143 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 144 | // Make driver current and sync userdata |
dchill42 | b185537 | 2012-07-31 09:32:23 -0400 | [diff] [blame] | 145 | $this->current = $this->$child; |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 146 | $this->userdata =& $this->current->get_userdata(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 147 | } |
| 148 | else |
| 149 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 150 | // Load new driver |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 151 | $this->load_driver($driver); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Destroy the current session |
| 158 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 159 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 160 | */ |
| 161 | public function sess_destroy() |
| 162 | { |
| 163 | // Just call destroy on driver |
| 164 | $this->current->sess_destroy(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Regenerate the current session |
| 169 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 170 | * @param boolean Destroy session data flag (default: false) |
| 171 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 172 | */ |
| 173 | public function sess_regenerate($destroy = false) |
| 174 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 175 | // Call regenerate on driver and resync userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 176 | $this->current->sess_regenerate($destroy); |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 177 | $this->userdata =& $this->current->get_userdata(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Fetch a specific item from the session array |
| 182 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 183 | * @param string Item key |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 184 | * @return string Item value or NULL if not found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 185 | */ |
| 186 | public function userdata($item) |
| 187 | { |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 188 | // Return value or NULL if not found |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 189 | return ( ! isset($this->userdata[$item])) ? NULL : $this->userdata[$item]; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Fetch all session data |
| 194 | * |
| 195 | * @return array User data array |
| 196 | */ |
| 197 | public function all_userdata() |
| 198 | { |
| 199 | // Return entire array |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 200 | return ( ! isset($this->userdata)) ? NULL : $this->userdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 204 | * Fetch all flashdata |
| 205 | * |
| 206 | * @return array Flash data array |
| 207 | */ |
| 208 | public function all_flashdata() |
| 209 | { |
| 210 | $out = array(); |
| 211 | |
| 212 | // loop through all userdata |
| 213 | foreach ($this->all_userdata() as $key => $val) |
| 214 | { |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 215 | // if it contains flashdata, add it |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 216 | if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE) |
| 217 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 218 | $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key); |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 219 | $out[$key] = $val; |
| 220 | } |
| 221 | } |
| 222 | return $out; |
| 223 | } |
| 224 | |
| 225 | /** |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 226 | * Add or change data in the "userdata" array |
| 227 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 228 | * @param mixed Item name or array of items |
| 229 | * @param string Item value or empty string |
| 230 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 231 | */ |
| 232 | public function set_userdata($newdata = array(), $newval = '') |
| 233 | { |
| 234 | // Wrap params as array if singular |
| 235 | if (is_string($newdata)) |
| 236 | { |
| 237 | $newdata = array($newdata => $newval); |
| 238 | } |
| 239 | |
| 240 | // Set each name/value pair |
| 241 | if (count($newdata) > 0) |
| 242 | { |
| 243 | foreach ($newdata as $key => $val) |
| 244 | { |
| 245 | $this->userdata[$key] = $val; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Tell driver data changed |
| 250 | $this->current->sess_save(); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Delete a session variable from the "userdata" array |
| 255 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 256 | * @param mixed Item name or array of item names |
| 257 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 258 | */ |
| 259 | public function unset_userdata($newdata = array()) |
| 260 | { |
| 261 | // Wrap single name as array |
| 262 | if (is_string($newdata)) |
| 263 | { |
| 264 | $newdata = array($newdata => ''); |
| 265 | } |
| 266 | |
| 267 | // Unset each item name |
| 268 | if (count($newdata) > 0) |
| 269 | { |
| 270 | foreach ($newdata as $key => $val) |
| 271 | { |
| 272 | unset($this->userdata[$key]); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Tell driver data changed |
| 277 | $this->current->sess_save(); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Determine if an item exists |
| 282 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 283 | * @param string Item name |
| 284 | * @return boolean |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 285 | */ |
| 286 | public function has_userdata($item) |
| 287 | { |
| 288 | // Check for item name |
| 289 | return isset($this->userdata[$item]); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Add or change flashdata, only available until the next request |
| 294 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 295 | * @param mixed Item name or array of items |
| 296 | * @param string Item value or empty string |
| 297 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 298 | */ |
| 299 | public function set_flashdata($newdata = array(), $newval = '') |
| 300 | { |
| 301 | // Wrap item as array if singular |
| 302 | if (is_string($newdata)) |
| 303 | { |
| 304 | $newdata = array($newdata => $newval); |
| 305 | } |
| 306 | |
| 307 | // Prepend each key name and set value |
| 308 | if (count($newdata) > 0) |
| 309 | { |
| 310 | foreach ($newdata as $key => $val) |
| 311 | { |
| 312 | $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key; |
| 313 | $this->set_userdata($flashdata_key, $val); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Keeps existing flashdata available to next request. |
| 320 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 321 | * @param string Item key |
| 322 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 323 | */ |
| 324 | public function keep_flashdata($key) |
| 325 | { |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 326 | // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep() |
| 327 | // Note the function will return NULL if the $key provided cannot be found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 328 | $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key; |
| 329 | $value = $this->userdata($old_flashdata_key); |
| 330 | |
| 331 | $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key; |
| 332 | $this->set_userdata($new_flashdata_key, $value); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Fetch a specific flashdata item from the session array |
| 337 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 338 | * @param string Item key |
| 339 | * @return string |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 340 | */ |
| 341 | public function flashdata($key) |
| 342 | { |
| 343 | // Prepend key and retrieve value |
| 344 | $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key; |
| 345 | return $this->userdata($flashdata_key); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Add or change tempdata, only available |
| 350 | * until expiration |
| 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 | * @param int Item lifetime in seconds or 0 for default |
| 355 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 356 | */ |
| 357 | public function set_tempdata($newdata = array(), $newval = '', $expire = 0) |
| 358 | { |
| 359 | // Set expiration time |
| 360 | $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF); |
| 361 | |
| 362 | // Wrap item as array if singular |
| 363 | if (is_string($newdata)) |
| 364 | { |
| 365 | $newdata = array($newdata => $newval); |
| 366 | } |
| 367 | |
| 368 | // Get or create expiration list |
| 369 | $expirations = $this->userdata(self::EXPIRATION_KEY); |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 370 | if ( ! $expirations) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 371 | { |
| 372 | $expirations = array(); |
| 373 | } |
| 374 | |
| 375 | // Prepend each key name and set value |
| 376 | if (count($newdata) > 0) |
| 377 | { |
| 378 | foreach ($newdata as $key => $val) |
| 379 | { |
| 380 | $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key; |
| 381 | $expirations[$tempdata_key] = $expire; |
| 382 | $this->set_userdata($tempdata_key, $val); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // Update expiration list |
| 387 | $this->set_userdata(self::EXPIRATION_KEY, $expirations); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Delete a temporary session variable from the "userdata" array |
| 392 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 393 | * @param mixed Item name or array of item names |
| 394 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 395 | */ |
| 396 | public function unset_tempdata($newdata = array()) |
| 397 | { |
| 398 | // Get expirations list |
| 399 | $expirations = $this->userdata(self::EXPIRATION_KEY); |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 400 | if ( ! $expirations || ! count($expirations)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 401 | { |
| 402 | // Nothing to do |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | // Wrap single name as array |
| 407 | if (is_string($newdata)) |
| 408 | { |
| 409 | $newdata = array($newdata => ''); |
| 410 | } |
| 411 | |
| 412 | // Prepend each item name and unset |
| 413 | if (count($newdata) > 0) |
| 414 | { |
| 415 | foreach ($newdata as $key => $val) |
| 416 | { |
| 417 | $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key; |
| 418 | unset($expirations[$tempdata_key]); |
| 419 | $this->unset_userdata($tempdata_key); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // Update expiration list |
| 424 | $this->set_userdata(self::EXPIRATION_KEY, $expirations); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Fetch a specific tempdata item from the session array |
| 429 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 430 | * @param string Item key |
| 431 | * @return string |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 432 | */ |
| 433 | public function tempdata($key) |
| 434 | { |
| 435 | // Prepend key and return value |
| 436 | $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key; |
| 437 | return $this->userdata($tempdata_key); |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Identifies flashdata as 'old' for removal |
| 442 | * when _flashdata_sweep() runs. |
| 443 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 444 | * @access protected |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 445 | * @return void |
| 446 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 447 | protected function _flashdata_mark() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 448 | { |
| 449 | $userdata = $this->all_userdata(); |
| 450 | foreach ($userdata as $name => $value) |
| 451 | { |
| 452 | $parts = explode(self::FLASHDATA_NEW, $name); |
| 453 | if (is_array($parts) && count($parts) === 2) |
| 454 | { |
| 455 | $new_name = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1]; |
| 456 | $this->set_userdata($new_name, $value); |
| 457 | $this->unset_userdata($name); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Removes all flashdata marked as 'old' |
| 464 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 465 | * @access protected |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 466 | * @return void |
| 467 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 468 | protected function _flashdata_sweep() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 469 | { |
| 470 | $userdata = $this->all_userdata(); |
| 471 | foreach ($userdata as $key => $value) |
| 472 | { |
| 473 | if (strpos($key, self::FLASHDATA_OLD)) |
| 474 | { |
| 475 | $this->unset_userdata($key); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Removes all expired tempdata |
| 482 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 483 | * @access protected |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 484 | * @return void |
| 485 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 486 | protected function _tempdata_sweep() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 487 | { |
| 488 | // Get expirations list |
| 489 | $expirations = $this->userdata(self::EXPIRATION_KEY); |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 490 | if ( ! $expirations || ! count($expirations)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 491 | { |
| 492 | // Nothing to do |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | // Unset expired elements |
| 497 | $now = time(); |
| 498 | $userdata = $this->all_userdata(); |
| 499 | foreach ($userdata as $key => $value) |
| 500 | { |
| 501 | if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now) |
| 502 | { |
| 503 | unset($expirations[$key]); |
| 504 | $this->unset_userdata($key); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // Update expiration list |
| 509 | $this->set_userdata(self::EXPIRATION_KEY, $expirations); |
| 510 | } |
| 511 | } |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 512 | // END CI_Session Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 513 | |
| 514 | |
| 515 | /** |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 516 | * CI_Session_driver Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 517 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 518 | * Extend this class to make a new CI_Session driver. |
| 519 | * A CI_Session driver basically manages an array of name/value pairs with some sort of storage mechanism. |
| 520 | * 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] | 521 | * session data. Then implement a save handler to write changed data to storage (sess_save), a destroy handler |
| 522 | * 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] | 523 | * Put your driver in the libraries/Session/drivers folder anywhere in the loader paths. This includes the |
| 524 | * application directory, the system directory, or any path you add with $CI->load->add_package_path(). |
| 525 | * Your driver must be named CI_Session_<name>, and your filename must be Session_<name>.php, |
| 526 | * preferably also capitalized. (e.g.: CI_Session_foo in libraries/Session/drivers/Session_foo.php) |
| 527 | * 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] | 528 | * object. (e.g.: $config['sess_driver'] = 'foo'; OR $CI->load->driver('session', array('sess_driver' => 'foo')); ) |
| 529 | * Already provided are the Native driver, which manages the native PHP $_SESSION array, and |
| 530 | * the Cookie driver, which manages the data in a browser cookie, with optional extra storage in a database table. |
| 531 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 532 | * @package CodeIgniter |
| 533 | * @subpackage Libraries |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 534 | * @category Sessions |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 535 | * @author ExpressionEngine Dev Team |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 536 | */ |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 537 | abstract class CI_Session_driver extends CI_Driver { |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 538 | /** |
| 539 | * Decorate |
| 540 | * |
| 541 | * Decorates the child with the parent driver lib's methods and properties |
| 542 | * |
| 543 | * @param object Parent library object |
| 544 | * @return void |
| 545 | */ |
| 546 | public function decorate($parent) |
| 547 | { |
| 548 | // Call base class decorate first |
| 549 | parent::decorate($parent); |
| 550 | |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 551 | // Call initialize method now that driver has access to $this->_parent |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 552 | $this->initialize(); |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * __call magic method |
| 557 | * |
| 558 | * Handles access to the parent driver library's methods |
| 559 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 560 | * @param string Library method name |
| 561 | * @param array Method arguments (default: none) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 562 | * @return mixed |
| 563 | */ |
| 564 | public function __call($method, $args = array()) |
| 565 | { |
| 566 | // Make sure the parent library uses this driver |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 567 | $this->_parent->select_driver(get_class($this)); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 568 | return parent::__call($method, $args); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Initialize driver |
| 573 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 574 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 575 | */ |
| 576 | protected function initialize() |
| 577 | { |
| 578 | // Overload this method to implement initialization |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Save the session data |
| 583 | * |
| 584 | * Data in the array has changed - perform any storage synchronization necessary |
| 585 | * The child class MUST implement this abstract method! |
| 586 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 587 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 588 | */ |
| 589 | abstract public function sess_save(); |
| 590 | |
| 591 | /** |
| 592 | * Destroy the current session |
| 593 | * |
| 594 | * Clean up storage for this session - it has been terminated |
| 595 | * The child class MUST implement this abstract method! |
| 596 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 597 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 598 | */ |
| 599 | abstract public function sess_destroy(); |
| 600 | |
| 601 | /** |
| 602 | * Regenerate the current session |
| 603 | * |
| 604 | * Regenerate the session id |
| 605 | * The child class MUST implement this abstract method! |
| 606 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 607 | * @param boolean Destroy session data flag (default: false) |
| 608 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 609 | */ |
| 610 | abstract public function sess_regenerate($destroy = false); |
| 611 | |
| 612 | /** |
| 613 | * Get a reference to user data array |
| 614 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 615 | * Give array access to the main CI_Session object |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 616 | * The child class MUST implement this abstract method! |
| 617 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 618 | * @return array Reference to userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 619 | */ |
| 620 | abstract public function &get_userdata(); |
| 621 | } |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 622 | // END CI_Session_driver Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 623 | |
| 624 | |
| 625 | /* End of file Session.php */ |
| 626 | /* Location: ./system/libraries/Session/Session.php */ |
| 627 | ?> |