diff options
author | Simon Rettberg | 2016-02-19 15:01:56 +0100 |
---|---|---|
committer | Simon Rettberg | 2016-02-19 15:01:56 +0100 |
commit | 317db874e7964231ac33b5646cc33b1b400a4f29 (patch) | |
tree | 6a1be1c286070ec9f3fd181ccb8fe4fecf47b2fb /apis | |
parent | [clientlog] Sanity checks for timestamps (diff) | |
download | slx-admin-317db874e7964231ac33b5646cc33b1b400a4f29.tar.gz slx-admin-317db874e7964231ac33b5646cc33b1b400a4f29.tar.xz slx-admin-317db874e7964231ac33b5646cc33b1b400a4f29.zip |
[locations] New module for managing locations
Diffstat (limited to 'apis')
-rw-r--r-- | apis/getconfig.inc.php | 56 | ||||
-rw-r--r-- | apis/update.inc.php | 38 |
2 files changed, 90 insertions, 4 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'); diff --git a/apis/update.inc.php b/apis/update.inc.php index a7212961..0c91ab2d 100644 --- a/apis/update.inc.php +++ b/apis/update.inc.php @@ -52,6 +52,7 @@ if ($list === false) { Message::addSuccess('db-update-done'); if (tableExists('eventlog')) EventLog::info("Database updated to version $currentVersion"); +DefaultData::populate(); Util::redirect('index.php?do=Main'); //////////////// @@ -256,7 +257,7 @@ function update_10() } Database::exec("CREATE TABLE IF NOT EXISTS `machine` ( `machineuuid` char(36) CHARACTER SET ascii NOT NULL, - `roomid` int(10) unsigned DEFAULT NULL, + `locationid` int(11) DEFAULT NULL, `macaddr` char(17) CHARACTER SET ascii NOT NULL, `clientip` varchar(45) CHARACTER SET ascii NOT NULL, `firstseen` int(10) unsigned NOT NULL, @@ -281,10 +282,43 @@ function update_10() KEY `mbram` (`mbram`), KEY `kvmstate` (`kvmstate`), KEY `id44mb` (`id44mb`), - KEY `roomid` (`roomid`), + KEY `locationid` (`locationid`), KEY `lastseen` (`lastseen`), KEY `cpumodel` (`cpumodel`), KEY `systemmodel` (`systemmodel`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"); return true; } + +function update_11() +{ + if (tableHasColumn('machine', 'roomid')) { + Database::exec("ALTER TABLE `machine` CHANGE `roomid` `locationid` INT(11) DEFAULT NULL"); + } + Database::exec("CREATE TABLE IF NOT EXISTS `setting_location` ( + `locationid` int(11) NOT NULL, + `setting` varchar(28) NOT NULL, + `value` text NOT NULL, + `displayvalue` text NOT NULL, + PRIMARY KEY (`locationid`,`setting`), + KEY `setting` (`setting`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8"); + Database::exec("CREATE TABLE IF NOT EXISTS `location` ( + `locationid` int(11) NOT NULL AUTO_INCREMENT, + `parentlocationid` int(11) NOT NULL, + `locationname` varchar(100) NOT NULL, + PRIMARY KEY (`locationid`), + KEY `locationname` (`locationname`), + KEY `parentlocationid` (`parentlocationid`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + Database::exec("CREATE TABLE IF NOT EXISTS `subnet` ( + `subnetid` int(11) NOT NULL AUTO_INCREMENT, + `startaddr` decimal(39,0) unsigned NOT NULL, + `endaddr` decimal(39,0) unsigned NOT NULL, + `locationid` int(11) NOT NULL, + PRIMARY KEY (`subnetid`), + KEY `startaddr` (`startaddr`,`endaddr`), + KEY `locationid` (`locationid`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + return true; +}
\ No newline at end of file |