From 3a92964172a4a346bbbd7203d01b35df1c7d34ba Mon Sep 17 00:00:00 2001 From: NorHei Date: Tue, 6 Oct 2015 20:33:06 +0200 Subject: [PATCH] Installation of new settings class As i tried to make a admin tool for setting of maintainance mode, i realized that there is nothing for easy management of WBs system settings. You still have to do it manually. But i remembered that some module where using some set_settings() get_settings() functions(e.g. jsadmin). So i did a deep thought an decided that it would be a good idea that the whole settings management would be a lot more fun it we got some dedicated functions to handle this. If we use constants we schould do it in a good and clean way, so i put a few lines together and would like to know if you got some more ideas i can put into this. // create or edit a setting Settings::Set("wb_new_setting","the value"); // using a setting if (WB_NEW_SETTING =="the value") echo "Horay"; // there is a get function but this is mostly used internal $myValue= Settings::Get ("wb_new_setting"); // deleting Settings::Delete("wb_new_setting"); // if used in modules please prepend (shortened)module name to avoid collisions Settings::Set("wysi_new_setting","another value"); Please keep in mind that WB stores settings always as strings. --- wbce/framework/class.settings.php | 137 ++++++++++++++++++++++++++++++ wbce/framework/initialize.php | 29 ++----- 2 files changed, 144 insertions(+), 22 deletions(-) create mode 100644 wbce/framework/class.settings.php diff --git a/wbce/framework/class.settings.php b/wbce/framework/class.settings.php new file mode 100644 index 000000000..06022169f --- /dev/null +++ b/wbce/framework/class.settings.php @@ -0,0 +1,137 @@ + "; + + // better go for savety + $name = $database->escapeString($name); + $value = $database->escapeString($value); + + // If its a boolean there was nothing set. + if($prev_value === false) { + $sql="INSERT INTO ".TABLE_PREFIX."settings (name,value) VALUES ('$name','$value')"; + $database->query($sql); + } else { + $sql="UPDATE ".TABLE_PREFIX."settings SET value = '$value' WHERE name = '$name'"; + //echo htmlentities( $sql); + $database->query($sql); + } + return false; + } + + + // only used as helper, as all setings converted to constants + public static function Get($name, $default= false) { + global $database; + + $sql="SELECT value FROM ".TABLE_PREFIX."settings WHERE name = '".$name."'"; + $rs = $database->query($sql); + + if($row = $rs->fetchRow()) return $row['value']; + + return $default; + } + + public static function Del($name) { + global $database; + + // is it set ? + $prev_value = Settings::get($name); + + if($prev_value === false) { + return "Setting not set"; + } + else { + $sql="DELETE FROM ".TABLE_PREFIX."settings WHERE name = '$name'"; + $database->query($sql); + } + return false; + } + + + // function to setup constants in init + public static function Setup() { + global $database; + + // Get website settings (title, keywords, description, header, footer...) + $sql = 'SELECT `name`, `value` FROM `' . TABLE_PREFIX . 'settings`'; + if (($get_settings = $database->query($sql))) { + $x = 0; //counter for debug + + while ($setting = $get_settings->fetchRow(MYSQL_ASSOC)) { + $setting_name = strtoupper($setting['name']); + $setting_value = $setting['value']; + if ($setting_value == 'false') { + $setting_value = false; + } + if ($setting_value == 'true') { + $setting_value = true; + } + if (!defined($setting_name)) //already set manually in config ? + define($setting_name, $setting_value); + $x++; + } + } + else { + die($database->get_error()); + } + return false; + } + + + // a function to display all settings in DB same as setup but returns a nice list + public static function Info() { + global $database; + + $sql = 'SELECT `name`, `value` FROM `' . TABLE_PREFIX . 'settings`'; + if (($get_settings = $database->query($sql))) { + $out = "

All Settings in DB

"; + + while ($setting = $get_settings->fetchRow(MYSQL_ASSOC)) { + $setting_name = strtoupper($setting['name']); + $setting_value = $setting['value']; + $setting_value = htmlentities($setting_value); + + $out.= "$setting_name = $setting_value
"; + } + } + else { + die($database->get_error()); + } + return $out; + } +} diff --git a/wbce/framework/initialize.php b/wbce/framework/initialize.php index 3f2dea389..bcaf3d474 100644 --- a/wbce/framework/initialize.php +++ b/wbce/framework/initialize.php @@ -98,28 +98,13 @@ function makePhExp($sList) @ini_set("magic_quotes_runtime", 0); // Disable magic_quotes_runtime } - // Get website settings (title, keywords, description, header, and footer) - $sql = 'SELECT `name`, `value` FROM `' . TABLE_PREFIX . 'settings`'; - if (($get_settings = $database->query($sql))) { - $x = 0; - while ($setting = $get_settings->fetchRow(MYSQL_ASSOC)) { - $setting_name = strtoupper($setting['name']); - $setting_value = $setting['value']; - if ($setting_value == 'false') { - $setting_value = false; - } - if ($setting_value == 'true') { - $setting_value = true; - } - @define($setting_name, $setting_value); - $x++; - } - } else { - die($database->get_error()); - } - if (!$x) { - throw new RuntimeException('no settings found'); - } + // load settings class + require_once WB_PATH . '/framework/class.settings.php'; + + // get all settings + Settings::Setup (); + + @define('DO_NOT_TRACK', (isset($_SERVER['HTTP_DNT']))); $string_file_mode = STRING_FILE_MODE; @define('OCTAL_FILE_MODE', (int) octdec($string_file_mode));