'text/javascript', 'mjs' => 'text/javascript', 'css' => 'text/css', 'map' => 'application/json', 'json' => 'application/json', 'svg' => 'image/svg+xml', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'webp' => 'image/webp', 'gif' => 'image/gif', 'woff2' => 'font/woff2', ]; /** @var array, css: list}>|null */ private ?array $cache = null; public function __construct(private readonly PluginManager $plugins) {} /** * Für das Frontend (window.__inhausPlugins): aktive Plugins mit Oberfläche. * * @return array, css: list}> */ public function forFrontend(): array { if ($this->cache !== null) { return $this->cache; } $this->cache = []; foreach ($this->plugins->loaded() as $slug => $plugin) { $manifest = $plugin->manifest; if ($manifest->core || ! is_file($manifest->path.'/'.self::MANIFEST)) { continue; } $data = json_decode((string) file_get_contents($manifest->path.'/'.self::MANIFEST), true); if (! is_array($data)) { continue; } $clean = fn ($file) => is_string($file) && preg_match('#^[A-Za-z0-9_./-]+$#', $file) && ! str_contains($file, '..') ? $file : null; // Einmal nach public/ kopieren – dann liefert der Webserver die Dateien ohne PHP aus $this->publish($slug, $manifest); $this->cache[$slug] = [ 'base' => '/plugin-assets/'.rawurlencode($slug).'/'.rawurlencode($manifest->version).'/', 'components' => array_filter(array_map($clean, (array) ($data['components'] ?? [])), fn ($file, $key) => $file !== null && preg_match('#^[A-Za-z0-9_/-]+$#', (string) $key), ARRAY_FILTER_USE_BOTH), 'css' => array_values(array_filter(array_map($clean, (array) ($data['css'] ?? [])))), ]; } return $this->cache; } /** * Import Map für Plugins: dieselben Module wie im Portal. * * @return array{imports: array} */ public static function importMap(): array { $v = '?v='.Version::VERSION; return ['imports' => [ 'vue' => '/sdk/vue.js'.$v, '@inertiajs/vue3' => '/sdk/inertia.js'.$v, '@inhaus/ui' => '/sdk/ui.js'.$v, ]]; } /** * dist/ eines aktiven Plugins nach public/plugin-assets/// kopieren (gleiche * Adresse wie die PHP-Route). Der Webserver liefert vorhandene Dateien direkt aus – schneller * und ohne Sitzung. Geht das Kopieren nicht (public/ schreibgeschützt), springt die Route ein. */ public function publish(string $slug, PluginManifest $manifest): bool { $target = self::publicPath($slug).'/'.$manifest->version; // Fingerabdruck des Builds: Inhalt von dist/inhaus.json plus Änderungszeit der dort genannten // Dateien (Seiten behalten ihren Namen) – ein neuer Build, auch bei gleicher Versionsnummer // (z. B. während der Entwicklung), wird neu kopiert $json = (string) @file_get_contents($manifest->path.'/'.self::MANIFEST); $data = json_decode($json, true); $listed = is_array($data) ? [...array_values((array) ($data['components'] ?? [])), ...(array) ($data['css'] ?? [])] : []; $times = array_map(fn ($file) => is_string($file) ? (int) @filemtime($manifest->path.'/dist/'.$file) : 0, $listed); $build = md5($json.'|'.implode(',', $times)); if (@file_get_contents($target.'/.complete') === $build) { return true; } $dist = realpath($manifest->path.'/dist'); if (! $dist || ! preg_match('#^[A-Za-z0-9.+-]+$#', $manifest->version)) { return false; } $staging = self::publicPath($slug).'/.tmp-'.bin2hex(random_bytes(4)); try { $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dist, \FilesystemIterator::SKIP_DOTS)); foreach ($files as $file) { $relative = substr($file->getPathname(), strlen($dist) + 1); // Nur erlaubte Dateitypen und saubere Namen (wie bei der Route) if (! $file->isFile() || ! isset(self::TYPES[strtolower($file->getExtension())]) || ! preg_match('#^[A-Za-z0-9_./-]+$#', $relative)) { continue; } if (! is_dir(dirname($staging.'/'.$relative)) && ! @mkdir(dirname($staging.'/'.$relative), 0775, true)) { throw new \RuntimeException('public/plugin-assets ist nicht beschreibbar.'); } if (! @copy($file->getPathname(), $staging.'/'.$relative)) { throw new \RuntimeException("{$relative} konnte nicht kopiert werden."); } } @file_put_contents($staging.'/.complete', $build); Filesystem::deleteDirectory($target); if (! @rename($staging, $target)) { throw new \RuntimeException('Kopie konnte nicht übernommen werden.'); } // Ältere Versionen dieses Plugins entfernen foreach (glob(self::publicPath($slug).'/*', GLOB_ONLYDIR) ?: [] as $dir) { if (basename($dir) !== $manifest->version) { Filesystem::deleteDirectory($dir); } } return true; } catch (\Throwable $e) { Filesystem::deleteDirectory($staging); Log::info("Plugin-Dateien von {$slug} werden über PHP ausgeliefert: ".$e->getMessage()); return false; } } /** Veröffentlichte Kopie entfernen (Plugin deaktiviert oder gelöscht) */ public static function unpublish(string $slug): void { if (preg_match('#^[a-z0-9-]+$#', $slug)) { Filesystem::deleteDirectory(self::publicPath($slug)); } } private static function publicPath(string $slug): string { return rtrim((string) (config('inhaus.plugin_assets_path') ?: public_path('plugin-assets')), '/').'/'.$slug; } /** * Pfad einer ausgelieferten Datei – nur aus dist/ eines aktiven Kunden-Plugins. */ public function file(string $slug, string $path): ?string { $plugin = $this->plugins->loaded()[$slug] ?? null; $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); if (! $plugin || $plugin->manifest->core || ! isset(self::TYPES[$extension]) || str_contains($path, '..')) { return null; } $dist = realpath($plugin->manifest->path.'/dist'); $file = realpath($plugin->manifest->path.'/dist/'.$path); return $dist && $file && str_starts_with($file, $dist.DIRECTORY_SEPARATOR) && is_file($file) ? $file : null; } }