Show host names (if known) on the Clients management page.

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2019-12-19 16:56:44 +00:00
parent ae08443fa5
commit 050021827f
4 changed files with 67 additions and 24 deletions

View File

@@ -149,17 +149,39 @@ if ($_POST['action'] == 'get_groups') {
} elseif ($_POST['action'] == 'get_clients') {
// List all available groups
try {
$QUERYDB = getQueriesDBFilename();
$FTLdb = SQLite3_connect($QUERYDB);
$query = $db->query('SELECT * FROM client;');
if (!$query) {
throw new Exception('Error while querying gravity\'s client table: ' . $db->lastErrorMsg());
}
$data = array();
while (($res = $query->fetchArray(SQLITE3_ASSOC)) !== false) {
$group_query = $db->query('SELECT group_id FROM client_by_group WHERE client_id = ' . $res['id'] . ';');
if (!$group_query) {
throw new Exception('Error while querying gravity\'s client_by_group table: ' . $db->lastErrorMsg());
}
$stmt = $FTLdb->prepare('SELECT name FROM network WHERE id = (SELECT network_id FROM network_addresses WHERE ip = :ip);');
if (!$stmt) {
throw new Exception('Error while preparing network table statement: ' . $db->lastErrorMsg());
}
if (!$stmt->bindValue(':ip', $res['ip'], SQLITE3_TEXT)) {
throw new Exception('While binding to network table statement: ' . $db->lastErrorMsg());
}
$result = $stmt->execute();
if (!$result) {
throw new Exception('While executing network table statement: ' . $db->lastErrorMsg());
}
// There will always be a result. Unknown host names are NULL
$name_result = $result->fetchArray(SQLITE3_ASSOC);
$res['name'] = $name_result['name'];
$groups = array();
while ($gres = $group_query->fetchArray(SQLITE3_ASSOC)) {
@@ -179,7 +201,7 @@ if ($_POST['action'] == 'get_groups') {
$QUERYDB = getQueriesDBFilename();
$FTLdb = SQLite3_connect($QUERYDB);
$query = $FTLdb->query('SELECT DISTINCT ip FROM network_addresses ORDER BY ip ASC;');
$query = $FTLdb->query('SELECT DISTINCT ip,network.name FROM network_addresses AS name LEFT JOIN network ON network.id = network_id ORDER BY ip ASC;');
if (!$query) {
throw new Exception('Error while querying FTL\'s database: ' . $db->lastErrorMsg());
}
@@ -187,7 +209,7 @@ if ($_POST['action'] == 'get_groups') {
// Loop over results
$ips = array();
while ($res = $query->fetchArray(SQLITE3_ASSOC)) {
array_push($ips, $res['ip']);
$ips[$res['ip']] = $res['name'];
}
$FTLdb->close();
@@ -198,13 +220,12 @@ if ($_POST['action'] == 'get_groups') {
// Loop over results, remove already configured clients
while (($res = $query->fetchArray(SQLITE3_ASSOC)) !== false) {
$idx = array_search($res['ip'], $ips);
if ($idx !== false) {
unset($ips[$idx]);
if (isset($ips[$res['ip']])) {
unset($ips[$res['ip']]);
}
}
echo json_encode(array_values($ips));
echo json_encode($ips);
} catch (\Exception $ex) {
return JSON_error($ex->getMessage());
}