Use API for forwarding hosts

master
andryyy 2017-05-09 13:43:54 +02:00
parent 74359f6df4
commit bbff045d04
9 changed files with 290 additions and 87 deletions

View File

@ -279,45 +279,14 @@ $tfa_data = get_tfa();
<div class="panel-heading"><?=$lang['admin']['forwarding_hosts'];?></div>
<div class="panel-body">
<p style="margin-bottom:40px"><?=$lang['admin']['forwarding_hosts_hint'];?></p>
<form method="post">
<div class="table-responsive">
<table class="table table-striped" id="forwardinghoststable">
<thead>
<tr>
<th style="min-width: 100px;"><?=$lang['edit']['host'];?></th>
<th style="min-width: 100px;"><?=$lang['edit']['source'];?></th>
<th><?=$lang['user']['spamfilter'];?></th>
<th style="text-align: right; min-width: 200px;"><?=$lang['admin']['action'];?></th>
</tr>
</thead>
<tbody>
<?php
$fwd_hosts = get_forwarding_hosts();
if (!empty($fwd_hosts)) {
foreach ($fwd_hosts as $host => $attr) {
?>
<tr id="data">
<td><?=htmlspecialchars($host);?></td>
<td><?=htmlspecialchars($attr['source']);?></td>
<td><?=($attr['keep_spam'] == "no") ? $lang['admin']['yes'] : $lang['admin']['no'];?></td>
<td style="text-align: right;">
<div class="btn-group">
<a href="delete.php?forwardinghost=<?=htmlspecialchars($host);?>" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> <?=$lang['admin']['remove'];?></a>
</div>
</td>
</td>
</tr>
<?php
}
}
else {
?>
<tr id="no-data"><td colspan="4" style="text-align: center; font-style: italic;"><?=$lang['admin']['no_record'];?></td></tr>
<?php
}
?>
</tbody>
</table>
<form method="post" data-id="fwdhosts">
<div class="btn-group btn-group-sm">
<button type="button" id="toggle_multi_select_all" data-form-id="fwdhosts" class="btn btn-default">Toggle all</button>
<button type="button" id="delete_fwdhosts" name="delete_fwdhosts" class="btn btn-danger"><?=$lang['admin']['remove'];?></button>
</div>
<hr >
<div class="table-responsive">
<table class="table table-striped" id="forwardinghoststable"></table>
</div>
</form>
<legend><?=$lang['admin']['add_forwarding_host'];?></legend>

View File

@ -55,4 +55,12 @@ body.modal-open {
overflow: inherit;
padding-right: inherit !important;
}
.mailcow-alert-box {
position: fixed;
bottom: 8px;
right: 25px;
min-width: 300px;
max-width: 350px;
z-index: 2000;
display: none;
}

View File

