Files
usbcheck.it/src/functions.php
2025-11-22 02:05:11 +01:00

154 lines
4.1 KiB
PHP

<?php
$GLOBALS['page_header_scripts'] = $GLOBALS['page_header_scripts'] ?? [];
$GLOBALS['page_footer_scripts'] = $GLOBALS['page_footer_scripts'] ?? [];
$GLOBALS['page_styles'] = $GLOBALS['page_styles'] ?? [];
/**
* 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,
string $pos = 'footer',
bool $defer = true,
bool $async = false,
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;
}
$data = [
'src' => $src,
'defer' => $defer,
'async' => $async,
'type' => $type,
'version' => $version, // kann null, '' oder string sein
];
if ($pos === 'header') {
$GLOBALS['page_header_scripts'][] = $data;
} else {
$GLOBALS['page_footer_scripts'][] = $data;
}
}
/**
* CSS registrieren
*
* @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
{
if ($version === null && defined('ASSET_VERSION')) {
$version = ASSET_VERSION;
}
$GLOBALS['page_styles'][] = [
'href' => $href,
'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
*/
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 "<!-- tpl(): Ungültiger Template-Name -->";
return;
}
if (preg_match('/[^a-zA-Z0-9_\-]/', $type)) {
echo "<!-- tpl(): Ungültiger Type -->";
return;
}
if (preg_match('/[^a-zA-Z0-9_\-]/', $site)) {
echo "<!-- tpl(): Ungültiger Site -->";
return;
}
if ($type === 'landing') {
$path = $base . "landing/$site/$file.php";
} else {
$path = $base . "structure/$file.php";
}
extract($GLOBALS, EXTR_SKIP);
if (file_exists($path)) {
include $path;
} else {
echo "<!-- tpl(): Datei nicht gefunden: $path -->";
}
}
/**
* 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
*/
function flash_set(string $type, string $message, ?string $context = null): void
{
if (session_status() !== PHP_SESSION_ACTIVE) {
@session_start();
}
$_SESSION['flash'] = [
'type' => $type,
'message' => $message,
'context' => $context,
];
}
/**
* Flash-Meldung holen und direkt löschen (Einmal-Anzeige).
*
* @return array|null ['type' => 'success|error|info|warning', 'message' => '...']
*/
function flash_get(): ?array
{
if (session_status() !== PHP_SESSION_ACTIVE) {
@session_start();
}
if (empty($_SESSION['flash']) || !is_array($_SESSION['flash'])) {
return null;
}
$flash = $_SESSION['flash'];
unset($_SESSION['flash']);
$flash['type'] = $flash['type'] ?? 'info';
$flash['message'] = $flash['message'] ?? '';
$flash['context'] = $flash['context'] ?? null;
return $flash;
}