stylehub/lib/navigation.php

141 lines
5.6 KiB
PHP
Raw Normal View History

2024-01-11 10:14:21 +01:00
<?php
/**
* Includes a PHP file that returns metadata associated with a dynamic page.
* It simply scopes an external file into a function to prevent variable conflicts.
*
* @param string $file The file path to the PHP file that contains metadata.
* @return array Returns an associative array of metadata from the included PHP file.
*/
function getDynamicMetadata(string $file): array{
2024-02-15 10:49:27 +01:00
return include($file);
2024-02-15 10:49:19 +01:00
}
/**
* Extracts and validates the minimal permission level required to access certain content,
* defaulting to system configuration if not properly set or in case of an error.
*
* @param array $metadata Metadata array that should include a 'parameters' key with 'minimal_permission_level'.
* @return int Returns the minimal permission level required to access a page.
*@global array $routerConfig Global router configuration settings.
*/
function getDynamicPermission(array $metadata): int {
2024-02-03 16:34:42 +01:00
global $routerConfig;
2024-02-15 10:50:22 +01:00
$params = $metadata["parameters"];
2024-02-03 16:46:09 +01:00
try {
2024-02-15 10:49:19 +01:00
$permission_level = $params["minimal_permission_level"];
2024-02-03 16:34:42 +01:00
2024-02-03 16:46:09 +01:00
if (!is_numeric($permission_level) || $permission_level <= 0) {
2024-02-06 16:24:57 +01:00
$permission_level = $routerConfig["page"]["default_permissions"];
2024-02-03 16:46:09 +01:00
}
}
catch (Exception){
2024-02-06 16:24:57 +01:00
$permission_level = $routerConfig["page"]["default_permissions"];
2024-02-03 16:46:09 +01:00
} finally {
return $permission_level;
2024-02-03 16:34:42 +01:00
}
2024-02-01 13:44:00 +01:00
}
/**
* Generates HTML navigation links for all sites and pages configured in the router,
* adjusting active states based on current request and filtering links by user permissions.
*
* @global array $routerConfig Global configuration that includes directory paths and default settings.
* @global array $routerRequest Current request details including site and page name.
* @return string Returns the HTML string of the navigation menu.
*/
function generateNavigation(): string
2024-01-12 16:20:17 +01:00
{
2024-01-16 20:43:57 +01:00
global $routerConfig;
global $routerRequest;
2024-02-05 22:26:02 +01:00
$nav = file_get_contents($routerConfig["template_dir"] . "nav.html");
2024-01-16 20:56:37 +01:00
$site_dirs = array_diff(scandir($routerConfig["page_dir"]), array('.', '..'));
2024-01-11 10:14:21 +01:00
2024-01-12 16:20:17 +01:00
$nav_out = "";
2024-01-11 10:14:21 +01:00
2024-01-12 16:20:17 +01:00
foreach ($site_dirs as $site_dir) {
2024-01-16 20:56:37 +01:00
$pages_dir = array_diff(scandir($routerConfig["page_dir"] . $site_dir), array('.', '..'));
2024-01-11 16:02:10 +01:00
2024-01-12 16:20:17 +01:00
$site_name = str_replace("_", " ", $site_dir);
2024-02-02 12:38:18 +01:00
$site_name = ucfirst($site_name);
2024-02-06 16:24:57 +01:00
$site_location = "/" . $site_dir . "/" . $routerConfig["default_page"];
2024-01-12 16:20:17 +01:00
2024-02-06 16:24:57 +01:00
if ($routerRequest["site_name"] == $site_dir) {
2024-01-12 16:20:17 +01:00
//this is the current page
$site_class = "class=\"navsite_link active\"";
}
2024-01-16 20:43:57 +01:00
else{
$site_class = "class=\"navsite_link\"";
}
2024-02-06 16:24:57 +01:00
$navigation_pages = "";
2024-02-02 00:37:10 +01:00
foreach ($pages_dir as $page_file) {
2024-02-03 16:08:26 +01:00
$page_file_tmp = explode(".", $page_file);
$page_basename = $page_file_tmp[0];
2024-01-12 16:20:17 +01:00
$page_class = "class=\"navpage_link\"";
2024-02-06 16:24:57 +01:00
if ($routerRequest["site_name"] == $site_dir && $routerRequest["page_name"] == $page_basename) {
2024-01-12 16:20:17 +01:00
$page_class = "class=\"navpage_link active\"";
}
2024-01-11 15:23:14 +01:00
2024-02-06 16:24:57 +01:00
$page_location = "/" . $site_dir . "/" . $page_basename;
2024-01-11 15:23:14 +01:00
2024-02-03 16:08:26 +01:00
$page_name = str_replace("_", " ", $page_basename);
2024-01-12 16:20:17 +01:00
$page_name = explode(".", $page_name)[0];
2024-01-17 11:06:02 +01:00
$page_name = ucfirst($page_name);
2024-02-15 10:49:19 +01:00
2024-02-02 00:37:10 +01:00
$page_file_path = $routerConfig["page_dir"] . $site_dir . "/" . $page_file ;
2024-02-03 16:08:26 +01:00
if($page_file_tmp[1] == "html"){
2024-02-02 00:37:10 +01:00
$page_tmp = file_get_contents($page_file_path);
2024-02-01 13:44:00 +01:00
$pageMetadata = parsePageTag($page_tmp);
if(!empty($pageMetadata["parameters"]["minimal_permission_level"])){
$page_required_permission = intval($pageMetadata["parameters"]["minimal_permission_level"]);
}
else{
2024-02-06 16:24:57 +01:00
$page_required_permission = $routerConfig["page"]["default_permissions"];
2024-02-01 13:44:00 +01:00
}
2024-02-15 10:49:19 +01:00
if(!empty($pageMetadata["parameters"]["page_title"])){
$page_name = $pageMetadata["parameters"]["page_title"];
}
2024-02-01 13:44:00 +01:00
}
2024-02-03 16:08:26 +01:00
elseif($page_file_tmp[1] == "php"){
2024-02-15 10:49:19 +01:00
$pageMetadata = getDynamicMetadata($page_file_path);
$page_required_permission = getDynamicPermission($pageMetadata);
if(!empty($pageMetadata["parameters"]["page_title"])){
$page_name = $pageMetadata["parameters"]["page_title"];
}
}
else{
2024-02-06 16:24:57 +01:00
$page_required_permission = $routerConfig["page"]["default_permissions"];
}
2024-02-06 16:24:57 +01:00
2024-02-03 16:08:26 +01:00
if($page_required_permission <= $_SESSION["privilege_level"]) {
2024-02-06 16:24:57 +01:00
$navpage_attributes = "data-site='$site_dir' data-page='$page_basename'";
$navigation_pages .= "<li class='navpage_item' $navpage_attributes ><a $navpage_attributes href='$page_location' $page_class>$page_name</a></li>";
}
}
2024-02-06 16:24:57 +01:00
if(!empty($navigation_pages)){
2024-02-05 21:29:29 +01:00
$default_page = $routerConfig["default_page"];
2024-02-06 16:24:57 +01:00
$navsite_attributes = "data-page='$default_page' data-site='$site_dir'";
$nav_out .= "<li class='navsite_item' ><a $navsite_attributes href='$site_location' $site_class>$site_name</a><ul class='navpage_list'>$navigation_pages</ul></li>";
2024-01-12 16:20:17 +01:00
}
2024-01-11 10:28:00 +01:00
}
2024-02-05 22:26:02 +01:00
return str_replace("__NAV_PAGES__", $nav_out, $nav);
2024-01-12 16:20:17 +01:00
}
/**
* Provides a simple API endpoint-like response for fetching generated navigation HTML.
* Wraps generateNavigation for an API.
*
* @return array Returns an associative array with the navigation HTML and a status indicating success.
*/
2024-02-05 21:21:04 +01:00
function getNavigationEndpoint() :array{
return [
"Status" => "Success",
"Navigation" => generateNavigation(),
];
}