From f01e3e59c58f09bfaf31810fd062241b5271a416 Mon Sep 17 00:00:00 2001 From: heavygale Date: Sun, 29 Sep 2019 19:04:00 +0200 Subject: [PATCH] don't count assigned domain quota for inactive domains now only summing up the assigned domain quota for active domains, and not subtracting disc base occupied by mailboxes of inactive domains (inactive_bytes) --- data/web/debug.php | 3 +- data/web/inc/functions.domain_admin.inc.php | 2 +- data/web/inc/functions.mailbox.inc.php | 49 +++++++++++++-------- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/data/web/debug.php b/data/web/debug.php index 09bd44b5..eaa4b9a9 100644 --- a/data/web/debug.php +++ b/data/web/debug.php @@ -44,8 +44,9 @@ else { $exec_fields = array('cmd' => 'system', 'task' => 'df', 'dir' => '/var/vmail'); $vmail_df = explode(',', json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true)); $domainQuota = round(domain_admin('total_quota')/1024); + $inactive_bytes = round(mailbox('get','inactive_bytes')/1024/1024/1024); $quotaPercent1 = round(($domainQuota/substr($vmail_df[3], 0, -1))*100); - $quotaPercent2 = round((($domainQuota-substr($vmail_df[2], 0, -1))/substr($vmail_df[3], 0, -1))*100); + $quotaPercent2 = round((($domainQuota-substr($vmail_df[2], 0, -1)+$inactive_bytes)/substr($vmail_df[3], 0, -1))*100); $quotaPercent2 = ($quotaPercent2+substr($vmail_df[4], 0, -1)>100) ? 100-substr($vmail_df[4], 0, -1) : $quotaPercent2; //handling overcommitment ?>
diff --git a/data/web/inc/functions.domain_admin.inc.php b/data/web/inc/functions.domain_admin.inc.php index 2bd2b807..7fe4b3fa 100644 --- a/data/web/inc/functions.domain_admin.inc.php +++ b/data/web/inc/functions.domain_admin.inc.php @@ -453,7 +453,7 @@ function domain_admin($_action, $_data = null) { ); return false; } - $stmt = $pdo->query("SELECT SUM(`quota`) AS `quota` FROM `domain`"); + $stmt = $pdo->query("SELECT SUM(`quota`) AS `quota` FROM `domain` WHERE `active`=1"); $row = $stmt->fetch(PDO::FETCH_ASSOC); return $row['quota']; break; diff --git a/data/web/inc/functions.mailbox.inc.php b/data/web/inc/functions.mailbox.inc.php index 99fda2d8..0b781a6c 100644 --- a/data/web/inc/functions.mailbox.inc.php +++ b/data/web/inc/functions.mailbox.inc.php @@ -46,7 +46,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { $stmt = $pdo->prepare("SELECT `domain` FROM `mailbox` WHERE `username` = :username"); $stmt->execute(array(':username' => $_SESSION['mailcow_cc_username'])); $domain = $stmt->fetch(PDO::FETCH_ASSOC)['domain']; - $validity = strtotime("+".$_data["validity"]." hour"); + $validity = strtotime("+".$_data["validity"]." hour"); $letters = 'abcefghijklmnopqrstuvwxyz1234567890'; $random_name = substr(str_shuffle($letters), 0, 24); $stmt = $pdo->prepare("INSERT INTO `spamalias` (`address`, `goto`, `validity`) VALUES @@ -485,7 +485,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { 'msg' => 'comment_too_long' ); return false; - } + } if (empty($addresses[0])) { $_SESSION['return'][] = array( 'type' => 'danger', @@ -841,7 +841,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { WHERE `domain` = :domain"); $stmt->execute(array(':domain' => $domain)); $DomainData = $stmt->fetch(PDO::FETCH_ASSOC); - $stmt = $pdo->prepare("SELECT + $stmt = $pdo->prepare("SELECT COUNT(*) as count, COALESCE(ROUND(SUM(`quota`)/1048576), 0) as `quota` FROM `mailbox` @@ -945,7 +945,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { ); return false; } - $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `attributes`, `active`) + $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `attributes`, `active`) VALUES (:username, :password_hashed, :name, :quota_b, :local_part, :domain, :mailbox_attrs, :active)"); $stmt->execute(array( ':username' => $username, @@ -1073,7 +1073,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { ); return false; } - $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `active`, `multiple_bookings`, `kind`) + $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `active`, `multiple_bookings`, `kind`) VALUES (:name, 'RESOURCE', :description, 0, :local_part, :domain, :active, :multiple_bookings, :kind)"); $stmt->execute(array( ':name' => $name, @@ -1249,7 +1249,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { 'msg' => 'access_denied' ); continue; - } + } $stmt = $pdo->prepare("UPDATE `mailbox` SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', :quarantine_notification) WHERE `username` = :username"); @@ -1360,7 +1360,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { continue; } $validity = round((int)time() + ($_data['validity'] * 3600)); - $stmt = $pdo->prepare("UPDATE `spamalias` SET `validity` = :validity WHERE + $stmt = $pdo->prepare("UPDATE `spamalias` SET `validity` = :validity WHERE `address` = :address"); $stmt->execute(array( ':address' => $address, @@ -1888,7 +1888,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { ); continue; } - $stmt = $pdo->prepare("UPDATE `domain` SET + $stmt = $pdo->prepare("UPDATE `domain` SET `description` = :description, `gal` = :gal WHERE `domain` = :domain"); @@ -1928,7 +1928,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { continue; } // todo: should be using api here - $stmt = $pdo->prepare("SELECT + $stmt = $pdo->prepare("SELECT COUNT(*) AS count, MAX(COALESCE(ROUND(`quota`/1048576), 0)) AS `biggest_mailbox`, COALESCE(ROUND(SUM(`quota`)/1048576), 0) AS `quota_all` @@ -2009,7 +2009,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { ); continue; } - $stmt = $pdo->prepare("UPDATE `domain` SET + $stmt = $pdo->prepare("UPDATE `domain` SET `relay_all_recipients` = :relay_all_recipients, `backupmx` = :backupmx, `gal` = :gal, @@ -2071,7 +2071,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { $domain = $is_now['domain']; $quota_b = $quota_m * 1048576; $password = (!empty($_data['password'])) ? $_data['password'] : null; - $password2 = (!empty($_data['password2'])) ? $_data['password2'] : null; + $password2 = (!empty($_data['password2'])) ? $_data['password2'] : null; } else { $_SESSION['return'][] = array( @@ -2517,14 +2517,14 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { } $stmt = $pdo->prepare("SELECT `domain` FROM `domain` WHERE `domain` NOT IN ( - SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl` + SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as1 AND `external` = '0' AND `send_as` LIKE '@%') UNION SELECT '*' FROM `domain` WHERE '*' NOT IN ( - SELECT `send_as` FROM `sender_acl` + SELECT `send_as` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as2 AND `external` = '0' )"); @@ -2546,7 +2546,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { $stmt = $pdo->prepare("SELECT `address` FROM `alias` WHERE `goto` != :goto AND `address` NOT IN ( - SELECT `send_as` FROM `sender_acl` + SELECT `send_as` FROM `sender_acl` WHERE `logged_in_as` = :logged_in_as AND `external` = '0' AND `send_as` NOT LIKE '@%')"); @@ -3074,11 +3074,11 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { if (!empty($row)) { $_data = $row['target_domain']; } - $stmt = $pdo->prepare("SELECT + $stmt = $pdo->prepare("SELECT `domain`, `description`, `aliases`, - `mailboxes`, + `mailboxes`, `defquota`, `maxquota`, `quota`, @@ -3096,7 +3096,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { ':domain' => $_data )); $row = $stmt->fetch(PDO::FETCH_ASSOC); - if (empty($row)) { + if (empty($row)) { return false; } $stmt = $pdo->prepare("SELECT COUNT(*) AS `count`, @@ -3254,6 +3254,19 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { } return $resourcedata; break; + case 'inactive_bytes': + if ($_SESSION['mailcow_cc_role'] != "admin") { + return false; + } + $stmt = $pdo->query("SELECT SUM(`quota2`.`bytes`) AS `bytes` + FROM `mailbox`, `quota2`, `domain` + WHERE `mailbox`.`kind` NOT REGEXP 'location|thing|group' + AND `mailbox`.`username` = `quota2`.`username` + AND `domain`.`domain` = `mailbox`.`domain` + AND `domain`.`active` = 0"); + $row = $stmt->fetch(PDO::FETCH_ASSOC); + return $row['bytes']; + break; } break; case 'delete': @@ -3724,7 +3737,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) { curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: text/xml')); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); - curl_setopt($curl, CURLOPT_POSTFIELDS, 'user:' . $username . ''); + curl_setopt($curl, CURLOPT_POSTFIELDS, 'user:' . $username . ''); curl_setopt($curl, CURLOPT_TIMEOUT, 30); $response = curl_exec($curl); if ($response === false) {