New users no longer get a manually typed password: admins get a random one-time password shown once after creation, and the account is flagged must_change_password. require_login() redirects anyone with that flag to the new change_password.php page (except that page and logout) until they set their own password. Admins can also toggle the flag when editing an existing user to force a re-change. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
function e(?string $value): string
|
|
{
|
|
return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
function redirect(string $path): void
|
|
{
|
|
header('Location: ' . $path);
|
|
exit;
|
|
}
|
|
|
|
function csrf_token(): string
|
|
{
|
|
if (empty($_SESSION['csrf_token'])) {
|
|
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
|
|
}
|
|
return $_SESSION['csrf_token'];
|
|
}
|
|
|
|
function csrf_field(): string
|
|
{
|
|
return '<input type="hidden" name="csrf_token" value="' . e(csrf_token()) . '">';
|
|
}
|
|
|
|
function verify_csrf(): void
|
|
{
|
|
$token = $_POST['csrf_token'] ?? '';
|
|
if (!is_string($token) || !hash_equals($_SESSION['csrf_token'] ?? '', $token)) {
|
|
http_response_code(400);
|
|
exit('Ungueltige Anfrage (CSRF-Token fehlt oder ist abgelaufen). Bitte Seite neu laden und erneut versuchen.');
|
|
}
|
|
}
|
|
|
|
function set_flash(string $type, string $message): void
|
|
{
|
|
$_SESSION['flash'][] = ['type' => $type, 'message' => $message];
|
|
}
|
|
|
|
function get_flashes(): array
|
|
{
|
|
$flashes = $_SESSION['flash'] ?? [];
|
|
unset($_SESSION['flash']);
|
|
return $flashes;
|
|
}
|
|
|
|
function generate_temp_password(int $length = 12): string
|
|
{
|
|
// Ohne visuell mehrdeutige Zeichen (0/O, 1/l/I, ...), damit sich das
|
|
// Einmalpasswort leicht von Hand abtippen laesst.
|
|
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789';
|
|
$max = strlen($chars) - 1;
|
|
$password = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$password .= $chars[random_int(0, $max)];
|
|
}
|
|
return $password;
|
|
}
|