From f3c5c9467c63cd751133cd2c98d6c4c35548e5da Mon Sep 17 00:00:00 2001 From: Michael Pietsch Date: Tue, 17 Apr 2018 10:50:33 +0200 Subject: [PATCH] send empty quarantine reports over user_manager; fixes 1054 --- mailscanner/languages/de.php | 1 + mailscanner/languages/en.php | 1 + mailscanner/languages/fr.php | 1 + mailscanner/languages/it.php | 1 + mailscanner/languages/ja.php | 1 + mailscanner/languages/nl.php | 1 + mailscanner/languages/pt_br.php | 1 + mailscanner/quarantine_report.inc.php | 64 ++++++++++++++++++--------- mailscanner/user_manager.php | 10 +++-- 9 files changed, 57 insertions(+), 24 deletions(-) diff --git a/mailscanner/languages/de.php b/mailscanner/languages/de.php index ca7319b2d..4f5e0c1b9 100644 --- a/mailscanner/languages/de.php +++ b/mailscanner/languages/de.php @@ -933,6 +933,7 @@ 'message61' => 'Die Variable %s ist leer, bitte setzen Sie einen Wert in conf.php.', 'text611' => 'Quarantäne-Bericht für %s', 'text612' => 'In den letzten %s Tagen wurden %s empfangene E-Mails in die Quarantäne verschoben, die unten aufgeführt sind. Alle Nachrichten in der Quarantäne werden automatisch nach %s Tagen nach deren Empfang gelöscht.', + 'text613' => 'In den letzten %s Tagen wurden keine empfangene E-Mails in die Quarantäne verschoben.', 'release61' => 'Freigabe', 'virus61' => 'Virus', 'badcontent61' => 'Schlechter Inhalt', diff --git a/mailscanner/languages/en.php b/mailscanner/languages/en.php index c094a6a08..3175dc734 100644 --- a/mailscanner/languages/en.php +++ b/mailscanner/languages/en.php @@ -934,6 +934,7 @@ 'message61' => 'The variable %s is empty, please set a value in conf.php.', 'text611' => 'Quarantine Report for %s', 'text612' => 'In the last %s day(s) you have received %s e-mails that have been quarantined and are listed below. All messages in the quarantine are automatically deleted %s days after the date that they were received.', + 'text613' => 'In the last %s day(s) you have received no e-mails that have been quarantined.', 'release61' => 'Release', 'virus61' => 'Virus', 'badcontent61' => 'Bad Content', diff --git a/mailscanner/languages/fr.php b/mailscanner/languages/fr.php index 9a3c81ea8..29a774bae 100644 --- a/mailscanner/languages/fr.php +++ b/mailscanner/languages/fr.php @@ -933,6 +933,7 @@ 'message61' => 'La variable %s est vide. Veuillez définir cette variable dans conf.php.', 'text611' => 'Rapport de Quarantaine pour %s', 'text612' => 'Durant le(s) %s dernier(s) jour(s) vous avez reçu %s messages qui ont été mis en Quarantaine et dont vous retrouverez la liste ci-dessous. Les messages en Quarantaine sont automatiquement effacés %s jours après la date à laquelle ils ont été reçus.', + 'text613' => 'In the last %s day(s) you have received no e-mails that have been quarantined.', 'release61' => 'Libérer', 'virus61' => 'Virus', 'badcontent61' => 'Contenu dangereux', diff --git a/mailscanner/languages/it.php b/mailscanner/languages/it.php index 6f515e9f0..a16accaeb 100644 --- a/mailscanner/languages/it.php +++ b/mailscanner/languages/it.php @@ -933,6 +933,7 @@ 'message61' => 'The variable %s is empty, please set a value in conf.php.', 'text611' => 'Quarantine Report for %s', 'text612' => 'In the last %s day(s) you have received %s e-mails that have been quarantined and are listed below. All messages in the quarantine are automatically deleted %s days after the date that they were received.', + 'text613' => 'In the last %s day(s) you have received no e-mails that have been quarantined.', 'release61' => 'Release', 'virus61' => 'Virus', 'badcontent61' => 'Bad Content', diff --git a/mailscanner/languages/ja.php b/mailscanner/languages/ja.php index 62e57a125..c16ddd793 100644 --- a/mailscanner/languages/ja.php +++ b/mailscanner/languages/ja.php @@ -924,6 +924,7 @@ 'message61' => '変数 %s は空です。conf.php に値を設定してください。', 'text611' => '%sの検疫レポート', 'text612' => '最後の %s 日に隔離されたメールを受信しました。そのメールは以下のとおりです。隔離されたすべてのメールは、受信日から %s 日後に自動的に削除されます。 ', + 'text613' => 'In the last %s day(s) you have received no e-mails that have been quarantined.', 'release61' => 'リリース', 'virus61' => 'ウイルス', 'badcontent61' => '悪いコンテンツ', diff --git a/mailscanner/languages/nl.php b/mailscanner/languages/nl.php index 8a1e43043..33cfbbf1e 100644 --- a/mailscanner/languages/nl.php +++ b/mailscanner/languages/nl.php @@ -933,6 +933,7 @@ 'message61' => 'The variable %s is empty, please set a value in conf.php.', 'text611' => 'Quarantine Report for %s', 'text612' => 'In the last %s day(s) you have received %s e-mails that have been quarantined and are listed below. All messages in the quarantine are automatically deleted %s days after the date that they were received.', + 'text613' => 'In the last %s day(s) you have received no e-mails that have been quarantined.', 'release61' => 'Release', 'virus61' => 'Virus', 'badcontent61' => 'Bad Content', diff --git a/mailscanner/languages/pt_br.php b/mailscanner/languages/pt_br.php index 081a6be6a..4db81bdb5 100644 --- a/mailscanner/languages/pt_br.php +++ b/mailscanner/languages/pt_br.php @@ -933,6 +933,7 @@ 'message61' => 'The variable %s is empty, please set a value in conf.php.', 'text611' => 'Quarantine Report for %s', 'text612' => 'In the last %s day(s) you have received %s e-mails that have been quarantined and are listed below. All messages in the quarantine are automatically deleted %s days after the date that they were received.', + 'text613' => 'In the last %s day(s) you have received no e-mails that have been quarantined.', 'release61' => 'Release', 'virus61' => 'Virus', 'badcontent61' => 'Bad Content', diff --git a/mailscanner/quarantine_report.inc.php b/mailscanner/quarantine_report.inc.php index 266090a24..b1237ee57 100755 --- a/mailscanner/quarantine_report.inc.php +++ b/mailscanner/quarantine_report.inc.php @@ -61,7 +61,7 @@ public static function check_quarantine_report_requirements() return $required_constant_missing; } - private static function get_html_template() + private static function get_html_template($empty=false) { return ' @@ -84,12 +84,14 @@ private static function get_html_template()

' . __('text611') . '

- ' . __('text612') . ' + ' . ($empty ? __('text613') : __('text612')) . ' +'. ($empty ? '' : ' %s +') . ' '; @@ -126,6 +128,10 @@ private static function get_html_table() %s'; + private static $text_template_empty = 'Quarantine Report for %s + +In the last %s day(s) you have received no e-mails that have been quarantined.'; + private static $text_content = 'Received: %s From: %s @@ -245,7 +251,7 @@ private static function get_report_sql() * @param array $usersForReport array containing users for which the reports should be send; if empty reports are send for all users * @return false|array if requirements not met; else an associative array counting successfull and failed reports for users */ - public function send_quarantine_reports($usersForReport = array()) + public function send_quarantine_reports($usersForReport = array(), $sendEmptyReports = false) { if (self::check_quarantine_report_requirements() !== true) { return false; @@ -278,6 +284,7 @@ public function send_quarantine_reports($usersForReport = array()) $rows = $result->num_rows; $num_successfull_reports = 0; $num_failed_reports = 0; + $num_empty_reports = 0; if ($rows > 0) { while ($user = $result->fetch_object()) { self::dbg("\n === Generating report for " . stripslashes($user->username) . ' type=' . $user->type); @@ -312,8 +319,10 @@ public function send_quarantine_reports($usersForReport = array()) } // Make sure we have a destination address if (!empty($email) && false !== $email) { - $sendResult = self::send_reports_for_user($user->username, $user->type, $email, $to_address, $to_domain); - if ($sendResult) { + $sendResult = self::send_reports_for_user($user->username, $user->type, $email, $to_address, $to_domain, $sendEmptyReports); + if ($sendResult === 0) { + $num_empty_reports++; + } elseif ($sendResult) { $num_successfull_reports++; } else { $num_failed_reports++; @@ -322,11 +331,14 @@ public function send_quarantine_reports($usersForReport = array()) self::dbg(' ==== ' . $user->username . ' has empty e-mail recipient address, skipping...'); } } + } else { + return -2; } return array( 'succ' => $num_successfull_reports, - 'failed' => $num_failed_reports + 'failed' => $num_failed_reports, + 'empty' => $num_empty_reports ); } @@ -338,7 +350,7 @@ public function send_quarantine_reports($usersForReport = array()) * @param string $to_domain * @return true if all reports were send successfully; false if one or more reports could not be send */ - private static function send_reports_for_user($username, $type, $email, $to_address, $to_domain) + private static function send_reports_for_user($username, $type, $email, $to_address, $to_domain, $sendEmptyReports = false) { self::dbg(" ==== Recipient e-mail address is $email"); // Get any additional reports required @@ -358,7 +370,7 @@ private static function send_reports_for_user($username, $type, $email, $to_addr $quarantined = self::return_quarantine_list_array($filter, $filter_domain); self::dbg(' ==== Found ' . count($quarantined) . ' quarantined e-mails'); - if (count($quarantined) > 0) { + if (count($quarantined) > 0 || $sendEmptyReports) { $sendResult = self::send_quarantine_email($email, $list_for, $quarantined); } unset($quarantined); @@ -389,10 +401,14 @@ private static function send_reports_for_user($username, $type, $email, $to_addr $quarantined[] = $tmp_quarantined; } } - $quarantined = call_user_func_array('array_merge', $quarantined); if (count($quarantined) > 0) { + $quarantined = call_user_func_array('array_merge', $quarantined); + } + if (count($quarantined) > 0 || $sendEmptyReports) { $list = implode(', ', $quarantine_list); return self::send_quarantine_email($email, $list, self::quarantine_sort($quarantined)); + } else { + return 0; } unset($quarantined, $quarantine_list); @@ -558,18 +574,26 @@ private static function send_quarantine_email($email, $filter, $quarantined) $qitem['reason'] ); } + if (count($quarantined) > 0) { + // HTML + $h2 = sprintf(self::get_html_table(), $h1); + $html_report = sprintf(self::get_html_template(), $filter, QUARANTINE_REPORT_DAYS, count($quarantined), QUARANTINE_DAYS_TO_KEEP, $h2); + if (DEBUG === true) { + echo '
' . $html_report . '
'; + } - // HTML - $h2 = sprintf(self::get_html_table(), $h1); - $html_report = sprintf(self::get_html_template(), $filter, QUARANTINE_REPORT_DAYS, count($quarantined), QUARANTINE_DAYS_TO_KEEP, $h2); - if (DEBUG === true) { - echo '
' . $html_report . '
'; - } - - // Text - $text_report = sprintf(self::$text_template, $filter, QUARANTINE_REPORT_DAYS, count($quarantined), QUARANTINE_DAYS_TO_KEEP, $t1); - if (DEBUG === true) { - echo "
$text_report
\n"; + // Text + $text_report = sprintf(self::$text_template, $filter, QUARANTINE_REPORT_DAYS, count($quarantined), QUARANTINE_DAYS_TO_KEEP, $t1); + if (DEBUG === true) { + echo "
$text_report
\n"; + } + } else { + $html_report = sprintf(self::get_html_template(true), $filter, QUARANTINE_REPORT_DAYS); + $text_report = sprintf(self::$text_template_empty, $filter, QUARANTINE_REPORT_DAYS); + if (DEBUG === true) { + echo "
$html_report
\n"; + echo "
$text_report
\n"; + } } // Send e-mail diff --git a/mailscanner/user_manager.php b/mailscanner/user_manager.php index 7be79b5a8..47ff25860 100644 --- a/mailscanner/user_manager.php +++ b/mailscanner/user_manager.php @@ -504,11 +504,13 @@ function sendReport() } $quarantine_report = new Quarantine_Report(); - $reportResult = $quarantine_report->send_quarantine_reports(array($user->username)); - if ($reportResult['succ'] >= 0) { + $reportResult = $quarantine_report->send_quarantine_reports(array($user->username), true); + if ($reportResult == -2) { + return getHtmlMessage(__('noReportsEnabled12'), 'error'); + } elseif ($reportResult['succ'] > 0) { return getHtmlMessage(__('quarantineReportSend12'), 'success'); } else { - return getHtmlMessage(__('quarantineReportFailed12'), 'success'); + return getHtmlMessage(__('quarantineReportFailed12'), 'error'); } } @@ -769,7 +771,7 @@ function logout_user(id, name) { if ($requirementsCheck !== true) { echo getHtmlMessage(__('checkReportRequirementsFailed12'), 'error'); error_log('Requirements for sending quarantine reports not met: ' . $requirementsCheck); - } elseif (!isset($_POST['quarantine_report'])) { + } elseif (!isset($_POST['quarantine_report']) || $_POST['quarantine_report'] !== 'on') { echo getHtmlMessage(__('noReportsEnabled12'), 'error'); } else { $quarantine_report = new Quarantine_Report();