From c4e266b87f39d521ff1002fefa9df809c6b9bd61 Mon Sep 17 00:00:00 2001 From: Darren Hill Date: Tue, 30 Aug 2011 15:40:27 -0400 Subject: Added Session driver with native PHP sessions and original-flavor CI cookie sessions --- .../libraries/Session/drivers/Session_cookie.php | 583 +++++++++++++++++++++ 1 file changed, 583 insertions(+) create mode 100755 system/libraries/Session/drivers/Session_cookie.php (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php new file mode 100755 index 000000000..0982b1e01 --- /dev/null +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -0,0 +1,583 @@ +CI =& get_instance(); + + // Set all the session preferences, which can either be set + // manually via the $params array above or via the config file + foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', + 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', + 'cookie_domain', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key) + { + $this->$key = (isset($this->parent->params[$key])) ? $this->parent->params[$key] : $this->CI->config->item($key); + } + + if ($this->encryption_key == '') + { + show_error('In order to use the Cookie Session driver you are required to set an encryption key '. + 'in your config file.'); + } + + // Load the string helper so we can use the strip_slashes() function + $this->CI->load->helper('string'); + + // Do we need encryption? If so, load the encryption class + if ($this->sess_encrypt_cookie == TRUE) + { + $this->CI->load->library('encrypt'); + } + + // Are we using a database? If so, load it + if ($this->sess_use_database === TRUE && $this->sess_table_name != '') + { + $this->CI->load->database(); + } + + // Set the "now" time. Can either be GMT or server time, based on the config prefs. + // We use this to set the "last activity" time + $this->now = $this->_get_time(); + + // Set the session length. If the session expiration is + // set to zero we'll set the expiration two years from now. + if ($this->sess_expiration == 0) + { + $this->sess_expiration = (60*60*24*365*2); + } + + // Set the cookie name + $this->sess_cookie_name = $this->cookie_prefix.$this->sess_cookie_name; + + // Run the Session routine. If a session doesn't exist we'll + // create a new one. If it does, we'll update it. + if ( ! $this->_sess_read()) + { + $this->_sess_create(); + } + else + { + $this->_sess_update(); + } + + // Delete expired sessions if necessary + $this->_sess_gc(); + } + + /** + * Write the session data + * + * @return void + */ + public function sess_save() + { + // Are we saving custom data to the DB? If not, all we do is update the cookie + if ($this->sess_use_database === FALSE) + { + $this->_set_cookie(); + return; + } + + // set the custom userdata, the session data we will set in a second + $custom_userdata = $this->all_userdata(); + $cookie_userdata = array(); + + // Before continuing, we need to determine if there is any custom data to deal with. + // Let's determine this by removing the default indexes to see if there's anything left in the array + // and set the session data while we're at it + foreach (array('session_id','ip_address','user_agent','last_activity') as $val) + { + unset($custom_userdata[$val]); + $cookie_userdata[$val] = $this->userdata($val); + } + + // Did we find any custom data? If not, we turn the empty array into a string + // since there's no reason to serialize and store an empty array in the DB + if (count($custom_userdata) === 0) + { + $custom_userdata = ''; + } + else + { + // Serialize the custom data array so we can store it + $custom_userdata = $this->_serialize($custom_userdata); + } + + // Run the update query + $this->CI->db->where('session_id', $this->userdata('session_id')); + $this->CI->db->update($this->sess_table_name, + array('last_activity' => $this->userdata('last_activity'), 'user_data' => $custom_userdata)); + + // Write the cookie. Notice that we manually pass the cookie data array to the + // _set_cookie() function. Normally that function will store $this->userdata, but + // in this case that array contains custom data, which we do not want in the cookie. + $this->_set_cookie($cookie_userdata); + } + + /** + * Destroy the current session + * + * @return void + */ + public function sess_destroy() + { + // Kill the session DB row + if ($this->sess_use_database === TRUE && $this->has_userdata('session_id')) + { + $this->CI->db->where('session_id', $this->userdata['session_id']); + $this->CI->db->delete($this->sess_table_name); + } + + // Kill the cookie + setcookie($this->sess_cookie_name, addslashes(serialize(array())), ($this->now - 31500000), + $this->cookie_path, $this->cookie_domain, 0); + } + + /** + * Regenerate the current session + * + * Regenerate the session id + * + * @param boolean Destroy session data flag (default: false) + * @return void + */ + public function sess_regenerate($destroy = false) + { + // Check destroy flag + if ($destroy) + { + // Destroy old session and create new one + $this->sess_destroy(); + $this->_sess_create(); + } + else + { + // Just force an update to recreate the id + $this->_sess_update(true); + } + } + + /** + * Get a reference to user data array + * + * @return array - Reference to userdata + */ + public function &get_userdata() + { + // Return reference to array + return $this->userdata; + } + + /** + * Fetch the current session data if it exists + * + * @access private + * @return bool + */ + private function _sess_read() + { + // Fetch the cookie + $session = $this->CI->input->cookie($this->sess_cookie_name); + + // No cookie? Goodbye cruel world!... + if ($session === FALSE) + { + log_message('debug', 'A session cookie was not found.'); + return FALSE; + } + + // Decrypt the cookie data + if ($this->sess_encrypt_cookie == TRUE) + { + $session = $this->CI->encrypt->decode($session); + } + else + { + // encryption was not used, so we need to check the md5 hash + $hash = substr($session, strlen($session)-32); // get last 32 chars + $session = substr($session, 0, strlen($session)-32); + + // Does the md5 hash match? This is to prevent manipulation of session data in userspace + if ($hash !== md5($session.$this->encryption_key)) + { + log_message('error', 'The session cookie data did not match what was expected. '. + 'This could be a possible hacking attempt.'); + $this->sess_destroy(); + return FALSE; + } + } + + // Unserialize the session array + $session = $this->_unserialize($session); + + // Is the session data we unserialized an array with the correct format? + if ( ! is_array($session) || ! isset($session['session_id']) || ! isset($session['ip_address']) || + ! isset($session['user_agent']) || ! isset($session['last_activity'])) + { + $this->sess_destroy(); + return FALSE; + } + + // Is the session current? + if (($session['last_activity'] + $this->sess_expiration) < $this->now()) + { + $this->sess_destroy(); + return FALSE; + } + + // Does the IP Match? + if ($this->sess_match_ip == TRUE && $session['ip_address'] != $this->CI->input->ip_address()) + { + $this->sess_destroy(); + return FALSE; + } + + // Does the User Agent Match? + if ($this->sess_match_useragent == TRUE && + trim($session['user_agent']) != trim(substr($this->CI->input->user_agent(), 0, 50))) + { + $this->sess_destroy(); + return FALSE; + } + + // Is there a corresponding session in the DB? + if ($this->sess_use_database === TRUE) + { + $this->CI->db->where('session_id', $session['session_id']); + + if ($this->sess_match_ip == TRUE) + { + $this->CI->db->where('ip_address', $session['ip_address']); + } + + if ($this->sess_match_useragent == TRUE) + { + $this->CI->db->where('user_agent', $session['user_agent']); + } + + $query = $this->CI->db->get($this->sess_table_name); + + // No result? Kill it! + if ($query->num_rows() == 0) + { + $this->sess_destroy(); + return FALSE; + } + + // Is there custom data? If so, add it to the main session array + $row = $query->row(); + if (isset($row->user_data) && $row->user_data != '') + { + $custom_data = $this->_unserialize($row->user_data); + + if (is_array($custom_data)) + { + foreach ($custom_data as $key => $val) + { + $session[$key] = $val; + } + } + } + } + + // Session is valid! + $this->userdata = $session; + unset($session); + + return TRUE; + } + + /** + * Create a new session + * + * @access private + * @return void + */ + private function _sess_create() + { + $sessid = ''; + while (strlen($sessid) < 32) + { + $sessid .= mt_rand(0, mt_getrandmax()); + } + + // To make the session ID even more secure we'll combine it with the user's IP + $sessid .= $this->CI->input->ip_address(); + + $this->set_userdata('session_id', md5(uniqid($sessid, TRUE))); + $this->set_userdata('ip_address', $this->CI->input->ip_address()); + $this->set_userdata('user_agent', substr($this->CI->input->user_agent(), 0, 50)); + $this->set_userdata('last_activity',$this->now()); + + + // Save the data to the DB if needed + if ($this->sess_use_database === TRUE) + { + $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->all_userdata())); + } + + // Write the cookie + $this->_set_cookie(); + } + + /** + * Update an existing session + * + * @access private + * @param boolean Force update flag (default: false) + * @return void + */ + private function _sess_update($force = false) + { + // We only update the session every five minutes by default (unless forced) + if (!$force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now()) + { + return; + } + + // Save the old session id so we know which record to + // update in the database if we need it + $old_sessid = $this->userdata['session_id']; + $new_sessid = ''; + while (strlen($new_sessid) < 32) + { + $new_sessid .= mt_rand(0, mt_getrandmax()); + } + + // To make the session ID even more secure we'll combine it with the user's IP + $new_sessid .= $this->CI->input->ip_address(); + + // Turn it into a hash + $new_sessid = md5(uniqid($new_sessid, TRUE)); + + // Update the session data in the session data array + $this->set_userdata('session_id', $new_sessid); + $this->set_userdata('last_activity', $this->now()); + + // _set_cookie() will handle this for us if we aren't using database sessions + // by pushing all userdata to the cookie. + $cookie_data = NULL; + + // Update the session ID and last_activity field in the DB if needed + if ($this->sess_use_database === TRUE) + { + // set cookie explicitly to only have our session data + $cookie_data = array(); + foreach (array('session_id','ip_address','user_agent','last_activity') as $val) + { + $cookie_data[$val] = $this->userdata[$val]; + } + + $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, + array('last_activity' => $this->now(), 'session_id' => $new_sessid), + array('session_id' => $old_sessid))); + } + + // Write the cookie + $this->_set_cookie($cookie_data); + } + + /** + * Get the "now" time + * + * @access private + * @return int + */ + private function _get_time() + { + if (strtolower($this->time_reference) == 'gmt') + { + $now = time(); + $time = mktime(gmdate('H', $now), gmdate('i', $now), gmdate('s', $now), gmdate('m', $now), + gmdate('d', $now), gmdate('Y', $now)); + } + else + { + $time = time(); + } + + return $time; + } + + /** + * Write the session cookie + * + * @access private + * @param array Cookie name/value pairs + * @return void + */ + private function _set_cookie(array $cookie_data = NULL) + { + if (is_null($cookie_data)) + { + $cookie_data = $this->all_userdata(); + } + + // Serialize the userdata for the cookie + $cookie_data = $this->_serialize($cookie_data); + + if ($this->sess_encrypt_cookie == TRUE) + { + $cookie_data = $this->CI->encrypt->encode($cookie_data); + } + else + { + // if encryption is not used, we provide an md5 hash to prevent userside tampering + $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key); + } + + $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time(); + + // Set the cookie + setcookie($this->sess_cookie_name, $cookie_data, $expire, $this->cookie_path, $this->cookie_domain, 0); + } + + /** + * Serialize an array + * + * This function first converts any slashes found in the array to a temporary + * marker, so when it gets unserialized the slashes will be preserved + * + * @access private + * @param mixed Data to serialize + * @return string + */ + private function _serialize($data) + { + if (is_array($data)) + { + foreach ($data as $key => $val) + { + if (is_string($val)) + { + $data[$key] = str_replace('\\', '{{slash}}', $val); + } + } + } + else + { + if (is_string($data)) + { + $data = str_replace('\\', '{{slash}}', $data); + } + } + + return serialize($data); + } + + /** + * Unserialize + * + * This function unserializes a data string, then converts any + * temporary slash markers back to actual slashes + * + * @access private + * @param string Data to unserialize + * @return mixed + */ + private function _unserialize($data) + { + $data = @unserialize(strip_slashes($data)); + + if (is_array($data)) + { + foreach ($data as $key => $val) + { + if (is_string($val)) + { + $data[$key] = str_replace('{{slash}}', '\\', $val); + } + } + + return $data; + } + + return (is_string($data)) ? str_replace('{{slash}}', '\\', $data) : $data; + } + + /** + * Garbage collection + * + * This deletes expired session rows from database + * if the probability percentage is met + * + * @access private + * @return void + */ + private function _sess_gc() + { + if ($this->sess_use_database != TRUE) + { + return; + } + + srand(time()); + if ((rand() % 100) < self::gc_probability) + { + $expire = $this->now() - $this->sess_expiration; + + $this->CI->db->where('last_activity < '.$expire); + $this->CI->db->delete($this->sess_table_name); + + log_message('debug', 'Session garbage collection performed.'); + } + } +} +// END Session_Cookie Class + +/* End of file Session_cookie.php */ +/* Location: ./system/libraries/Session/Session.php */ +?> -- cgit v1.2.3-24-g4f1b From 5073a375951f09b654f6b991df7ca04e1f88d93c Mon Sep 17 00:00:00 2001 From: Darren Hill Date: Wed, 31 Aug 2011 13:54:19 -0400 Subject: Better style guide compliance --- system/libraries/Session/drivers/Session_cookie.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 0982b1e01..d26ab0432 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -24,9 +24,9 @@ * @package CodeIgniter * @subpackage Libraries * @category Sessions - * @author ExpressionEngine Dev Team and Darren Hill (DChill42) + * @author ExpressionEngine Dev Team */ -class Session_Cookie extends SessionDriver { +class CI_Session_cookie extends CI_Session_driver { private $sess_encrypt_cookie = FALSE; private $sess_use_database = FALSE; private $sess_table_name = ''; @@ -576,8 +576,8 @@ class Session_Cookie extends SessionDriver { } } } -// END Session_Cookie Class +// END CI_Session_cookie Class /* End of file Session_cookie.php */ -/* Location: ./system/libraries/Session/Session.php */ +/* Location: ./system/libraries/Session/drivers/Session_cookie.php */ ?> -- cgit v1.2.3-24-g4f1b From a2ae6571e55d5a3d23645e96929eea996e9f0499 Mon Sep 17 00:00:00 2001 From: Darren Hill Date: Thu, 1 Sep 2011 07:36:26 -0400 Subject: Made private members protected for inheritance --- .../libraries/Session/drivers/Session_cookie.php | 66 +++++++++++----------- 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index d26ab0432..334218ec2 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -27,23 +27,23 @@ * @author ExpressionEngine Dev Team */ class CI_Session_cookie extends CI_Session_driver { - private $sess_encrypt_cookie = FALSE; - private $sess_use_database = FALSE; - private $sess_table_name = ''; - private $sess_expiration = 7200; - private $sess_expire_on_close = FALSE; - private $sess_match_ip = FALSE; - private $sess_match_useragent = TRUE; - private $sess_cookie_name = 'ci_session'; - private $cookie_prefix = ''; - private $cookie_path = ''; - private $cookie_domain = ''; - private $sess_time_to_update = 300; - private $encryption_key = ''; - private $time_reference = 'time'; - private $userdata = array(); - private $CI = null; - private $now = 0; + protected $sess_encrypt_cookie = FALSE; + protected $sess_use_database = FALSE; + protected $sess_table_name = ''; + protected $sess_expiration = 7200; + protected $sess_expire_on_close = FALSE; + protected $sess_match_ip = FALSE; + protected $sess_match_useragent = TRUE; + protected $sess_cookie_name = 'ci_session'; + protected $cookie_prefix = ''; + protected $cookie_path = ''; + protected $cookie_domain = ''; + protected $sess_time_to_update = 300; + protected $encryption_key = ''; + protected $time_reference = 'time'; + protected $userdata = array(); + protected $CI = null; + protected $now = 0; const gc_probability = 5; @@ -224,10 +224,10 @@ class CI_Session_cookie extends CI_Session_driver { /** * Fetch the current session data if it exists * - * @access private + * @access protected * @return bool */ - private function _sess_read() + protected function _sess_read() { // Fetch the cookie $session = $this->CI->input->cookie($this->sess_cookie_name); @@ -343,10 +343,10 @@ class CI_Session_cookie extends CI_Session_driver { /** * Create a new session * - * @access private + * @access protected * @return void */ - private function _sess_create() + protected function _sess_create() { $sessid = ''; while (strlen($sessid) < 32) @@ -376,11 +376,11 @@ class CI_Session_cookie extends CI_Session_driver { /** * Update an existing session * - * @access private + * @access protected * @param boolean Force update flag (default: false) * @return void */ - private function _sess_update($force = false) + protected function _sess_update($force = false) { // We only update the session every five minutes by default (unless forced) if (!$force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now()) @@ -433,10 +433,10 @@ class CI_Session_cookie extends CI_Session_driver { /** * Get the "now" time * - * @access private + * @access protected * @return int */ - private function _get_time() + protected function _get_time() { if (strtolower($this->time_reference) == 'gmt') { @@ -455,11 +455,11 @@ class CI_Session_cookie extends CI_Session_driver { /** * Write the session cookie * - * @access private + * @access protected * @param array Cookie name/value pairs * @return void */ - private function _set_cookie(array $cookie_data = NULL) + protected function _set_cookie(array $cookie_data = NULL) { if (is_null($cookie_data)) { @@ -491,11 +491,11 @@ class CI_Session_cookie extends CI_Session_driver { * This function first converts any slashes found in the array to a temporary * marker, so when it gets unserialized the slashes will be preserved * - * @access private + * @access protected * @param mixed Data to serialize * @return string */ - private function _serialize($data) + protected function _serialize($data) { if (is_array($data)) { @@ -524,11 +524,11 @@ class CI_Session_cookie extends CI_Session_driver { * This function unserializes a data string, then converts any * temporary slash markers back to actual slashes * - * @access private + * @access protected * @param string Data to unserialize * @return mixed */ - private function _unserialize($data) + protected function _unserialize($data) { $data = @unserialize(strip_slashes($data)); @@ -554,10 +554,10 @@ class CI_Session_cookie extends CI_Session_driver { * This deletes expired session rows from database * if the probability percentage is met * - * @access private + * @access protected * @return void */ - private function _sess_gc() + protected function _sess_gc() { if ($this->sess_use_database != TRUE) { -- cgit v1.2.3-24-g4f1b From 42b77a9a1a5d4ec7ceb94b421b12af9c442769ba Mon Sep 17 00:00:00 2001 From: dchill42 Date: Mon, 23 Jul 2012 11:28:42 -0400 Subject: Made cookie driver default and did miniscule code cleanup on drivers --- system/libraries/Session/drivers/Session_cookie.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 0dc109bd1..255a1ae3e 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -39,7 +39,6 @@ * @link http://codeigniter.com/user_guide/libraries/sessions.html */ class CI_Session_cookie extends CI_Session_driver { - /** * Whether to encrypt the session cookie * @@ -152,7 +151,6 @@ class CI_Session_cookie extends CI_Session_driver { */ public $time_reference = 'local'; - /** * Session data * @@ -186,7 +184,7 @@ class CI_Session_cookie extends CI_Session_driver { $this->CI =& get_instance(); // Set all the session preferences, which can either be set - // manually via the $params array above or via the config file + // manually via the $params array or via the config file foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'cookie_secure', 'cookie_httponly', 'sess_time_to_update', 'time_reference', 'cookie_prefix', @@ -527,9 +525,7 @@ class CI_Session_cookie extends CI_Session_driver { // by pushing all userdata to the cookie. $cookie_data = NULL; - /* Changing the session ID during an AJAX call causes problems, - * so we'll only update our last_activity - */ + // Changing the session ID during an AJAX call causes problems, so we'll only update our last_activity if ($this->CI->input->is_ajax_request()) { $this->userdata['last_activity'] = $this->now; @@ -752,7 +748,6 @@ class CI_Session_cookie extends CI_Session_driver { log_message('debug', 'Session garbage collection performed.'); } } - } /* End of file Session_cookie.php */ -- cgit v1.2.3-24-g4f1b From 77ee3fdac34d317b600a269e0b845588c88fa4c5 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Tue, 24 Jul 2012 11:50:01 -0400 Subject: Cleaned up bangs and lowercase booleans, and fixed userdata return on not found to NULL --- system/libraries/Session/drivers/Session_cookie.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 255a1ae3e..e39ada052 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -516,7 +516,7 @@ class CI_Session_cookie extends CI_Session_driver { protected function _sess_update($force = FALSE) { // We only update the session every five minutes by default (unless forced) - if (!$force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) + if ( ! $force && ($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) { return; } -- cgit v1.2.3-24-g4f1b From c58722535e0358367f351c168480ef98a033264c Mon Sep 17 00:00:00 2001 From: dchill42 Date: Mon, 30 Jul 2012 14:53:11 -0400 Subject: Fixed _parent references and several minor bugs --- .../libraries/Session/drivers/Session_cookie.php | 30 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index e39ada052..19ccd417d 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -190,13 +190,13 @@ class CI_Session_cookie extends CI_Session_driver { 'cookie_domain', 'cookie_secure', 'cookie_httponly', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key) { - $this->$key = isset($this->parent->params[$key]) ? $this->parent->params[$key] : + $this->$key = isset($this->_parent->params[$key]) ? $this->_parent->params[$key] : $this->CI->config->item($key); } if ($this->encryption_key === '') { - show_error('In order to use the Session Cookie driver you are required to set an encryption key '. + show_error('In order to use the Cookie Session driver you are required to set an encryption key '. 'in your config file.'); } @@ -309,7 +309,7 @@ class CI_Session_cookie extends CI_Session_driver { } // Kill the cookie - setcookie($this->sess_cookie_name, addslashes(serialize(array())), ($this->now - 31500000), + $this->_setcookie($this->sess_cookie_name, addslashes(serialize(array())), ($this->now - 31500000), $this->cookie_path, $this->cookie_domain, 0); // Kill session data @@ -632,10 +632,32 @@ class CI_Session_cookie extends CI_Session_driver { $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time(); // Set the cookie - setcookie($this->sess_cookie_name, $cookie_data, $expire, $this->cookie_path, $this->cookie_domain, + $this->_setcookie($this->sess_cookie_name, $cookie_data, $expire, $this->cookie_path, $this->cookie_domain, $this->cookie_secure, $this->cookie_httponly); } + /** + * Set a cookie with the system + * + * This abstraction of the setcookie call allows overriding for unit testing + * + * @access protected + * @param string Cookie name + * @param string Cookie value + * @param int Expiration time + * @param string Cookie path + * @param string Cookie domain + * @param bool Secure connection flag + * @param bool HTTP protocol only flag + * @return void + */ + protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = false, + $httponly = false) + { + // Set the cookie + setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); + } + /** * Serialize an array * -- cgit v1.2.3-24-g4f1b From 2642920e4781db091309ab97d0ff43c22e7c7e44 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Tue, 31 Jul 2012 10:55:07 -0400 Subject: Damn, missed files on last commit --- .../libraries/Session/drivers/Session_cookie.php | 61 +++++++++++++++++----- 1 file changed, 48 insertions(+), 13 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 19ccd417d..8ac92e432 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -72,7 +72,7 @@ class CI_Session_cookie extends CI_Session_driver { * * @var bool */ - public $sess_expire_on_close = FALSE; + public $sess_expire_on_close = FALSE; /** * Whether to match session on ip address @@ -86,7 +86,7 @@ class CI_Session_cookie extends CI_Session_driver { * * @var bool */ - public $sess_match_useragent = TRUE; + public $sess_match_useragent = TRUE; /** * Name of session cookie @@ -107,7 +107,7 @@ class CI_Session_cookie extends CI_Session_driver { * * @var string */ - public $cookie_path = ''; + public $cookie_path = ''; /** * Session cookie domain @@ -156,7 +156,7 @@ class CI_Session_cookie extends CI_Session_driver { * * @var array */ - public $userdata = array(); + public $userdata = array(); /** * Reference to CodeIgniter instance @@ -185,10 +185,25 @@ class CI_Session_cookie extends CI_Session_driver { // Set all the session preferences, which can either be set // manually via the $params array or via the config file - foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', - 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', - 'cookie_domain', 'cookie_secure', 'cookie_httponly', 'sess_time_to_update', 'time_reference', 'cookie_prefix', - 'encryption_key') as $key) + $prefs = array( + 'sess_encrypt_cookie', + 'sess_use_database', + 'sess_table_name', + 'sess_expiration', + 'sess_expire_on_close', + 'sess_match_ip', + 'sess_match_useragent', + 'sess_cookie_name', + 'cookie_path', + 'cookie_domain', + 'cookie_secure', + 'cookie_httponly', + 'sess_time_to_update', + 'time_reference', + 'cookie_prefix', + 'encryption_key' + ); + foreach ($prefs as $key) { $this->$key = isset($this->_parent->params[$key]) ? $this->_parent->params[$key] : $this->CI->config->item($key); @@ -265,7 +280,13 @@ class CI_Session_cookie extends CI_Session_driver { // Before continuing, we need to determine if there is any custom data to deal with. // Let's determine this by removing the default indexes to see if there's anything left in the array // and set the session data while we're at it - foreach (array('session_id','ip_address','user_agent','last_activity') as $val) + $defaults = array( + 'session_id', + 'ip_address', + 'user_agent', + 'last_activity' + ); + foreach ($defaults as $val) { unset($custom_userdata[$val]); $cookie_userdata[$val] = $this->userdata[$val]; @@ -285,8 +306,10 @@ class CI_Session_cookie extends CI_Session_driver { // Run the update query $this->CI->db->where('session_id', $this->userdata['session_id']); - $this->CI->db->update($this->sess_table_name, - array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata)); + $this->CI->db->update($this->sess_table_name, array( + 'last_activity' => $this->userdata['last_activity'], + 'user_data' => $custom_userdata + )); // Write the cookie. Notice that we manually pass the cookie data array to the // _set_cookie() function. Normally that function will store $this->userdata, but @@ -535,7 +558,13 @@ class CI_Session_cookie extends CI_Session_driver { { // set cookie explicitly to only have our session data $cookie_data = array(); - foreach (array('session_id','ip_address','user_agent','last_activity') as $val) + $defaults = array( + 'session_id', + 'ip_address', + 'user_agent', + 'last_activity' + ); + foreach ($defaults as $val) { $cookie_data[$val] = $this->userdata[$val]; } @@ -570,7 +599,13 @@ class CI_Session_cookie extends CI_Session_driver { { // set cookie explicitly to only have our session data $cookie_data = array(); - foreach (array('session_id','ip_address','user_agent','last_activity') as $val) + $defaults = array( + 'session_id', + 'ip_address', + 'user_agent', + 'last_activity' + ); + foreach ($defaults as $val) { $cookie_data[$val] = $this->userdata[$val]; } -- cgit v1.2.3-24-g4f1b From aee9265602c3bb30a1f7f3dfd562b9b36cc612a4 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Sun, 26 Aug 2012 21:45:35 -0400 Subject: Fixed select_driver(), cookie sess_destroy(), and native cookie name conflict --- system/libraries/Session/drivers/Session_cookie.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 8ac92e432..89e81386f 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -325,7 +325,7 @@ class CI_Session_cookie extends CI_Session_driver { public function sess_destroy() { // Kill the session DB row - if ($this->sess_use_database === TRUE && $this->has_userdata('session_id')) + if ($this->sess_use_database === TRUE && isset($this->userdata['session_id'])) { $this->CI->db->where('session_id', $this->userdata['session_id']); $this->CI->db->delete($this->sess_table_name); -- cgit v1.2.3-24-g4f1b From 3cecd8234d3bb9045e9cc41e15f603a6e87c5fac Mon Sep 17 00:00:00 2001 From: dchill42 Date: Tue, 28 Aug 2012 21:37:27 -0400 Subject: Extracted cookie database saves to shutdown and cleaned up code Signed-off-by: dchill42 --- .../libraries/Session/drivers/Session_cookie.php | 269 ++++++++++----------- 1 file changed, 134 insertions(+), 135 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 89e81386f..df3282cee 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -28,9 +28,7 @@ /** * Cookie-based session management driver * - * This is the CI_Session functionality, as written by EllisLab, abstracted out to a driver. - * I have done a little updating for PHP5, and made minor changes to extract this functionality from - * the public interface (now in the Session Library), but effectively this code is unchanged. + * This is the classic CI_Session functionality, as written by EllisLab, abstracted out to a driver. * * @package CodeIgniter * @subpackage Libraries @@ -172,6 +170,25 @@ class CI_Session_cookie extends CI_Session_driver { */ public $now; + /** + * Default userdata keys + * + * @var array + */ + protected $defaults = array( + 'session_id', + 'ip_address', + 'user_agent', + 'last_activity' + ); + + /** + * Data needs DB update flag + * + * @var bool + */ + protected $data_dirty = FALSE; + /** * Initialize session driver object * @@ -224,10 +241,14 @@ class CI_Session_cookie extends CI_Session_driver { $this->CI->load->library('encrypt'); } - // Are we using a database? If so, load it + // Check for database if ($this->sess_use_database === TRUE && $this->sess_table_name !== '') { + // Load database driver $this->CI->load->database(); + + // Register shutdown function + register_shutdown_function(array($this, '_update_db')); } // Set the "now" time. Can either be GMT or server time, based on the config prefs. @@ -259,6 +280,17 @@ class CI_Session_cookie extends CI_Session_driver { $this->_sess_gc(); } + /** + * Shutdown session driver object + * + * @return void + */ + public function shutdown() + { + // Just update the DB + $this->_update_db(); + } + /** * Write the session data * @@ -266,55 +298,15 @@ class CI_Session_cookie extends CI_Session_driver { */ public function sess_save() { - // Are we saving custom data to the DB? If not, all we do is update the cookie + // Check for database if ($this->sess_use_database === FALSE) { - $this->_set_cookie(); - return; - } - - // set the custom userdata, the session data we will set in a second - $custom_userdata = $this->all_userdata(); - $cookie_userdata = array(); - - // Before continuing, we need to determine if there is any custom data to deal with. - // Let's determine this by removing the default indexes to see if there's anything left in the array - // and set the session data while we're at it - $defaults = array( - 'session_id', - 'ip_address', - 'user_agent', - 'last_activity' - ); - foreach ($defaults as $val) - { - unset($custom_userdata[$val]); - $cookie_userdata[$val] = $this->userdata[$val]; + // Mark custom data as dirty so we know to update the DB + $this->data_dirty = TRUE; } - // Did we find any custom data? If not, we turn the empty array into a string - // since there's no reason to serialize and store an empty array in the DB - if (count($custom_userdata) === 0) - { - $custom_userdata = ''; - } - else - { - // Serialize the custom data array so we can store it - $custom_userdata = $this->_serialize($custom_userdata); - } - - // Run the update query - $this->CI->db->where('session_id', $this->userdata['session_id']); - $this->CI->db->update($this->sess_table_name, array( - 'last_activity' => $this->userdata['last_activity'], - 'user_data' => $custom_userdata - )); - - // Write the cookie. Notice that we manually pass the cookie data array to the - // _set_cookie() function. Normally that function will store $this->userdata, but - // in this case that array contains custom data, which we do not want in the cookie. - $this->_set_cookie($cookie_userdata); + // Write the cookie + $this->_set_cookie(); } /** @@ -327,8 +319,7 @@ class CI_Session_cookie extends CI_Session_driver { // Kill the session DB row if ($this->sess_use_database === TRUE && isset($this->userdata['session_id'])) { - $this->CI->db->where('session_id', $this->userdata['session_id']); - $this->CI->db->delete($this->sess_table_name); + $this->CI->db->delete($this->sess_table_name, array('session_id' => $this->userdata['session_id'])); } // Kill the cookie @@ -392,16 +383,18 @@ class CI_Session_cookie extends CI_Session_driver { return FALSE; } - // Decrypt the cookie data + // Check for encryption if ($this->sess_encrypt_cookie === TRUE) { + // Decrypt the cookie data $session = $this->CI->encrypt->decode($session); } else { - // encryption was not used, so we need to check the md5 hash - $hash = substr($session, strlen($session)-32); // get last 32 chars - $session = substr($session, 0, strlen($session)-32); + // Encryption was not used, so we need to check the md5 hash in the last 32 chars + $len = strlen($session)-32; + $hash = substr($session, $len); + $session = substr($session, 0, $len); // Does the md5 hash match? This is to prevent manipulation of session data in userspace if ($hash !== md5($session.$this->encryption_key)) @@ -478,18 +471,13 @@ class CI_Session_cookie extends CI_Session_driver { if (is_array($custom_data)) { - foreach ($custom_data as $key => $val) - { - $session[$key] = $val; - } + $session = $session + $custom_data; } } } // Session is valid! $this->userdata = $session; - unset($session); - return TRUE; } @@ -501,28 +489,19 @@ class CI_Session_cookie extends CI_Session_driver { */ protected function _sess_create() { - $sessid = ''; - do - { - $sessid .= mt_rand(0, mt_getrandmax()); - } - while (strlen($sessid) < 32); - - // To make the session ID even more secure we'll combine it with the user's IP - $sessid .= $this->CI->input->ip_address(); - + // Initialize userdata $this->userdata = array( - 'session_id' => md5(uniqid($sessid, TRUE)), + 'session_id' => $this->_make_sess_id(), 'ip_address' => $this->CI->input->ip_address(), 'user_agent' => substr($this->CI->input->user_agent(), 0, 120), 'last_activity' => $this->now, - 'user_data' => '' ); - // Save the data to the DB if needed + // Check for database if ($this->sess_use_database === TRUE) { - $this->CI->db->query($this->CI->db->insert_string($this->sess_table_name, $this->userdata)); + // Add empty user_data field and save the data to the DB + $this->CI->db->set('user_data', '')->insert($this->sess_table_name, $this->userdata); } // Write the cookie @@ -544,42 +523,84 @@ class CI_Session_cookie extends CI_Session_driver { return; } - // _set_cookie() will handle this for us if we aren't using database sessions - // by pushing all userdata to the cookie. - $cookie_data = NULL; + // Update last activity to now + $this->userdata['last_activity'] = $this->now; + + // Save the old session id so we know which DB record to update + $old_sessid = $this->userdata['session_id']; + + // Changing the session ID during an AJAX call causes problems + if ( ! $this->CI->input->is_ajax_request()) + { + // Get new id + $this->userdata['session_id'] = $this->_make_sess_id(); + } - // Changing the session ID during an AJAX call causes problems, so we'll only update our last_activity - if ($this->CI->input->is_ajax_request()) + // Check for database + if ($this->sess_use_database === TRUE) { - $this->userdata['last_activity'] = $this->now; + // Update the session ID and last_activity field in the DB + $this->CI->db->update($this->sess_table_name, array( + 'last_activity' => $this->now, + 'session_id' => $this->userdata['session_id'] + ), array('session_id' => $old_sessid)); + } - // Update the session ID and last_activity field in the DB if needed - if ($this->sess_use_database === TRUE) - { - // set cookie explicitly to only have our session data - $cookie_data = array(); - $defaults = array( - 'session_id', - 'ip_address', - 'user_agent', - 'last_activity' - ); - foreach ($defaults as $val) - { - $cookie_data[$val] = $this->userdata[$val]; - } + // Write the cookie + $this->_set_cookie(); + } - $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, - array('last_activity' => $this->userdata['last_activity']), - array('session_id' => $this->userdata['session_id']))); + /** + * Update database with current data + * + * This gets called from the shutdown function and also + * registered with PHP to run at the end of the request + * so it's guaranteed to update even when a fatal error + * occurs. The first call makes the update and clears the + * dirty flag so it won't happen twice. + */ + public function _update_db() + { + // Check for database and dirty flag and unsaved + if ($this->sess_use_database === TRUE && $this->data_dirty === TRUE) + { + // Set up activity and data fields to be set + // If we don't find custom data, user_data will remain an empty string + $set = array( + 'last_activity' => $this->userdata['last_activity'], + 'user_data' => '' + ); + + // Get the custom userdata, leaving out the defaults + // (which get stored in the cookie) + $userdata = array_diff_key($this->userdata, $this->defaults); + + // Did we find any custom data? + if ( ! empty($userdata)) + { + // Serialize the custom data array so we can store it + $set['user_data'] = $this->_serialize($userdata); } - return $this->_set_cookie($cookie_data); + // Run the update query + // Any time we change the session id, it gets updated immediately, + // so our where clause below is always safe + $this->CI->db->update($this->sess_table_name, $set, array('session_id' => $this->userdata['session_id'])); + + // Clear dirty flag to prevent double updates + $this->data_dirty = FALSE; + + log_message('debug', 'CI_Session Data Saved To DB'); } + } - // Save the old session id so we know which record to - // update in the database if we need it - $old_sessid = $this->userdata['session_id']; + /** + * Generate a new session id + * + * @return string Hashed session id + */ + protected function _make_sess_id() + { $new_sessid = ''; do { @@ -590,32 +611,8 @@ class CI_Session_cookie extends CI_Session_driver { // To make the session ID even more secure we'll combine it with the user's IP $new_sessid .= $this->CI->input->ip_address(); - // Turn it into a hash and update the session data array - $this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE)); - $this->userdata['last_activity'] = $this->now; - - // Update the session ID and last_activity field in the DB if needed - if ($this->sess_use_database === TRUE) - { - // set cookie explicitly to only have our session data - $cookie_data = array(); - $defaults = array( - 'session_id', - 'ip_address', - 'user_agent', - 'last_activity' - ); - foreach ($defaults as $val) - { - $cookie_data[$val] = $this->userdata[$val]; - } - - $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, - array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid))); - } - - // Write the cookie - $this->_set_cookie($cookie_data); + // Turn it into a hash and return + return md5(uniqid($new_sessid, TRUE)); } /** @@ -641,12 +638,16 @@ class CI_Session_cookie extends CI_Session_driver { * Write the session cookie * * @access protected - * @param array Cookie name/value pairs * @return void */ - protected function _set_cookie(array $cookie_data = NULL) + protected function _set_cookie() { - if (is_null($cookie_data)) + // Get userdata (only defaults if database) + if ($this->sess_use_database === TRUE) + { + $cookie_data = array_intersect_key($this->userdata, $this->defaults); + } + else { $cookie_data = $this->userdata; } @@ -798,9 +799,7 @@ class CI_Session_cookie extends CI_Session_driver { if ((mt_rand(0, $divisor) / $divisor) < $probability) { $expire = $this->now - $this->sess_expiration; - - $this->CI->db->where('last_activity < '.$expire); - $this->CI->db->delete($this->sess_table_name); + $this->CI->db->delete($this->sess_table_name, 'last_activity < '.$expire); log_message('debug', 'Session garbage collection performed.'); } -- cgit v1.2.3-24-g4f1b From 88b636b06dd91807706e2d442d910fe8b6a3b50c Mon Sep 17 00:00:00 2001 From: dchill42 Date: Wed, 29 Aug 2012 08:47:05 -0400 Subject: Fixed defaults and database check, reverted redundant shutdown feature Signed-off-by: dchill42 --- system/libraries/Session/drivers/Session_cookie.php | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index df3282cee..69e5fde14 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -176,10 +176,10 @@ class CI_Session_cookie extends CI_Session_driver { * @var array */ protected $defaults = array( - 'session_id', - 'ip_address', - 'user_agent', - 'last_activity' + 'session_id' => NULL, + 'ip_address' => NULL, + 'user_agent' => NULL, + 'last_activity' => NULL ); /** @@ -280,17 +280,6 @@ class CI_Session_cookie extends CI_Session_driver { $this->_sess_gc(); } - /** - * Shutdown session driver object - * - * @return void - */ - public function shutdown() - { - // Just update the DB - $this->_update_db(); - } - /** * Write the session data * @@ -299,7 +288,7 @@ class CI_Session_cookie extends CI_Session_driver { public function sess_save() { // Check for database - if ($this->sess_use_database === FALSE) + if ($this->sess_use_database === TRUE) { // Mark custom data as dirty so we know to update the DB $this->data_dirty = TRUE; -- cgit v1.2.3-24-g4f1b From 97b0d8331eecd7f3efe3a1c9a93de55a2f26e877 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Tue, 4 Sep 2012 10:09:00 -0400 Subject: Fixed issues #1756 and #1711 Signed-off-by: dchill42 --- system/libraries/Session/drivers/Session_cookie.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 69e5fde14..ce63b976f 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -309,6 +309,7 @@ class CI_Session_cookie extends CI_Session_driver { if ($this->sess_use_database === TRUE && isset($this->userdata['session_id'])) { $this->CI->db->delete($this->sess_table_name, array('session_id' => $this->userdata['session_id'])); + $this->data_dirty = FALSE; } // Kill the cookie @@ -571,11 +572,22 @@ class CI_Session_cookie extends CI_Session_driver { $set['user_data'] = $this->_serialize($userdata); } + // Is caching in effect? Turn it off + $db_cache = $this->CI->db->cache_on; + $this->CI->db->cache_off(); + // Run the update query // Any time we change the session id, it gets updated immediately, // so our where clause below is always safe $this->CI->db->update($this->sess_table_name, $set, array('session_id' => $this->userdata['session_id'])); + // Was caching in effect? + if ($db_cache) + { + // Turn it back on + $this->CI->db->cache_on(); + } + // Clear dirty flag to prevent double updates $this->data_dirty = FALSE; -- cgit v1.2.3-24-g4f1b From cd436e92ec5f9a5d0361fb186bccacb908dbea22 Mon Sep 17 00:00:00 2001 From: dchill42 Date: Tue, 4 Sep 2012 10:15:14 -0400 Subject: That doesn't go there. Put cache fix around correct query. Signed-off-by: dchill42 --- .../libraries/Session/drivers/Session_cookie.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index ce63b976f..52eeddbc4 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -444,8 +444,19 @@ class CI_Session_cookie extends CI_Session_driver { $this->CI->db->where('user_agent', $session['user_agent']); } + // Is caching in effect? Turn it off + $db_cache = $this->CI->db->cache_on; + $this->CI->db->cache_off(); + $query = $this->CI->db->limit(1)->get($this->sess_table_name); + // Was caching in effect? + if ($db_cache) + { + // Turn it back on + $this->CI->db->cache_on(); + } + // No result? Kill it! if ($query->num_rows() === 0) { @@ -572,22 +583,11 @@ class CI_Session_cookie extends CI_Session_driver { $set['user_data'] = $this->_serialize($userdata); } - // Is caching in effect? Turn it off - $db_cache = $this->CI->db->cache_on; - $this->CI->db->cache_off(); - // Run the update query // Any time we change the session id, it gets updated immediately, // so our where clause below is always safe $this->CI->db->update($this->sess_table_name, $set, array('session_id' => $this->userdata['session_id'])); - // Was caching in effect? - if ($db_cache) - { - // Turn it back on - $this->CI->db->cache_on(); - } - // Clear dirty flag to prevent double updates $this->data_dirty = FALSE; -- cgit v1.2.3-24-g4f1b From 9ffcee60140b20ca3ec4e7688f83a039c7c080f7 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 5 Sep 2012 16:25:16 +0300 Subject: Cleanup and optimize new Session classes --- .../libraries/Session/drivers/Session_cookie.php | 125 ++++++++++++--------- 1 file changed, 69 insertions(+), 56 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 52eeddbc4..6d931c16c 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -37,6 +37,7 @@ * @link http://codeigniter.com/user_guide/libraries/sessions.html */ class CI_Session_cookie extends CI_Session_driver { + /** * Whether to encrypt the session cookie * @@ -192,7 +193,6 @@ class CI_Session_cookie extends CI_Session_driver { /** * Initialize session driver object * - * @access protected * @return void */ protected function initialize() @@ -220,16 +220,17 @@ class CI_Session_cookie extends CI_Session_driver { 'cookie_prefix', 'encryption_key' ); + foreach ($prefs as $key) { - $this->$key = isset($this->_parent->params[$key]) ? $this->_parent->params[$key] : - $this->CI->config->item($key); + $this->$key = isset($this->_parent->params[$key]) + ? $this->_parent->params[$key] + : $this->CI->config->item($key); } if ($this->encryption_key === '') { - show_error('In order to use the Cookie Session driver you are required to set an encryption key '. - 'in your config file.'); + show_error('In order to use the Cookie Session driver you are required to set an encryption key in your config file.'); } // Load the string helper so we can use the strip_slashes() function @@ -280,6 +281,8 @@ class CI_Session_cookie extends CI_Session_driver { $this->_sess_gc(); } + // ------------------------------------------------------------------------ + /** * Write the session data * @@ -298,6 +301,8 @@ class CI_Session_cookie extends CI_Session_driver { $this->_set_cookie(); } + // ------------------------------------------------------------------------ + /** * Destroy the current session * @@ -320,15 +325,17 @@ class CI_Session_cookie extends CI_Session_driver { $this->userdata = array(); } + // ------------------------------------------------------------------------ + /** * Regenerate the current session * * Regenerate the session id * - * @param boolean Destroy session data flag (default: false) + * @param bool Destroy session data flag (default: false) * @return void */ - public function sess_regenerate($destroy = false) + public function sess_regenerate($destroy = FALSE) { // Check destroy flag if ($destroy) @@ -344,21 +351,23 @@ class CI_Session_cookie extends CI_Session_driver { } } + // ------------------------------------------------------------------------ + /** * Get a reference to user data array * - * @return array - Reference to userdata + * @return array Reference to userdata */ public function &get_userdata() { - // Return reference to array return $this->userdata; } + // ------------------------------------------------------------------------ + /** * Fetch the current session data if it exists * - * @access protected * @return bool */ protected function _sess_read() @@ -389,8 +398,7 @@ class CI_Session_cookie extends CI_Session_driver { // Does the md5 hash match? This is to prevent manipulation of session data in userspace if ($hash !== md5($session.$this->encryption_key)) { - log_message('error', 'The session cookie data did not match what was expected. '. - 'This could be a possible hacking attempt.'); + log_message('error', 'The session cookie data did not match what was expected. This could be a possible hacking attempt.'); $this->sess_destroy(); return FALSE; } @@ -400,8 +408,7 @@ class CI_Session_cookie extends CI_Session_driver { $session = $this->_unserialize($session); // Is the session data we unserialized an array with the correct format? - if ( ! is_array($session) || ! isset($session['session_id'], $session['ip_address'], $session['user_agent'], - $session['last_activity'])) + if ( ! is_array($session) OR ! isset($session['session_id'], $session['ip_address'], $session['user_agent'], $session['last_activity'])) { $this->sess_destroy(); return FALSE; @@ -423,7 +430,7 @@ class CI_Session_cookie extends CI_Session_driver { // Does the User Agent Match? if ($this->sess_match_useragent === TRUE && - trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120))) + trim($session['user_agent']) !== trim(substr($this->CI->input->user_agent(), 0, 120))) { $this->sess_destroy(); return FALSE; @@ -482,10 +489,11 @@ class CI_Session_cookie extends CI_Session_driver { return TRUE; } + // ------------------------------------------------------------------------ + /** * Create a new session * - * @access protected * @return void */ protected function _sess_create() @@ -509,11 +517,12 @@ class CI_Session_cookie extends CI_Session_driver { $this->_set_cookie(); } + // ------------------------------------------------------------------------ + /** * Update an existing session * - * @access protected - * @param boolean Force update flag (default: false) + * @param bool Force update flag (default: false) * @return void */ protected function _sess_update($force = FALSE) @@ -551,6 +560,8 @@ class CI_Session_cookie extends CI_Session_driver { $this->_set_cookie(); } + // ------------------------------------------------------------------------ + /** * Update database with current data * @@ -559,6 +570,8 @@ class CI_Session_cookie extends CI_Session_driver { * so it's guaranteed to update even when a fatal error * occurs. The first call makes the update and clears the * dirty flag so it won't happen twice. + * + * @return void */ public function _update_db() { @@ -595,6 +608,8 @@ class CI_Session_cookie extends CI_Session_driver { } } + // ------------------------------------------------------------------------ + /** * Generate a new session id * @@ -616,15 +631,16 @@ class CI_Session_cookie extends CI_Session_driver { return md5(uniqid($new_sessid, TRUE)); } + // ------------------------------------------------------------------------ + /** * Get the "now" time * - * @access protected * @return int Time */ protected function _get_time() { - if ($this->time_reference === 'local' || $this->time_reference === date_default_timezone_get()) + if ($this->time_reference === 'local' OR $this->time_reference === date_default_timezone_get()) { return time(); } @@ -635,36 +651,27 @@ class CI_Session_cookie extends CI_Session_driver { return mktime($hour, $minute, $second, $month, $day, $year); } + // ------------------------------------------------------------------------ + /** * Write the session cookie * - * @access protected * @return void */ protected function _set_cookie() { // Get userdata (only defaults if database) - if ($this->sess_use_database === TRUE) - { - $cookie_data = array_intersect_key($this->userdata, $this->defaults); - } - else - { - $cookie_data = $this->userdata; - } + $cookie_data = ($this->sess_use_database === TRUE) + ? array_intersect_key($this->userdata, $this->defaults) + : $this->userdata; // Serialize the userdata for the cookie $cookie_data = $this->_serialize($cookie_data); - if ($this->sess_encrypt_cookie === TRUE) - { - $cookie_data = $this->CI->encrypt->encode($cookie_data); - } - else - { + $cookie_data = ($this->sess_encrypt_cookie === TRUE) + ? $this->CI->encrypt->encode($cookie_data) // if encryption is not used, we provide an md5 hash to prevent userside tampering - $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key); - } + : $cookie_data.md5($cookie_data.$this->encryption_key); $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time(); @@ -673,35 +680,35 @@ class CI_Session_cookie extends CI_Session_driver { $this->cookie_secure, $this->cookie_httponly); } + // ------------------------------------------------------------------------ + /** * Set a cookie with the system * * This abstraction of the setcookie call allows overriding for unit testing * - * @access protected - * @param string Cookie name - * @param string Cookie value - * @param int Expiration time - * @param string Cookie path - * @param string Cookie domain - * @param bool Secure connection flag - * @param bool HTTP protocol only flag - * @return void - */ - protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = false, - $httponly = false) + * @param string Cookie name + * @param string Cookie value + * @param int Expiration time + * @param string Cookie path + * @param string Cookie domain + * @param bool Secure connection flag + * @param bool HTTP protocol only flag + * @return void + */ + protected function _setcookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = FALSE, $httponly = FALSE) { - // Set the cookie setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); } + // ------------------------------------------------------------------------ + /** * Serialize an array * * This function first converts any slashes found in the array to a temporary * marker, so when it gets unserialized the slashes will be preserved * - * @access protected * @param mixed Data to serialize * @return string Serialized data */ @@ -715,15 +722,17 @@ class CI_Session_cookie extends CI_Session_driver { { $data = str_replace('\\', '{{slash}}', $data); } + return serialize($data); } + // ------------------------------------------------------------------------ + /** * Escape slashes * * This function converts any slashes found into a temporary marker * - * @access protected * @param string Value * @param string Key * @return void @@ -736,13 +745,14 @@ class CI_Session_cookie extends CI_Session_driver { } } + // ------------------------------------------------------------------------ + /** * Unserialize * * This function unserializes a data string, then converts any * temporary slash markers back to actual slashes * - * @access protected * @param mixed Data to unserialize * @return mixed Unserialized data */ @@ -759,12 +769,13 @@ class CI_Session_cookie extends CI_Session_driver { return is_string($data) ? str_replace('{{slash}}', '\\', $data) : $data; } + // ------------------------------------------------------------------------ + /** * Unescape slashes * * This function converts any slash markers back into actual slashes * - * @access protected * @param string Value * @param string Key * @return void @@ -777,13 +788,14 @@ class CI_Session_cookie extends CI_Session_driver { } } + // ------------------------------------------------------------------------ + /** * Garbage collection * * This deletes expired session rows from database * if the probability percentage is met * - * @access protected * @return void */ protected function _sess_gc() @@ -805,7 +817,8 @@ class CI_Session_cookie extends CI_Session_driver { log_message('debug', 'Session garbage collection performed.'); } } + } /* End of file Session_cookie.php */ -/* Location: ./system/libraries/Session/drivers/Session_cookie.php */ +/* Location: ./system/libraries/Session/drivers/Session_cookie.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 3f3f135ed5b47fd87a59d31fb3d1a4c773dcc3b3 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 5 Sep 2012 16:39:28 +0300 Subject: Misc. style changes --- system/libraries/Session/drivers/Session_cookie.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 6d931c16c..4f415cc0d 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -347,7 +347,7 @@ class CI_Session_cookie extends CI_Session_driver { else { // Just force an update to recreate the id - $this->_sess_update(true); + $this->_sess_update(TRUE); } } -- cgit v1.2.3-24-g4f1b From 2e3e23053d9748c68fa2c0e11f43af67da8743e8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 9 Oct 2012 15:52:34 +0300 Subject: Disable Session library under CLI and create a CI singleton to be used by its drivers --- system/libraries/Session/drivers/Session_cookie.php | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'system/libraries/Session/drivers/Session_cookie.php') diff --git a/system/libraries/Session/drivers/Session_cookie.php b/system/libraries/Session/drivers/Session_cookie.php index 4f415cc0d..fb62c7ec4 100755 --- a/system/libraries/Session/drivers/Session_cookie.php +++ b/system/libraries/Session/drivers/Session_cookie.php @@ -157,13 +157,6 @@ class CI_Session_cookie extends CI_Session_driver { */ public $userdata = array(); - /** - * Reference to CodeIgniter instance - * - * @var object - */ - public $CI; - /** * Current time * @@ -197,9 +190,6 @@ class CI_Session_cookie extends CI_Session_driver { */ protected function initialize() { - // Set the super object to a local variable for use throughout the class - $this->CI =& get_instance(); - // Set all the session preferences, which can either be set // manually via the $params array or via the config file $prefs = array( -- cgit v1.2.3-24-g4f1b