From 943301e9083ecc54580feb44ca30168df638dadc Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Sun, 27 Mar 2022 12:44:44 +0200 Subject: [PATCH] refactor error-reporting for admin/customer; remove now unused Froxlor\UI\Template class Signed-off-by: Michael Kaufmann --- admin_index.php | 74 +--------- customer_index.php | 75 +---------- error_report.php | 99 ++++++++++++++ .../preconfig/0.9/preconfig_0.9.inc.php | 5 +- lib/Froxlor/UI/Template.php | 127 ------------------ templates/Froxlor/user/error_report.html.twig | 25 ++++ 6 files changed, 131 insertions(+), 274 deletions(-) create mode 100644 error_report.php delete mode 100644 lib/Froxlor/UI/Template.php create mode 100644 templates/Froxlor/user/error_report.html.twig diff --git a/admin_index.php b/admin_index.php index 6033716b..a42eeb03 100644 --- a/admin_index.php +++ b/admin_index.php @@ -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) { diff --git a/customer_index.php b/customer_index.php index 2eba33a5..815d5494 100644 --- a/customer_index.php +++ b/customer_index.php @@ -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", "
", $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) { diff --git a/error_report.php b/error_report.php new file mode 100644 index 00000000..2f186bad --- /dev/null +++ b/error_report.php @@ -0,0 +1,99 @@ + (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); +} diff --git a/install/updates/preconfig/0.9/preconfig_0.9.inc.php b/install/updates/preconfig/0.9/preconfig_0.9.inc.php index b6451d1d..aa40f5a3 100644 --- a/install/updates/preconfig/0.9/preconfig_0.9.inc.php +++ b/install/updates/preconfig/0.9/preconfig_0.9.inc.php @@ -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 = 'Select default panel theme:'; - $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.

'; $return['classic_theme_replacement_note'] = ['type' => 'infotext', 'value' => $description]; $question = 'Select your preferred Classic Theme replacement:'; - $themes = \Froxlor\UI\Template::getThemes(); + $themes = UI::getThemes(); $sel_themes = []; foreach ($themes as $cur_theme) { $sel_themes[$cur_theme] = $cur_theme; diff --git a/lib/Froxlor/UI/Template.php b/lib/Froxlor/UI/Template.php deleted file mode 100644 index 2cbdd449..00000000 --- a/lib/Froxlor/UI/Template.php +++ /dev/null @@ -1,127 +0,0 @@ -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 - * - * @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>|(.*)<\/if>)/Uis', $templatefile)) { - $templatefile = preg_replace('/(.*)(<\/if>|(.*)<\/if>)/Uis', '".( ($1) ? ("$2") : ("$4") )."', $templatefile); - $templatefile = str_replace('\\\\', '\\', $templatefile); - } - - return $templatefile; - } - return false; - } -} diff --git a/templates/Froxlor/user/error_report.html.twig b/templates/Froxlor/user/error_report.html.twig new file mode 100644 index 00000000..533cdfcb --- /dev/null +++ b/templates/Froxlor/user/error_report.html.twig @@ -0,0 +1,25 @@ +{% extends "Froxlor/userarea.html.twig" %} + +{% block heading %} +
+
+ + {{ lng('error.send_report_title') }} +
+
+{% endblock %} + +{% block content %} +
+ + {{ mail_html|nl2br }} + +
+ + +
+ +
+
+
+{% endblock %}