' . $errorMsg . '
$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((string)memory_get_usage()) . " / " . MD_STD::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 . '
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 .= '
";
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;
}