Files
racket-chatgpt-bootstrap/login.php
T
www-data 475765e31f Move rendering into private templates
Add an explicit template renderer with HTML views and partials for the app, bootstrap, package, and catalog pages. Move shared reporting setup into config/reporting.php and relocate stylesheet assets under css/.
2026-05-26 12:50:26 +02:00

99 lines
2.6 KiB
PHP

<?php
/*
* login.php
*/
require_once __DIR__ . '/private/auth.php';
require_once __DIR__ . '/private/Template.php';
require_once __DIR__ . '/config/reporting.php';
$auth = new RacketSandboxAuth(__DIR__ . '/data/racket-sandbox.sqlite');
$error = '';
function detect_login_language($supported, $fallback)
{
$header = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
$preferences = array();
foreach (explode(',', $header) as $part) {
$pieces = array_map('trim', explode(';', $part));
$lang = strtolower($pieces[0] ?? '');
$quality = 1.0;
foreach (array_slice($pieces, 1) as $piece) {
if (strpos($piece, 'q=') === 0) {
$quality = (float)substr($piece, 2);
}
}
if ($lang !== '') {
$preferences[] = array('lang' => $lang, 'quality' => $quality);
}
}
usort($preferences, function ($a, $b) {
return $a['quality'] < $b['quality'] ? 1 : -1;
});
foreach ($preferences as $preference) {
$lang = $preference['lang'];
$primary = explode('-', $lang, 2)[0];
if (isset($supported[$lang])) {
return $lang;
}
if (isset($supported[$primary])) {
return $primary;
}
}
return $fallback;
}
$pageTitle = 'Racket ChatGPT Agent Sandbox Creator';
$templateData = RacketSandboxTemplate::dataFile('login.html');
$texts = $templateData['translations'] ?? array();
$language = detect_login_language($texts, 'en');
$styleVersion = @filemtime(__DIR__ . '/css/styles.css') ?: time();
if ($auth->currentUser() !== null && $_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: /');
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$auth->login($_POST['email'] ?? '', $_POST['password'] ?? '');
header('Location: /');
exit;
} catch (Throwable $e) {
$error = $e->getMessage();
}
}
header('Content-Type: text/html; charset=utf-8');
$loginText = $texts[$language];
$errorHtml = $error === ''
? ''
: RacketSandboxTemplate::renderFile('partials/message.html', array(
'class' => 'error',
'message' => $error,
));
echo RacketSandboxTemplate::renderFile('login.html', array(
'language' => $language,
'page_title' => $pageTitle,
'style_version' => $styleVersion,
'error_html' => $errorHtml,
'email_label' => $loginText['email'],
'password_label' => $loginText['password'],
'login_label' => $loginText['login'],
'account_title' => $loginText['account_title'],
'account_text' => $loginText['account_text'],
'account_link' => $loginText['account_link'],
));