diff --git a/src/fix_dovecot_quota.php b/src/fix_dovecot_quota.php index 8562da96..39bf7b75 100755 --- a/src/fix_dovecot_quota.php +++ b/src/fix_dovecot_quota.php @@ -1,90 +1,93 @@ -#!/usr/bin/php5 -q +#!/usr/bin/php $val){ - $nb2=count($options[$opt]); + $nb2=count($options[$opt]); } if ( $nb2 != 1 ){ - echo "usage : script -[m|l|d]\n"; - exit(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($query){ - global $db; - $db2=new DB_System(); +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 + WHERE $conditions ;"; - if(!$db->query($query)){ - echo "failed"; - exit(1); - } - while ($db->next_record()) { - $dir=$db->f("dir"); - $id=$db->f("id"); - $size = exec ( "/usr/bin/du -sb $dir|cut -f1" ); - if(!$db2->query("UPDATE mailbox set bytes=$size where id=$id;")){ - echo "fail updating quota for mailbox :".$id."\n"; - } - } + 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. switch($opt){ - case "m": - if (!filter_var($val,FILTER_VALIDATE_EMAIL)) { - echo " the email you entered is syntaxically incorrect\n"; - exit(1); - } - $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 - where concat(address.address,'@',domaines.domaine) ='".$val."';"; - break; - case "l": - $login=strtolower($val); - if (!preg_match("#^[a-z0-9]+$#",$login)) { //$ - echo " the login you entered is syntaxically incorrect\n"; - exit(1); - } - $query=("select mailbox.id,concat(path, '/Maildir/') as dir from mailbox - join address on mailbox.address_id = address.id - join domaines on address.domain_id = domaines.id - join membres on domaines.compte = membres.uid where membres.login = '".$login."';"); - break; - case "d": - if(checkfqdn($val) != 0){ - echo " the domain you entered is syntaxically incorrect\n"; - exit(1); - } - $query="select mailbox.id,concat(path, '/Maildir/') as dir from mailbox - join address on mailbox.address_id = address.id - join domaines on address.domain_id = domaines.id where domaines.domaine = '".$val."' ;"; - print_r($query); - break; - default: - echo "usage : script -[m|l|d]\n"; - exit(1); + case "m": + if (!filter_var($val,FILTER_VALIDATE_EMAIL)) { + usage("The email you entered is syntaxically incorrect"); + exit(1); + } + $cond = "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 = "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 = "domaines.domaine = ".mysql_real_escape_string($val) ; + break; + default: + usage(); + exit(1); } -FixQuotaDovecot($query); +FixQuotaDovecot($cond); exit(0);