stylehub/lib/routing.php

95 lines
3.1 KiB
PHP
Raw Normal View History

2024-01-16 19:24:40 +01:00
<?php
2024-01-16 20:43:57 +01:00
function initRouter(){
global $routerRequest;
global $routerConfig;
2024-01-16 19:39:54 +01:00
2024-01-16 20:43:57 +01:00
$routerRequest["requestAddress"] = array_slice(explode('.', $_SERVER['HTTP_HOST']), -3, 3); //get the last 3 elements
2024-01-16 19:39:54 +01:00
2024-01-16 20:43:57 +01:00
$needsRedirect = false;
2024-01-16 19:39:54 +01:00
2024-01-16 20:43:57 +01:00
if(count($routerRequest["requestAddress"]) < 3){
// Root domain accessed directly
$needsRedirect = true;
2024-01-16 21:31:19 +01:00
$routerRequest["subdomain"] = $routerConfig["default_site"];
$routerRequest["domain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["tld"] = basename($routerRequest["requestAddress"][1]);
2024-01-16 20:43:57 +01:00
} else {
$routerRequest["subdomain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["domain"] = basename($routerRequest["requestAddress"][1]);
$routerRequest["tld"] = basename($routerRequest["requestAddress"][2]);
2024-01-16 19:39:54 +01:00
2024-01-16 20:43:57 +01:00
$routerRequest["page_name"] = basename($_SERVER["QUERY_STRING"]);
2024-01-16 19:39:54 +01:00
2024-01-16 21:31:19 +01:00
if (empty($routerRequest["page_name"])) {
2024-01-16 21:20:20 +01:00
// Page name is empty
2024-01-16 20:43:57 +01:00
$needsRedirect = true;
2024-01-16 21:31:19 +01:00
$routerRequest["page_name"] = $routerConfig["default_page"];
2024-01-16 20:43:57 +01:00
}
}
2024-01-16 19:39:54 +01:00
2024-01-16 20:43:57 +01:00
if ($needsRedirect) {
2024-01-16 21:33:08 +01:00
$redirectAddress = $routerConfig["protocol"] .
$routerRequest["subdomain"] . "." .
$routerRequest["domain"] . "." .
$routerRequest["tld"] . "/" .
$routerRequest["page_name"];
2024-01-16 21:31:19 +01:00
// Redirect with default page name
2024-01-16 21:33:08 +01:00
header("Location: $redirectAddress");
2024-01-16 19:39:54 +01:00
}
2024-01-16 21:20:20 +01:00
2024-01-16 20:43:57 +01:00
return !$needsRedirect;
}
function renderDynamicPage($page_file)
{
require_once $page_file;
return render();
}
function getPage($page_name = null){
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-01-16 20:43:57 +01:00
$page = renderDynamicPage($dynamic_page_file_global);
2024-01-16 19:24:40 +01:00
}
elseif (file_exists($page_file_global)){
$page = file_get_contents($page_file_global);
}
elseif (file_exists($dynamic_page_file)){
2024-01-16 20:43:57 +01:00
$page = renderDynamicPage($dynamic_page_file);
2024-01-16 19:24:40 +01:00
}
elseif (file_exists($page_file)){
$page = file_get_contents($page_file);
}
else{
2024-01-16 20:43:57 +01:00
$page = file_get_contents($routerConfig["template_dir"] . "404.html");
2024-01-16 19:38:42 +01:00
}
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);
$out = str_replace("__TEMPLATE__PAGE__", $page, $out);
2024-01-16 20:43:57 +01:00
return str_replace("__TEMPLATE_PAGE_NAME__", $page_name, $out);
2024-01-16 19:24:40 +01:00
}