templates = json_decode(file_get_contents(TEMPLATE_CONF), true); $this->getTemplate(); } 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; $this->template_conf = file_get_contents(TEMPLATE_PATH . $this->templates[$template]["dir_name"] . "/template.json"); } 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"]); } $template = $response["data"]["template_name"]; $this->template_conf = json_decode(file_get_contents(TEMPLATE_PATH . $this->templates[$template]["dir_name"] . "/template.json"), true); $this->template = $template; } function setPage($page) { if (!array_key_exists($page, $this->template_conf)) { $this->showError("Page " . $page . " for template " . $this->template . " not found."); }else{ $this->page = $page; } } function showError($error) { $template_dir = $this->templates[$this->template]["dir_name"]; $template_conf = json_decode(file_get_contents(TEMPLATE_PATH . $template_dir . "/template.json"), true); $page = $template_conf["error"]; $html = file_get_contents(TEMPLATE_PATH . $template_dir . "/" . $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 .= ''; } $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; $tempalte_conf = $this->template_conf; $template_dir = $this->templates[$template]["dir_name"]; $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; if (isset($response["error"])) { $this->showError("Failed to get Application Name: API Returned an error: " . $response["error_text"]); } $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 ' . $file . ' for Template ' . $this->template . ' in ' . __FILE__ . " not found."); } // Add CSS Files to HTML $css .= ''; } $html = str_replace("%css%", $css, $html); // Insert Args foreach ($args as $key => $value) { $html = str_replace($key, $value, $html); } die($html); } }