=0.20" } } * * oder eine Release-Antwort von Forgejo/Gitea/GitHub (…/releases/latest bzw. * …/releases): Version aus „tag_name“, Download = erstes ZIP unter „assets“. */ class PluginUpdates { public const STATE = 'plugins.updates'; public const MAX_BYTES = 50 * 1024 * 1024; public function __construct( private readonly PluginManager $plugins, private readonly PluginInstaller $installer, private readonly Settings $settings, ) {} /** * Alle Kunden-Plugins mit update_url prüfen. * * @return array> */ public function check(): array { $results = []; foreach ($this->plugins->all() as $manifest) { if ($manifest->core || ! $manifest->updateUrl) { continue; } try { $results[$manifest->slug] = $this->fetch($manifest->updateUrl); } catch (Throwable $e) { $results[$manifest->slug] = ['error' => $e->getMessage()]; } } $this->settings->set(self::STATE, ['checked_at' => now()->toIso8601String(), 'plugins' => $results]); return $results; } public function checkedAt(): ?string { return $this->settings->get(self::STATE)['checked_at'] ?? null; } /** * Bekannte Informationen zu einem Plugin (aus der letzten Prüfung). * * @return array{version?: string, download_url?: string, sha256?: ?string, changelog?: ?string, requires_core?: ?string, error?: string, available: bool, compatible: bool}|null */ public function info(string $slug): ?array { $manifest = $this->plugins->find($slug); $info = $this->settings->get(self::STATE)['plugins'][$slug] ?? null; if (! $manifest || $manifest->core || ! is_array($info)) { return null; } $newer = isset($info['version']) && version_compare(ltrim($info['version'], 'vV'), ltrim($manifest->version, 'vV'), '>'); $compatible = ! isset($info['requires_core']) || VersionConstraint::satisfies(Version::VERSION, $info['requires_core']); return [...$info, 'available' => $newer && isset($info['download_url']), 'compatible' => $compatible]; } /** * Neue Fassung herunterladen und installieren; Daten und Aktivierung bleiben erhalten. */ public function update(string $slug): PluginManifest { $info = $this->info($slug); if (! $info || ! $info['available']) { throw new RuntimeException('Für dieses Plugin ist kein Update bekannt – bitte zuerst „Nach Updates suchen“.'); } if (! $info['compatible']) { throw new RuntimeException("Die neue Fassung benötigt Inhaus {$info['requires_core']}. Bitte zuerst Inhaus aktualisieren."); } $file = tempnam(sys_get_temp_dir(), 'plugin-update'); try { // Nur über HTTPS (auch bei Weiterleitungen), nie an interne Adressen, Größe schon beim Laden begrenzen $response = Http::timeout(120)->withOptions([ ...SafeOutbound::options(['https']), 'sink' => $file, 'progress' => function ($total, $downloaded) { if ($total > self::MAX_BYTES || $downloaded > self::MAX_BYTES) { throw new RuntimeException('Das Update ist größer als 50 MB.'); } }, ])->get($this->safeUrl($info['download_url'])); if (! $response->successful()) { throw new RuntimeException("Download fehlgeschlagen (HTTP {$response->status()})."); } if (filesize($file) > self::MAX_BYTES) { throw new RuntimeException('Das Update ist größer als 50 MB.'); } if (! empty($info['sha256']) && ! hash_equals(strtolower($info['sha256']), hash_file('sha256', $file))) { throw new RuntimeException('Die Prüfsumme des Downloads stimmt nicht – das Update wurde nicht installiert.'); } $wasEnabled = $this->plugins->isEnabled($slug); $manifest = $this->installer->install($file, expectedSlug: $slug); // Neue Tabellen oder Spalten des Plugins sofort anlegen if ($wasEnabled && is_dir($manifest->migrationsPath())) { Artisan::call('migrate', ['--path' => $manifest->migrationsPath(), '--realpath' => true, '--force' => true]); } Audit::log('plugin.auto_updated', ['plugin' => $slug, 'version' => $manifest->version]); return $manifest; } finally { @unlink($file); } } /** * @return array{version: string, download_url: ?string, sha256: ?string, changelog: ?string, requires_core: ?string} */ private function fetch(string $url): array { $response = Http::timeout(15)->acceptJson()->withOptions(SafeOutbound::options(['https']))->get($this->safeUrl($url)); if (! $response->successful()) { throw new RuntimeException("Update-Adresse antwortet mit HTTP {$response->status()}."); } $data = $response->json(); if (! is_array($data)) { throw new RuntimeException('Update-Adresse liefert kein JSON.'); } // Liste von Releases: das neueste ohne Vorabversion if (array_is_list($data)) { $data = collect($data)->first(fn ($release) => is_array($release) && ! ($release['prerelease'] ?? false) && ! ($release['draft'] ?? false)); if (! $data) { throw new RuntimeException('Keine veröffentlichte Version gefunden.'); } } if (isset($data['tag_name'])) { $zip = collect($data['assets'] ?? [])->first(fn ($asset) => str_ends_with(strtolower((string) ($asset['name'] ?? '')), '.zip')); return [ 'version' => ltrim((string) $data['tag_name'], 'vV'), 'download_url' => $zip['browser_download_url'] ?? null, 'sha256' => null, 'changelog' => isset($data['body']) ? mb_substr((string) $data['body'], 0, 2000) : null, 'requires_core' => null, ]; } if (! is_string($data['version'] ?? null)) { throw new RuntimeException('Update-Adresse liefert keine Version.'); } return [ 'version' => ltrim($data['version'], 'vV'), 'download_url' => is_string($data['download_url'] ?? null) ? $data['download_url'] : null, 'sha256' => is_string($data['sha256'] ?? null) ? $data['sha256'] : null, 'changelog' => is_string($data['changelog'] ?? null) ? mb_substr($data['changelog'], 0, 2000) : null, 'requires_core' => is_string($data['requires']['core'] ?? null) ? $data['requires']['core'] : null, ]; } private function safeUrl(string $url): string { if (! preg_match('#^https://[^\s/]+#i', $url)) { throw new RuntimeException('Update-Adressen müssen mit https:// beginnen.'); } if (! SafeOutbound::isPublicUrl($url, ['https'])) { throw new RuntimeException('Update-Adressen dürfen nicht auf interne Server zeigen.'); } return $url; } }