2020-02-09 23:02:19 +01:00
|
|
|
<?php
|
|
|
|
|
2020-04-03 18:09:01 +02:00
|
|
|
use Api\Request;
|
2020-06-18 15:35:09 +02:00
|
|
|
use Configuration\Configuration;
|
2020-04-03 18:09:01 +02:00
|
|
|
use Documents\Document404;
|
|
|
|
use Elements\Document;
|
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
include_once 'core/core.php';
|
|
|
|
include_once 'core/datetime.php';
|
|
|
|
include_once 'core/constants.php';
|
2020-02-10 00:52:25 +01: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");
|
2020-06-18 15:39:10 +02:00
|
|
|
die(json_encode(array( "success" => false, "msg" => "Configuration directory is not readable, check permissions before proceeding." )));
|
2020-06-18 15:35:09 +02:00
|
|
|
}
|
|
|
|
|
2020-02-09 23:02:19 +01:00
|
|
|
spl_autoload_extensions(".php");
|
|
|
|
spl_autoload_register(function($class) {
|
2020-06-18 15:35:09 +02:00
|
|
|
$full_path = getClassPath($class, true);
|
|
|
|
if(file_exists($full_path)) {
|
2020-02-09 23:02:19 +01:00
|
|
|
include_once $full_path;
|
2020-06-18 15:35:09 +02:00
|
|
|
} else {
|
2020-02-09 23:02:19 +01:00
|
|
|
include_once getClassPath($class, false);
|
2020-06-18 15:35:09 +02:00
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
});
|
|
|
|
|
2020-06-18 15:35:09 +02:00
|
|
|
$config = new Configuration();
|
2020-02-09 23:02:19 +01:00
|
|
|
$installation = (!$config->load());
|
|
|
|
$user = new Objects\User($config);
|
|
|
|
|
2020-02-10 00:52:25 +01:00
|
|
|
if(isset($_GET["api"]) && is_string($_GET["api"])) {
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
if($installation) {
|
|
|
|
$response = createError("Not installed");
|
|
|
|
} else {
|
|
|
|
$apiFunction = $_GET["api"];
|
|
|
|
if(empty($apiFunction)) {
|
|
|
|
header("403 Forbidden");
|
|
|
|
$response = "";
|
|
|
|
} else if(!preg_match("/[a-zA-Z]+(\/[a-zA-Z]+)*/", $apiFunction)) {
|
2020-02-10 12:16:34 +01:00
|
|
|
header("400 Bad Request");
|
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);
|
|
|
|
if(!file_exists($file)) {
|
|
|
|
header("404 Not Found");
|
|
|
|
$response = createError("Not found");
|
|
|
|
} else {
|
|
|
|
$parentClass = new ReflectionClass($parentClass);
|
|
|
|
$apiClass = new ReflectionClass($apiClass);
|
|
|
|
if(!$apiClass->isSubclassOf(Request::class) || !$apiClass->isInstantiable()) {
|
|
|
|
header("400 Bad Request");
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
} else {
|
2020-06-22 21:39:15 +02:00
|
|
|
$requestedUri = $_GET["site"] ?? $_SERVER["REQUEST_URI"];
|
|
|
|
if (startsWith($requestedUri, "/")) {
|
|
|
|
$requestedUri = substr($requestedUri, 1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-06-19 14:12:07 +02:00
|
|
|
$req = new \Api\Routes\Find($user);
|
2020-06-22 21:39:15 +02:00
|
|
|
$success = $req->execute(array("request" => $requestedUri));
|
2020-06-19 14:12:07 +02:00
|
|
|
$response = "";
|
|
|
|
if (!$success) {
|
2020-06-22 19:09:02 +02:00
|
|
|
http_response_code(500);
|
2020-06-19 14:12:07 +02:00
|
|
|
$response = "Unable to find route: " . $req->getLastError();
|
2020-02-10 12:32:53 +01:00
|
|
|
} else {
|
2020-06-19 14:12:07 +02:00
|
|
|
$route = $req->getResult()["route"];
|
|
|
|
if (is_null($route)) {
|
|
|
|
$response = (new Document404($user))->getCode();
|
|
|
|
} else {
|
|
|
|
$target = trim(explode("\n", $route["target"])[0]);
|
|
|
|
switch ($route["action"]) {
|
|
|
|
case "redirect_temporary":
|
2020-06-19 18:36:30 +02:00
|
|
|
http_response_code(307);
|
2020-06-19 14:12:07 +02:00
|
|
|
header("Location: $target");
|
|
|
|
break;
|
|
|
|
case "redirect_permanently":
|
2020-06-19 18:36:30 +02:00
|
|
|
http_response_code(308);
|
2020-06-19 14:12:07 +02:00
|
|
|
header("Location: $target");
|
|
|
|
break;
|
|
|
|
case "static":
|
2020-06-22 19:09:02 +02:00
|
|
|
$currentDir = dirname(__FILE__);
|
|
|
|
$response = serveStatic($currentDir, $target);
|
2020-06-19 14:12:07 +02:00
|
|
|
break;
|
|
|
|
case "dynamic":
|
|
|
|
$view = $route["extra"] ?? "";
|
|
|
|
$file = getClassPath($target);
|
|
|
|
if(!file_exists($file) || !is_subclass_of($target, Document::class)) {
|
2020-06-19 16:51:41 +02:00
|
|
|
$document = new Document404($user, $view);
|
2020-06-19 14:12:07 +02:00
|
|
|
} else {
|
2020-06-19 16:51:41 +02:00
|
|
|
$document = new $target($user, $view);
|
2020-06-19 14:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = $document->getCode();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 12:32:53 +01:00
|
|
|
}
|
2020-02-10 00:52:25 +01:00
|
|
|
|
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);
|