Skip to content
Snippets Groups Projects
Commit d5d7d18f authored by Ere Maijala's avatar Ere Maijala Committed by Demian Katz
Browse files

Made disabling of session writes really disable the writes (#728)

- Now no attempt is made to store the session when disabled.
parent f6c3facf
No related merge requests found
...@@ -58,6 +58,24 @@ abstract class AbstractBase implements SaveHandlerInterface, ...@@ -58,6 +58,24 @@ abstract class AbstractBase implements SaveHandlerInterface,
*/ */
protected $config = null; 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. * Set configuration.
* *
...@@ -145,4 +163,30 @@ abstract class AbstractBase implements SaveHandlerInterface, ...@@ -145,4 +163,30 @@ abstract class AbstractBase implements SaveHandlerInterface,
// Something to keep in mind though. // Something to keep in mind though.
return true; 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);
} }
...@@ -59,20 +59,6 @@ class Database extends AbstractBase ...@@ -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 * The destroy handler, this is executed when a session is destroyed with
* session_destroy() and takes the session id as its only parameter. * session_destroy() and takes the session id as its only parameter.
...@@ -105,4 +91,18 @@ class Database extends AbstractBase ...@@ -105,4 +91,18 @@ class Database extends AbstractBase
$this->getTable('Session')->garbageCollect($sess_maxlifetime); $this->getTable('Session')->garbageCollect($sess_maxlifetime);
return true; 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;
}
} }
...@@ -100,32 +100,6 @@ class File extends AbstractBase ...@@ -100,32 +100,6 @@ class File extends AbstractBase
return (string)file_get_contents($sess_file); 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 * The destroy handler, this is executed when a session is destroyed with
* session_destroy() and takes the session id as its only parameter. * session_destroy() and takes the session id as its only parameter.
...@@ -164,4 +138,30 @@ class File extends AbstractBase ...@@ -164,4 +138,30 @@ class File extends AbstractBase
} }
return true; 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;
}
} }
...@@ -91,7 +91,7 @@ class ManagerFactory implements \Zend\ServiceManager\FactoryInterface ...@@ -91,7 +91,7 @@ class ManagerFactory implements \Zend\ServiceManager\FactoryInterface
* handler: http://us.php.net/manual/en/function.session-set-save-handler.php * handler: http://us.php.net/manual/en/function.session-set-save-handler.php
* *
* This method sets that up. * This method sets that up.
* *
* @param SessionManager $sessionManager Session manager instance * @param SessionManager $sessionManager Session manager instance
* *
* @return void * @return void
...@@ -133,7 +133,7 @@ class ManagerFactory implements \Zend\ServiceManager\FactoryInterface ...@@ -133,7 +133,7 @@ class ManagerFactory implements \Zend\ServiceManager\FactoryInterface
// be written as part of the current process): // be written as part of the current process):
$settings = $sm->get('VuFind\Session\Settings'); $settings = $sm->get('VuFind\Session\Settings');
if ($settings->setSessionManager($sessionManager)->isWriteDisabled()) { if ($settings->setSessionManager($sessionManager)->isWriteDisabled()) {
$sessionManager->writeClose(); $sessionManager->getSaveHandler()->disableWrites();
} else { } else {
// If the session is not disabled, we should set up the normal // If the session is not disabled, we should set up the normal
// shutdown function: // shutdown function:
......
...@@ -89,21 +89,6 @@ class Memcache extends AbstractBase ...@@ -89,21 +89,6 @@ class Memcache extends AbstractBase
return $this->getConnection()->get("vufind_sessions/{$sess_id}"); 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 * The destroy handler, this is executed when a session is destroyed with
* session_destroy() and takes the session id as its only parameter. * session_destroy() and takes the session id as its only parameter.
...@@ -120,4 +105,19 @@ class Memcache extends AbstractBase ...@@ -120,4 +105,19 @@ class Memcache extends AbstractBase
// Perform Memcache-specific cleanup: // Perform Memcache-specific cleanup:
return $this->getConnection()->delete("vufind_sessions/{$sess_id}"); 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
);
}
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment