Initial Kaffeekasse SaaS restart
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
use App\Core\Csrf;
|
||||
use App\Core\Session;
|
||||
|
||||
function e(?string $value): string
|
||||
{
|
||||
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
function current_path(): string
|
||||
{
|
||||
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
|
||||
|
||||
return is_string($uri) ? $uri : '/';
|
||||
}
|
||||
|
||||
function asset_url(string $path): string
|
||||
{
|
||||
return '/assets/' . ltrim($path, '/');
|
||||
}
|
||||
|
||||
function old(string $key, mixed $default = ''): mixed
|
||||
{
|
||||
return $_SESSION['_old'][$key] ?? $default;
|
||||
}
|
||||
|
||||
function remember_old_input(array $input): void
|
||||
{
|
||||
foreach (['password', 'admin_password', 'login_password', 'owner_password', 'db_pass'] as $sensitiveKey) {
|
||||
if (array_key_exists($sensitiveKey, $input)) {
|
||||
unset($input[$sensitiveKey]);
|
||||
}
|
||||
}
|
||||
|
||||
$_SESSION['_old'] = $input;
|
||||
}
|
||||
|
||||
function clear_old_input(): void
|
||||
{
|
||||
unset($_SESSION['_old']);
|
||||
}
|
||||
|
||||
function csrf_field(Csrf $csrf): string
|
||||
{
|
||||
return '<input type="hidden" name="_token" value="' . e($csrf->token()) . '">';
|
||||
}
|
||||
|
||||
function flash(Session $session, string $key, mixed $default = null): mixed
|
||||
{
|
||||
return $session->pullFlash($key, $default);
|
||||
}
|
||||
|
||||
function tenant_url(string $tenantSlug, string $path = ''): string
|
||||
{
|
||||
$suffix = $path === '' ? '' : '/' . ltrim($path, '/');
|
||||
|
||||
return '/t/' . rawurlencode($tenantSlug) . $suffix;
|
||||
}
|
||||
|
||||
function money_from_cents(int $amountCents): string
|
||||
{
|
||||
return number_format($amountCents / 100, 2, ',', '.') . ' EUR';
|
||||
}
|
||||
|
||||
function secure_password_hash(string $password): string
|
||||
{
|
||||
$algo = defined('PASSWORD_ARGON2ID') ? PASSWORD_ARGON2ID : PASSWORD_BCRYPT;
|
||||
|
||||
return password_hash($password, $algo);
|
||||
}
|
||||
|
||||
function normalize_datetime_input(?string $value): string
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
|
||||
if ($value === '') {
|
||||
return gmdate('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
$normalized = str_replace('T', ' ', $value);
|
||||
|
||||
if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/', $normalized)) {
|
||||
return $normalized . ':00';
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
function request_origin_matches_host(): bool
|
||||
{
|
||||
$host = $_SERVER['HTTP_HOST'] ?? '';
|
||||
|
||||
if ($host === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (['HTTP_ORIGIN', 'HTTP_REFERER'] as $header) {
|
||||
if (!empty($_SERVER[$header])) {
|
||||
$originHost = parse_url((string) $_SERVER[$header], PHP_URL_HOST);
|
||||
|
||||
if (is_string($originHost) && $originHost !== '' && !hash_equals($host, $originHost)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user