280 lines
6.9 KiB
PHP
280 lines
6.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Globale Hilfsfunktionen. Diese Datei wird beim Start geladen.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Lädt eine .env-Datei (KEY=VALUE) in die Umgebung, ohne vorhandene Werte zu überschreiben. */
|
||
|
|
function load_env_file(string $path): void
|
||
|
|
{
|
||
|
|
if (!is_file($path)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach (file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
|
||
|
|
$line = trim($line);
|
||
|
|
|
||
|
|
if ($line === '' || str_starts_with($line, '#')) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!str_contains($line, '=')) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
[$key, $value] = explode('=', $line, 2);
|
||
|
|
$key = trim($key);
|
||
|
|
$value = trim($value);
|
||
|
|
|
||
|
|
if (strlen($value) >= 2) {
|
||
|
|
$first = $value[0];
|
||
|
|
$last = $value[strlen($value) - 1];
|
||
|
|
if (($first === '"' && $last === '"') || ($first === "'" && $last === "'")) {
|
||
|
|
$value = substr($value, 1, -1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($key !== '' && getenv($key) === false) {
|
||
|
|
putenv("$key=$value");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Datenbank-Zugriff (Singleton). */
|
||
|
|
function db(): \App\Core\Database
|
||
|
|
{
|
||
|
|
return \App\Core\Database::get();
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Zugriff auf einen Konfigurationswert mit Punktnotation, z. B. config('db.host'). */
|
||
|
|
function config(string $key, $default = null)
|
||
|
|
{
|
||
|
|
static $config = null;
|
||
|
|
|
||
|
|
if ($config === null) {
|
||
|
|
$config = require dirname(__DIR__) . '/src/config.php';
|
||
|
|
}
|
||
|
|
|
||
|
|
$segments = explode('.', $key);
|
||
|
|
$value = $config;
|
||
|
|
|
||
|
|
foreach ($segments as $segment) {
|
||
|
|
if (!is_array($value) || !array_key_exists($segment, $value)) {
|
||
|
|
return $default;
|
||
|
|
}
|
||
|
|
$value = $value[$segment];
|
||
|
|
}
|
||
|
|
|
||
|
|
return $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** HTML-Escaping zur Ausgabe in Templates. */
|
||
|
|
function e($value): string
|
||
|
|
{
|
||
|
|
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Ein Request-Eingabewert (POST hat Vorrang vor GET). */
|
||
|
|
function input(string $key, $default = null)
|
||
|
|
{
|
||
|
|
return $_POST[$key] ?? $_GET[$key] ?? $default;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Alter Eingabewert nach fehlgeschlagener Validierung (für Formulare). */
|
||
|
|
function old(string $key, $default = null)
|
||
|
|
{
|
||
|
|
return $_SESSION['_old'][$key] ?? $default;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Gesammelte Validierungsfehler für ein Feld. */
|
||
|
|
function errors(): array
|
||
|
|
{
|
||
|
|
return $_SESSION['_errors'] ?? [];
|
||
|
|
}
|
||
|
|
|
||
|
|
function error(string $field): ?string
|
||
|
|
{
|
||
|
|
return $_SESSION['_errors'][$field][0] ?? null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Kurzlebige Flash-Meldung lesen (z. B. 'status'). */
|
||
|
|
function session_flash(string $key, $default = null)
|
||
|
|
{
|
||
|
|
return $_SESSION['_flash'][$key] ?? $default;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Redirect und Skript beenden. */
|
||
|
|
function redirect(string $url): void
|
||
|
|
{
|
||
|
|
header('Location: ' . $url);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Zurück zur vorherigen Seite (fallback auf '/'). */
|
||
|
|
function back(): void
|
||
|
|
{
|
||
|
|
redirect($_SERVER['HTTP_REFERER'] ?? '/');
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Render einer View mit Layout. */
|
||
|
|
function view(string $name, array $data = []): void
|
||
|
|
{
|
||
|
|
echo \App\Core\View::render($name, $data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** CSRF-Token erzeugen. */
|
||
|
|
function csrf_token(): string
|
||
|
|
{
|
||
|
|
if (empty($_SESSION['_csrf'])) {
|
||
|
|
$_SESSION['_csrf'] = bin2hex(random_bytes(32));
|
||
|
|
}
|
||
|
|
|
||
|
|
return $_SESSION['_csrf'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/** CSRF-Token überprüfen (bei POST/PUT/DELETE). */
|
||
|
|
function verify_csrf(): void
|
||
|
|
{
|
||
|
|
if (empty($_SESSION['_csrf'])) {
|
||
|
|
http_response_code(419);
|
||
|
|
exit('CSRF-Token fehlt (Seite erneut laden).');
|
||
|
|
}
|
||
|
|
|
||
|
|
$token = $_POST['_token'] ?? ($_SERVER['HTTP_X_CSRF_TOKEN'] ?? '');
|
||
|
|
|
||
|
|
if (!hash_equals($_SESSION['_csrf'], $token)) {
|
||
|
|
http_response_code(419);
|
||
|
|
exit('Ungültiges CSRF-Token.');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/** CSRF-Hidden-Field ausgeben (für Formulare). */
|
||
|
|
function csrf_field(): string
|
||
|
|
{
|
||
|
|
return '<input type="hidden" name="_token" value="' . e(csrf_token()) . '">';
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Aktuell angemeldeter Benutzer (oder null). */
|
||
|
|
function user(): ?\App\Models\User
|
||
|
|
{
|
||
|
|
return \App\Core\Auth::user();
|
||
|
|
}
|
||
|
|
|
||
|
|
/** true, wenn ein Benutzer angemeldet ist. */
|
||
|
|
function auth_check(): bool
|
||
|
|
{
|
||
|
|
return \App\Core\Auth::check();
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Aktueller Zeitpunkt als 'Y-m-d H:i:s'. */
|
||
|
|
function now(): string
|
||
|
|
{
|
||
|
|
return date('Y-m-d H:i:s');
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Heutiges Datum als 'Y-m-d'. */
|
||
|
|
function today(): string
|
||
|
|
{
|
||
|
|
return date('Y-m-d');
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Asset-Pfad unter /. */
|
||
|
|
function asset(string $path): string
|
||
|
|
{
|
||
|
|
return '/' . ltrim($path, '/');
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Aktueller Anfrage-Pfad (ohne Query-String). */
|
||
|
|
function current_path(): string
|
||
|
|
{
|
||
|
|
return parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Absoluter Pfad in storage/. */
|
||
|
|
function storage_path(string $rel = ''): string
|
||
|
|
{
|
||
|
|
return config('paths.storage') . ($rel ? '/' . ltrim($rel, '/') : '');
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Absoluter Pfad in public/. */
|
||
|
|
function public_path(string $rel = ''): string
|
||
|
|
{
|
||
|
|
return config('paths.public') . ($rel ? '/' . ltrim($rel, '/') : '');
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Wandelt '2021-09-03' in '03.09.2021' um. */
|
||
|
|
function german_date(?string $engDate): string
|
||
|
|
{
|
||
|
|
if (!$engDate || !str_contains($engDate, '-')) {
|
||
|
|
return (string) $engDate;
|
||
|
|
}
|
||
|
|
|
||
|
|
[$year, $month, $day] = explode('-', $engDate);
|
||
|
|
|
||
|
|
return $day . '.' . $month . '.' . $year;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Wandelt '03.09.2021' (oder '03.09.2021 18:00') in '2021-09-03' um. */
|
||
|
|
function db_date(?string $deDate): string
|
||
|
|
{
|
||
|
|
if (!$deDate) {
|
||
|
|
return '';
|
||
|
|
}
|
||
|
|
|
||
|
|
$datePart = explode(' ', trim($deDate))[0];
|
||
|
|
$parts = explode('.', $datePart);
|
||
|
|
|
||
|
|
if (count($parts) !== 3) {
|
||
|
|
return (string) $deDate;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $parts[2] . '-' . $parts[1] . '-' . $parts[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Einfache Validierung: validate(['name' => 'required|min:3', ...]). */
|
||
|
|
function validate(array $rules): array
|
||
|
|
{
|
||
|
|
$errors = [];
|
||
|
|
|
||
|
|
foreach ($rules as $field => $ruleString) {
|
||
|
|
$value = $_POST[$field] ?? null;
|
||
|
|
|
||
|
|
foreach (explode('|', $ruleString) as $rule) {
|
||
|
|
if ($rule === 'required' && ($value === null || trim((string) $value) === '')) {
|
||
|
|
$errors[$field][] = 'Das Feld ' . $field . ' ist erforderlich.';
|
||
|
|
}
|
||
|
|
|
||
|
|
if (str_starts_with($rule, 'min:')) {
|
||
|
|
$min = (int) substr($rule, 4);
|
||
|
|
if (is_string($value) && mb_strlen($value) < $min) {
|
||
|
|
$errors[$field][] = 'Mindestens ' . $min . ' Zeichen erforderlich.';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($rule === 'email' && $value !== null && $value !== '' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||
|
|
$errors[$field][] = 'Bitte eine gültige E-Mail-Adresse angeben.';
|
||
|
|
}
|
||
|
|
|
||
|
|
if (str_starts_with($rule, 'mimes:')) {
|
||
|
|
$allowed = explode(',', substr($rule, 6));
|
||
|
|
if (!empty($_FILES[$field]) && !empty($_FILES[$field]['name'])) {
|
||
|
|
$ext = strtolower(pathinfo($_FILES[$field]['name'], PATHINFO_EXTENSION));
|
||
|
|
if (!in_array($ext, $allowed, true)) {
|
||
|
|
$errors[$field][] = 'Erlaubte Dateitypen: ' . implode(', ', $allowed) . '.';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($errors !== []) {
|
||
|
|
$_SESSION['_errors'] = $errors;
|
||
|
|
$_SESSION['_old'] = $_POST;
|
||
|
|
back();
|
||
|
|
}
|
||
|
|
|
||
|
|
return [];
|
||
|
|
}
|