refactor error-reporting for admin/customer; remove now unused Froxlor\UI\Template class

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2022-03-27 12:44:44 +02:00
parent 2a91dafe20
commit 943301e908
6 changed files with 131 additions and 274 deletions

View File

@@ -284,7 +284,7 @@ if ($page == 'overview') {
$default_theme = $userinfo['theme'];
}
$themes_avail = \Froxlor\UI\Template::getThemes();
$themes_avail = UI::getThemes();
UI::view('user/change_theme.html.twig', [
'themes' => $themes_avail,
@@ -292,77 +292,7 @@ if ($page == 'overview') {
]);
}
} elseif ($page == 'send_error_report' && Settings::Get('system.allow_error_report_admin') == '1') {
// only show this if we really have an exception to report
if (isset($_GET['errorid']) && $_GET['errorid'] != '') {
$errid = $_GET['errorid'];
// read error file
$err_dir = \Froxlor\FileDir::makeCorrectDir(\Froxlor\Froxlor::getInstallDir() . "/logs/");
$err_file = \Froxlor\FileDir::makeCorrectFile($err_dir . "/" . $errid . "_sql-error.log");
if (file_exists($err_file)) {
$error_content = file_get_contents($err_file);
$error = explode("|", $error_content);
$_error = array(
'code' => str_replace("\n", "", substr($error[1], 5)),
'message' => str_replace("\n", "", substr($error[2], 4)),
'file' => str_replace("\n", "", substr($error[3], 5 + strlen(\Froxlor\Froxlor::getInstallDir()))),
'line' => str_replace("\n", "", substr($error[4], 5)),
'trace' => str_replace(\Froxlor\Froxlor::getInstallDir(), "", substr($error[5], 6))
);
// build mail-content
$mail_body = "Dear froxlor-team,\n\n";
$mail_body .= "the following error has been reported by a user:\n\n";
$mail_body .= "-------------------------------------------------------------\n";
$mail_body .= $_error['code'] . ' ' . $_error['message'] . "\n\n";
$mail_body .= "File: " . $_error['file'] . ':' . $_error['line'] . "\n\n";
$mail_body .= "Trace:\n" . trim($_error['trace']) . "\n\n";
$mail_body .= "-------------------------------------------------------------\n\n";
$mail_body .= "Froxlor-version: " . $version . "\n";
$mail_body .= "DB-version: " . $dbversion . "\n\n";
$mail_body .= "End of report";
$mail_html = nl2br($mail_body);
// send actual report to dev-team
if (isset($_POST['send']) && $_POST['send'] == 'send') {
// send mail and say thanks
$_mailerror = false;
try {
$mail->Subject = '[Froxlor] Error report by user';
$mail->AltBody = $mail_body;
$mail->MsgHTML($mail_html);
$mail->AddAddress('error-reports@froxlor.org', 'Froxlor Developer Team');
$mail->Send();
} catch (\PHPMailer\PHPMailer\Exception $e) {
$mailerr_msg = $e->errorMessage();
$_mailerror = true;
} catch (Exception $e) {
$mailerr_msg = $e->getMessage();
$_mailerror = true;
}
if ($_mailerror) {
// error when reporting an error...LOLFUQ
\Froxlor\UI\Response::standard_error('send_report_error', $mailerr_msg);
}
// finally remove error from fs
@unlink($err_file);
\Froxlor\UI\Response::redirectTo($filename);
}
// show a nice summary of the error-report
// before actually sending anything
eval("echo \"" . \Froxlor\UI\Template::getTemplate("index/send_error_report") . "\";");
} else {
\Froxlor\UI\Response::redirectTo($filename);
}
} else {
\Froxlor\UI\Response::redirectTo($filename);
}
require_once __DIR__ . '/error_report.php';
} elseif ($page == 'apikeys' && Settings::Get('api.enabled') == 1) {
require_once __DIR__ . '/api_keys.php';
} elseif ($page == '2fa' && Settings::Get('2fa.enabled') == 1) {

View File

@@ -204,7 +204,6 @@ if ($page == 'overview') {
} catch (Exception $e) {
\Froxlor\UI\Response::dynamic_error($e->getMessage());
}
}
$log->logAction(\Froxlor\FroxlorLogger::USR_ACTION, LOG_NOTICE, "changed default language to '" . $def_language . "'");
\Froxlor\UI\Response::redirectTo($filename);
@@ -240,7 +239,7 @@ if ($page == 'overview') {
$default_theme = $userinfo['theme'];
}
$themes_avail = \Froxlor\UI\Template::getThemes();
$themes_avail = UI::getThemes();
UI::view('user/change_theme.html.twig', [
'themes' => $themes_avail,
@@ -248,77 +247,7 @@ if ($page == 'overview') {
]);
}
} elseif ($page == 'send_error_report' && Settings::Get('system.allow_error_report_customer') == '1') {
// only show this if we really have an exception to report
if (isset($_GET['errorid']) && $_GET['errorid'] != '') {
$errid = $_GET['errorid'];
// read error file
$err_dir = \Froxlor\FileDir::makeCorrectDir(\Froxlor\Froxlor::getInstallDir() . "/logs/");
$err_file = \Froxlor\FileDir::makeCorrectFile($err_dir . "/" . $errid . "_sql-error.log");
if (file_exists($err_file)) {
$error_content = file_get_contents($err_file);
$error = explode("|", $error_content);
$_error = array(
'code' => str_replace("\n", "", substr($error[1], 5)),
'message' => str_replace("\n", "", substr($error[2], 4)),
'file' => str_replace("\n", "", substr($error[3], 5 + strlen(\Froxlor\Froxlor::getInstallDir()))),
'line' => str_replace("\n", "", substr($error[4], 5)),
'trace' => str_replace(\Froxlor\Froxlor::getInstallDir(), "", substr($error[5], 6))
);
// build mail-content
$mail_body = "Dear froxlor-team,\n\n";
$mail_body .= "the following error has been reported by a user:\n\n";
$mail_body .= "-------------------------------------------------------------\n";
$mail_body .= $_error['code'] . ' ' . $_error['message'] . "\n\n";
$mail_body .= "File: " . $_error['file'] . ':' . $_error['line'] . "\n\n";
$mail_body .= "Trace:\n" . trim($_error['trace']) . "\n\n";
$mail_body .= "-------------------------------------------------------------\n\n";
$mail_body .= "Froxlor-version: " . $version . "\n";
$mail_body .= "DB-version: " . $dbversion . "\n\n";
$mail_body .= "End of report";
$mail_html = str_replace("\n", "<br />", $mail_body);
// send actual report to dev-team
if (isset($_POST['send']) && $_POST['send'] == 'send') {
// send mail and say thanks
$_mailerror = false;
try {
$mail->Subject = '[Froxlor] Error report by user';
$mail->AltBody = $mail_body;
$mail->MsgHTML($mail_html);
$mail->AddAddress('error-reports@froxlor.org', 'Froxlor Developer Team');
$mail->Send();
} catch (\PHPMailer\PHPMailer\Exception $e) {
$mailerr_msg = $e->errorMessage();
$_mailerror = true;
} catch (Exception $e) {
$mailerr_msg = $e->getMessage();
$_mailerror = true;
}
if ($_mailerror) {
// error when reporting an error...LOLFUQ
\Froxlor\UI\Response::standard_error('send_report_error', $mailerr_msg);
}
// finally remove error from fs
@unlink($err_file);
\Froxlor\UI\Response::redirectTo($filename);
}
// show a nice summary of the error-report
// before actually sending anything
eval("echo \"" . \Froxlor\UI\Template::getTemplate("index/send_error_report") . "\";");
} else {
\Froxlor\UI\Response::redirectTo($filename);
}
} else {
\Froxlor\UI\Response::redirectTo($filename);
}
require_once __DIR__ . '/error_report.php';
} elseif ($page == 'apikeys' && Settings::Get('api.enabled') == 1) {
require_once __DIR__ . '/api_keys.php';
} elseif ($page == '2fa' && Settings::Get('2fa.enabled') == 1) {

99
error_report.php Normal file
View File

@@ -0,0 +1,99 @@
<?php
if (!defined('AREA')) {
header("Location: index.php");
exit();
}
/**
* This file is part of the Froxlor project.
* Copyright (c) 2022 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2016-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Panel
*
*/
use Froxlor\UI\Request;
use Froxlor\UI\Panel\UI;
// This file is being included in admin_domains and customer_domains
// and therefore does not need to require lib/init.php
$errid = Request::get('errorid');
if (!empty($errid)) {
// read error file
$err_dir = \Froxlor\FileDir::makeCorrectDir(\Froxlor\Froxlor::getInstallDir() . "/logs/");
$err_file = \Froxlor\FileDir::makeCorrectFile($err_dir . "/" . $errid . "_sql-error.log");
if (file_exists($err_file)) {
$error_content = file_get_contents($err_file);
$error = explode("|", $error_content);
$_error = array(
'code' => str_replace("\n", "", substr($error[1], 5)),
'message' => str_replace("\n", "", substr($error[2], 4)),
'file' => str_replace("\n", "", substr($error[3], 5 + strlen(\Froxlor\Froxlor::getInstallDir()))),
'line' => str_replace("\n", "", substr($error[4], 5)),
'trace' => str_replace(\Froxlor\Froxlor::getInstallDir(), "", substr($error[5], 6))
);
// build mail-content
$mail_body = "Dear froxlor-team,\n\n";
$mail_body .= "the following error has been reported by a user:\n\n";
$mail_body .= "-------------------------------------------------------------\n";
$mail_body .= $_error['code'] . ' ' . $_error['message'] . "\n\n";
$mail_body .= "File: " . $_error['file'] . ':' . $_error['line'] . "\n\n";
$mail_body .= "Trace:\n" . trim($_error['trace']) . "\n\n";
$mail_body .= "-------------------------------------------------------------\n\n";
$mail_body .= "User-Area: " . AREA . "\n";
$mail_body .= "Froxlor-version: " . $version . "\n";
$mail_body .= "DB-version: " . $dbversion . "\n\n";
$mail_body .= "End of report";
$mail_html = nl2br($mail_body);
// send actual report to dev-team
if (isset($_POST['send']) && $_POST['send'] == 'send') {
// send mail and say thanks
$_mailerror = false;
try {
$mail->Subject = '[Froxlor] Error report by user';
$mail->AltBody = $mail_body;
$mail->MsgHTML($mail_html);
$mail->AddAddress('error-reports@froxlor.org', 'Froxlor Developer Team');
$mail->Send();
} catch (\PHPMailer\PHPMailer\Exception $e) {
$mailerr_msg = $e->errorMessage();
$_mailerror = true;
} catch (Exception $e) {
$mailerr_msg = $e->getMessage();
$_mailerror = true;
}
if ($_mailerror) {
// error when reporting an error...LOLFUQ
\Froxlor\UI\Response::standard_error('send_report_error', $mailerr_msg);
}
// finally remove error from fs
@unlink($err_file);
\Froxlor\UI\Response::redirectTo($filename);
}
// show a nice summary of the error-report
// before actually sending anything
UI::view('user/error_report.html.twig', [
'mail_html' => $mail_body
]);
} else {
\Froxlor\UI\Response::redirectTo($filename);
}
} else {
\Froxlor\UI\Response::redirectTo($filename);
}

View File

@@ -18,6 +18,7 @@
use Froxlor\Database\Database;
use Froxlor\Settings;
use PHPMailer\PHPMailer;
use Froxlor\UI\Panel\UI;
/**
* checks if the new-version has some updating to do
@@ -399,7 +400,7 @@ function parseAndOutputPreconfig(&$has_preconfig, &$return, $current_version, $c
$description = 'As you can (obviously) see, Froxlor now comes with a new theme. You also have the possibility to switch back to "Classic" if you want to.';
$return['update_default_theme_note'] = ['type' => 'infotext', 'value' => $description];
$question = '<strong>Select default panel theme:</strong>';
$themes = \Froxlor\UI\Template::getThemes();
$themes = UI::getThemes();
$sel_themes = [];
foreach ($themes as $cur_theme) {
$sel_themes[$cur_theme] = $cur_theme;
@@ -421,7 +422,7 @@ function parseAndOutputPreconfig(&$has_preconfig, &$return, $current_version, $c
$description .= 'test this update in a testing environment using your existing data.<br /><br />';
$return['classic_theme_replacement_note'] = ['type' => 'infotext', 'value' => $description];
$question = '<strong>Select your preferred Classic Theme replacement:</strong>';
$themes = \Froxlor\UI\Template::getThemes();
$themes = UI::getThemes();
$sel_themes = [];
foreach ($themes as $cur_theme) {
$sel_themes[$cur_theme] = $cur_theme;

View File

@@ -1,127 +0,0 @@
<?php
namespace Froxlor\UI;
class Template
{
/**
* returns an array for the settings-array
*
* @return array
*/
public static function getThemes()
{
$themespath = \Froxlor\FileDir::makeCorrectDir(\Froxlor\Froxlor::getInstallDir() . '/templates/');
$themes_available = array();
if (is_dir($themespath)) {
$its = new \DirectoryIterator($themespath);
foreach ($its as $it) {
if ($it->isDir() && $it->getFilename() != '.' && $it->getFilename() != '..' && $it->getFilename() != 'misc') {
$theme = $themespath . $it->getFilename();
if (file_exists($theme . '/config.json')) {
$themeconfig = json_decode(file_get_contents($theme . '/config.json'), true);
if (array_key_exists('variants', $themeconfig) && is_array($themeconfig['variants'])) {
foreach ($themeconfig['variants'] as $variant => $data) {
if ($variant == "default") {
$themes_available[$it->getFilename()] = $it->getFilename();
} elseif (array_key_exists('description', $data)) {
$themes_available[$it->getFilename() . '_' . $variant] = $data['description'];
} else {
$themes_available[$it->getFilename() . '_' . $variant] = $it->getFilename() . ' (' . $variant . ')';
}
}
} else {
$themes_available[$it->getFilename()] = $it->getFilename();
}
}
}
}
}
return $themes_available;
}
/**
* Get template from filesystem
*
* @param
* string Templatename
* @param
* string noarea If area should be used to get template
* @return string The Template
* @author Florian Lippert <flo@syscp.org>
*
* @deprecated
*/
public static function getTemplate($template, $noarea = 0)
{
global $templatecache, $theme;
$fallback_theme = 'Sparkle';
if (! isset($theme) || $theme == '') {
$theme = $fallback_theme;
}
if ($noarea != 1) {
$template = AREA . '/' . $template;
}
if (! isset($templatecache[$theme][$template])) {
$filename = \Froxlor\Froxlor::getInstallDir() . 'templates/' . $theme . '/' . $template . '.tpl';
// check the current selected theme for the template
$templatefile = self::checkAndParseTpl($filename);
if ($templatefile == false && $theme != $fallback_theme) {
// check fallback
$_filename = \Froxlor\Froxlor::getInstallDir() . 'templates/' . $fallback_theme . '/' . $template . '.tpl';
$templatefile = self::checkAndParseTpl($_filename);
if ($templatefile == false) {
// check for old layout
$_filename = \Froxlor\Froxlor::getInstallDir() . 'templates/' . $template . '.tpl';
$templatefile = self::checkAndParseTpl($_filename);
if ($templatefile == false) {
// not found
$templatefile = 'TEMPLATE NOT FOUND: ' . $filename;
}
}
}
$output = $templatefile;
$templatecache[$theme][$template] = $output;
}
return $templatecache[$theme][$template];
}
/**
* check whether a tpl file exists and if so, return it's content or else return false
*
* @param string $filename
*
* @return string|bool content on success, else false
*/
private static function checkAndParseTpl($filename)
{
$templatefile = "";
if (file_exists($filename) && is_readable($filename)) {
$templatefile = addcslashes(file_get_contents($filename), '"\\');
// loop through template more than once in case we have an "if"-statement in another one
while (preg_match('/<if[ \t]*(.*)>(.*)(<\/if>|<else>(.*)<\/if>)/Uis', $templatefile)) {
$templatefile = preg_replace('/<if[ \t]*(.*)>(.*)(<\/if>|<else>(.*)<\/if>)/Uis', '".( ($1) ? ("$2") : ("$4") )."', $templatefile);
$templatefile = str_replace('\\\\', '\\', $templatefile);
}
return $templatefile;
}
return false;
}
}

View File

@@ -0,0 +1,25 @@
{% extends "Froxlor/userarea.html.twig" %}
{% block heading %}
<div>
<h5 class="mb-1">
<i class="fa-solid fa-bug me-1"></i>
{{ lng('error.send_report_title') }}
</h5>
</div>
{% endblock %}
{% block content %}
<form action="{{ linker({'section':'index','page':'send_error_report','errorid':errorid}) }}" class="col-12 d-flex flex-column" method="post" enctype="application/x-www-form-urlencoded">
<div class="alert alert-primary" role="alert">{{ lng('error.send_report_desc')|raw }}</div>
<code class="border rounded bg-white p-2 mb-3">{{ mail_html|nl2br }}</code>
<div>
<input type="hidden" name="send" value="send"/>
<div class="col-12 text-end">
<button type="submit" class="btn btn-primary">{{ lng('error.send_report') }}</button>
</div>
</div>
</form>
{% endblock %}