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,59 +49,53 @@ 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"]); |             } catch (\Exception $e) { | ||||||
|                 } catch (\Exception $e) { |                 TemplateUtils::i()->renderErrorTemplate("DB error", "Cannot get config data from database", $e->getMessage()); | ||||||
|                     TemplateUtils::i()->renderErrorTemplate("DB error", "Cannot get config data from database", $e->getMessage()); |                 exit; | ||||||
|                     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])) { |                 $cfg[$key] = $val; | ||||||
|                     return null; |             } | ||||||
|                 } |  | ||||||
|  |  | ||||||
|                 $cfg = []; |             $this->config = $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); |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         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,31 +85,34 @@ 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"]; |         $data["sqlCount"] = "none"; | ||||||
|         else |  | ||||||
|             $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(); |         $csrfToken = CsrfUtils::getToken(); | ||||||
|         $data["csrfToken"] = $csrfToken; |         $data["csrfToken"] = $csrfToken; | ||||||
|         $data["csrfField"] = new Html('<input type="hidden" name="csrf-token" value="' . $csrfToken . '">'); |         $data["csrfField"] = new Html('<input type="hidden" name="csrf-token" value="' . $csrfToken . '">'); | ||||||
|  |  | ||||||
|         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); |         return $this->getLatte()->renderToString(__TEMPLATES_DIR . "/$templateName.latte", $data); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user