pi hole setup

This commit is contained in:
2026-03-09 02:24:29 +01:00
parent c144d9bcd2
commit 8de05d5552
6 changed files with 138 additions and 7 deletions

View File

@@ -220,42 +220,51 @@ if ($action === 'dashboard') {
$instancePayloads = [];
$statuses = [];
$makeError = function (string $scope, array $result): array {
return [
'scope' => $scope,
'error' => $result['error'] ?? 'error',
'http_code' => $result['http_code'] ?? 0,
'url' => $result['url'] ?? '',
];
};
foreach ($instances as $id => $instance) {
$errors = [];
$summary = $apiRequest($instance, ['summaryRaw' => 1]);
if (!$summary['ok']) {
$errors[] = ['scope' => 'summary', 'error' => $summary['error'] ?? 'error'];
$errors[] = $makeError('summary', $summary);
}
$topItems = $apiRequest($instance, ['topItems' => 50]);
if (!$topItems['ok']) {
$errors[] = ['scope' => 'topItems', 'error' => $topItems['error'] ?? 'error'];
$errors[] = $makeError('topItems', $topItems);
}
$queryTypes = $apiRequest($instance, ['getQueryTypes' => 1]);
if (!$queryTypes['ok']) {
$errors[] = ['scope' => 'queryTypes', 'error' => $queryTypes['error'] ?? 'error'];
$errors[] = $makeError('queryTypes', $queryTypes);
}
$querySources = $apiRequest($instance, ['getQuerySources' => 1]);
if (!$querySources['ok']) {
$errors[] = ['scope' => 'querySources', 'error' => $querySources['error'] ?? 'error'];
$errors[] = $makeError('querySources', $querySources);
}
$forwardDest = $apiRequest($instance, ['getForwardDestinations' => 1]);
if (!$forwardDest['ok']) {
$errors[] = ['scope' => 'forwardDestinations', 'error' => $forwardDest['error'] ?? 'error'];
$errors[] = $makeError('forwardDestinations', $forwardDest);
}
$recentBlocked = $apiRequest($instance, ['recentBlocked' => 30]);
if (!$recentBlocked['ok']) {
$errors[] = ['scope' => 'recentBlocked', 'error' => $recentBlocked['error'] ?? 'error'];
$errors[] = $makeError('recentBlocked', $recentBlocked);
}
$versions = $apiRequest($instance, ['versions' => 1]);
if (!$versions['ok']) {
$errors[] = ['scope' => 'versions', 'error' => $versions['error'] ?? 'error'];
$errors[] = $makeError('versions', $versions);
}
$summaryData = $summary['ok'] ? $summary['data'] : null;
@@ -364,6 +373,55 @@ if ($action === 'dashboard') {
]);
}
if ($action === 'test') {
require_admin();
$target = (string)($payload['instance'] ?? '');
if ($target === '') {
$respond(['ok' => false, 'error' => 'missing_instance'], 400);
}
if (!isset($instances[$target])) {
$respond(['ok' => false, 'error' => 'invalid_instance'], 400);
}
$instance = $instances[$target];
$result = $apiRequest($instance, ['summaryRaw' => 1]);
if ($result['ok']) {
$respond([
'ok' => true,
'status' => 'ok',
'message' => 'Verbindung OK. API antwortet.',
]);
}
$httpCode = (int)($result['http_code'] ?? 0);
$error = (string)($result['error'] ?? 'error');
$status = 'error';
$message = 'Unbekannter Fehler.';
if ($httpCode === 0) {
$status = 'unreachable';
$message = 'Host nicht erreichbar oder kein HTTP-Response.';
} elseif ($httpCode === 401 || $httpCode === 403) {
$status = 'auth';
$message = 'API Token/Passwort falsch oder nicht berechtigt.';
} elseif ($error === 'invalid_json') {
$status = 'invalid';
$message = 'API antwortet nicht mit JSON. URL oder API-Pfad pruefen.';
} else {
$status = 'error';
$message = 'API Fehler: ' . $error . ' (HTTP ' . $httpCode . ')';
}
$respond([
'ok' => false,
'status' => $status,
'message' => $message,
'http_code' => $httpCode,
'error' => $error,
'url' => (string)($result['url'] ?? ''),
]);
}
if ($action === 'disable') {
require_admin();
$minutes = (int)($payload['minutes'] ?? 0);