|
|
|
@ -17,30 +17,55 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
exit;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$username = trim($_POST['username'] ?? '');
|
|
|
|
if (($_POST['action'] ?? '') === 'register' && ALLOW_REGISTRATION) {
|
|
|
|
$password = (string) ($_POST['password'] ?? '');
|
|
|
|
$regUsername = trim((string) ($_POST['register_username'] ?? ''));
|
|
|
|
|
|
|
|
$regEmail = trim((string) ($_POST['register_email'] ?? ''));
|
|
|
|
|
|
|
|
$regPassword = (string) ($_POST['register_password'] ?? '');
|
|
|
|
|
|
|
|
$regConfirmPassword = (string) ($_POST['register_confirm_password'] ?? '');
|
|
|
|
|
|
|
|
|
|
|
|
if ($username === '' || $password === '') {
|
|
|
|
if ($regUsername === '' || $regPassword === '' || $regConfirmPassword === '') {
|
|
|
|
$errors[] = 'Please enter both username and password.';
|
|
|
|
$errors[] = 'Please enter a username and password for registration.';
|
|
|
|
} else {
|
|
|
|
} elseif ($regPassword !== $regConfirmPassword) {
|
|
|
|
$user = findUserByUsername($username);
|
|
|
|
$errors[] = 'Passwords do not match.';
|
|
|
|
if ($user && verifyPassword($password, $user['passwordHash'] ?? '')) {
|
|
|
|
} 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'] = [
|
|
|
|
$_SESSION['auth_user'] = [
|
|
|
|
'username' => $user['username'],
|
|
|
|
'username' => $regUsername,
|
|
|
|
'role' => $user['role'] ?? 'user'
|
|
|
|
'role' => 'user'
|
|
|
|
];
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
if (!empty($_POST['remember'])) {
|
|
|
|
|
|
|
|
setRememberMeCookie($user['username']);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
clearRememberMeCookie($user['username']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
header('Location: index.php');
|
|
|
|
header('Location: index.php');
|
|
|
|
exit;
|
|
|
|
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']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$errors[] = 'Invalid username or password.';
|
|
|
|
header('Location: index.php');
|
|
|
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$errors[] = 'Invalid username or password.';
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -87,6 +112,7 @@ if (empty(loadUsers())) {
|
|
|
|
</form>
|
|
|
|
</form>
|
|
|
|
<?php endif; ?>
|
|
|
|
<?php endif; ?>
|
|
|
|
<form method="post">
|
|
|
|
<form method="post">
|
|
|
|
|
|
|
|
<input type="hidden" name="action" value="login">
|
|
|
|
<label for="username">Username</label>
|
|
|
|
<label for="username">Username</label>
|
|
|
|
<input id="username" name="username" required>
|
|
|
|
<input id="username" name="username" required>
|
|
|
|
|
|
|
|
|
|
|
|
@ -100,6 +126,12 @@ if (empty(loadUsers())) {
|
|
|
|
|
|
|
|
|
|
|
|
<button type="submit">Sign in</button>
|
|
|
|
<button type="submit">Sign in</button>
|
|
|
|
</form>
|
|
|
|
</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)): ?>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
|
|
<div class="error">
|
|
|
|
<div class="error">
|
|
|
|
<?php echo htmlspecialchars(implode('<br>', $errors)); ?>
|
|
|
|
<?php echo htmlspecialchars(implode('<br>', $errors)); ?>
|
|
|
|
|