73 lines
2.2 KiB
PHP
73 lines
2.2 KiB
PHP
<?php
|
|
|
|
|
|
function initRouter(): bool
|
|
{
|
|
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]);
|
|
|
|
}
|
|
else {
|
|
$routerRequest["subdomain"] = basename($routerRequest["requestAddress"][0]);
|
|
$routerRequest["domain"] = basename($routerRequest["requestAddress"][1]);
|
|
$routerRequest["tld"] = basename($routerRequest["requestAddress"][2]);
|
|
|
|
if($routerRequest["subdomain"] == $routerConfig["default_site"]){
|
|
$routerRequest["subdomain"] = "";
|
|
$needsRedirect = true;
|
|
echo "cuz apex";
|
|
}
|
|
}
|
|
|
|
$routerRequest["page_name"] = basename($_SERVER["QUERY_STRING"]);
|
|
|
|
if (empty($routerRequest["page_name"])) {
|
|
// Page name is empty
|
|
$needsRedirect = true;
|
|
$routerRequest["page_name"] = $routerConfig["default_page"];
|
|
header("redirReason: cuz page name");
|
|
}
|
|
|
|
|
|
if ($needsRedirect) {
|
|
if(!empty($routerRequest["subdomain"])){
|
|
$sub_domain = $routerRequest["subdomain"] . ".";
|
|
}
|
|
else{
|
|
$sub_domain = "";
|
|
}
|
|
$redirectAddress = $routerConfig["protocol"] .
|
|
$sub_domain .
|
|
$routerRequest["domain"] . "." .
|
|
$routerRequest["tld"] . "/" .
|
|
$routerRequest["page_name"];
|
|
// Redirect with default page name
|
|
header("Location: $redirectAddress");
|
|
header("RedirHere: $redirectAddress");
|
|
header("redirReason: false");
|
|
return false;
|
|
}
|
|
else{
|
|
if($_SERVER["REQUEST_METHOD"] == "POST"){
|
|
$routerRequest["type"] = "api";
|
|
}
|
|
if(empty($routerRequest["type"])){
|
|
$routerRequest["type"] = "page";
|
|
}
|
|
echo "true";
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|