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.

123 lines
5.4 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 (!ALLOW_REGISTRATION) {
$errors[] = 'Registration is currently disabled.';
} else {
$action = $_POST['action'] ?? 'start';
if ($action === 'verify') {
$pending = getPendingRegistration();
$code = trim((string) ($_POST['verification_code'] ?? ''));
if ($pending === null) {
$errors[] = 'No pending registration found. Please start again.';
} elseif ($code === '' || $code !== (string) ($pending['code'] ?? '')) {
$errors[] = 'The verification code is incorrect.';
} else {
if (createUser($pending['username'], $pending['password'], 'user', $pending['email'])) {
clearPendingRegistration();
$_SESSION['auth_user'] = [
'username' => $pending['username'],
'role' => 'user'
];
header('Location: index.php');
exit;
}
$errors[] = 'Registration failed. The username or email may already be in use.';
}
} else {
$username = trim((string) ($_POST['username'] ?? ''));
$email = trim((string) ($_POST['email'] ?? ''));
$password = (string) ($_POST['password'] ?? '');
$confirmPassword = (string) ($_POST['confirm_password'] ?? '');
if ($username === '' || $email === '' || $password === '' || $confirmPassword === '') {
$errors[] = 'Please fill in all fields.';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Please enter a valid email address.';
} elseif ($password !== $confirmPassword) {
$errors[] = 'Passwords do not match.';
} elseif (findUserByUsername($username) !== null || findUserByEmail($email) !== null) {
$errors[] = 'That username or email is already in use.';
} else {
$code = generateRegistrationCode();
if (savePendingRegistration($username, $email, $password, $code) && sendRegistrationVerificationEmail($email, $code)) {
$message = 'A verification code has been sent to ' . htmlspecialchars($email) . '. Please enter it below.';
} else {
$errors[] = 'Unable to start registration. Please check that your email address is valid and the mail server is configured.';
}
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - 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; }
.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>Create account</h1>
<?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; ?>
<?php if (getPendingRegistration() !== null): ?>
<form method="post">
<input type="hidden" name="action" value="verify">
<label for="verification_code">Verification code</label>
<input id="verification_code" name="verification_code" required>
<button type="submit">Confirm registration</button>
</form>
<?php else: ?>
<form method="post">
<input type="hidden" name="action" value="start">
<label for="username">Username</label>
<input id="username" name="username" required>
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<label for="password">Password</label>
<input id="password" name="password" type="password" required>
<label for="confirm_password">Confirm password</label>
<input id="confirm_password" name="confirm_password" type="password" required>
<button type="submit">Register</button>
</form>
<?php endif; ?>
<div class="small"><a href="login.php" style="color:#89b4fa;">Back to login</a></div>
</div>
</body>
</html>