diff --git a/public/index.php b/public/index.php index 620737b..6086a9a 100644 --- a/public/index.php +++ b/public/index.php @@ -8,6 +8,7 @@ require_once __DIR__ . '/../src/Modules/License/KeyGen.php'; require_once __DIR__ . '/../src/Modules/License/LicenseService.php'; require_once __DIR__ . '/../src/Modules/Watchdog/MonitorRepo.php'; require_once __DIR__ . '/../src/Modules/Watchdog/EventLog.php'; +require_once __DIR__ . '/../src/Modules/Watchdog/TokenManager.php'; require_once __DIR__ . '/../src/Modules/UpdateService/UpdateManager.php'; use Deploymentcenter\Core\Db; @@ -15,6 +16,7 @@ use Deploymentcenter\Core\Auth; use Deploymentcenter\Modules\License\KeyGen; use Deploymentcenter\Modules\Watchdog\MonitorRepo; use Deploymentcenter\Modules\Watchdog\EventLog; +use Deploymentcenter\Modules\Watchdog\TokenManager; use Deploymentcenter\Modules\UpdateService\UpdateManager; Auth::requireLogin(); @@ -22,10 +24,10 @@ Auth::requireLogin(); $config = require __DIR__ . '/../config/config.php'; $pdo = Db::init($config); -// Handle POST actions (Create Product, Generate License, Revoke License, Create Release, etc.) $msg = null; $msgType = 'success'; +// Handle POST actions if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; @@ -40,7 +42,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $stmt = $pdo->prepare('INSERT INTO license_products (slug, name, default_cache_ttl_hours, notes) VALUES (:s, :n, :t, :notes)'); $stmt->execute([':s' => $slug, ':n' => $name, ':t' => $ttl, ':notes' => $notes]); - $msg = "Produkt '{$name}' wurde erfolgreich erstellt."; + $msg = "Produkt '{$name}' (Slug: {$slug}) wurde erfolgreich angelegt."; } catch (Throwable $e) { $msg = "Fehler beim Erstellen des Produkts: " . $e->getMessage(); $msgType = 'danger'; @@ -73,7 +75,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { ':exp' => $expiresAt, ':notes' => $notes ]); - $msg = "Lizenzschlüssel erfolgreich generiert: {$licenseKey}"; + + // Audit log + $pdo->prepare('INSERT INTO license_audit_log (actor, action, details) VALUES ("admin", "license.create", :d)') + ->execute([':d' => json_encode(['key' => $licenseKey, 'customer' => $customerName])]); + + $msg = "Lizenzschlüssel erfolgreich generiert: {$licenseKey}"; } catch (Throwable $e) { $msg = "Fehler bei Generierung: " . $e->getMessage(); $msgType = 'danger'; @@ -87,10 +94,57 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($licId > 0) { $stmt = $pdo->prepare('UPDATE license_licenses SET status = "revoked" WHERE id = :id'); $stmt->execute([':id' => $licId]); + + $pdo->prepare('INSERT INTO license_audit_log (actor, action, details) VALUES ("admin", "license.revoke", :d)') + ->execute([':d' => json_encode(['license_id' => $licId])]); + $msg = "Lizenz wurde widerrufen."; } } + // Block / Unblock Hardware Activation + if ($action === 'toggle_block_activation') { + $actId = (int)($_POST['activation_id'] ?? 0); + $block = (int)($_POST['block_state'] ?? 0); + if ($actId > 0) { + $stmt = $pdo->prepare('UPDATE license_activations SET is_blocked = :b WHERE id = :id'); + $stmt->execute([':b' => $block, ':id' => $actId]); + $msg = $block ? "Hardware-Aktivierung wurde gesperrt." : "Hardware-Aktivierung wurde entsperrt."; + } + } + + // Extend License Expiration + if ($action === 'extend_license') { + $licId = (int)($_POST['license_id'] ?? 0); + $newExp = !empty($_POST['new_expires_at']) ? $_POST['new_expires_at'] . ' 23:59:59' : null; + if ($licId > 0) { + $stmt = $pdo->prepare('UPDATE license_licenses SET expires_at = :exp, status = "active" WHERE id = :id'); + $stmt->execute([':exp' => $newExp, ':id' => $licId]); + $msg = "Ablaufdatum der Lizenz wurde aktualisiert."; + } + } + + // Create Agent Token (Watchdog) + if ($action === 'create_agent_token') { + $source = trim($_POST['token_source'] ?? ''); + $name = trim($_POST['token_name'] ?? ''); + if ($name) { + $tokMgr = new TokenManager($pdo); + $res = $tokMgr->createToken($source, $name); + $msg = "Agent-Token für '{$name}' generiert: {$res['raw_token']} (Bitte sicher aufbewahren!)"; + } + } + + // Revoke Agent Token + if ($action === 'revoke_agent_token') { + $tokId = trim($_POST['token_id'] ?? ''); + if ($tokId) { + $stmt = $pdo->prepare('UPDATE watchdog_agent_tokens SET revoked = 1 WHERE token_id = :id'); + $stmt->execute([':id' => $tokId]); + $msg = "Agent-Token wurde widerrufen."; + } + } + // Add Update Release if ($action === 'add_release') { $productSlug = trim($_POST['product_slug'] ?? ''); @@ -112,26 +166,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } } -// Fetch stats & data -$productsCount = (int)$pdo->query('SELECT COUNT(*) FROM license_products')->fetchColumn(); -$licensesCount = (int)$pdo->query('SELECT COUNT(*) FROM license_licenses')->fetchColumn(); -$activationsCount = (int)$pdo->query('SELECT COUNT(*) FROM license_activations')->fetchColumn(); - -$monitorRepo = new MonitorRepo($pdo); -$monitors = $monitorRepo->getAllMonitors(); - -$monitorsUp = 0; -$monitorsWarning = 0; -$monitorsDown = 0; -foreach ($monitors as $m) { - if ($m['state'] === 'up') $monitorsUp++; - elseif ($m['state'] === 'warning') $monitorsWarning++; - else $monitorsDown++; -} - -$eventLog = new EventLog($pdo); -$recentEvents = $eventLog->getRecentEvents(15); - +// Fetch all Data $products = $pdo->query('SELECT * FROM license_products ORDER BY name ASC')->fetchAll(); $licenses = $pdo->query(' SELECT l.*, p.name as product_name, p.slug as product_slug, @@ -141,458 +176,1125 @@ $licenses = $pdo->query(' ORDER BY l.created_at DESC ')->fetchAll(); +$activations = $pdo->query(' + SELECT a.*, l.license_key, p.slug as product_slug, p.name as product_name + FROM license_activations a + JOIN license_licenses l ON a.license_id = l.id + JOIN license_products p ON l.product_id = p.id + ORDER BY a.last_seen DESC +')->fetchAll(); + +$auditLogs = $pdo->query('SELECT * FROM license_audit_log ORDER BY created_at DESC LIMIT 100')->fetchAll(); + +$monitorRepo = new MonitorRepo($pdo); +$monitors = $monitorRepo->getAllMonitors(); + +$monitorsUp = 0; $monitorsWarning = 0; $monitorsDown = 0; +foreach ($monitors as $m) { + if ($m['state'] === 'up') $monitorsUp++; + elseif ($m['state'] === 'warning') $monitorsWarning++; + else $monitorsDown++; +} + +$eventLog = new EventLog($pdo); +$recentEvents = $eventLog->getRecentEvents(100); + +$agentTokens = $pdo->query('SELECT * FROM watchdog_agent_tokens ORDER BY created_at_utc DESC')->fetchAll(); + $updateMgr = new UpdateManager($pdo); $releases = $updateMgr->getReleases(); + +$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; +$host = $_SERVER['HTTP_HOST'] ?? 'dc.mhdf.de'; +$baseUrl = $protocol . '://' . $host; ?>
-