Add MD_STD, MDErrorReporter, MDAllowedValueSets

phpcs-errors:226 phpunit-status:successful
This commit is contained in:
Joshua Ramon Enslin 2020-08-10 20:05:22 +02:00 committed by Stefan Rohde-Enslin
parent f96b10748a
commit f75ecb08a4
5 changed files with 186 additions and 2 deletions

9
.gitmodules vendored
View File

@ -1,3 +1,12 @@
[submodule "mdAvailableLicenses"] [submodule "mdAvailableLicenses"]
path = mdAvailableLicenses path = mdAvailableLicenses
url = /var/www/vhosts/museum-digital.de/git-base/mdAvailableLicenses url = /var/www/vhosts/museum-digital.de/git-base/mdAvailableLicenses
[submodule "classes/MD_STD"]
path = classes/MD_STD
url = /var/www/vhosts/museum-digital.de/git-base/MD_STD
[submodule "classes/MDErrorReporter"]
path = classes/MDErrorReporter
url = /var/www/vhosts/museum-digital.de/git-base/MDErrorReporter
[submodule "classes/MDAllowedValueSets"]
path = classes/MDAllowedValueSets
url = /var/www/vhosts/museum-digital.de/git-base/MDAllowedValueSets

@ -0,0 +1 @@
Subproject commit 3d96d0df89529c407fa013b054034c083b0d73ce

@ -0,0 +1 @@
Subproject commit c6aeec1d2fb40654236a17b5f4f5feb28c7ee0f2

1
classes/MD_STD Submodule

@ -0,0 +1 @@
Subproject commit a09eba6b843804c33eeff7205b8a376d4e46724b

View File

