feat: domain edit/delete, fix logs, add admin quarantine with block

- Add ajax_woocow_admin_domain_edit and _delete PHP handlers
- Domain table: richer columns (mailboxes used/limit, quota), icon buttons
- Edit domain modal: pre-populates fields, loads relayhosts for transport select
- Fix logs: correct Mailcow API slugs (rspamd-history, ratelimited) and add /{count} suffix to endpoint
- Add admin Quarantine submenu: view all quarantined messages, delete, blacklist sender via domain policy
- Add domain policy methods to API class (add/delete/get_bl)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-27 08:53:11 +01:00
parent 1c5b58f238
commit dbe4abccf7
3 changed files with 500 additions and 13 deletions

View File

@@ -26,10 +26,15 @@ class WooCow_Admin {
'woocow_admin_mailbox_edit',
'woocow_admin_domain_add',
'woocow_admin_domain_dns',
'woocow_admin_domain_edit',
'woocow_admin_domain_delete',
'woocow_admin_relayhosts_list',
'woocow_admin_relayhost_save',
'woocow_admin_relayhost_delete',
'woocow_admin_logs',
'woocow_admin_quarantine',
'woocow_admin_quarantine_delete',
'woocow_admin_quarantine_block',
];
foreach ( $ajax_actions as $action ) {
@@ -55,7 +60,8 @@ class WooCow_Admin {
add_submenu_page( 'woocow', 'Mailboxes', 'Mailboxes', 'manage_woocommerce', 'woocow-mailboxes', [ $this, 'page_mailboxes' ] );
add_submenu_page( 'woocow', 'Domains', 'Domains', 'manage_woocommerce', 'woocow-domains', [ $this, 'page_domains' ] );
add_submenu_page( 'woocow', 'Transports','Transports', 'manage_woocommerce', 'woocow-transports', [ $this, 'page_transports' ] );
add_submenu_page( 'woocow', 'Logs', 'Logs', 'manage_woocommerce', 'woocow-logs', [ $this, 'page_logs' ] );
add_submenu_page( 'woocow', 'Logs', 'Logs', 'manage_woocommerce', 'woocow-logs', [ $this, 'page_logs' ] );
add_submenu_page( 'woocow', 'Quarantine', 'Quarantine', 'manage_woocommerce', 'woocow-quarantine', [ $this, 'page_quarantine' ] );
}
public function enqueue_assets( string $hook ): void {
@@ -385,6 +391,71 @@ class WooCow_Admin {
<!-- Domain list -->
<div id="wc-dom-table-wrap"></div>
<!-- Edit Domain Modal -->
<div id="wc-dom-edit-modal" class="woocow-modal" style="display:none">
<div class="woocow-modal-box" style="max-width:600px">
<h3>Edit Domain: <span id="wc-dom-edit-name"></span></h3>
<input type="hidden" id="wc-dom-edit-domain">
<table class="form-table">
<tr>
<th><label for="wc-dom-edit-desc">Description</label></th>
<td><input type="text" id="wc-dom-edit-desc" class="regular-text"></td>
</tr>
<tr>
<th><label for="wc-dom-edit-mboxes">Max Mailboxes</label></th>
<td><input type="number" id="wc-dom-edit-mboxes" class="small-text" min="1"></td>
</tr>
<tr>
<th><label for="wc-dom-edit-aliases">Max Aliases</label></th>
<td><input type="number" id="wc-dom-edit-aliases" class="small-text" min="0"></td>
</tr>
<tr>
<th><label for="wc-dom-edit-quota">Total Quota (MB)</label></th>
<td><input type="number" id="wc-dom-edit-quota" class="small-text" min="1">
<p class="description">Sum of all mailbox quotas for this domain.</p></td>
</tr>
<tr>
<th><label for="wc-dom-edit-defquota">Default Mailbox Quota (MB)</label></th>
<td><input type="number" id="wc-dom-edit-defquota" class="small-text" min="1"></td>
</tr>
<tr>
<th><label>Rate Limit</label></th>
<td>
<div class="woocow-flex-inline">
<input type="number" id="wc-dom-edit-rl-value" class="small-text" min="0" placeholder="0 = off">
<span>msgs per</span>
<select id="wc-dom-edit-rl-frame">
<option value="s">Second</option>
<option value="m">Minute</option>
<option value="h">Hour</option>
<option value="d">Day</option>
</select>
<span class="description">(0 = disabled)</span>
</div>
</td>
</tr>
<tr>
<th><label for="wc-dom-edit-relayhost">Outbound Transport</label></th>
<td>
<select id="wc-dom-edit-relayhost" class="regular-text">
<option value="0">— Direct delivery (no relay) —</option>
</select>
<p class="description">Route outbound mail for this domain through a sender-dependent transport.</p>
</td>
</tr>
<tr>
<th><label for="wc-dom-edit-active">Active</label></th>
<td><input type="checkbox" id="wc-dom-edit-active"></td>
</tr>
</table>
<div class="woocow-modal-actions">
<button class="button button-primary" id="wc-dom-edit-save">Save Changes</button>
<button class="button" id="wc-dom-edit-cancel">Cancel</button>
<span id="wc-dom-edit-notice"></span>
</div>
</div>
</div>
<!-- DNS Records panel -->
<div id="wc-dom-dns-panel" class="woocow-card" style="display:none">
<div style="display:flex;align-items:center;gap:12px;margin-bottom:12px">
@@ -458,7 +529,18 @@ class WooCow_Admin {
public function page_logs(): void {
global $wpdb;
$servers = $wpdb->get_results( "SELECT id, name FROM {$wpdb->prefix}woocow_servers WHERE active=1 ORDER BY name" );
$log_types = [ 'postfix', 'dovecot', 'rspamd', 'ratelimit', 'api', 'acme', 'autodiscover', 'sogo', 'netfilter', 'watchdog' ];
$log_types = [
'postfix' => 'Postfix',
'dovecot' => 'Dovecot',
'rspamd-history' => 'Rspamd',
'ratelimited' => 'Rate Limit',
'api' => 'API',
'acme' => 'ACME',
'autodiscover' => 'Autodiscover',
'sogo' => 'SOGo',
'netfilter' => 'Netfilter',
'watchdog' => 'Watchdog',
];
?>
<div class="wrap woocow-wrap">
<h1>WooCow Server Logs</h1>
@@ -471,8 +553,8 @@ class WooCow_Admin {
<?php endforeach; ?>
</select>
<select id="wc-log-type">
<?php foreach ( $log_types as $t ) : ?>
<option value="<?php echo esc_attr( $t ); ?>"><?php echo esc_html( ucfirst( $t ) ); ?></option>
<?php foreach ( $log_types as $slug => $label ) : ?>
<option value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<button class="button" id="wc-log-load" disabled>Load Logs</button>
@@ -485,6 +567,34 @@ class WooCow_Admin {
<?php
}
// ── Page: Quarantine ─────────────────────────────────────────────────────
public function page_quarantine(): void {
global $wpdb;
$servers = $wpdb->get_results( "SELECT id, name FROM {$wpdb->prefix}woocow_servers WHERE active=1 ORDER BY name" );
?>
<div class="wrap woocow-wrap">
<h1>WooCow Quarantine</h1>
<p>View and manage quarantined messages across all servers. You can permanently delete messages or blacklist senders by domain.</p>
<div class="woocow-toolbar woocow-flex">
<select id="wc-quar-server" class="regular-text">
<option value="">— Select server —</option>
<?php foreach ( $servers as $s ) : ?>
<option value="<?php echo esc_attr( $s->id ); ?>"><?php echo esc_html( $s->name ); ?></option>
<?php endforeach; ?>
</select>
<button class="button" id="wc-quar-load" disabled>Load Quarantine</button>
</div>
<div id="wc-quar-notices"></div>
<div id="wc-quar-wrap">
<p class="description">Select a server above to view quarantined messages.</p>
</div>
</div>
<?php
}
// ── AJAX helpers ─────────────────────────────────────────────────────────
private function verify(): void {
@@ -595,7 +705,26 @@ class WooCow_Admin {
if ( ! $result['success'] ) {
$this->json_err( $result['error'] ?? 'Failed to fetch domains.' );
}
$domains = array_map( fn( $d ) => [ 'domain' => $d['domain_name'], 'active' => $d['active'] ], (array) $result['data'] );
$domains = array_map( function ( $d ) {
$rl = is_array( $d['rl'] ?? false ) ? $d['rl'] : [];
return [
'domain' => $d['domain_name'],
'active' => $d['active'],
'description' => $d['description'] ?? '',
'mailboxes' => $d['max_num_mboxes_for_domain'] ?? 0,
'mboxes_in' => $d['mboxes_in_domain'] ?? 0,
'aliases' => $d['max_num_aliases_for_domain'] ?? 0,
'aliases_in' => $d['aliases_in_domain'] ?? 0,
'quota' => (int) round( ( $d['max_quota_for_domain'] ?? 0 ) / 1024 / 1024 ),
'defquota' => (int) round( ( $d['def_new_mailbox_quota'] ?? 0 ) / 1024 / 1024 ),
'quota_used' => (int) round( ( $d['quota_used_in_domain'] ?? 0 ) / 1024 / 1024 ),
'relayhost' => $d['relayhost'] ?? '0',
'rl_value' => $rl['value'] ?? 0,
'rl_frame' => $rl['frame'] ?? 's',
'gal' => $d['gal'] ?? '0',
'backupmx' => $d['backupmx'] ?? '0',
];
}, (array) $result['data'] );
$this->json_ok( $domains );
}
@@ -904,6 +1033,90 @@ class WooCow_Admin {
] );
}
public function ajax_woocow_admin_domain_edit(): void {
$this->verify();
$server_id = absint( $_POST['server_id'] ?? 0 );
$domain = sanitize_text_field( $_POST['domain'] ?? '' );
if ( ! $domain ) {
$this->json_err( 'Domain name is required.' );
}
$server = $this->get_server( $server_id );
if ( ! $server ) {
$this->json_err( 'Server not found.' );
}
$attr = [];
if ( isset( $_POST['description'] ) ) {
$attr['description'] = sanitize_text_field( $_POST['description'] );
}
if ( isset( $_POST['mailboxes'] ) ) {
$attr['mailboxes'] = absint( $_POST['mailboxes'] );
}
if ( isset( $_POST['aliases'] ) ) {
$attr['aliases'] = absint( $_POST['aliases'] );
}
if ( isset( $_POST['quota'] ) ) {
$attr['quota'] = absint( $_POST['quota'] );
$attr['maxquota'] = absint( $_POST['quota'] );
}
if ( isset( $_POST['defquota'] ) ) {
$attr['defquota'] = absint( $_POST['defquota'] );
}
if ( isset( $_POST['rl_value'] ) ) {
$attr['rl_value'] = absint( $_POST['rl_value'] );
}
if ( isset( $_POST['rl_frame'] ) ) {
$frame = sanitize_text_field( $_POST['rl_frame'] );
if ( in_array( $frame, [ 's', 'm', 'h', 'd' ], true ) ) {
$attr['rl_frame'] = $frame;
}
}
if ( isset( $_POST['relayhost'] ) ) {
$attr['relayhost'] = absint( $_POST['relayhost'] );
}
if ( isset( $_POST['active'] ) ) {
$attr['active'] = absint( $_POST['active'] );
}
$api = WooCow_API::from_server( $server );
$result = $api->edit_domain( [ $domain ], $attr );
if ( ! $result['success'] ) {
$this->json_err( $result['error'] ?? 'Failed to update domain.' );
}
$this->json_ok();
}
public function ajax_woocow_admin_domain_delete(): void {
$this->verify();
$server_id = absint( $_POST['server_id'] ?? 0 );
$domain = sanitize_text_field( $_POST['domain'] ?? '' );
if ( ! $domain ) {
$this->json_err( 'Domain name is required.' );
}
$server = $this->get_server( $server_id );
if ( ! $server ) {
$this->json_err( 'Server not found.' );
}
$api = WooCow_API::from_server( $server );
$result = $api->delete_domain( $domain );
if ( ! $result['success'] ) {
$this->json_err( $result['error'] ?? 'Failed to delete domain.' );
}
$this->json_ok();
}
// ── AJAX: Transports ──────────────────────────────────────────────────────
public function ajax_woocow_admin_relayhosts_list(): void {
@@ -982,7 +1195,7 @@ class WooCow_Admin {
$server_id = absint( $_POST['server_id'] ?? 0 );
$log_type = sanitize_key( $_POST['log_type'] ?? 'postfix' );
$allowed = [ 'postfix', 'dovecot', 'rspamd', 'ratelimit', 'api', 'acme', 'autodiscover', 'sogo', 'netfilter', 'watchdog' ];
$allowed = [ 'postfix', 'dovecot', 'rspamd-history', 'ratelimited', 'api', 'acme', 'autodiscover', 'sogo', 'netfilter', 'watchdog' ];
if ( ! in_array( $log_type, $allowed, true ) ) {
$this->json_err( 'Invalid log type.' );
}
@@ -993,7 +1206,7 @@ class WooCow_Admin {
}
$api = WooCow_API::from_server( $server );
$result = $api->request( 'GET', '/api/v1/get/logs/' . $log_type );
$result = $api->request( 'GET', '/api/v1/get/logs/' . $log_type . '/100' );
if ( ! $result['success'] ) {
$this->json_err( $result['error'] ?? 'Failed to fetch logs.' );
@@ -1001,4 +1214,76 @@ class WooCow_Admin {
$this->json_ok( $result['data'] ?? [] );
}
// ── AJAX: Admin Quarantine ─────────────────────────────────────────────────
public function ajax_woocow_admin_quarantine(): void {
$this->verify();
$server_id = absint( $_POST['server_id'] ?? 0 );
$server = $this->get_server( $server_id );
if ( ! $server ) {
$this->json_err( 'Server not found.' );
}
$api = WooCow_API::from_server( $server );
$result = $api->get_quarantine();
if ( ! $result['success'] ) {
$this->json_err( $result['error'] ?? 'Failed to fetch quarantine.' );
}
$this->json_ok( $result['data'] ?? [] );
}
public function ajax_woocow_admin_quarantine_delete(): void {
$this->verify();
$server_id = absint( $_POST['server_id'] ?? 0 );
$qid = absint( $_POST['qid'] ?? 0 );
if ( ! $qid ) {
$this->json_err( 'Message ID is required.' );
}
$server = $this->get_server( $server_id );
if ( ! $server ) {
$this->json_err( 'Server not found.' );
}
$api = WooCow_API::from_server( $server );
$result = $api->delete_quarantine( $qid );
if ( ! $result['success'] ) {
$this->json_err( $result['error'] ?? 'Failed to delete quarantine message.' );
}
$this->json_ok();
}
public function ajax_woocow_admin_quarantine_block(): void {
$this->verify();
$server_id = absint( $_POST['server_id'] ?? 0 );
$domain = sanitize_text_field( $_POST['domain'] ?? '' );
$object_from = sanitize_text_field( $_POST['object_from'] ?? '' );
if ( ! $domain || ! $object_from ) {
$this->json_err( 'Domain and sender address are required.' );
}
$server = $this->get_server( $server_id );
if ( ! $server ) {
$this->json_err( 'Server not found.' );
}
$api = WooCow_API::from_server( $server );
$result = $api->add_domain_policy( $domain, $object_from, 'bl' );
if ( ! $result['success'] ) {
$this->json_err( $result['error'] ?? 'Failed to blacklist sender.' );
}
$this->json_ok();
}
}

View File

@@ -183,6 +183,24 @@ class WooCow_API {
return $this->request( 'POST', '/api/v1/delete/quarantine', [ 'items' => [ $id ] ] );
}
// ── Domain Policies ──────────────────────────────────────────────────────
public function get_domain_policy_bl( string $domain ): array {
return $this->request( 'GET', '/api/v1/get/policy_bl_domain/' . rawurlencode( $domain ) );
}
public function add_domain_policy( string $domain, string $object_from, string $list = 'bl' ): array {
return $this->request( 'POST', '/api/v1/add/domain-policy', [
'domain' => $domain,
'object_from' => $object_from,
'object_list' => $list,
] );
}
public function delete_domain_policy( array $prefids ): array {
return $this->request( 'POST', '/api/v1/delete/domain-policy', [ 'items' => $prefids ] );
}
// ── Helpers ──────────────────────────────────────────────────────────────
public function get_webmail_url(): string {