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
This commit is contained in:
		| @@ -17,9 +17,8 @@ class Config { | |||||||
|  |  | ||||||
|     protected $databaseConfig; |     protected $databaseConfig; | ||||||
|     protected $config; |     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); |         return self::i()->getValue($key, $default); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -36,14 +35,13 @@ class Config { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         $this->databaseConfig = $config; |         $this->databaseConfig = $config; | ||||||
|         $this->cache = new PhpFileCache(__CACHE_DIR, "config"); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Returns config used to connect to the database |      * Returns config used to connect to the database | ||||||
|      * @return array Config as an array |      * @return array Config as an array | ||||||
|      */ |      */ | ||||||
|     public function getDatabaseConfig() { |     public function getDatabaseConfig(): array { | ||||||
|         return $this->databaseConfig; |         return $this->databaseConfig; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -51,9 +49,8 @@ class Config { | |||||||
|      * Returns configuration saved in database |      * Returns configuration saved in database | ||||||
|      * @return array Config file as an key => value array |      * @return array Config file as an key => value array | ||||||
|      */ |      */ | ||||||
|     public function getConfig() { |     public function getConfig(): array { | ||||||
|         if($this->config === null) { |         if($this->config === null) { | ||||||
|             $this->config = $this->cache->refreshIfExpired("config", function () { |  | ||||||
|             try { |             try { | ||||||
|                 $db = DatabaseUtils::i()->getDb(); |                 $db = DatabaseUtils::i()->getDb(); | ||||||
|                 $data = $db->select("config", ["identifier", "type", "value"]); |                 $data = $db->select("config", ["identifier", "type", "value"]); | ||||||
| @@ -62,10 +59,6 @@ class Config { | |||||||
|                 exit; |                 exit; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|                 if(!empty($db->error()[1])) { |  | ||||||
|                     return null; |  | ||||||
|                 } |  | ||||||
|  |  | ||||||
|             $cfg = []; |             $cfg = []; | ||||||
|  |  | ||||||
|             foreach ($data as $item) { |             foreach ($data as $item) { | ||||||
| @@ -89,7 +82,7 @@ class Config { | |||||||
|                     case "JSON": |                     case "JSON": | ||||||
|                         $json = json_decode((string) $val, true); |                         $json = json_decode((string) $val, true); | ||||||
|  |  | ||||||
|                             if ($json === false) { |                         if (json_last_error() !== JSON_ERROR_NONE) { | ||||||
|                             throw new \Exception("Error loading config from db: cannot parse JSON from $key"); |                             throw new \Exception("Error loading config from db: cannot parse JSON from $key"); | ||||||
|                         } |                         } | ||||||
|  |  | ||||||
| @@ -102,8 +95,7 @@ class Config { | |||||||
|                 $cfg[$key] = $val; |                 $cfg[$key] = $val; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|                 return $cfg; |             $this->config = $cfg; | ||||||
|             }, 60); |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return $this->config; |         return $this->config; | ||||||
| @@ -112,9 +104,8 @@ class Config { | |||||||
|     /** |     /** | ||||||
|      * Resets current config cache |      * Resets current config cache | ||||||
|      */ |      */ | ||||||
|     public function clearConfigCache() { |     public function clearConfigCache(): void { | ||||||
|         $this->config = null; |         $this->config = null; | ||||||
|         $this->cache->eraseKey("config"); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -123,19 +114,19 @@ class Config { | |||||||
|      * @param null $default |      * @param null $default | ||||||
|      * @return mixed value Returns string with |      * @return mixed value Returns string with | ||||||
|      * the value if key exists, null otherwise |      * the value if key exists, null otherwise | ||||||
|  |      * @throws \Exception | ||||||
|      */ |      */ | ||||||
|     public function getValue($key, $default = null) { |     public function getValue(string $key, $default = null) { | ||||||
|         return isset($this->getConfig()[$key]) ? $this->getConfig()[$key] : $default; |         return $this->getConfig()[$key] ?? $default; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Saves key => value combo in config table |      * Saves key => value combo in config table | ||||||
|      * @param string $key |      * @param string $key | ||||||
|      * @param string|int|float|bool|array|object $value |      * @param string|int|float|bool|array|object $value | ||||||
|      * @return bool true on success, false otherwise |  | ||||||
|      * @throws \Exception |      * @throws \Exception | ||||||
|      */ |      */ | ||||||
|     public function setValue($key, $value) { |     public function setValue(string $key, $value): void { | ||||||
|         $db = DatabaseUtils::i()->getDb(); |         $db = DatabaseUtils::i()->getDb(); | ||||||
|  |  | ||||||
|         switch (gettype($value)) { |         switch (gettype($value)) { | ||||||
| @@ -168,12 +159,11 @@ class Config { | |||||||
|         ]; |         ]; | ||||||
|  |  | ||||||
|         if($db->has("config", ["identifier" => $key])) { |         if($db->has("config", ["identifier" => $key])) { | ||||||
|             $ret = $db->update("config", $data, ["identifier" => $key]); |             $db->update("config", $data, ["identifier" => $key]); | ||||||
|         } else { |         } else { | ||||||
|             $ret = $db->insert("config", $data); |             $db->insert("config", $data); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         $this->clearConfigCache(); |         $this->clearConfigCache(); | ||||||
|         return $ret; |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -26,7 +26,7 @@ class DatabaseUtils { | |||||||
|      * database config. Stores connection for reuse. |      * database config. Stores connection for reuse. | ||||||
|      * @return \Medoo\Medoo database object |      * @return \Medoo\Medoo database object | ||||||
|      */ |      */ | ||||||
|     public function getDb() { |     public function getDb(): Medoo { | ||||||
|         if($this->db === null) { |         if($this->db === null) { | ||||||
|             try { |             try { | ||||||
|                 $config = $this->configUtils->getDatabaseConfig(); |                 $config = $this->configUtils->getDatabaseConfig(); | ||||||
| @@ -53,7 +53,7 @@ class DatabaseUtils { | |||||||
|      * for checking if there was a database connection attempt. |      * for checking if there was a database connection attempt. | ||||||
|      * @return bool |      * @return bool | ||||||
|      */ |      */ | ||||||
|     public function isInitialised() { |     public function isInitialised(): bool { | ||||||
|         return !empty($this->db); |         return $this->db !== null; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -85,21 +85,19 @@ class TemplateUtils { | |||||||
|             $data["userLanguage"] = $userlang; |             $data["userLanguage"] = $userlang; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if ($timestamp = $this->getOldestCacheTimestamp()) |         if ($timestamp = $this->getOldestCacheTimestamp()) { | ||||||
|             $data["oldestTimestamp"] = $timestamp; |             $data["oldestTimestamp"] = $timestamp; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         $data["tsExceptions"] = TeamSpeakUtils::i()->getExceptionsList(); |         $data["tsExceptions"] = TeamSpeakUtils::i()->getExceptionsList(); | ||||||
|  |  | ||||||
|         if(@$dbutils->isInitialised()) |         $data["config"] = []; | ||||||
|             $data["sqlCount"] = @$dbutils->getDb()->query("SHOW SESSION STATUS LIKE 'Questions'")->fetch()["Value"]; |  | ||||||
|         else |  | ||||||
|         $data["sqlCount"] = "none"; |         $data["sqlCount"] = "none"; | ||||||
|  |  | ||||||
|  |         // only fetch those when DB connection is established | ||||||
|  |         if($dbutils->isInitialised()) { | ||||||
|             $data["config"] = Config::i()->getConfig(); |             $data["config"] = Config::i()->getConfig(); | ||||||
|  |             $data["sqlCount"] = @$dbutils->getDb()->query("SHOW SESSION STATUS LIKE 'Questions'")->fetchColumn(1); | ||||||
|         $csrfToken = CsrfUtils::getToken(); |  | ||||||
|         $data["csrfToken"] = $csrfToken; |  | ||||||
|         $data["csrfField"] = new Html('<input type="hidden" name="csrf-token" value="' . $csrfToken . '">'); |  | ||||||
|  |  | ||||||
|             if (Config::get("adminstatus_enabled")) { |             if (Config::get("adminstatus_enabled")) { | ||||||
|                 $data["adminStatus"] = AdminStatus::i()->getStatus( |                 $data["adminStatus"] = AdminStatus::i()->getStatus( | ||||||
| @@ -109,6 +107,11 @@ class TemplateUtils { | |||||||
|                     Config::get("adminstatus_ignoredusers") |                     Config::get("adminstatus_ignoredusers") | ||||||
|                 ); |                 ); | ||||||
|             } |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         $csrfToken = CsrfUtils::getToken(); | ||||||
|  |         $data["csrfToken"] = $csrfToken; | ||||||
|  |         $data["csrfField"] = new Html('<input type="hidden" name="csrf-token" value="' . $csrfToken . '">'); | ||||||
|  |  | ||||||
|         return $this->getLatte()->renderToString(__TEMPLATES_DIR . "/$templateName.latte", $data); |         return $this->getLatte()->renderToString(__TEMPLATES_DIR . "/$templateName.latte", $data); | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user