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