Alternc  3.2
Alternc logiel libre pour l'h├ębergement
 All Data Structures Namespaces Files Functions Variables Pages
fix_dovecot_quota.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
3 require_once("/usr/share/alternc/panel/class/config_nochk.php");
4 
5 function usage($msg=null) {
6  if ($msg) {
7  echo "Error:\n$msg";
8  }
9  echo "usage : script -[m|l|d]\n";
10 }
11 
12 #arguments can be a mailbox or a domain or a login
13 $options = getopt('m:l:d:');
14 
15 #parser les arguments correctement.
16 #We check that only one type of option is specified
17 $nb=count($options);
18 
19 if ( $nb != 1 ){
20  usage();
21  exit(1);
22 }
23 
24 #we check that for that type only one option is specified
25 foreach($options as $opt => $val){
26  $nb2=count($options[$opt]);
27 }
28 
29 if ( $nb2 != 1 ){
30  usage();
31  exit(1);
32 }
33 
34 #function taking a query used to select the mailbox(es) root and updating their quotas into the mailbox table
35 function FixQuotaDovecot($conditions){
36  global $db;
37  $db2=new DB_System();
38  $query="SELECT mailbox.id,concat(path, '/Maildir/') as dir
39  FROM
40  mailbox
41  join address on address.id = mailbox.address_id
42  join domaines on domaines.id = address.domain_id
43  $conditions ;";
44 
45  if(!$db->query($query)){
46  usage("failed"); // FIXME real error
47  exit(1);
48  }
49  while ($db->next_record()) {
50  $dir=$db->f("dir");
51  $id=$db->f("id");
52  $size = exec ( "/usr/bin/du -sb $dir|cut -f1" ); // FIXME check return value
53  if(!$db2->query("UPDATE mailbox set bytes=".intval($size)." where id=".intval($id).";")){
54  echo "Fail updating quota for mailbox : $id\n";
55  }
56  }
57 
58 }
59 
60 #We construct a sql query to get the mailbox root based on the option.
61 switch($opt){
62  case "m":
63  if (!filter_var($val,FILTER_VALIDATE_EMAIL)) {
64  usage("The email you entered is syntaxically incorrect");
65  exit(1);
66  }
67  $cond = "WHERE concat(address.address,'@',domaines.domaine) ='".$val."'" ;
68  break;
69  case "l":
70  $login=strtolower($val);
71  if (!preg_match("#^[a-z0-9]+$#",$login)) { //FIXME use an alternc function for that
72  usage("the login you entered is syntaxically incorrect");
73  exit(1);
74  }
75  $cond = "join membres on domaines.compte = membres.uid WHERE membres.login = '".mysql_real_escape_string($login)."'";
76  break;
77  case "d":
78  if(checkfqdn($val) != 0){
79  usage("The domain you entered is syntaxically incorrect");
80  exit(1);
81  }
82  $cond = "WHERE domaines.domaine = '".mysql_real_escape_string($val)."'" ;
83  break;
84  default:
85  usage();
86  exit(1);
87 }
88 
89 FixQuotaDovecot($cond);
90 
91 exit(0);
92 
93 ?>