web-base/index.php

75 lines
2.2 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
2021-04-06 23:05:02 +02:00
include_once 'core/core.php';
include_once 'core/datetime.php';
include_once 'core/constants.php';
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);
2021-12-08 16:53:43 +01:00
serveStatic(WEBROOT, "/static/maintenance.html");
2021-04-06 23:05:02 +02:00
die();
}
2020-06-18 15:35:09 +02:00
use Configuration\Configuration;
2022-06-01 09:47:31 +02:00
use 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");
2022-06-01 09:47:31 +02:00
die(json_encode([ "success" => false, "msg" => "Configuration class is not readable, check permissions before proceeding." ]));
2020-06-18 15:35:09 +02:00
}
$config = new Configuration();
2020-02-09 23:02:19 +01:00
$user = new Objects\User($config);
2020-06-25 16:54:58 +02:00
$sql = $user->getSQL();
$settings = $config->getSettings();
$installation = !$sql || ($sql->isConnected() && !$settings->isInstalled());
2020-02-09 23:02:19 +01:00
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-06-01 12:28:50 +02:00
$document = new Documents\Install(new Router($user));
2022-06-01 09:47:31 +02:00
$response = $document->getCode();
}
} else {
2020-06-20 20:13:51 +02:00
2022-06-01 09:47:31 +02:00
$router = null;
$routerCacheClass = '\Cache\RouterCache';
$routerCachePath = getClassPath($routerCacheClass);
if (is_file($routerCachePath)) {
@include_once $routerCachePath;
if (class_exists($routerCacheClass)) {
$router = new $routerCacheClass($user);
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) {
$req = new \Api\Routes\GenerateCache($user);
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();
$response = (new Router($user))->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) {
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 {
2022-05-31 16:14:49 +02:00
$response = $router->run($requestedUri);
}
2020-04-02 22:25:13 +02:00
}
2022-06-01 09:47:31 +02:00
$user->processVisit();
2020-02-09 23:02:19 +01:00
}
2020-02-10 00:52:25 +01:00
$user->sendCookies();
2020-04-03 18:09:01 +02:00
die($response);