diff --git a/src/functions.php b/src/functions.php index 1d6ccf7..90c16fc 100644 --- a/src/functions.php +++ b/src/functions.php @@ -27,15 +27,6 @@ function app_fakecheck_url(): string /** * Script registrieren - * - * @param string $src Pfad zur Datei, z. B. '/assets/js/auth.js' - * @param string $pos 'header' oder 'footer' - * @param bool $defer standard true - * @param bool $async standard false - * @param string $type z. B. 'module' - * @param string|null $version null = nutze ASSET_VERSION (falls definiert), - * '' = kein ?v=, - * 'xyz'= erzwinge diese Version */ function tpl_add_script( string $src, @@ -45,17 +36,19 @@ function tpl_add_script( string $type = '', ?string $version = null ): void { - // Standard-Verhalten: falls Version nicht explizit gesetzt → globale ASSET_VERSION verwenden if ($version === null && defined('ASSET_VERSION')) { $version = ASSET_VERSION; } + $GLOBALS['page_header_scripts'] = $GLOBALS['page_header_scripts'] ?? []; + $GLOBALS['page_footer_scripts'] = $GLOBALS['page_footer_scripts'] ?? []; + $data = [ 'src' => $src, 'defer' => $defer, 'async' => $async, 'type' => $type, - 'version' => $version, // kann null, '' oder string sein + 'version' => $version, ]; if ($pos === 'header') { @@ -66,11 +59,7 @@ function tpl_add_script( } /** - * CSS registrieren - * - * @param string $href Pfad zur CSS - * @param string $pos 'header' oder 'footer' - * @param string|null $version gleiche Logik wie bei Scripts + * CSS registrieren – MIT korrekt funktionierendem Cache-Buster */ function tpl_add_style(string $href, string $pos = 'header', ?string $version = null): void { @@ -78,28 +67,28 @@ function tpl_add_style(string $href, string $pos = 'header', ?string $version = $version = ASSET_VERSION; } + // version = null oder '' → kein Cache-Buster + $finalHref = $href; + + if ($version !== null && $version !== '') { + $separator = (strpos($href, '?') === false) ? '?' : '&'; + $finalHref = $href . $separator . 'v=' . rawurlencode($version); + } + $GLOBALS['page_styles'][] = [ - 'href' => $href, + 'href' => $finalHref, 'pos' => $pos, 'version' => $version, ]; } - /** - * Templating Loader - * - * Beispiel: - * tpl('header'); // structure/header.php - * tpl('hero', 'landing', 'main'); // landing/main/hero.php - * tpl('faq', 'landing', 'fakecheck'); // landing/fakecheck/faq.php + * Template Loader */ - function tpl(string $file, string $type = 'structure', string $site = 'main'): void { $base = __DIR__ . '/../partials/'; - // VALIDIERUNG: Nur einfache Check, kein Path-Traversal if (preg_match('/[^a-zA-Z0-9_\-]/', $file)) { echo ""; return; @@ -128,13 +117,8 @@ function tpl(string $file, string $type = 'structure', string $site = 'main'): v } } - /** - * Flash-Meldung setzen (wird genau einmal nach Redirect angezeigt). - * - * @param string $type z.B. 'success', 'error', 'info', 'warning' - * @param string $message Die Meldung für den Nutzer - * @param string|null $context Optionaler Kontext (z.B. 'login', 'register') + * Flash setzen */ function flash_set(string $type, string $message, ?string $context = null): void { @@ -150,9 +134,7 @@ function flash_set(string $type, string $message, ?string $context = null): void } /** - * Flash-Meldung holen und direkt löschen (Einmal-Anzeige). - * - * @return array|null ['type' => 'success|error|info|warning', 'message' => '...', 'context' => '...'] + * Flash holen */ function flash_get(): ?array { @@ -175,11 +157,7 @@ function flash_get(): ?array } /** - * Interne Helper-Funktion: traversiert ein Array mit "dot notation"-Segmenten. - * - * @param array $data - * @param string $key z.B. 'pages.landing.meta.title' - * @return mixed|null + * interner Helper für dot-notation keys */ function _i18n_traverse_array(array $data, string $key) { @@ -197,21 +175,7 @@ function _i18n_traverse_array(array $data, string $key) } /** - * Zentrale Funktion zum Laden eines i18n-Strings mit Fallback. - * - * - greift zuerst auf $GLOBALS['i18n']['current'] zu - * - dann auf $GLOBALS['i18n']['fallback'] - * - unterstützt Platzhalter {key} und {{key}} - * - eingebaute Platzhalter: - * {year} - * {{primary_domain}} - * {{primary_url}} - * - * @param string $path z.B. 'pages.login.meta.title' - * @param mixed $default Fallback, falls nichts gefunden wird - * @param array $replacements ['name' => 'Lars'] - * - * @return string + * Hauptfunktion i18n_get */ function i18n_get(string $path, $default = null, array $replacements = []): string { @@ -222,10 +186,8 @@ function i18n_get(string $path, $default = null, array $replacements = []): stri $current = $GLOBALS['i18n']['current'] ?? []; $fallback = $GLOBALS['i18n']['fallback'] ?? []; - // zuerst in der aktiven Sprache suchen $value = _i18n_traverse_array($current, $path); - // wenn dort nichts → Fallback-Sprache if ($value === null && !empty($fallback)) { $value = _i18n_traverse_array($fallback, $path); } @@ -236,7 +198,7 @@ function i18n_get(string $path, $default = null, array $replacements = []): stri $text = $value; - // eingebaute Platzhalter + // built-in placeholder $builtIn = [ '{year}' => date('Y'), '{{primary_domain}}' => function_exists('app_primary_domain') ? app_primary_domain() : '', @@ -247,12 +209,9 @@ function i18n_get(string $path, $default = null, array $replacements = []): stri $text = str_replace($ph, $val, $text); } - // zusätzliche Platzhalter aus $replacements foreach ($replacements as $key => $val) { $val = (string)$val; - // Variante {key} $text = str_replace('{' . $key . '}', $val, $text); - // Variante {{key}} $text = str_replace('{{' . $key . '}}', $val, $text); } @@ -260,16 +219,10 @@ function i18n_get(string $path, $default = null, array $replacements = []): stri } /** - * Convenience-Wrapper für i18n_get, etwas "kürzer" zu tippen - * und vom bisherigen Code kompatibel genutzt. - * - * Beispiel: - * i18n_get_fmt('fake_ui.log_capacity_probe_result', 'Fallback', ['size' => '2 GB']); + * Kurzform für i18n_get */ function i18n_get_fmt(string $path, $default = '', array $vars = []): string { - // Falls $default explizit null ist → leere String als Basis $def = $default ?? ''; - return i18n_get($path, $def, $vars); }