Security: verwijder hardcoded wachtwoord, voeg random-wachtwoord-generator toe bij eerste installatie
This commit is contained in:
+169
-45
@@ -5,24 +5,62 @@ use Twig\Environment;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
|
||||
/**
|
||||
* ThemeManager - Resolves and renders the active theme
|
||||
* Beheert het actieve thema en de rendering hiervan.
|
||||
*
|
||||
* Responsibilities:
|
||||
* - Resolve the active theme directory from config
|
||||
* - Load theme.json (title, default_layout, template mapping, colors)
|
||||
* - Build a Twig environment rooted at the theme directory
|
||||
* - Compile theme SCSS to CSS (in assets/css_compiled/)
|
||||
* - Map a requested layout to a concrete .twig template, falling back
|
||||
* to the theme's default_layout when the layout is unknown
|
||||
* Verantwoordelijkheden:
|
||||
* - Bepaalt de actieve thema-map aan de hand van de configuratie.
|
||||
* - Laadt theme.json (titel, default_layout, template-mapping, kleuren).
|
||||
* - Bouwt een Twig-omgeving geworteld in de thema-map.
|
||||
* - Compileert thema-SCSS naar CSS (in assets/css_compiled/).
|
||||
* - Mapt een aangevraagde layout naar een concreet .twig-template, met
|
||||
* fallback naar de default_layout van het thema wanneer de layout
|
||||
* onbekend is.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*/
|
||||
class ThemeManager {
|
||||
/**
|
||||
* Volledige CMS-configuratie.
|
||||
*
|
||||
* @since 2.6.4
|
||||
* @var array Bevat o.a. 'theme_dir' en 'theme'.
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* Absoluut pad naar de actieve thema-map.
|
||||
*
|
||||
* @since 2.6.4
|
||||
* @var string
|
||||
*/
|
||||
private $themeDir;
|
||||
|
||||
/**
|
||||
* Ruwe theme.json configuratie-array.
|
||||
*
|
||||
* @since 2.6.4
|
||||
* @var array
|
||||
*/
|
||||
private $themeConfig;
|
||||
|
||||
/**
|
||||
* Twig-omgeving voor het renderen van thema-templates.
|
||||
*
|
||||
* @since 2.6.4
|
||||
* @var Environment
|
||||
*/
|
||||
private $twig;
|
||||
|
||||
/**
|
||||
* @param array $config Full CMS config (must contain 'theme_dir' and 'theme')
|
||||
* Constructor: initialiseert de thema-manager met de CMS-configuratie.
|
||||
*
|
||||
* Stelt de thema-map, theme.json-configuratie en een Twig-omgeving in.
|
||||
* De Twig-cache staat uit en auto-escape is uitgeschakeld (de thema's
|
||||
* beheren eigen escaping waar nodig).
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param array $config Volledige CMS-configuratie; moet 'theme_dir' en 'theme' bevatten.
|
||||
*/
|
||||
public function __construct(array $config) {
|
||||
$this->config = $config;
|
||||
@@ -37,55 +75,85 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute path of the active theme directory
|
||||
* Geeft het absolute pad van de actieve thema-map.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return string Absoluut pad naar de thema-map.
|
||||
*/
|
||||
public function getThemeDir(): string {
|
||||
return $this->themeDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw theme.json config array
|
||||
* Geeft de ruwe theme.json configuratie-array.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return array Theme.json inhoud, of lege array bij afwezigheid.
|
||||
*/
|
||||
public function getThemeConfig(): array {
|
||||
return $this->themeConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the theme title (from theme.json 'title' or 'name')
|
||||
* Geeft de thema-titel.
|
||||
*
|
||||
* Leest eerst 'title' uit theme.json, dan 'name', en als laatste
|
||||
* fallback de mapnaam van het thema.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return string Thematische titel.
|
||||
*/
|
||||
public function getTitle(): string {
|
||||
return $this->themeConfig['title'] ?? $this->themeConfig['name'] ?? basename($this->themeDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the theme config section (default_template, background settings, etc.)
|
||||
* Geeft de 'config'-sectie van theme.json.
|
||||
*
|
||||
* Bevat o.a. default_template en achtergrondinstellingen.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return array Config-sectie, of lege array bij afwezigheid.
|
||||
*/
|
||||
public function getConfig(): array {
|
||||
return $this->themeConfig['config'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the theme template section (layout key => .twig file mapping)
|
||||
* Geeft de 'template'-sectie van theme.json.
|
||||
*
|
||||
* Bevat de mapping van layout-key naar .twig-bestand.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return array<string,string> Layout-key => .twig-bestand mapping.
|
||||
*/
|
||||
public function getTemplates(): array {
|
||||
return $this->themeConfig['template'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the .twig template file for a requested layout.
|
||||
* Bepaalt het .twig-templatebestand voor een aangevraagde layout.
|
||||
*
|
||||
* Templates are defined in theme.json under the "template" section:
|
||||
* Templates worden gedefinieerd in theme.json onder de "template"-sectie:
|
||||
* { "template": { "full_content": "full_content.twig", ... } }
|
||||
* The default template is defined in the "config" section:
|
||||
* De standaardtemplate staat in de "config"-sectie:
|
||||
* { "config": { "default_template": "full_content", ... } }
|
||||
*
|
||||
* Priority:
|
||||
* 1. If the layout is a known key in the "template" section, use its mapped file.
|
||||
* 2. Otherwise fall back to config.default_template.
|
||||
* 3. Final safety net: full_content.twig.
|
||||
* Prioriteit:
|
||||
* 1. Als de layout een bekende key is in de "template"-sectie, gebruik
|
||||
* het bijbehorende bestand.
|
||||
* 2. Anders fallback naar config.default_template.
|
||||
* 3. Laatste veiligheidsnet: full_content.twig.
|
||||
*
|
||||
* @param string $layout Requested layout key (e.g. 'left_sidebar')
|
||||
* @return string Template name usable by the Twig loader
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $layout Aangevraagde layout-key (bijv. 'left_sidebar').
|
||||
* @return string Templacenaam bruikbaar voor de Twig-loader.
|
||||
*/
|
||||
public function getTemplateForLayout(string $layout): string {
|
||||
$layout = trim($layout);
|
||||
@@ -111,16 +179,23 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of available layout keys defined in the "template" section.
|
||||
* Geeft de lijst met beschikbare layout-keys uit de "template"-sectie.
|
||||
*
|
||||
* @return array List of layout keys
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return array<string> Lijst met layout-keys.
|
||||
*/
|
||||
public function getLayouts(): array {
|
||||
return array_keys($this->getTemplates());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a template file exists in the theme directory
|
||||
* Controleert of een templatebestand bestaat in de thema-map.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $file Bestandsnaam van het template (relatief t.o.v. thema-map).
|
||||
* @return bool True als het bestand bestaat.
|
||||
*/
|
||||
private function templateExists(string $file): bool {
|
||||
$path = $this->themeDir . '/' . ltrim($file, '/');
|
||||
@@ -128,11 +203,13 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a layout template with the given data.
|
||||
* Rendert een layout-template met de gegeven data.
|
||||
*
|
||||
* @param string $layout Requested layout key
|
||||
* @param array $data Template variables
|
||||
* @return string Rendered HTML
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $layout Aangevraagde layout-key.
|
||||
* @param array $data Template-variabelen.
|
||||
* @return string Gerenderde HTML.
|
||||
*/
|
||||
public function render(string $layout, array $data): string {
|
||||
$template = $this->getTemplateForLayout($layout);
|
||||
@@ -140,11 +217,17 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile the theme's SCSS to CSS (cached by source mtime).
|
||||
* Compiles from assets/scss/theme.scss to assets/css_compiled/theme.css
|
||||
* Compileert de SCSS van het thema naar CSS (gecacht op bron-mtime).
|
||||
*
|
||||
* @param bool $force Force recompilation
|
||||
* @return string|null Absolute path to the compiled CSS, or null if none
|
||||
* Compileert vanuit assets/scss/theme.scss naar assets/css_compiled/theme.css.
|
||||
* De gecompileerde CSS en een .mtime-cachebestand worden op alleen-lezen
|
||||
* (0444) gezet om handmatige aanpassingen te voorkomen. Bij een fout
|
||||
* wordt deze gelogd en null teruggegeven.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param bool $force Forceer hercompilatie ongeacht de cache. Default false.
|
||||
* @return string|null Absoluut pad naar de gecompileerde CSS, of null bij fout/afwezigheid.
|
||||
*/
|
||||
public function compileCss(bool $force = false): ?string {
|
||||
$scssFile = $this->themeDir . '/assets/scss/theme.scss';
|
||||
@@ -193,8 +276,15 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the public URL for the theme CSS.
|
||||
* Uses assets/css_compiled/theme.css (compiled from SCSS by scssphp).
|
||||
* Geeft de publieke URL voor de thema-CSS.
|
||||
*
|
||||
* Gebruikt assets/css_compiled/theme.css (gecompileerd vanuit SCSS door
|
||||
* scssphp). Indien de gecompileerde CSS nog niet bestaat, wordt deze
|
||||
* alsnog gecompileerd.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return string|null Publieke CSS-URL, of null indien niet beschikbaar.
|
||||
*/
|
||||
public function getCssUrl(): ?string {
|
||||
$compiledCss = $this->themeDir . '/assets/css_compiled/theme.css';
|
||||
@@ -212,7 +302,11 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the public URL for the theme JS, or null if unavailable.
|
||||
* Geeft de publieke URL voor de thema-JS, of null indien niet beschikbaar.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return string|null Publieke JS-URL, of null indien niet beschikbaar.
|
||||
*/
|
||||
public function getJsUrl(): ?string {
|
||||
$jsFile = $this->themeDir . '/assets/js/theme.js';
|
||||
@@ -223,7 +317,12 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get public URL for a theme asset.
|
||||
* Geeft de publieke URL voor een thema-asset.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @param string $assetPath Pad naar de asset binnen de assets/-map van het thema.
|
||||
* @return string Publieke URL in de vorm '/themes/<naam>/assets/<path>'.
|
||||
*/
|
||||
private function getThemeAssetUrl(string $assetPath): string {
|
||||
$themeName = basename($this->themeDir);
|
||||
@@ -231,14 +330,22 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if theme has SCSS source file.
|
||||
* Controleert of het thema een SCSS-bronbestand heeft.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return bool True als assets/scss/theme.scss bestaat.
|
||||
*/
|
||||
public function hasScss(): bool {
|
||||
return is_file($this->themeDir . '/assets/scss/theme.scss');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if compiled CSS is newer than SCSS source.
|
||||
* Controleert of de gecompileerde CSS nieuwer is dan de SCSS-bron.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return bool True als de CSS-_mtime >= SCSS-mtime, anders false.
|
||||
*/
|
||||
public function isScssCompiled(): bool {
|
||||
$scssFile = $this->themeDir . '/assets/scss/theme.scss';
|
||||
@@ -252,15 +359,24 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if theme has manual CSS file.
|
||||
* Controleert of het thema een handmatig CSS-bestand heeft.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return bool True als assets/css/theme.css bestaat.
|
||||
*/
|
||||
public function hasManualCss(): bool {
|
||||
return is_file($this->themeDir . '/assets/css/theme.css');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of CSS files in theme assets/css/ directory.
|
||||
* Excludes css_compiled directory.
|
||||
* Geeft een lijst met CSS-bestanden in de assets/css/-map van het thema.
|
||||
*
|
||||
* De css_compiled-map wordt uitgesloten.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return array<string> Lijst met publieke CSS-URLs.
|
||||
*/
|
||||
public function getCssFiles(): array {
|
||||
$cssDir = $this->themeDir . '/assets/css';
|
||||
@@ -281,7 +397,11 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of JS files in theme assets/js/ directory.
|
||||
* Geeft een lijst met JS-bestanden in de assets/js/-map van het thema.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return array<string> Lijst met publieke JS-URLs.
|
||||
*/
|
||||
public function getJsFiles(): array {
|
||||
$jsDir = $this->themeDir . '/assets/js';
|
||||
@@ -302,7 +422,11 @@ class ThemeManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL for favicon if it exists.
|
||||
* Geeft de URL voor de favicon indien deze bestaat.
|
||||
*
|
||||
* @since 2.6.4
|
||||
*
|
||||
* @return string|null Publieke favicon-URL, of null indien niet beschikbaar.
|
||||
*/
|
||||
public function getFaviconUrl(): ?string {
|
||||
$faviconFile = $this->themeDir . '/assets/img/favicon.svg';
|
||||
|
||||
Reference in New Issue
Block a user