Major Fix, added new logic for pages, added redirect for env-error.php and db-error.php
This commit is contained in:
6
.htaccess
Normal file
6
.htaccess
Normal 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]
|
||||||
@@ -1,123 +1,52 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// CHECKING ERROR ENV AND PRINT RESULT
|
|
||||||
function error_env($option) {
|
function error_env($option) {
|
||||||
global $env;
|
global $env;
|
||||||
|
|
||||||
if (empty($env)) {
|
if (empty($env)) {
|
||||||
$result = "<p class='err_env_result'><span>ERROR:</span> Variable '<u>\$env</u>' is empty!</p>";
|
$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>";
|
$help = "<p class='err_env_help'>Please insert the correct value in the file 'config/init.php' or leave it to 'generic'</p>";
|
||||||
} else {
|
} else {
|
||||||
$result="<p class='err_env_result'><span>ERROR:</span> The environment '<u>{$env}</u>' not exists!</p>";
|
$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>";
|
$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 $option === "result" ? $result : $help;
|
||||||
echo $result;
|
|
||||||
}elseif($option == "help"){
|
|
||||||
echo $help;
|
|
||||||
}else{
|
|
||||||
echo "ERROR: Wrong attribute in declared in the function!";
|
|
||||||
die;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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"
|
|
||||||
];
|
|
||||||
|
|
||||||
$missing_pages=[];
|
/**
|
||||||
|
* Get current page safely.
|
||||||
|
*/
|
||||||
|
function get_current_page() {
|
||||||
|
$page = $_GET["page"] ?? "home";
|
||||||
|
|
||||||
foreach($required_pages as $required){
|
$page = strtolower(trim($page));
|
||||||
if (! file_exists($required)){
|
$page = preg_replace("/[^a-z0-9\-]/", "", $page);
|
||||||
array_push($missing_pages, $required);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! empty($missing_pages)){
|
if ($page === "") {
|
||||||
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!.");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($_GET["page"])){
|
|
||||||
// ---- OPTIONS IF PAGE HAS BEEN DECLARED ----
|
|
||||||
$pagecheck=$_GET["page"];
|
|
||||||
if (empty($pagecheck)){
|
|
||||||
$page = "home";
|
$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;
|
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" ){
|
* Resolve page file.
|
||||||
$page_title=DOMAIN . " | " . ucfirst($page);
|
*/
|
||||||
}else{
|
function resolve_page() {
|
||||||
$page_title= ucfirst($page);
|
$page = get_current_page();
|
||||||
|
$file = PAGES_FOLD . $page . ".php";
|
||||||
|
|
||||||
|
// 👉 rendi disponibile globalmente
|
||||||
|
$GLOBALS["current_page"] = $page;
|
||||||
|
|
||||||
|
if (file_exists($file)) {
|
||||||
|
return $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
// return call
|
http_response_code(404);
|
||||||
switch($option){
|
$GLOBALS["current_page"] = "404";
|
||||||
case "return_page":
|
return PAGES_FOLD . "404.php";
|
||||||
return $page;
|
|
||||||
break;
|
|
||||||
case "return_title":
|
|
||||||
return $page_title;
|
|
||||||
break;
|
|
||||||
case "page":
|
|
||||||
echo $page;
|
|
||||||
break;
|
|
||||||
case "title":
|
|
||||||
echo $page_title;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
echo $page;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
/* ----------------------------------- */
|
/* ----------------------------------- */
|
||||||
default:
|
default:
|
||||||
// Showing error page by default.
|
// Showing error page by default.
|
||||||
|
$error_env_check=true;
|
||||||
include("pages/env-error.php");
|
include("pages/env-error.php");
|
||||||
die;
|
die;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
include("./config/init.php");
|
include("./config/init.php");
|
||||||
php_pages("error");
|
|
||||||
include(ROOT_DIR . "/templates/header.php");
|
include(TEMPLATES_FOLD . "header.php");
|
||||||
include("./pages/home.php");
|
include(resolve_page());
|
||||||
include("./templates/footer.php");
|
include(TEMPLATES_FOLD . "footer.php");
|
||||||
?>
|
?>
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
<section>
|
||||||
|
<h1>404</h1>
|
||||||
|
<p>Page not found.</p>
|
||||||
|
</section>
|
||||||
1
pages/about.php
Normal file
1
pages/about.php
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<h1>About</h1>
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
<?php if ($db_needed !== "yes"){
|
||||||
|
header("Location: " . ROOT_URL );
|
||||||
|
}
|
||||||
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
if (! $error_env_check){
|
||||||
|
header("Location: " . ROOT_URL );
|
||||||
|
}
|
||||||
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<?php include(INCLUDES_FOLD . "head_links.php"); ?>
|
<?php include(INCLUDES_FOLD . "head_links.php"); ?>
|
||||||
<title><?php php_pages("title", "show"); ?></title>
|
<title><?php echo ucfirst($GLOBALS["current_page"] ?? "Home"); ?></title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
|
|||||||
Reference in New Issue
Block a user