From 502a7d2fde084477b2fcfca87f901f506819080c Mon Sep 17 00:00:00 2001 From: Wruczek Date: Tue, 6 Oct 2020 17:05:08 +0200 Subject: [PATCH] More minor improvements - more type declarations - remove config caching, it was a minor speed improvement, but a big confusion when website didnt updated after editing config - fix issue where DB problems will display a PHP recursion error instead of the error template --- src/private/php/Config.php | 114 +++++++++++------------- src/private/php/Utils/DatabaseUtils.php | 6 +- src/private/php/Utils/TemplateUtils.php | 33 +++---- 3 files changed, 73 insertions(+), 80 deletions(-) diff --git a/src/private/php/Config.php b/src/private/php/Config.php index ff51976..e8834b5 100644 --- a/src/private/php/Config.php +++ b/src/private/php/Config.php @@ -17,9 +17,8 @@ class Config { protected $databaseConfig; protected $config; - protected $cache; - public static function get($key, $default = null) { + public static function get(string $key, $default = null) { return self::i()->getValue($key, $default); } @@ -36,14 +35,13 @@ class Config { } $this->databaseConfig = $config; - $this->cache = new PhpFileCache(__CACHE_DIR, "config"); } /** * Returns config used to connect to the database * @return array Config as an array */ - public function getDatabaseConfig() { + public function getDatabaseConfig(): array { return $this->databaseConfig; } @@ -51,59 +49,53 @@ class Config { * Returns configuration saved in database * @return array Config file as an key => value array */ - public function getConfig() { + public function getConfig(): array { if($this->config === null) { - $this->config = $this->cache->refreshIfExpired("config", function () { - try { - $db = DatabaseUtils::i()->getDb(); - $data = $db->select("config", ["identifier", "type", "value"]); - } catch (\Exception $e) { - TemplateUtils::i()->renderErrorTemplate("DB error", "Cannot get config data from database", $e->getMessage()); - exit; + try { + $db = DatabaseUtils::i()->getDb(); + $data = $db->select("config", ["identifier", "type", "value"]); + } catch (\Exception $e) { + TemplateUtils::i()->renderErrorTemplate("DB error", "Cannot get config data from database", $e->getMessage()); + exit; + } + + $cfg = []; + + foreach ($data as $item) { + $key = $item["identifier"]; + $type = $item["type"]; + $val = $item["value"]; + + switch ($type) { + case "STRING": + $val = (string) $val; + break; + case "INT": + $val = (int) $val; + break; + case "FLOAT": + $val = (float) $val; + break; + case "BOOL": + $val = strtolower($val) === "true"; + break; + case "JSON": + $json = json_decode((string) $val, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + throw new \Exception("Error loading config from db: cannot parse JSON from $key"); + } + + $val = $json; + break; + default: + throw new \Exception("Error loading config from db: unrecognised data type $type"); } - if(!empty($db->error()[1])) { - return null; - } + $cfg[$key] = $val; + } - $cfg = []; - - foreach ($data as $item) { - $key = $item["identifier"]; - $type = $item["type"]; - $val = $item["value"]; - - switch ($type) { - case "STRING": - $val = (string) $val; - break; - case "INT": - $val = (int) $val; - break; - case "FLOAT": - $val = (float) $val; - break; - case "BOOL": - $val = strtolower($val) === "true"; - break; - case "JSON": - $json = json_decode((string) $val, true); - - if ($json === false) { - throw new \Exception("Error loading config from db: cannot parse JSON from $key"); - } - - $val = $json; - break; - default: - throw new \Exception("Error loading config from db: unrecognised data type $type"); - } - - $cfg[$key] = $val; - } - - return $cfg; - }, 60); + $this->config = $cfg; } return $this->config; @@ -112,9 +104,8 @@ class Config { /** * Resets current config cache */ - public function clearConfigCache() { + public function clearConfigCache(): void { $this->config = null; - $this->cache->eraseKey("config"); } /** @@ -123,19 +114,19 @@ class Config { * @param null $default * @return mixed value Returns string with * the value if key exists, null otherwise + * @throws \Exception */ - public function getValue($key, $default = null) { - return isset($this->getConfig()[$key]) ? $this->getConfig()[$key] : $default; + public function getValue(string $key, $default = null) { + return $this->getConfig()[$key] ?? $default; } /** * Saves key => value combo in config table * @param string $key * @param string|int|float|bool|array|object $value - * @return bool true on success, false otherwise * @throws \Exception */ - public function setValue($key, $value) { + public function setValue(string $key, $value): void { $db = DatabaseUtils::i()->getDb(); switch (gettype($value)) { @@ -168,12 +159,11 @@ class Config { ]; if($db->has("config", ["identifier" => $key])) { - $ret = $db->update("config", $data, ["identifier" => $key]); + $db->update("config", $data, ["identifier" => $key]); } else { - $ret = $db->insert("config", $data); + $db->insert("config", $data); } $this->clearConfigCache(); - return $ret; } } diff --git a/src/private/php/Utils/DatabaseUtils.php b/src/private/php/Utils/DatabaseUtils.php index 0d52f67..98d7b97 100644 --- a/src/private/php/Utils/DatabaseUtils.php +++ b/src/private/php/Utils/DatabaseUtils.php @@ -26,7 +26,7 @@ class DatabaseUtils { * database config. Stores connection for reuse. * @return \Medoo\Medoo database object */ - public function getDb() { + public function getDb(): Medoo { if($this->db === null) { try { $config = $this->configUtils->getDatabaseConfig(); @@ -53,7 +53,7 @@ class DatabaseUtils { * for checking if there was a database connection attempt. * @return bool */ - public function isInitialised() { - return !empty($this->db); + public function isInitialised(): bool { + return $this->db !== null; } } diff --git a/src/private/php/Utils/TemplateUtils.php b/src/private/php/Utils/TemplateUtils.php index 95ecf4f..0a3bfc6 100644 --- a/src/private/php/Utils/TemplateUtils.php +++ b/src/private/php/Utils/TemplateUtils.php @@ -85,31 +85,34 @@ class TemplateUtils { $data["userLanguage"] = $userlang; } - if ($timestamp = $this->getOldestCacheTimestamp()) + if ($timestamp = $this->getOldestCacheTimestamp()) { $data["oldestTimestamp"] = $timestamp; + } $data["tsExceptions"] = TeamSpeakUtils::i()->getExceptionsList(); - if(@$dbutils->isInitialised()) - $data["sqlCount"] = @$dbutils->getDb()->query("SHOW SESSION STATUS LIKE 'Questions'")->fetch()["Value"]; - else - $data["sqlCount"] = "none"; + $data["config"] = []; + $data["sqlCount"] = "none"; - $data["config"] = Config::i()->getConfig(); + // only fetch those when DB connection is established + if($dbutils->isInitialised()) { + $data["config"] = Config::i()->getConfig(); + $data["sqlCount"] = @$dbutils->getDb()->query("SHOW SESSION STATUS LIKE 'Questions'")->fetchColumn(1); + + if (Config::get("adminstatus_enabled")) { + $data["adminStatus"] = AdminStatus::i()->getStatus( + Config::get("adminstatus_groups"), + Config::get("adminstatus_mode"), + Config::get("adminstatus_hideoffline"), + Config::get("adminstatus_ignoredusers") + ); + } + } $csrfToken = CsrfUtils::getToken(); $data["csrfToken"] = $csrfToken; $data["csrfField"] = new Html(''); - if (Config::get("adminstatus_enabled")) { - $data["adminStatus"] = AdminStatus::i()->getStatus( - Config::get("adminstatus_groups"), - Config::get("adminstatus_mode"), - Config::get("adminstatus_hideoffline"), - Config::get("adminstatus_ignoredusers") - ); - } - return $this->getLatte()->renderToString(__TEMPLATES_DIR . "/$templateName.latte", $data); }