. * Nutzt denselben ZIP-Slip-Schutz wie der Self-Updater. Ein vorhandenes Plugin * mit gleichem Kürzel wird ersetzt (Update), seine Daten bleiben erhalten. */ class PluginInstaller { public function __construct( private readonly PluginManager $plugins, private readonly ZipExtractor $extractor, ) {} public static function uploadsAllowed(): bool { return ! filter_var(config('inhaus.disallow_file_mods'), FILTER_VALIDATE_BOOL); } /** * @param string|null $expectedSlug bei Updates: das Archiv muss genau dieses Plugin enthalten */ public function install(string $zipFile, ?string $expectedSlug = null): PluginManifest { if (! self::uploadsAllowed()) { throw new RuntimeException('Das Installieren von Plugins ist in dieser Installation abgeschaltet.'); } $staging = storage_path('framework/plugin-upload-'.bin2hex(random_bytes(4))); try { $this->extractor->extract($zipFile, $staging); try { $manifest = PluginManifest::fromDirectory($staging, core: false); } catch (Throwable $e) { throw new RuntimeException('Das Archiv enthält kein gültiges Plugin: '.$e->getMessage()); } if ($expectedSlug !== null && $manifest->slug !== $expectedSlug) { throw new RuntimeException("Das Archiv enthält das Plugin „{$manifest->slug}“ statt „{$expectedSlug}“."); } $existing = $this->plugins->find($manifest->slug); if ($existing?->core) { throw new RuntimeException("Das Kürzel „{$manifest->slug}“ gehört zu einem mitgelieferten Plugin."); } if (! is_file($staging.'/src/'.str_replace('\\', '/', substr($manifest->class, strlen($manifest->namespace))).'.php')) { throw new RuntimeException("Die Plugin-Klasse {$manifest->class} fehlt im Archiv (erwartet unter src/)."); } $target = $this->plugins->customerPath().'/'.$manifest->slug; if (! is_dir(dirname($target)) && ! @mkdir(dirname($target), 0775, true)) { throw new RuntimeException('Das Verzeichnis plugins/ ist nicht beschreibbar.'); } // Parallele Anfragen sehen kurz ein halbes Plugin – das soll es nicht abschalten $window = app(MaintenanceFlag::class); $window->beginQuietUpdate(); try { Filesystem::deleteDirectory($target); if (! Filesystem::moveDirectory($staging, $target)) { throw new RuntimeException('Das Plugin konnte nicht nach plugins/ verschoben werden. Bitte Schreibrechte prüfen.'); } } finally { $window->endQuietUpdate(); } // Neu veröffentlichen (auch bei gleicher Versionsnummer) PluginAssets::unpublish($manifest->slug); $this->plugins->forgetDiscovered(); Audit::log($existing ? 'plugin.updated' : 'plugin.installed', ['plugin' => $manifest->slug, 'version' => $manifest->version]); return PluginManifest::fromDirectory($target, core: false); } finally { Filesystem::deleteDirectory($staging); } } }