This commit is contained in:
2025-11-30 03:24:14 +01:00
parent 337f65205d
commit c7e355c1ea
5 changed files with 112 additions and 54 deletions

View File

@@ -0,0 +1,97 @@
<?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);
}

109
api/router/router.v1.php Normal file
View File

@@ -0,0 +1,109 @@
<?php
// /api/router.v1.php
declare(strict_types=1);
/**
* Router für /v1/...
*
* Wird von /api/index.php aufgerufen:
* router_v1_dispatch($segments)
*
* $segments[0] ist dann z.B. "browser.quick.test" oder "quickcheck"
*/
function router_v1_dispatch(array $segments): void
{
if (empty($segments[0])) {
http_response_code(404);
echo json_encode([
'ok' => false,
'error' => 'No endpoint specified for v1',
], JSON_UNESCAPED_UNICODE);
return;
}
$endpoint = $segments[0]; // z.B. "browser.quick.test" oder "quickcheck"
switch ($endpoint) {
case 'quickcheck':
$file = require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/db.php'; // stellt $pdo (PDO) bereit
'/v1/target/quickcheck.php';
$handler = 'quickcheck_handle_request';
break;
case 'browser.quick.test':
$file = require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/db.php'; // stellt $pdo (PDO) bereit
'/v1/result/browser.quick.test.php';
$handler = 'browser_quick_test_handle_request';
break;
default:
http_response_code(404);
echo json_encode([
'ok' => false,
'error' => 'Unknown v1 endpoint',
'endpoint'=> $endpoint,
], JSON_UNESCAPED_UNICODE);
return;
}
if (!file_exists($file)) {
http_response_code(500);
echo json_encode([
'ok' => false,
'error' => 'Endpoint file not found',
'file' => basename($file),
], JSON_UNESCAPED_UNICODE);
return;
}
require_once $file;
if (!function_exists($handler)) {
http_response_code(500);
echo json_encode([
'ok' => false,
'error' => 'Handler not found',
'handler' => $handler,
], JSON_UNESCAPED_UNICODE);
return;
}
try {
$result = $handler();
// Falls der Handler mal kein Array zurückgibt
if (!is_array($result)) {
$result = [
'ok' => false,
'error' => 'Handler did not return array',
'raw' => $result,
'handler'=> $handler,
];
}
// HTTP-Status aus Ergebnis ableiten (optional)
if (isset($result['ok']) && $result['ok'] === false) {
// Bei Fehler eher 400 als 200, außer du willst es anders
if (!http_response_code()) {
http_response_code(400);
}
} else {
if (!http_response_code()) {
http_response_code(200);
}
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
http_response_code(500);
error_log('[usbcheck] router_v1_dispatch error: ' . $e->getMessage());
echo json_encode([
'ok' => false,
'error' => 'Unhandled exception in endpoint',
'debug' => $e->getMessage(), // später ggf. entfernen
], JSON_UNESCAPED_UNICODE);
}
}