@ -19,6 +19,9 @@ if (isset($_SESSION['mailcow_cc_role']) && $_SESSION['mailcow_cc_role'] == "admi
</div>
</div>
</div>
<?php
endif;
?>
<div id="ConfirmDeleteModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
@ -36,9 +39,6 @@ if (isset($_SESSION['mailcow_cc_role']) && $_SESSION['mailcow_cc_role'] == "admi
</div>
</div>
</div>
<?php
endif;
?>
<div style="margin-bottom:100px"></div>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="/js/bootstrap-switch.min.js"></script>
@ -53,6 +53,16 @@ function setLang(sel) {
}
$(document).ready(function() {
function mailcow_alert_box(type, message) {
$('.mailcow-alert-box').show();
$('.mailcow-alert-box').addClass("alert-" + type);
$('#mailcow-alert-text').text(message);
}
// PHP error handler
<?php if (isset($_SESSION['return'])): ?>
mailcow_alert_box("<?=$_SESSION['return']['type'];?>", "<?=$_SESSION['return']['msg'];?>");
<?php endif; unset($_SESSION['return']); ?>
// Confirm TFA modal
<?php if (isset($_SESSION['pending_tfa_method'])):?>
$('#ConfirmTFAModal').modal({
@ -220,21 +230,10 @@ $(document).ready(function() {
});
});
</script>
<?php
if (isset($_SESSION['return'])):
?>
<div class="container">
<div style="position:fixed;bottom:8px;right:25px;min-width:300px;max-width:350px;z-index:2000">
<div <?=($_SESSION['return']['type'] == 'danger') ? null : 'id="alert-fade"'?> class="alert alert-<?=$_SESSION['return']['type'];?>" role="alert">
<a href="#" class="close" data-dismiss="alert"> &times;</a>
<?=htmlspecialchars($_SESSION['return']['msg']);?>
</div>
</div>
<div class="mailcow-alert-box alert" role="alert">
<a href="#" class="close" data-dismiss="alert"> &times;</a>
<span id="mailcow-alert-text"></span>
</div>
<?php
unset($_SESSION['return']);
endif;
?>
</body>
</html>
<?php $stmt = null; $pdo = null; ?>

View File

@ -5103,11 +5103,10 @@ function get_forwarding_hosts() {
global $redis;
$data = array();
try {
$wl_hosts = $redis->hGetAll('WHITELISTED_FWD_HOST');
if (!empty($wl_hosts)) {
foreach ($wl_hosts as $host => $source) {
$data[$host]['keep_spam'] = ($redis->hGet('KEEP_SPAM', $host)) ? "yes" : "no";
$data[$host]['source'] = $source;
$fwd_hosts = $redis->hGetAll('WHITELISTED_FWD_HOST');
if (!empty($fwd_hosts)) {
foreach ($fwd_hosts as $fwd_host => $source) {
$data[] = $fwd_host;
}
}
}
@ -5120,6 +5119,31 @@ function get_forwarding_hosts() {
}
return $data;
}
function get_forwarding_host_details($host) {
global $redis;
$data = array();
if (!isset($host) || empty($host)) {
return false;
}
if (filter_var($host, FILTER_VALIDATE_IP)) {
return;
}
try {
if ($source = $redis->hGet('WHITELISTED_FWD_HOST', $host)) {
$data['host'] = $host;
$data['source'] = $source;
$data['keep_spam'] = ($redis->hGet('KEEP_SPAM', $host)) ? "yes" : "no";
}
}
catch (RedisException $e) {
$_SESSION['return'] = array(
'type' => 'danger',
'msg' => 'Redis: '.$e
);
return false;
}
return $data;
}
function add_forwarding_host($postarray) {
require_once 'spf.inc.php';
global $redis;
@ -5132,7 +5156,7 @@ function add_forwarding_host($postarray) {
return false;
}
$source = $postarray['hostname'];
$host = trim($postarray['hostname']);
$host = trim($postarray['hostname']);
$filter_spam = $postarray['filter_spam'];
if (isset($postarray['filter_spam']) && $postarray['filter_spam'] == 1) {
$filter_spam = 1;
@ -5140,13 +5164,16 @@ function add_forwarding_host($postarray) {
else {
$filter_spam = 0;
}
if (filter_var($host, FILTER_VALIDATE_IP)) {
if (preg_match('/^[0-9a-fA-F:\/]+$/', $host)) { // IPv6 address
$hosts = array($host);
}
}
elseif (preg_match('/^[0-9\.\/]+$/', $host)) { // IPv4 address
$hosts = array($host);
}
else {
$hosts = get_outgoing_hosts_best_guess($host);
}
if (!$hosts) {
if (empty($hosts)) {
$_SESSION['return'] = array(
'type' => 'danger',
'msg' => 'Invalid host specified: '. htmlspecialchars($host)
@ -5195,8 +5222,8 @@ function delete_forwarding_host($postarray) {
}
foreach ($hosts as $host) {
try {
return $redis->hDel('WHITELISTED_FWD_HOST', $host);
return $redis->hDel('KEEP_SPAM', $host);
$redis->hDel('WHITELISTED_FWD_HOST', $host);
$redis->hDel('KEEP_SPAM', $host);
}
catch (RedisException $e) {
$_SESSION['return'] = array(
@ -5208,7 +5235,7 @@ function delete_forwarding_host($postarray) {
}
$_SESSION['return'] = array(
'type' => 'success',
'msg' => sprintf($lang['success']['forwarding_host_removed'], htmlspecialchars($host))
'msg' => sprintf($lang['success']['forwarding_host_removed'], htmlspecialchars(implode(', ', $hosts)))
);
}
function get_logs($container, $lines = 100) {

View File

@ -1,4 +1,37 @@
$(document).ready(function() {
// Collect values of input fields with name multi_select to js array multi_data[data-id-of-checkbox]
var multi_data = [];
$(document).on('change', 'input[name=multi_select]:checkbox', function() {
if ($(this).is(':checked') && $(this).attr('data-form-id')) {
var id = $(this).data('form-id');
if (typeof multi_data[id] == "undefined") {
multi_data[id] = [];
}
multi_data[id].push($(this).val());
}
else {
var id = $(this).data('form-id');
multi_data[id].splice($.inArray($(this).val(), multi_data[id]),1);
}
});
// Select by click on tr
$(document).on('click', 'tr', function(e) {
if (e.target.type == "checkbox") {
e.stopPropagation();
} else {
var checkbox = $(this).find(':checkbox');
checkbox.trigger('click');
}
});
// Select or deselect all checkboxes with same data-form-id
$(document).on('click', '#toggle_multi_select_all', function(e) {
e.preventDefault();
var closests_checkboxes = $("input[data-form-id=" + $(this).attr("data-form-id") + "]");
closests_checkboxes.prop("checked", !closests_checkboxes.prop("checked")).change();
});
// Draw domain admin table
$.ajax({
dataType: 'json',
url: '/api/v1/get/domain-admin/all',
@ -39,6 +72,84 @@ $(document).ready(function() {
});
}
});
// Draw fwd hosts table
$.ajax({
dataType: 'json',
url: '/api/v1/get/fwdhost/all',
jsonp: false,
error: function () {
console.log('Cannot draw forwarding hosts table');
},
success: function (data) {
$.each(data, function (i, item) {
item.action = '<div class="btn-group">' +
'<a href="/delete.php?forwardinghost=' + encodeURI(item.host) + '" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span> ' + lang.remove + '</a>' +
'</div>';
if (item.keep_spam == "yes") {
item.keep_spam = lang.no;
}
else {
item.keep_spam = lang.yes;
}
item.chkbox = '<input type="checkbox" data-form-id="fwdhosts" name="multi_select" value="' + item.host + '" />';
});
$('#forwardinghoststable').footable({
"columns": [
{"name":"chkbox","title":"","style":{"maxWidth":"40px","width":"40px"},"filterable": false,"sortable": false,"type":"html"},
{"name":"host","type":"text","title":lang.host,"style":{"width":"250px"}},
{"name":"source","title":lang.source,"breakpoints":"xs sm"},
{"name":"keep_spam","title":lang.spamfilter, "type": "text","style":{"maxWidth":"80px","width":"80px"}},
{"name":"action","filterable": false,"sortable": false,"style":{"text-align":"right","maxWidth":"180px","width":"180px"},"type":"html","title":lang.action,"breakpoints":"xs sm"}
],
"rows": data,
"empty": lang.empty,
"paging": {
"enabled": true,
"limit": 5,
"size": pagination_size
},
"sorting": {
"enabled": true
}
});
}
});
$(document).on('click', '#delete_fwdhosts', function(e) {
e.preventDefault();
var id = $(this).closest("form").attr('data-id');
if (typeof multi_data[id] == "undefined") return;
data_array = multi_data[id];
if (Object.keys(data_array).length !== 0) {
$(document).on('show.bs.modal','#ConfirmDeleteModal', function () {
$("#ItemsToDelete").empty();
for (var i in data_array) {
$("#ItemsToDelete").append("<li>" + data_array[i] + "</li>");
}
})
$('#ConfirmDeleteModal').modal({
backdrop: 'static',
keyboard: false
})
.one('click', '#IsConfirmed', function(e) {
$.ajax({
type: "POST",
dataType: "json",
data: { "forwardinghost": JSON.stringify(data_array) },
url: '/api/v1/delete/fwdhost',
jsonp: false,
complete: function (data) {
location.reload();
}
});
})
.one('click', '#isCanceled', function(e) {
$('#ConfirmDeleteModal').modal('hide');
});;
}
});
$("#refresh_dovecot_log").on('click', function(e) {
function unix_time_format(tm) {
var date = new Date(tm ? tm * 1000 : 0);

View File

@ -301,7 +301,7 @@ $(document).ready(function() {
$(document).on('click', '#activate_selected_alias', function(e) {
e.preventDefault();
if (selected_aliases.length !== 0) {
if (Object.keys(selected_aliases).length !== 0) {
$.ajax({
type: "POST",
dataType: "json",

View File

@ -33,7 +33,12 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
$domains = mailbox_get_domains();
if (!empty($domains)) {
foreach ($domains as $domain) {
$data[] = mailbox_get_domain_details($domain);
if ($details = mailbox_get_domain_details($domain)) {
$data[] = $details;
}
else {
continue;
}
}
if (!isset($data) || empty($data)) {
echo '{}';
@ -53,7 +58,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
echo '{}';
}
else {
echo json_encode(mailbox_get_domain_details($object), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
break;
}
@ -102,7 +107,12 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
$mailboxes = mailbox_get_mailboxes($domain);
if (!empty($mailboxes)) {
foreach ($mailboxes as $mailbox) {
$data[] = mailbox_get_mailbox_details($mailbox);
if ($details = mailbox_get_mailbox_details($mailbox)) {
$data[] = $details;
}
else {
continue;
}
}
}
}
@ -124,7 +134,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
echo '{}';
}
else {
echo json_encode(mailbox_get_mailbox_details($object), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
break;
@ -139,7 +149,12 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
$resources = mailbox_get_resources($domain);
if (!empty($resources)) {
foreach ($resources as $resource) {
$data[] = mailbox_get_resource_details($resource);
if ($details = mailbox_get_resource_details($resource)) {
$data[] = $details;
}
else {
continue;
}
}
}
}
@ -161,7 +176,40 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
echo '{}';
}
else {
echo json_encode(mailbox_get_resource_details($object), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
break;
}
break;
case "fwdhost":
switch ($object) {
case "all":
$fwdhosts = get_forwarding_hosts();
if (!empty($fwdhosts)) {
foreach ($fwdhosts as $fwdhost) {
if ($details = get_forwarding_host_details($fwdhost)) {
$data[] = $details;
}
else {
continue;
}
}
}
if (!isset($data) || empty($data)) {
echo '{}';
}
else {
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
break;
default:
$data = get_forwarding_host_details($object);
if (!isset($data) || empty($data)) {
echo '{}';
}
else {
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
break;
@ -176,7 +224,12 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
$alias_domains = mailbox_get_alias_domains($domain);
if (!empty($alias_domains)) {
foreach ($alias_domains as $alias_domain) {
$data[] = mailbox_get_alias_domain_details($alias_domain);
if ($details = mailbox_get_alias_domain_details($alias_domain)) {
$data[] = $details;
}
else {
continue;
}
}
}
}
@ -198,7 +251,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
echo '{}';
}
else {
echo json_encode(mailbox_get_alias_domains($object), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
break;
}
@ -212,7 +265,12 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
$aliases = mailbox_get_aliases($domain);
if (!empty($aliases)) {
foreach ($aliases as $alias) {
$data[] = mailbox_get_alias_details($alias);
if ($details = mailbox_get_alias_details($alias)) {
$data[] = $details;
}
else {
continue;
}
}
}
}
@ -234,7 +292,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
echo '{}';
}
else {
echo json_encode(mailbox_get_alias_details($object), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
break;
}
@ -245,7 +303,12 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
$domain_admins = get_domain_admins();
if (!empty($domain_admins)) {
foreach ($domain_admins as $domain_admin) {
$data[] = get_domain_admin_details($domain_admin);
if ($details = get_domain_admin_details($domain_admin)) {
$data[] = $details;
}
else {
continue;
}
}
if (!isset($data) || empty($data)) {
echo '{}';
@ -265,7 +328,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
echo '{}';
}
else {
echo json_encode(get_domain_admin_details($object), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
break;
}
@ -307,7 +370,7 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
if (mailbox_delete_alias(array('address' => $address)) === false) {
echo json_encode(array(
'type' => 'error',
'message' => 'Deletion of item failed'
'message' => 'Deletion of item/s failed'
));
exit();
}
@ -324,6 +387,30 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u
));
}
break;
case "fwdhost":
if (isset($_POST['forwardinghost'])) {
$forwardinghost = json_decode($_POST['forwardinghost'], true);
if (is_array($forwardinghost)) {
if (delete_forwarding_host(array('forwardinghost' => $forwardinghost)) === false) {
echo json_encode(array(
'type' => 'error',
'message' => 'Deletion of item/s failed'
));
exit();
}
echo json_encode(array(
'type' => 'success',
'message' => 'Task completed'
));
}
}
else {
echo json_encode(array(
'type' => 'error',
'message' => 'Cannot find forwardinghost array in post data'
));
}
break;
}
break;
case "edit":

View File

@ -135,6 +135,7 @@ $lang['user']['day'] = 'Tag';
$lang['user']['week'] = 'Woche';
$lang['user']['weeks'] = 'Wochen';
$lang['user']['spamfilter'] = 'Spamfilter';
$lang['admin']['spamfilter'] = 'Spamfilter';
$lang['user']['spamfilter_wl'] = 'Whitelist';
$lang['user']['spamfilter_wl_desc'] = 'Für E-Mail-Adressen, die vom Spamfilter <b>nicht</b> erfasst werden sollen. Die Verwendung von Wildcards ist gestattet.';
$lang['user']['spamfilter_bl'] = 'Blacklist';
@ -480,8 +481,8 @@ $lang['admin']['message'] = 'Nachricht';
$lang['admin']['forwarding_hosts'] = 'Weiterleitungs-Hosts';
$lang['admin']['forwarding_hosts_hint'] = 'Eingehende Nachrichten werden von den hier gelisteten Hosts bedingungslos akzeptiert. Diese Hosts werden dann nicht mit DNSBLs abgeglichen oder Greylisting unterworfen. Von ihnen empfangener Spam wird nie abgelehnt, optional kann er aber in den Spam-Ordner einsortiert werden. Die übliche Verwendung für diese Funktion ist, um Mailserver anzugeben, auf denen eine Weiterleitung zu Ihrem Mailcow-Server eingerichtet wurde.';
$lang['admin']['forwarding_hosts_add_hint'] = 'Sie können entweder IPv4/IPv6-Adressen, Netzwerke in CIDR-Notation, Hostnamen (die zu IP-Adressen aufgelöst werden), oder Domainnamen (die zu IP-Adressen aufgelöst werden, indem ihr SPF-Record abgefragt wird oder, in dessen Abwesenheit, ihre MX-Records) angeben.';
$lang['edit']['host'] = 'Host';
$lang['edit']['source'] = 'Quelle';
$lang['admin']['host'] = 'Host';
$lang['admin']['source'] = 'Quelle';
$lang['admin']['add_forwarding_host'] = 'Weiterleitungs-Host hinzufügen';
$lang['delete']['remove_forwardinghost_warning'] = '<b>Warnung:</b> Sie entfernen den Weiterleitungs-Host <b>%s</b>!';
$lang['success']['forwarding_host_removed'] = "Weiterleitungs-Host %s wurde entfernt";

View File

@ -137,6 +137,7 @@ $lang['user']['day'] = 'Day';
$lang['user']['week'] = 'Week';
$lang['user']['weeks'] = 'Weeks';
$lang['user']['spamfilter'] = 'Spam filter';
$lang['admin']['spamfilter'] = 'Spam filter';
$lang['user']['spamfilter_wl'] = 'Whitelist';
$lang['user']['spamfilter_wl_desc'] = 'Whitelisted email addresses to <b>never</b> classify as spam. Wildcards maybe used.';
$lang['user']['spamfilter_bl'] = 'Blacklist';
@ -492,8 +493,8 @@ $lang['admin']['logs'] = 'Logs';
$lang['admin']['forwarding_hosts'] = 'Forwarding Hosts';
$lang['admin']['forwarding_hosts_hint'] = 'Incoming messages are unconditionally accepted from any hosts listed here. These hosts are then not checked against DNSBLs or subjected to greylisting. Spam received from them is never rejected, but optionally it can be filed into the Junk folder. The most common use for this is to specify mail servers on which you have set up a rule that forwards incoming emails to your Mailcow server.';
$lang['admin']['forwarding_hosts_add_hint'] = 'You can either specify IPv4/IPv6 addresses, networks in CIDR notation, host names (which will be resolved to IP addresses), or domain names (which will be resolved to IP addresses by querying SPF records or, in their absence, MX records).';
$lang['edit']['host'] = 'Host';
$lang['edit']['source'] = 'Source';
$lang['admin']['host'] = 'Host';
$lang['admin']['source'] = 'Source';
$lang['admin']['add_forwarding_host'] = 'Add Forwarding Host';
$lang['delete']['remove_forwardinghost_warning'] = '<b>Warning:</b> You are about to remove the forwarding host <b>%s</b>!';
$lang['success']['forwarding_host_removed'] = "Forwarding host %s has been removed";