// Include needed files
require_once(__DIR__."/config.php");
require_once(__DIR__."/classes/functions.php");
// Get page type
$page_data = (object) [];
$request = parse_url($_SERVER['REQUEST_URI']);
$path = trim($request['path']);
$page_type = get_page_type($path);
$controller = new controller;
$page_data->user = get_user();
// If a user is not found, redirect to the login page
if($page_data->user->found != 1 && $page_type != "welcome"){
header("Location: /welcome");
exit;
}
// Include controller if exists
$file_path = __DIR__."/controllers/".$page_type.".php";
if(file_exists($file_path)) include($file_path);
// Add 404 header if needed
if($page_type == '404'){
header("HTTP/1.0 404 Not Found");
}
switch($page_type){
// DEFAULT
default:
// Header file (meta data)
require_once(__DIR__.'/design/parts/header.php');
// Navigation file (top bar) (only don't show for the welcome page)
if($page_type != "welcome") require_once(__DIR__.'/design/parts/navigation.php');
// Body template
?>
include(__DIR__."/design/templates/".$page_type."_content.php");
?>
// Footer file
require_once(__DIR__.'/design/parts/footer.php');
}
// Get page type from path
function get_page_type($path){
// Path for each page
$regex = [
// Home page links
"^/index.html/?$" => "home",
"^/$" => "home",
"^/welcome/?$" => "welcome",
// Inventory
"^/inventory/?$" => "inventory",
// Items
"^/items/?$" => "items",
"^/items/new/?$" => "item",
"^/items/([0-9]+)/?$" => "item",
// Categories
"^/categories/?$" => "categories",
"^/categories/new/?$" => "category",
"^/categories/([a-z0-9-]+)/?$" => "category",
// Tags
"^/tags/?$" => "tags",
"^/tags/new/?$" => "tag",
"^/tags/([a-z0-9-]+)/?$" => "tag",
// Updates (Activity Log)
"^/updates/?$" => "updates",
"^/updates/new/?$" => "update",
"^/updates/([0-9]+)/?$" => "update",
// Profile
"^/profile/?$" => "profile"
]; // Associative array of key => values as regex => page_type
// Check each regex
$type = "404";
$params = [];
foreach($regex as $search => $replace){ // Loop through key => values to get the matching result
$search = str_replace("/", "\/", $search); // str_replace replaces whatever is in the 1st string, with whatever is in the 2nd string, inside the 3rd string
if(preg_match("/$search/", $path, $matches)){
$type = $replace;
// Store any captured groups (IDs, slugs, etc.)
if(count($matches) > 1){
$params = array_slice($matches, 1);
}
break;
}
}
// Store params globally for controllers
global $page_params;
$page_params = $params;
// Return page type
return $type;
}