Files
usbcheck.it/api/router/router.internal.php
2025-11-30 03:24:14 +01:00

98 lines
2.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// /api/router.internal.php
declare(strict_types=1);
// *** SICHERHEIT ***
// → Unbedingt User/Pass ändern oder später auf Token/IP-Restriktion umstellen
$validUser = 'usbcheck-internal';
$validPass = 'SwejaFynja050223!';
// Basic-Auth prüfen
if (
!isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) ||
$_SERVER['PHP_AUTH_USER'] !== $validUser ||
$_SERVER['PHP_AUTH_PW'] !== $validPass
) {
header('WWW-Authenticate: Basic realm="USBCheck Internal API"');
http_response_code(401);
echo json_encode([
'ok' => false,
'error' => 'Authentication required',
]);
exit;
}
// Pfad erneut bestimmen
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
$path = rtrim($uri, '/');
// interne Routen
switch ($path) {
// Beispiel: Aggregierte Stats
case '/internal/stats.overview':
internal_stats_overview($pdo);
break;
// Beispiel: Wartung / Cleanup
case '/internal/maintenance.cleanup-tests':
internal_cleanup_tests($pdo);
break;
default:
http_response_code(404);
echo json_encode([
'ok' => false,
'error' => 'Unknown internal endpoint',
'path' => $path,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
break;
}
/**
* Beispiel: einfache Übersicht für Admin-Dashboard
*/
function internal_stats_overview(PDO $pdo): void
{
// alles nur Beispiel du kannst die Queries anpassen
$totalQuicktests = (int)$pdo->query("SELECT COUNT(*) FROM web_quicktests")->fetchColumn();
$lastTestsStmt = $pdo->query("
SELECT id, created_at, ip_address, measured_capacity_bytes
FROM web_quicktests
ORDER BY created_at DESC
LIMIT 10
");
$lastTests = $lastTestsStmt ? $lastTestsStmt->fetchAll(PDO::FETCH_ASSOC) : [];
echo json_encode([
'ok' => true,
'stats' => [
'total_quicktests' => $totalQuicktests,
'last_quicktests' => $lastTests,
],
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
/**
* Beispiel: alte Tests aufräumen (z.B. älter als 90 Tage)
*/
function internal_cleanup_tests(PDO $pdo): void
{
// je nach Schema musst du Feldnamen anpassen hier: created_at
$stmt = $pdo->prepare("
DELETE FROM web_quicktests
WHERE created_at < (NOW() - INTERVAL 90 DAY)
");
$stmt->execute();
$deleted = $stmt->rowCount();
echo json_encode([
'ok' => true,
'deleted' => $deleted,
'note' => 'Tests älter als 90 Tage wurden entfernt (Beispiel-Implementierung).',
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}