@ -8,6 +8,178 @@
*/ */
declare(strict_types = 1); declare(strict_types = 1);
// Set autoloader
spl_autoload_register("mdCsvxmlAutoloader");
set_exception_handler("mdExceptionHandler");
set_error_handler("mdErrorHandler", E_ALL);
/**
* Autoloader for museum-digital.org.
*
* @param string $className Name of the class to load.
*
* @return void
*/
function mdCsvxmlAutoloader(string $className):void {
$classDirs = [
__DIR__ . "/../classes/MDTlLoader",
__DIR__ . "/../classes/MD_STD",
__DIR__ . "/../classes/MDAllowedValueSets/src",
__DIR__ . "/../classes/MDExportFormats/src",
__DIR__ . "/../classes/MDErrorReporter",
__DIR__ . "/../classes/MDErrorReporter/exceptions/generic",
__DIR__ . "/../classes/MDErrorReporter/exceptions/page",
__DIR__ . "/../classes/MDErrorReporter/exceptions/updates",
];
foreach ($classDirs as $classDir) {
if (file_exists("$classDir/$className.php")) {
include "$classDir/$className.php";
return;
}
}
}
/**
* Own error handler: Set to enforce exit on any error.
*
* @param integer $errno Error number.
* @param string $string Error message.
* @param string $file File in which the error occured.
* @param integer $line Line number.
*
* @return void
*/
function mdErrorHandler(int $errno, string $string, string $file, int $line):void {
$getStr = [];
foreach ($_GET as $key => $value) {
if (is_array($value)) continue;
$getStr[] = $key . "=" . $value;
}
$userMsg = "";
if (isset($_SESSION['anmnam'])) $userMsg .= " User: " . $_SESSION["anmnam"];
if (isset($_SESSION['username'])) $userMsg .= " (" . $_SESSION["username"] . ")";
if ($userMsg) $userMsg = " |--" . $userMsg;
$errorMsg = "";
if (!empty($_SERVER) && !empty($_SERVER["HTTP_HOST"])) {
$errorPage = $_SERVER['PHP_SELF'] . "?" . implode("&", $getStr);
$errorPageFull = "https://" . $_SERVER["HTTP_HOST"] . $errorPage;
$errorMsg = "*$errno (<a href='https://www.google.de/search?q=php+" . str_replace(" ", "+", $string) . "'>$string</a>) at $file: line_ $line _";
$errorMsg .= $userMsg;
$errorMsg .= " |-- Error generating page: <a href='$errorPageFull'>$errorPage</a>";
$errorMsg .= " |-- Used RAM / Peak RAM / Allowed: " . human_filesize((string)memory_get_usage()) . " / " . human_filesize((string)memory_get_peak_usage()) . " / " . ini_get("memory_limit");
$errorMsg = str_replace(PHP_EOL, " ", $errorMsg);
error_log($errorMsg);
}
if ($errno == E_ERROR) exit;
}
/**
* Exception handler to also be able to handle custom exceptions.
*
* @param Throwable $exception Exception.
*
* @return void
*/
function mdExceptionHandler(Throwable $exception):void {
$formatErrorPage = function(string $errorMsg = "", string $versionName = "") :string {
if (PHP_SAPI === "cli") {
return $errorMsg . PHP_EOL;
}
$output = '<!DOCTYPE html>
<html id="errorPage">
<head>
<script src="js/loadCaches.min.js?2020013517" type="text/javascript" async></script>
<script src="js/applyCaches.min.js?2020013517" type="text/javascript"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="manifest" href="./manifest.webmanifest" />
<link rel="shortcut icon" sizes="16x16 32x32" href="./db_images_gestaltung/mdlogo-32px.png" />
<link rel="shortcut icon" sizes="64x64" href="./db_images_gestaltung/mdlogo-64px.png" />
<link rel="stylesheet" type="text/css" href="css/main.min.css?2020013517" />
<link rel="stylesheet" type="text/css" href="css/errorPage.css" />';
if (!empty($_SESSION['dark-theme'])) $output .= '
<link rel="stylesheet" type="text/css" href="css/dark-theme.css?2020013517" />';
$output .= '
<title>Error :: ';
$output .= $versionName;
$output .= '</title>
</head>
<body>
<main>
<img src="./db_images_gestaltung/mdlogo-256px.png" />
<p>' . $errorMsg . '</p>
<nav>
<a href="index.php?t=home">Home</a>
<a href="index.php?t=museum">Museum</a>
<a href="index.php?t=collection">Collection</a>
<a href="index.php?t=exhibitions_overview">Exhibition</a>
<a href="index.php?t=events">Event</a>
<a href="index.php?t=topics">Topics</a>
<a href="index.php?t=listen&sv=+&done=yes">Objects</a>
</nav>
<nav>
<a href="index.php?t=kontakt">Contact</a>
<a href="index.php?t=impressum">Impressum</a>
<a href="index.php?t=privacy">Privacy Policy</a>
</nav>
</main>
</body>
</html>';
return $output;
};
$errorReporter = new MDErrorReporter("md:csvxml", "bugs-csvxml@museum-digital.de");
$errorCategory = MDErrorReporter::categorizeError($exception);
http_response_code(404);
switch ($errorCategory) {
case MDErrorReporter::MD_ERROR_KNOWN:
if (isset($_GET["output"]) and $_GET['output'] === "json") {
header('Content-type: application/json');
$output = [
"status" => "Error",
"msg" => $exception->getMessage(),
];
echo MD_STD::json_encode($output);
exit;
}
echo $formatErrorPage($exception->getMessage(), "");
exit;
default:
$errorReporter->sendErrorReport($exception, "joshua@museum-digital.de");
echo $formatErrorPage("Uncaught exception ...<br />Our team has been notified and will get to fixing this error shortly.", "");
exit;
}
}
/** /**
* Function lang_getfrombrowser gets the browser language based on HTTP headers. * Function lang_getfrombrowser gets the browser language based on HTTP headers.
* *
@ -101,7 +273,7 @@ function lang_getfrombrowser(array $allowed_languages, string $default_language,
* *
* @return string * @return string
*/ */
function printHTMLHead(string $injected = "") { function printHTMLHead(string $injected = ""):string {
$output = '<!DOCTYPE HTML> $output = '<!DOCTYPE HTML>
<html lang="en"> <html lang="en">
@ -222,7 +394,7 @@ function getBlankRecordChannel():array {
* *
* @return void * @return void
*/ */
function rrmdir(string $dir) { function rrmdir(string $dir):void {
if (is_dir($dir)) { if (is_dir($dir)) {
$objects = scandir($dir); $objects = scandir($dir);
foreach ($objects as $object) { foreach ($objects as $object) {