Kometenseite erstmal fertig
This commit is contained in:
@@ -1,6 +1,62 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
function isLocalDevelopmentRequest(): bool
|
||||
{
|
||||
$host = strtolower((string) ($_SERVER['HTTP_HOST'] ?? ''));
|
||||
$host = preg_replace('/:\d+$/', '', $host) ?? $host;
|
||||
$remoteAddr = strtolower((string) ($_SERVER['REMOTE_ADDR'] ?? ''));
|
||||
|
||||
$localHosts = ['localhost', '127.0.0.1', '::1'];
|
||||
$localAddresses = ['127.0.0.1', '::1', ''];
|
||||
|
||||
return in_array($host, $localHosts, true) && in_array($remoteAddr, $localAddresses, true);
|
||||
}
|
||||
|
||||
function applyLocalDevLoginBypass(): void
|
||||
{
|
||||
if (!isLocalDevelopmentRequest()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((string) ($_GET['dev_logout'] ?? '') === '1') {
|
||||
unset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['role']);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($_SESSION['user_id']) || (string) ($_GET['dev_login'] ?? '') !== '1') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$cfg = require __DIR__ . '/../config/database.php';
|
||||
$pdo = new PDO(
|
||||
sprintf('mysql:host=%s;dbname=%s;charset=%s', $cfg['host'], $cfg['dbname'], $cfg['charset'] ?? 'utf8mb4'),
|
||||
$cfg['user'],
|
||||
$cfg['pass'],
|
||||
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]
|
||||
);
|
||||
|
||||
$stmt = $pdo->query("
|
||||
SELECT `id`, `username`, `role`
|
||||
FROM `app_users`
|
||||
WHERE `is_active` = 1
|
||||
AND `role` IN ('master', 'admin')
|
||||
ORDER BY CASE WHEN `role` = 'master' THEN 0 ELSE 1 END, `id` ASC
|
||||
LIMIT 1
|
||||
");
|
||||
$user = $stmt->fetch();
|
||||
|
||||
if (is_array($user)) {
|
||||
$_SESSION['user_id'] = (int) $user['id'];
|
||||
$_SESSION['username'] = (string) $user['username'];
|
||||
$_SESSION['role'] = (string) $user['role'];
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function currentUserRole(): string
|
||||
{
|
||||
return trim((string) ($_SESSION['role'] ?? ''));
|
||||
|
||||
Reference in New Issue
Block a user