You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
5.7 KiB
148 lines
5.7 KiB
<?php
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
require_once __DIR__ . '/default.php';
|
|
require_once __DIR__ . '/auth.php';
|
|
|
|
$errors = [];
|
|
$message = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (!empty($_POST['guest']) && isGuestModeEnabled()) {
|
|
loginAsGuest();
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
if (($_POST['action'] ?? '') === 'register' && ALLOW_REGISTRATION) {
|
|
$regUsername = trim((string) ($_POST['register_username'] ?? ''));
|
|
$regEmail = trim((string) ($_POST['register_email'] ?? ''));
|
|
$regPassword = (string) ($_POST['register_password'] ?? '');
|
|
$regConfirmPassword = (string) ($_POST['register_confirm_password'] ?? '');
|
|
|
|
if ($regUsername === '' || $regPassword === '' || $regConfirmPassword === '') {
|
|
$errors[] = 'Please enter a username and password for registration.';
|
|
} elseif ($regPassword !== $regConfirmPassword) {
|
|
$errors[] = 'Passwords do not match.';
|
|
} elseif (filter_var($regEmail, FILTER_VALIDATE_EMAIL) === false && $regEmail !== '') {
|
|
$errors[] = 'Please enter a valid email address.';
|
|
} elseif (createUser($regUsername, $regPassword, 'user', $regEmail)) {
|
|
$_SESSION['auth_user'] = [
|
|
'username' => $regUsername,
|
|
'role' => 'user'
|
|
];
|
|
|
|
header('Location: index.php');
|
|
exit;
|
|
} else {
|
|
$errors[] = 'Registration failed. The username or email may already be in use.';
|
|
}
|
|
} else {
|
|
$username = trim($_POST['username'] ?? '');
|
|
$password = (string) ($_POST['password'] ?? '');
|
|
|
|
if ($username === '' || $password === '') {
|
|
$errors[] = 'Please enter both username and password.';
|
|
} else {
|
|
$user = findUserByUsername($username);
|
|
if ($user && verifyPassword($password, $user['passwordHash'] ?? '')) {
|
|
$_SESSION['auth_user'] = [
|
|
'username' => $user['username'],
|
|
'role' => $user['role'] ?? 'user'
|
|
];
|
|
|
|
if (!empty($_POST['remember'])) {
|
|
setRememberMeCookie($user['username']);
|
|
} else {
|
|
clearRememberMeCookie($user['username']);
|
|
}
|
|
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
$errors[] = 'Invalid username or password.';
|
|
}
|
|
}
|
|
}
|
|
|
|
if (isLoggedIn() && !isGuestSession()) {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
ensureConfigDirectory();
|
|
|
|
if (empty(loadUsers())) {
|
|
$created = createUser('admin', 'changeme', 'admin', 'admin@example.com');
|
|
if ($created) {
|
|
$message = 'Default admin account created. Username: admin / Password: changeme';
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Login - KaI</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; background: #1e1e2e; color: #cdd6f4; display: flex; align-items: center; justify-content: center; min-height: 100vh; margin: 0; }
|
|
.card { background: #11111b; padding: 24px; border-radius: 10px; width: 360px; box-shadow: 0 10px 30px rgba(0,0,0,0.3); }
|
|
h1 { margin-top: 0; font-size: 1.4rem; }
|
|
label { display: block; margin-top: 12px; font-weight: bold; }
|
|
input { width: 100%; padding: 10px; margin-top: 6px; border-radius: 6px; border: 1px solid #45475a; background: #313244; color: #fff; }
|
|
button { margin-top: 16px; width: 100%; padding: 10px; border: none; border-radius: 6px; background: #89b4fa; color: #11111b; font-weight: bold; cursor: pointer; }
|
|
.guest-btn { background: #a6e3a1; color: #11111b; }
|
|
.error { color: #f38ba8; margin-top: 12px; }
|
|
.message { color: #a6e3a1; margin-top: 12px; }
|
|
.small { font-size: 0.9rem; color: #999; margin-top: 12px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="card">
|
|
<h1>KaI Login</h1>
|
|
<?php if (isGuestModeEnabled()): ?>
|
|
<form method="post">
|
|
<input type="hidden" name="guest" value="1">
|
|
<button type="submit" class="guest-btn">Continue as Guest</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
<form method="post">
|
|
<input type="hidden" name="action" value="login">
|
|
<label for="username">Username</label>
|
|
<input id="username" name="username" required>
|
|
|
|
<label for="password">Password</label>
|
|
<input id="password" name="password" type="password" required>
|
|
|
|
<label style="font-weight:normal; margin-top:12px;">
|
|
<input type="checkbox" name="remember" value="1">
|
|
Remember me on this browser for 30 days
|
|
</label>
|
|
|
|
<button type="submit">Sign in</button>
|
|
</form>
|
|
<?php if (ALLOW_REGISTRATION): ?>
|
|
<div class="small" style="margin-top:16px;">No account yet?</div>
|
|
<div style="margin-top:8px;">
|
|
<a href="register.php" style="color:#89b4fa; font-weight:bold; text-decoration:none;">Create a new account</a>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="error">
|
|
<?php echo htmlspecialchars(implode('<br>', $errors)); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if ($message !== ''): ?>
|
|
<div class="message"><?php echo htmlspecialchars($message); ?></div>
|
|
<?php endif; ?>
|
|
<div class="small">Default admin account is created on first run: admin / changeme</div>
|
|
<div class="small"><a href="forgot.php" style="color:#89b4fa;">Forgot password?</a></div>
|
|
</div>
|
|
</body>
|
|
</html>
|