1
0
mirror of https://git.tt-rss.org/git/tt-rss.git synced 2025-12-13 08:25:55 +00:00

db-prefs: minor cleanup, add warnings if unknown prefs are requested

This commit is contained in:
Andrew Dolgov
2021-02-15 22:01:11 +03:00
parent 70e293bccb
commit 5c4223992f

View File

@@ -6,9 +6,8 @@ class Db_Prefs {
function __construct() { function __construct() {
$this->pdo = Db::pdo(); $this->pdo = Db::pdo();
$this->cache = array(); $this->cache = [];
$this->cache_prefs();
if (!empty($_SESSION["uid"])) $this->cache();
} }
private function __clone() { private function __clone() {
@@ -22,31 +21,30 @@ class Db_Prefs {
return self::$instance; return self::$instance;
} }
function cache() { private function cache_prefs() {
$user_id = $_SESSION["uid"]; if (!empty($_SESSION["uid"])) {
$profile = $_SESSION["profile"] ?? false; $profile = $_SESSION["profile"] ?? false;
if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null; if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null;
$sth = $this->pdo->prepare("SELECT $sth = $this->pdo->prepare("SELECT up.pref_name, pt.type_name, up.value
value,ttrss_prefs_types.type_name as type_name,ttrss_prefs.pref_name AS pref_name FROM ttrss_user_prefs up
FROM JOIN ttrss_prefs p ON (up.pref_name = p.pref_name)
ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types JOIN ttrss_prefs_types pt ON (p.type_id = pt.id)
WHERE WHERE
(profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND up.pref_name NOT LIKE '_MOBILE%' AND
ttrss_prefs.pref_name NOT LIKE '_MOBILE%' AND (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND
ttrss_prefs_types.id = type_id AND owner_uid = :uid");
owner_uid = :uid AND
ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
$sth->execute([":profile" => $profile, ":uid" => $user_id]); $sth->execute([":profile" => $profile, ":uid" => $_SESSION["uid"]]);
while ($line = $sth->fetch()) { while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
if ($user_id == $_SESSION["uid"]) { $pref_name = $row["pref_name"];
$pref_name = $line["pref_name"];
$this->cache[$pref_name]["type"] = $line["type_name"]; $this->cache[$pref_name] = [
$this->cache[$pref_name]["value"] = $line["value"]; "type" => $row["type_name"],
"value" => $row["value"]
];
} }
} }
} }
@@ -67,35 +65,37 @@ class Db_Prefs {
if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null; if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null;
$sth = $this->pdo->prepare("SELECT $sth = $this->pdo->prepare("SELECT up.pref_name, pt.type_name, up.value
value,ttrss_prefs_types.type_name as type_name FROM ttrss_user_prefs up
FROM JOIN ttrss_prefs p ON (up.pref_name = p.pref_name)
ttrss_user_prefs,ttrss_prefs,ttrss_prefs_types JOIN ttrss_prefs_types pt ON (p.type_id = pt.id)
WHERE WHERE
up.pref_name = :pref_name AND
(profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND (profile = :profile OR (:profile IS NULL AND profile IS NULL)) AND
ttrss_user_prefs.pref_name = :pref_name AND owner_uid = :uid");
ttrss_prefs_types.id = type_id AND
owner_uid = :uid AND
ttrss_user_prefs.pref_name = ttrss_prefs.pref_name");
$sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]); $sth->execute([":uid" => $user_id, ":profile" => $profile, ":pref_name" => $pref_name]);
if ($row = $sth->fetch()) { if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$value = $row["value"]; $value = $row["value"];
$type_name = $row["type_name"]; $type_name = $row["type_name"];
if ($user_id == ($_SESSION["uid"] ?? false)) { if ($user_id == ($_SESSION["uid"] ?? false)) {
$this->cache[$pref_name]["type"] = $type_name; $this->cache[$pref_name] = [
$this->cache[$pref_name]["value"] = $value; "type" => $row["type_name"],
"value" => $row["value"]
];
} }
return $this->convert($value, $type_name); return $this->convert($value, $type_name);
} else if ($die_on_error) { } else if ($die_on_error) {
user_error("Fatal error, unknown preferences key: $pref_name (owner: $user_id)", E_USER_ERROR); user_error("Failed retrieving preference $pref_name for user $user_id", E_USER_ERROR);
return null;
} else { } else {
return null; user_error("Failed retrieving preference $pref_name for user $user_id", E_USER_WARNING);
} }
return null;
} }
function convert($value, $type_name) { function convert($value, $type_name) {