web-base/index.php

130 lines
4.0 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-04-03 18:09:01 +02:00
use Api\Request;
2020-06-18 15:35:09 +02:00
use Configuration\Configuration;
2022-05-31 16:14:49 +02:00
use Objects\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");
2021-04-07 19:43:22 +02:00
die(json_encode(array( "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-05-31 16:14:49 +02:00
// API routes, prefix: /api/
// TODO: move this to Router?
2022-02-20 16:53:26 +01:00
if (isset($_GET["api"]) && is_string($_GET["api"])) {
$isApiResponse = true;
if ($installation) {
2020-02-10 00:52:25 +01:00
$response = createError("Not installed");
} else {
$apiFunction = $_GET["api"];
2022-02-20 16:53:26 +01:00
if (empty($apiFunction) || $apiFunction === "/") {
$document = new \Elements\TemplateDocument($user, "swagger.twig");
$response = $document->getCode();
$isApiResponse = false;
2020-02-10 00:52:25 +01:00
} else if(!preg_match("/[a-zA-Z]+(\/[a-zA-Z]+)*/", $apiFunction)) {
2021-12-08 16:53:43 +01:00
http_response_code(400);
2020-02-10 00:52:25 +01:00
$response = createError("Invalid Method");
} else {
2020-06-20 20:13:51 +02:00
$apiFunction = array_filter(array_map('ucfirst', explode("/", $apiFunction)));
if (count($apiFunction) > 1) {
$parentClass = "\\Api\\" . reset($apiFunction) . "API";
$apiClass = "\\Api\\" . implode("\\", $apiFunction);
2020-02-10 00:52:25 +01:00
} else {
2020-06-20 20:13:51 +02:00
$apiClass = "\\Api\\" . implode("\\", $apiFunction);
$parentClass = $apiClass;
}
try {
$file = getClassPath($parentClass);
2020-06-26 23:32:45 +02:00
if(!file_exists($file) || !class_exists($parentClass) || !class_exists($apiClass)) {
2021-12-08 16:53:43 +01:00
http_response_code(404);
2020-06-20 20:13:51 +02:00
$response = createError("Not found");
} else {
$parentClass = new ReflectionClass($parentClass);
$apiClass = new ReflectionClass($apiClass);
if(!$apiClass->isSubclassOf(Request::class) || !$apiClass->isInstantiable()) {
2021-12-08 16:53:43 +01:00
http_response_code(400);
2020-06-20 20:13:51 +02:00
$response = createError("Invalid Method");
} else {
$request = $apiClass->newInstanceArgs(array($user, true));
$success = $request->execute();
$msg = $request->getLastError();
$response = $request->getJsonResult();
}
}
} catch (ReflectionException $e) {
$response = createError("Error instantiating class: $e");
2020-02-10 00:52:25 +01:00
}
}
2022-02-20 16:53:26 +01:00
if ($isApiResponse) {
header("Content-Type: application/json");
} else {
header("Content-Type: text/html");
}
2020-02-10 00:52:25 +01:00
}
2020-02-09 23:02:19 +01:00
} else {
2020-07-01 23:07:00 +02:00
2022-05-31 16:14:49 +02:00
// all other routes
$requestedUri = $_GET["site"] ?? $_SERVER["REQUEST_URI"];
$requestedUri = Router::cleanURL($requestedUri);
2020-06-22 21:39:15 +02:00
2020-02-10 00:52:25 +01:00
if ($installation) {
2020-06-22 21:39:15 +02:00
if ($requestedUri !== "" && $requestedUri !== "index.php") {
2020-04-02 21:19:06 +02:00
$response = "Redirecting to <a href=\"/\">/</a>";
header("Location: /");
} else {
$document = new Documents\Install($user);
2020-04-02 22:25:13 +02:00
$response = $document->getCode();
2020-04-02 21:19:06 +02:00
}
2020-02-10 00:52:25 +01:00
} else {
2020-02-10 12:32:53 +01:00
2022-05-31 16:14:49 +02:00
$router = null;
2022-05-31 16:14:49 +02:00
$routerCacheClass = '\Cache\RouterCache';
$routerCachePath = getClassPath($routerCacheClass);
if (is_file($routerCachePath)) {
@include_once $routerCachePath;
if (class_exists($routerCacheClass)) {
$router = new $routerCacheClass($user);
}
}
2022-05-31 16:14:49 +02:00
if ($router === null) {
$req = new \Api\Routes\GenerateCache($user);
if ($req->execute()) {
$router = $req->getRouter();
} else {
$message = "Unable to generate router cache: " . $req->getLastError();
$response = (new Router($user))->returnStatusCode(500, [ "message" => $message ]);
2020-06-19 14:12:07 +02:00
}
2020-02-10 12:32:53 +01:00
}
2020-02-10 00:52:25 +01:00
2022-05-31 16:14:49 +02:00
if ($router !== null) {
$response = $router->run($requestedUri);
}
2020-06-17 23:50:08 +02:00
$user->processVisit();
2020-04-02 22:25:13 +02:00
}
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);