$a) { [$a, $b] = [$b, $a]; } $answer = $a - $b; } elseif ($op === '×') { $a = random_int(2, 9); $b = random_int(2, 9); $answer = $a * $b; } else { $answer = $a + $b; } $_SESSION['login_math'] = [ 'q' => "What is {$a} {$op} {$b}?", 'a' => (string) $answer, 't' => time(), ]; return $_SESSION['login_math']; } function admin_login_math_get(): array { $m = $_SESSION['login_math'] ?? null; if (!is_array($m) || empty($m['q']) || !isset($m['a'])) { return admin_login_math_new(); } // Refresh stale challenges. if (time() - (int) ($m['t'] ?? 0) > 900) { return admin_login_math_new(); } return $m; } function admin_login_math_check(?string $answer): bool { $m = admin_login_math_get(); $ok = is_string($answer) && trim($answer) !== '' && hash_equals((string) $m['a'], trim($answer)); // One attempt per challenge. admin_login_math_new(); return $ok; } $error = ''; $notice = ''; $pending = admin_pending_login(); $stage = $pending ? 'verify' : 'request'; $math = admin_login_math_get(); $gotifyEnabled = function_exists('admin_gotify_enabled') && admin_gotify_enabled(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!admin_csrf_valid($_POST['csrf'] ?? null)) { $error = 'Security check failed. Please try again.'; } elseif (admin_login_throttled()) { $error = 'Too many login attempts. Take a breather and try again later.'; } else { $action = (string) ($_POST['action'] ?? ''); if ($action === 'request_code') { $screenName = trim((string) ($_POST['screen_name'] ?? '')); $mathAnswer = (string) ($_POST['math_answer'] ?? ''); $delivery = 'oscar'; if ($gotifyEnabled && function_exists('admin_gotify_resolve')) { $gcfg = admin_gotify_resolve($screenName); if ($gcfg) { $delivery = 'gotify'; } } if (!admin_login_math_check($mathAnswer)) { admin_record_login_failure(); $error = 'Math check failed. Try again (robots are notoriously bad at this).'; $math = admin_login_math_get(); } elseif ($screenName === '' || strlen($screenName) > 64) { $error = 'Enter your OSCAR screen name.'; $math = admin_login_math_get(); } elseif (!admin_is_allowed($screenName)) { admin_record_login_failure(); $funny = [ "Nice try, {$screenName}. This admin panel is members-only, and the velvet rope just laughed at you.", "Access denied. The admin hamsters held an emergency meeting and voted you 'not it'.", "Whoa there, keyboard cowboy. You're not on the admin list. The server has filed your request under 'bold, but no'.", "Nope. You're not an admin. Please return your imaginary badge at the nearest imaginary desk.", "The OSCAR server checked the VIP list twice. You are on neither list. It also asked me to tell you to get lost, politely-ish.", "Error 403: You lack the ancient admin runes. The control room door remains unimpressed.", "Sorry {$screenName}, this dashboard runs on admin juice, and your cup is empty.", "The admin council consulted the sacred spreadsheet. Your name was not there. It was a short meeting.", "You have reached the admin panel. Unfortunately, the admin panel has not reached you back.", "Bold move. Wrong permissions. The server admires the confidence, but not enough to let you in.", "Access denied. I asked the mainframe, and it said 'lol no' with surprising professionalism.", "This area is for admins only. You are currently in the 'very enthusiastic spectator' category.", "The bouncer checked your name, checked it again, then checked the vibes. All three said no.", "You must be this admin to ride this dashboard. You are currently several admins short.", "The keys to the control room are guarded by a dragon. The dragon says you may not pass.", ]; $msg = $funny[array_rand($funny)]; // Best effort: notify via selected channel. Failure is intentionally silent to avoid leaking details. if ($delivery === 'gotify' && function_exists('admin_gotify_send_for_user')) { $gcfg = admin_gotify_resolve($screenName); if ($gcfg) { admin_gotify_send_for_user($screenName, 'Oscar Admin login denied', $msg); } // If no gotify config, silently skip send but keep same UI. } else { admin_send_im($screenName, $msg, true); } // Behave identically to a successful send to avoid account enumeration. $fakeCode = admin_random_code((int) admin_cfg('login_code_length', 8)); admin_set_pending_login($screenName, $fakeCode, $delivery); $pending = admin_pending_login(); $stage = 'verify'; $notice = $delivery === 'gotify' ? 'Login code sent via Gotify. Check your Gotify notifications.' : 'Login code sent to ' . $screenName . ' on OSCAR. Check your IMs.'; } else { $code = admin_random_code((int) admin_cfg('login_code_length', 8)); $text = "Oscar Admin login code for {$screenName}: {$code}\nThis code expires in " . (int) admin_cfg('login_code_ttl_seconds', 600) . " seconds. If you didn't request it, ignore this message."; $send = ['ok' => false, 'error' => 'Delivery failed']; if ($delivery === 'gotify' && function_exists('admin_gotify_send_for_user')) { $gcfg = function_exists('admin_gotify_resolve') ? admin_gotify_resolve($screenName) : null; if ($gcfg) { $send = admin_gotify_send_for_user($screenName, 'Oscar Admin login code', $text); } else { // No personal/global gotify config - fake success UI to avoid enumeration, but no code is sent. // Legit admins must configure Gotify in dashboard first. $send = ['ok' => true, 'status' => 0, 'error' => null, 'data' => null, 'raw' => '']; } } else { $send = admin_send_im($screenName, $text, true); } if (!$send['ok']) { admin_record_login_failure(); $error = $delivery === 'gotify' ? 'Could not send a login code via Gotify right now. Check Gotify settings.' : 'Could not send a login code via OSCAR right now. Check the system account and management API config.'; $math = admin_login_math_get(); } else { admin_set_pending_login($screenName, $code, $delivery); $pending = admin_pending_login(); $stage = 'verify'; $notice = $delivery === 'gotify' ? 'Login code sent via Gotify. Check your Gotify notifications.' : 'Login code sent to ' . $screenName . ' on OSCAR. Check your IMs.'; } } } elseif ($action === 'verify_code') { $pending = admin_pending_login(); $code = trim((string) ($_POST['code'] ?? '')); if (!$pending) { $error = 'Your login code expired. Request a new one.'; $stage = 'request'; } elseif ($code === '') { $error = 'Enter the login code you received on OSCAR.'; $stage = 'verify'; } elseif (!password_verify($code, (string) $pending['code_hash'])) { admin_record_login_failure(); $error = 'That code is not right. The OSCAR gods remain unimpressed.'; $stage = 'verify'; // After several failures, clear pending. $attempts = $_SESSION['login_attempts'] ?? []; if (is_array($attempts) && count($attempts) >= (int) admin_cfg('max_login_attempts', 8)) { admin_clear_pending_login(); $stage = 'request'; } } else { $name = (string) $pending['screen_name']; if (!admin_is_allowed($name)) { // Treat as invalid to avoid account/enumeration leaks. admin_record_login_failure(); $error = 'That code is not right. The OSCAR gods remain unimpressed.'; $stage = 'verify'; $attempts = $_SESSION['login_attempts'] ?? []; if (is_array($attempts) && count($attempts) >= (int) admin_cfg('max_login_attempts', 8)) { admin_clear_pending_login(); $stage = 'request'; } } else { admin_login_user($name); admin_flash('success', 'Welcome back, ' . $name . '. The control room is yours.'); admin_redirect('index.php'); } } } elseif ($action === 'reset') { admin_clear_pending_login(); $pending = null; $stage = 'request'; $notice = 'Login reset. Start fresh.'; } else { $error = 'Unknown action.'; } } } admin_page('Login', 'login', function () use ($error, $notice, $pending, $stage, $math, $gotifyEnabled): void { ?>
Secure sign-in

Admin access

Sign in with your OSCAR screen name. We'll send a one-time code through .

Code sent via
Expires in s

How it works

  1. 1
    Enter your screen name

    The one configured as an admin.

  2. 2
    Get your code

  3. 3
    Verify and manage

    Access the full admin dashboard.

true, 'description' => 'Oscar Admin is a standalone open-source web dashboard for managing Open OSCAR Server: users, sessions, chat rooms, BART assets, buddy lists, and more.', 'repo_url' => 'https://git.claytonia.net/Clarth/oscar-admin', ]);