stylehub/lib/page.php

163 lines
5.1 KiB
PHP
Raw Normal View History

2024-01-16 19:24:40 +01:00
<?php
2024-02-01 09:10:55 +01:00
function renderDynamicPage($page_file): array
2024-01-16 20:43:57 +01:00
{
require_once $page_file;
2024-02-01 09:10:55 +01:00
$page_parameters = get_parameters();
$page_content = render();
return ["output" => $page_content, "parameters" => $page_parameters];
2024-01-16 20:43:57 +01:00
}
function parsePageTag($input): array
{
// Define the pattern for the tag
$pattern = '/<page\s+([^>]+)><\/page>/i';
// Check if the pattern matches the input
if (preg_match($pattern, $input, $matches)) {
// Extract parameters
$parameters = [];
if (preg_match_all('/(\w+)="([^"]+)"/', $matches[1], $paramMatches, PREG_SET_ORDER)) {
foreach ($paramMatches as $paramMatch) {
$parameters[$paramMatch[1]] = $paramMatch[2];
}
}
// Remove the tag from the input
$output = preg_replace($pattern, '', $input, 1);
return ['parameters' => $parameters, 'output' => $output];
}
// If no match is found, return the original input
return ['parameters' => [], 'output' => $input];
}
function getPage($page_name = null): array|false|string
{
2024-01-16 20:43:57 +01:00
global $routerConfig;
global $routerRequest;
2024-01-16 19:39:54 +01:00
2024-01-16 20:43:57 +01:00
if(!$page_name){
$page_name = $routerRequest["page_name"];
2024-01-16 19:39:54 +01:00
}
2024-01-16 19:24:40 +01:00
2024-01-16 20:57:51 +01:00
$dynamic_page_file = $routerConfig["page_dir"] . $routerRequest["subdomain"] . "/" . $page_name . ".php";
$page_file = $routerConfig["page_dir"] . $routerRequest["subdomain"] . "/" . $page_name . ".html";
2024-01-16 19:24:40 +01:00
2024-01-16 20:57:51 +01:00
$dynamic_page_file_global = $routerConfig["page_dir"] . "global/" . $page_name . ".php";
$page_file_global = $routerConfig["page_dir"] . "global/" . $page_name . ".html";
2024-01-16 19:24:40 +01:00
2024-01-16 20:43:57 +01:00
$skeleton = file_get_contents($routerConfig["template_dir"] . "skeleton.html");
$nav = file_get_contents($routerConfig["template_dir"] . "nav.html");
2024-01-16 19:24:40 +01:00
if (file_exists($dynamic_page_file_global)){
2024-02-01 09:10:55 +01:00
$pageMetadata = renderDynamicPage($dynamic_page_file_global);
$page = $pageMetadata["output"];
2024-01-16 19:24:40 +01:00
}
elseif (file_exists($page_file_global)){
2024-02-01 09:24:58 +01:00
$page_tmp = file_get_contents($page_file_global);
2024-02-01 09:10:55 +01:00
2024-02-01 09:24:58 +01:00
$pageMetadata = parsePageTag($page_tmp);
2024-02-01 09:10:55 +01:00
$page = $pageMetadata["output"];
2024-01-16 19:24:40 +01:00
}
elseif (file_exists($dynamic_page_file)){
2024-02-01 09:10:55 +01:00
$pageMetadata = renderDynamicPage($dynamic_page_file);
$page = $pageMetadata["output"];
2024-01-16 19:24:40 +01:00
}
elseif (file_exists($page_file)){
2024-02-01 09:24:58 +01:00
$page_tmp = file_get_contents($page_file);
2024-02-01 09:10:55 +01:00
2024-02-01 09:24:58 +01:00
$pageMetadata = parsePageTag($page_tmp);
2024-02-01 09:10:55 +01:00
$page = $pageMetadata["output"];
2024-01-16 19:24:40 +01:00
}
else{
2024-02-01 09:38:16 +01:00
$page_tmp = file_get_contents($routerConfig["template_dir"] . "404.html");
$pageMetadata = parsePageTag($page_tmp);
$page = $pageMetadata["output"];
http_response_code(404);
2024-01-16 19:38:42 +01:00
}
2024-02-01 09:24:58 +01:00
print_r($pageMetadata["parameters"]);
2024-02-01 09:27:50 +01:00
echo "<br>";
print_r($_SESSION);
if(!empty($pageMetadata["parameters"]["minimal_permission_level"])){
$page_required_permission = intval($pageMetadata["parameters"]["minimal_permission_level"]);
}
else{
$page_required_permission = $routerConfig["default_page_permission_level"];
}
if(!empty($pageMetadata["parameters"]["secret"])){
$is_secret_page = intval($pageMetadata["parameters"]["secret"]);
}
else{
$is_secret_page = $routerConfig["default_page_secret"];
}
if($page_required_permission < $_SESSION["privilegelevel"]){
if($is_secret_page == 1) {
2024-02-01 09:38:16 +01:00
$page_tmp = file_get_contents($routerConfig["template_dir"] . "404.html");
$pageMetadata = parsePageTag($page_tmp);
$page = $pageMetadata["output"];
2024-02-01 09:10:55 +01:00
http_response_code(404);
}
else{
2024-02-01 09:38:16 +01:00
$page_tmp = file_get_contents($routerConfig["template_dir"] . "403.html");
$pageMetadata = parsePageTag($page_tmp);
$page = $pageMetadata["output"];
2024-02-01 09:10:55 +01:00
http_response_code(403);
}
}
if(!empty($pageMetadata["parameters"]["page_title"])){
$page_title = $pageMetadata["parameters"]["page_title"];
}
else{
$page_title = $page_name;
}
2024-02-01 09:46:52 +01:00
$site_name = $routerRequest["subdomain"];
if(!empty($pageMetadata["parameters"]["page_style"])){
$style_location = $pageMetadata["parameters"]["page_style"];
}
else{
$style_location = "assets/styles/$site_name/$page_name.css";
}
if(file_exists($style_location)) {
$style_tag = $style_location;
}
else{
$style_tag = "";
}
if(!empty($pageMetadata["parameters"]["page_script"])){
$script_location = $pageMetadata["parameters"]["page_script"];
}
else{
$script_location = "assets/scripts/$site_name/$page_name.js";
}
if(file_exists($style_location)) {
$script_tag = $script_location;
}
else{
$script_tag = "";
}
2024-01-16 20:43:57 +01:00
$navpages = generateNavigation();
2024-01-16 19:38:42 +01:00
$nav = str_replace("__NAV_PAGES__", $navpages, $nav);
$out = $skeleton;
$out = str_replace("__TEMPLATE__NAV__", $nav, $out);
2024-02-01 09:46:52 +01:00
$out = str_replace("__TEMPLATE_PAGE_STYLE_TAG__", $style_tag, $out);
$out = str_replace("__TEMPLATE_PAGE_SCRIPT_TAG__", $script_tag, $out);
2024-01-16 19:38:42 +01:00
$out = str_replace("__TEMPLATE__PAGE__", $page, $out);
return str_replace("__TEMPLATE_PAGE_TITLE__", $page_title, $out);
2024-01-18 11:49:38 +01:00
}