POST admin/update_api.php?action=check * POST /admin/update/install -> POST admin/update_api.php?action=install * GET /admin/update/progress -> GET admin/update_api.php?action=progress * POST /admin/update/channel -> POST admin/update_api.php?action=channel * POST /admin/update/migrations -> POST admin/update_api.php?action=migrations * * Alle Endpunkte setzen einen angemeldeten Administrator voraus; alle * schreibenden zusaetzlich ein gueltiges CSRF-Token. */ class UpdateController { /** @var \Database */ private $db; /** @var \Auth */ private $auth; public function __construct(\Database $db, \Auth $auth) { $this->db = $db; $this->auth = $auth; } public function handle(string $action): void { try { switch ($action) { case 'check': $this->requirePost(); $this->json($this->check()); break; case 'install': $this->requirePost(); $this->json($this->install()); break; case 'progress': $this->json($this->progress()); break; case 'channel': $this->requirePost(); $this->json($this->channel()); break; case 'migrations': $this->requirePost(); $this->json($this->migrations()); break; default: http_response_code(404); $this->json(['ok' => false, 'error' => 'Unbekannter Endpunkt.']); } } catch (RetryLaterException $e) { http_response_code(503); header('Retry-After: ' . $e->retryAfter); $this->json(['ok' => false, 'retry' => true, 'retry_after' => $e->retryAfter, 'error' => $e->getMessage()]); } catch (\Throwable $e) { http_response_code(500); $this->json(['ok' => false, 'error' => $e->getMessage()]); } } // ----------------------------------------------------------------- Aktionen private function check(): array { $updater = UpdaterFactory::create($this->db, new AuditLogger($this->db)); $result = $updater->check(); $result['ok'] = true; $result['current_release'] = Version::RELEASE; return $result; } private function install(): array { if (Progress::isRunning()) { return ['ok' => false, 'error' => 'Es laeuft bereits eine Aktualisierung.']; } // Der Ablauf laeuft serverseitig weiter, auch wenn der Browser die // Verbindung kappt - die Oberflaeche fragt nur den Fortschritt ab. @set_time_limit(0); ignore_user_abort(true); $updater = UpdaterFactory::create($this->db, new AuditLogger($this->db)); $result = $updater->installUpdate($_SESSION['user_id'] ?? null); $result['ok'] = $result['success']; return $result; } private function progress(): array { $progress = Progress::get(); return [ 'ok' => true, 'pct' => $progress['pct'], 'step' => $progress['step'], 'text' => $progress['text'], 'running' => Progress::isRunning(), 'maintenance' => Maintenance::isActive(), ]; } private function channel(): array { $channel = (string)($_POST['channel'] ?? ''); if (!isset(Updater::CHANNELS[$channel])) { http_response_code(400); return ['ok' => false, 'error' => 'Unbekannter Kanal.']; } UpdaterFactory::saveChannel($channel); (new AuditLogger($this->db))->log('update_channel_changed', ['channel' => $channel], $_SESSION['user_id'] ?? null); return ['ok' => true, 'channel' => $channel]; } private function migrations(): array { $updater = UpdaterFactory::create($this->db, new AuditLogger($this->db)); $done = $updater->runMigrations(); (new AuditLogger($this->db))->log('update_migrations_run', ['count' => count($done)], $_SESSION['user_id'] ?? null); return [ 'ok' => true, 'applied' => $done, 'count' => count($done), 'status' => $updater->migrationStatus(), ]; } // ------------------------------------------------------------------ Helfer private function requirePost(): void { if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { http_response_code(405); $this->json(['ok' => false, 'error' => 'Nur POST erlaubt.']); exit; } if (!$this->auth->validateCsrfToken($_POST['csrf_token'] ?? '')) { http_response_code(403); $this->json(['ok' => false, 'error' => 'Sicherheitstoken ungueltig. Seite neu laden.']); exit; } } private function json(array $data): void { header('Content-Type: application/json; charset=utf-8'); header('Cache-Control: no-store'); echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); } }