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. --- .../baseconfig/inc/baseconfig.inc.php | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 modules-available/baseconfig/inc/baseconfig.inc.php (limited to 'modules-available/baseconfig/inc/baseconfig.inc.php') diff --git a/modules-available/baseconfig/inc/baseconfig.inc.php b/modules-available/baseconfig/inc/baseconfig.inc.php new file mode 100644 index 00000000..064e0f89 --- /dev/null +++ b/modules-available/baseconfig/inc/baseconfig.inc.php @@ -0,0 +1,148 @@ +', function($id) { + return [ + 'name' => Dictionary::translate('source-global', true), + 'locationid' => 0, + ]; + }); + $res = Database::simpleQuery('SELECT setting, value, displayvalue 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, -1); + } + + // Fallback to default values from json files + self::addDefaults($defaults); + + } + + private static function addDefaults($defaults) + { + ConfigHolder::setContext('', function($id) { + return [ + 'name' => Dictionary::translate('source-default', true), + 'locationid' => 0, + ]; + }); + foreach ($defaults as $setting => $value) { + ConfigHolder::add($setting, $value['defaultvalue'], -1000); + } + } + + private static function handleModule($name, $ip, $uuid, $needJsonHook) // Pass ip and uuid instead of global to make them read only + { + if (isset(self::$modulesDone[$name])) + return; + self::$modulesDone[$name] = true; + // Module has getconfig hook + $file = 'modules/' . $name . '/baseconfig/getconfig.inc.php'; + if (!is_file($file)) + return; + // We want only static hooks that have a json config (currently used for displaying inheritance tree) + if ($needJsonHook && !is_file('modules/' . $name . '/baseconfig/hook.json')) + 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) { + self::handleModule($dep, $ip, $uuid, $needJsonHook); + } + ConfigHolder::setContext($name); + (function ($file, $ip, $uuid) { + include_once($file); + })($file, $ip, $uuid); + } + + public static function hasOverride($key) + { + return array_key_exists($key, self::$overrides); + } + + public static function getOverride($key) + { + return self::$overrides[$key]; + } + +} \ No newline at end of file -- cgit v1.2.3-55-g7522