summaryrefslogtreecommitdiffstats
path: root/modules-available/baseconfig/inc
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/baseconfig/inc')
-rw-r--r--modules-available/baseconfig/inc/baseconfig.inc.php148
-rw-r--r--modules-available/baseconfig/inc/configholder.inc.php137
2 files changed, 285 insertions, 0 deletions
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 @@
+<?php
+
+class BaseConfig
+{
+
+ private static $modulesDone = [];
+
+ /**
+ * @var array key-value-pairs of override vars, can be accessed by hooks
+ */
+ private static $overrides = [];
+
+ /**
+ * Fill the ConfigHolder with values from various hooks, while taking
+ * into account UUID and IP-address of the client making the current
+ * HTTP request.
+ */
+ public static function prepareFromRequest()
+ {
+ $ip = $_SERVER['REMOTE_ADDR'];
+ if (substr($ip, 0, 7) === '::ffff:') {
+ $ip = substr($ip, 7);
+ }
+ $uuid = Request::any('uuid', false, 'string');
+ if ($uuid !== false && strlen($uuid) !== 36) {
+ $uuid = false;
+ }
+ // 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]);
+ self::handleModule($out[1], $ip, $uuid, false);
+ }
+ self::commonBase();
+ }
+
+ /**
+ * Fill the ConfigHolder with data from various hooks that supply
+ * static overrides for config variables. The overrides can be used
+ * to make the hooks behave in certain ways, by setting specific values like
+ * 'locationid'
+ * @param array $overrides key value pairs of overrides
+ */
+ public static function prepareWithOverrides($overrides)
+ {
+ self::$overrides = $overrides;
+ $ip = $uuid = false;
+ if (self::hasOverride('ip')) {
+ $ip = self::getOverride('ip');
+ }
+ if (self::hasOverride('uuid')) {
+ $uuid = self::getOverride('uuid');
+ }
+ // Handle only static hooks that don't dynamically generate output
+ foreach (glob('modules/*/baseconfig/hook.json') as $file) {
+ preg_match('#^modules/([^/]+)/#', $file, $out);
+ ConfigHolder::setContext($out[1]);
+ self::handleModule($out[1], $ip, $uuid, true);
+ }
+ self::commonBase();
+ }
+
+ /**
+ * Just fill the ConfigHolder with the defaults from all the json files
+ * that define config variables.
+ */
+ public static function prepareDefaults()
+ {
+ $defaults = BaseConfigUtil::getVariables();
+ self::addDefaults($defaults);
+ }
+
+ private static function commonBase()
+ {
+ $defaults = BaseConfigUtil::getVariables();
+
+ // Dump global config from DB
+ ConfigHolder::setContext('<global>', 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 <module>/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('<default>', 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
diff --git a/modules-available/baseconfig/inc/configholder.inc.php b/modules-available/baseconfig/inc/configholder.inc.php
new file mode 100644
index 00000000..e5856128
--- /dev/null
+++ b/modules-available/baseconfig/inc/configholder.inc.php
@@ -0,0 +1,137 @@
+<?php
+
+class ConfigHolder
+{
+ private static $config = [];
+
+ private static $context = '';
+
+ private static $postHooks = [];
+
+ public static function setContext($id, $resolver = false)
+ {
+ $tmp = ['id' => $id, 'resolver' => $resolver];
+ self::$context =& $tmp;
+ }
+
+ /**
+ * @param string $key config variable name
+ * @param false|string|array $value false to unset, string value, or array with keys value and displayvalue
+ * @param int $prio priority of this value, in case the same key gets set multiple times
+ */
+ public static function add($key, $value, $prio = 0)
+ {
+ if (!isset(self::$config[$key])) {
+ self::$config[$key] = [];
+ }
+ $new = [
+ 'prio' => $prio,
+ 'context' => &self::$context,
+ ];
+ if (is_array($value)) {
+ $new['value'] = $value['value'];
+ $new['displayvalue'] = $value['displayvalue'];
+ } else {
+ $new['value'] = $value;
+ }
+ 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) {
+ $newContext = $hook['context'];
+ $newContext['post'] = true;
+ self::$context =& $newContext;
+ $hook['function']();
+ }
+ self::$postHooks = [];
+ }
+
+ public static function getRecursiveConfig($prettyPrint = true)
+ {
+ $ret = [];
+ foreach (self::$config as $key => $list) {
+ $last = false;
+ foreach ($list as $entry) {
+ if ($last !== false && $last['context']['id'] === '<global>'
+ && $entry['context']['id'] === '<default>' && $last['value'] === $entry['value'])
+ continue;
+ $cb =& $entry['context']['resolver'];
+ $valueKey = 'value';
+ if ($prettyPrint && is_callable($cb)) {
+ $data = $cb($entry['context']['id']);
+ $name = $data['name'];
+ if (isset($data['locationid']) && isset($entry['displayvalue'])
+ && User::hasPermission('.baseconfig.view', $data['locationid'])) {
+ $valueKey = 'displayvalue';
+ }
+ } else {
+ $name = $entry['context']['id'];
+ }
+ $ret[$key][] = ['name' => $name, 'value' => $entry[$valueKey]];
+ $last = $entry;
+ }
+ }
+ return $ret;
+ }
+
+ public static function outputConfig()
+ {
+ foreach (self::$config as $key => $list) {
+ echo str_pad('# ' . $key . ' ', 35, '#', STR_PAD_BOTH), "\n";
+ foreach ($list as $pos => $item) {
+ $ctx = $item['context']['id'];
+ if (isset($item['context']['post'])) {
+ $ctx .= '[post-hook]';
+ }
+ $ctx .= ' :: ' . $item['prio'];
+ if ($pos != 0 || $item['value'] === false) {
+ echo '# (', $ctx, ')';
+ if ($pos == 0) {
+ echo " <disabled this setting>\n";
+ } else {
+ echo " <overridden>\n";
+ }
+ } else {
+ echo $key, "='", self::escape($item['value']), "'\n";
+ echo '# (', $ctx, ") <active>\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
+ */
+ private static function escape($string)
+ {
+ return str_replace("'", "'\"'\"'", $string);
+ }
+
+}