From a8e550244e9afc893161815fbcb11047f7fb211c Mon Sep 17 00:00:00 2001 From: andryyy Date: Tue, 9 May 2017 21:30:08 +0200 Subject: [PATCH] Remove DKIM keys with api --- data/web/admin.php | 21 ++---- data/web/inc/functions.inc.php | 50 +++++++------- data/web/js/admin.js | 39 +++++++++-- data/web/js/mailbox.js | 6 +- data/web/json_api.php | 116 +++++++++++++++++++++++++-------- 5 files changed, 158 insertions(+), 74 deletions(-) diff --git a/data/web/admin.php b/data/web/admin.php index 39900fac..9a7f2ba2 100644 --- a/data/web/admin.php +++ b/data/web/admin.php @@ -155,6 +155,7 @@ $tfa_data = get_tfa();
+
- - - - - +
-
- - - -
+
-
- - - -
+
+ +
diff --git a/data/web/inc/functions.inc.php b/data/web/inc/functions.inc.php index 26888b79..50b26660 100644 --- a/data/web/inc/functions.inc.php +++ b/data/web/inc/functions.inc.php @@ -2497,8 +2497,13 @@ function dkim_get_blind_keys() { function dkim_delete_key($postarray) { global $redis; global $lang; - $domain = $postarray['domain']; - + if (!is_array($postarray['domains'])) { + $domains = array(); + $domains[] = $postarray['domains']; + } + else { + $domains = $postarray['domains']; + } if ($_SESSION['mailcow_cc_role'] != "admin") { $_SESSION['return'] = array( 'type' => 'danger', @@ -2506,29 +2511,28 @@ function dkim_delete_key($postarray) { ); return false; } - if (!is_valid_domain_name($domain)) { - $_SESSION['return'] = array( - 'type' => 'danger', - 'msg' => sprintf($lang['danger']['dkim_domain_or_sel_invalid']) - ); - return false; - } - try { - foreach ($redis->hGetAll('DKIM_PRIV_KEYS') as $key => $value) { - if (preg_match('/\.' . $domain . '$/i', $key)) { - $redis->hDel('DKIM_PUB_KEYS', $key); + foreach ($domains as $domain) { + if (!is_valid_domain_name($domain)) { + $_SESSION['return'] = array( + 'type' => 'danger', + 'msg' => sprintf($lang['danger']['dkim_domain_or_sel_invalid']) + ); + return false; + } + try { + foreach ($redis->hGetAll('DKIM_SELECTORS') as $domain_name => $selector) { + $redis->hDel('DKIM_PUB_KEYS', $domain_name); + $redis->hDel('DKIM_PRIV_KEYS', $selector . '.' . $domain_name); + $redis->hDel('DKIM_SELECTORS', $domain_name); } } - $redis->hDel('DKIM_PUB_KEYS', $domain); - $redis->hDel('DKIM_SELECTORS', $domain); - $redis->hDel('DKIM_PRIV_KEYS', $domain); - } - catch (RedisException $e) { - $_SESSION['return'] = array( - 'type' => 'danger', - 'msg' => 'Redis: '.$e - ); - return false; + catch (RedisException $e) { + $_SESSION['return'] = array( + 'type' => 'danger', + 'msg' => 'Redis: '.$e + ); + return false; + } } $_SESSION['return'] = array( 'type' => 'success', diff --git a/data/web/js/admin.js b/data/web/js/admin.js index be0fbd11..76367b15 100644 --- a/data/web/js/admin.js +++ b/data/web/js/admin.js @@ -1,6 +1,6 @@ $(document).ready(function() { - // Collect values of input fields with name multi_select to js array multi_data[data-id-of-checkbox] + // Collect values of input fields with name multi_select with same data-form-id to js array multi_data[data-form-id-of-checkbox] var multi_data = []; $(document).on('change', 'input[name=multi_select]:checkbox', function() { if ($(this).is(':checked') && $(this).attr('data-form-id')) { @@ -15,7 +15,7 @@ $(document).ready(function() { multi_data[id].splice($.inArray($(this).val(), multi_data[id]),1); } }); - // Select by click on tr + // Select checkbox by click on parent tr $(document).on('click', 'tr', function(e) { if (e.target.type == "checkbox") { e.stopPropagation(); @@ -27,8 +27,8 @@ $(document).ready(function() { // 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(); + var all_checkboxes = $("input[data-form-id=" + $(this).attr("data-form-id") + "]"); + all_checkboxes.prop("checked", !closests_checkboxes.prop("checked")).change(); }); // Draw domain admin table @@ -140,7 +140,7 @@ $(document).ready(function() { url: '/api/v1/delete/fwdhost', jsonp: false, complete: function (data) { - location.reload(); + window.location.href = window.location.href; } }); }) @@ -150,6 +150,35 @@ $(document).ready(function() { } }); + $(document).on('click', '#delete_dkim_key', function(e) { + e.preventDefault(); + var dkim_domain = $(this).data('dkim-domain'); + var dkim_selector = $(this).data('dkim-selector'); + $(document).on('show.bs.modal','#ConfirmDeleteModal', function () { + $("#ItemsToDelete").empty(); + $("#ItemsToDelete").append("
  • " + dkim_domain + ", " + dkim_selector + "
  • "); + }) + $('#ConfirmDeleteModal').modal({ + backdrop: 'static', + keyboard: false + }) + .one('click', '#IsConfirmed', function(e) { + $.ajax({ + type: "POST", + dataType: "json", + data: { "domains": JSON.stringify(dkim_domain) }, + url: '/api/v1/delete/dkim', + jsonp: false, + complete: function (data) { + window.location.href = window.location.href; + } + }); + }) + .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); diff --git a/data/web/js/mailbox.js b/data/web/js/mailbox.js index 1e168f59..39f2d976 100644 --- a/data/web/js/mailbox.js +++ b/data/web/js/mailbox.js @@ -309,7 +309,7 @@ $(document).ready(function() { url: '/api/v1/edit/alias', jsonp: false, complete: function (data) { - location.reload(); + window.location.href = window.location.href; } }); } @@ -325,7 +325,7 @@ $(document).ready(function() { url: '/api/v1/edit/alias', jsonp: false, complete: function (data) { - location.reload(); + window.location.href = window.location.href; } }); } @@ -352,7 +352,7 @@ $(document).ready(function() { url: '/api/v1/delete/alias', jsonp: false, complete: function (data) { - location.reload(); + window.location.href = window.location.href; } }); }) diff --git a/data/web/json_api.php b/data/web/json_api.php index c48a1293..3d15d3f3 100644 --- a/data/web/json_api.php +++ b/data/web/json_api.php @@ -356,6 +356,18 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u return; } break; + case "csrf-cookie": + if (isset($_SESSION['mailcow_cc_username']) && isset($_SESSION['mailcow_cc_role'])) { + csrfprotector::refreshToken(); + echo json_encode(array( + 'type' => 'success', + 'msg' => 'Cookie refreshed' + )); + } + else { + return; + } + break; default: echo '{}'; break; @@ -368,46 +380,94 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u $address = json_decode($_POST['address'], true); if (is_array($address)) { if (mailbox_delete_alias(array('address' => $address)) === false) { - echo json_encode(array( - 'type' => 'error', - 'message' => 'Deletion of item/s failed' - )); + if (isset($_SESSION['return'])) { + echo json_encode($_SESSION['return']); + } + else { + echo json_encode(array( + 'type' => 'error', + 'msg' => 'Deletion of items/s failed' + )); + } exit(); } - echo json_encode(array( - 'type' => 'success', - 'message' => 'Task completed' - )); + if (isset($_SESSION['return'])) { + echo json_encode($_SESSION['return']); + } + else { + echo json_encode(array( + 'type' => 'success', + 'msg' => 'Item/s deleted: ' . $domains + )); + } } } - else { - echo json_encode(array( - 'type' => 'error', - 'message' => 'Cannot find address array in post data' - )); - } break; case "fwdhost": if (isset($_POST['forwardinghost'])) { - $forwardinghost = json_decode($_POST['forwardinghost'], true); + $forwardinghost = (array)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' - )); + if (isset($_SESSION['return'])) { + echo json_encode($_SESSION['return']); + } + else { + echo json_encode(array( + 'type' => 'error', + 'msg' => 'Deletion of items/s failed' + )); + } exit(); } - echo json_encode(array( - 'type' => 'success', - 'message' => 'Task completed' - )); + if (isset($_SESSION['return'])) { + echo json_encode($_SESSION['return']); + } + else { + echo json_encode(array( + 'type' => 'success', + 'msg' => 'Item/s deleted: ' . $domains + )); + } } } else { echo json_encode(array( 'type' => 'error', - 'message' => 'Cannot find forwardinghost array in post data' + 'msg' => 'Cannot find forwardinghost array in post data' + )); + } + break; + case "dkim": + if (isset($_POST['domains'])) { + $domains = (array)json_decode($_POST['domains'], true); + if (is_array($domains)) { + if (dkim_delete_key(array('domains' => $domains)) === false) { + if (isset($_SESSION['return'])) { + echo json_encode($_SESSION['return']); + } + else { + echo json_encode(array( + 'type' => 'error', + 'msg' => 'Deletion of items/s failed' + )); + } + exit(); + } + if (isset($_SESSION['return'])) { + echo json_encode($_SESSION['return']); + } + else { + echo json_encode(array( + 'type' => 'success', + 'msg' => 'Item/s deleted: ' . $domains + )); + } + } + } + else { + echo json_encode(array( + 'type' => 'error', + 'msg' => 'Cannot find domains array in post data' )); } break; @@ -417,25 +477,25 @@ if (isset($_SESSION['mailcow_cc_role']) || isset($_SESSION['pending_mailcow_cc_u switch ($category) { case "alias": if (isset($_POST['address']) && isset($_POST['active'])) { - $address = json_decode($_POST['address'], true); + $address = (array)json_decode($_POST['address'], true); if (is_array($address)) { if (mailbox_edit_alias(array('address' => $address, 'active' => ($_POST['active'] == "1") ? $active = 1 : null)) === false) { echo json_encode(array( 'type' => 'error', - 'message' => 'Edit item failed' + 'msg' => 'Edit item failed' )); exit(); } echo json_encode(array( 'type' => 'success', - 'message' => 'Task completed' + 'msg' => 'Task completed' )); } } else { echo json_encode(array( 'type' => 'error', - 'message' => 'Cannot find address array in post data' + 'msg' => 'Cannot find address array in post data' )); } break;