add dberrornice template for database-errors

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2022-02-16 09:25:27 +01:00
parent 2ced9cdc2d
commit 3ba196fec6
3 changed files with 78 additions and 33 deletions

View File

@@ -441,7 +441,7 @@ class FroxlorInstall
return bin2hex(openssl_random_pseudo_bytes($length)); return bin2hex(openssl_random_pseudo_bytes($length));
} }
// if everything else fails, use unsafe fallback // if everything else fails, use unsafe fallback
return md5(uniqid(microtime(), 1)); return substr(md5(uniqid(microtime(), 1)), 0, $length);
} }
/** /**

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace Froxlor\Database; namespace Froxlor\Database;
/** /**
@@ -428,7 +429,7 @@ class Database
/** /**
* log error for reporting * log error for reporting
*/ */
$errid = substr(md5(microtime()), 5, 5); $errid = self::genUniqueToken();
$err_file = \Froxlor\FileDir::makeCorrectFile($sl_dir . "/" . $errid . "_sql-error.log"); $err_file = \Froxlor\FileDir::makeCorrectFile($sl_dir . "/" . $errid . "_sql-error.log");
$errlog = @fopen($err_file, 'w'); $errlog = @fopen($err_file, 'w');
@fwrite($errlog, "|CODE " . $error->getCode() . "\n"); @fwrite($errlog, "|CODE " . $error->getCode() . "\n");
@@ -453,42 +454,60 @@ class Database
} }
if ($showerror) { if ($showerror) {
// fallback
$theme = 'Sparkle';
// clean up sensitive data // clean up sensitive data
unset($sql); unset($sql);
unset($sql_root); unset($sql_root);
if ((isset($theme) && $theme != '') && !isset($_SERVER['SHELL']) || (isset($_SERVER['SHELL']) && $_SERVER['SHELL'] == '')) { if ((isset($theme) && $theme != '') && !isset($_SERVER['SHELL']) || (isset($_SERVER['SHELL']) && $_SERVER['SHELL'] == '')) {
// if we're not on the shell, output a nice error // if we're not on the shell, output a nice error
$_errtpl = dirname($sl_dir) . '/templates/' . $theme . '/misc/dberrornice.tpl'; $err_report_link = '';
if (file_exists($_errtpl)) {
$err_hint = file_get_contents($_errtpl);
// replace values
$err_hint = str_replace("<TEXT>", $error_message, $err_hint);
$err_hint = str_replace("<DEBUG>", $error_trace, $err_hint);
$err_hint = str_replace("<CURRENT_YEAR>", date('Y', time()), $err_hint);
$err_report_html = '';
if (is_array($userinfo) && (($userinfo['adminsession'] == '1' && \Froxlor\Settings::Get('system.allow_error_report_admin') == '1') || ($userinfo['adminsession'] == '0' && \Froxlor\Settings::Get('system.allow_error_report_customer') == '1'))) { if (is_array($userinfo) && (($userinfo['adminsession'] == '1' && \Froxlor\Settings::Get('system.allow_error_report_admin') == '1') || ($userinfo['adminsession'] == '0' && \Froxlor\Settings::Get('system.allow_error_report_customer') == '1'))) {
$err_report_html = '<a href="<LINK>" title="Click here to report error">Report error</a>'; $err_report_link = $linker->getLink(array(
$err_report_html = str_replace("<LINK>", $linker->getLink(array(
'section' => 'index', 'section' => 'index',
'page' => 'send_error_report', 'page' => 'send_error_report',
'errorid' => $errid 'errorid' => $errid
)), $err_report_html); ));
} }
$err_hint = str_replace("<REPORT>", $err_report_html, $err_hint);
// show // show
die($err_hint); \Froxlor\UI\Panel\UI::initTwig(true);
} \Froxlor\UI\Panel\UI::Twig()->addGlobal('install_mode', '1');
\Froxlor\UI\Panel\UI::TwigBuffer('misc/dberrornice.html.twig', [
'page_title' => 'Database error',
'message' => $error_message,
'debug' => $error_trace,
'report' => $err_report_link
]);
echo \Froxlor\UI\Panel\UI::TwigOutputBuffer();
die();
} }
die("We are sorry, but a MySQL - error occurred. The administrator may find more information in the syslog"); die("We are sorry, but a MySQL - error occurred. The administrator may find more information in the syslog");
} }
} }
/**
* generate safe unique token
*
* @param int $length
* @return string
*/
private static function genUniqueToken(int $length = 16)
{
if (!isset($length) || intval($length) <= 8) {
$length = 16;
}
if (function_exists('random_bytes')) {
return bin2hex(random_bytes($length));
}
if (function_exists('mcrypt_create_iv')) {
return bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
}
if (function_exists('openssl_random_pseudo_bytes')) {
return bin2hex(openssl_random_pseudo_bytes($length));
}
// if everything else fails, use unsafe fallback
return substr(md5(uniqid(microtime(), 1)), 0, $length);
}
/** /**
* Substitutes patterns in content. * Substitutes patterns in content.
* *

View File

@@ -0,0 +1,26 @@
{% extends "Froxlor/base.html.twig" %}
{% block body %}
<div class="container my-auto">
<div class="alert alert-danger fade show" role="alert">
<h4 class="alert-heading">
A database error occurred
</h4>
<p>
{{ message }}
</p>
{% if debug is not empty %}
<hr>
<p class="mb-0">
<pre>{{ debug }}</pre>
</p>
{% endif %}
<p class="mt-1 text-center">
<a href="#" class="btn btn-primary" title="Click here to go back" id="historyback">Go back</a>
{% if report is not empty %}
<a href="{{ report|raw }}" class="btn btn-warning" title="Click here to report error">Report error</a>
{% endif %}
</p>
</div>
</div>
{% endblock %}