diff --git a/module/VuFind/src/VuFind/Session/AbstractBase.php b/module/VuFind/src/VuFind/Session/AbstractBase.php index 98f3a1dbb7b7279b160c069868bcb469e3f25ada..9febadbb966ca2dead70f360bb60c61553cc9526 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 23a57bb56cf036bc2f5f8d6065e63e84aca54827..089f30fe532a8187f3939852eefc24a59ef602df 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 fbfa84da7ba6b7bcbd52fc204b1ff5c77ea523eb..e05ba4531f0ae7a5a3831e0b484dca9b33715cda 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 16c7ac2e74d8fa8bd6399a329922fcad0efc92d7..a518893db99cf4f3d387971fece66de3dafd6a4a 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 ca035c508eafe652b16aa0b5d1a3e77d5aa24b4c..25bfd186d06abb9abc209edf49daa120693f8f0b 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 + ); + } }