This commit is contained in:
2025-11-29 01:57:38 +01:00
parent cc7ec65685
commit d63acce8f7
3 changed files with 226 additions and 117 deletions

View File

@@ -3,6 +3,9 @@ $GLOBALS['page_header_scripts'] = $GLOBALS['page_header_scripts'] ?? [];
$GLOBALS['page_footer_scripts'] = $GLOBALS['page_footer_scripts'] ?? [];
$GLOBALS['page_styles'] = $GLOBALS['page_styles'] ?? [];
/**
* Primäre Domain / URL (Brand)
*/
function app_primary_domain(): string
{
return defined('APP_DOMAIN_PRIMARY') ? APP_DOMAIN_PRIMARY : 'usbcheck.it';
@@ -25,6 +28,88 @@ function app_fakecheck_url(): string
return rtrim($url, '/');
}
/* -------------------------------------------------
Zentrale Request-/URL-Helper
------------------------------------------------- */
/**
* Aktuelles Schema (http / https), inkl. Proxy-Header-Fallback.
*/
function app_request_scheme(): string
{
// Proxy / Loadbalancer
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
$proto = strtolower((string)$_SERVER['HTTP_X_FORWARDED_PROTO']);
if ($proto === 'https' || $proto === 'http') {
return $proto;
}
}
// Direkt
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
return 'https';
}
return 'http';
}
/**
* Aktueller Host (inkl. Port, falls im Host-Header).
*/
function app_request_host(): string
{
return $_SERVER['HTTP_HOST'] ?? app_primary_domain();
}
/**
* Basis-URL der aktuellen Anfrage, z. B. https://staging.usbcheck.it
*/
function app_current_base_url(): string
{
return app_request_scheme() . '://' . app_request_host();
}
/**
* Aktueller Pfad ohne Query, z. B. /login/ oder /fakecheck/demo
*/
function app_current_path(): string
{
return strtok($_SERVER['REQUEST_URI'] ?? '/', '?');
}
/**
* Aktuelle URL, optional mit Query-String.
*/
function app_current_url(bool $withQuery = true): string
{
$base = app_current_base_url();
$uri = $_SERVER['REQUEST_URI'] ?? '/';
if ($withQuery) {
return $base . $uri;
}
return $base . strtok($uri, '?');
}
/**
* Canonical-URL bestimmen.
* - Wenn $override gesetzt: exakt diesen Wert verwenden.
* - Sonst: aktuelle URL ohne Query.
*/
function app_canonical_url(?string $override = null): string
{
if (is_string($override) && $override !== '') {
return $override;
}
return app_current_url(false);
}
/* -------------------------------------------------
Assets: JS / CSS
------------------------------------------------- */
/**
* Script registrieren
*/
@@ -59,32 +144,37 @@ function tpl_add_script(
}
/**
* CSS registrieren MIT korrekt funktionierendem Cache-Buster
* CSS registrieren MIT Cache-Buster und Priorität
*
* @param string $href Pfad zur CSS-Datei oder externe URL
* @param string $pos aktuell nur 'header' relevant (aber für Symmetrie drin)
* @param string $priority 'early' | 'normal' | 'late'
* @param string|null $version null = nutze ASSET_VERSION (falls definiert),
* '' = kein ?v=,
* 'xyz'= erzwinge diese Version
*/
function tpl_add_style(string $href, string $pos = 'header', ?string $version = null): void
{
function tpl_add_style(
string $href,
string $pos = 'header',
string $priority = 'normal',
?string $version = null
): void {
if ($version === null && defined('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'][] = [
'href' => $finalHref,
'pos' => $pos,
'version' => $version,
'href' => $href,
'pos' => $pos,
'priority' => $priority,
'version' => $version,
];
}
/**
* Template Loader
*/
/* -------------------------------------------------
Template Loader
------------------------------------------------- */
function tpl(string $file, string $type = 'structure', string $site = 'main'): void
{
$base = __DIR__ . '/../partials/';
@@ -117,9 +207,10 @@ function tpl(string $file, string $type = 'structure', string $site = 'main'): v
}
}
/**
* Flash setzen
*/
/* -------------------------------------------------
Flash-Messages
------------------------------------------------- */
function flash_set(string $type, string $message, ?string $context = null): void
{
if (session_status() !== PHP_SESSION_ACTIVE) {
@@ -133,9 +224,6 @@ function flash_set(string $type, string $message, ?string $context = null): void
];
}
/**
* Flash holen
*/
function flash_get(): ?array
{
if (session_status() !== PHP_SESSION_ACTIVE) {
@@ -156,6 +244,10 @@ function flash_get(): ?array
return $flash;
}
/* -------------------------------------------------
i18n Helper
------------------------------------------------- */
/**
* interner Helper für dot-notation keys
*/