From f2d08bb991e8670fdafba4d4215139dbad2a4c79 Mon Sep 17 00:00:00 2001 From: ruan <2369127-ruany@users.noreply.gitlab.com> Date: Wed, 19 Jan 2022 10:43:16 +0200 Subject: [PATCH] Move database implementation out of Settings --- check.php | 28 +------- history.php | 2 +- inc/database.php | 112 ++++++++++++++++++++++++++++++++ inc/header.php | 6 +- inc/page.php | 44 ++++++------- inc/settings.php | 120 ++--------------------------------- inc/test/php/EnvSettings.php | 6 +- 7 files changed, 145 insertions(+), 173 deletions(-) create mode 100644 inc/database.php diff --git a/check.php b/check.php index 911ef20..617fb64 100644 --- a/check.php +++ b/check.php @@ -51,34 +51,8 @@ class Check { echo "
"; redirect($page->link($href)); - /* - $table = $page->settings->table['bans']; - - $stmt = $page->conn->prepare("SELECT * FROM $table WHERE (uuid=? AND active=" . Settings::$TRUE . ") LIMIT 1"); - if ($stmt->execute(array($uuid))) { - if (!($row = $stmt->fetch())) { - $this->println("$name is not banned."); - return; - } - $banner = $page->get_banner_name($row); - $reason = $page->clean($row['reason']); - $time = $page->millis_to_date($row['time']); - $until = $page->millis_to_date($row['until']); - - $this->println("$name is banned!"); - $this->println("Banned by: $banner"); - $this->println("Reason: $reason"); - $this->println("Banned on: $time"); - if ($row['until'] > 0) { - $this->println("Banned until: $until"); - } else { - $this->println("Banned permanently."); - } - } - $stmt->closeCursor(); - */ } catch (PDOException $ex) { - Settings::handle_error($page->settings, $ex); + $page->db->handle_error($page->settings, $ex); } } diff --git a/history.php b/history.php index 522da1a..14887ae 100644 --- a/history.php +++ b/history.php @@ -266,5 +266,5 @@ try { $page->print_footer(); } catch (PDOException $ex) { - Settings::handle_error($page->settings, $ex); + $page->db->handle_error($page->settings, $ex); } diff --git a/inc/database.php b/inc/database.php new file mode 100644 index 0000000..1dba82b --- /dev/null +++ b/inc/database.php @@ -0,0 +1,112 @@ +connect($settings, $verify); + } else { + $this->conn = null; + } + } + + function connect(Settings $cfg, $verify = true) { + $this->active_query = ""; + + if ($cfg->driver === "pgsql") { + Database::$TRUE = "B'1'"; + Database::$FALSE = "B'0'"; + } + + if (!$cfg->show_inactive_bans) { + $this->active_query = self::append_query($this->active_query, "active=" . Database::$TRUE); + } + + if (!$cfg->show_silent_bans) { + $this->active_query = self::append_query($this->active_query, "silent=" . Database::$FALSE); + } + + $this->verify = $verify; + $driver = $cfg->driver; + $host = $cfg->host; + $port = $cfg->port; + $database = $cfg->database; + $username = $cfg->username; + $password = $cfg->password; + if ($username === "" && $password === "") { + redirect("error/unconfigured.php"); + } + + $dsn = "$driver:dbname=$database;host=$host;port=$port"; + if ($driver === 'mysql') { + $dsn .= ';charset=utf8'; + } + + $options = array( + PDO::ATTR_TIMEOUT => 5, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_EMULATE_PREPARES => false, + PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", + ); + + try { + $this->conn = new PDO($dsn, $username, $password, $options); + + if (!$cfg->header_show_totals && $verify) { + $st = $this->conn->query("SELECT * FROM " . $cfg->table['config'] . " LIMIT 1;"); + $st->fetch(); + $st->closeCursor(); + } + } catch (PDOException $e) { + $this->handle_error($cfg, $e); + } + if ($driver === 'pgsql') { + $this->conn->exec("SET NAMES 'UTF8';"); + } + } + + static function append_query($existing, $new) { + if ($existing !== "") { + return "$existing AND $new"; + } + return "WHERE $new"; + } + + /** + * @param $cfg Settings + * @param $e Exception + * @throws Exception + */ + function handle_error(Settings $cfg, Exception $e) { + if ($cfg->error_throw) throw $e; + + $message = $e->getMessage(); + if ($cfg->error_pages) { + if (strstr($message, "Access denied for user")) { + if ($cfg->error_reporting) { + redirect("error/access-denied.php?error=" . base64_encode($message)); + } else { + redirect("error/access-denied.php"); + } + } + if (strstr($message, "Base table or view not found:")) { + try { + $st = $this->conn->query("SELECT * FROM " . $cfg->table['bans'] . " LIMIT 1;"); + $st->fetch(); + $st->closeCursor(); + } catch (PDOException $e) { + redirect("error/tables-not-found.php"); + } + redirect("error/outdated-plugin.php"); + } + if (strstr($message, "Unknown column")) { + redirect("error/outdated-plugin.php"); + } + } + if ($cfg->error_reporting) { + die("Database error: $message"); + } + die("Database error"); + } +} diff --git a/inc/header.php b/inc/header.php index 8261784..13b1e65 100644 --- a/inc/header.php +++ b/inc/header.php @@ -47,7 +47,7 @@ if ($page->settings->header_show_totals) { $t_mutes = $t['mutes']; $t_warnings = $t['warnings']; $t_kicks = $t['kicks']; - $active_query = $page->settings->active_query; + $active_query = $page->db->active_query; try { $sql = "SELECT (SELECT COUNT(*) FROM $t_bans $active_query), @@ -55,7 +55,7 @@ if ($page->settings->header_show_totals) { (SELECT COUNT(*) FROM $t_warnings $active_query), (SELECT COUNT(*) FROM $t_kicks $active_query)"; - if ($page->settings->verify) { + if ($page->db->verify) { $sql .= ",(SELECT id FROM " . $t['config'] . " LIMIT 1)"; } $st = $page->conn->query($sql); @@ -69,7 +69,7 @@ if ($page->settings->header_show_totals) { 'kicks.php' => $row[3], ); } catch (PDOException $ex) { - Settings::handle_error($page->settings, $ex); + $page->db->handle_error($page->settings, $ex); } } ?> diff --git a/inc/page.php b/inc/page.php index 76c7ea3..aa72e6d 100644 --- a/inc/page.php +++ b/inc/page.php @@ -6,26 +6,28 @@ class Page { $this->time = microtime(true); ini_set('default_charset', 'utf-8'); require_once './inc/settings.php'; + require_once './inc/database.php'; if (class_exists("EnvSettings")) { - $settings = new EnvSettings($connect); + $cfg = new EnvSettings(); } else { - $settings = new Settings($connect); + $cfg = new Settings(); } - setlocale(LC_ALL, $settings->lang); + setlocale(LC_ALL, $cfg->lang); require_once './lang/en_US.utf8.php'; - require_once './lang/' . $settings->lang . '.php'; - $lang_class = substr($settings->lang, 0, strpos($settings->lang, ".")); // grab "en_US" from "en_US.utf8" + require_once './lang/' . $cfg->lang . '.php'; + $lang_class = substr($cfg->lang, 0, strpos($cfg->lang, ".")); // grab "en_US" from "en_US.utf8" if ($lang_class !== "en_US" && class_exists($lang_class)) { $this->lang = new $lang_class; } else { $this->lang = new en_US(); } + $this->db = new Database($cfg, $connect, !($cfg instanceof EnvSettings)); - $this->formatter = new IntlDateFormatter($settings->lang, IntlDateFormatter::LONG, IntlDateFormatter::NONE, $settings->timezone, IntlDateFormatter::GREGORIAN, $settings->date_format); + $this->formatter = new IntlDateFormatter($cfg->lang, IntlDateFormatter::LONG, IntlDateFormatter::NONE, $cfg->timezone, IntlDateFormatter::GREGORIAN, $cfg->date_format); - $this->conn = $settings->conn; - $this->settings = $settings; + $this->conn = $this->db->conn; + $this->settings = $cfg; $this->uuid_name_cache = array(); $this->name = $name; @@ -67,7 +69,7 @@ class Page { $this->is_index = ((substr($_SERVER['SCRIPT_NAME'], -strlen("index.php"))) === "index.php"); if ($this->is_index) { $this->index_base_path = substr($_SERVER["PHP_SELF"], 0, -strlen("index.php")); - if ($settings->simple_urls) { + if ($cfg->simple_urls) { $keys = array_keys($_GET); if (count($keys) > 0) { @@ -85,10 +87,8 @@ class Page { $page = "1"; if (isset($_GET['page'])) { $page = $_GET['page']; // user input - } else { - if ($argc > 1) { - $page = $this->args[$argc - 2]; - } + } else if ($argc > 1) { + $page = $this->args[$argc - 2]; } if (filter_var($page, FILTER_VALIDATE_INT)) { $this->page = max(0, (int)$page); @@ -119,13 +119,13 @@ class Page { } public function type_info($type) { - $settings = $this->settings; + $cfg = $this->settings; switch ($type) { case "ban": case "bans": return array( "type" => "ban", - "table" => $settings->table['bans'], + "table" => $cfg->table['bans'], "title" => $this->t("title.bans"), "page" => "bans.php", ); @@ -133,7 +133,7 @@ class Page { case "mutes": return array( "type" => "mute", - "table" => $settings->table['mutes'], + "table" => $cfg->table['mutes'], "title" => $this->t("title.mutes"), "page" => "mutes.php", ); @@ -141,7 +141,7 @@ class Page { case "warnings": return array( "type" => "warn", - "table" => $settings->table['warnings'], + "table" => $cfg->table['warnings'], "title" => $this->t("title.warnings"), "page" => "warnings.php", ); @@ -149,7 +149,7 @@ class Page { case "kicks": return array( "type" => "kick", - "table" => $settings->table['kicks'], + "table" => $cfg->table['kicks'], "title" => $this->t("title.kicks"), "page" => "kicks.php", ); @@ -185,7 +185,7 @@ class Page { $select = $this->get_selection($table); // Not user input - $where = $this->where_append($this->name === "kicks" ? "" : $this->settings->active_query); // Not user input + $where = $this->where_append($this->name === "kicks" ? "" : $this->db->active_query); // Not user input $where .= "(uuid <> '#offline#' AND uuid IS NOT NULL)"; $st = $this->conn->prepare("SELECT $select FROM $table $where ORDER BY time DESC LIMIT :limit OFFSET :offset"); @@ -200,7 +200,7 @@ class Page { return $rows; } catch (PDOException $ex) { - Settings::handle_error($this->settings, $ex); + $this->db->handle_error($this->settings, $ex); return array(); } } @@ -432,7 +432,7 @@ class Page { */ function is_uuid($str) { $len = strlen($str); - return $len == 32 || $len == 36; + return ($len == 32 || $len == 36); } function uuid_dashify($str) { @@ -564,7 +564,7 @@ class Page { } if ($total === -1) { - $where = $this->where_append($this->name === "kicks" ? "" : $this->settings->active_query); + $where = $this->where_append($this->name === "kicks" ? "" : $this->db->active_query); $where .= "(uuid <> '#offline#' AND uuid IS NOT NULL)"; $st = $this->conn->query("SELECT COUNT(*) AS count FROM $table $where"); diff --git a/inc/settings.php b/inc/settings.php index 831e615..78008ff 100644 --- a/inc/settings.php +++ b/inc/settings.php @@ -1,9 +1,7 @@ lang = 'en_US.utf8'; @@ -130,128 +128,18 @@ class Settings { ); } - - /*** End of configuration ***/ - - - /** Don't modify anything here unless you know what you're doing **/ - - $this->error_throw = false; - - date_default_timezone_set($this->timezone); + /**** End of configuration ****/ if ($this->error_reporting) { error_reporting(E_ALL); ini_set("display_errors", 1); } - $this->active_query = ""; + $this->error_throw = false; - if ($this->driver === "pgsql") { - Settings::$TRUE = "B'1'"; - Settings::$FALSE = "B'0'"; - } - - if (!$this->show_inactive_bans) { - $this->active_query = self::append_query($this->active_query, "active=" . Settings::$TRUE); - } - - if (!$this->show_silent_bans) { - $this->active_query = self::append_query($this->active_query, "silent=" . Settings::$FALSE); - } - $this->verify = false; + date_default_timezone_set($this->timezone); $this->init_tables(); - - if ($connect) { - $this->connect(); - } else { - $this->conn = null; - } - } - - protected function connect($verify = true) { - $this->verify = $verify; - $driver = $this->driver; - $host = $this->host; - $port = $this->port; - $database = $this->database; - $username = $this->username; - $password = $this->password; - if ($username === "" && $password === "") { - redirect("error/unconfigured.php"); - } - - $dsn = "$driver:dbname=$database;host=$host;port=$port"; - if ($driver === 'mysql') { - $dsn .= ';charset=utf8'; - } - - $options = array( - PDO::ATTR_TIMEOUT => 5, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_EMULATE_PREPARES => false, - PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", - ); - - try { - $this->conn = new PDO($dsn, $username, $password, $options); - - if (!$this->header_show_totals && $verify) { - $st = $this->conn->query("SELECT * FROM " . $this->table['config'] . " LIMIT 1;"); - $st->fetch(); - $st->closeCursor(); - } - } catch (PDOException $e) { - Settings::handle_error($this, $e); - } - if ($driver === 'pgsql') { - $this->conn->exec("SET NAMES 'UTF8';"); - } - } - - static function append_query($existing, $new) { - if ($existing !== "") { - return "$existing AND $new"; - } - return "WHERE $new"; - } - - /** - * @param $settings Settings - * @param $e Exception - * @throws Exception - */ - static function handle_error($settings, Exception $e) { - if ($settings->error_throw) throw $e; - - $message = $e->getMessage(); - if ($settings->error_pages) { - if (strstr($message, "Access denied for user")) { - if ($settings->error_reporting) { - redirect("error/access-denied.php?error=" . base64_encode($message)); - } else { - redirect("error/access-denied.php"); - } - } - if (strstr($message, "Base table or view not found:")) { - try { - $st = $settings->conn->query("SELECT * FROM " . $settings->table['bans'] . " LIMIT 1;"); - $st->fetch(); - $st->closeCursor(); - } catch (PDOException $e) { - redirect("error/tables-not-found.php"); - } - redirect("error/outdated-plugin.php"); - } - if (strstr($message, "Unknown column")) { - redirect("error/outdated-plugin.php"); - } - } - if ($settings->error_reporting) { - die("Database error: $message"); - } - die("Database error"); } protected function init_tables() { diff --git a/inc/test/php/EnvSettings.php b/inc/test/php/EnvSettings.php index 8ffa052..8091c7d 100644 --- a/inc/test/php/EnvSettings.php +++ b/inc/test/php/EnvSettings.php @@ -1,8 +1,8 @@ host = getenv("MYSQL_HOST"); $this->database = getenv("MYSQL_DATABASE"); $this->username = getenv("MYSQL_USERNAME"); @@ -11,7 +11,5 @@ class EnvSettings extends Settings { $this->error_throw = true; $this->init_tables(); - - if ($connect) $this->connect($verify); } }