SessionHandlerDb.php (mrbs-1.9.4) | : | SessionHandlerDb.php (mrbs-1.10.0) | ||
---|---|---|---|---|
<?php | <?php | |||
namespace MRBS; | namespace MRBS; | |||
use SessionHandlerInterface; | ||||
// Suppress deprecation notices until we get to requiring at least PHP 8 | ||||
// because union types, needed for the return types of read() and gc(), are | ||||
// not supported in PHP 7. | ||||
global $min_PHP_version; | ||||
if (version_compare($min_PHP_version, '8.0.0') < 0) | ||||
{ | ||||
$old_level = error_reporting(); | ||||
error_reporting($old_level & ~E_DEPRECATED); | ||||
} | ||||
else | ||||
{ | ||||
trigger_error("This code can now be removed", E_USER_NOTICE); | ||||
} | ||||
// Use our own PHP session handling by storing sessions in the database. This has three advantages: | // Use our own PHP session handling by storing sessions in the database. This has three advantages: | |||
// (a) it's more secure, especially on shared servers | // (a) it's more secure, especially on shared servers | |||
// (b) it avoids problems with ordinary sessions not working because the PHP session save | // (b) it avoids problems with ordinary sessions not working because the PHP session save | |||
// directory is not writable | // directory is not writable | |||
// (c) it's more resilient in clustered environments | // (c) it's more resilient in clustered environments | |||
class SessionHandlerDb implements \SessionHandlerInterface | class SessionHandlerDb implements SessionHandlerInterface | |||
{ | { | |||
private static $table; | private static $table; | |||
public function __construct() | public function __construct() | |||
{ | { | |||
self::$table = _tbl('sessions'); | self::$table = _tbl('sessions'); | |||
if (!db()->table_exists(self::$table)) | if (!db()->table_exists(self::$table)) | |||
{ | { | |||
// We throw an exception if the table doesn't exist rather than returning FALSE, because in some | // We throw an exception if the table doesn't exist rather than returning FALSE, because in some | |||
skipping to change at line 33 | skipping to change at line 50 | |||
// a session, rather than just returning FALSE as the documentation seems to suggest. So | // a session, rather than just returning FALSE as the documentation seems to suggest. So | |||
// when a new SessionHandlerDb object is created we do it in a try/catch b lock. [Note that | // when a new SessionHandlerDb object is created we do it in a try/catch b lock. [Note that | |||
// the exception can't be thrown on open() because a try/catch round sessi on_start() won't | // the exception can't be thrown on open() because a try/catch round sessi on_start() won't | |||
// catch the exception - maybe because open() is a callback function??] | // catch the exception - maybe because open() is a callback function??] | |||
throw new \Exception("MRBS: session table does not exist"); | throw new \Exception("MRBS: session table does not exist"); | |||
} | } | |||
} | } | |||
// The return value (usually TRUE on success, FALSE on failure). Note this val ue is | // The return value (usually TRUE on success, FALSE on failure). Note this val ue is | |||
// returned internally to PHP for processing. | // returned internally to PHP for processing. | |||
public function open($path, $name) | public function open($path, $name): bool | |||
{ | { | |||
return true; | return true; | |||
} | } | |||
// The return value (usually TRUE on success, FALSE on failure). Note this val ue is | // The return value (usually TRUE on success, FALSE on failure). Note this val ue is | |||
// returned internally to PHP for processing. | // returned internally to PHP for processing. | |||
public function close() | public function close(): bool | |||
{ | { | |||
return true; | return true; | |||
} | } | |||
// Returns an encoded string of the read data. If nothing was read, it must | // Returns an encoded string of the read data. If nothing was read, it must | |||
// return an empty string. Note this value is returned internally to PHP for | // return an empty string. Note this value is returned internally to PHP for | |||
// processing. | // processing. | |||
public function read($id) | public function read($id) | |||
{ | { | |||
try | try | |||
skipping to change at line 77 | skipping to change at line 94 | |||
return ''; | return ''; | |||
} | } | |||
throw $e; | throw $e; | |||
} | } | |||
return ($result === -1) ? '' : $result; | return ($result === -1) ? '' : $result; | |||
} | } | |||
// The return value (usually TRUE on success, FALSE on failure). Note this val ue is | // The return value (usually TRUE on success, FALSE on failure). Note this val ue is | |||
// returned internally to PHP for processing. | // returned internally to PHP for processing. | |||
public function write($id, $data) | public function write($id, $data): bool | |||
{ | { | |||
$sql = "SELECT COUNT(*) FROM " . self::$table . " WHERE id=:id LIMIT 1"; | $sql = "SELECT COUNT(*) FROM " . self::$table . " WHERE id=:id LIMIT 1"; | |||
$rows = db()->query1($sql, array(':id' => $id)); | $rows = db()->query1($sql, array(':id' => $id)); | |||
if ($rows > 0) | if ($rows > 0) | |||
{ | { | |||
$sql = "UPDATE " . self::$table . " | $sql = "UPDATE " . self::$table . " | |||
SET data=:data, access=:access | SET data=:data, access=:access | |||
WHERE id=:id"; | WHERE id=:id"; | |||
} | } | |||
skipping to change at line 108 | skipping to change at line 125 | |||
':data' => $data, | ':data' => $data, | |||
':access' => time()); | ':access' => time()); | |||
db()->command($sql, $sql_params); | db()->command($sql, $sql_params); | |||
return true; | return true; | |||
} | } | |||
// The return value (usually TRUE on success, FALSE on failure). Note this val ue is | // The return value (usually TRUE on success, FALSE on failure). Note this val ue is | |||
// returned internally to PHP for processing. | // returned internally to PHP for processing. | |||
public function destroy($id) | public function destroy($id): bool | |||
{ | { | |||
$sql = "DELETE FROM " . self::$table . " WHERE id=:id"; | try | |||
$rows = db()->command($sql, array(':id' => $id)); | { | |||
return ($rows === 1); | $sql = "DELETE FROM " . self::$table . " WHERE id=:id"; | |||
db()->command($sql, array(':id' => $id)); | ||||
return true; | ||||
} | ||||
catch (\Exception $e) | ||||
{ | ||||
return false; | ||||
} | ||||
} | } | |||
// The return value (usually TRUE on success, FALSE on failure). Note this val ue is | // The return value (usually TRUE on success, FALSE on failure). Note this val ue is | |||
// returned internally to PHP for processing. | // returned internally to PHP for processing. | |||
public function gc($max_lifetime) | public function gc($max_lifetime) | |||
{ | { | |||
$sql = "DELETE FROM " . self::$table . " WHERE access<:old"; | $sql = "DELETE FROM " . self::$table . " WHERE access<:old"; | |||
db()->command($sql, array(':old' => time() - $max_lifetime)); | db()->command($sql, array(':old' => time() - $max_lifetime)); | |||
return true; // An exception will be thrown on error | return true; // An exception will be thrown on error | |||
} | } | |||
} | } | |||
// Restore the original error reporting level | ||||
if (version_compare($min_PHP_version, '8.0.0') < 0) | ||||
{ | ||||
error_reporting($old_level); | ||||
} | ||||
else | ||||
{ | ||||
trigger_error("This code can now be removed", E_USER_NOTICE); | ||||
} | ||||
End of changes. 8 change blocks. | ||||
8 lines changed or deleted | 32 lines changed or added |