WebOrbiton
v1.1.0.0

StocketBase

354 lines · 17.8 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/includes/config.php';
  6. require_once __DIR__ . '/includes/database.php';
  7. require_once __DIR__ . '/includes/user-auth.php';
  8. require_once __DIR__ . '/includes/csrf.php';
  9. require_once __DIR__ . '/includes/language.php';
  10. require_once __DIR__ . '/includes/site-front.php';
  11. require_once __DIR__ . '/includes/asset.php';
  12. require_once __DIR__ . '/includes/antibot.php';
  13. require_once __DIR__ . '/includes/login-throttle.php';
  14. ​
  15. UserAuth::boot();
  16. AntiBot::boot('user');
  17. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  18. AntiBot::refresh('user');
  19. }
  20. ​
  21. $settings = SiteFront::settings();
  22. $loginEnabled = ($settings['login_enabled'] ?? '1') !== '0';
  23. ​
  24. function sanitizeRedirectTarget(string $target): string
  25. {
  26. if ($target !== '' && preg_match('#^[a-zA-Z0-9_\-]+\.php(\?[a-zA-Z0-9_\-\.=&%]*)?$#', $target) === 1) {
  27. return $target;
  28. }
  29. return '';
  30. }
  31. ​
  32. $redirectTarget = sanitizeRedirectTarget((string) ($_GET['redirect'] ?? ''));
  33. ​
  34. if (isset($_GET['logout'])) {
  35. UserAuth::logout();
  36. header('Location: user-login.php');
  37. exit;
  38. }
  39. ​
  40. if (UserAuth::check() && !in_array((string) ($_GET['mode'] ?? ''), ['verify', 'reset'], true)) {
  41. header('Location: ' . ($redirectTarget !== '' ? $redirectTarget : 'account.php'));
  42. exit;
  43. }
  44. ​
  45. if (!$loginEnabled) {
  46. $siteName = Config::get('APP_NAME', 'StocketBase');
  47. ?>
  48. <!DOCTYPE html>
  49. <html lang="en">
  50. ​
  51. <head>
  52. <meta charset="utf-8">
  53. <meta name="viewport" content="width=device-width, initial-scale=1">
  54. <title><?= htmlspecialchars(Language::get('auth_login_button', 'Log in')) ?> - <?= htmlspecialchars($siteName) ?></title>
  55. <?= Asset::favicon() ?>
  56. <?= Asset::css('assets/site.css') ?>
  57. <style>
  58. <?= SiteFront::renderFontFaces() ?><?= SiteFront::fontVariablesCss() ?>
  59. </style>
  60. <?php if (!empty($settings['custom_css'])): ?>
  61. <style>
  62. <?= $settings['custom_css'] ?>
  63. </style>
  64. <?php endif; ?>
  65. </head>
  66. ​
  67. <body class="auth-page" data-theme="light">
  68. <script>
  69. (function() {
  70. try {
  71. var t = localStorage.getItem('stocketbase_theme');
  72. if (t === 'dark' || t === 'light') {
  73. document.body.setAttribute('data-theme', t);
  74. } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
  75. document.body.setAttribute('data-theme', 'dark');
  76. }
  77. } catch (e) {}
  78. })();
  79. </script>
  80. <div class="auth-card auth-card-disabled">
  81. <h1><?= htmlspecialchars($siteName) ?></h1>
  82. <p class="auth-subtitle"><?= htmlspecialchars(Language::get('auth_login_disabled', 'Customer login is temporarily disabled. Please check back later.')) ?></p>
  83. </div>
  84. </body>
  85. ​
  86. </html>
  87. <?php
  88. exit;
  89. }
  90. ​
  91. require_once __DIR__ . '/includes/account-tokens.php';
  92. require_once __DIR__ . '/includes/account-emails.php';
  93. require_once __DIR__ . '/includes/orders.php';
  94. ​
  95. $error = null;
  96. $notice = null;
  97. $requestedMode = (string) ($_GET['mode'] ?? 'login');
  98. $mode = in_array($requestedMode, ['login', 'register', 'forgot', 'reset', 'verify'], true) ? $requestedMode : 'login';
  99. $resetToken = strtolower(trim((string) ($_REQUEST['token'] ?? '')));
  100. ​
  101. if ($mode === 'verify') {
  102. $verifiedUserId = AccountTokens::consume($resetToken, AccountTokens::PURPOSE_VERIFY);
  103. if ($verifiedUserId !== null) {
  104. $usersDb = Database::users();
  105. $usersDb->prepare('UPDATE user_accounts SET email_verified_at = COALESCE(email_verified_at, NOW()) WHERE id = :id')->execute(['id' => $verifiedUserId]);
  106. $emailStatement = $usersDb->prepare('SELECT email FROM user_accounts WHERE id = :id LIMIT 1');
  107. $emailStatement->execute(['id' => $verifiedUserId]);
  108. $claimed = Orders::claimGuestOrders($verifiedUserId, (string) $emailStatement->fetchColumn());
  109. $_SESSION['account_flash'] = ['success', $claimed > 0
  110. ? str_replace('{count}', (string) $claimed, Language::get('auth_verified_claimed', 'Email confirmed. We added {count} earlier orders to your account.'))
  111. : Language::get('auth_verified', 'Email confirmed. Thank you!')];
  112. header('Location: ' . (UserAuth::check() ? 'account.php' : 'user-login.php?mode=login&verified=1'));
  113. exit;
  114. }
  115. $mode = 'login';
  116. $error = Language::get('auth_verify_invalid', 'This confirmation link has expired or was already used. Log in and ask for a new one.');
  117. }
  118. ​
  119. if (isset($_GET['verified'])) {
  120. $notice = Language::get('auth_verified_login', 'Email confirmed. You can log in now.');
  121. }
  122. if (isset($_GET['reset_done'])) {
  123. $notice = Language::get('auth_reset_done', 'Your password was changed. Log in with the new password.');
  124. }
  125. ​
  126. if ($mode === 'reset' && AccountTokens::peek($resetToken, AccountTokens::PURPOSE_RESET) === null) {
  127. $mode = 'forgot';
  128. $error = Language::get('auth_reset_invalid', 'This reset link has expired or was already used. Ask for a new one below.');
  129. }
  130. ​
  131. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  132. if (!Csrf::verify($_POST['csrf_token'] ?? null)) {
  133. $error = Language::get('form_security_failed', 'Security check failed, please try again.');
  134. } elseif (!AntiBot::verify('user', $_POST['antibot_answer'] ?? null, $_POST['antibot_started'] ?? null, $_POST['website'] ?? null)) {
  135. $error = Language::get('auth_security_failed', 'Security verification failed, please try again.');
  136. } else {
  137. $formAction = $_POST['form_action'] ?? '';
  138. ​
  139. if ($formAction === 'login') {
  140. $email = trim((string) ($_POST['email'] ?? ''));
  141. $password = (string) ($_POST['password'] ?? '');
  142. ​
  143. if ($email === '' || $password === '') {
  144. $error = Language::get('auth_missing_credentials', 'Please enter your email and password.');
  145. $mode = 'login';
  146. } elseif (($wait = LoginThrottle::secondsUntilAllowed('reader', $email)) > 0) {
  147. $error = LoginThrottle::message($wait);
  148. $mode = 'login';
  149. } elseif (UserAuth::attemptLogin($email, $password)) {
  150. LoginThrottle::clear('reader', $email);
  151. header('Location: ' . ($redirectTarget !== '' ? $redirectTarget : 'index.php'));
  152. exit;
  153. } else {
  154. LoginThrottle::recordFailure('reader', $email);
  155. $error = Language::get('auth_invalid_credentials', 'Invalid email or password.');
  156. $mode = 'login';
  157. }
  158. }
  159. ​
  160. if ($formAction === 'register') {
  161. $email = trim((string) ($_POST['email'] ?? ''));
  162. $password = (string) ($_POST['password'] ?? '');
  163. $passwordConfirm = (string) ($_POST['password_confirm'] ?? '');
  164. $displayName = trim((string) ($_POST['display_name'] ?? ''));
  165. ​
  166. if ($password !== $passwordConfirm) {
  167. $error = Language::get('auth_password_mismatch', 'Passwords do not match.');
  168. $mode = 'register';
  169. } else {
  170. [$success, $registerError] = UserAuth::register($email, $password, $displayName);
  171. if ($success) {
  172. $newAccount = UserAuth::user();
  173. if ($newAccount !== null) {
  174. AccountEmails::sendVerification($newAccount);
  175. }
  176. header('Location: ' . ($redirectTarget !== '' ? $redirectTarget : 'account.php'));
  177. exit;
  178. }
  179. $error = $registerError;
  180. $mode = 'register';
  181. }
  182. }
  183. ​
  184. if ($formAction === 'forgot') {
  185. $email = trim((string) ($_POST['email'] ?? ''));
  186. if (($wait = LoginThrottle::secondsUntilAllowed('reset', $email)) > 0) {
  187. $error = LoginThrottle::message($wait);
  188. } else {
  189. LoginThrottle::recordFailure('reset', $email);
  190. AccountEmails::sendPasswordReset($email);
  191. $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.');
  192. }
  193. $mode = 'forgot';
  194. }
  195. ​
  196. if ($formAction === 'reset') {
  197. $password = (string) ($_POST['password'] ?? '');
  198. $passwordConfirm = (string) ($_POST['password_confirm'] ?? '');
  199. $mode = 'reset';
  200. if (strlen($password) < 8) {
  201. $error = Language::get('auth_password_too_short', 'Password must be at least 8 characters.');
  202. } elseif ($password !== $passwordConfirm) {
  203. $error = Language::get('auth_password_mismatch', 'Passwords do not match.');
  204. } else {
  205. $resetUserId = AccountTokens::consume($resetToken, AccountTokens::PURPOSE_RESET);
  206. if ($resetUserId === null) {
  207. $mode = 'forgot';
  208. $error = Language::get('auth_reset_invalid', 'This reset link has expired or was already used. Ask for a new one below.');
  209. } else {
  210. $usersDb = Database::users();
  211. $usersDb->prepare('UPDATE user_accounts SET password_hash = :hash, email_verified_at = COALESCE(email_verified_at, NOW()) WHERE id = :id')
  212. ->execute(['hash' => password_hash($password, PASSWORD_DEFAULT), 'id' => $resetUserId]);
  213. $usersDb->prepare('DELETE FROM user_sessions WHERE user_account_id = :id')->execute(['id' => $resetUserId]);
  214. $resetEmail = $usersDb->prepare('SELECT email FROM user_accounts WHERE id = :id');
  215. $resetEmail->execute(['id' => $resetUserId]);
  216. $resetAddress = (string) $resetEmail->fetchColumn();
  217. LoginThrottle::clear('reader', $resetAddress);
  218. Orders::claimGuestOrders($resetUserId, $resetAddress);
  219. header('Location: user-login.php?mode=login&reset_done=1');
  220. exit;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. ​
  227. $siteName = Config::get('APP_NAME', 'StocketBase');
  228. ​
  229. ?>
  230. <!DOCTYPE html>
  231. <html lang="en">
  232. ​
  233. <head>
  234. <meta charset="utf-8">
  235. <meta name="viewport" content="width=device-width, initial-scale=1">
  236. <title><?= $mode === 'register' ? htmlspecialchars(Language::get('auth_register_button', 'Create account')) : htmlspecialchars(Language::get('auth_login_button', 'Log in')) ?> - <?= htmlspecialchars($siteName) ?></title>
  237. <?= Asset::favicon() ?>
  238. <?= Asset::css('assets/site.css') ?>
  239. <style>
  240. <?= SiteFront::renderFontFaces() ?><?= SiteFront::fontVariablesCss() ?>
  241. </style>
  242. <?php if (!empty($settings['custom_css'])): ?>
  243. <style>
  244. <?= $settings['custom_css'] ?>
  245. </style>
  246. <?php endif; ?>
  247. </head>
  248. ​
  249. <body class="auth-page" data-theme="light">
  250. <script>
  251. (function() {
  252. try {
  253. var t = localStorage.getItem('stocketbase_theme');
  254. if (t === 'dark' || t === 'light') {
  255. document.body.setAttribute('data-theme', t);
  256. } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
  257. document.body.setAttribute('data-theme', 'dark');
  258. }
  259. } catch (e) {}
  260. })();
  261. </script>
  262. <div class="auth-card">
  263. <h1><?= htmlspecialchars($siteName) ?></h1>
  264. <p class="auth-subtitle"><?= htmlspecialchars(Language::get('auth_account_subtitle', 'Customer account')) ?></p>
  265. ​
  266. <div class="auth-tabs">
  267. <a href="user-login.php?mode=login<?= $redirectTarget !== '' ? '&redirect=' . urlencode($redirectTarget) : '' ?>" class="<?= $mode === 'login' ? 'active' : '' ?>"><?= htmlspecialchars(Language::get('auth_login_button', 'Log in')) ?></a>
  268. <a href="user-login.php?mode=register<?= $redirectTarget !== '' ? '&redirect=' . urlencode($redirectTarget) : '' ?>" class="<?= $mode === 'register' ? 'active' : '' ?>"><?= htmlspecialchars(Language::get('auth_register_button', 'Create account')) ?></a>
  269. </div>
  270. ​
  271. <?php if ($error !== null): ?>
  272. <div class="auth-error"><?= htmlspecialchars($error) ?></div>
  273. <?php endif; ?>
  274. <?php if ($notice !== null): ?>
  275. <div class="auth-notice"><?= htmlspecialchars($notice) ?></div>
  276. <?php endif; ?>
  277. ​
  278. <?php if ($mode === 'forgot'): ?>
  279. <form method="post" action="user-login.php?mode=forgot">
  280. <?= Csrf::field() ?>
  281. <?= AntiBot::field('user') ?>
  282. <input type="hidden" name="form_action" value="forgot">
  283. <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>
  284. <label><?= htmlspecialchars(Language::get('auth_email', 'Email')) ?></label>
  285. <input type="email" name="email" required autofocus autocomplete="email">
  286. <div class="antibot-box">
  287. <div class="antibot-image"><?= AntiBot::image('user') ?></div>
  288. <label for="antibot-answer"><?= htmlspecialchars(Language::get('auth_security_code', 'Security code')) ?></label>
  289. <input id="antibot-answer" type="text" name="antibot_answer" required inputmode="text" autocomplete="off" maxlength="6" spellcheck="false">
  290. </div>
  291. <button type="submit"><?= htmlspecialchars(Language::get('auth_forgot_button', 'Send the link')) ?></button>
  292. <p class="auth-help"><a href="user-login.php?mode=login"><?= htmlspecialchars(Language::get('auth_back_to_login', 'Back to log in')) ?></a></p>
  293. </form>
  294. <?php elseif ($mode === 'reset'): ?>
  295. <form method="post" action="user-login.php?mode=reset">
  296. <?= Csrf::field() ?>
  297. <?= AntiBot::field('user') ?>
  298. <input type="hidden" name="form_action" value="reset">
  299. <input type="hidden" name="token" value="<?= htmlspecialchars($resetToken, ENT_QUOTES) ?>">
  300. <label><?= htmlspecialchars(Language::get('account_new_password_short', 'New password')) ?></label>
  301. <input type="password" name="password" required minlength="8" autofocus autocomplete="new-password">
  302. <label><?= htmlspecialchars(Language::get('auth_confirm_password', 'Confirm password')) ?></label>
  303. <input type="password" name="password_confirm" required minlength="8" autocomplete="new-password">
  304. <div class="antibot-box">
  305. <div class="antibot-image"><?= AntiBot::image('user') ?></div>
  306. <label for="antibot-answer"><?= htmlspecialchars(Language::get('auth_security_code', 'Security code')) ?></label>
  307. <input id="antibot-answer" type="text" name="antibot_answer" required inputmode="text" autocomplete="off" maxlength="6" spellcheck="false">
  308. </div>
  309. <button type="submit"><?= htmlspecialchars(Language::get('auth_reset_button', 'Save the new password')) ?></button>
  310. </form>
  311. <?php elseif ($mode === 'login'): ?>
  312. <form method="post">
  313. <?= Csrf::field() ?>
  314. <?= AntiBot::field('user') ?>
  315. <input type="hidden" name="form_action" value="login">
  316. <label><?= htmlspecialchars(Language::get('auth_email', 'Email')) ?></label>
  317. <input type="email" name="email" required autofocus>
  318. <label><?= htmlspecialchars(Language::get('auth_password', 'Password')) ?></label>
  319. <input type="password" name="password" required>
  320. <div class="antibot-box">
  321. <div class="antibot-image"><?= AntiBot::image('user') ?></div>
  322. <label for="antibot-answer"><?= htmlspecialchars(Language::get('auth_security_code', 'Security code')) ?></label>
  323. <input id="antibot-answer" type="text" name="antibot_answer" required inputmode="text" autocomplete="off" maxlength="6" spellcheck="false">
  324. </div>
  325. <button type="submit"><?= htmlspecialchars(Language::get('auth_login_button', 'Log in')) ?></button>
  326. <p class="auth-help"><a href="user-login.php?mode=forgot"><?= htmlspecialchars(Language::get('auth_forgot_link', 'Forgot your password?')) ?></a></p>
  327. </form>
  328. <?php else: ?>
  329. <form method="post">
  330. <?= Csrf::field() ?>
  331. <?= AntiBot::field('user') ?>
  332. <input type="hidden" name="form_action" value="register">
  333. <label><?= htmlspecialchars(Language::get('auth_display_name', 'Display name')) ?></label>
  334. <input type="text" name="display_name">
  335. <label><?= htmlspecialchars(Language::get('auth_email', 'Email')) ?></label>
  336. <input type="email" name="email" required>
  337. <label><?= htmlspecialchars(Language::get('auth_password', 'Password')) ?></label>
  338. <input type="password" name="password" required minlength="8">
  339. <label><?= htmlspecialchars(Language::get('auth_confirm_password', 'Confirm password')) ?></label>
  340. <input type="password" name="password_confirm" required minlength="8">
  341. <div class="antibot-box">
  342. <div class="antibot-image"><?= AntiBot::image('user') ?></div>
  343. <label for="antibot-answer"><?= htmlspecialchars(Language::get('auth_security_code', 'Security code')) ?></label>
  344. <input id="antibot-answer" type="text" name="antibot_answer" required inputmode="text" autocomplete="off" maxlength="6" spellcheck="false">
  345. </div>
  346. <button type="submit"><?= htmlspecialchars(Language::get('auth_register_button', 'Create account')) ?></button>
  347. </form>
  348. <?php endif; ?>
  349. ​
  350. </div>
  351. </body>
  352. ​
  353. </html>
  354. ​