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

+58 -26
View File
@@ -1,33 +1,55 @@
<?php
class HTMLLoader {
private $template;
private $page;
private $templates;
private $tempalte_conf;
function __construct() {
// Check if Paths variables exists
if (!defined("TEMPLATE_PATH") || !defined("TEMPLATE_CONF") || !defined("APP_DOMAIN")) {
die("TEMPLATE_PATH or TEMPLATE_CONF are not defined at " . __FILE__);
}
$this->templates = json_decode(file_get_contents(TEMPLATE_CONF), true);
}
function setTemplate($template) {
// Check if template exists
$file = file_get_contents(TEMPLATE_CONF);
$json = json_decode($file, true);
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.");
if (!array_key_exists($template, $this->templates)) {
die("Template " . $template . " 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) {
$file = file_get_contents(TEMPLATE_CONF);
$json = json_decode($file, true);
$template_dir = $this->templates[$this->template]["dir_name"];
$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"]);
$css = "";
@@ -35,12 +57,12 @@ class HTMLLoader {
// Get CSS
foreach ($page["css_files"] as $file) {
// Check if File exists
if (!is_file(TEMPLATE_PATH . $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.");
if (!is_file(TEMPLATE_PATH . $template_dir . "/" . $page["dir_name"] . "/" . $file)) {
die("CSS File " . $file . " not found.");
}
// 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);
@@ -51,32 +73,42 @@ class HTMLLoader {
die($replace);
}
function show($args = []) {
function show($args = [], $showAppName = true) {
if (!isset($this->template)) {
$this->getTemplate();
}
$template = $this->template;
$file = file_get_contents(TEMPLATE_CONF);
$json = json_decode($file, true);
$template_dir = $this->templates[$template]["dir_name"];
$template_conf = TEMPLATE_PATH . $template_dir . "/template.json";
$page = $json[$template];
$html = file_get_contents(TEMPLATE_PATH . $page["dir_name"] . "/" . $page["html_file"]);
$file = file_get_contents($template_conf);
$tempalte_conf = json_decode($file, true);
$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"];
$page = $tempalte_conf[$this->page];
$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
$css = "";
foreach ($page["css_files"] as $file) {
// 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.");
}
// 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);