diff --git a/auth.php b/auth.php index 9439184..7526790 100644 --- a/auth.php +++ b/auth.php @@ -384,6 +384,42 @@ function findUserByEmail($email) { return null; } +function generateRegistrationCode() { + return str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT); +} + +function savePendingRegistration($username, $email, $password, $code) { + if (session_status() === PHP_SESSION_NONE) { + session_start(); + } + + $_SESSION['pending_registration'] = [ + 'username' => trim((string) $username), + 'email' => trim((string) $email), + 'password' => (string) $password, + 'code' => (string) $code, + 'createdAt' => time(), + ]; + + return true; +} + +function getPendingRegistration() { + if (session_status() === PHP_SESSION_NONE) { + session_start(); + } + + return $_SESSION['pending_registration'] ?? null; +} + +function clearPendingRegistration() { + if (session_status() === PHP_SESSION_NONE) { + session_start(); + } + + unset($_SESSION['pending_registration']); +} + function createUser($username, $password, $role = 'user', $email = '') { $username = trim((string) $username); $email = trim((string) $email); diff --git a/default.php b/default.php index 0de2b0f..8aeb983 100644 --- a/default.php +++ b/default.php @@ -5,6 +5,7 @@ define('OLLAMA_URL', 'http://127.0.0.1:11434'); define('GUEST_USER_ENABLED', true); define('GUEST_USERNAME', 'guest'); define('DEFAULT_MODEL', 'llama3.2:latest'); +define('ALLOW_REGISTRATION', true); define('MAIL_FROM', getenv('MAIL_FROM') ?: 'noreply@kai.local'); define('MAIL_FROM_NAME', getenv('MAIL_FROM_NAME') ?: 'KaI'); diff --git a/login.php b/login.php index dd053e4..1206162 100644 --- a/login.php +++ b/login.php @@ -17,30 +17,55 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { exit; } - $username = trim($_POST['username'] ?? ''); - $password = (string) ($_POST['password'] ?? ''); + 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 ($username === '' || $password === '') { - $errors[] = 'Please enter both username and password.'; - } else { - $user = findUserByUsername($username); - if ($user && verifyPassword($password, $user['passwordHash'] ?? '')) { + 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' => $user['username'], - 'role' => $user['role'] ?? 'user' + 'username' => $regUsername, + 'role' => 'user' ]; - if (!empty($_POST['remember'])) { - setRememberMeCookie($user['username']); - } else { - clearRememberMeCookie($user['username']); - } - 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']); + } - $errors[] = 'Invalid username or password.'; + header('Location: index.php'); + exit; + } + + $errors[] = 'Invalid username or password.'; + } } } @@ -87,6 +112,7 @@ if (empty(loadUsers())) {
+ @@ -100,6 +126,12 @@ if (empty(loadUsers())) {
+ +
No account yet?
+
+ Create a new account +
+
', $errors)); ?>