This commit is contained in:
2025-11-29 01:35:45 +01:00
parent 73872b4b86
commit cc7ec65685

View File

@@ -27,15 +27,6 @@ function app_fakecheck_url(): string
/** /**
* Script registrieren * 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( function tpl_add_script(
string $src, string $src,
@@ -45,17 +36,19 @@ function tpl_add_script(
string $type = '', string $type = '',
?string $version = null ?string $version = null
): void { ): void {
// Standard-Verhalten: falls Version nicht explizit gesetzt → globale ASSET_VERSION verwenden
if ($version === null && defined('ASSET_VERSION')) { if ($version === null && defined('ASSET_VERSION')) {
$version = ASSET_VERSION; $version = ASSET_VERSION;
} }
$GLOBALS['page_header_scripts'] = $GLOBALS['page_header_scripts'] ?? [];
$GLOBALS['page_footer_scripts'] = $GLOBALS['page_footer_scripts'] ?? [];
$data = [ $data = [
'src' => $src, 'src' => $src,
'defer' => $defer, 'defer' => $defer,
'async' => $async, 'async' => $async,
'type' => $type, 'type' => $type,
'version' => $version, // kann null, '' oder string sein 'version' => $version,
]; ];
if ($pos === 'header') { if ($pos === 'header') {
@@ -66,11 +59,7 @@ function tpl_add_script(
} }
/** /**
* CSS registrieren * CSS registrieren MIT korrekt funktionierendem Cache-Buster
*
* @param string $href Pfad zur CSS
* @param string $pos 'header' oder 'footer'
* @param string|null $version gleiche Logik wie bei Scripts
*/ */
function tpl_add_style(string $href, string $pos = 'header', ?string $version = null): void 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 = 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'][] = [ $GLOBALS['page_styles'][] = [
'href' => $href, 'href' => $finalHref,
'pos' => $pos, 'pos' => $pos,
'version' => $version, 'version' => $version,
]; ];
} }
/** /**
* Templating Loader * Template Loader
*
* Beispiel:
* tpl('header'); // structure/header.php
* tpl('hero', 'landing', 'main'); // landing/main/hero.php
* tpl('faq', 'landing', 'fakecheck'); // landing/fakecheck/faq.php
*/ */
function tpl(string $file, string $type = 'structure', string $site = 'main'): void function tpl(string $file, string $type = 'structure', string $site = 'main'): void
{ {
$base = __DIR__ . '/../partials/'; $base = __DIR__ . '/../partials/';
// VALIDIERUNG: Nur einfache Check, kein Path-Traversal
if (preg_match('/[^a-zA-Z0-9_\-]/', $file)) { if (preg_match('/[^a-zA-Z0-9_\-]/', $file)) {
echo "<!-- tpl(): Ungültiger Template-Name -->"; echo "<!-- tpl(): Ungültiger Template-Name -->";
return; 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). * Flash setzen
*
* @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')
*/ */
function flash_set(string $type, string $message, ?string $context = null): void 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). * Flash holen
*
* @return array|null ['type' => 'success|error|info|warning', 'message' => '...', 'context' => '...']
*/ */
function flash_get(): ?array function flash_get(): ?array
{ {
@@ -175,11 +157,7 @@ function flash_get(): ?array
} }
/** /**
* Interne Helper-Funktion: traversiert ein Array mit "dot notation"-Segmenten. * interner Helper für dot-notation keys
*
* @param array $data
* @param string $key z.B. 'pages.landing.meta.title'
* @return mixed|null
*/ */
function _i18n_traverse_array(array $data, string $key) 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. * Hauptfunktion i18n_get
*
* - 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
*/ */
function i18n_get(string $path, $default = null, array $replacements = []): string 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'] ?? []; $current = $GLOBALS['i18n']['current'] ?? [];
$fallback = $GLOBALS['i18n']['fallback'] ?? []; $fallback = $GLOBALS['i18n']['fallback'] ?? [];
// zuerst in der aktiven Sprache suchen
$value = _i18n_traverse_array($current, $path); $value = _i18n_traverse_array($current, $path);
// wenn dort nichts → Fallback-Sprache
if ($value === null && !empty($fallback)) { if ($value === null && !empty($fallback)) {
$value = _i18n_traverse_array($fallback, $path); $value = _i18n_traverse_array($fallback, $path);
} }
@@ -236,7 +198,7 @@ function i18n_get(string $path, $default = null, array $replacements = []): stri
$text = $value; $text = $value;
// eingebaute Platzhalter // built-in placeholder
$builtIn = [ $builtIn = [
'{year}' => date('Y'), '{year}' => date('Y'),
'{{primary_domain}}' => function_exists('app_primary_domain') ? app_primary_domain() : '', '{{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); $text = str_replace($ph, $val, $text);
} }
// zusätzliche Platzhalter aus $replacements
foreach ($replacements as $key => $val) { foreach ($replacements as $key => $val) {
$val = (string)$val; $val = (string)$val;
// Variante {key}
$text = str_replace('{' . $key . '}', $val, $text); $text = str_replace('{' . $key . '}', $val, $text);
// Variante {{key}}
$text = str_replace('{{' . $key . '}}', $val, $text); $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 * Kurzform für i18n_get
* und vom bisherigen Code kompatibel genutzt.
*
* Beispiel:
* i18n_get_fmt('fake_ui.log_capacity_probe_result', 'Fallback', ['size' => '2 GB']);
*/ */
function i18n_get_fmt(string $path, $default = '', array $vars = []): string function i18n_get_fmt(string $path, $default = '', array $vars = []): string
{ {
// Falls $default explizit null ist → leere String als Basis
$def = $default ?? ''; $def = $default ?? '';
return i18n_get($path, $def, $vars); return i18n_get($path, $def, $vars);
} }