stylehub/lib/router.php

70 lines
2.0 KiB
PHP
Raw Normal View History

2024-01-18 11:49:38 +01:00
<?php
function initRouter(): bool
{
2024-01-18 11:49:38 +01:00
global $routerRequest;
global $routerConfig;
$routerRequest["requestAddress"] = array_slice(explode('.', $_SERVER['HTTP_HOST']), -3, 3); //get the last 3 elements
$needsRedirect = false;
if(count($routerRequest["requestAddress"]) < 3){
// Root domain accessed directly
$routerRequest["subdomain"] = $routerConfig["default_site"];
$routerRequest["domain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["tld"] = basename($routerRequest["requestAddress"][1]);
2024-02-02 15:53:32 +01:00
}
2024-02-02 15:55:23 +01:00
else {
$routerRequest["subdomain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["domain"] = basename($routerRequest["requestAddress"][1]);
$routerRequest["tld"] = basename($routerRequest["requestAddress"][2]);
2024-02-02 15:57:26 +01:00
if($routerRequest["subdomain"] == $routerConfig["default_site"]){
$routerRequest["subdomain"] = "";
2024-02-02 15:58:01 +01:00
$needsRedirect = true;
2024-02-02 15:57:26 +01:00
}
2024-02-02 15:55:23 +01:00
}
2024-02-02 15:57:26 +01:00
2024-02-02 15:53:32 +01:00
$routerRequest["page_name"] = basename($_SERVER["QUERY_STRING"]);
2024-01-18 11:49:38 +01:00
2024-02-02 15:53:32 +01:00
if (empty($routerRequest["page_name"])) {
// Page name is empty
$needsRedirect = true;
$routerRequest["page_name"] = $routerConfig["default_page"];
2024-01-18 11:49:38 +01:00
}
2024-02-02 15:53:32 +01:00
2024-01-18 11:49:38 +01:00
if ($needsRedirect) {
2024-02-02 15:59:36 +01:00
if(!empty($routerRequest["subdomain"])){
$sub_domain = $routerRequest["subdomain"] . ".";
}
else{
$sub_domain = "";
}
2024-01-18 11:49:38 +01:00
$redirectAddress = $routerConfig["protocol"] .
2024-02-02 15:59:36 +01:00
$sub_domain .
2024-01-18 11:49:38 +01:00
$routerRequest["domain"] . "." .
$routerRequest["tld"] . "/" .
$routerRequest["page_name"];
// Redirect with default page name
2024-02-02 16:23:31 +01:00
echo "false";
2024-02-02 16:23:54 +01:00
header("Location: $redirectAddress");
2024-02-02 16:22:36 +01:00
return false;
2024-01-18 11:49:38 +01:00
}
else{
if($_SERVER["REQUEST_METHOD"] == "POST"){
$routerRequest["type"] = "api";
}
if(empty($routerRequest["type"])){
$routerRequest["type"] = "page";
}
2024-02-02 16:23:31 +01:00
echo "true";
2024-02-02 16:22:36 +01:00
return true;
2024-01-18 11:49:38 +01:00
}
}