Files
Gazelle/gazelle.php
2026-10-31 11:41:31 +01:00

125 lines
3.8 KiB
PHP

<?php
// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
namespace Gazelle;
// 1. Initialization
require __DIR__ . '/lib/bootstrap.php';
global $Cache, $Debug, $Twig;
$Debug->mark('bootstrap complete');
register_shutdown_function(
function () {
if (preg_match(DEBUG_URI, $_SERVER['REQUEST_URI'])) {
require DEBUG_TRACE; /** @phpstan-ignore-line */
}
$error = error_get_last();
global $Debug;
if ($error['type'] == E_ERROR) {
$Debug->saveCase(str_replace(SERVER_ROOT . '/', '', $error['message']));
}
$Debug->storeMemory(memory_get_usage(true));
$Debug->storeDuration($Debug->duration() * 1000000);
}
);
// Get the user's actual IP address if they're proxied.
if (
!empty($_SERVER['HTTP_X_FORWARDED_FOR'])
&& proxyCheck($_SERVER['REMOTE_ADDR'])
&& filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
$context = new RequestContext(
$_SERVER['SCRIPT_NAME'],
$_SERVER['REMOTE_ADDR'],
$_SERVER['HTTP_USER_AGENT'] ?? '[no-useragent]',
);
if (!$context->isValid()) {
exit;
}
$module = $context->module();
if (
in_array($module, ['announce', 'scrape'])
|| isset($_REQUEST['info_hash'], $_REQUEST['peer_id'])
) {
die("d14:failure reason40:Invalid .torrent, try downloading again.e");
}
// 2. Do we have a viewer?
Base::setRequestContext($context);
$Viewer = $context->authn->authenticate();
$context->authn->updateIp($Viewer);
// 3. We have a viewer (or this is a login or registration attempt)
if ($Viewer) {
// these endpoints do not exist
if (in_array($module, OBSOLETE_ENDPOINTS)) {
$Viewer->session()->logoutEverywhere();
header('Location: login.php');
exit;
}
$authorized = ['ajax', 'chat', 'locked', 'login', 'logout', 'staffpm'];
if ($Viewer->isLocked() && !in_array($module, $authorized)) {
$Viewer->requestContext()->setModule('locked');
$module = 'locked';
} elseif ($Viewer->enforceMFA() && !in_array($module, [...$authorized, 'user'])) {
header("Location: user.php?action=mfa&do=configure&auth=" . $Viewer->auth());
exit;
}
}
if (DEBUG_TWIG) {
$Twig->addExtension(new \Twig\Extension\DebugExtension());
}
// for sections/tools/development/process_info.php
$Cache->cache_value('php_' . getmypid(), [
'start' => date('Y-m-d H:i:s'),
'document' => $module,
'query' => $_SERVER['QUERY_STRING'],
'get' => $_GET,
'post' => array_diff_key(
$_POST,
array_fill_keys(['password', 'new_pass_1', 'new_pass_2', 'verifypassword', 'confirm_password', 'ChangePassword', 'Password'], true)
)
], 600);
// 4. Display the page
header('Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0');
try {
$route = Router::resolve($context, $Cache, $Debug, $Twig);
$Debug->mark('call ' . $route::class);
$route->process();
} catch (Exception\EarlyExitException) {
// pass
} catch (\Error | \Exception $e) {
// if there was an ongoing transaction, abort it
if ($e::class === DB\MysqlException::class) {
DB::DB()->rollback();
}
new Manager\ErrorLog()->pg()->abort();
$errorLog = $Debug->saveError($e);
try {
Error500::error(
$Viewer?->permitted('site_debug')
? ($e->getMessage() . " ({$errorLog->link()})")
: "That is not supposed to happen. Check to see whether someone has created a thread in the the Bugs forum, or create a new thread to explain what you were doing and reference the Error ID {$errorLog->id}."
);
} catch (Exception\EarlyExitException) {
// pass
}
} finally {
if (!is_null($Viewer)) {
$Debug->profile($Viewer, isset($_REQUEST['profile']));
}
}