From 9d7ba773ec97bfb44601348c07e818f1a1d2c841 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 16 Feb 2021 17:13:16 +0300 Subject: [PATCH] move session-related functions to their own namespace --- api/index.php | 2 +- backend.php | 2 +- classes/userhelper.php | 2 +- include/sessions.php | 27 +++++++++++++++------------ 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/api/index.php b/api/index.php index 664e92abe..eb79422f9 100644 --- a/api/index.php +++ b/api/index.php @@ -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, diff --git a/backend.php b/backend.php index 89b06b7eb..9ecc22914 100644 --- a/backend.php +++ b/backend.php @@ -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; diff --git a/classes/userhelper.php b/classes/userhelper.php index 8e9b9a01b..42d50a0f4 100644 --- a/classes/userhelper.php +++ b/classes/userhelper.php @@ -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"])) { diff --git a/include/sessions.php b/include/sessions.php index 4de894c95..16de53ab2 100644 --- a/include/sessions.php +++ b/include/sessions.php @@ -1,4 +1,6 @@ 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'); }