206 lines
5.8 KiB
PHP
Executable File
206 lines
5.8 KiB
PHP
Executable File
<?php
|
|
|
|
if (!isset($GLOBALS['page_header_scripts'])) {
|
|
$GLOBALS['page_header_scripts'] = [];
|
|
}
|
|
if (!isset($GLOBALS['page_footer_scripts'])) {
|
|
$GLOBALS['page_footer_scripts'] = [];
|
|
}
|
|
if (!isset($GLOBALS['page_styles'])) {
|
|
$GLOBALS['page_styles'] = [];
|
|
}
|
|
|
|
function debug_log_write(string $name, $payload, array $options = []): bool
|
|
{
|
|
$dir = (string)($options['dir'] ?? (dirname(__DIR__) . '/debug'));
|
|
if ($dir === '') {
|
|
return false;
|
|
}
|
|
if (!is_dir($dir)) {
|
|
@mkdir($dir, 0777, true);
|
|
@chmod($dir, 0777);
|
|
}
|
|
|
|
$fileName = (string)($options['filename'] ?? '');
|
|
if ($fileName === '') {
|
|
$safeName = preg_replace('/[^a-zA-Z0-9_\.\-]/', '_', $name) ?: 'debug';
|
|
$fileName = $safeName . '.log';
|
|
} else {
|
|
$fileName = basename($fileName);
|
|
}
|
|
|
|
$asJson = $options['json'] ?? (is_array($payload) || is_object($payload));
|
|
if ($asJson) {
|
|
$line = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
} else {
|
|
$line = is_string($payload) ? $payload : (string)$payload;
|
|
}
|
|
|
|
if (($options['newline'] ?? true) && $line !== '' && substr($line, -1) !== "\n") {
|
|
$line .= "\n";
|
|
}
|
|
|
|
$path = rtrim($dir, '/\\') . '/' . $fileName;
|
|
$flags = ($options['append'] ?? false) ? FILE_APPEND : 0;
|
|
$result = @file_put_contents($path, $line, $flags | LOCK_EX);
|
|
if ($result === false) {
|
|
return false;
|
|
}
|
|
@chmod($path, 0666);
|
|
return true;
|
|
}
|
|
|
|
function helper_asset_version(): ?string
|
|
{
|
|
if (isset($GLOBALS['layoutContext']['asset_version'])) {
|
|
return (string)$GLOBALS['layoutContext']['asset_version'];
|
|
}
|
|
if (defined('ASSET_VERSION')) {
|
|
return (string)ASSET_VERSION;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function helper_append_version(string $url, ?string $version = null): string
|
|
{
|
|
if ($version === null) {
|
|
$version = helper_asset_version();
|
|
}
|
|
if ($version === null || $version === '') {
|
|
return $url;
|
|
}
|
|
if (preg_match('/[?&]v=/', $url)) {
|
|
return $url;
|
|
}
|
|
return $url . (strpos($url, '?') === false ? '?' : '&') . 'v=' . rawurlencode($version);
|
|
}
|
|
|
|
function tpl_add_script(
|
|
string $src,
|
|
string $pos = 'footer',
|
|
bool $defer = false,
|
|
bool $async = false,
|
|
string $type = '',
|
|
?string $version = null,
|
|
bool $module = false
|
|
): void {
|
|
$data = [
|
|
'src' => helper_append_version($src, $version),
|
|
'defer' => $defer,
|
|
'async' => $async,
|
|
'type' => $type,
|
|
'module' => $module,
|
|
];
|
|
|
|
if ($pos === 'header') {
|
|
$GLOBALS['page_header_scripts'][] = $data;
|
|
} else {
|
|
$GLOBALS['page_footer_scripts'][] = $data;
|
|
}
|
|
}
|
|
|
|
function tpl_add_style(
|
|
string $href,
|
|
string $pos = 'header',
|
|
string $priority = 'normal',
|
|
?string $version = null,
|
|
string $media = 'all'
|
|
): void {
|
|
$GLOBALS['page_styles'][] = [
|
|
'href' => helper_append_version($href, $version),
|
|
'pos' => $pos,
|
|
'priority' => $priority,
|
|
'media' => $media,
|
|
];
|
|
}
|
|
|
|
function tpl(string $file, string $type = 'structure', string $site = 'admin'): void
|
|
{
|
|
$base = __DIR__ . '/../partials/';
|
|
|
|
$safe = static function ($value) {
|
|
return preg_match('/^[a-zA-Z0-9_\-]+$/', $value) === 1;
|
|
};
|
|
|
|
if (!$safe($file) || !$safe($type) || !$safe($site)) {
|
|
echo "<!-- tpl(): Ungültiger Parameter -->";
|
|
return;
|
|
}
|
|
|
|
$path = $type === 'landing'
|
|
? $base . "landingpage/{$site}/{$file}.php"
|
|
: $base . "structure/{$file}.php";
|
|
|
|
extract($GLOBALS, EXTR_SKIP);
|
|
|
|
if (is_file($path)) {
|
|
include $path;
|
|
} else {
|
|
echo "<!-- tpl(): Datei nicht gefunden: {$path} -->";
|
|
}
|
|
}
|
|
|
|
function tpl_render_scripts(?array $scripts = null, string $pos = 'footer'): void
|
|
{
|
|
if ($scripts === null) {
|
|
$scripts = $pos === 'header'
|
|
? ($GLOBALS['page_header_scripts'] ?? [])
|
|
: ($GLOBALS['page_footer_scripts'] ?? []);
|
|
}
|
|
|
|
foreach ($scripts as $script) {
|
|
if (empty($script['src'])) {
|
|
continue;
|
|
}
|
|
$attrs = [];
|
|
if (!empty($script['module'])) {
|
|
$attrs[] = 'type="module"';
|
|
} elseif (!empty($script['type'])) {
|
|
$attrs[] = 'type="' . htmlspecialchars((string)$script['type'], ENT_QUOTES) . '"';
|
|
}
|
|
if (!empty($script['defer'])) {
|
|
$attrs[] = 'defer';
|
|
}
|
|
if (!empty($script['async'])) {
|
|
$attrs[] = 'async';
|
|
}
|
|
$attrString = '';
|
|
if (!empty($attrs)) {
|
|
$attrString = ' ' . implode(' ', $attrs);
|
|
}
|
|
echo '<script src="' . htmlspecialchars($script['src'], ENT_QUOTES) . '"' . $attrString . '></script>' . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
function tpl_render_styles(?array $styles = null, string $pos = 'header'): void
|
|
{
|
|
if ($styles === null) {
|
|
$styles = array_filter(
|
|
$GLOBALS['page_styles'] ?? [],
|
|
static fn ($style) => ($style['pos'] ?? 'header') === $pos
|
|
);
|
|
}
|
|
|
|
if (!$styles) {
|
|
return;
|
|
}
|
|
|
|
$priorityOrder = ['critical' => 0, 'high' => 1, 'normal' => 2, 'low' => 3];
|
|
usort($styles, static function ($a, $b) use ($priorityOrder) {
|
|
$aPriority = strtolower($a['priority'] ?? 'normal');
|
|
$bPriority = strtolower($b['priority'] ?? 'normal');
|
|
$aScore = $priorityOrder[$aPriority] ?? $priorityOrder['normal'];
|
|
$bScore = $priorityOrder[$bPriority] ?? $priorityOrder['normal'];
|
|
return $aScore <=> $bScore;
|
|
});
|
|
|
|
foreach ($styles as $style) {
|
|
if (empty($style['href'])) {
|
|
continue;
|
|
}
|
|
$media = trim((string)($style['media'] ?? ''));
|
|
$mediaAttr = $media && strtolower($media) !== 'all' ? ' media="' . htmlspecialchars($media, ENT_QUOTES) . '"' : '';
|
|
echo '<link rel="stylesheet" href="' . htmlspecialchars($style['href'], ENT_QUOTES) . '"' . $mediaAttr . '>' . PHP_EOL;
|
|
}
|
|
}
|