[Web] Read default actions from Rspamd instead of using/printing "5,15"

[Web] Allow to reset spam score to server default (which deletes the custom spam score from the database and prints the default action values of Rspamd in use)
This commit is contained in:
André 2018-10-18 21:48:30 +02:00
parent 7a7fafec87
commit 67658c1a07
4 changed files with 65 additions and 8 deletions

View File

@ -1151,13 +1151,21 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
return false; return false;
} }
foreach ($usernames as $username) { foreach ($usernames as $username) {
if ($_data['spam_score'] == "default") {
$stmt = $pdo->prepare("DELETE FROM `filterconf` WHERE `object` = :username
AND (`option` = 'lowspamlevel' OR `option` = 'highspamlevel')");
$stmt->execute(array(
':username' => $username
));
continue;
}
$lowspamlevel = explode(',', $_data['spam_score'])[0]; $lowspamlevel = explode(',', $_data['spam_score'])[0];
$highspamlevel = explode(',', $_data['spam_score'])[1]; $highspamlevel = explode(',', $_data['spam_score'])[1];
if (!is_numeric($lowspamlevel) || !is_numeric($highspamlevel)) { if (!is_numeric($lowspamlevel) || !is_numeric($highspamlevel)) {
$_SESSION['return'][] = array( $_SESSION['return'][] = array(
'type' => 'danger', 'type' => 'danger',
'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr), 'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
'msg' => 'access_denied' 'msg' => 'Invalid spam score, format must be "1,2" where first is low and second is high spam value.'
); );
continue; continue;
} }
@ -2455,7 +2463,47 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
return $syncjobdata; return $syncjobdata;
break; break;
case 'spam_score': case 'spam_score':
$curl = curl_init();
curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, '/var/lib/rspamd/rspamd.sock');
curl_setopt($curl, CURLOPT_URL,"http://rspamd/actions");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$default_actions = curl_exec($curl);
if (!curl_errno($curl)) {
$data_array = json_decode($default_actions, true);
curl_close($curl);
foreach ($data_array as $data) {
if ($data['action'] == 'reject') {
$reject = $data['value'];
continue;
}
elseif ($data['action'] == 'add header') {
$add_header = $data['value'];
continue;
}
}
if (empty($add_header) || empty($reject)) {
// Assume default, set warning
$default = "5, 15"; $default = "5, 15";
$_SESSION['return'][] = array(
'type' => 'warning',
'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
'msg' => 'Could not determine servers default spam score, assuming default'
);
}
else {
$default = $add_header . ', ' . $reject;
}
}
else {
// Assume default, set warning
$default = "5, 15";
$_SESSION['return'][] = array(
'type' => 'warning',
'log' => array(__FUNCTION__, $_action, $_type, $_data_log, $_attr),
'msg' => 'Could not determine servers default spam score, assuming default'
);
}
curl_close($curl);
$policydata = array(); $policydata = array();
if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) { if (isset($_data) && filter_var($_data, FILTER_VALIDATE_EMAIL)) {
if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) { if (!hasMailboxObjectAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data)) {

View File

@ -598,6 +598,7 @@ $lang['admin']['reset_default'] = "Zurücksetzen auf Standard";
$lang['admin']['merged_vars_hint'] = 'Ausgegraute Reihen wurden aus der Datei <code>vars.(local.)inc.php</code> gelesen und können hier nicht verändert werden.'; $lang['admin']['merged_vars_hint'] = 'Ausgegraute Reihen wurden aus der Datei <code>vars.(local.)inc.php</code> gelesen und können hier nicht verändert werden.';
$lang['edit']['spam_score'] = "Einen benutzerdefiniterten Spam-Score festlegen"; $lang['edit']['spam_score'] = "Einen benutzerdefiniterten Spam-Score festlegen";
$lang['user']['spam_score_reset'] = "Auf Server-Standard zurücksetzen";
$lang['edit']['spam_policy'] = "Hinzufügen und Entfernen von Einträgen in White- und Blacklists"; $lang['edit']['spam_policy'] = "Hinzufügen und Entfernen von Einträgen in White- und Blacklists";
$lang['edit']['spam_alias'] = "Anpassen temporärer Alias-Adressen"; $lang['edit']['spam_alias'] = "Anpassen temporärer Alias-Adressen";

View File

@ -614,6 +614,7 @@ $lang['mailbox']['status'] = "Status";
$lang['mailbox']['running'] = "Running"; $lang['mailbox']['running'] = "Running";
$lang['edit']['spam_score'] = "Set a custom spam score"; $lang['edit']['spam_score'] = "Set a custom spam score";
$lang['user']['spam_score_reset'] = "Reset to server default";
$lang['edit']['spam_policy'] = "Add or remove items to white-/blacklist"; $lang['edit']['spam_policy'] = "Add or remove items to white-/blacklist";
$lang['edit']['spam_alias'] = "Create or change time limited alias addresses"; $lang['edit']['spam_alias'] = "Create or change time limited alias addresses";

View File

@ -305,18 +305,25 @@ elseif (isset($_SESSION['mailcow_cc_role']) && $_SESSION['mailcow_cc_role'] == '
<li><?=$lang['user']['spamfilter_yellow'];?></li> <li><?=$lang['user']['spamfilter_yellow'];?></li>
<li><?=$lang['user']['spamfilter_red'];?></li> <li><?=$lang['user']['spamfilter_red'];?></li>
</ul> </ul>
<p><i><?=$lang['user']['spamfilter_default_score'];?> 5:15</i></p>
<p><?=$lang['user']['spamfilter_hint'];?></p> <p><?=$lang['user']['spamfilter_hint'];?></p>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<div class="col-sm-10"> <div class="col-sm-10">
<button data-acl="<?=$_SESSION['acl']['spam_score'];?>" type="button" class="btn btn-sm btn-success" data-action="edit_selected"
</div>
<div class="btn-group" data-acl="<?=$_SESSION['acl']['spam_policy'];?>">
<a data-acl="<?=$_SESSION['acl']['spam_score'];?>" type="button" class="btn btn-sm btn-success" data-action="edit_selected"
data-item="<?= htmlentities($username); ?>" data-item="<?= htmlentities($username); ?>"
data-id="spam_score" data-id="spam_score"
data-api-url='edit/spam-score' data-api-url='edit/spam-score'
data-api-attr='{}'><?=$lang['user']['save_changes'];?></button> data-api-attr='{}'><?=$lang['user']['save_changes'];?></a>
<a data-acl="<?=$_SESSION['acl']['spam_score'];?>" type="button" class="btn btn-sm btn-default" data-action="edit_selected"
data-item="<?= htmlentities($username); ?>"
data-id="spam_score_reset"
data-api-url='edit/spam-score'
data-api-attr='{"spam_score":"default"}'><?=$lang['user']['spam_score_reset'];?></a>
</div> </div>
</div> </div>