From 3c9837bcb11c76bcb6f67aaa8c883e17e1b54bb3 Mon Sep 17 00:00:00 2001
From: sld-admin
Date: Thu, 30 Apr 2026 02:03:51 +0100
Subject: [PATCH] Major Fix, added new logic for pages, added redirect for
env-error.php and db-error.php
---
.htaccess | 6 ++
config/constants.php | 2 +-
config/functions_init.php | 151 ++++++++++----------------------------
config/init.php | 1 +
index.php | 8 +-
pages/404.php | 4 +
pages/about.php | 1 +
pages/db-error.php | 4 +
pages/env-error.php | 5 ++
templates/header.php | 2 +-
10 files changed, 67 insertions(+), 117 deletions(-)
create mode 100644 .htaccess
create mode 100644 pages/about.php
diff --git a/.htaccess b/.htaccess
new file mode 100644
index 0000000..05f1d9f
--- /dev/null
+++ b/.htaccess
@@ -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]
\ No newline at end of file
diff --git a/config/constants.php b/config/constants.php
index dc2ffdb..ab60dd8 100644
--- a/config/constants.php
+++ b/config/constants.php
@@ -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/");
diff --git a/config/functions_init.php b/config/functions_init.php
index 5316b09..55ea01a 100644
--- a/config/functions_init.php
+++ b/config/functions_init.php
@@ -1,123 +1,52 @@
ERROR: Variable '\$env' is empty!
";
- $help="Please insert the correct value in the file 'config/init.php' or leave it to 'generic'
";
- }else{
- $result="ERROR: The environment '{$env}' not exists!
";
- $help="Please insert the correct value in the file 'config/init.php or leave it to 'generic'
";
+
+ if (empty($env)) {
+ $result = "ERROR: Variable '\$env' is empty!
";
+ $help = "Please insert the correct value in the file 'config/init.php' or leave it to 'generic'
";
+ } else {
+ $result = "ERROR: The environment '{$env}' does not exist!
";
+ $help = "Please insert the correct value in the file 'config/init.php' or leave it to 'generic'
";
}
- 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 "ERROR: Missing Required Pages:
";
- echo "";
- foreach ($missing_pages as $pagepath){
- echo "- Required file '" . $pagepath . "' is missing!
";
- }
- echo "
";
-
- }
- 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;
}
-
-?>
\ No newline at end of file
+
+ http_response_code(404);
+ $GLOBALS["current_page"] = "404";
+ return PAGES_FOLD . "404.php";
+}
\ No newline at end of file
diff --git a/config/init.php b/config/init.php
index 67e9d66..0081277 100644
--- a/config/init.php
+++ b/config/init.php
@@ -15,6 +15,7 @@
/* ----------------------------------- */
default:
// Showing error page by default.
+ $error_env_check=true;
include("pages/env-error.php");
die;
diff --git a/index.php b/index.php
index e222efc..acee08f 100644
--- a/index.php
+++ b/index.php
@@ -1,7 +1,7 @@
\ No newline at end of file
diff --git a/pages/404.php b/pages/404.php
index e69de29..6e6d4e0 100644
--- a/pages/404.php
+++ b/pages/404.php
@@ -0,0 +1,4 @@
+
+ 404
+ Page not found.
+
\ No newline at end of file
diff --git a/pages/about.php b/pages/about.php
new file mode 100644
index 0000000..7d877fd
--- /dev/null
+++ b/pages/about.php
@@ -0,0 +1 @@
+About
\ No newline at end of file
diff --git a/pages/db-error.php b/pages/db-error.php
index d86e7ca..9d92267 100644
--- a/pages/db-error.php
+++ b/pages/db-error.php
@@ -1,3 +1,7 @@
+
diff --git a/pages/env-error.php b/pages/env-error.php
index 619d9bc..02501ca 100644
--- a/pages/env-error.php
+++ b/pages/env-error.php
@@ -1,3 +1,8 @@
+
diff --git a/templates/header.php b/templates/header.php
index 4e0f6f9..c81b171 100644
--- a/templates/header.php
+++ b/templates/header.php
@@ -4,7 +4,7 @@
-
+