Files
summitwave_landing/src/manual/auth.php
mheuer dc7dcdd650 feat: remove deprecated AI-optimized deployment script
- Delete deploy_to_hetzner_AI.sh which was superseded by main deployment script
- Remove cache-busting implementation for index_noSIG_AIopt.html
- Clean up unused SFTP deployment workflow with sshpass authentication
2025-12-15 17:03:08 +01:00

88 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
session_start();
const SW_MANUAL_LOG_FILE = __DIR__ . '/logs/access.log';
function sw_load_users(): array {
$cfgPath = __DIR__ . '/auth.config.php';
if (!file_exists($cfgPath)) {
return [];
}
$cfg = require $cfgPath;
return $cfg['users'] ?? [];
}
function sw_verify_login(string $username, string $password): bool {
$users = sw_load_users();
if (!isset($users[$username])) {
return false;
}
$expected = (string)$users[$username];
return hash_equals($expected, $password);
}
function sw_current_user(): ?string {
return $_SESSION['sw_manual_user'] ?? null;
}
function sw_require_login(): void {
if (!sw_current_user()) {
header('Location: login.php');
exit;
}
}
function sw_record_login(string $username): void {
$dir = dirname(SW_MANUAL_LOG_FILE);
if (!is_dir($dir)) {
@mkdir($dir, 0775, true);
}
$ts = (new DateTimeImmutable('now', new DateTimeZone('Europe/Berlin')))->format(DateTimeInterface::ATOM);
$ip = $_SERVER['REMOTE_ADDR'] ?? '-';
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '-';
$line = $ts . ';' . $username . ';' . $ip . ';' . str_replace("\n", ' ', $ua) . "\n";
@file_put_contents(SW_MANUAL_LOG_FILE, $line, FILE_APPEND | LOCK_EX);
}
function sw_last_login_for_user(string $username): ?DateTimeImmutable {
if (!file_exists(SW_MANUAL_LOG_FILE)) {
return null;
}
$fh = @fopen(SW_MANUAL_LOG_FILE, 'r');
if (!$fh) {
return null;
}
$last = null;
while (($line = fgets($fh)) !== false) {
$parts = explode(';', trim($line));
if (count($parts) < 2) {
continue;
}
[$ts, $user] = $parts;
if ($user !== $username) {
continue;
}
try {
$dt = new DateTimeImmutable($ts);
if ($last === null || $dt > $last) {
$last = $dt;
}
} catch (Exception $e) {
continue;
}
}
fclose($fh);
return $last;
}
function sw_format_dt(?DateTimeImmutable $dt): string {
if ($dt === null) {
return '';
}
return $dt->setTimezone(new DateTimeZone('Europe/Berlin'))->format('d.m.Y, H:i \U\h\r');
}