summaryrefslogtreecommitdiffstats
path: root/modules-available/debugconfig/api.inc.php
blob: af780d990d3ed597ff57ba73023409e838a0afbf (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
<?php

$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;
}

/**
 * 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
 * db. If a variable is already set, it will not be overridden by the
 * global setting.
 */

$configVars = array();
function handleModule($file, $ip, $uuid) // Pass ip and uuid instead of global to make them read only
{
	global $configVars;
	include_once $file;
}

// 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);
	$mod = Module::get($out[1]);
	if ($mod === false)
		continue;
	$mod->activate();
	foreach ($mod->getDependencies() as $dep) {
		$depFile = 'modules/' . $dep . '/baseconfig/getconfig.inc.php';
		if (file_exists($depFile) && Module::isAvailable($dep)) {
			handleModule($depFile, $ip, $uuid);
		}
	}
	handleModule($file, $ip, $uuid);
}

// Rest is handled by module
$defaults = BaseConfigUtil::getVariables();

// Dump global config from DB
$res = Database::simpleQuery('SELECT setting, value, enabled FROM setting_global');
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
	if (isset($configVars[$row['setting']]) // Already set by a hook above, ignore
			|| !isset($defaults[$row['setting']])) // Setting is not defined in any <module>/baseconfig/settings.json
		continue;
	if ($row['enabled'] != 1) {
		// Setting is disabled
		$configVars[$row['setting']] = false;
	} else {
		$configVars[$row['setting']] = $row['value'];
	}
}

// Fallback to default values from json files
foreach ($defaults as $setting => $value) {
	if (isset($configVars[$setting])) {
		if ($configVars[$setting] === false) {
			unset($configVars[$setting]);
		}
	} else {
		$configVars[$setting] = $value['defaultvalue'];
	}
}

// All done, now output

if (Request::any('save') === 'true') {
	// output AND save to disk: Generate contents
	$lines = '';
	foreach ($configVars as $setting => $value) {
		$lines .= $setting . "='" . escape($value) . "'\n";
	}
	// Save to all the locations
	$data = Property::getVersionCheckInformation();
	if (is_array($data) && isset($data['systems'])) {
		foreach ($data['systems'] as $system) {
			$path = CONFIG_HTTP_DIR . '/' . $system['id'] . '/config';
			if (file_put_contents($path, $lines) > 0) {
				echo "# Saved config to $path\n";
			} else {
				echo "# Error saving config to $path\n";
			}
		}
	}
	// Output to browser
	echo $lines;
} else {
	// Only output to client
	foreach ($configVars as $setting => $value) {
		echo $setting, "='", escape($value), "'\n";
	}
}

// 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');