Navigation Menu

Skip to content

Commit

Permalink
PHPMailer tidy boot.php, install / update (#4427)
Browse files Browse the repository at this point in the history
Co-authored-by: Gregor Harlan <330436+gharlan@users.noreply.github.com>
  • Loading branch information
skerbis and gharlan committed Feb 10, 2021
1 parent 6de81ad commit bab21d5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 84 deletions.
5 changes: 0 additions & 5 deletions .tools/phpstan/baseline.neon
Expand Up @@ -35,11 +35,6 @@ parameters:
count: 1
path: ../../redaxo/src/addons/metainfo/functions/function_metainfo.php

-
message: "#^Right side of && is always true\\.$#"
count: 1
path: ../../redaxo/src/addons/phpmailer/boot.php

-
message: "#^If condition is always true\\.$#"
count: 1
Expand Down
82 changes: 3 additions & 79 deletions redaxo/src/addons/phpmailer/boot.php
Expand Up @@ -9,88 +9,12 @@

$addon = rex_addon::get('phpmailer');

if (!$addon->hasConfig('errormail')) {
$addon->setConfig('errormail', 0);
}

if (!$addon->hasConfig('security_mode')) {
$addon->setConfig('security_mode', true); // true = AutoTLS
}

if (!rex::isBackend() && 0 != $addon->getConfig('errormail')) {
rex_extension::register('RESPONSE_SHUTDOWN', static function (rex_extension_point $ep) use ($addon) {
$logFile = rex_path::log('system.log');
$sendTime = $addon->getConfig('last_log_file_send_time', 0);
$timediff = '';
$fatalerror = false;
$logevent = false;
$timediff = time() - $sendTime;
if ($timediff > $addon->getConfig('errormail') && filesize($logFile) > 0 && $file = new rex_log_file($logFile)) {
//Start - generate mailbody
$mailBody = '<h2>Error protocol for: ' . rex::getServerName() . '</h2>';
$mailBody .= '<style> .errorbg {background: #F6C4AF; } .eventbg {background: #E1E1E1; } td, th {padding: 5px;} table {width: 100%; border: 1px solid #ccc; } th {background: #b00; color: #fff;} td { border: 0; border-bottom: 1px solid #b00;} </style> ';
$mailBody .= '<table>';
$mailBody .= ' <thead>';
$mailBody .= ' <tr>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_timestamp') . '</th>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_type') . '</th>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_message') . '</th>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_file') . '</th>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_line') . '</th>';
$mailBody .= ' </tr>';
$mailBody .= ' </thead>';
$mailBody .= ' <tbody>';
foreach (new LimitIterator($file, 0, 30) as $entry) {
/** @var rex_log_entry $entry */
$data = $entry->getData();
$style = '';
$logtypes = [
'error',
'exception',
];

foreach ($logtypes as $type) {
if (false !== stripos($data[0], $type)) {
$logevent = true;
$style = ' class="errorbg"';
break;
}
}

if ('logevent' == $data[0]) {
$style = ' class="eventbg"';
$logevent = true;
}
$mailBody .= ' <tr' . $style . '>';
$mailBody .= ' <td>' . $entry->getTimestamp('%d.%m.%Y %H:%M:%S') . '</td>';
$mailBody .= ' <td>' . $data[0] . '</td>';
$mailBody .= ' <td>' . substr(rex_escape($data[1]), 0, 128) . '</td>';
$mailBody .= ' <td>' . ($data[2] ?? '') . '</td>';
$mailBody .= ' <td>' . ($data[3] ?? '') . '</td>';
$mailBody .= ' </tr>';
}
// check if logevent occured then send mail
if (true == $logevent) {
$mailBody .= ' </tbody>';
$mailBody .= '</table>';
//End - generate mailbody
$mail = new rex_mailer();
$mail->Subject = rex::getServerName() . ' - error report ';
$mail->Body = $mailBody;
$mail->AltBody = strip_tags($mailBody);
$mail->setFrom(rex::getErrorEmail(), 'REDAXO error report');
$mail->addAddress(rex::getErrorEmail());
$addon->setConfig('last_log_file_send_time', time());
if ($mail->Send()) {
// mail has been sent
}
}
// close logger, to free remaining file-handles to syslog
rex_logger::close();
//End send mail
}
rex_extension::register('RESPONSE_SHUTDOWN', static function (rex_extension_point $ep) {
rex_mailer::errorMail();
});
}

if ('system' == rex_be_controller::getCurrentPagePart(1)) {
rex_system_setting::register(new rex_system_setting_phpmailer_errormail());
}
Expand Down
85 changes: 85 additions & 0 deletions redaxo/src/addons/phpmailer/lib/mailer.php
Expand Up @@ -222,4 +222,89 @@ public static function logFile(): string
{
return rex_path::log('mail.log');
}

/**
* @internal
*/
public static function errorMail(): void
{
$addon = rex_addon::get('phpmailer');
$logFile = rex_path::log('system.log');
$sendTime = $addon->getConfig('last_log_file_send_time', 0);
$timediff = time() - $sendTime;

if ($timediff <= $addon->getConfig('errormail') || !filesize($logFile)) {
return;
}

$file = new rex_log_file($logFile);

$logevent = false;

//Start - generate mailbody
$mailBody = '<h2>Error protocol for: ' . rex::getServerName() . '</h2>';
$mailBody .= '<style> .errorbg {background: #F6C4AF; } .eventbg {background: #E1E1E1; } td, th {padding: 5px;} table {width: 100%; border: 1px solid #ccc; } th {background: #b00; color: #fff;} td { border: 0; border-bottom: 1px solid #b00;} </style> ';
$mailBody .= '<table>';
$mailBody .= ' <thead>';
$mailBody .= ' <tr>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_timestamp') . '</th>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_type') . '</th>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_message') . '</th>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_file') . '</th>';
$mailBody .= ' <th>' . rex_i18n::msg('syslog_line') . '</th>';
$mailBody .= ' </tr>';
$mailBody .= ' </thead>';
$mailBody .= ' <tbody>';

/** @var rex_log_entry $entry */
foreach (new LimitIterator($file, 0, 30) as $entry) {
$data = $entry->getData();
$style = '';
$logtypes = [
'error',
'exception',
];

foreach ($logtypes as $type) {
if (false !== stripos($data[0], $type)) {
$logevent = true;
$style = ' class="errorbg"';
break;
}
}

if ('logevent' == $data[0]) {
$style = ' class="eventbg"';
$logevent = true;
}

$mailBody .= ' <tr' . $style . '>';
$mailBody .= ' <td>' . $entry->getTimestamp('%d.%m.%Y %H:%M:%S') . '</td>';
$mailBody .= ' <td>' . $data[0] . '</td>';
$mailBody .= ' <td>' . substr(rex_escape($data[1]), 0, 128) . '</td>';
$mailBody .= ' <td>' . ($data[2] ?? '') . '</td>';
$mailBody .= ' <td>' . ($data[3] ?? '') . '</td>';
$mailBody .= ' </tr>';
}

// check if logevent occured then send mail
if (!$logevent) {
return;
}

$mailBody .= ' </tbody>';
$mailBody .= '</table>';
//End - generate mailbody

$mail = new self();
$mail->Subject = rex::getServerName() . ' - error report ';
$mail->Body = $mailBody;
$mail->AltBody = strip_tags($mailBody);
$mail->setFrom(rex::getErrorEmail(), 'REDAXO error report');
$mail->addAddress(rex::getErrorEmail());

$addon->setConfig('last_log_file_send_time', time());

$mail->Send();
}
}

0 comments on commit bab21d5

Please sign in to comment.