MDL-79890 core: Session handlers must implement SessionHandlerInterface

This commit is contained in:
Andrew Nicols
2023-11-15 09:14:40 +08:00
parent dfccb3f3c4
commit e734858e55
4 changed files with 126 additions and 127 deletions
+34 -32
View File
@@ -24,7 +24,7 @@
namespace core\session;
defined('MOODLE_INTERNAL') || die();
use SessionHandlerInterface;
/**
* Database based session handler.
@@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class database extends handler {
class database extends handler implements SessionHandlerInterface {
/** @var \stdClass $record session record */
protected $recordid = null;
@@ -70,12 +70,7 @@ class database extends handler {
throw new exception('sessionhandlerproblem', 'error', '', null, 'Database does not support session locking');
}
$result = session_set_save_handler(array($this, 'handler_open'),
array($this, 'handler_close'),
array($this, 'handler_read'),
array($this, 'handler_write'),
array($this, 'handler_destroy'),
array($this, 'handler_gc'));
$result = session_set_save_handler($this);
if (!$result) {
throw new exception('dbsessionhandlerproblem', 'error');
}
@@ -117,11 +112,11 @@ class database extends handler {
*
* {@see http://php.net/manual/en/function.session-set-save-handler.php}
*
* @param string $save_path
* @param string $session_name
* @param string $path
* @param string $name
* @return bool success
*/
public function handler_open($save_path, $session_name) {
public function open(string $path, string $name): bool {
// Note: we use the already open database.
return true;
}
@@ -133,7 +128,7 @@ class database extends handler {
*
* @return bool success
*/
public function handler_close() {
public function close(): bool {
if ($this->recordid) {
try {
$this->database->release_session_lock($this->recordid);
@@ -152,9 +147,9 @@ class database extends handler {
* {@see http://php.net/manual/en/function.session-set-save-handler.php}
*
* @param string $sid
* @return string
* @return string|false
*/
public function handler_read($sid) {
public function read(string $sid): string|false {
try {
if (!$record = $this->database->get_record('sessions', array('sid'=>$sid), 'id')) {
// Let's cheat and skip locking if this is the first access,
@@ -221,17 +216,18 @@ class database extends handler {
* NOTE: Do not write to output or throw any exceptions!
* Hopefully the next page is going to display nice error or it recovers...
*
* @param string $sid
* @param string $session_data
* @param string $id
* @param string $data
* @return bool success
*/
public function handler_write($sid, $session_data) {
public function write(string $id, string $data): bool {
if ($this->failed) {
// Do not write anything back - we failed to start the session properly.
return false;
}
$sessdata = base64_encode($session_data); // There might be some binary mess :-(
// There might be some binary mess.
$sessdata = base64_encode($data);
$hash = sha1($sessdata);
if ($hash === $this->lasthash) {
@@ -240,14 +236,17 @@ class database extends handler {
try {
if ($this->recordid) {
$this->database->set_field('sessions', 'sessdata', $sessdata, array('id'=>$this->recordid));
$this->database->set_field('sessions', 'sessdata', $sessdata, ['id' => $this->recordid]);
} else {
// This happens in the first request when session record was just created in manager.
$this->database->set_field('sessions', 'sessdata', $sessdata, array('sid'=>$sid));
$this->database->set_field('sessions', 'sessdata', $sessdata, ['sid' => $id]);
}
} catch (\Exception $ex) {
// Do not rethrow exceptions here, this should not happen.
error_log('Unknown exception when writing database session data : '.$sid.' - '.$ex->getMessage());
// phpcs:ignore moodle.PHP.ForbiddenFunctions.FoundWithAlternative
error_log(
"Unknown exception when writing database session data : {$id} - " . $ex->getMessage(),
);
}
return true;
@@ -258,19 +257,19 @@ class database extends handler {
*
* {@see http://php.net/manual/en/function.session-set-save-handler.php}
*
* @param string $sid
* @param string $id
* @return bool success
*/
public function handler_destroy($sid) {
if (!$session = $this->database->get_record('sessions', array('sid'=>$sid), 'id, sid')) {
if ($sid == session_id()) {
public function destroy(string $id): bool {
if (!$session = $this->database->get_record('sessions', ['sid' => $id], 'id, sid')) {
if ($id == session_id()) {
$this->recordid = null;
$this->lasthash = null;
}
return true;
}
if ($this->recordid and $session->id == $this->recordid) {
if ($this->recordid && ($session->id == $this->recordid)) {
try {
$this->database->release_session_lock($this->recordid);
} catch (\Exception $ex) {
@@ -280,7 +279,7 @@ class database extends handler {
$this->lasthash = null;
}
$this->database->delete_records('sessions', array('id'=>$session->id));
$this->database->delete_records('sessions', ['id' => $session->id]);
return true;
}
@@ -290,16 +289,19 @@ class database extends handler {
*
* {@see http://php.net/manual/en/function.session-set-save-handler.php}
*
* @param int $ignored_maxlifetime moodle uses special timeout rules
* @param int $max_lifetime moodle uses special timeout rules
* @return bool success
*/
public function handler_gc($ignored_maxlifetime) {
// phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameUnderscore
public function gc(int $max_lifetime): int|false {
// This should do something only if cron is not running properly...
if (!$stalelifetime = ini_get('session.gc_maxlifetime')) {
return true;
return false;
}
$params = array('purgebefore' => (time() - $stalelifetime));
$params = ['purgebefore' => (time() - $stalelifetime)];
$count = $this->database->count_records_select('sessions', 'userid = 0 AND timemodified < :purgebefore', $params);
$this->database->delete_records_select('sessions', 'userid = 0 AND timemodified < :purgebefore', $params);
return true;
return $count;
}
}