AlternC/src/fix_dovecot_quota.php

100 lines
2.7 KiB
PHP
Raw Normal View History

2014-03-17 13:45:36 +00:00
#!/usr/bin/php
2014-03-13 14:56:34 +00:00
<?php
require_once("/usr/share/alternc/panel/class/config_nochk.php");
2014-03-26 13:59:41 +00:00
/**
* @param string $msg
*/
2014-03-17 13:45:36 +00:00
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
2014-03-13 14:56:34 +00:00
$options = getopt('m:l:d:');
#parser les arguments correctement.
2014-03-17 13:45:36 +00:00
#We check that only one type of option is specified
2014-03-13 14:56:34 +00:00
$nb=count($options);
if ( $nb != 1 ){
2014-03-17 13:45:36 +00:00
usage();
exit(1);
2014-03-13 14:56:34 +00:00
}
#we check that for that type only one option is specified
2014-03-27 13:50:44 +00:00
# FIXME je doute que ca fasse un truc pertinent ce morceau
$nb2=0;
2014-03-13 14:56:34 +00:00
foreach($options as $opt => $val){
2014-03-17 13:45:36 +00:00
$nb2=count($options[$opt]);
2014-03-13 14:56:34 +00:00
}
if ( $nb2 != 1 ){
2014-03-17 13:45:36 +00:00
usage();
exit(1);
2014-03-13 14:56:34 +00:00
}
#function taking a query used to select the mailbox(es) root and updating their quotas into the mailbox table
2014-03-17 13:45:36 +00:00
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
2014-03-18 15:48:39 +00:00
$conditions ;";
2014-03-13 14:56:34 +00:00
2014-03-17 13:45:36 +00:00
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).";")){
2014-03-17 15:13:28 +00:00
echo "Fail updating quota for mailbox : $id\n";
2014-03-17 13:45:36 +00:00
}
}
2014-03-13 14:56:34 +00:00
}
#We construct a sql query to get the mailbox root based on the option.
2014-03-27 13:50:44 +00:00
// FIXME where does $opt come from ??
2014-03-13 14:56:34 +00:00
switch($opt){
2014-03-17 13:45:36 +00:00
case "m":
if (!filter_var($val,FILTER_VALIDATE_EMAIL)) {
usage("The email you entered is syntaxically incorrect");
exit(1);
}
2014-03-18 15:48:39 +00:00
$cond = "WHERE concat(address.address,'@',domaines.domaine) ='".$val."'" ;
2014-03-17 13:45:36 +00:00
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);
}
2014-03-18 15:48:39 +00:00
$cond = "join membres on domaines.compte = membres.uid WHERE membres.login = '".mysql_real_escape_string($login)."'";
2014-03-17 13:45:36 +00:00
break;
case "d":
if(checkfqdn($val) != 0){
usage("The domain you entered is syntaxically incorrect");
exit(1);
}
2014-03-18 15:48:39 +00:00
$cond = "WHERE domaines.domaine = '".mysql_real_escape_string($val)."'" ;
2014-03-17 13:45:36 +00:00
break;
default:
usage();
exit(1);
2014-03-13 14:56:34 +00:00
}
2014-03-17 13:45:36 +00:00
FixQuotaDovecot($cond);
2014-03-13 14:56:34 +00:00
exit(0);
?>