From d5d7d18f5ad649cc97e01cf851bb89bc25ab0819 Mon Sep 17 00:00:00 2001 From: Ere Maijala <ere.maijala@helsinki.fi> Date: Wed, 15 Jun 2016 17:02:49 +0300 Subject: [PATCH] Made disabling of session writes really disable the writes (#728) - Now no attempt is made to store the session when disabled. --- .../src/VuFind/Session/AbstractBase.php | 44 ++++++++++++++++ module/VuFind/src/VuFind/Session/Database.php | 28 +++++----- module/VuFind/src/VuFind/Session/File.php | 52 +++++++++---------- .../src/VuFind/Session/ManagerFactory.php | 4 +- module/VuFind/src/VuFind/Session/Memcache.php | 30 +++++------ 5 files changed, 101 insertions(+), 57 deletions(-) diff --git a/module/VuFind/src/VuFind/Session/AbstractBase.php b/module/VuFind/src/VuFind/Session/AbstractBase.php index 98f3a1dbb7b..9febadbb966 100644 --- a/module/VuFind/src/VuFind/Session/AbstractBase.php +++ b/module/VuFind/src/VuFind/Session/AbstractBase.php @@ -58,6 +58,24 @@ abstract class AbstractBase implements SaveHandlerInterface, */ protected $config = null; + /** + * Whether writes are disabled, i.e. any changes to the session are not written + * to the storage + * + * @var bool + */ + protected $writesDisabled = false; + + /** + * Disable session writing, i.e. make it read-only + * + * @return void + */ + public function disableWrites() + { + $this->writesDisabled = true; + } + /** * Set configuration. * @@ -145,4 +163,30 @@ abstract class AbstractBase implements SaveHandlerInterface, // Something to keep in mind though. return true; } + + /** + * Write function that is called when session data is to be saved. + * + * @param string $sess_id The current session ID + * @param string $data The session data to write + * + * @return bool + */ + public function write($sess_id, $data) + { + if ($this->writesDisabled) { + return true; + } + return $this->saveSession($sess_id, $data); + } + + /** + * A function that is called internally when session data is to be saved. + * + * @param string $sess_id The current session ID + * @param string $data The session data to write + * + * @return bool + */ + abstract protected function saveSession($sess_id, $data); } diff --git a/module/VuFind/src/VuFind/Session/Database.php b/module/VuFind/src/VuFind/Session/Database.php index 23a57bb56cf..089f30fe532 100644 --- a/module/VuFind/src/VuFind/Session/Database.php +++ b/module/VuFind/src/VuFind/Session/Database.php @@ -59,20 +59,6 @@ class Database extends AbstractBase } } - /** - * Write function that is called when session data is to be saved. - * - * @param string $sess_id The current session ID - * @param string $data The session data to write - * - * @return bool - */ - public function write($sess_id, $data) - { - $this->getTable('Session')->writeSession($sess_id, $data); - return true; - } - /** * The destroy handler, this is executed when a session is destroyed with * session_destroy() and takes the session id as its only parameter. @@ -105,4 +91,18 @@ class Database extends AbstractBase $this->getTable('Session')->garbageCollect($sess_maxlifetime); return true; } + + /** + * A function that is called internally when session data is to be saved. + * + * @param string $sess_id The current session ID + * @param string $data The session data to write + * + * @return bool + */ + protected function saveSession($sess_id, $data) + { + $this->getTable('Session')->writeSession($sess_id, $data); + return true; + } } diff --git a/module/VuFind/src/VuFind/Session/File.php b/module/VuFind/src/VuFind/Session/File.php index fbfa84da7ba..e05ba4531f0 100644 --- a/module/VuFind/src/VuFind/Session/File.php +++ b/module/VuFind/src/VuFind/Session/File.php @@ -100,32 +100,6 @@ class File extends AbstractBase return (string)file_get_contents($sess_file); } - /** - * Write function that is called when session data is to be saved. - * - * @param string $sess_id The current session ID - * @param string $data The session data to write - * - * @return bool - */ - public function write($sess_id, $data) - { - $sess_file = $this->getPath() . '/sess_' . $sess_id; - if ($fp = fopen($sess_file, "w")) { - $return = fwrite($fp, $data); - fclose($fp); - if ($return !== false) { - return true; - } - } - // If we got this far, something went wrong with the file output... - // It is tempting to throw an exception here, but this code is called - // outside of the context of exception handling, so all we can do is - // echo a message. - echo 'Cannot write session to ' . $sess_file . "\n"; - return false; - } - /** * The destroy handler, this is executed when a session is destroyed with * session_destroy() and takes the session id as its only parameter. @@ -164,4 +138,30 @@ class File extends AbstractBase } return true; } + + /** + * A function that is called internally when session data is to be saved. + * + * @param string $sess_id The current session ID + * @param string $data The session data to write + * + * @return bool + */ + protected function saveSession($sess_id, $data) + { + $sess_file = $this->getPath() . '/sess_' . $sess_id; + if ($fp = fopen($sess_file, "w")) { + $return = fwrite($fp, $data); + fclose($fp); + if ($return !== false) { + return true; + } + } + // If we got this far, something went wrong with the file output... + // It is tempting to throw an exception here, but this code is called + // outside of the context of exception handling, so all we can do is + // echo a message. + echo 'Cannot write session to ' . $sess_file . "\n"; + return false; + } } diff --git a/module/VuFind/src/VuFind/Session/ManagerFactory.php b/module/VuFind/src/VuFind/Session/ManagerFactory.php index 16c7ac2e74d..a518893db99 100644 --- a/module/VuFind/src/VuFind/Session/ManagerFactory.php +++ b/module/VuFind/src/VuFind/Session/ManagerFactory.php @@ -91,7 +91,7 @@ class ManagerFactory implements \Zend\ServiceManager\FactoryInterface * handler: http://us.php.net/manual/en/function.session-set-save-handler.php * * This method sets that up. - * + * * @param SessionManager $sessionManager Session manager instance * * @return void @@ -133,7 +133,7 @@ class ManagerFactory implements \Zend\ServiceManager\FactoryInterface // be written as part of the current process): $settings = $sm->get('VuFind\Session\Settings'); if ($settings->setSessionManager($sessionManager)->isWriteDisabled()) { - $sessionManager->writeClose(); + $sessionManager->getSaveHandler()->disableWrites(); } else { // If the session is not disabled, we should set up the normal // shutdown function: diff --git a/module/VuFind/src/VuFind/Session/Memcache.php b/module/VuFind/src/VuFind/Session/Memcache.php index ca035c508ea..25bfd186d06 100644 --- a/module/VuFind/src/VuFind/Session/Memcache.php +++ b/module/VuFind/src/VuFind/Session/Memcache.php @@ -89,21 +89,6 @@ class Memcache extends AbstractBase return $this->getConnection()->get("vufind_sessions/{$sess_id}"); } - /** - * Write function that is called when session data is to be saved. - * - * @param string $sess_id The current session ID - * @param string $data The session data to write - * - * @return bool - */ - public function write($sess_id, $data) - { - return $this->getConnection()->set( - "vufind_sessions/{$sess_id}", $data, 0, $this->lifetime - ); - } - /** * The destroy handler, this is executed when a session is destroyed with * session_destroy() and takes the session id as its only parameter. @@ -120,4 +105,19 @@ class Memcache extends AbstractBase // Perform Memcache-specific cleanup: return $this->getConnection()->delete("vufind_sessions/{$sess_id}"); } + + /** + * A function that is called internally when session data is to be saved. + * + * @param string $sess_id The current session ID + * @param string $data The session data to write + * + * @return bool + */ + protected function saveSession($sess_id, $data) + { + return $this->getConnection()->set( + "vufind_sessions/{$sess_id}", $data, 0, $this->lifetime + ); + } } -- GitLab