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