error handling

This commit is contained in:
Bruno Rybársky 2024-02-03 16:46:09 +01:00
parent a2edadd640
commit 0249aad0a3
5 changed files with 75 additions and 45 deletions

@ -1,41 +1,24 @@
<?php /** @noinspection PhpArrayIsAlwaysEmptyInspection */ <?php
/** @noinspection PhpArrayIsAlwaysEmptyInspection */ try {
/** @noinspection PhpArrayIsAlwaysEmptyInspection */ require_once 'lib/config.php';
/** @noinspection PhpIncludeInspection */ global $routerConfig;
require_once "secrets/config.php"; try {
require_once "lib/config.php"; require_once 'lib/main.php';
require_once "lib/navigation.php"; } catch (Exception $eMain) {
require_once "lib/router.php"; $page = file_get_contents($routerConfig["template_dir"] . "500.html");
require_once "lib/page.php"; $error_msg = "Error Message";
require_once "lib/endpoint.php"; try {
require_once "lib/account.php"; require_once 'lib/account.php';
if (isAdmin()) {
$routerConfig = array(); $error_msg = $eMain;
$routerRequest = array(); }
} catch (Exception) {
loadRouterConfig(); } finally {
$canRender = initRouter(); $page = str_replace("__TEMPLATE_ERROR__", $error_msg, $page);
if($canRender) { echo $page;
/** @noinspection PhpArrayIsAlwaysEmptyInspection */ http_response_code(500);
session_set_cookie_params(0, '/', "." . $routerRequest["domain"] . "." . $routerRequest["tld"], true, true); }
session_start();
if(!isLoggedIn()){
setDefaultSessionData();
} }
} catch (Exception $e){
if($routerRequest["type"] == "api") { echo $e;
echo getEndpoint($routerRequest["page_name"]); }
}elseif ($routerRequest["type"] == "page") {
/** @noinspection PhpArrayIsAlwaysEmptyInspection */
echo getPage($routerRequest["page_name"]);
}
else{
echo "Unknown request";
}
}
else{
exit();
}

37
lib/main.php Normal file

@ -0,0 +1,37 @@
<?php /** @noinspection PhpArrayIsAlwaysEmptyInspection */
/** @noinspection PhpArrayIsAlwaysEmptyInspection */
/** @noinspection PhpArrayIsAlwaysEmptyInspection */
/** @noinspection PhpIncludeInspection */
require_once "secrets/config.php";
require_once 'lib/config.php';
require_once "lib/navigation.php";
require_once "lib/router.php";
require_once "lib/page.php";
require_once "lib/endpoint.php";
require_once "lib/account.php";
$routerConfig = array();
$routerRequest = array();
loadRouterConfig();
$canRender = initRouter();
if($canRender) {
/** @noinspection PhpArrayIsAlwaysEmptyInspection */
session_set_cookie_params(0, '/', "." . $routerRequest["domain"] . "." . $routerRequest["tld"], true, true);
session_start();
if(!isLoggedIn()){
setDefaultSessionData();
}
if($routerRequest["type"] == "api") {
echo getEndpoint($routerRequest["page_name"]);
}elseif ($routerRequest["type"] == "page") {
/** @noinspection PhpArrayIsAlwaysEmptyInspection */
echo getPage($routerRequest["page_name"]);
}
else{
echo "Unknown request";
}
}

@ -2,13 +2,19 @@
function getDynamicPermission($file): int { function getDynamicPermission($file): int {
global $routerConfig; global $routerConfig;
$page_tmp = include_once($file); try {
$permission_level = $page_tmp["parameters"]["minimal_permission_level"]; $page_tmp = include_once($file);
$permission_level = $page_tmp["parameters"]["minimal_permission_level"];
if(!is_numeric($permission_level) || $permission_level <= 0){ if (!is_numeric($permission_level) || $permission_level <= 0) {
$permission_level = $routerConfig["default_page_permission_level"]; $permission_level = $routerConfig["default_page_permission_level"];
}
}
catch (Exception){
$permission_level = $routerConfig["default_page_permission_level"];
} finally {
return $permission_level;
} }
return $permission_level;
} }
function generateNavigation(): string function generateNavigation(): string

@ -97,6 +97,9 @@ function getPage($page_name = null): array|false|string
$page_tmp = file_get_contents($routerConfig["template_dir"] . "403.html"); $page_tmp = file_get_contents($routerConfig["template_dir"] . "403.html");
$pageMetadata = parsePageTag($page_tmp); $pageMetadata = parsePageTag($page_tmp);
$page = $pageMetadata["output"]; $page = $pageMetadata["output"];
if(!$page){
$page = "";
}
http_response_code(403); http_response_code(403);
} }
} }

@ -2,5 +2,6 @@
<h2>Niekto to pobabral</h2> <h2>Niekto to pobabral</h2>
<h1 class="error-code">500</h1> <h1 class="error-code">500</h1>
<h3><i class="ri-error-warning-line"></i> Nejaký neschopný vývojár nevedel robiť túto stránku. <i class="ri-error-warning-line"></i></h3> <h3><i class="ri-error-warning-line"></i> Nejaký neschopný vývojár nevedel robiť túto stránku. <i class="ri-error-warning-line"></i></h3>
<h4>__TEMPLATE_ERROR__</h4>
<a href="/index" class="back"><i class="ri-arrow-left-line"></i> SPÄŤ DOMOV</a> <a href="/index" class="back"><i class="ri-arrow-left-line"></i> SPÄŤ DOMOV</a>
</div> </div>