Files
better_guestbook/public/html_loader.php
T

123 lines
4.0 KiB
PHP
Executable File

<?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
if (!array_key_exists($template, $this->templates)) {
die("Template " . $template . " not found.");
}
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) {
$template_dir = $this->templates[$this->template]["dir_name"];
$template_conf = json_decode(file_get_contents(TEMPLATE_DIR . $template_dir));
$page = $tempalte_conf["error"];
$html = file_get_contents(TEMPLATE_PATH . $page["dir_name"] . "/" . $page["html_file"]);
$css = "";
// Get CSS
foreach ($page["css_files"] as $file) {
// Check if File exists
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/" . $template_dir . "/" . $page["dir_name"] . "/" . $file . '">';
}
$html = str_replace("%css%", $css, $html);
// Write error
$replace = str_replace("%error%", $error, $html);
die($replace);
}
function show($args = [], $showAppName = true) {
if (!isset($this->template)) {
$this->getTemplate();
}
$template = $this->template;
$template_dir = $this->templates[$template]["dir_name"];
$template_conf = TEMPLATE_PATH . $template_dir . "/template.json";
$file = file_get_contents($template_conf);
$tempalte_conf = json_decode($file, true);
$page = $tempalte_conf[$this->page];
$html = file_get_contents(TEMPLATE_PATH . $template_dir . "/" . $page["dir_name"] . "/" . $page["html_file"]);
if ($showAppName) {
$call = new APICall("settings/appname.php");
$response = $call->response;
$app_name = $response["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 . $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/" . $template_dir . "/" . $page["dir_name"] . "/" . $file . '">';
}
$html = str_replace("%css%", $css, $html);
// Insert Args
foreach ($args as $key => $value) {
$html = str_replace($key, $value, $html);
}
die($html);
}
}