summaryrefslogtreecommitdiffstats
path: root/apis/getconfig.inc.php
diff options
context:
space:
mode:
authorSimon Rettberg2016-02-19 15:01:56 +0100
committerSimon Rettberg2016-02-19 15:01:56 +0100
commit317db874e7964231ac33b5646cc33b1b400a4f29 (patch)
tree6a1be1c286070ec9f3fd181ccb8fe4fecf47b2fb /apis/getconfig.inc.php
parent[clientlog] Sanity checks for timestamps (diff)
downloadslx-admin-317db874e7964231ac33b5646cc33b1b400a4f29.tar.gz
slx-admin-317db874e7964231ac33b5646cc33b1b400a4f29.tar.xz
slx-admin-317db874e7964231ac33b5646cc33b1b400a4f29.zip
[locations] New module for managing locations
Diffstat (limited to 'apis/getconfig.inc.php')
-rw-r--r--apis/getconfig.inc.php56
1 files changed, 54 insertions, 2 deletions
diff --git a/apis/getconfig.inc.php b/apis/getconfig.inc.php
index 5d5dbca8..2447d622 100644
--- a/apis/getconfig.inc.php
+++ b/apis/getconfig.inc.php
@@ -1,5 +1,10 @@
<?php
+$ip = $_SERVER['REMOTE_ADDR'];
+if (substr($ip, 0, 7) === '::ffff:') {
+ $ip = substr($ip, 7);
+}
+
/**
* Escape given string so it is a valid string in sh that can be surrounded
* by single quotes ('). This basically turns _'_ into _'"'"'_
@@ -12,13 +17,59 @@ function escape($string)
return str_replace("'", "'\"'\"'", $string);
}
+// Location handling: figure out location
+$locationId = false; // TODO: machine specific mapping
+if ($locationId === false) {
+ // Fallback to subnets
+ $locationId = Location::getFromIp($ip);
+}
+$matchingLocations = array();
+if ($locationId !== false) {
+ // Get all parents
+ settype($locationId, 'integer');
+ $locations = Location::getLocationsAssoc();
+ $find = $locationId;
+ while (isset($locations[$find]) && !in_array($find, $matchingLocations)) {
+ $matchingLocations[] = $find;
+ $find = (int)$locations[$find]['parentlocationid'];
+ }
+ echo "SLX_LOCATIONS='" . escape(implode(' ', $matchingLocations)) . "'\n";
+}
+
+$configVars = array();
+// Query location specific settings (from bottom to top)
+if (!empty($matchingLocations)) {
+ // First get all settings for all locations we're in
+ $list = implode(',', $matchingLocations);
+ $res = Database::simpleQuery("SELECT locationid, setting, value FROM setting_location WHERE locationid IN ($list)");
+ $tmp = array();
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ $tmp[(int)$row['locationid']][$row['setting']] = $row['value'];
+ }
+ // $matchingLocations contains the location ids sorted from closest to furthest, so we use it to make sure the order
+ // in which they override is correct (closest setting wins, e.g. room setting beats department setting)
+ foreach ($matchingLocations as $lid) {
+ if (!isset($tmp[$lid])) continue;
+ foreach ($tmp[$lid] as $setting => $value) {
+ if (!isset($configVars[$setting])) {
+ $configVars[$setting] = $value;
+ }
+ }
+ }
+ unset($tmp);
+}
+
// Dump config from DB
$res = Database::simpleQuery('SELECT setting.setting, setting.defaultvalue, tbl.value
FROM setting
LEFT JOIN setting_global AS tbl USING (setting)
ORDER BY setting ASC'); // TODO: Add setting groups and sort order
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- if (is_null($row['value'])) $row['value'] = $row['defaultvalue'];
+ if (isset($configVars[$row['setting']])) {
+ $row['value'] = $configVars[$row['setting']];
+ } elseif (is_null($row['value'])) {
+ $row['value'] = $row['defaultvalue'];
+ }
echo $row['setting'] . "='" . escape($row['value']) . "'\n";
}
@@ -47,6 +98,7 @@ if (is_array($vmstore)) {
}
}
+
// 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'); \ No newline at end of file
+if (file_exists('client_config_additional.php')) @include('client_config_additional.php');