(See #1364) Fix some bugs that gave us a bad display of user quotas

This commit is contained in:
Axel ROGER 2012-11-08 14:33:46 +00:00
parent 28b949201e
commit ecafca7e58
2 changed files with 60 additions and 48 deletions

View File

@ -3,6 +3,7 @@
require_once("../class/config.php"); require_once("../class/config.php");
if (!defined("QUOTASONE")) return; if (!defined("QUOTASONE")) return;
//FIXME missing getfield for $mode
if (!isset($mode)) { # when included from adm_login, mode is not set if (!isset($mode)) { # when included from adm_login, mode is not set
$mode = 0; $mode = 0;
} }
@ -13,19 +14,20 @@ if (!isset($mode)) { # when included from adm_login, mode is not set
<div style="width: 550px"> <div style="width: 550px">
<!-- Les esapces web --> <!-- Webspaces -->
<?php <?php
$totalweb = $quota->get_size_web_sum_user($mem->user["uid"]); $totalweb = $quota->get_size_web_sum_user($mem->user["uid"]);
// $totalweb is in KB, so we call get_size_unit() with it in Bytes
$t=$quota->get_size_unit($totalweb * 1024);
echo "<p>"._("Web Space:")." "; echo "<p>"._("Web Space:")." ";
echo sprintf("%.1f", $totalweb / 1024)."&nbsp;"._("MB"); echo sprintf("%.1f", $t['size'])."&nbsp;".$t['unit'];
echo "</p>"; echo "</p>";
?> ?>
<!-- Les mails --> <!-- Mails -->
<table class="tedit"> <table class="tedit">
<thead> <thead>
@ -45,32 +47,36 @@ if (!isset($mode)) { # when included from adm_login, mode is not set
$totalmail+=$mstmp; $totalmail+=$mstmp;
} }
$t=$quota->get_size_unit($totalmail);
echo "<p>"._("Mailboxes size:")." "; echo "<p>"._("Mailboxes size:")." ";
echo sprintf("%.1f", $totalmail / 1024)."&nbsp;"._("MB"); echo sprintf("%.1f", $t['size'])."&nbsp;".$t['unit'];
echo "</p>"; echo "</p>";
foreach ($domaines_user as $domaine) { foreach ($domaines_user as $domaine) {
$alias_sizes = $quota->get_size_mail_details_domain($domaine); $alias_sizes = $quota->get_size_mail_details_domain($domaine);
$domsize = 0; $domsize = 0;
foreach ($alias_sizes as $e) { foreach ($alias_sizes as $e) {
$domsize += $e['size']; if($e['size'] > 0) {
echo "<tr><td>{$domaine}</td>"; $domsize += $e['size'];
echo "<td>".str_replace("_","@",$e["alias"])."</td>"; $d = $quota->get_size_unit($domsize);
echo "<td"; if ($mode!=2) echo " style=\"text-align: right\""; echo ">"; echo "<tr><td>{$domaine}</td>";
$ms=$e["size"]; echo "<td>".str_replace("_","@",$e["alias"])."</td>";
if ($totalmail) { echo "<td"; if ($mode!=2) echo " style=\"text-align: right\""; echo ">";
$pc=intval(100*$ms/$totalmail); $ms = $quota->get_size_unit($e['size']);
} else { if ($totalmail) {
$pc=0; $pc=intval(100*($ms['size']/$totalmail));
} else {
$pc=0;
}
if ($mode==0) {
echo sprintf("%.1f", $ms['size'])."&nbsp;".$ms['unit'];
} elseif ($mode==1) {
echo sprintf("%.1f", $pc)."&nbsp;%";
} else {
echo "<img src=\"hippo_bleue.gif\" style=\"width: ".(2*$pc)."px; height: 16px\" alt=\"".$pc."%\" title=\"".$pc."\"/>";
}
echo "</td></tr>";
} }
if ($mode==0) {
echo sprintf("%.1f", $ms / 1024)."&nbsp;"._("MB");
} elseif ($mode==1) {
echo sprintf("%.1f", $pc)."&nbsp;%";
} else {
echo "<img src=\"hippo_bleue.gif\" style=\"width: ".(2*$pc)."px; height: 16px\" alt=\"".$pc."%\" title=\"".$pc."\"/>";
}
echo "</td></tr>";
} }
if ($totalmail) { if ($totalmail) {
$tpc = intval(100 * $domsize / $totalmail); $tpc = intval(100 * $domsize / $totalmail);
@ -83,7 +89,7 @@ if (!isset($mode)) { # when included from adm_login, mode is not set
if ($mode!=2) echo " style=\"text-align: right\""; if ($mode!=2) echo " style=\"text-align: right\"";
echo "><i>"; echo "><i>";
if ($mode==0) { if ($mode==0) {
echo sprintf("%.1f", $domsize / 1024)."&nbsp;"._("MB"); echo sprintf("%.1f", $d['size'])."&nbsp;".$d['unit'];
} elseif ($mode==1) { } elseif ($mode==1) {
echo sprintf("%.1f", $tpc)."&nbsp;%"; echo sprintf("%.1f", $tpc)."&nbsp;%";
} else { } else {
@ -91,18 +97,19 @@ if (!isset($mode)) { # when included from adm_login, mode is not set
} }
echo "</i></td></tr>"; echo "</i></td></tr>";
} }
} }
?> ?>
</tbody> </tbody>
</table> </table>
<!-- Les bases --> <!-- Databases -->
<?php <?php
$totaldb = $quota->get_size_db_sum_user($mem->user["login"]); $totaldb = $quota->get_size_db_sum_user($mem->user["login"]);
$t = $quota->get_size_unit($totaldb);
echo "<p>"._("Databases:")." "; echo "<p>"._("Databases:")." ";
echo sprintf("%.1f", $totaldb/(1024*1024))."&nbsp;"._("MB"); echo sprintf("%.1f", $t['size'])."&nbsp;".$t['unit'];
echo "</p>"; echo "</p>";
?> ?>
@ -121,14 +128,14 @@ if (!isset($mode)) { # when included from adm_login, mode is not set
echo "<tr><td>".$d["db"]."</td><td"; echo "<tr><td>".$d["db"]."</td><td";
if ($mode!=2) echo " style=\"text-align: right\""; if ($mode!=2) echo " style=\"text-align: right\"";
echo ">"; echo ">";
$ds=$d["size"]; $ds = $quota->get_size_unit($d["size"]);
if ($totaldb) { if ($totaldb) {
$pc=intval(100*$ds/$totaldb); $pc=intval(100*$ds['size']/$totaldb);
} else { } else {
$pc=0; $pc=0;
} }
if (isset($mode) && $mode==0) { if (isset($mode) && $mode==0) {
echo sprintf("%.1f", $ds / 1024/1024)."&nbsp;"._("MB"); echo sprintf("%.1f", $ds['size'])."&nbsp;".$ds['unit'];
} elseif (isset($mode) &&$mode==1) { } elseif (isset($mode) &&$mode==1) {
echo sprintf("%.1f", $pc)."&nbsp;%"; echo sprintf("%.1f", $pc)."&nbsp;%";
} else { } else {
@ -140,17 +147,16 @@ if (!isset($mode)) { # when included from adm_login, mode is not set
</tbody> </tbody>
</table> </table>
<!-- Les listes --> <!-- Mailing lists -->
<?php <?php
$totallist = $quota->get_size_mailman_sum_user($c["uid"]); $totallist = $quota->get_size_mailman_sum_user($c["uid"]);
if ($totallist) { if ($totallist) {
?> // $totalweb is in KB, so we call get_size_unit() with it in Bytes
$t=$quota->get_size_unit($totallist * 1024);
<?php echo "<p>"._("Mailman lists:")." ";
echo "<p>"._("Mailman lists:")." "; echo sprintf("%.1f", $t['size'])."&nbsp;".$t['unit'];
echo sprintf("%.1f", $totallist/1024)."&nbsp;"._("MB"); echo "</p>";
echo "</p>";
?> ?>
<table class="tedit"> <table class="tedit">
@ -168,14 +174,14 @@ if (!isset($mode)) { # when included from adm_login, mode is not set
echo "<tr><td>".$d["list"]."</td><td"; echo "<tr><td>".$d["list"]."</td><td";
if ($mode!=2) echo " style=\"text-align: right\""; if ($mode!=2) echo " style=\"text-align: right\"";
echo ">"; echo ">";
$ds=$d["size"]; $ds = $quota->get_size_unit($d["size"] * 1024);
if ($totallist) { if ($totallist) {
$pc=intval(100*$ds/$totallist); $pc=intval(100*$ds['size']/$totallist);
} else { } else {
$pc=0; $pc=0;
} }
if ($mode==0) { if ($mode==0) {
echo sprintf("%.1f", $ds / 1024)."&nbsp;"._("MB"); echo sprintf("%.1f", $ds['size'])."&nbsp;".$ds['unit'];
} elseif ($mode==1) { } elseif ($mode==1) {
echo sprintf("%.1f", $pc)."&nbsp;%"; echo sprintf("%.1f", $pc)."&nbsp;%";
} else { } else {

View File

@ -261,7 +261,7 @@ class m_quota {
if(empty($type)) return false; if(empty($type)) return false;
$type=strtolower($type); $type=strtolower($type);
if (!preg_match("#^[a-z0-9]*$#",$type)) { if (!preg_match("#^[a-z0-9]*$#",$type)) {
$err->raise("quota", _("Type can only contains characters a-z and 0-9")); $err->raise("quota", "Type can only contains characters a-z and 0-9");
return false; return false;
} }
while (list($key,$val)=each($qlist)) { while (list($key,$val)=each($qlist)) {
@ -401,7 +401,6 @@ class m_quota {
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_web WHERE uid='$u';"); return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_web WHERE uid='$u';");
} }
/* sum of mailbox sizes from all domains */ /* sum of mailbox sizes from all domains */
function get_size_mail_sum_all() { function get_size_mail_sum_all() {
return $this->_get_sum_sql("SELECT SUM(bytes) AS sum FROM mailbox;"); return $this->_get_sum_sql("SELECT SUM(bytes) AS sum FROM mailbox;");
@ -409,7 +408,7 @@ class m_quota {
/* sum of mailbox sizes for one domain */ /* sum of mailbox sizes for one domain */
function get_size_mail_sum_domain($dom) { function get_size_mail_sum_domain($dom) {
return $this->_get_sum_sql("SELECT SUM(quota_dovecot) AS sum FROM dovecot_view WHERE user LIKE '%@{$dom}'"); return $this->_get_sum_sql("SELECT SUM(quota_dovecot) AS sum FROM dovecot_view WHERE user LIKE '%@{$dom}';");
} }
/* count of mailbox sizes from all domains */ /* count of mailbox sizes from all domains */
@ -424,10 +423,9 @@ class m_quota {
/* get list of mailbox alias and size for one domain */ /* get list of mailbox alias and size for one domain */
function get_size_mail_details_domain($dom) { function get_size_mail_details_domain($dom) {
return $this->_get_size_and_record_sql("SELECT user as alias,quota_dovecot as size FROM dovecot_view WHERE alias LIKE '%@{$dom}' ORDER BY alias;"); return $this->_get_size_and_record_sql("SELECT user as alias,quota_dovecot as size FROM dovecot_view WHERE user LIKE '%@{$dom}' ORDER BY alias;");
} }
/* sum of mailman lists sizes from all domains */ /* sum of mailman lists sizes from all domains */
function get_size_mailman_sum_all() { function get_size_mailman_sum_all() {
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_mailman;"); return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_mailman;");
@ -458,7 +456,6 @@ class m_quota {
return $this->_get_size_and_record_sql("SELECT list,size FROM size_mailman WHERE uid='{$u}' ORDER BY list ASC"); return $this->_get_size_and_record_sql("SELECT list,size FROM size_mailman WHERE uid='{$u}' ORDER BY list ASC");
} }
/* sum of databases sizes from all users */ /* sum of databases sizes from all users */
function get_size_db_sum_all() { function get_size_db_sum_all() {
return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_db;"); return $this->_get_sum_sql("SELECT SUM(size) AS sum FROM size_db;");
@ -484,7 +481,16 @@ class m_quota {
return $this->_get_size_and_record_sql("SELECT db,size FROM size_db WHERE db='{$u}' OR db LIKE '{$u}\_%';"); return $this->_get_size_and_record_sql("SELECT db,size FROM size_db WHERE db='{$u}' OR db LIKE '{$u}\_%';");
} }
/* Return appropriate value and unit of a size given in Bytes (e.g. 1024 Bytes -> return 1 KB) */
function get_size_unit($size) {
$units=array(1073741824=>_("GB"), 1048576=>_("MB"), 1024=>_("KB"), 0=>_("B"));
foreach($units as $value=>$unit){
if($size>=$value){
$size=str_pad(round($size/($value ? $value : 1), 1), 5, ' ', STR_PAD_LEFT);
return array('size'=>$size, 'unit'=>$unit);
}
}
}
/* ==== Hook functions ==== */ /* ==== Hook functions ==== */