$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. * * @param array $allowed_languages Array containing all the languages for which * there are translations. * @param string $default_language Default language of the instance of MD. * @param string $lang_variable Currently set language variable. Optional. * @param boolean $strict_mode Whether to demand "de-de" (true) or "de" (false) Optional. * * @return string */ function lang_getfrombrowser(array $allowed_languages, string $default_language, string $lang_variable = "", bool $strict_mode = true):string { // $_SERVER['HTTP_ACCEPT_LANGUAGE'] verwenden, wenn keine Sprachvariable mitgegeben wurde if ($lang_variable === "") { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) $lang_variable = $_SERVER['HTTP_ACCEPT_LANGUAGE']; } // wurde irgendwelche Information mitgeschickt? if (empty($lang_variable)) { // Nein? => Standardsprache zurückgeben return $default_language; } // Den Header auftrennen $accepted_languages = preg_split('/,\s*/', $lang_variable); // Die Standardwerte einstellen $current_lang = $default_language; $current_q = 0; // Nun alle mitgegebenen Sprachen abarbeiten foreach ($accepted_languages as $accepted_language) { // Alle Infos über diese Sprache rausholen // phpcs:disable Generic.Strings.UnnecessaryStringConcat $res = preg_match('/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accepted_language, $matches); // phpcs:enable // war die Syntax gültig? if (!$res) { // Nein? Dann ignorieren continue; } // Sprachcode holen und dann sofort in die Einzelteile trennen $lang_code = explode('-', $matches[1]); // Wurde eine Qualität mitgegeben? if (isset($matches[2])) { // die Qualität benutzen $lang_quality = (float)$matches[2]; } else { // Kompabilitätsmodus: Qualität 1 annehmen $lang_quality = 1.0; } // Bis der Sprachcode leer ist... while (!empty($lang_code)) { // mal sehen, ob der Sprachcode angeboten wird if (in_array(strtolower(join('-', $lang_code)), $allowed_languages)) { // Qualität anschauen if ($lang_quality > $current_q) { // diese Sprache verwenden $current_lang = strtolower(join('-', $lang_code)); $current_q = $lang_quality; // Hier die innere while-Schleife verlassen break; } } // Wenn wir im strengen Modus sind, die Sprache nicht versuchen zu minimalisieren if ($strict_mode) { // innere While-Schleife aufbrechen break; } // den rechtesten Teil des Sprachcodes abschneiden array_pop($lang_code); } } // die gefundene Sprache zurückgeben return $current_lang; } /** * 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; }