v1.1.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/includes/config.php';
- require_once __DIR__ . '/includes/database.php';
- require_once __DIR__ . '/includes/user-auth.php';
- require_once __DIR__ . '/includes/csrf.php';
- require_once __DIR__ . '/includes/language.php';
- require_once __DIR__ . '/includes/site-front.php';
- require_once __DIR__ . '/includes/asset.php';
- require_once __DIR__ . '/includes/antibot.php';
- require_once __DIR__ . '/includes/login-throttle.php';
-
- UserAuth::boot();
- AntiBot::boot('user');
- if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
- AntiBot::refresh('user');
- }
-
- $settings = SiteFront::settings();
- $loginEnabled = ($settings['login_enabled'] ?? '1') !== '0';
-
- function sanitizeRedirectTarget(string $target): string
- {
- if ($target !== '' && preg_match('#^[a-zA-Z0-9_\-]+\.php(\?[a-zA-Z0-9_\-\.=&%]*)?$#', $target) === 1) {
- return $target;
- }
- return '';
- }
-
- $redirectTarget = sanitizeRedirectTarget((string) ($_GET['redirect'] ?? ''));
-
- if (isset($_GET['logout'])) {
- UserAuth::logout();
- header('Location: user-login.php');
- exit;
- }
-
- if (UserAuth::check() && !in_array((string) ($_GET['mode'] ?? ''), ['verify', 'reset'], true)) {
- header('Location: ' . ($redirectTarget !== '' ? $redirectTarget : 'account.php'));
- exit;
- }
-
- if (!$loginEnabled) {
- $siteName = Config::get('APP_NAME', 'StocketBase');
- ?>
- <!DOCTYPE html>
- <html lang="en">
-
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title><?= htmlspecialchars(Language::get('auth_login_button', 'Log in')) ?> - <?= htmlspecialchars($siteName) ?></title>
- <?= Asset::favicon() ?>
- <?= Asset::css('assets/site.css') ?>
- <style>
- <?= SiteFront::renderFontFaces() ?><?= SiteFront::fontVariablesCss() ?>
- </style>
- <?php if (!empty($settings['custom_css'])): ?>
- <style>
- <?= $settings['custom_css'] ?>
- </style>
- <?php endif; ?>
- </head>
-
- <body class="auth-page" data-theme="light">
- <script>
- (function() {
- try {
- var t = localStorage.getItem('stocketbase_theme');
- if (t === 'dark' || t === 'light') {
- document.body.setAttribute('data-theme', t);
- } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
- document.body.setAttribute('data-theme', 'dark');
- }
- } catch (e) {}
- })();
- </script>
- <div class="auth-card auth-card-disabled">
- <h1><?= htmlspecialchars($siteName) ?></h1>
- <p class="auth-subtitle"><?= htmlspecialchars(Language::get('auth_login_disabled', 'Customer login is temporarily disabled. Please check back later.')) ?></p>
- </div>
- </body>
-
- </html>
- <?php
- exit;
- }
-
- require_once __DIR__ . '/includes/account-tokens.php';
- require_once __DIR__ . '/includes/account-emails.php';
- require_once __DIR__ . '/includes/orders.php';
-
- $error = null;
- $notice = null;
- $requestedMode = (string) ($_GET['mode'] ?? 'login');
- $mode = in_array($requestedMode, ['login', 'register', 'forgot', 'reset', 'verify'], true) ? $requestedMode : 'login';
- $resetToken = strtolower(trim((string) ($_REQUEST['token'] ?? '')));
-
- if ($mode === 'verify') {
- $verifiedUserId = AccountTokens::consume($resetToken, AccountTokens::PURPOSE_VERIFY);
- if ($verifiedUserId !== null) {
- $usersDb = Database::users();
- $usersDb->prepare('UPDATE user_accounts SET email_verified_at = COALESCE(email_verified_at, NOW()) WHERE id = :id')->execute(['id' => $verifiedUserId]);
- $emailStatement = $usersDb->prepare('SELECT email FROM user_accounts WHERE id = :id LIMIT 1');
- $emailStatement->execute(['id' => $verifiedUserId]);
- $claimed = Orders::claimGuestOrders($verifiedUserId, (string) $emailStatement->fetchColumn());
- $_SESSION['account_flash'] = ['success', $claimed > 0
- ? str_replace('{count}', (string) $claimed, Language::get('auth_verified_claimed', 'Email confirmed. We added {count} earlier orders to your account.'))
- : Language::get('auth_verified', 'Email confirmed. Thank you!')];
- header('Location: ' . (UserAuth::check() ? 'account.php' : 'user-login.php?mode=login&verified=1'));
- exit;
- }
- $mode = 'login';
- $error = Language::get('auth_verify_invalid', 'This confirmation link has expired or was already used. Log in and ask for a new one.');
- }
-
- if (isset($_GET['verified'])) {
- $notice = Language::get('auth_verified_login', 'Email confirmed. You can log in now.');
- }
- if (isset($_GET['reset_done'])) {
- $notice = Language::get('auth_reset_done', 'Your password was changed. Log in with the new password.');
- }
-
- if ($mode === 'reset' && AccountTokens::peek($resetToken, AccountTokens::PURPOSE_RESET) === null) {
- $mode = 'forgot';
- $error = Language::get('auth_reset_invalid', 'This reset link has expired or was already used. Ask for a new one below.');
- }
-
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- if (!Csrf::verify($_POST['csrf_token'] ?? null)) {
- $error = Language::get('form_security_failed', 'Security check failed, please try again.');
- } elseif (!AntiBot::verify('user', $_POST['antibot_answer'] ?? null, $_POST['antibot_started'] ?? null, $_POST['website'] ?? null)) {
- $error = Language::get('auth_security_failed', 'Security verification failed, please try again.');
- } else {
- $formAction = $_POST['form_action'] ?? '';
-
- if ($formAction === 'login') {
- $email = trim((string) ($_POST['email'] ?? ''));
- $password = (string) ($_POST['password'] ?? '');
-
- if ($email === '' || $password === '') {
- $error = Language::get('auth_missing_credentials', 'Please enter your email and password.');
- $mode = 'login';
- } elseif (($wait = LoginThrottle::secondsUntilAllowed('reader', $email)) > 0) {
- $error = LoginThrottle::message($wait);
- $mode = 'login';
- } elseif (UserAuth::attemptLogin($email, $password)) {
- LoginThrottle::clear('reader', $email);
- header('Location: ' . ($redirectTarget !== '' ? $redirectTarget : 'index.php'));
- exit;
- } else {
- LoginThrottle::recordFailure('reader', $email);
- $error = Language::get('auth_invalid_credentials', 'Invalid email or password.');
- $mode = 'login';
- }
- }
-
- if ($formAction === 'register') {
- $email = trim((string) ($_POST['email'] ?? ''));
- $password = (string) ($_POST['password'] ?? '');
- $passwordConfirm = (string) ($_POST['password_confirm'] ?? '');
- $displayName = trim((string) ($_POST['display_name'] ?? ''));
-
- if ($password !== $passwordConfirm) {
- $error = Language::get('auth_password_mismatch', 'Passwords do not match.');
- $mode = 'register';
- } else {
- [$success, $registerError] = UserAuth::register($email, $password, $displayName);
- if ($success) {
- $newAccount = UserAuth::user();
- if ($newAccount !== null) {
- AccountEmails::sendVerification($newAccount);
- }
- header('Location: ' . ($redirectTarget !== '' ? $redirectTarget : 'account.php'));
- exit;
- }
- $error = $registerError;
- $mode = 'register';
- }
- }
-
- if ($formAction === 'forgot') {
- $email = trim((string) ($_POST['email'] ?? ''));
- if (($wait = LoginThrottle::secondsUntilAllowed('reset', $email)) > 0) {
- $error = LoginThrottle::message($wait);
- } else {
- LoginThrottle::recordFailure('reset', $email);
- AccountEmails::sendPasswordReset($email);
- $notice = Language::get('auth_reset_sent', 'If an account with this email exists, we sent a link to set a new password. Check your inbox and spam folder.');
- }
- $mode = 'forgot';
- }
-
- if ($formAction === 'reset') {
- $password = (string) ($_POST['password'] ?? '');
- $passwordConfirm = (string) ($_POST['password_confirm'] ?? '');
- $mode = 'reset';
- if (strlen($password) < 8) {
- $error = Language::get('auth_password_too_short', 'Password must be at least 8 characters.');
- } elseif ($password !== $passwordConfirm) {
- $error = Language::get('auth_password_mismatch', 'Passwords do not match.');
- } else {
- $resetUserId = AccountTokens::consume($resetToken, AccountTokens::PURPOSE_RESET);
- if ($resetUserId === null) {
- $mode = 'forgot';
- $error = Language::get('auth_reset_invalid', 'This reset link has expired or was already used. Ask for a new one below.');
- } else {
- $usersDb = Database::users();
- $usersDb->prepare('UPDATE user_accounts SET password_hash = :hash, email_verified_at = COALESCE(email_verified_at, NOW()) WHERE id = :id')
- ->execute(['hash' => password_hash($password, PASSWORD_DEFAULT), 'id' => $resetUserId]);
- $usersDb->prepare('DELETE FROM user_sessions WHERE user_account_id = :id')->execute(['id' => $resetUserId]);
- $resetEmail = $usersDb->prepare('SELECT email FROM user_accounts WHERE id = :id');
- $resetEmail->execute(['id' => $resetUserId]);
- $resetAddress = (string) $resetEmail->fetchColumn();
- LoginThrottle::clear('reader', $resetAddress);
- Orders::claimGuestOrders($resetUserId, $resetAddress);
- header('Location: user-login.php?mode=login&reset_done=1');
- exit;
- }
- }
- }
- }
- }
-
- $siteName = Config::get('APP_NAME', 'StocketBase');
-
- ?>
- <!DOCTYPE html>
- <html lang="en">
-
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title><?= $mode === 'register' ? htmlspecialchars(Language::get('auth_register_button', 'Create account')) : htmlspecialchars(Language::get('auth_login_button', 'Log in')) ?> - <?= htmlspecialchars($siteName) ?></title>
- <?= Asset::favicon() ?>
- <?= Asset::css('assets/site.css') ?>
- <style>
- <?= SiteFront::renderFontFaces() ?><?= SiteFront::fontVariablesCss() ?>
- </style>
- <?php if (!empty($settings['custom_css'])): ?>
- <style>
- <?= $settings['custom_css'] ?>
- </style>
- <?php endif; ?>
- </head>
-
- <body class="auth-page" data-theme="light">
- <script>
- (function() {
- try {
- var t = localStorage.getItem('stocketbase_theme');
- if (t === 'dark' || t === 'light') {
- document.body.setAttribute('data-theme', t);
- } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
- document.body.setAttribute('data-theme', 'dark');
- }
- } catch (e) {}
- })();
- </script>
- <div class="auth-card">
- <h1><?= htmlspecialchars($siteName) ?></h1>
- <p class="auth-subtitle"><?= htmlspecialchars(Language::get('auth_account_subtitle', 'Customer account')) ?></p>
-
- <div class="auth-tabs">
- <a href="user-login.php?mode=login<?= $redirectTarget !== '' ? '&redirect=' . urlencode($redirectTarget) : '' ?>" class="<?= $mode === 'login' ? 'active' : '' ?>"><?= htmlspecialchars(Language::get('auth_login_button', 'Log in')) ?></a>
- <a href="user-login.php?mode=register<?= $redirectTarget !== '' ? '&redirect=' . urlencode($redirectTarget) : '' ?>" class="<?= $mode === 'register' ? 'active' : '' ?>"><?= htmlspecialchars(Language::get('auth_register_button', 'Create account')) ?></a>
- </div>
-
- <?php if ($error !== null): ?>
- <div class="auth-error"><?= htmlspecialchars($error) ?></div>
- <?php endif; ?>
- <?php if ($notice !== null): ?>
- <div class="auth-notice"><?= htmlspecialchars($notice) ?></div>
- <?php endif; ?>
-
- <?php if ($mode === 'forgot'): ?>
- <form method="post" action="user-login.php?mode=forgot">
- <?= Csrf::field() ?>
- <?= AntiBot::field('user') ?>
- <input type="hidden" name="form_action" value="forgot">
- <p class="auth-help"><?= htmlspecialchars(Language::get('auth_forgot_intro', 'Enter the email you use for your account. We will send you a link to set a new password.')) ?></p>
- <label><?= htmlspecialchars(Language::get('auth_email', 'Email')) ?></label>
- <input type="email" name="email" required autofocus autocomplete="email">
- <div class="antibot-box">
- <div class="antibot-image"><?= AntiBot::image('user') ?></div>
- <label for="antibot-answer"><?= htmlspecialchars(Language::get('auth_security_code', 'Security code')) ?></label>
- <input id="antibot-answer" type="text" name="antibot_answer" required inputmode="text" autocomplete="off" maxlength="6" spellcheck="false">
- </div>
- <button type="submit"><?= htmlspecialchars(Language::get('auth_forgot_button', 'Send the link')) ?></button>
- <p class="auth-help"><a href="user-login.php?mode=login"><?= htmlspecialchars(Language::get('auth_back_to_login', 'Back to log in')) ?></a></p>
- </form>
- <?php elseif ($mode === 'reset'): ?>
- <form method="post" action="user-login.php?mode=reset">
- <?= Csrf::field() ?>
- <?= AntiBot::field('user') ?>
- <input type="hidden" name="form_action" value="reset">
- <input type="hidden" name="token" value="<?= htmlspecialchars($resetToken, ENT_QUOTES) ?>">
- <label><?= htmlspecialchars(Language::get('account_new_password_short', 'New password')) ?></label>
- <input type="password" name="password" required minlength="8" autofocus autocomplete="new-password">
- <label><?= htmlspecialchars(Language::get('auth_confirm_password', 'Confirm password')) ?></label>
- <input type="password" name="password_confirm" required minlength="8" autocomplete="new-password">
- <div class="antibot-box">
- <div class="antibot-image"><?= AntiBot::image('user') ?></div>
- <label for="antibot-answer"><?= htmlspecialchars(Language::get('auth_security_code', 'Security code')) ?></label>
- <input id="antibot-answer" type="text" name="antibot_answer" required inputmode="text" autocomplete="off" maxlength="6" spellcheck="false">
- </div>
- <button type="submit"><?= htmlspecialchars(Language::get('auth_reset_button', 'Save the new password')) ?></button>
- </form>
- <?php elseif ($mode === 'login'): ?>
- <form method="post">
- <?= Csrf::field() ?>
- <?= AntiBot::field('user') ?>
- <input type="hidden" name="form_action" value="login">
- <label><?= htmlspecialchars(Language::get('auth_email', 'Email')) ?></label>
- <input type="email" name="email" required autofocus>
- <label><?= htmlspecialchars(Language::get('auth_password', 'Password')) ?></label>
- <input type="password" name="password" required>
- <div class="antibot-box">
- <div class="antibot-image"><?= AntiBot::image('user') ?></div>
- <label for="antibot-answer"><?= htmlspecialchars(Language::get('auth_security_code', 'Security code')) ?></label>
- <input id="antibot-answer" type="text" name="antibot_answer" required inputmode="text" autocomplete="off" maxlength="6" spellcheck="false">
- </div>
- <button type="submit"><?= htmlspecialchars(Language::get('auth_login_button', 'Log in')) ?></button>
- <p class="auth-help"><a href="user-login.php?mode=forgot"><?= htmlspecialchars(Language::get('auth_forgot_link', 'Forgot your password?')) ?></a></p>
- </form>
- <?php else: ?>
- <form method="post">
- <?= Csrf::field() ?>
- <?= AntiBot::field('user') ?>
- <input type="hidden" name="form_action" value="register">
- <label><?= htmlspecialchars(Language::get('auth_display_name', 'Display name')) ?></label>
- <input type="text" name="display_name">
- <label><?= htmlspecialchars(Language::get('auth_email', 'Email')) ?></label>
- <input type="email" name="email" required>
- <label><?= htmlspecialchars(Language::get('auth_password', 'Password')) ?></label>
- <input type="password" name="password" required minlength="8">
- <label><?= htmlspecialchars(Language::get('auth_confirm_password', 'Confirm password')) ?></label>
- <input type="password" name="password_confirm" required minlength="8">
- <div class="antibot-box">
- <div class="antibot-image"><?= AntiBot::image('user') ?></div>
- <label for="antibot-answer"><?= htmlspecialchars(Language::get('auth_security_code', 'Security code')) ?></label>
- <input id="antibot-answer" type="text" name="antibot_answer" required inputmode="text" autocomplete="off" maxlength="6" spellcheck="false">
- </div>
- <button type="submit"><?= htmlspecialchars(Language::get('auth_register_button', 'Create account')) ?></button>
- </form>
- <?php endif; ?>
-
- </div>
- </body>
-
- </html>
-