This commit is contained in:
Bruno Rybársky 2024-01-16 21:31:19 +01:00
parent 77ec0423fb
commit 372f42749c
3 changed files with 13 additions and 23 deletions

@ -9,4 +9,6 @@
$routerConfig["template_dir"] = "templates/";
$routerConfig["page_dir"] = "pages/";
$routerConfig["protocol"] = "https://";
}

@ -19,7 +19,7 @@ function generateNavigation()
}
$site_name = ucfirst($site_name);
$site_location = $routerRequest["protocol"] . $site_dir . "." . $routerRequest["domain"] . "." . $routerRequest["tld"] . "/" . $routerConfig["default_page"];
$site_location = $routerConfig["protocol"] . $site_dir . "." . $routerRequest["domain"] . "." . $routerRequest["tld"] . "/" . $routerConfig["default_page"];
if ($routerRequest["subdomain"] == $site_dir) {
//this is the current page
@ -37,7 +37,7 @@ function generateNavigation()
$page_class = "class=\"navpage_link active\"";
}
$page_location = $routerRequest["protocol"] . $site_dir . "." . $routerRequest["domain"] . "." . $routerRequest["tld"] . "/" . $page_dir;
$page_location = $routerConfig["protocol"] . $site_dir . "." . $routerRequest["domain"] . "." . $routerRequest["tld"] . "/" . $page_dir;
$page_name = str_replace("_", " ", $page_dir);
$page_name = explode(".", $page_name)[0];

@ -1,19 +1,10 @@
<?php
function getProtocol(){
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
return "https://";
} else {
/** @noinspection HttpUrlsUsage */
return "http://";
}
}
function initRouter(){
global $routerRequest;
global $routerConfig;
$routerRequest["protocol"] = getProtocol();
$routerRequest["requestAddress"] = array_slice(explode('.', $_SERVER['HTTP_HOST']), -3, 3); //get the last 3 elements
$needsRedirect = false;
@ -21,6 +12,11 @@ function initRouter(){
if(count($routerRequest["requestAddress"]) < 3){
// Root domain accessed directly
$needsRedirect = true;
$routerRequest["subdomain"] = $routerConfig["default_site"];
$routerRequest["domain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["tld"] = basename($routerRequest["requestAddress"][1]);
} else {
$routerRequest["subdomain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["domain"] = basename($routerRequest["requestAddress"][1]);
@ -28,25 +24,17 @@ function initRouter(){
$routerRequest["page_name"] = basename($_SERVER["QUERY_STRING"]);
if (empty($routerRequest["subdomain"])) {
// Subdomain is missing
$needsRedirect = true;
} elseif (empty($routerRequest["page_name"])) {
if (empty($routerRequest["page_name"])) {
// Page name is empty
$needsRedirect = true;
$routerRequest["page_name"] = $routerConfig["default_page"];
}
}
if ($needsRedirect) {
$redirectAddress = implode('.', $routerRequest["requestAddress"]);
if (empty($routerRequest["page_name"])) {
// Redirect without page name
header("Location: " . $routerRequest["protocol"] . $redirectAddress . "/");
} else {
// Redirect with default page name
header("Location: " . $routerRequest["protocol"] . $redirectAddress . "/" . $routerConfig["default_page"]);
}
exit; // Ensure that the script stops execution after the redirect header
header("Location: " . $routerConfig["protocol"] . $redirectAddress . "/" . $routerRequest["page_name"]);
}
return !$needsRedirect;