From 50d28a0ad8dcf5d9fe697278b6ea05aa00f8fb87 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 5 Nov 2019 18:01:37 +0100 Subject: [baseconfig] Overhaul hook system This enables us to finally properly show the inheritance flow of all the config variables when editing the baseconfig for a certain location or machine. --- modules-available/baseconfig/api.inc.php | 175 ++----------------------------- 1 file changed, 8 insertions(+), 167 deletions(-) (limited to 'modules-available/baseconfig/api.inc.php') diff --git a/modules-available/baseconfig/api.inc.php b/modules-available/baseconfig/api.inc.php index aab03b5a..350c6173 100644 --- a/modules-available/baseconfig/api.inc.php +++ b/modules-available/baseconfig/api.inc.php @@ -1,125 +1,5 @@ $value) { - self::add($key, $value, $prio); - } - } - - public static function add($key, $value, $prio = 0) - { - if (!isset(self::$config[$key])) { - self::$config[$key] = []; - } - $new = [ - 'prio' => $prio, - 'value' => $value, - 'context' => self::$context, - ]; - if (empty(self::$config[$key]) || self::$config[$key][0]['prio'] > $prio) { - // Existing is higher, append new one - array_push(self::$config[$key], $new); - } else { - // New one has highest prio or matches existing, put in front - array_unshift(self::$config[$key], $new); - } - } - - public static function get($key) - { - if (!isset(self::$config[$key])) - return false; - return self::$config[$key][0]['value']; - } - - /** - * @param callable $func - */ - public static function addPostHook($func) - { - self::$postHooks[] = array('context' => self::$context, 'function' => $func); - } - - public static function applyPostHooks() - { - foreach (self::$postHooks as $hook) { - self::$context = $hook['context'] . ':post'; - $hook['function'](); - } - self::$postHooks = []; - } - - public static function getConfig() - { - self::applyPostHooks(); - $ret = []; - foreach (self::$config as $key => $list) { - if ($list[0]['value'] === false) - continue; - $ret[$key] = $list[0]['value']; - } - return $ret; - } - - public static function outputConfig() - { - self::applyPostHooks(); - foreach (self::$config as $key => $list) { - echo str_pad('# ' . $key . ' ', 35, '#', STR_PAD_BOTH), "\n"; - foreach ($list as $pos => $item) { - if ($pos != 0 || $item['value'] === false) { - echo '# (', $item['context'], ':', $item['prio'], ')'; - if ($pos == 0) { - echo " \n"; - } else { - echo " \n"; - } - } else { - echo $key, "='", escape($item['value']), "'\n"; - echo '# (', $item['context'], ':', $item['prio'], ") \n"; - } - } - } - } - -} - -/** - * Escape given string so it is a valid string in sh that can be surrounded - * by single quotes ('). This basically turns _'_ into _'"'"'_ - * - * @param string $string input - * @return string escaped sh string - */ -function escape($string) -{ - return str_replace("'", "'\"'\"'", $string); -} - /* * We gather all config variables here. First, let other modules generate * their desired config vars. Afterwards, add the global config vars from @@ -127,58 +7,19 @@ function escape($string) * global setting. */ -function handleModule($name, $ip, $uuid) // Pass ip and uuid instead of global to make them read only -{ - // Module has getconfig hook - $file = 'modules/' . $name . '/baseconfig/getconfig.inc.php'; - if (!is_file($file)) - return; - // Properly registered and can be activated - $mod = Module::get($name); - if ($mod === false) - return; - if (!$mod->activate(1, false)) - return; - // Process dependencies first - foreach ($mod->getDependencies() as $dep) { - handleModule($dep, $ip, $uuid); - } - ConfigHolder::setContext($name); - (function($file, $ip, $uuid) { - include_once($file); - })($file, $ip, $uuid); -} - -// Handle any hooks by other modules first -// other modules should generally only populate $configVars -foreach (glob('modules/*/baseconfig/getconfig.inc.php') as $file) { - preg_match('#^modules/([^/]+)/#', $file, $out); - ConfigHolder::setContext($out[1]); - handleModule($out[1], $ip, $uuid); -} - -// Rest is handled by module -$defaults = BaseConfigUtil::getVariables(); -// Dump global config from DB -ConfigHolder::setContext(''); -$res = Database::simpleQuery('SELECT setting, value FROM setting_global'); -while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - if (!isset($defaults[$row['setting']])) - continue; // Setting is not defined in any /baseconfig/settings.json - ConfigHolder::add($row['setting'], $row['value'], -1); -} +// Prepare ConfigHolder from request data +BaseConfig::prepareFromRequest(); -// Fallback to default values from json files -ConfigHolder::setContext(''); -foreach ($defaults as $setting => $value) { - ConfigHolder::add($setting, $value['defaultvalue'], -1000); -} +ConfigHolder::add('SLX_NOW', time(), PHP_INT_MAX); // All done, now output -ConfigHolder::add('SLX_NOW', time(), PHP_INT_MAX); +ConfigHolder::applyPostHooks(); ConfigHolder::outputConfig(); // For quick testing or custom extensions: Include external file that should do nothing // more than outputting more key-value-pairs. It's expected in the webroot of slxadmin -if (file_exists('client_config_additional.php')) @include('client_config_additional.php'); +if (file_exists('client_config_additional.php')) { + echo "########## client_config_additional.php:\n"; + @include('client_config_additional.php'); +} -- cgit v1.2.3-55-g7522