Use API for forwarding hosts

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

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) {