Update system/libraries/Session/Session.php #1860

Closed
wants to merge 1 commit into
from
View
1,151 system/libraries/Session/Session.php
@@ -1,4 +1,7 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
+
+if (!defined('BASEPATH'))
+ exit('No direct script access allowed');
/**
* CodeIgniter
*
@@ -51,513 +54,457 @@
*/
class CI_Session extends CI_Driver_Library {
- public $params = array();
- protected $current = NULL;
- protected $userdata = array();
-
- const FLASHDATA_KEY = 'flash';
- const FLASHDATA_NEW = ':new:';
- const FLASHDATA_OLD = ':old:';
- const FLASHDATA_EXP = ':exp:';
- const EXPIRATION_KEY = '__expirations';
- const TEMP_EXP_DEF = 300;
-
- /**
- * CI_Session constructor
- *
- * The constructor loads the configured driver ('sess_driver' in config.php or as a parameter), running
- * routines in its constructor, and manages flashdata aging.
- *
- * @param array Configuration parameters
- */
- public function __construct(array $params = array())
- {
- log_message('debug', 'CI_Session Class Initialized');
-
- // Get valid drivers list
- $CI =& get_instance();
- $this->valid_drivers = array(
- 'Session_native',
- 'Session_cookie'
- );
- $key = 'sess_valid_drivers';
- $drivers = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
- if ($drivers)
- {
- is_array($drivers) OR $drivers = array($drivers);
-
- // Add driver names to valid list
- foreach ($drivers as $driver)
- {
- if ( ! in_array(strtolower($driver), array_map('strtolower', $this->valid_drivers)))
- {
- $this->valid_drivers[] = $driver;
- }
- }
- }
-
- // Get driver to load
- $key = 'sess_driver';
- $driver = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
- if ( ! $driver)
- {
- $driver = 'cookie';
- }
-
- if ( ! in_array('session_'.strtolower($driver), array_map('strtolower', $this->valid_drivers)))
- {
- $this->valid_drivers[] = 'Session_'.$driver;
- }
-
- // Save a copy of parameters in case drivers need access
- $this->params = $params;
-
- // Load driver and get array reference
- $this->load_driver($driver);
-
- // Delete 'old' flashdata (from last request)
- $this->_flashdata_sweep();
-
- // Mark all new flashdata as old (data will be deleted before next request)
- $this->_flashdata_mark();
-
- // Delete expired tempdata
- $this->_tempdata_sweep();
-
- log_message('debug', 'CI_Session routines successfully run');
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Loads session storage driver
- *
- * @param string Driver classname
- * @return object Loaded driver object
- */
- public function load_driver($driver)
- {
- // Save reference to most recently loaded driver as library default and sync userdata
- $this->current = parent::load_driver($driver);
- $this->userdata =& $this->current->get_userdata();
- return $this->current;
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Select default session storage driver
- *
- * @param string Driver classname
- * @return void
- */
- public function select_driver($driver)
- {
- // Validate driver name
- $lowername = strtolower(str_replace('CI_', '', $driver));
- if (in_array($lowername, array_map('strtolower', $this->valid_drivers)))
- {
- // See if driver is loaded
- $child = str_replace($this->lib_name.'_', '', $driver);
- if (isset($this->$child))
- {
- // See if driver is already current
- if ($this->$child !== $this->current)
- {
- // Make driver current and sync userdata
- $this->current = $this->$child;
- $this->userdata =& $this->current->get_userdata();
- }
- }
- else
- {
- // Load new driver
- $this->load_driver($child);
- }
- }
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Destroy the current session
- *
- * @return void
- */
- public function sess_destroy()
- {
- // Just call destroy on driver
- $this->current->sess_destroy();
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Regenerate the current session
- *
- * @param bool Destroy session data flag (default: false)
- * @return void
- */
- public function sess_regenerate($destroy = FALSE)
- {
- // Call regenerate on driver and resync userdata
- $this->current->sess_regenerate($destroy);
- $this->userdata =& $this->current->get_userdata();
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Fetch a specific item from the session array
- *
- * @param string Item key
- * @return string Item value or NULL if not found
- */
- public function userdata($item)
- {
- return isset($this->userdata[$item]) ? $this->userdata[$item] : NULL;
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Fetch all session data
- *
- * @return array User data array
- */
- public function all_userdata()
- {
- return isset($this->userdata) ? $this->userdata : NULL;
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Fetch all flashdata
- *
- * @return array Flash data array
- */
- public function all_flashdata()
- {
- $out = array();
-
- // loop through all userdata
- foreach ($this->all_userdata() as $key => $val)
- {
- // if it contains flashdata, add it
- if (strpos($key, self::FLASHDATA_KEY.self::FLASHDATA_OLD) !== FALSE)
- {
- $key = str_replace(self::FLASHDATA_KEY.self::FLASHDATA_OLD, '', $key);
- $out[$key] = $val;
- }
- }
- return $out;
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Add or change data in the "userdata" array
- *
- * @param mixed Item name or array of items
- * @param string Item value or empty string
- * @return void
- */
- public function set_userdata($newdata = array(), $newval = '')
- {
- // Wrap params as array if singular
- if (is_string($newdata))
- {
- $newdata = array($newdata => $newval);
- }
-
- // Set each name/value pair
- if (count($newdata) > 0)
- {
- foreach ($newdata as $key => $val)
- {
- $this->userdata[$key] = $val;
- }
- }
-
- // Tell driver data changed
- $this->current->sess_save();
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Delete a session variable from the "userdata" array
- *
- * @param mixed Item name or array of item names
- * @return void
- */
- public function unset_userdata($newdata = array())
- {
- // Wrap single name as array
- if (is_string($newdata))
- {
- $newdata = array($newdata => '');
- }
-
- // Unset each item name
- if (count($newdata) > 0)
- {
- foreach (array_keys($newdata) as $key)
- {
- unset($this->userdata[$key]);
- }
- }
-
- // Tell driver data changed
- $this->current->sess_save();
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Determine if an item exists
- *
- * @param string Item name
- * @return bool
- */
- public function has_userdata($item)
- {
- return isset($this->userdata[$item]);
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Add or change flashdata, only available until the next request
- *
- * @param mixed Item name or array of items
- * @param string Item value or empty string
- * @return void
- */
- public function set_flashdata($newdata = array(), $newval = '')
- {
- // Wrap item as array if singular
- if (is_string($newdata))
- {
- $newdata = array($newdata => $newval);
- }
-
- // Prepend each key name and set value
- if (count($newdata) > 0)
- {
- foreach ($newdata as $key => $val)
- {
- $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
- $this->set_userdata($flashdata_key, $val);
- }
- }
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Keeps existing flashdata available to next request.
- *
- * @param string Item key
- * @return void
- */
- public function keep_flashdata($key)
- {
- // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep()
- // Note the function will return NULL if the $key provided cannot be found
- $old_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
- $value = $this->userdata($old_flashdata_key);
-
- $new_flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_NEW.$key;
- $this->set_userdata($new_flashdata_key, $value);
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Fetch a specific flashdata item from the session array
- *
- * @param string Item key
- * @return string
- */
- public function flashdata($key)
- {
- // Prepend key and retrieve value
- $flashdata_key = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$key;
- return $this->userdata($flashdata_key);
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Add or change tempdata, only available until expiration
- *
- * @param mixed Item name or array of items
- * @param string Item value or empty string
- * @param int Item lifetime in seconds or 0 for default
- * @return void
- */
- public function set_tempdata($newdata = array(), $newval = '', $expire = 0)
- {
- // Set expiration time
- $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF);
-
- // Wrap item as array if singular
- if (is_string($newdata))
- {
- $newdata = array($newdata => $newval);
- }
-
- // Get or create expiration list
- $expirations = $this->userdata(self::EXPIRATION_KEY);
- if ( ! $expirations)
- {
- $expirations = array();
- }
-
- // Prepend each key name and set value
- if (count($newdata) > 0)
- {
- foreach ($newdata as $key => $val)
- {
- $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
- $expirations[$tempdata_key] = $expire;
- $this->set_userdata($tempdata_key, $val);
- }
- }
-
- // Update expiration list
- $this->set_userdata(self::EXPIRATION_KEY, $expirations);
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Delete a temporary session variable from the "userdata" array
- *
- * @param mixed Item name or array of item names
- * @return void
- */
- public function unset_tempdata($newdata = array())
- {
- // Get expirations list
- $expirations = $this->userdata(self::EXPIRATION_KEY);
- if (empty($expirations))
- {
- // Nothing to do
- return;
- }
-
- // Wrap single name as array
- if (is_string($newdata))
- {
- $newdata = array($newdata => '');
- }
-
- // Prepend each item name and unset
- if (count($newdata) > 0)
- {
- foreach (array_keys($newdata) as $key)
- {
- $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
- unset($expirations[$tempdata_key]);
- $this->unset_userdata($tempdata_key);
- }
- }
-
- // Update expiration list
- $this->set_userdata(self::EXPIRATION_KEY, $expirations);
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Fetch a specific tempdata item from the session array
- *
- * @param string Item key
- * @return string
- */
- public function tempdata($key)
- {
- // Prepend key and return value
- $tempdata_key = self::FLASHDATA_KEY.self::FLASHDATA_EXP.$key;
- return $this->userdata($tempdata_key);
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Identifies flashdata as 'old' for removal
- * when _flashdata_sweep() runs.
- *
- * @return void
- */
- protected function _flashdata_mark()
- {
- foreach ($this->all_userdata() as $name => $value)
- {
- $parts = explode(self::FLASHDATA_NEW, $name);
- if (is_array($parts) && count($parts) === 2)
- {
- $new_name = self::FLASHDATA_KEY.self::FLASHDATA_OLD.$parts[1];
- $this->set_userdata($new_name, $value);
- $this->unset_userdata($name);
- }
- }
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Removes all flashdata marked as 'old'
- *
- * @return void
- */
- protected function _flashdata_sweep()
- {
- $userdata = $this->all_userdata();
- foreach (array_keys($userdata) as $key)
- {
- if (strpos($key, self::FLASHDATA_OLD))
- {
- $this->unset_userdata($key);
- }
- }
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Removes all expired tempdata
- *
- * @return void
- */
- protected function _tempdata_sweep()
- {
- // Get expirations list
- $expirations = $this->userdata(self::EXPIRATION_KEY);
- if (empty($expirations))
- {
- // Nothing to do
- return;
- }
-
- // Unset expired elements
- $now = time();
- $userdata = $this->all_userdata();
- foreach (array_keys($userdata) as $key)
- {
- if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now)
- {
- unset($expirations[$key]);
- $this->unset_userdata($key);
- }
- }
-
- // Update expiration list
- $this->set_userdata(self::EXPIRATION_KEY, $expirations);
- }
+ public $params = array();
+ protected $current = NULL;
+ protected $userdata = array();
+
+ const FLASHDATA_KEY = 'flash';
+ const FLASHDATA_NEW = ':new:';
+ const FLASHDATA_OLD = ':old:';
+ const FLASHDATA_EXP = ':exp:';
+ const EXPIRATION_KEY = '__expirations';
+ const TEMP_EXP_DEF = 300;
+
+ /**
+ * CI_Session constructor
+ *
+ * The constructor loads the configured driver ('sess_driver' in config.php or as a parameter), running
+ * routines in its constructor, and manages flashdata aging.
+ *
+ * @param array Configuration parameters
+ */
+ public function __construct(array $params = array()) {
+ log_message('debug', 'CI_Session Class Initialized');
+
+ // Get valid drivers list
+ $CI = & get_instance();
+ $this->valid_drivers = array(
+ 'Session_native',
+ 'Session_cookie'
+ );
+ $key = 'sess_valid_drivers';
+ $drivers = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
+ if ($drivers) {
+ is_array($drivers) OR $drivers = array($drivers);
+
+ // Add driver names to valid list
+ foreach ($drivers as $driver) {
+ if (!in_array(strtolower($driver), array_map('strtolower', $this->valid_drivers))) {
+ $this->valid_drivers[] = $driver;
+ }
+ }
+ }
+
+ // Get driver to load
+ $key = 'sess_driver';
+ $driver = isset($params[$key]) ? $params[$key] : $CI->config->item($key);
+ if (!$driver) {
+ $driver = 'cookie';
+ }
+
+ if (!in_array('session_' . strtolower($driver), array_map('strtolower', $this->valid_drivers))) {
+ $this->valid_drivers[] = 'Session_' . $driver;
+ }
+
+ // Save a copy of parameters in case drivers need access
+ $this->params = $params;
+
+ // Load driver and get array reference
+ $this->load_driver($driver);
+
+ // Delete 'old' flashdata (from last request)
+ $this->_flashdata_sweep();
+
+ // Mark all new flashdata as old (data will be deleted before next request)
+ $this->_flashdata_mark();
+
+ // Delete expired tempdata
+ $this->_tempdata_sweep();
+
+ log_message('debug', 'CI_Session routines successfully run');
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Loads session storage driver
+ *
+ * @param string Driver classname
+ * @return object Loaded driver object
+ */
+ public function load_driver($driver) {
+ // Save reference to most recently loaded driver as library default and sync userdata
+ $this->current = parent::load_driver($driver);
+ $this->userdata = & $this->current->get_userdata();
+ return $this->current;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Select default session storage driver
+ *
+ * @param string Driver classname
+ * @return void
+ */
+ public function select_driver($driver) {
+ // Validate driver name
+ $lowername = strtolower(str_replace('CI_', '', $driver));
+ if (in_array($lowername, array_map('strtolower', $this->valid_drivers))) {
+ // See if driver is loaded
+ $child = str_replace($this->lib_name . '_', '', $driver);
+ if (isset($this->$child)) {
+ // See if driver is already current
+ if ($this->$child !== $this->current) {
+ // Make driver current and sync userdata
+ $this->current = $this->$child;
+ $this->userdata = & $this->current->get_userdata();
+ }
+ } else {
+ // Load new driver
+ $this->load_driver($child);
+ }
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Destroy the current session
+ *
+ * @return void
+ */
+ public function sess_destroy() {
+ // Just call destroy on driver
+ $this->current->sess_destroy();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Regenerate the current session
+ *
+ * @param bool Destroy session data flag (default: false)
+ * @return void
+ */
+ public function sess_regenerate($destroy = FALSE) {
+ // Call regenerate on driver and resync userdata
+ $this->current->sess_regenerate($destroy);
+ $this->userdata = & $this->current->get_userdata();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Fetch a specific item from the session array
+ *
+ * @param string Item key
+ * @return string Item value or NULL if not found
+ */
+ public function userdata($item) {
+ return isset($this->userdata[$item]) ? $this->userdata[$item] : NULL;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Fetch all session data
+ *
+ * @return array User data array
+ */
+ public function all_userdata() {
+ return isset($this->userdata) ? $this->userdata : NULL;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Fetch all flashdata
+ *
+ * @return array Flash data array
+ */
+ public function all_flashdata() {
+ $out = array();
+
+ // loop through all userdata
+ foreach ($this->all_userdata() as $key => $val) {
+ // if it contains flashdata, add it
+ if (strpos($key, self::FLASHDATA_KEY . self::FLASHDATA_OLD) !== FALSE) {
+ $key = str_replace(self::FLASHDATA_KEY . self::FLASHDATA_OLD, '', $key);
+ $out[$key] = $val;
+ }
+ }
+ return $out;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Add or change data in the "userdata" array
+ *
+ * @param mixed Item name or array of items
+ * @param string Item value or empty string
+ * @return void
+ */
+ public function set_userdata($newdata = array(), $newval = '') {
+ // Wrap params as array if singular
+ if (is_string($newdata)) {
+ $newdata = array($newdata => $newval);
+ }
+
+ // Set each name/value pair
+ if (count($newdata) > 0) {
+ foreach ($newdata as $key => $val) {
+ $this->userdata[$key] = $val;
+ }
+ }
+
+ // Tell driver data changed
+ $this->current->sess_save();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Delete a session variable from the "userdata" array
+ *
+ * @param mixed Item name or array of item names
+ * @return void
+ */
+ public function unset_userdata($newdata = array()) {
+ // Wrap single name as array
+ if (is_string($newdata)) {
+ $newdata = array($newdata => '');
+ }
+
+ // Unset each item name
+ if (count($newdata) > 0) {
+ foreach (array_keys($newdata) as $key) {
+ unset($this->userdata[$key]);
+ }
+ }
+
+ // Tell driver data changed
+ $this->current->sess_save();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Determine if an item exists
+ *
+ * @param string Item name
+ * @return bool
+ */
+ public function has_userdata($item) {
+ return isset($this->userdata[$item]);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Add or change flashdata, only available until the next request
+ *
+ * @param mixed Item name or array of items
+ * @param string Item value or empty string
+ * @return void
+ */
+ public function set_flashdata($newdata = array(), $newval = '') {
+ // Wrap item as array if singular
+ if (is_string($newdata)) {
+ $newdata = array($newdata => $newval);
+ }
+
+ // Prepend each key name and set value
+ if (count($newdata) > 0) {
+ foreach ($newdata as $key => $val) {
+ $flashdata_key = self::FLASHDATA_KEY . self::FLASHDATA_NEW . $key;
+ $this->set_userdata($flashdata_key, $val);
+ }
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Keeps existing flashdata available to next request.
+ *
+ * @param string Item key
+ * @return void
+ */
+ public function keep_flashdata($key) {
+ // 'old' flashdata gets removed. Here we mark all flashdata as 'new' to preserve it from _flashdata_sweep()
+ // Note the function will return NULL if the $key provided cannot be found
+ $old_flashdata_key = self::FLASHDATA_KEY . self::FLASHDATA_OLD . $key;
+ $value = $this->userdata($old_flashdata_key);
+
+ $new_flashdata_key = self::FLASHDATA_KEY . self::FLASHDATA_NEW . $key;
+ $this->set_userdata($new_flashdata_key, $value);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Fetch a specific flashdata item from the session array
+ *
+ * @param string Item key
+ * @return string
+ */
+ public function flashdata($key) {
+ // Prepend key and retrieve value
+ $flashdata_key = self::FLASHDATA_KEY . self::FLASHDATA_OLD . $key;
+ return $this->userdata($flashdata_key);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Add or change tempdata, only available until expiration
+ *
+ * @param mixed Item name or array of items
+ * @param string Item value or empty string
+ * @param int Item lifetime in seconds or 0 for default
+ * @return void
+ */
+ public function set_tempdata($newdata = array(), $newval = '', $expire = 0) {
+ // Set expiration time
+ $expire = time() + ($expire ? $expire : self::TEMP_EXP_DEF);
+
+ // Wrap item as array if singular
+ if (is_string($newdata)) {
+ $newdata = array($newdata => $newval);
+ }
+
+ // Get or create expiration list
+ $expirations = $this->userdata(self::EXPIRATION_KEY);
+ if (!$expirations) {
+ $expirations = array();
+ }
+
+ // Prepend each key name and set value
+ if (count($newdata) > 0) {
+ foreach ($newdata as $key => $val) {
+ $tempdata_key = self::FLASHDATA_KEY . self::FLASHDATA_EXP . $key;
+ $expirations[$tempdata_key] = $expire;
+ $this->set_userdata($tempdata_key, $val);
+ }
+ }
+
+ // Update expiration list
+ $this->set_userdata(self::EXPIRATION_KEY, $expirations);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Delete a temporary session variable from the "userdata" array
+ *
+ * @param mixed Item name or array of item names
+ * @return void
+ */
+ public function unset_tempdata($newdata = array()) {
+ // Get expirations list
+ $expirations = $this->userdata(self::EXPIRATION_KEY);
+ if (empty($expirations)) {
+ // Nothing to do
+ return;
+ }
+
+ // Wrap single name as array
+ if (is_string($newdata)) {
+ $newdata = array($newdata => '');
+ }
+
+ // Prepend each item name and unset
+ if (count($newdata) > 0) {
+ foreach (array_keys($newdata) as $key) {
+ $tempdata_key = self::FLASHDATA_KEY . self::FLASHDATA_EXP . $key;
+ unset($expirations[$tempdata_key]);
+ $this->unset_userdata($tempdata_key);
+ }
+ }
+
+ // Update expiration list
+ $this->set_userdata(self::EXPIRATION_KEY, $expirations);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Fetch a specific tempdata item from the session array
+ *
+ * @param string Item key
+ * @return string
+ */
+ public function tempdata($key) {
+ // Prepend key and return value
+ $tempdata_key = self::FLASHDATA_KEY . self::FLASHDATA_EXP . $key;
+ return $this->userdata($tempdata_key);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Identifies flashdata as 'old' for removal
+ * when _flashdata_sweep() runs.
+ *
+ * @return void
+ */
+ protected function _flashdata_mark() {
+ foreach ($this->all_userdata() as $name => $value) {
+ $parts = explode(self::FLASHDATA_NEW, $name);
+ if (is_array($parts) && count($parts) === 2) {
+ $new_name = self::FLASHDATA_KEY . self::FLASHDATA_OLD . $parts[1];
+ $this->set_userdata($new_name, $value);
+ $this->unset_userdata($name);
+ }
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Removes all flashdata marked as 'old'
+ *
+ * @return void
+ */
+ protected function _flashdata_sweep() {
+ $userdata = $this->all_userdata();
+ foreach (array_keys($userdata) as $key) {
+ if (strpos($key, self::FLASHDATA_OLD)) {
+ $this->unset_userdata($key);
+ }
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Removes all expired tempdata
+ *
+ * @return void
+ */
+ protected function _tempdata_sweep() {
+ // Get expirations list
+ $expirations = $this->userdata(self::EXPIRATION_KEY);
+ if (empty($expirations)) {
+ // Nothing to do
+ return;
+ }
+
+ // Unset expired elements
+ $now = time();
+ $userdata = $this->all_userdata();
+ foreach (array_keys($userdata) as $key) {
+ if (strpos($key, self::FLASHDATA_EXP) && $expirations[$key] < $now) {
+ unset($expirations[$key]);
+ $this->unset_userdata($key);
+ }
+ }
+
+ // Update expiration list
+ $this->set_userdata(self::EXPIRATION_KEY, $expirations);
+ }
}
@@ -587,102 +534,98 @@ protected function _tempdata_sweep()
*/
abstract class CI_Session_driver extends CI_Driver {
- /**
- * Decorate
- *
- * Decorates the child with the parent driver lib's methods and properties
- *
- * @param object Parent library object
- * @return void
- */
- public function decorate($parent)
- {
- // Call base class decorate first
- parent::decorate($parent);
-
- // Call initialize method now that driver has access to $this->_parent
- $this->initialize();
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * __call magic method
- *
- * Handles access to the parent driver library's methods
- *
- * @param string Library method name
- * @param array Method arguments (default: none)
- * @return mixed
- */
- public function __call($method, $args = array())
- {
- // Make sure the parent library uses this driver
- $this->_parent->select_driver(get_class($this));
- return parent::__call($method, $args);
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Initialize driver
- *
- * @return void
- */
- protected function initialize()
- {
- // Overload this method to implement initialization
- }
-
- // ------------------------------------------------------------------------
-
- /**
- * Save the session data
- *
- * Data in the array has changed - perform any storage synchronization
- * necessary. The child class MUST implement this abstract method!
- *
- * @return void
- */
- abstract public function sess_save();
-
- // ------------------------------------------------------------------------
-
- /**
- * Destroy the current session
- *
- * Clean up storage for this session - it has been terminated.
- * The child class MUST implement this abstract method!
- *
- * @return void
- */
- abstract public function sess_destroy();
-
- // ------------------------------------------------------------------------
-
- /**
- * Regenerate the current session
- *
- * Regenerate the session ID.
- * The child class MUST implement this abstract method!
- *
- * @param bool Destroy session data flag (default: false)
- * @return void
- */
- abstract public function sess_regenerate($destroy = FALSE);
-
- // ------------------------------------------------------------------------
-
- /**
- * Get a reference to user data array
- *
- * Give array access to the main CI_Session object.
- * The child class MUST implement this abstract method!
- *
- * @return array Reference to userdata
- */
- abstract public function &get_userdata();
-
+ /**
+ * Decorate
+ *
+ * Decorates the child with the parent driver lib's methods and properties
+ *
+ * @param object Parent library object
+ * @return void
+ */
+ public function decorate($parent) {
+ // Call base class decorate first
+ parent::decorate($parent);
+
+ // Call initialize method now that driver has access to $this->_parent
+ $this->initialize();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * __call magic method
+ *
+ * Handles access to the parent driver library's methods
+ *
+ * @param string Library method name
+ * @param array Method arguments (default: none)
+ * @return mixed
+ */
+ public function __call($method, $args = array()) {
+ // Make sure the parent library uses this driver
+ $this->_parent->select_driver(get_class($this));
+ return parent::__call($method, $args);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Initialize driver
+ *
+ * @return void
+ */
+ protected function initialize() {
+ // Overload this method to implement initialization
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Save the session data
+ *
+ * Data in the array has changed - perform any storage synchronization
+ * necessary. The child class MUST implement this abstract method!
+ *
+ * @return void
+ */
+ abstract public function sess_save();
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Destroy the current session
+ *
+ * Clean up storage for this session - it has been terminated.
+ * The child class MUST implement this abstract method!
+ *
+ * @return void
+ */
+ abstract public function sess_destroy();
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Regenerate the current session
+ *
+ * Regenerate the session ID.
+ * The child class MUST implement this abstract method!
+ *
+ * @param bool Destroy session data flag (default: false)
+ * @return void
+ */
+ abstract public function sess_regenerate($destroy = FALSE);
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get a reference to user data array
+ *
+ * Give array access to the main CI_Session object.
+ * The child class MUST implement this abstract method!
+ *
+ * @return array Reference to userdata
+ */
+ abstract public function &get_userdata();
}
/* End of file Session.php */