From f75ecb08a43536bbe18251fb20e21cfc14ade26e Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Mon, 10 Aug 2020 20:05:22 +0200 Subject: [PATCH] Add MD_STD, MDErrorReporter, MDAllowedValueSets phpcs-errors:226 phpunit-status:successful --- .gitmodules | 9 ++ classes/MDAllowedValueSets | 1 + classes/MDErrorReporter | 1 + classes/MD_STD | 1 + functions/functions.php | 176 ++++++++++++++++++++++++++++++++++++- 5 files changed, 186 insertions(+), 2 deletions(-) create mode 160000 classes/MDAllowedValueSets create mode 160000 classes/MDErrorReporter create mode 160000 classes/MD_STD diff --git a/.gitmodules b/.gitmodules index dcea89f..189fa48 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,12 @@ [submodule "mdAvailableLicenses"] path = 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 diff --git a/classes/MDAllowedValueSets b/classes/MDAllowedValueSets new file mode 160000 index 0000000..3d96d0d --- /dev/null +++ b/classes/MDAllowedValueSets @@ -0,0 +1 @@ +Subproject commit 3d96d0df89529c407fa013b054034c083b0d73ce diff --git a/classes/MDErrorReporter b/classes/MDErrorReporter new file mode 160000 index 0000000..c6aeec1 --- /dev/null +++ b/classes/MDErrorReporter @@ -0,0 +1 @@ +Subproject commit c6aeec1d2fb40654236a17b5f4f5feb28c7ee0f2 diff --git a/classes/MD_STD b/classes/MD_STD new file mode 160000 index 0000000..a09eba6 --- /dev/null +++ b/classes/MD_STD @@ -0,0 +1 @@ +Subproject commit a09eba6b843804c33eeff7205b8a376d4e46724b diff --git a/functions/functions.php b/functions/functions.php index 68c0216..1f7fa4c 100644 --- a/functions/functions.php +++ b/functions/functions.php @@ -8,6 +8,178 @@ */ 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 ($string) at $file: line_ $line _"; + $errorMsg .= $userMsg; + $errorMsg .= " |-- Error generating page: $errorPage"; + $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 = ' + + + + + + + + + + + '; + if (!empty($_SESSION['dark-theme'])) $output .= ' + '; + $output .= ' + Error :: '; + $output .= $versionName; + $output .= ' + + + +
+ +

' . $errorMsg . '

+ + + +
+ + +'; + + 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 ...
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. * @@ -101,7 +273,7 @@ function lang_getfrombrowser(array $allowed_languages, string $default_language, * * @return string */ -function printHTMLHead(string $injected = "") { +function printHTMLHead(string $injected = ""):string { $output = ' @@ -222,7 +394,7 @@ function getBlankRecordChannel():array { * * @return void */ -function rrmdir(string $dir) { +function rrmdir(string $dir):void { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) {