Major Fix, added new logic for pages, added redirect for env-error.php and db-error.php

This commit is contained in:
2026-04-30 02:03:51 +01:00
parent f32dff9b85
commit 3c9837bcb1
10 changed files with 67 additions and 117 deletions

6
.htaccess Normal file
View File

@@ -0,0 +1,6 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?page=$1 [QSA,L]

View File

@@ -12,7 +12,7 @@
define("INCLUDES_FOLD", ROOT_DIR . "includes/");
// - ASSETS
define("CSS_FOLD", ASSETS_FOLD . " css/");
define("CSS_FOLD", ASSETS_FOLD . "css/");
define("IMG_FOLD", ASSETS_FOLD . "img/");
define("JS_FOLD", ASSETS_FOLD . "js/");
define("FONTS_FOLD", CSS_FOLD . "fonts/");

View File

@@ -1,123 +1,52 @@
<?php
// CHECKING ERROR ENV AND PRINT RESULT
function error_env($option){
function error_env($option) {
global $env;
if (empty($env)){
$result="<p class='err_env_result'><span>ERROR:</span> Variable '<u>\$env</u>' is empty!</p>";
$help="<p class='err_env_help'>Please insert the correct value in the file 'config/init.php' or leave it to 'generic'</p>";
}else{
$result="<p class='err_env_result'><span>ERROR:</span> The environment '<u>{$env}</u>' not exists!</p>";
$help="<p class='err_env_help'>Please insert the correct value in the file 'config/init.php or leave it to 'generic'</p>";
if (empty($env)) {
$result = "<p class='err_env_result'><span>ERROR:</span> Variable '<u>\$env</u>' is empty!</p>";
$help = "<p class='err_env_help'>Please insert the correct value in the file 'config/init.php' or leave it to 'generic'</p>";
} else {
$result = "<p class='err_env_result'><span>ERROR:</span> The environment '<u>{$env}</u>' does not exist!</p>";
$help = "<p class='err_env_help'>Please insert the correct value in the file 'config/init.php' or leave it to 'generic'</p>";
}
if($option == "result"){
echo $result;
}elseif($option == "help"){
echo $help;
}else{
echo "ERROR: Wrong attribute in declared in the function!";
die;
}
echo $option === "result" ? $result : $help;
}
/**
* Get current page safely.
*/
function get_current_page() {
$page = $_GET["page"] ?? "home";
$page = strtolower(trim($page));
$page = preg_replace("/[^a-z0-9\-]/", "", $page);
if ($page === "") {
$page = "home";
}
function php_pages($option=null, $domain_shows=null){
// Required files or shows error if debug has been set to on.
if ($option == "error"){
$required_pages=[
PAGES_FOLD . "404.php",
PAGES_FOLD . "home.php"
];
return $page;
}
$missing_pages=[];
foreach($required_pages as $required){
if (! file_exists($required)){
array_push($missing_pages, $required);
}
}
/**
* Resolve page file.
*/
function resolve_page() {
$page = get_current_page();
$file = PAGES_FOLD . $page . ".php";
if (! empty($missing_pages)){
if (DEBUG){
echo "<h1>ERROR: Missing Required Pages:</h1>";
echo "<ul>";
foreach ($missing_pages as $pagepath){
echo "<li>Required file '" . $pagepath . "' is missing!</li>";
}
echo "</ul>";
}
die("Required pages are missing!.");
}
}
// 👉 rendi disponibile globalmente
$GLOBALS["current_page"] = $page;
if (isset($_GET["page"])){
// ---- OPTIONS IF PAGE HAS BEEN DECLARED ----
$pagecheck=$_GET["page"];
if (empty($pagecheck)){
$page="home";
}elseif(file_exists(PAGES_FOLD . $pagecheck . ".php")){
$page=$pagecheck;
}else{
$page="404";
}
// title for pages
if ( $domain_shows == "show" ){
$page_title=DOMAIN . " | " . ucfirst($page);
}else{
$page_title= ucfirst($page);
}
// return call
switch($option){
case "return_page":
return $page;
break;
case "return_title":
return $page_title;
break;
case "page":
echo $page;
break;
case "title":
echo $page_title;
break;
default:
echo $page;
}
}else{
// ---- OPTIONS IF PAGE HAS NOT BEEN DECLARED ----
$page = "home";
// title for pages
if ( $domain_shows == "show" ){
$page_title=DOMAIN . " | " . ucfirst($page);
}else{
$page_title= ucfirst($page);
}
// return call
switch($option){
case "return_page":
return $page;
break;
case "return_title":
return $page_title;
break;
case "page":
echo $page;
break;
case "title":
echo $page_title;
break;
default:
echo $page;
}
}
if (file_exists($file)) {
return $file;
}
?>
http_response_code(404);
$GLOBALS["current_page"] = "404";
return PAGES_FOLD . "404.php";
}

View File

@@ -15,6 +15,7 @@
/* ----------------------------------- */
default:
// Showing error page by default.
$error_env_check=true;
include("pages/env-error.php");
die;

View File

@@ -1,7 +1,7 @@
<?php
include("./config/init.php");
php_pages("error");
include(ROOT_DIR . "/templates/header.php");
include("./pages/home.php");
include("./templates/footer.php");
include(TEMPLATES_FOLD . "header.php");
include(resolve_page());
include(TEMPLATES_FOLD . "footer.php");
?>

View File

@@ -0,0 +1,4 @@
<section>
<h1>404</h1>
<p>Page not found.</p>
</section>

1
pages/about.php Normal file
View File

@@ -0,0 +1 @@
<h1>About</h1>

View File

@@ -1,3 +1,7 @@
<?php if ($db_needed !== "yes"){
header("Location: " . ROOT_URL );
}
?>
<!DOCTYPE html>
<html lang="en">
<head>

View File

@@ -1,3 +1,8 @@
<?php
if (! $error_env_check){
header("Location: " . ROOT_URL );
}
?>
<!DOCTYPE html>
<html lang="en">
<head>

View File

@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php include(INCLUDES_FOLD . "head_links.php"); ?>
<title><?php php_pages("title", "show"); ?></title>
<title><?php echo ucfirst($GLOBALS["current_page"] ?? "Home"); ?></title>
</head>
<body>
<header>