]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/commitdiff
Writing and reading settings from DB file now work (for SQL).
authorBram Matthys <redacted>
Fri, 21 Apr 2023 08:01:06 +0000 (10:01 +0200)
committerBram Matthys <redacted>
Fri, 21 Apr 2023 08:01:47 +0000 (10:01 +0200)
common.php
plugins/sql_auth/SQL/settings.php
plugins/sql_auth/sql_auth.php
settings/install.php

index 55e56a2e578add5f4b0ad50efecdb04711986da5..93431fdf706b73c151f65156c53f28aa877ba810 100644 (file)
@@ -34,7 +34,7 @@ function page_requires_no_config()
        return FALSE;
 }
 
-function read_config()
+function read_config_file()
 {
        GLOBAL $config;
 
@@ -49,12 +49,22 @@ function read_config()
        }
 }
 
+function read_config_db()
+{
+       GLOBAL $config;
+
+       $merge = DbSettings::get();
+       /* DB settings overwrite config.php keys: */
+       $config = array_merge($config, $merge);
+}
+
 function config_is_file_item($name)
 {
        // TODO: move 'unrealircd' and 'plugins' probably ;)
        if (($name == "unrealircd") ||
            ($name == "plugins") ||
-           ($name == "mysql"))
+           ($name == "mysql") ||
+           ($name == "base_url"))
        {
                return true;
        }
@@ -100,6 +110,7 @@ function write_config_file()
         * without having the DB settings read. (And it also
         * serves no purpose)
         */
+       return true;
 }
 
 // XXX: handle unsetting of config items :D - explicit unset function ?
@@ -131,6 +142,7 @@ function write_config($setting = null)
 
        foreach($db_settings as $k=>$v)
        {
+               echo "Writing $k => $v<br>\n";
                $ret = DbSettings::set($k, $v);
                if (!$ret)
                        return $ret;
@@ -140,7 +152,7 @@ function write_config($setting = null)
 }
 
 /* Now read the config, and redirect to install screen if we don't have it */
-if (!read_config())
+if (!read_config_file())
 {
        if (page_requires_no_config())
        {
@@ -156,7 +168,6 @@ if (!read_config())
        }
 }
 
-if (!get_config("base_url")) die("You need to define the base_url in config/config.php");
 require_once "Classes/class-hook.php";
 if (!is_dir(UPATH . "/vendor"))
        die("The vendor/ directory is missing. Most likely the admin forgot to run 'composer install'\n");
@@ -174,6 +185,12 @@ require_once UPATH . "/Classes/class-rpc.php";
 require_once UPATH . "/Classes/class-paneluser.php";
 require_once UPATH . "/plugins.php";
 
+/* Now that plugins are loaded, read config from DB */
+read_config_db();
+
+/* And a check... */
+if (!get_config("base_url")) die("The base_url was not found in your config. Setup went wrong?");
+
 $pages = [
        "Overview"     => "",
        "Users"        => "users",
index c019c08d072abe44174c2fddbf1abeaa55c42528..e01aab9930e6ad4a97bb0994b8f616d1d122d3eb 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 class DbSettings {
-       function get()
+       public static function get()
        {
                $conn = sqlnew();
                $query = "SELECT * FROM " . get_config("mysql::table_prefix") . "settings";
@@ -8,24 +8,24 @@ class DbSettings {
                $list = [];
                while ($row = $result->fetch())
                {
-                       $list[$row['setting_key']] = $row['setting_value'];
+                       $list[$row['setting_key']] = unserialize($row['setting_value']);
                }
                return $list;
        }
-       function set($key, $val) : int
+       public static function set($key, $val) : int
        {
                $conn = sqlnew();
-               $stmt = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "settings WHERE option_name = :name LIMIT 1");
+               $stmt = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "settings WHERE setting_key = :name LIMIT 1");
                $stmt->execute(["name" => $key]);
                if ($stmt->rowCount()) // if it already exists update it
-                       $stmt = $conn->prepare("UPDATE " . get_config("mysql::table_prefix") . "settings SET option_value = :value WHERE option_name = :name");
+                       $stmt = $conn->prepare("UPDATE " . get_config("mysql::table_prefix") . "settings SET setting_value = :value WHERE setting_key = :name");
                        
                else // otherwise create it
-                       $stmt = $conn->prepare("INSERT INTO " . get_config("mysql::table_prefix") . "settings (option_name, option_value) VALUES (:name, :value)");
+                       $stmt = $conn->prepare("INSERT INTO " . get_config("mysql::table_prefix") . "settings (setting_key, setting_value) VALUES (:name, :value)");
 
                // make sure it's there/correct
-               $stmt->execute(["name" => $key, "value" => $val]);
-               $stmt = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "settings WHERE option_name = :name LIMIT 1");
+               $stmt->execute(["name" => $key, "value" => serialize($val)]);
+               $stmt = $conn->prepare("SELECT * FROM " . get_config("mysql::table_prefix") . "settings WHERE setting_key = :name LIMIT 1");
                $stmt->execute(["name" => $key]);
                return $stmt->rowCount(); // return 1 or 0 bool-like int
        }
index 8f48fa34120cdd7a36aee94a6b29f1d6eb817acc..cf8d71c06ec22d06389b17e9e71eb3ea60fe80d2 100644 (file)
@@ -134,10 +134,6 @@ class sql_auth
                if (!empty($c))
                        $conn->query("ALTER TABLE `".get_config("mysql::table_prefix")."user_meta` CHANGE `meta_value` `meta_value` VARCHAR(5000) CHARACTER SET utf8mb3 COLLATE utf8mb3_bin NULL DEFAULT NULL");
 
-
-               new DbSettings();
-               
-
                /* make sure everything went well */
                $tables = ["users", "user_meta", "fail2ban", "settings"];
                $errors = 0; // counter
index f3abec7a110ee141e228cde06866d97d39ab0a3c..7b0596e6a835331b84ce1d74e388e5fe1562005f 100644 (file)
@@ -130,7 +130,7 @@ $writable = (is_writable("../config/")) ? true: false;
                        }
 
                        /* First, write only the config file */
-                       write_file_config();
+                       write_config_file();
 
                        if ($auth_method == "sql_auth")
                                if (!sql_auth::create_tables())