$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: " . MD_STD::human_filesize(memory_get_usage()) . " / " . MD_STD::human_filesize(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 for generating the HTML head. * * @param string $injected Additional code to inject into the head, e.g. a * reference to JS files. * * @return string */ function printHTMLHead(string $injected = ""):string { $output = ' CSVXML :: museum-digital '; $output .= $injected; $output .= '

museum-digital:csvxml

'; return $output; } /** * Function generateHelpTooltip returns a tooltip for hovering over using the common settings. * * @param string $identifier ID attribute of the tooltip. * @param string $title Title of the tooltip. * @param string $explica More in-depth explanation: body of the tooltip. * @param boolean $setParagraph If set to true (default), the content of the tooltip will be put into a

element. Optional. * * @return array */ function generateHelpTooltip(string $identifier, string $title, string $explica, bool $setParagraph = true):array { $outputTag = ''; $output = ''; if ($setParagraph) $output .= '

'; $output .= $explica; if ($setParagraph) $output .= '

'; $output .= ''; return [$output, $outputTag]; } /** * Outputs a DOMDocument with correct header and then aborts. * Used mainly for debugging. * * @param DOMDocument $xmlDoc XML object. * * @return string */ function printDOMDocToXML(DOMDocument $xmlDoc):string { return '' . $xmlDoc->saveXML($xmlDoc->documentElement); } /** * Function for creating a DOMElement with a text node inside. * * @param DOMDocument $xmlDoc XML document. * @param string $tag Tag. * @param string $content Text content. * * @return DOMElement */ function createTextDomElement(DOMDocument $xmlDoc, string $tag, string $content):DOMElement { try { $element = $xmlDoc->createElement($tag); } catch (DOMException $e) { echo "Error at " . __FILE__ . ", line #" . __LINE__ . PHP_EOL . "
"; echo "Cannot create DOM element for $tag / $content"; exit; } $element->appendChild($xmlDoc->createTextNode($content)); return $element; } /** * Function for creating a DOMDocument record channel. * * @return array */ function getBlankRecordChannel():array { $xmlDoc = new DOMDocument("1.0", "UTF-8"); $xmlMainElem = $xmlDoc->createElement("record"); $record_node = $xmlDoc->appendChild($xmlMainElem); //add RSS element to XML node return [$xmlDoc, $record_node]; } /** * Function for removing a directory with all its contents. * * @param string $dir File path of the directory to remove. * * @return void */ function rrmdir(string $dir):void { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir . "/" . $object) == "dir") rrmdir($dir . "/" . $object); else unlink($dir . "/" . $object); } } reset($objects); rmdir($dir); } } /** * Function for checking if two arrays have identical values / contents. * * @param array $arrayA First array to compare. * @param array $arrayB Second array to compare. * * @return boolean */ function identical_values(array $arrayA, array $arrayB):bool { sort($arrayA); sort($arrayB); return $arrayA == $arrayB; } /** * Function for retrieving the anti-csrf token or generating it if need be. * * @return string */ function getAntiCsrfToken():string { if (empty($_SESSION['csrf-token'])) { $_SESSION['csrf-token'] = bin2hex(random_bytes(32)); } return $_SESSION['csrf-token']; } /** * Function for validating anti-csrf tokens. Each anti-csrf token is removed * after use. * * @return boolean */ function validateAntiCsrfToken():bool { $validity = false; if (!empty($_POST['csrf-token']) && !empty($_SESSION['csrf-token']) && hash_equals($_SESSION['csrf-token'], $_POST['csrf-token']) === true ) { $validity = true; } $_SESSION['csrf-token'] = null; unset($_SESSION['csrf-token']); return $validity; }