' . $errorMsg . '
+ + + +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 .= ' +' . $errorMsg . '
+ + + +