Initial
This commit is contained in:
139
src/global_functions.php
Normal file
139
src/global_functions.php
Normal file
@ -0,0 +1,139 @@
|
||||
<?PHP
|
||||
/**
|
||||
* This file collects generally, often used functions in the public frontend for museum-digital
|
||||
*
|
||||
* @file
|
||||
*
|
||||
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
||||
*/
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* Autoloader for museum-digital.org.
|
||||
*
|
||||
* @param string $className Name of the class to load.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function mdShortenerAutoloader(string $className):void {
|
||||
|
||||
if ($className === 'MDMailerHelper') {
|
||||
include_once __DIR__ . '/../vendor/autoload.php';
|
||||
}
|
||||
|
||||
$classDirs = AUTOLOAD_DIRS;
|
||||
|
||||
foreach ($classDirs as $classDir) {
|
||||
if (\file_exists("$classDir/$className.php")) {
|
||||
include "$classDir/$className.php";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception handler to also be able to handle custom exceptions.
|
||||
*
|
||||
* @param Throwable $exception Exception.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function mdShortenerExceptionHandler(Throwable $exception):void {
|
||||
|
||||
$formatErrorPage = function(string $errorMsg = "") :string {
|
||||
|
||||
if (PHP_SAPI === "cli") {
|
||||
return $errorMsg . PHP_EOL;
|
||||
}
|
||||
|
||||
$output = '<!DOCTYPE html>
|
||||
<html id="errorPage">
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
|
||||
<title>Error :: URL shortener</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<main>
|
||||
<p>' . $errorMsg . '</p>
|
||||
</main>
|
||||
|
||||
</body>
|
||||
</html>';
|
||||
|
||||
return $output;
|
||||
};
|
||||
|
||||
$errorReporter = new MDErrorReporter("md:frontend", "bugs-frontend@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 ...<br />Our team has been notified and will get to fixing this error shortly.");
|
||||
exit;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 boolean
|
||||
*/
|
||||
function mdShortenerErrorHandler(int $errno, string $string, string $file, int $line):bool {
|
||||
|
||||
$getStr = [];
|
||||
foreach ($_GET as $key => $value) {
|
||||
if (is_array($value)) continue;
|
||||
$getStr[] = $key . "=" . $value;
|
||||
}
|
||||
# print_r(debug_backtrace());
|
||||
# exit;
|
||||
|
||||
$errorMsg = "";
|
||||
|
||||
if (!empty($_SERVER) && !empty($_SERVER["HTTP_HOST"])) {
|
||||
|
||||
$errorPage = $_SERVER['PHP_SELF'] . "?" . implode("&", $getStr);
|
||||
$errorPageFull = "https://" . $_SERVER["HTTP_HOST"] . $errorPage;
|
||||
|
||||
$errorMsg = "*$errno (<a href='https://www.google.de/search?q=php+" . str_replace(" ", "+", $string) . "'>$string</a>) at $file: line_ $line _";
|
||||
$errorMsg .= " |-- Error generating page: <a href='$errorPageFull'>$errorPage</a>";
|
||||
$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;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user