From af6a1b6626a296f5db0219d07d18d0e578d4a4d4 Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Tue, 19 Apr 2011 20:18:46 +0000 Subject: [PATCH] Add support for domain-level quota (total quota for a domain) Based on a patch from W. Rossmann (W. Rossmann@SF), https://sourceforge.net/tracker/index.php?func=detail&aid=2974928&group_id=191583&atid=937966 with some modifications, cleanup and adoptions to trunk (especially templates) config.inc.php: - new config option $CONF['domain_quota'] to enable/disable domain-level quota (default: enabled) - new config option $CONF['domain_quota_default'] (default: 2 GB) functions.inc.php - check_quota(): - add code to check the quota sum on a domain - add optional parameter $username (to exclude that username from quota calculation, used by edit-mailbox) edit-domain.php, create-domain.php, admin_edit-domain.tpl, admin_create-domain.tpl: - add input field and handling for domain-level quota list-domain.php, overview-get.tpl, adminlistdomain.tpl: - display allocated and allowed domain quota - beautify quota and max_quota fields - display "unlimited" instead of "-1" edit-mailbox.php: - hand over username to check_quota() git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1042 a1433add-5e2c-0410-b055-b7f2511e0802 --- config.inc.php | 3 +++ create-domain.php | 11 ++++++--- edit-domain.php | 9 +++++++- edit-mailbox.php | 2 +- functions.inc.php | 38 +++++++++++++++++++++++++------ list-domain.php | 5 +++- templates/admin_create-domain.tpl | 7 ++++++ templates/admin_edit-domain.tpl | 7 ++++++ templates/adminlistdomain.tpl | 4 +++- templates/overview-get.tpl | 2 ++ 10 files changed, 74 insertions(+), 14 deletions(-) diff --git a/config.inc.php b/config.inc.php index d7e41e58..ddde116f 100644 --- a/config.inc.php +++ b/config.inc.php @@ -182,10 +182,13 @@ function maildir_name_hook($domain, $user) { $CONF['aliases'] = '10'; $CONF['mailboxes'] = '10'; $CONF['maxquota'] = '10'; +$CONF['domain_quota_default'] = '2048'; // Quota // When you want to enforce quota for your mailbox users set this to 'YES'. $CONF['quota'] = 'NO'; +// If you want to enforce domain-level quotas set this to 'YES'. +$CONF['domain_quota'] = 'YES'; // You can either use '1024000' or '1048576' $CONF['quota_multiplier'] = '1024000'; diff --git a/create-domain.php b/create-domain.php index 824426fe..645d8c7d 100644 --- a/create-domain.php +++ b/create-domain.php @@ -46,7 +46,8 @@ $form_fields = array( 'fDescription' => array('type' => 'str', 'default' =>''), 'fAliases' => array('type' => 'int', 'default' => $CONF['aliases']), 'fMailboxes' => array('type' => 'int', 'default' => $CONF['mailboxes']), - 'fMaxquota' => array('type' => 'int', 'default' => $CONF['maxquota']), + 'fMaxquota' => array('type' => 'int', 'default' => $CONF['maxquota']), + 'fDomainquota' => array('type' => 'int', 'default' => $CONF['domain_quota_default']), 'fTransport' => array('type' => 'str', 'default' => $CONF['transport_default'], 'options' => $CONF['transport_options']), 'fDefaultaliases' => array('type' => 'str', 'default' => 'on', 'options' => array('on', 'off')), 'fBackupmx' => array('type' => 'str', 'default' => 'off', 'options' => array('on', 'off')) @@ -80,6 +81,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") $tTransport = $fTransport; $tAliases = $fAliases; $tMaxquota = $fMaxquota; + $tDomainquota = $fDomainquota; $tMailboxes = $fMailboxes; $tDefaultaliases = $fDefaultaliases; $tBackupmx = $fBackupmx; @@ -96,6 +98,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") $tAliases = $fAliases; $tMailboxes = $fMailboxes; if (isset ($_POST['fMaxquota'])) $tMaxquota = $fMaxquota; + if (isset ($_POST['fDomainquota'])) $tDomainquota = $fDomainquota; if (isset ($_POST['fTransport'])) $tTransport = $fTransport; if (isset ($_POST['fDefaultaliases'])) $tDefaultaliases = $fDefaultaliases; if (isset ($_POST['fBackupmx'])) $tBackupmx = $fBackupmx; @@ -108,6 +111,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") $tAliases = $CONF['aliases']; $tMailboxes = $CONF['mailboxes']; $tMaxquota = $CONF['maxquota']; + $tDomainquota = $CONF['domain_quota_default']; if ($fBackupmx == "on") { @@ -120,7 +124,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") $sqlBackupmx = db_get_boolean(false); } - $sql_query = "INSERT INTO $table_domain (domain,description,aliases,mailboxes,maxquota,transport,backupmx,created,modified) VALUES ('$fDomain','$fDescription',$fAliases,$fMailboxes,$fMaxquota,'$fTransport','$sqlBackupmx',NOW(),NOW())"; + $sql_query = "INSERT INTO $table_domain (domain,description,aliases,mailboxes,maxquota,quota,transport,backupmx,created,modified) VALUES ('$fDomain','$fDescription',$fAliases,$fMailboxes,$fMaxquota,$fDomainquota,'$fTransport','$sqlBackupmx',NOW(),NOW())"; $result = db_query($sql_query); if ($result['rows'] != 1) { @@ -150,7 +154,8 @@ $smarty->assign ('pAdminCreate_domain_domain_text', $pAdminCreate_domain_domain_ $smarty->assign ('tDescription', $tDescription, false); $smarty->assign ('tAliases', $tAliases); $smarty->assign ('tMailboxes', $tMailboxes); -$smarty->assign ('tMaxquota', $tMaxquota,false); +$smarty->assign ('tDomainquota', $tDomainquota); +$smarty->assign ('tMaxquota', $tMaxquota,false); # TODO: why is sanitize disabled? Should be just integer... $smarty->assign ('select_options', select_options ($CONF ['transport_options'], array ($tTransport)),false); $smarty->assign ('tDefaultaliases', ($tDefaultaliases == 'on') ? ' checked="checked"' : ''); $smarty->assign ('tBackupmx', ($tBackupmx == 'on') ? ' checked="checked"' : ''); diff --git a/edit-domain.php b/edit-domain.php index 965f0e3e..b1493671 100644 --- a/edit-domain.php +++ b/edit-domain.php @@ -47,6 +47,7 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") $tDescription = $domain_properties['description']; $tAliases = $domain_properties['aliases']; $tMailboxes = $domain_properties['mailboxes']; + $tDomainquota = $domain_properties['quota']; $tMaxquota = $domain_properties['maxquota']; $tTransport = $domain_properties['transport']; $tBackupmx = $domain_properties['backupmx']; @@ -66,6 +67,11 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") } else { $fMaxquota = 0; } + if (isset ($_POST['fDomainquota'])) { + $fDomainquota = intval($_POST['fDomainquota']); + } else { + $fDomainquota = $CONF['domain_quota_default']; + } $fTransport = $CONF['transport_default']; if($CONF['transport'] != 'NO' && isset ($_POST['fTransport'])) { @@ -101,7 +107,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") $sqltransport = "transport='$fTransport',"; } - $result = db_query ("UPDATE $table_domain SET description='$fDescription',aliases=$fAliases,mailboxes=$fMailboxes,maxquota=$fMaxquota,$sqltransport backupmx='$sqlBackupmx',active='$sqlActive',modified=NOW() WHERE domain='$domain'"); + $result = db_query ("UPDATE $table_domain SET description='$fDescription',aliases=$fAliases,mailboxes=$fMailboxes,maxquota=$fMaxquota,quota=$fDomainquota,$sqltransport backupmx='$sqlBackupmx',active='$sqlActive',modified=NOW() WHERE domain='$domain'"); if ($result['rows'] == 1) { header ("Location: list-domain.php"); @@ -118,6 +124,7 @@ $smarty->assign ('tDescription', $tDescription); $smarty->assign ('tAliases', $tAliases); $smarty->assign ('tMailboxes', $tMailboxes); $smarty->assign ('tMaxquota', $tMaxquota); +$smarty->assign ('tDomainquota', $tDomainquota); $smarty->assign ('select_options', select_options($CONF['transport_options'], array($tTransport)), false); if ($tBackupmx) $smarty->assign ('tBackupmx', ' checked="checked"'); if ($tActive) $smarty->assign ('tActive', ' checked="checked"'); diff --git a/edit-mailbox.php b/edit-mailbox.php index b4437faa..2242d1e2 100644 --- a/edit-mailbox.php +++ b/edit-mailbox.php @@ -117,7 +117,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") } if ($CONF['quota'] == "YES") { - if (!check_quota ($fQuota, $fDomain)) + if (!check_quota ($fQuota, $fDomain, $fUsername)) { $error = 1; $tName = $fName; diff --git a/functions.inc.php b/functions.inc.php index 2ccc512c..5197b0fc 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -678,29 +678,53 @@ function check_mailbox ($domain) // Action: Checks if the user is creating a mailbox with the correct quota // Call: check_quota (string domain) // -function check_quota ($quota, $domain) -{ +function check_quota ($quota, $domain, $username="") { + global $CONF; + $rval = false; $limit = get_domain_properties ($domain); if ($limit['maxquota'] == 0) { - return true; + $rval = true; } if (($limit['maxquota'] < 0) and ($quota < 0)) { - return true; + $rval = true; } if (($limit['maxquota'] > 0) and ($quota == 0)) { - return false; + $rval = false; } if ($quota > $limit['maxquota']) { - return false; + $rval = false; } else { - return true; + $rval = true; } + + # TODO: detailed error message ("domain quota exceeded", "mailbox quota too big" etc.) via flash_error? Or "available quota: xxx MB"? + if (!$rval || $CONF['domain_quota'] != 'YES') { + return $rval; + } elseif ($limit['quota'] <= 0) { + $rval = true; + } else { + $table_mailbox = table_by_key('mailbox'); + $query = "SELECT SUM(quota) FROM $table_mailbox WHERE domain = '" . escape_string($domain) . "'"; + if ($username != "") { + $query .= " AND username != '" . escape_string($username) . "'"; + } + $result = db_query ($query); + $row = db_row ($result['result']); + $cur_quota_total = divide_quota($row[0]); # convert to MB + if ( ($quota + $cur_quota_total) > $limit['quota'] ) { + $rval = false; + } else { + $rval = true; + } + } + + return $rval; } diff --git a/list-domain.php b/list-domain.php index 1c9cac31..150e77b5 100644 --- a/list-domain.php +++ b/list-domain.php @@ -73,7 +73,7 @@ $table_domain_fieldlist = " "; $query = " - SELECT $table_domain_fieldlist , COUNT( DISTINCT $table_mailbox.username ) AS mailbox_count + SELECT $table_domain_fieldlist , COUNT( DISTINCT $table_mailbox.username ) AS mailbox_count, SUM( $table_mailbox.quota ) AS total_quota FROM $table_domain LEFT JOIN $table_mailbox ON $table_domain.domain = $table_mailbox.domain $where @@ -103,6 +103,9 @@ $result = db_query($query); while ($row = db_array ($result['result'])) { # add number of aliases to $domain_properties array. mailbox aliases do not count. $domain_properties [$row['domain']] ['alias_count'] = $row['alias_count'] - $domain_properties [$row['domain']] ['mailbox_count']; + $domain_properties [$row['domain']] ['total_quota'] = (int) divide_quota($domain_properties [$row['domain']] ['total_quota']); # convert to MB + if ($domain_properties [$row['domain']] ['quota'] == -1) $domain_properties [$row['domain']] ['quota'] = $PALANG['pOverview_unlimited']; + if ($domain_properties [$row['domain']] ['maxquota'] == -1) $domain_properties [$row['domain']] ['maxquota'] = $PALANG['pOverview_unlimited']; } $smarty->assign ('domain_properties', $domain_properties); diff --git a/templates/admin_create-domain.tpl b/templates/admin_create-domain.tpl index 54cb2385..1fea865b 100644 --- a/templates/admin_create-domain.tpl +++ b/templates/admin_create-domain.tpl @@ -24,6 +24,13 @@ {$PALANG.pAdminCreate_domain_mailboxes_text} +{if $CONF.domain_quota===YES} + + {$PALANG.pAdminEdit_domain_quota}: + + {$PALANG.pAdminCreate_domain_maxquota_text} + +{/if} {if $CONF.quota===YES} {$PALANG.pAdminCreate_domain_maxquota}: diff --git a/templates/admin_edit-domain.tpl b/templates/admin_edit-domain.tpl index 3f48bb22..e3a52e88 100644 --- a/templates/admin_edit-domain.tpl +++ b/templates/admin_edit-domain.tpl @@ -24,6 +24,13 @@ {$PALANG.pAdminEdit_domain_mailboxes_text} +{if $CONF.domain_quota===YES} + + {$PALANG.pAdminEdit_domain_quota}: + + {$PALANG.pAdminEdit_domain_maxquota_text} + +{/if} {if $CONF.quota===YES} {$PALANG.pAdminEdit_domain_maxquota}: diff --git a/templates/adminlistdomain.tpl b/templates/adminlistdomain.tpl index 4cc6b1c5..e483e2c6 100644 --- a/templates/adminlistdomain.tpl +++ b/templates/adminlistdomain.tpl @@ -14,7 +14,8 @@ {$PALANG.pAdminList_domain_description} {$PALANG.pAdminList_domain_aliases} {$PALANG.pAdminList_domain_mailboxes} - {if $CONF.quota==YES}{$PALANG.pAdminList_domain_maxquota}{/if} + {if $CONF.quota==YES}{$PALANG.pOverview_get_quota}{/if} + {if $CONF.domain_quota==YES}{$PALANG.pAdminList_domain_quota}{/if} {if $CONF.transport==YES}{$PALANG.pAdminList_domain_transport}{/if} {$PALANG.pAdminList_domain_backupmx} {$PALANG.pAdminList_domain_modified} @@ -28,6 +29,7 @@ {$domain.alias_count} / {$domain.aliases} {$domain.mailbox_count} / {$domain.mailboxes} {if $CONF.quota==YES}{$domain.maxquota}{/if} + {if $CONF.domain_quota===YES}{$domain.total_quota} / {$domain.quota}{/if} {if $CONF.transport==YES}{$domain.transport}{/if} {$domain.backupmx} {$domain.modified} diff --git a/templates/overview-get.tpl b/templates/overview-get.tpl index 4f726e0c..aabd7193 100644 --- a/templates/overview-get.tpl +++ b/templates/overview-get.tpl @@ -16,6 +16,7 @@ {$PALANG.pOverview_get_aliases} {$PALANG.pOverview_get_mailboxes} {if $CONF.quota===YES}{$PALANG.pOverview_get_quota}{/if} + {if $CONF.domain_quota===YES}{$PALANG.pAdminList_domain_quota}{/if} {foreach from=$domain_properties item=domain} {#tr_hilightoff#} @@ -23,6 +24,7 @@ {$domain.alias_count} / {$domain.aliases} {$domain.mailbox_count} / {$domain.mailboxes} {if $CONF.quota===YES}{$domain.maxquota}{/if} + {if $CONF.domain_quota===YES}{$domain.total_quota} / {$domain.quota}{/if} {/foreach}