weitere Bearibeitung

This commit is contained in:
2026-06-17 16:45:14 +02:00
parent 06645f1e9c
commit 1891ec0a51
38 changed files with 2720 additions and 109 deletions
+141 -2
View File
@@ -3,6 +3,32 @@
use App\Core\Csrf;
use App\Core\Session;
function base_path(): string
{
foreach ([
$_SERVER['KAFFEEKASSE_PROXY_PREFIX'] ?? null,
$_SERVER['HTTP_X_FORWARDED_PREFIX'] ?? null,
$_ENV['APP_BASE_PATH'] ?? null,
] as $prefix) {
$prefix = trim((string) $prefix);
if ($prefix !== '') {
return normalize_base_path($prefix);
}
}
return '';
}
function normalize_base_path(string $prefix): string
{
$path = parse_url($prefix, PHP_URL_PATH);
$path = is_string($path) ? $path : $prefix;
$path = '/' . trim($path, '/');
return $path === '/' ? '' : $path;
}
function e(?string $value): string
{
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
@@ -12,12 +38,120 @@ function current_path(): string
{
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
return is_string($uri) ? $uri : '/';
$path = is_string($uri) && $uri !== '' ? $uri : '/';
$basePath = base_path();
if ($basePath !== '' && ($path === $basePath || str_starts_with($path, $basePath . '/'))) {
$path = substr($path, strlen($basePath)) ?: '/';
}
$path = '/' . ltrim($path, '/');
return $path === '/' ? '/' : rtrim($path, '/');
}
function url(string $path = '/', array $query = []): string
{
$path = trim($path);
if ($path === '') {
$path = '/';
}
if (preg_match('#^(?:[a-z][a-z0-9+.-]*:)?//#i', $path) || str_starts_with($path, '#')) {
return append_query($path, $query);
}
$fragment = '';
$hashPosition = strpos($path, '#');
if ($hashPosition !== false) {
$fragment = substr($path, $hashPosition);
$path = substr($path, 0, $hashPosition);
}
$existingQuery = '';
$queryPosition = strpos($path, '?');
if ($queryPosition !== false) {
$existingQuery = substr($path, $queryPosition + 1);
$path = substr($path, 0, $queryPosition);
}
$targetPath = '/' . ltrim($path, '/');
$targetPath = $targetPath === '//' ? '/' : $targetPath;
$targetPath = $targetPath === '/' ? '/' : rtrim($targetPath, '/');
$queryString = build_query_string($existingQuery, $query);
$basePath = base_path();
if ($basePath !== '') {
return $basePath . ($targetPath === '/' ? '/' : $targetPath) . $queryString . $fragment;
}
return relative_url($targetPath) . $queryString . $fragment;
}
function append_query(string $url, array $query): string
{
if ($query === []) {
return $url;
}
return $url . (str_contains($url, '?') ? '&' : '?') . http_build_query($query, '', '&', PHP_QUERY_RFC3986);
}
function build_query_string(string $existingQuery, array $query): string
{
$parts = [];
if ($existingQuery !== '') {
$parts[] = $existingQuery;
}
if ($query !== []) {
$parts[] = http_build_query($query, '', '&', PHP_QUERY_RFC3986);
}
return $parts === [] ? '' : '?' . implode('&', $parts);
}
function relative_url(string $targetPath): string
{
$currentPath = current_path();
$fromSegments = path_segments($currentPath);
if (!str_ends_with($currentPath, '/')) {
array_pop($fromSegments);
}
$toSegments = path_segments($targetPath);
$common = 0;
$maxCommon = min(count($fromSegments), count($toSegments));
while ($common < $maxCommon && $fromSegments[$common] === $toSegments[$common]) {
$common++;
}
if ($toSegments !== [] && $common === count($toSegments) && $common === count($fromSegments)) {
return '../' . end($toSegments);
}
$relativeSegments = array_merge(
array_fill(0, count($fromSegments) - $common, '..'),
array_slice($toSegments, $common)
);
return $relativeSegments === [] ? '.' : implode('/', $relativeSegments);
}
function path_segments(string $path): array
{
$trimmed = trim($path, '/');
return $trimmed === '' ? [] : explode('/', $trimmed);
}
function asset_url(string $path): string
{
return '/assets/' . ltrim($path, '/');
return url('/assets/' . ltrim($path, '/'));
}
function old(string $key, mixed $default = ''): mixed
@@ -52,6 +186,11 @@ function flash(Session $session, string $key, mixed $default = null): mixed
}
function tenant_url(string $tenantSlug, string $path = ''): string
{
return url(tenant_path($tenantSlug, $path));
}
function tenant_path(string $tenantSlug, string $path = ''): string
{
$suffix = $path === '' ? '' : '/' . ltrim($path, '/');