web-base/index.php

93 lines
2.7 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
function getClassPath($class, $suffix=true) {
$path = str_replace('\\', '/', $class);
$suffix = ($suffix ? ".class" : "");
return "core/$path$suffix.php";
}
function getWebRoot() {
return dirname(__FILE__);
}
2020-02-10 00:52:25 +01:00
function createError($msg) {
return json_encode(array("success" => false, "msg" => $msg));
}
2020-02-09 23:02:19 +01:00
spl_autoload_extensions(".php");
spl_autoload_register(function($class) {
$full_path = getClassPath($class);
if(file_exists($full_path))
include_once $full_path;
else
include_once getClassPath($class, false);
});
include_once 'core/core.php';
include_once 'core/datetime.php';
include_once 'core/constants.php';
$config = new Configuration\Configuration();
$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 {
$apiFunction = strtoupper($apiFunction[0]) . substr($apiFunction, 1);
2020-02-10 12:16:34 +01:00
$apiFunction = str_replace("/", "\\", $apiFunction);
2020-02-10 00:52:25 +01:00
$class = "\\Api\\$apiFunction";
$file = getClassPath($class);
if(!file_exists($file)) {
header("404 Not Found");
$response = createError("Not found");
2020-02-10 12:16:34 +01:00
} else if(!is_subclass_of($class, \Api\Request::class)) {
header("400 Bad Request");
$response = createError("Inalid Method");
2020-02-10 00:52:25 +01:00
} else {
$request = new $class($user, true);
$success = $request->execute();
$msg = $request->getLastError();
$response = $request->getJsonResult();
}
}
}
2020-02-09 23:02:19 +01:00
} else {
2020-02-10 00:52:25 +01:00
if ($installation) {
$document = new Documents\Install($user);
} else {
2020-02-10 12:32:53 +01:00
$documentName = $_GET["site"];
if(empty($documentName) || strcasecmp($documentName, "install") === 0) {
$documentName = "home";
} else if(!preg_match("/[a-zA-Z]+(\/[a-zA-Z]+)*/", $documentName)) {
$documentName = "Document404";
}
$documentName = strtoupper($documentName[0]) . substr($documentName, 1);
$documentName = str_replace("/", "\\", $documentName);
$class = "\\Documents\\$documentName";
$file = getClassPath($class);
if(!file_exists($file) || !is_subclass_of($class, \Elements\Document::class)) {
$document = new \Documents\Document404($user);
} else {
$document = new $class($user);
}
2020-02-10 00:52:25 +01:00
}
$response = $document->getCode();
2020-02-09 23:02:19 +01:00
}
2020-02-10 00:52:25 +01:00
$user->sendCookies();
die($response);
2020-02-09 23:02:19 +01:00
?>