Selbst-Updater neu gebaut, Diagnoseseiten abgesichert
Some checks failed
CI / PHP Lint-1 (push) Successful in 43s
CI / PHP Lint (push) Successful in 45s
CI / PHP Lint (pull_request) Successful in 38s
CI / Unit Tests & Static Analysis (push) Failing after 43s
CI / PHP Lint-1 (pull_request) Successful in 40s
CI / Unit Tests & Static Analysis (pull_request) Failing after 41s
Some checks failed
CI / PHP Lint-1 (push) Successful in 43s
CI / PHP Lint (push) Successful in 45s
CI / PHP Lint (pull_request) Successful in 38s
CI / Unit Tests & Static Analysis (push) Failing after 43s
CI / PHP Lint-1 (pull_request) Successful in 40s
CI / Unit Tests & Static Analysis (pull_request) Failing after 41s
Aktualisierung per Klick im Administrationsbereich, ohne Zugang zum Server. Der bisherige Updater lud ein ZIP und entpackte es ungeprüft über die Produktion; das ist vollständig ersetzt. Neu in updater/: - Version: Release-Version als Konstante, installierter Commit-SHA in storage/.version (Datei, nicht Datenbank - der Updater muss den Stand auch bei hängender Datenbank lesen können) - Progress: Fortschrittsdatei mit eigenem Fehlerzustand (pct = -1) und Verfallszeit, damit ein abgestürzter Lauf den nächsten nicht blockiert - Maintenance: Wartungsmarkierung mit Ablauf, die sich beim Prüfen selbst aufräumt - ProxyClient: nur HTTPS, Zertifikatsprüfung an, begrenzte Weiterleitungen, 503 mirror_pending/not_released als Bitte um Geduld - Updater: Prüfsumme gegen X-Content-Sha256, zweistufiger ZIP-Slip-Schutz, geschützte Pfade, atomares Schreiben je Datei, nie löschen - AutoMigrate: zieht Migrationen beim ersten Seitenaufruf nach, nur bei geändertem Fingerabdruck und mit Sperre gegen Parallellauf - guard.php: Wartungs-Wächter für die Einstiegsseiten, JSON für die Schnittstellen; ausgenommen bleiben Update-Seite, Anmeldung und Health-Check - Changelog: zerlegt CHANGELOG.md und rendert Markdown ohne unsichere Links Oberfläche: Seite "Aktualisierung" mit Stand, Kanal, Suche, Fortschritt, Migrationsliste und Versionsgeschichte. Der Datenschutzhinweis steht dauerhaft neben der Schaltfläche, nicht in einem Dialog. Fehlt changelog_preview in der Antwort, bleibt es bei "neue Fassung verfügbar" - kein leerer Kasten, kein erfundener Ersatztext. Commit-Texte werden nirgends angezeigt. Außerdem (Punkt 2 der Liste): - cron_test.php entfernt: ohne jede Anmeldung erreichbar, meldete Datenbankfehler samt Meldung und die Spalten der vouchers-Tabelle - test.php verweigert ohne config.php die Auskunft; bisher lief die Diagnose auf einer frisch entpackten Installation ohne Anmeldung PHPStan prüft jetzt den gesamten updater/-Ordner. 18 neue Tests (93 Tests, 243 Assertions), Ablauf gegen den echten Update-Dienst durchgespielt. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
3a5b95119c
commit
25b8c51362
83 changed files with 2690 additions and 1042 deletions
|
|
@ -2,12 +2,20 @@
|
|||
namespace Updater;
|
||||
|
||||
/**
|
||||
* UpdateController – bedient die Admin-Seite /admin/update sowie deren
|
||||
* AJAX-Actions (check, install, progress, set_channel, migrations).
|
||||
* Bedient die Update-Endpunkte.
|
||||
*
|
||||
* Nutzt die vorhandenen Projekt-Klassen \Database und \Auth per Injection.
|
||||
* Die eigentliche Logik liegt im UpdateManager; dieser Controller ist nur
|
||||
* die duenne Auslieferungs-/Routing-Schicht.
|
||||
* Routing-Anpassung: Dieses Projekt hat keinen Router - die Auslieferung ist
|
||||
* datei-basiert. Die Endpunkte der Vorlage bilden wir deshalb ueber
|
||||
* admin/update_api.php mit dem Parameter `action` ab:
|
||||
*
|
||||
* POST /admin/update/check -> 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
|
||||
{
|
||||
|
|
@ -15,131 +23,143 @@ class UpdateController
|
|||
private $db;
|
||||
/** @var \Auth */
|
||||
private $auth;
|
||||
/** @var UpdateManager */
|
||||
private $manager;
|
||||
|
||||
public function __construct(\Database $db, \Auth $auth)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->auth = $auth;
|
||||
$this->manager = UpdaterFactory::create($db, new AuditLogger($db));
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$action = $_GET['action'] ?? $_POST['action'] ?? '';
|
||||
switch ($action) {
|
||||
case 'check': $this->actionCheck(); break;
|
||||
case 'install': $this->actionInstall(); break;
|
||||
case 'progress': $this->actionProgress(); break;
|
||||
case 'set_channel': $this->actionSetChannel(); break;
|
||||
case 'migrations': $this->actionMigrations(); break;
|
||||
case 'run_migrations': $this->actionRunMigrations(); break;
|
||||
default: $this->renderPage();
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- AJAX-Actions
|
||||
|
||||
private function actionCheck(): void
|
||||
public function handle(string $action): void
|
||||
{
|
||||
try {
|
||||
$this->json($this->manager->checkForUpdates());
|
||||
} catch (\Throwable $e) {
|
||||
$this->json(['error' => $e->getMessage()], 502);
|
||||
}
|
||||
}
|
||||
|
||||
private function actionInstall(): void
|
||||
{
|
||||
if (!$this->auth->validateCsrfToken($_POST['csrf_token'] ?? '')) {
|
||||
$this->json(['error' => 'Ungültiges Sicherheits-Token'], 403);
|
||||
return;
|
||||
}
|
||||
// Session-Lock freigeben, damit parallele Progress-Polls nicht blockieren.
|
||||
if (session_status() === PHP_SESSION_ACTIVE) {
|
||||
session_write_close();
|
||||
}
|
||||
@set_time_limit(300);
|
||||
$userId = $_SESSION['user_id'] ?? null;
|
||||
$result = $this->manager->installUpdate($userId !== null ? (int)$userId : null);
|
||||
$this->json($result, $result['success'] ? 200 : 500);
|
||||
}
|
||||
|
||||
private function actionProgress(): void
|
||||
{
|
||||
// Kein CSRF noetig (read-only). Session-Lock sofort freigeben.
|
||||
if (session_status() === PHP_SESSION_ACTIVE) {
|
||||
session_write_close();
|
||||
}
|
||||
$this->json($this->manager->getProgress());
|
||||
}
|
||||
|
||||
private function actionSetChannel(): void
|
||||
{
|
||||
if (!$this->auth->validateCsrfToken($_POST['csrf_token'] ?? '')) {
|
||||
$this->json(['error' => 'Ungültiges Sicherheits-Token'], 403);
|
||||
return;
|
||||
}
|
||||
$channel = $_POST['channel'] ?? 'stable';
|
||||
UpdaterFactory::saveChannel($channel);
|
||||
$this->json(['success' => true, 'channel' => $channel]);
|
||||
}
|
||||
|
||||
private function actionMigrations(): void
|
||||
{
|
||||
try {
|
||||
$runner = new MigrationRunner(
|
||||
$this->db->getConnection(),
|
||||
__DIR__ . '/migrations',
|
||||
__DIR__ . '/storage'
|
||||
);
|
||||
$this->json(['migrations' => $runner->status()]);
|
||||
} catch (\Throwable $e) {
|
||||
$this->json(['error' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
private function actionRunMigrations(): void
|
||||
{
|
||||
if (!$this->auth->validateCsrfToken($_POST['csrf_token'] ?? '')) {
|
||||
$this->json(['error' => 'Ungültiges Sicherheits-Token'], 403);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
$runner = new MigrationRunner(
|
||||
$this->db->getConnection(),
|
||||
__DIR__ . '/migrations',
|
||||
__DIR__ . '/storage'
|
||||
);
|
||||
$applied = $runner->runPending(true);
|
||||
if ($this->audit) {
|
||||
$this->audit->log('migrations_run', ['applied' => $applied], $_SESSION['user_id'] ?? null);
|
||||
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.']);
|
||||
}
|
||||
$this->json(['success' => true, 'applied' => $applied, 'migrations' => $runner->status()]);
|
||||
} 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) {
|
||||
$this->json(['error' => $e->getMessage()], 500);
|
||||
http_response_code(500);
|
||||
$this->json(['ok' => false, 'error' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- Render
|
||||
// ----------------------------------------------------------------- Aktionen
|
||||
|
||||
private function renderPage(): void
|
||||
private function check(): array
|
||||
{
|
||||
$manager = $this->manager;
|
||||
$auth = $this->auth;
|
||||
$currentSha = $manager->getCurrentVersion();
|
||||
$channel = $manager->getChannel();
|
||||
$csrfToken = $auth->getCsrfToken();
|
||||
require __DIR__ . '/templates/update.php';
|
||||
$updater = UpdaterFactory::create($this->db, new AuditLogger($this->db));
|
||||
$result = $updater->check();
|
||||
$result['ok'] = true;
|
||||
$result['current_release'] = Version::RELEASE;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function json($data, int $status = 200): void
|
||||
private function install(): array
|
||||
{
|
||||
if (!headers_sent()) {
|
||||
http_response_code($status);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue