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 | aee9265 | 2012-08-26 21:45:35 -0400 | [diff] [blame] | 144 | // See if driver is already current |
| 145 | if ($this->$child !== $this->current) { |
| 146 | // Make driver current and sync userdata |
| 147 | $this->current = $this->$child; |
| 148 | $this->userdata =& $this->current->get_userdata(); |
| 149 | } |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 150 | } |
| 151 | else |
| 152 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 153 | // Load new driver |
dchill42 | aee9265 | 2012-08-26 21:45:35 -0400 | [diff] [blame] | 154 | $this->load_driver($child); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Destroy the current session |
| 161 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 162 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 163 | */ |
| 164 | public function sess_destroy() |
| 165 | { |
| 166 | // Just call destroy on driver |
| 167 | $this->current->sess_destroy(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Regenerate the current session |
| 172 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 173 | * @param boolean Destroy session data flag (default: false) |
| 174 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 175 | */ |
| 176 | public function sess_regenerate($destroy = false) |
| 177 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 178 | // Call regenerate on driver and resync userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 179 | $this->current->sess_regenerate($destroy); |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 180 | $this->userdata =& $this->current->get_userdata(); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Fetch a specific item from the session array |
| 185 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 186 | * @param string Item key |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 187 | * @return string Item value or NULL if not found |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 188 | */ |
| 189 | public function userdata($item) |
| 190 | { |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 191 | // Return value or NULL if not found |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 192 | return ( ! isset($this->userdata[$item])) ? NULL : $this->userdata[$item]; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Fetch all session data |
| 197 | * |
| 198 | * @return array User data array |
| 199 | */ |
| 200 | public function all_userdata() |
| 201 | { |
| 202 | // Return entire array |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 203 | return ( ! isset($this->userdata)) ? NULL : $this->userdata; |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 207 | * Fetch all flashdata |
| 208 | * |
| 209 | * @return array Flash data array |
| 210 | */ |
| 211 | public function all_flashdata() |
| 212 | { |
| 213 | $out = array(); |
| 214 | |
| 215 | // loop through all userdata |
| 216 | foreach ($this->all_userdata() as $key => $val) |
| 217 | { |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 218 | // if it contains flashdata, add it |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 219 | if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE) |
| 220 | { |
dchill42 | 2642920 | 2012-07-31 10:55:07 -0400 | [diff] [blame] | 221 | $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key); |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 222 | $out[$key] = $val; |
| 223 | } |
| 224 | } |
| 225 | return $out; |
| 226 | } |
| 227 | |
| 228 | /** |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 229 | * Add or change data in the "userdata" array |
| 230 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 231 | * @param mixed Item name or array of items |
| 232 | * @param string Item value or empty string |
| 233 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 234 | */ |
| 235 | public function set_userdata($newdata = array(), $newval = '') |
| 236 | { |
| 237 | // Wrap params as array if singular |
| 238 | if (is_string($newdata)) |
| 239 | { |
| 240 | $newdata = array($newdata => $newval); |
| 241 | } |
| 242 | |
| 243 | // Set each name/value pair |
| 244 | if (count($newdata) > 0) |
| 245 | { |
| 246 | foreach ($newdata as $key => $val) |
| 247 | { |
| 248 | $this->userdata[$key] = $val; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // Tell driver data changed |
| 253 | $this->current->sess_save(); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Delete a session variable from the "userdata" array |
| 258 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 259 | * @param mixed Item name or array of item names |
| 260 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 261 | */ |
| 262 | public function unset_userdata($newdata = array()) |
| 263 | { |
| 264 | // Wrap single name as array |
| 265 | if (is_string($newdata)) |
| 266 | { |
| 267 | $newdata = array($newdata => ''); |
| 268 | } |
| 269 | |
| 270 | // Unset each item name |
| 271 | if (count($newdata) > 0) |
| 272 | { |
| 273 | foreach ($newdata as $key => $val) |
| 274 | { |
| 275 | unset($this->userdata[$key]); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // Tell driver data changed |
| 280 | $this->current->sess_save(); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Determine if an item exists |
| 285 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 286 | * @param string Item name |
| 287 | * @return boolean |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 288 | */ |
| 289 | public function has_userdata($item) |
| 290 | { |
| 291 | // Check for item name |
| 292 | return isset($this->userdata[$item]); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Add or change flashdata, only available until the next request |
| 297 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 298 | * @param mixed Item name or array of items |
| 299 | * @param string Item value or empty string |
| 300 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 301 | */ |
| 302 | public function set_flashdata($newdata = array(), $newval = '') |
| 303 | { |
| 304 | // Wrap item as array if singular |
| 305 | if (is_string($newdata)) |
| 306 | { |
| 307 | $newdata = array($newdata => $newval); |
| 308 | } |
| 309 | |
| 310 | // Prepend each key name and set value |
| 311 | if (count($newdata) > 0) |
| 312 | { |
| 313 | foreach ($newdata as $key => $val) |
| 314 | { |
| 315 | $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key; |
| 316 | $this->set_userdata($flashdata_key, $val); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Keeps existing flashdata available to next request. |
| 323 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 324 | * @param string Item key |
| 325 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 326 | */ |
| 327 | public function keep_flashdata($key) |
| 328 | { |
dchill42 | c5079de | 2012-07-23 10:53:47 -0400 | [diff] [blame] | 329 | // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep() |
| 330 | // 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] | 331 | $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key; |
| 332 | $value = $this->userdata($old_flashdata_key); |
| 333 | |
| 334 | $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key; |
| 335 | $this->set_userdata($new_flashdata_key, $value); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Fetch a specific flashdata item from the session array |
| 340 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 341 | * @param string Item key |
| 342 | * @return string |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 343 | */ |
| 344 | public function flashdata($key) |
| 345 | { |
| 346 | // Prepend key and retrieve value |
| 347 | $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key; |
| 348 | return $this->userdata($flashdata_key); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Add or change tempdata, only available |
| 353 | * until expiration |
| 354 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 355 | * @param mixed Item name or array of items |
| 356 | * @param string Item value or empty string |
| 357 | * @param int Item lifetime in seconds or 0 for default |
| 358 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 359 | */ |
| 360 | public function set_tempdata($newdata = array(), $newval = '', $expire = 0) |
| 361 | { |
| 362 | // Set expiration time |
| 363 | $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF); |
| 364 | |
| 365 | // Wrap item as array if singular |
| 366 | if (is_string($newdata)) |
| 367 | { |
| 368 | $newdata = array($newdata => $newval); |
| 369 | } |
| 370 | |
| 371 | // Get or create expiration list |
| 372 | $expirations = $this->userdata(self::EXPIRATION_KEY); |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 373 | if ( ! $expirations) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 374 | { |
| 375 | $expirations = array(); |
| 376 | } |
| 377 | |
| 378 | // Prepend each key name and set value |
| 379 | if (count($newdata) > 0) |
| 380 | { |
| 381 | foreach ($newdata as $key => $val) |
| 382 | { |
| 383 | $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key; |
| 384 | $expirations[$tempdata_key] = $expire; |
| 385 | $this->set_userdata($tempdata_key, $val); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // Update expiration list |
| 390 | $this->set_userdata(self::EXPIRATION_KEY, $expirations); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Delete a temporary session variable from the "userdata" array |
| 395 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 396 | * @param mixed Item name or array of item names |
| 397 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 398 | */ |
| 399 | public function unset_tempdata($newdata = array()) |
| 400 | { |
| 401 | // Get expirations list |
| 402 | $expirations = $this->userdata(self::EXPIRATION_KEY); |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 403 | if ( ! $expirations || ! count($expirations)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 404 | { |
| 405 | // Nothing to do |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | // Wrap single name as array |
| 410 | if (is_string($newdata)) |
| 411 | { |
| 412 | $newdata = array($newdata => ''); |
| 413 | } |
| 414 | |
| 415 | // Prepend each item name and unset |
| 416 | if (count($newdata) > 0) |
| 417 | { |
| 418 | foreach ($newdata as $key => $val) |
| 419 | { |
| 420 | $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key; |
| 421 | unset($expirations[$tempdata_key]); |
| 422 | $this->unset_userdata($tempdata_key); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Update expiration list |
| 427 | $this->set_userdata(self::EXPIRATION_KEY, $expirations); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Fetch a specific tempdata item from the session array |
| 432 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 433 | * @param string Item key |
| 434 | * @return string |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 435 | */ |
| 436 | public function tempdata($key) |
| 437 | { |
| 438 | // Prepend key and return value |
| 439 | $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key; |
| 440 | return $this->userdata($tempdata_key); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Identifies flashdata as 'old' for removal |
| 445 | * when _flashdata_sweep() runs. |
| 446 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 447 | * @access protected |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 448 | * @return void |
| 449 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 450 | protected function _flashdata_mark() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 451 | { |
| 452 | $userdata = $this->all_userdata(); |
| 453 | foreach ($userdata as $name => $value) |
| 454 | { |
| 455 | $parts = explode(self::FLASHDATA_NEW, $name); |
| 456 | if (is_array($parts) && count($parts) === 2) |
| 457 | { |
| 458 | $new_name = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1]; |
| 459 | $this->set_userdata($new_name, $value); |
| 460 | $this->unset_userdata($name); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Removes all flashdata marked as 'old' |
| 467 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 468 | * @access protected |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 469 | * @return void |
| 470 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 471 | protected function _flashdata_sweep() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 472 | { |
| 473 | $userdata = $this->all_userdata(); |
| 474 | foreach ($userdata as $key => $value) |
| 475 | { |
| 476 | if (strpos($key, self::FLASHDATA_OLD)) |
| 477 | { |
| 478 | $this->unset_userdata($key); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Removes all expired tempdata |
| 485 | * |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 486 | * @access protected |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 487 | * @return void |
| 488 | */ |
Darren Hill | a2ae657 | 2011-09-01 07:36:26 -0400 | [diff] [blame] | 489 | protected function _tempdata_sweep() |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 490 | { |
| 491 | // Get expirations list |
| 492 | $expirations = $this->userdata(self::EXPIRATION_KEY); |
dchill42 | 77ee3fd | 2012-07-24 11:50:01 -0400 | [diff] [blame] | 493 | if ( ! $expirations || ! count($expirations)) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 494 | { |
| 495 | // Nothing to do |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | // Unset expired elements |
| 500 | $now = time(); |
| 501 | $userdata = $this->all_userdata(); |
| 502 | foreach ($userdata as $key => $value) |
| 503 | { |
| 504 | if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now) |
| 505 | { |
| 506 | unset($expirations[$key]); |
| 507 | $this->unset_userdata($key); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Update expiration list |
| 512 | $this->set_userdata(self::EXPIRATION_KEY, $expirations); |
| 513 | } |
| 514 | } |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 515 | // END CI_Session Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 516 | |
| 517 | |
| 518 | /** |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 519 | * CI_Session_driver Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 520 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 521 | * Extend this class to make a new CI_Session driver. |
| 522 | * A CI_Session driver basically manages an array of name/value pairs with some sort of storage mechanism. |
| 523 | * 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] | 524 | * session data. Then implement a save handler to write changed data to storage (sess_save), a destroy handler |
| 525 | * 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] | 526 | * Put your driver in the libraries/Session/drivers folder anywhere in the loader paths. This includes the |
| 527 | * application directory, the system directory, or any path you add with $CI->load->add_package_path(). |
| 528 | * Your driver must be named CI_Session_<name>, and your filename must be Session_<name>.php, |
| 529 | * preferably also capitalized. (e.g.: CI_Session_foo in libraries/Session/drivers/Session_foo.php) |
| 530 | * 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] | 531 | * object. (e.g.: $config['sess_driver'] = 'foo'; OR $CI->load->driver('session', array('sess_driver' => 'foo')); ) |
| 532 | * Already provided are the Native driver, which manages the native PHP $_SESSION array, and |
| 533 | * the Cookie driver, which manages the data in a browser cookie, with optional extra storage in a database table. |
| 534 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 535 | * @package CodeIgniter |
| 536 | * @subpackage Libraries |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 537 | * @category Sessions |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 538 | * @author ExpressionEngine Dev Team |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 539 | */ |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 540 | abstract class CI_Session_driver extends CI_Driver { |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 541 | /** |
| 542 | * Decorate |
| 543 | * |
| 544 | * Decorates the child with the parent driver lib's methods and properties |
| 545 | * |
| 546 | * @param object Parent library object |
| 547 | * @return void |
| 548 | */ |
| 549 | public function decorate($parent) |
| 550 | { |
| 551 | // Call base class decorate first |
| 552 | parent::decorate($parent); |
| 553 | |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 554 | // Call initialize method now that driver has access to $this->_parent |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 555 | $this->initialize(); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * __call magic method |
| 560 | * |
| 561 | * Handles access to the parent driver library's methods |
| 562 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 563 | * @param string Library method name |
| 564 | * @param array Method arguments (default: none) |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 565 | * @return mixed |
| 566 | */ |
| 567 | public function __call($method, $args = array()) |
| 568 | { |
| 569 | // Make sure the parent library uses this driver |
dchill42 | c587225 | 2012-07-30 14:53:11 -0400 | [diff] [blame] | 570 | $this->_parent->select_driver(get_class($this)); |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 571 | return parent::__call($method, $args); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Initialize driver |
| 576 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 577 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 578 | */ |
| 579 | protected function initialize() |
| 580 | { |
| 581 | // Overload this method to implement initialization |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Save the session data |
| 586 | * |
| 587 | * Data in the array has changed - perform any storage synchronization necessary |
| 588 | * The child class MUST implement this abstract method! |
| 589 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 590 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 591 | */ |
| 592 | abstract public function sess_save(); |
| 593 | |
| 594 | /** |
| 595 | * Destroy the current session |
| 596 | * |
| 597 | * Clean up storage for this session - it has been terminated |
| 598 | * The child class MUST implement this abstract method! |
| 599 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 600 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 601 | */ |
| 602 | abstract public function sess_destroy(); |
| 603 | |
| 604 | /** |
| 605 | * Regenerate the current session |
| 606 | * |
| 607 | * Regenerate the session id |
| 608 | * The child class MUST implement this abstract method! |
| 609 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 610 | * @param boolean Destroy session data flag (default: false) |
| 611 | * @return void |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 612 | */ |
| 613 | abstract public function sess_regenerate($destroy = false); |
| 614 | |
| 615 | /** |
| 616 | * Get a reference to user data array |
| 617 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 618 | * Give array access to the main CI_Session object |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 619 | * The child class MUST implement this abstract method! |
| 620 | * |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 621 | * @return array Reference to userdata |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 622 | */ |
| 623 | abstract public function &get_userdata(); |
| 624 | } |
Darren Hill | 5073a37 | 2011-08-31 13:54:19 -0400 | [diff] [blame] | 625 | // END CI_Session_driver Class |
Darren Hill | c4e266b | 2011-08-30 15:40:27 -0400 | [diff] [blame] | 626 | |
| 627 | |
| 628 | /* End of file Session.php */ |
| 629 | /* Location: ./system/libraries/Session/Session.php */ |
| 630 | ?> |