Refactor API structure: update internal handling, enhance input retrieval, and improve template loading

This commit is contained in:
marc-go committed 2026-10-10 15:05:24 +02:00
1 parent a33de15f0c
commit 9cb50d7d28
13 files changed
+181 -95

No files matched your search

+2 -5
View File
@@ -1,7 +1,4 @@
COOKIE_TOKEN_SIGNATURE=628ef1d351018f7bb15dd5b5bc17fbc478fa6e7c7b993d136d5c03a4c2593c98 COOKIE_TOKEN_SIGNATURE=a11a9ea5fd839e616bd08ebc432f641a24ef1f4d74ca478f904e7a3122fc7a05
COOKIE_TOKEN_LIFETIME=86400 COOKIE_TOKEN_LIFETIME=86400
API_TOKEN_SIGNATURE=38bcd8a8e9ac3be83ddfcf4c12c19ccb3be70df00e48d43d4f63f54f014d1946
API_TOKEN_SIGNATURE=2740fbb0a179e0e9fd801798e21515cd8a20ae964e3ef0abdeaf92830eaeefb4
DB_FILE=database.db DB_FILE=database.db
+64 -13
View File
@@ -6,13 +6,17 @@ if ($debug) {
ini_set("display_startup_errors", 1); ini_set("display_startup_errors", 1);
error_reporting(E_ALL); error_reporting(E_ALL);
} }
debug_print_backtrace();
// API Main Library // API Main Library
class gbAPI { class gbAPI {
public $username; public $username;
public $input;
private $db; private $db;
private $config; private $config;
private $internal; private $internal;
private $response;
private function b64url_encode($data) { private function b64url_encode($data) {
return rtrim(strtr(base64_encode($data), "+/", "-_"), "="); return rtrim(strtr(base64_encode($data), "+/", "-_"), "=");
@@ -23,10 +27,13 @@ class gbAPI {
} }
function __construct($authRequired, $internal) { function __construct($authRequired, $internal) {
define("APP_PATH", __DIR__ . "/../"); if (!defined("APP_PATH")) {
define("APP_PATH", __DIR__ . "/../");
}
$this->internal = $internal; $this->internal = $internal;
if ($authRequired) { if ($authRequired && !$internal) {
if (!$this->checkAuth()) { if (!$this->checkAuth()) {
$status = 401; $status = 401;
$data = [ $data = [
@@ -39,6 +46,27 @@ class gbAPI {
} }
} }
function getInput() {
if (isset($this->input)) {
return $this->input;
}elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
try {
$input = json_decode(file_get_contents("php://input"), true);
} catch (JsonException $error) {
$this->answer(400, [
"error" => "invalid_body",
"error_text" => "Can not read request body: " . $error
]);
}
return $input;
}else{
$this->answer(400, [
"error" => "false_request_method",
"error_text" => "Only POST allowed"
], true);
}
}
function getConfig() { function getConfig() {
if (isset($this->config)) { if (isset($this->config)) {
return $config; return $config;
@@ -136,27 +164,49 @@ class gbAPI {
} }
function answer($status, $data = [], $error = false) { function answer($status, $data = [], $error = false) {
http_response_code($status);
header("Content-Type: application/json; charset=UTF-8");
if ($error) { if ($error) {
$json = []; $return = [];
$json["status"] = $status; $return["status"] = $status;
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
$json[$key] = $value; $return[$key] = $value;
} }
}else{ }else{
$json = [ $return = [
"status" => $status, "status" => $status,
"data" => $data "data" => $data
]; ];
} }
$return = json_encode($json); // Check if request is internal or external
die($return); if ($this->internal) {
$this->response = $return;
}else{
http_response_code($status);
header("Content-Type: application/json; charset=UTF-8");
$return = json_encode($return);
die($return);
}
}
function getAnswer() {
if (!isset($this->response)) {
$error = [
"status" => 500,
"error" => "empty_return",
"error_text" => "API Returned nothing."
];
return $error;
}
return $this->response;
}
function getResponse() {
return $this->response;
} }
function dbCommand($command, $expectResult, $stmtArgsRequired = false, $stmtArgs = []) { function dbCommand($command, $expectResult, $stmtArgsRequired = false, $stmtArgs = []) {
@@ -192,6 +242,7 @@ class gbAPI {
$command = "SELECT name, value FROM settings WHERE name = :name"; $command = "SELECT name, value FROM settings WHERE name = :name";
$value = $this->dbCommand($command, true, true, [":name" => $key]); $value = $this->dbCommand($command, true, true, [":name" => $key]);
return $value;
return $value[0]["value"];
} }
} }
+3 -2
View File
@@ -2,11 +2,12 @@
// Set internal to false so that the answer goes to the client // Set internal to false so that the answer goes to the client
if (!isset($internal)) { if (!isset($internal)) {
$internal = false; $internal = false;
define("API_DIR", "../");
} }
// Load Main Library // Load Main Library
require "../api.php"; require API_DIR . "api.php";
$api = new gbAPI(true); $api = new gbAPI(true, $internal);
$api->answer(200, [ $api->answer(200, [
"login" => true, "login" => true,
+4 -10
View File
@@ -2,22 +2,16 @@
// Set internal to false so that the answer goes to the client // Set internal to false so that the answer goes to the client
if (!isset($internal)) { if (!isset($internal)) {
$internal = false; $internal = false;
define("API_DIR", "../");
} }
// Load API Library // Load API Library
require "../api.php"; require API_DIR . "api.php";
$api = new gbAPI(false); $api = new gbAPI(false, $internal);
// Check Request Body // Check Request Body
if ($_SERVER["REQUEST_METHOD"] !== "POST") { $input = $api->getInput();
$api->answer(400, [
"error" => "false_request_method",
"error_text" => "Only POST allowed"
], true);
}
$input = json_decode(file_get_contents("php://input"), true);
if (!isset($input["username"]) || !isset($input["password"])) { if (!isset($input["username"]) || !isset($input["password"])) {
$api->answer(400, [ $api->answer(400, [
+4 -10
View File
@@ -2,21 +2,15 @@
// Set internal to false so that the answer goes to the client // Set internal to false so that the answer goes to the client
if (!isset($internal)) { if (!isset($internal)) {
$internal = false; $internal = false;
define("API_DIR", "../");
} }
// Load Main Library // Load Main Library
require "../api.php"; require API_DIR . "api.php";
$api = new gbAPI(false); $api = new gbAPI(false, $internal);
if ($_SERVER["REQUEST_METHOD"] !== "POST") { $input = $api->getInput();
$api->answer(400, [
"error" => "false_request_method",
"error_text" => "Only POST allowed"
]);
}
$input = json_decode(file_get_contents("php://input"), true);
if (!isset($input["name"]) || !isset($input["text"])) { if (!isset($input["name"]) || !isset($input["text"])) {
$api->answer(400, [ $api->answer(400, [
+3 -2
View File
@@ -2,12 +2,13 @@
// Set internal to false so that the answer goes to the client // Set internal to false so that the answer goes to the client
if (!isset($internal)) { if (!isset($internal)) {
$internal = false; $internal = false;
define("API_DIR", "../");
} }
// Load Main Library // Load Main Library
require "../api.php"; require API_DIR . "api.php";
$api = new gbAPI(false); $api = new gbAPI(false, $internal);
// Get Entrys // Get Entrys
$command = "SELECT * FROM entrys WHERE status = :status ORDER BY date DESC"; $command = "SELECT * FROM entrys WHERE status = :status ORDER BY date DESC";
+3 -2
View File
@@ -2,12 +2,13 @@
// Set internal to false so that the answer goes to the client // Set internal to false so that the answer goes to the client
if (!isset($internal)) { if (!isset($internal)) {
$internal = false; $internal = false;
define("API_DIR", "../");
} }
// Load Main Library // Load Main Library
require "../api.php"; require API_DIR . "api.php";
$api = new gbAPI(false); $api = new gbAPI(false, $internal);
// Get App Name // Get App Name
$command = "SELECT name, value FROM settings WHERE name = :name"; $command = "SELECT name, value FROM settings WHERE name = :name";
+12 -13
View File
@@ -1,18 +1,17 @@
<?php <?php
// Load Main Libary // Set internal to false so that the answer goes to the client
require "../api.php"; if (!isset($internal)) {
$internal = false;
$api = new gbAPI(false); define("API_DIR", "../");
// Check Request body
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
$api->answer(400, [
"error" => "false_request_method",
"error_text" => "Only POST allowed"
]);
} }
$input = json_decode(file_get_contents("php://input"), true); // Load Main Libary
require API_DIR . "api.php";
$api = new gbAPI(false, $internal);
// Check Request body
$input = $api->getInput();
if (!isset($input["username"]) || !isset($input["password"]) || !isset($input["password_repeat"])) { if (!isset($input["username"]) || !isset($input["password"]) || !isset($input["password_repeat"])) {
$api->answer(400, [ $api->answer(400, [
@@ -94,12 +93,12 @@ $env = "";
$cookie_token = bin2hex(random_bytes(32)); $cookie_token = bin2hex(random_bytes(32));
$env .= "COOKIE_TOKEN_SIGNATURE=" . $cookie_token . "\n"; $env .= "COOKIE_TOKEN_SIGNATURE=" . $cookie_token . "\n";
$env .= "COOKIE_TOKEN_LIFETIME=86400\n";
$api_token = bin2hex(random_bytes(32)); $api_token = bin2hex(random_bytes(32));
$env .= "API_TOKEN_SIGNATURE=" . $api_token . "\n"; $env .= "API_TOKEN_SIGNATURE=" . $api_token . "\n";
$env .= "DB_FILE=database.db"; $env .= "DB_FILE=database.db";
$env .= "COOKIE_TOKEN_LIFETIME=86400";
file_put_contents(APP_PATH . ".env", $env); file_put_contents(APP_PATH . ".env", $env);
+16
View File
@@ -0,0 +1,16 @@
<?php
// Set internal to false so that the answer goes to the client
if (!isset($internal)) {
$internal = false;
define("API_DIR", "../");
}
// Load Main Library
require API_DIR . "api.php";
$api = new gbAPI(false, $internal);
// Get Template
$template = $api->getSetting("template");
$api->answer(200, [
"template_name" => $template
]);
Executable → Regular
BIN
View File
Binary file not shown.
+58 -26
View File
@@ -1,33 +1,55 @@
<?php <?php
class HTMLLoader { class HTMLLoader {
private $template;
private $page; private $page;
private $templates;
private $tempalte_conf;
function __construct() { function __construct() {
// Check if Paths variables exists // Check if Paths variables exists
if (!defined("TEMPLATE_PATH") || !defined("TEMPLATE_CONF") || !defined("APP_DOMAIN")) { if (!defined("TEMPLATE_PATH") || !defined("TEMPLATE_CONF") || !defined("APP_DOMAIN")) {
die("TEMPLATE_PATH or TEMPLATE_CONF are not defined at " . __FILE__); die("TEMPLATE_PATH or TEMPLATE_CONF are not defined at " . __FILE__);
} }
$this->templates = json_decode(file_get_contents(TEMPLATE_CONF), true);
} }
function setTemplate($template) { function setTemplate($template) {
// Check if template exists // Check if template exists
$file = file_get_contents(TEMPLATE_CONF); if (!array_key_exists($template, $this->templates)) {
$json = json_decode($file, true); die("Template " . $template . " not found.");
if (!array_key_exists($template, $json) && array_key_exists("error", $json)) {
$this->showError('Template "' . $template . '" was not found.');
exit;
}elseif (!array_key_exists("error", $json)){
die("Error Template was not found.");
} }
$this->page = $template;
if (!is_dir(TEMPLATE_PATH . $this->templates[$template]["dir_name"])) {
die("Template " . $template . " not found.");
}
$this->template = $template;
}
function getTemplate() {
// Call API
$call = new APICall("template/get.php");
$response = $call->response;
if ($response["status"] !== 200) {
$this->setTemplate("standard");
$this->showError("Template API Returned an error: " . $response["error_text"]);
}
$this->template = $response["data"]["template_name"];
}
function setPage($page) {
$this->page = $page;
} }
function showError($error) { function showError($error) {
$file = file_get_contents(TEMPLATE_CONF); $template_dir = $this->templates[$this->template]["dir_name"];
$json = json_decode($file, true); $template_conf = json_decode(file_get_contents(TEMPLATE_DIR . $template_dir));
$page = $json["error"]; $page = $tempalte_conf["error"];
$html = file_get_contents(TEMPLATE_PATH . $page["dir_name"] . "/" . $page["html_file"]); $html = file_get_contents(TEMPLATE_PATH . $page["dir_name"] . "/" . $page["html_file"]);
$css = ""; $css = "";
@@ -35,12 +57,12 @@ class HTMLLoader {
// Get CSS // Get CSS
foreach ($page["css_files"] as $file) { foreach ($page["css_files"] as $file) {
// Check if File exists // Check if File exists
if (!is_file(TEMPLATE_PATH . $page["dir_name"] . "/" . $file)) { if (!is_file(TEMPLATE_PATH . $template_dir . "/" . $page["dir_name"] . "/" . $file)) {
$this->showError('CSS File <span class="code">' . $file . '</span> for Template <span class="code">error</span> in <span class="code">' . __FILE__ . "</span> not found."); die("CSS File " . $file . " not found.");
} }
// Add CSS Files to HTML // Add CSS Files to HTML
$css .= '<link rel="stylesheet" type="text/css" href="http://' . APP_DOMAIN . "/templates/" . $page["dir_name"] . "/" . $file . '">'; $css .= '<link rel="stylesheet" type="text/css" href="http://' . APP_DOMAIN . "/templates/" . $template_dir . "/" . $page["dir_name"] . "/" . $file . '">';
} }
$html = str_replace("%css%", $css, $html); $html = str_replace("%css%", $css, $html);
@@ -51,32 +73,42 @@ class HTMLLoader {
die($replace); die($replace);
} }
function show($args = []) { function show($args = [], $showAppName = true) {
if (!isset($this->template)) {
$this->getTemplate();
}
$template = $this->template; $template = $this->template;
$file = file_get_contents(TEMPLATE_CONF); $template_dir = $this->templates[$template]["dir_name"];
$json = json_decode($file, true); $template_conf = TEMPLATE_PATH . $template_dir . "/template.json";
$page = $json[$template]; $file = file_get_contents($template_conf);
$html = file_get_contents(TEMPLATE_PATH . $page["dir_name"] . "/" . $page["html_file"]); $tempalte_conf = json_decode($file, true);
$app_name = file_get_contents("http://" . APP_DOMAIN . "/api/settings/appname.php"); $page = $tempalte_conf[$this->page];
$app_name = json_decode($app_name, true);
$app_name = $app_name["data"]["application_name"];
$html = str_replace("%appname%", $app_name, $html); $html = file_get_contents(TEMPLATE_PATH . $template_dir . "/" . $page["dir_name"] . "/" . $page["html_file"]);
if ($showAppName) {
$app_name = file_get_contents("http://" . APP_DOMAIN . "/api/settings/appname.php");
$app_name = json_decode($app_name, true);
$app_name = $app_name["data"]["application_name"];
$html = str_replace("%appname%", $app_name, $html);
}
// Get CSS // Get CSS
$css = ""; $css = "";
foreach ($page["css_files"] as $file) { foreach ($page["css_files"] as $file) {
// Check if File exists // Check if File exists
if (!is_file(TEMPLATE_PATH . $page["dir_name"] . "/" . $file)) { if (!is_file(TEMPLATE_PATH . $template_dir . "/" . $page["dir_name"] . "/" . $file)) {
$this->showError('CSS File <span class="code">' . $file . '</span> for Template <span class="code">' . $this->template . '</span> in <span class="code">' . __FILE__ . "</span> not found."); $this->showError('CSS File <span class="code">' . $file . '</span> for Template <span class="code">' . $this->template . '</span> in <span class="code">' . __FILE__ . "</span> not found.");
} }
// Add CSS Files to HTML // Add CSS Files to HTML
$css .= '<link rel="stylessheet" text="text/css" href="' . APP_DOMAIN . "/templates/" . $page["dir_name"] . "/" . $file . '">'; $css .= '<link rel="stylessheet" text="text/css" href="' . APP_DOMAIN . "/templates/" . $template_dir . "/" . $page["dir_name"] . "/" . $file . '">';
} }
$html = str_replace("%css%", $css, $html); $html = str_replace("%css%", $css, $html);
+4 -5
View File
@@ -13,12 +13,11 @@ require FRONTEND_PATH . "html_loader.php";
$htmlLoader = new HTMLLoader(); $htmlLoader = new HTMLLoader();
// Build Entry Page // Build Entry Page
$htmlLoader->setTemplate("entrys_frontend"); $htmlLoader->setPage("entrys_frontend");
$url = "http://" . APP_DOMAIN . "/api/entrys/get.php"; // Get entrys
$response = file_get_contents($url); $call = new APICall("entrys/get.php");
$response = $call->response;
$response = json_decode($response, true);
if ($response["data"]["empty"]) { if ($response["data"]["empty"]) {
$html_block = "<p>There aren'n any entrys yet. Be the first one!</p>"; $html_block = "<p>There aren'n any entrys yet. Be the first one!</p>";
+8 -7
View File
@@ -12,7 +12,7 @@ class gb {
define("APP_DOMAIN", $_SERVER["SERVER_NAME"]); define("APP_DOMAIN", $_SERVER["SERVER_NAME"]);
define("APP_PATH", __DIR__ . "/../"); define("APP_PATH", __DIR__ . "/../");
define("FRONTEND_PATH", __DIR__ . "/"); define("FRONTEND_PATH", __DIR__ . "/");
define("API_PATH", __DIR__ . "/../api/"); define("API_DIR", __DIR__ . "/../api/");
define("TEMPLATE_PATH", FRONTEND_PATH . "templates/"); define("TEMPLATE_PATH", FRONTEND_PATH . "templates/");
define("TEMPLATE_CONF", TEMPLATE_PATH . "templates.json"); define("TEMPLATE_CONF", TEMPLATE_PATH . "templates.json");
@@ -28,29 +28,30 @@ class gb {
$html = new HTMLLoader(); $html = new HTMLLoader();
// Set template to Setup an show it // Set template to Setup an show it
$html->setTemplate("setup"); $html->setTemplate("standard");
$html->show(); $html->setPage("setup");
$html->show([], false);
} }
} }
// Internal API Call Library // Internal API Call Library
class APICall { class APICall {
private $response; public $response;
private $error; private $error;
function __construct($path, $data) { function __construct($path, $data = []) {
// Set $internal to true so the api return does not go to the browser // Set $internal to true so the api return does not go to the browser
$internal = true; $internal = true;
// Check Path // Check Path
if (!is_file(API_PATH . $path)) { if (!is_file(API_DIR . $path)) {
$this->error("API Path invalid."); $this->error("API Path invalid.");
return false; return false;
} }
// Call API // Call API
require API_PATH . $path; require API_DIR . $path;
// Get return from the API Library // Get return from the API Library
$response = $api->getAnswer(); $response = $api->getAnswer();