web-base/index.php

104 lines
3.5 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
2022-11-18 18:06:46 +01:00
include_once 'Core/core.php';
include_once 'Core/datetime.php';
include_once 'Core/constants.php';
2021-04-06 23:05:02 +02:00
2021-12-08 16:53:43 +01:00
define("WEBROOT", realpath("."));
if (is_file("MAINTENANCE") && !in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'])) {
2021-04-06 23:05:02 +02:00
http_response_code(503);
2022-11-18 18:06:46 +01:00
\Core\Objects\Router\StaticFileRoute::serveStatic(WEBROOT . "/static/maintenance.html");
2021-04-06 23:05:02 +02:00
die();
}
2022-11-18 18:06:46 +01:00
use Core\Configuration\Configuration;
2023-01-07 15:34:05 +01:00
use Core\Objects\Context;
2022-11-18 18:06:46 +01:00
use Core\Objects\Router\Router;
2020-04-03 18:09:01 +02:00
2020-06-18 15:35:09 +02:00
if (!is_readable(getClassPath(Configuration::class))) {
2020-06-18 15:39:38 +02:00
header("Content-Type: application/json");
2024-04-11 17:51:50 +02:00
http_response_code(500);
die(json_encode(createError("Configuration class is not readable, check permissions before proceeding.")));
2020-06-18 15:35:09 +02:00
}
2023-01-07 15:34:05 +01:00
$context = Context::instance();
2022-06-20 19:52:31 +02:00
$sql = $context->initSQL();
$settings = $context->getSettings();
$context->parseCookies();
2020-02-09 23:02:19 +01:00
2024-04-11 17:51:50 +02:00
$currentHostName = getCurrentHostName();
2022-06-20 19:52:31 +02:00
$installation = !$sql || ($sql->isConnected() && !$settings->isInstalled());
2022-06-01 09:47:31 +02:00
$requestedUri = $_GET["site"] ?? $_GET["api"] ?? $_SERVER["REQUEST_URI"];
if ($installation) {
2022-06-01 12:28:50 +02:00
$requestedUri = Router::cleanURL($requestedUri);
2022-06-01 09:47:31 +02:00
if ($requestedUri !== "" && $requestedUri !== "index.php") {
$response = "Redirecting to <a href=\"/\">/</a>";
header("Location: /");
2020-02-10 00:52:25 +01:00
} else {
2022-11-18 18:06:46 +01:00
$document = new \Core\Documents\Install(new Router($context));
2022-08-20 22:17:17 +02:00
$response = $document->load();
2022-06-01 09:47:31 +02:00
}
} else {
2020-06-20 20:13:51 +02:00
2022-06-01 09:47:31 +02:00
$router = null;
2022-12-04 13:21:40 +01:00
$routerCacheClass = '\Site\Cache\RouterCache';
2022-06-01 09:47:31 +02:00
$routerCachePath = getClassPath($routerCacheClass);
if (is_file($routerCachePath)) {
@include_once $routerCachePath;
if (class_exists($routerCacheClass)) {
2022-06-20 19:52:31 +02:00
$router = new $routerCacheClass($context);
2020-02-10 00:52:25 +01:00
}
2022-06-01 09:47:31 +02:00
}
2022-02-20 16:53:26 +01:00
2022-06-01 09:47:31 +02:00
if ($router === null) {
2022-11-18 18:06:46 +01:00
$req = new \Core\API\Routes\GenerateCache($context);
2022-06-01 09:47:31 +02:00
if ($req->execute()) {
$router = $req->getRouter();
2022-02-20 16:53:26 +01:00
} else {
2022-06-01 09:47:31 +02:00
$message = "Unable to generate router cache: " . $req->getLastError();
2022-06-20 19:52:31 +02:00
$response = (new Router($context))->returnStatusCode(500, [ "message" => $message ]);
2022-02-20 16:53:26 +01:00
}
2020-02-10 00:52:25 +01:00
}
2020-06-22 21:39:15 +02:00
2022-06-01 09:47:31 +02:00
if ($router !== null) {
2024-04-12 16:10:33 +02:00
$logger = $router->getLogger();
2022-06-01 12:37:09 +02:00
if ((!isset($_GET["site"]) || $_GET["site"] === "/") && isset($_GET["error"]) &&
is_string($_GET["error"]) && preg_match("/^\d+$/", $_GET["error"])) {
2022-06-01 09:47:31 +02:00
$response = $router->returnStatusCode(intval($_GET["error"]));
2020-04-02 21:19:06 +02:00
} else {
try {
2024-04-11 17:51:50 +02:00
$pathParams = [];
$route = $router->run($requestedUri, $pathParams);
if ($route === null) {
$response = $router->returnStatusCode(404);
} else if (!$settings->isTrustedDomain($currentHostName)) {
2024-04-12 11:53:56 +02:00
$error = "Untrusted Origin. Adjust the 'trusted_domains' setting " .
"to include the current host '$currentHostName' or contact the administrator to resolve this issue";
2024-04-11 17:51:50 +02:00
if ($route instanceof \Core\Objects\Router\ApiRoute) {
header("Content-Type: application/json");
http_response_code(403);
2024-04-12 11:53:56 +02:00
$response = json_encode(createError($error));
2024-04-11 17:51:50 +02:00
} else {
2024-04-12 11:53:56 +02:00
$response = $router->returnStatusCode(403, ["message" => $error]);
2024-04-11 17:51:50 +02:00
}
2024-04-12 16:10:33 +02:00
$logger->warning("The site was accessed by an untrusted domain: $currentHostName");
2024-04-11 17:51:50 +02:00
} else {
$response = $route->call($router, $pathParams);
}
2024-04-07 18:29:33 +02:00
} catch (\Throwable $e) {
http_response_code(500);
2024-04-12 16:10:33 +02:00
$logger->error($e->getMessage());
$response = $router->returnStatusCode(500);
}
2022-05-31 16:14:49 +02:00
}
} else {
http_response_code(500);
$response = "Router could not be instantiated.";
2020-04-02 22:25:13 +02:00
}
2020-02-09 23:02:19 +01:00
}
2022-06-20 19:52:31 +02:00
$context->sendCookies();
2020-04-03 18:09:01 +02:00
die($response);