move session-related functions to their own namespace

This commit is contained in:
Andrew Dolgov 2021-02-16 17:13:16 +03:00
parent 7fad6ce651
commit 9d7ba773ec
4 changed files with 18 additions and 15 deletions

View File

@ -50,7 +50,7 @@
if (!init_plugins()) return;
if (!empty($_SESSION["uid"])) {
if (!validate_session()) {
if (!\Sessions\validate_session()) {
header("Content-Type: text/json");
print json_encode(array("seq" => -1,

View File

@ -45,7 +45,7 @@
}
if (!empty($_SESSION["uid"])) {
if (!validate_session()) {
if (!\Sessions\validate_session()) {
header("Content-Type: text/json");
print error_json(6);
return;

View File

@ -94,7 +94,7 @@ class UserHelper {
startup_gettext();
self::load_user_plugins($_SESSION["uid"]);
} else {
if (!validate_session()) $_SESSION["uid"] = false;
if (!\Sessions\validate_session()) $_SESSION["uid"] = false;
if (empty($_SESSION["uid"])) {

View File

@ -1,4 +1,6 @@
<?php
namespace Sessions;
// Original from http://www.daniweb.com/code/snippet43.html
require_once "config.php";
@ -23,7 +25,7 @@
global $schema_version;
if (!$schema_version) {
$row = Db::pdo()->query("SELECT schema_version FROM ttrss_version")->fetch();
$row = \Db::pdo()->query("SELECT schema_version FROM ttrss_version")->fetch();
$version = $row["schema_version"];
@ -42,7 +44,7 @@
__("Session failed to validate (schema version changed)");
return false;
}
$pdo = Db::pdo();
$pdo = \Db::pdo();
if (!empty($_SESSION["uid"])) {
@ -85,7 +87,7 @@
function ttrss_read ($id){
global $session_expire;
$sth = Db::pdo()->prepare("SELECT data FROM ttrss_sessions WHERE id=?");
$sth = \Db::pdo()->prepare("SELECT data FROM ttrss_sessions WHERE id=?");
$sth->execute([$id]);
if ($row = $sth->fetch()) {
@ -94,7 +96,7 @@
} else {
$expire = time() + $session_expire;
$sth = Db::pdo()->prepare("INSERT INTO ttrss_sessions (id, data, expire)
$sth = \Db::pdo()->prepare("INSERT INTO ttrss_sessions (id, data, expire)
VALUES (?, '', ?)");
$sth->execute([$id, $expire]);
@ -110,14 +112,14 @@
$data = base64_encode($data);
$expire = time() + $session_expire;
$sth = Db::pdo()->prepare("SELECT id FROM ttrss_sessions WHERE id=?");
$sth = \Db::pdo()->prepare("SELECT id FROM ttrss_sessions WHERE id=?");
$sth->execute([$id]);
if ($row = $sth->fetch()) {
$sth = Db::pdo()->prepare("UPDATE ttrss_sessions SET data=?, expire=? WHERE id=?");
$sth = \Db::pdo()->prepare("UPDATE ttrss_sessions SET data=?, expire=? WHERE id=?");
$sth->execute([$data, $expire, $id]);
} else {
$sth = Db::pdo()->prepare("INSERT INTO ttrss_sessions (id, data, expire)
$sth = \Db::pdo()->prepare("INSERT INTO ttrss_sessions (id, data, expire)
VALUES (?, ?, ?)");
$sth->execute([$id, $data, $expire]);
}
@ -130,22 +132,23 @@
}
function ttrss_destroy($id) {
$sth = Db::pdo()->prepare("DELETE FROM ttrss_sessions WHERE id = ?");
$sth = \Db::pdo()->prepare("DELETE FROM ttrss_sessions WHERE id = ?");
$sth->execute([$id]);
return true;
}
function ttrss_gc ($expire) {
Db::pdo()->query("DELETE FROM ttrss_sessions WHERE expire < " . time());
\Db::pdo()->query("DELETE FROM ttrss_sessions WHERE expire < " . time());
return true;
}
if (!SINGLE_USER_MODE /* && DB_TYPE == "pgsql" */) {
session_set_save_handler("ttrss_open",
"ttrss_close", "ttrss_read", "ttrss_write",
"ttrss_destroy", "ttrss_gc");
session_set_save_handler('\Sessions\ttrss_open',
'\Sessions\ttrss_close', '\Sessions\ttrss_read',
'\Sessions\ttrss_write', '\Sessions\ttrss_destroy',
'\Sessions\ttrss_gc');
register_shutdown_function('session_write_close');
}