summaryrefslogtreecommitdiffstats
path: root/modules-available/baseconfig/inc/configholder.inc.php
blob: 224f2aabb44ecb6b9c0cd37919a7a94f54d0d9bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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(["'", "\n", "\r"], ["'\"'\"'", ' ', ' '], $string);
	}

}