fixing Dovecot Quotas computing + removing duplicate code

This commit is contained in:
Benjamin Sonntag 2014-09-29 17:06:16 +02:00
parent 404f37e8f5
commit b0a5e23299
2 changed files with 8 additions and 105 deletions

View File

@ -1,99 +0,0 @@
#!/usr/bin/php
<?php
require_once("/usr/share/alternc/panel/class/config_nochk.php");
/**
* @param string $msg
*/
function usage($msg=null) {
if ($msg) {
echo "Error:\n$msg";
}
echo "usage : script -[m|l|d]\n";
}
#arguments can be a mailbox or a domain or a login
$options = getopt('m:l:d:');
#parser les arguments correctement.
#We check that only one type of option is specified
$nb=count($options);
if ( $nb != 1 ){
usage();
exit(1);
}
#we check that for that type only one option is specified
# FIXME je doute que ca fasse un truc pertinent ce morceau
$nb2=0;
foreach($options as $opt => $val){
$nb2=count($options[$opt]);
}
if ( $nb2 != 1 ){
usage();
exit(1);
}
#function taking a query used to select the mailbox(es) root and updating their quotas into the mailbox table
function FixQuotaDovecot($conditions){
global $db;
$db2=new DB_System();
$query="SELECT mailbox.id,concat(path, '/Maildir/') as dir
FROM
mailbox
join address on address.id = mailbox.address_id
join domaines on domaines.id = address.domain_id
$conditions ;";
if(!$db->query($query)){
usage("failed"); // FIXME real error
exit(1);
}
while ($db->next_record()) {
$dir=$db->f("dir");
$id=$db->f("id");
$size = exec ( "/usr/bin/du -sb $dir|cut -f1" ); // FIXME check return value
if(!$db2->query("UPDATE mailbox set bytes=".intval($size)." where id=".intval($id).";")){
echo "Fail updating quota for mailbox : $id\n";
}
}
}
#We construct a sql query to get the mailbox root based on the option.
// FIXME where does $opt come from ??
switch($opt){
case "m":
if (!filter_var($val,FILTER_VALIDATE_EMAIL)) {
usage("The email you entered is syntaxically incorrect");
exit(1);
}
$cond = "WHERE concat(address.address,'@',domaines.domaine) ='".$val."'" ;
break;
case "l":
$login=strtolower($val);
if (!preg_match("#^[a-z0-9]+$#",$login)) { //FIXME use an alternc function for that
usage("the login you entered is syntaxically incorrect");
exit(1);
}
$cond = "join membres on domaines.compte = membres.uid WHERE membres.login = '".mysql_real_escape_string($login)."'";
break;
case "d":
if(checkfqdn($val) != 0){
usage("The domain you entered is syntaxically incorrect");
exit(1);
}
$cond = "WHERE domaines.domaine = '".mysql_real_escape_string($val)."'" ;
break;
default:
usage();
exit(1);
}
FixQuotaDovecot($cond);
exit(0);
?>

View File

@ -9,21 +9,21 @@ function showhelp() {
exit
}
# FIXME: storing THAT amount of data in MAILDIRS (on BIG install like Lautre.net) may crash the shell?
# Generate the $maildirs list based on the arguments
while getopts "a:m:d:c:" optname
while getopts "am:d:c:" optname
do
case "$optname" in
"a")
# All mails
# FIXME replace it by a select in da DB
maildirs=`find "$ALTERNC_MAIL/" -maxdepth 2 -mindepth 2 -type d`
maildirs=$(mysql_query "select userdb_home from dovecot_view order by 1")
;;
"m")
# An email
if [[ "$OPTARG" =~ ^[^\@]*@[^\@]*$ ]] ; then
if [[ "$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")" ]]; then
maildirs=$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG'")
maildirs=$(mysql_query "select userdb_home from dovecot_view where user = '$OPTARG' order by 1")
else
echo "Bad mail provided"
showhelp
@ -49,13 +49,13 @@ do
showhelp
fi
maildirs=$(mysql_query "select userdb_home from dovecot_view where user like '%@$OPTARG'")
maildirs=$(mysql_query "select userdb_home from dovecot_view where user like '%@$OPTARG' order by 1")
;;
"c")
# An account
if [[ "$OPTARG" =~ ^[a-z]*$ ]] ; then
if [[ "$(mysql_query "select domaine from domaines where domaine = '$1'")" ]]; then
maildirs=$(mysql_query "select userdb_home from dovecot_view where userdb_uid = $OPTARG")
maildirs=$(mysql_query "select userdb_home from dovecot_view where userdb_uid = $OPTARG order by 1")
else
echo "Bad account provided"
showhelp
@ -112,3 +112,5 @@ for i in $maildirs ; do
mysql_query "UPDATE mailbox SET messages=$mail_count WHERE path='$i' ; "
done
# may cause a problem, let's fix this here :)
mysql_query "UPDATE mailbox SET quota=0 WHERE quota IS NULL;"