tbrehm
2012-06-11 897af06af9522ded99b1e0f46730299e89856ffe
commit | author | age
313e33 1 <?php
T 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
8e9a5f 31 define('SCRIPT_PATH', dirname($_SERVER["SCRIPT_FILENAME"]));
N 32 require(SCRIPT_PATH."/lib/config.inc.php");
33 require(SCRIPT_PATH."/lib/app.inc.php");
313e33 34
T 35 set_time_limit(0);
36
37 // make sure server_id is always an int
85c4e5 38 $conf['server_id'] = intval($conf['server_id']);
313e33 39
695825 40
313e33 41 // Load required base-classes
58c210 42 $app->uses('ini_parser,file,services,getconf');
313e33 43
T 44
45 #######################################################################################################
46 // store the mailbox statistics in the database
47 #######################################################################################################
48
85c4e5 49 $sql = "SELECT mailuser_id,maildir FROM mail_user WHERE server_id = ".$conf['server_id'];
313e33 50 $records = $app->db->queryAllRecords($sql);
T 51 foreach($records as $rec) {
85c4e5 52     if(@is_file($rec['maildir'].'/ispconfig_mailsize')) {
695825 53
313e33 54         // rename file
85c4e5 55         rename($rec['maildir'].'/ispconfig_mailsize',$rec['maildir'].'/ispconfig_mailsize_save');
695825 56
313e33 57         // Read the file
85c4e5 58         $lines = file($rec['maildir'].'/ispconfig_mailsize_save');
313e33 59         $mail_traffic = 0;
T 60         foreach($lines as $line) {
61             $mail_traffic += intval($line);
62         }
63         unset($lines);
695825 64
313e33 65         // Delete backup file
85c4e5 66         if(@is_file($rec['maildir'].'/ispconfig_mailsize_save')) unlink($rec['maildir'].'/ispconfig_mailsize_save');
695825 67
313e33 68         // Save the traffic stats in the sql database
85c4e5 69         $tstamp = date('Y-m');
695825 70
85c4e5 71         $sql = "SELECT * FROM mail_traffic WHERE month = '$tstamp' AND mailuser_id = ".$rec['mailuser_id'];
a73335 72         $tr = $app->dbmaster->queryOneRecord($sql);
695825 73
85c4e5 74         $mail_traffic += $tr['traffic'];
J 75         if($tr['traffic_id'] > 0) {
76             $sql = "UPDATE mail_traffic SET traffic = $mail_traffic WHERE traffic_id = ".$tr['traffic_id'];
313e33 77         } else {
85c4e5 78             $sql = "INSERT INTO mail_traffic (month,mailuser_id,traffic) VALUES ('$tstamp',".$rec['mailuser_id'].",$mail_traffic)";
313e33 79         }
a73335 80         $app->dbmaster->query($sql);
313e33 81         echo $sql;
695825 82
313e33 83     }
695825 84
313e33 85 }
T 86
87 #######################################################################################################
88 // Create webalizer statistics
89 #######################################################################################################
90
035886 91 function setConfigVar( $filename, $varName, $varValue ) {
T 92     if($lines = @file($filename)) {
93         $out = '';
94         $found = 0;
95         foreach($lines as $line) {
1ca823 96             @list($key, $value) = preg_split('/[\t= ]+/', $line, 2);
035886 97             if($key == $varName) {
85c4e5 98                 $out .= $varName.' '.$varValue."\n";
035886 99                 $found = 1;
T 100             } else {
101                 $out .= $line;
102             }
103         }
104         if($found == 0) {
105             //* add \n if the last line does not end with \n or \r
106             if(substr($out,-1) != "\n" && substr($out,-1) != "\r") $out .= "\n";
107             //* add the new line at the end of the file
85c4e5 108             if($append == 1) $out .= $varName.' '.$varValue."\n";
035886 109         }
T 110
111         file_put_contents($filename,$out);
112     }
113 }
114
313e33 115
85c4e5 116 $sql = "SELECT domain_id, domain, document_root FROM web_domain WHERE stats_type = 'webalizer' AND server_id = ".$conf['server_id'];
313e33 117 $records = $app->db->queryAllRecords($sql);
035886 118
313e33 119 foreach($records as $rec) {
13b41c 120     //$yesterday = date('Ymd',time() - 86400);
T 121     $yesterday = date('Ymd',strtotime("-1 day", time()));
85c4e5 122     $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log');
8c034e 123     if(!@is_file($logfile)) {
85c4e5 124         $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log.gz');
58c210 125         if(!@is_file($logfile)) {
T 126             continue;
127         }
313e33 128     }
035886 129
85c4e5 130     $domain = escapeshellcmd($rec['domain']);
J 131     $statsdir = escapeshellcmd($rec['document_root'].'/web/stats');
58c210 132     $webalizer = '/usr/bin/webalizer';
T 133     $webalizer_conf_main = '/etc/webalizer/webalizer.conf';
85c4e5 134     $webalizer_conf = escapeshellcmd($rec['document_root'].'/log/webalizer.conf');
d81a4c 135     
9d0baa 136     if(is_file($statsdir.'/index.php')) unlink($statsdir.'/index.php');
035886 137
58c210 138     if(!@is_file($webalizer_conf)) {
8e725d 139         copy($webalizer_conf_main,$webalizer_conf);
58c210 140     }
035886 141
58c210 142     if(@is_file($webalizer_conf)) {
T 143         setConfigVar($webalizer_conf, 'Incremental', 'yes');
144         setConfigVar($webalizer_conf, 'IncrementalName', $statsdir.'/webalizer.current');
145         setConfigVar($webalizer_conf, 'HistoryName', $statsdir.'/webalizer.hist');
146     }
8388ae 147
T 148
58c210 149     if(!@is_dir($statsdir)) mkdir($statsdir);
8c034e 150     exec("$webalizer -c $webalizer_conf -n $domain -s $domain -r $domain -q -T -p -o $statsdir $logfile");
313e33 151 }
T 152
153 #######################################################################################################
58c210 154 // Create awstats statistics
T 155 #######################################################################################################
156
e756e7 157 $sql = "SELECT domain_id, domain, document_root, system_user, system_group FROM web_domain WHERE stats_type = 'awstats' AND server_id = ".$conf['server_id'];
58c210 158 $records = $app->db->queryAllRecords($sql);
T 159
85c4e5 160 $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
58c210 161
T 162 foreach($records as $rec) {
13b41c 163     //$yesterday = date('Ymd',time() - 86400);
T 164     $yesterday = date('Ymd',strtotime("-1 day", time()));
85c4e5 165     $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log');
58c210 166     if(!@is_file($logfile)) {
85c4e5 167         $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday.'-access.log.gz');
58c210 168         if(!@is_file($logfile)) {
T 169             continue;
170         }
171     }
172     
85c4e5 173     $domain = escapeshellcmd($rec['domain']);
J 174     $statsdir = escapeshellcmd($rec['document_root'].'/web/stats');
58c210 175     $awstats_pl = $web_config['awstats_pl'];
2a6eac 176     $awstats_buildstaticpages_pl = $web_config['awstats_buildstaticpages_pl'];
58c210 177     
fb3a98 178     $awstats_conf_dir = $web_config['awstats_conf_dir'];
T 179     $awstats_website_conf_file = $web_config['awstats_conf_dir'].'/awstats.'.$domain.'.conf';
180     
04620b 181     if(is_file($awstats_website_conf_file)) unlink($awstats_website_conf_file);
T 182     
1abf85 183     $sql = "SELECT domain FROM web_domain WHERE (type = 'alias' OR type = 'subdomain') AND parent_domain_id = ".$rec['domain_id'];
320e4e 184     $aliases = $app->db->queryAllRecords($sql);
T 185     $aliasdomain = '';
186     
187     if(is_array($aliases)) {
188         foreach ($aliases as $alias) {
189             $aliasdomain.= ' '.$alias['domain']. ' www.'.$alias['domain'];
190         }
191     }
192     
fb3a98 193     if(!is_file($awstats_website_conf_file)) {
T 194         $awstats_conf_file_content = 'Include "'.$awstats_conf_dir.'/awstats.conf"
04620b 195 LogFile="/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log"
fb3a98 196 SiteDomain="'.$domain.'"
1abf85 197 HostAliases="www.'.$domain.' localhost 127.0.0.1'.$aliasdomain.'"';
fb3a98 198         file_put_contents($awstats_website_conf_file,$awstats_conf_file_content);
T 199     }
200     
58c210 201     if(!@is_dir($statsdir)) mkdir($statsdir);
1ca823 202     if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
04620b 203     symlink($logfile,'/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
58c210 204     
ff765b 205     $awmonth = date("n");
39bc77 206     $awyear = date("Y");
ff765b 207
T 208     if (date("d") == 1) {
209         $awmonth = date("m")-1;
210         if (date("m") == 1) {
211             $awyear = date("Y")-1;
212             $awmonth = "12";
213         }
39bc77 214     }
T 215     
58c210 216     // awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/web/stats -awstatsprog=/path/to/awstats.pl
39bc77 217     // $command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
T 218     
219     $command = "$awstats_buildstaticpages_pl -month='$awmonth' -year='$awyear' -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
220
221     if (date("d") == 2) {
222         $awmonth = date("m")-1;
223         if (date("m") == 1) {
224             $awyear = date("Y")-1;
225             $awmonth = "12";
226         }
227
228         $statsdirold = $statsdir."/".$awyear."-".$awmonth."/";
229         mkdir($statsdirold);
230         $files = scandir($statsdir);
231         foreach ($files as $file) {
8506fc 232             if (substr($file,0,1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file,0,1) != "w" && substr($file,0,1) != "i") copy("$statsdir"."/"."$file","$statsdirold"."$file");
39bc77 233         }
T 234     }
235     
58c210 236     
2a6eac 237     if($awstats_pl != '' && $awstats_buildstaticpages_pl != '' && fileowner($awstats_pl) == 0 && fileowner($awstats_buildstaticpages_pl) == 0) {
58c210 238         exec($command);
39bc77 239         if(is_file($rec['document_root'].'/web/stats/index.html')) unlink($rec['document_root'].'/web/stats/index.html');
T 240         rename($rec['document_root'].'/web/stats/awstats.'.$domain.'.html',$rec['document_root'].'/web/stats/awsindex.html');
241         if(!is_file($rec['document_root']."/web/stats/index.php")) copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$rec['document_root']."/web/stats/index.php");
242         
85c4e5 243         $app->log('Created awstats statistics with command: '.$command,LOGLEVEL_DEBUG);
58c210 244     } else {
2a6eac 245         $app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.",LOGLEVEL_WARN);
58c210 246     }
T 247     
5061c0 248     if(is_file($rec['document_root']."/web/stats/index.php")) {
T 249         chown($rec['document_root']."/web/stats/index.php",$rec['system_user']);
250         chgrp($rec['document_root']."/web/stats/index.php",$rec['system_group']);
251     }
252     
58c210 253 }
T 254
255
256 #######################################################################################################
033c80 257 // Make the web logfiles directories world readable to enable ftp access
T 258 #######################################################################################################
259
260 exec('chmod +r /var/log/ispconfig/httpd/*');
261
262 #######################################################################################################
15c949 263 // Manage and compress web logfiles and create traffic statistics
313e33 264 #######################################################################################################
T 265
85c4e5 266 $sql = "SELECT domain_id, domain, document_root FROM web_domain WHERE server_id = ".$conf['server_id'];
313e33 267 $records = $app->db->queryAllRecords($sql);
T 268 foreach($records as $rec) {
15c949 269     
T 270     //* create traffic statistics based on yesterdays access log file
271     $yesterday = date('Ymd',time() - 86400);
272     $logfile = $rec['document_root'].'/log/'.$yesterday.'-access.log';
273     $total_bytes = 0;
274     
275     $handle = @fopen($logfile, "r");
276     if ($handle) {
277         while (($line = fgets($handle, 4096)) !== false) {
278             if (preg_match('/^\S+ \S+ \S+ \[.*?\] "\S+.*?" \d+ (\d+) ".*?" ".*?"/', $line, $m)) {
279                 $total_bytes += intval($m[1]);
280             }
281         }
282         
283         //* Insert / update traffic in master database
284         $traffic_date = date('Y-m-d',time() - 86400);
285         $tmp = $app->dbmaster->queryOneRecord("select hostname from web_traffic where hostname='".$rec['domain']."' and traffic_date='".$traffic_date."'");
286         if(is_array($tmp) && count($tmp) > 0) {
287             $sql = "update web_traffic set traffic_bytes=traffic_bytes+"
288                   . $total_bytes
289                   . " where hostname='" . $rec['domain']
290                   . "' and traffic_date='" . $traffic_date . "'";
291         } else {
292             $sql = "insert into web_traffic (hostname, traffic_date, traffic_bytes) values ('".$rec['domain']."', '".$traffic_date."', '".$total_bytes."')";
293         }
294         $app->dbmaster->query($sql);
295         
296         fclose($handle);
297     }
298     
299     $yesterday2 = date('Ymd',time() - 86400*2);
300     $logfile = escapeshellcmd($rec['document_root'].'/log/'.$yesterday2.'-access.log');
301     
302     //* Compress logfile
313e33 303     if(@is_file($logfile)) {
T 304         // Compress yesterdays logfile
305         exec("gzip -c $logfile > $logfile.gz");
b28db4 306         unlink($logfile);
313e33 307     }
3e0034 308     
T 309     // rotate and compress the error.log when it exceeds a size of 10 MB
85c4e5 310     $logfile = escapeshellcmd($rec['document_root'].'/log/error.log');
3e0034 311     if(is_file($logfile) && filesize($logfile) > 10000000) {
T 312         exec("gzip -c $logfile > $logfile.1.gz");
313         exec("cat /dev/null > $logfile");
314     }
695825 315
313e33 316     // delete logfiles after 30 days
85c4e5 317     $month_ago = date('Ymd',time() - 86400 * 30);
J 318     $logfile = escapeshellcmd($rec['document_root'].'/log/'.$month_ago.'-access.log.gz');
313e33 319     if(@is_file($logfile)) {
T 320         unlink($logfile);
321     }
5ef60a 322     
T 323     //* Delete older Log files, in case that we missed them before due to serverdowntimes.
324     $datepart = date('Ym',time() - 86400 * 31 * 2);
325     
326     $logfile = escapeshellcmd($rec['document_root']).'/log/'.$datepart.'*-access.log.gz';
327     exec('rm -f '.$logfile);
328     
329     $logfile = escapeshellcmd($rec['document_root']).'/log/'.$datepart.'*-access.log';
330     exec('rm -f '.$logfile);
313e33 331 }
T 332
d7f14d 333 #######################################################################################################
3e0034 334 // Rotate the ispconfig.log file
T 335 #######################################################################################################
336
337 // rotate the ispconfig.log when it exceeds a size of 10 MB
e38d14 338 $logfile = $conf['ispconfig_log_dir'].'/ispconfig.log';
3e0034 339 if(is_file($logfile) && filesize($logfile) > 10000000) {
T 340     exec("gzip -c $logfile > $logfile.1.gz");
341     exec("cat /dev/null > $logfile");
342 }
343
cafdec 344 // rotate the cron.log when it exceeds a size of 10 MB
e38d14 345 $logfile = $conf['ispconfig_log_dir'].'/cron.log';
cafdec 346 if(is_file($logfile) && filesize($logfile) > 10000000) {
T 347     exec("gzip -c $logfile > $logfile.1.gz");
348     exec("cat /dev/null > $logfile");
349 }
350
3e0034 351 #######################################################################################################
2155fb 352 // Cleanup website tmp directories
T 353 #######################################################################################################
354
85c4e5 355 $sql = "SELECT domain_id, domain, document_root, system_user FROM web_domain WHERE server_id = ".$conf['server_id'];
2155fb 356 $records = $app->db->queryAllRecords($sql);
ab6140 357 $app->uses('system');
2155fb 358 if(is_array($records)) {
T 359     foreach($records as $rec){
85c4e5 360         $tmp_path = realpath(escapeshellcmd($rec['document_root'].'/tmp'));
ab6140 361         if($tmp_path != '' && strlen($tmp_path) > 10 && is_dir($tmp_path) && $app->system->is_user($rec['system_user'])){
85c4e5 362             exec('cd '.$tmp_path."; find . -mtime +1 -name 'sess_*' | grep -v -w .no_delete | xargs rm > /dev/null 2> /dev/null");
2b849b 363         }
T 364     }
365 }
2155fb 366
T 367 #######################################################################################################
e265dc 368 // Cleanup logs in master database (only the "master-server")
d7f14d 369 #######################################################################################################
313e33 370
e265dc 371 if ($app->dbmaster == $app->db) {
V 372     /** 7 days */
373     $tstamp = time() - (60*60*24*7);
313e33 374
e265dc 375     /*
V 376      *  Keep 7 days in sys_log
377      * (we can delete the old items, because if they are OK, they don't interrest anymore
378      * if they are NOT ok, the server will try to process them in 1 minute and so the
379      * error appears again after 1 minute. So it is no problem to delete the old one!
380      */
45841f 381     $sql = "DELETE FROM sys_log WHERE tstamp < " . $tstamp . " AND server_id != 0";
V 382     $app->dbmaster->query($sql);
383
384     /*
9db145 385      * Delete all remote-actions "done" and older than 7 days
V 386      * ATTENTION: We have the same problem as described in cleaning the datalog. We must not
387      * delete the last entry
45841f 388      */
9db145 389     $sql = "SELECT max(action_id) FROM sys_remoteaction";
V 390     $res = $app->dbmaster->queryOneRecord($sql);
391     $maxId = $res['max(action_id)'];
392     $sql =  "DELETE FROM sys_remoteaction " .
393             "WHERE tstamp < " . $tstamp . " " .
9f56bd 394             " AND action_state = 'ok' " .
9db145 395             " AND action_id <" . intval($maxId);
e265dc 396     $app->dbmaster->query($sql);
V 397
398     /*
399      * The sys_datalog is more difficult.
400      * 1) We have to keet ALL entries with
401      *    server_id=0, because they depend on ALL servers (even if they are not
402      *    actually in the system (and will be insered in 3 days or so).
403      * 2) We have to keey ALL entries which are not actually precessed by the
404      *    server never mind how old they are!
0061e0 405      * 3) We have to keep the entry with the highest autoinc-id, because mysql calculates the
V 406      *    autoinc-id as "new value = max(row) +1" and does not store this in a separate table.
407      *    This means, if we delete to entry with the highest autoinc-value then this value is
408      *    reused as autoinc and so there are more than one entries with the same value (over
409      *    for example 4 Weeks). This is confusing for our system.
410      *    ATTENTION 2) and 3) is in some case NOT the same! so we have to check both!
e265dc 411      */
V 412
413     /* First we need all servers and the last sys_datalog-id they processed */
414     $sql = "SELECT server_id, updated FROM server ORDER BY server_id";
415     $records = $app->dbmaster->queryAllRecords($sql);
416
0061e0 417     /* Then we need the highest value ever */
V 418     $sql = "SELECT max(datalog_id) FROM sys_datalog";
419     $res = $app->dbmaster->queryOneRecord($sql);
420     $maxId = $res['max(datalog_id)'];
421
e265dc 422     /* Then delete server by server */
V 423     foreach($records as $server) {
942538 424         $tmp_server_id = intval($server['server_id']);
T 425         if($tmp_server_id > 0) {
0061e0 426             $sql =     "DELETE FROM sys_datalog " .
V 427                     "WHERE tstamp < " . $tstamp .
942538 428                     " AND server_id = " . intval($server['server_id']) .
0061e0 429                     " AND datalog_id < " . intval($server['updated']) .
V 430                     " AND datalog_id < " . intval($maxId);
942538 431         }
e265dc 432 //        echo $sql . "\n";
V 433         $app->dbmaster->query($sql);
434     }
435 }
313e33 436
8d0c6b 437 #######################################################################################################
942538 438 // enforce traffic quota (run only on the "master-server")
8d0c6b 439 #######################################################################################################
T 440
441 if ($app->dbmaster == $app->db) {
442
443     $current_month = date('Y-m');
695825 444
8d0c6b 445     //* Check website traffic quota
9c7f61 446     $sql = "SELECT sys_groupid,domain_id,domain,traffic_quota,traffic_quota_lock FROM web_domain WHERE (traffic_quota > 0 or traffic_quota_lock = 'y') and type = 'vhost'";
8d0c6b 447     $records = $app->db->queryAllRecords($sql);
T 448     if(is_array($records)) {
449         foreach($records as $rec) {
695825 450
8d0c6b 451             $web_traffic_quota = $rec['traffic_quota'];
b0ebbd 452             $domain = $rec['domain'];
695825 453
8d0c6b 454             // get the client
T 455             /*
456             $client_group_id = $rec["sys_groupid"];
457             $client = $app->db->queryOneRecord("SELECT limit_traffic_quota,parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
458             $reseller = $app->db->queryOneRecord("SELECT limit_traffic_quota FROM client WHERE client_id = ".intval($client['parent_client_id']));
695825 459
8d0c6b 460             $client_traffic_quota = intval($client['limit_traffic_quota']);
T 461             $reseller_traffic_quota = intval($reseller['limit_traffic_quota']);
462             */
695825 463
8d0c6b 464             //* get the traffic
695825 465             $tmp = $app->db->queryOneRecord("SELECT SUM(traffic_bytes) As total_traffic_bytes FROM web_traffic WHERE traffic_date like '$current_month%' AND hostname = '$domain'");
M 466             $web_traffic = (int)$tmp['total_traffic_bytes']/1024/1024;
467
8d0c6b 468             //* Website is over quota, we will disable it
T 469             /*if( ($web_traffic_quota > 0 && $web_traffic > $web_traffic_quota) ||
470                 ($client_traffic_quota > 0 && $web_traffic > $client_traffic_quota) ||
471                 ($reseller_traffic_quota > 0 && $web_traffic > $reseller_traffic_quota)) {*/
472             if($web_traffic_quota > 0 && $web_traffic > $web_traffic_quota) {
b0ebbd 473                 $app->dbmaster->datalogUpdate('web_domain', "traffic_quota_lock = 'y',active = 'n'", 'domain_id', $rec['domain_id']);
85c4e5 474                 $app->log('Traffic quota for '.$rec['domain_id'].' exceeded. Disabling website.',LOGLEVEL_DEBUG);
8d0c6b 475             } else {
T 476                 //* unlock the website, if traffic is lower then quota
477                 if($rec['traffic_quota_lock'] == 'y') {
b0ebbd 478                     $app->dbmaster->datalogUpdate('web_domain', "traffic_quota_lock = 'n',active = 'y'", 'domain_id', $rec['domain_id']);
85c4e5 479                     $app->log('Traffic quota for '.$rec['domain_id'].' ok again. Re-enabling website.',LOGLEVEL_DEBUG);
8d0c6b 480                 }
T 481             }
482         }
483     }
695825 484
M 485
8d0c6b 486 }
T 487
b74ef5 488
T 489 #######################################################################################################
490 // deactivate virtual servers (run only on the "master-server")
491 #######################################################################################################
492
493 if ($app->dbmaster == $app->db) {
494     $current_date = date('Y-m-d');
495
496     //* Check which virtual machines have to be deactivated
497     $sql = "SELECT * FROM openvz_vm WHERE active = 'y' AND active_until_date != '0000-00-00' AND active_until_date < '$current_date'";
498     $records = $app->db->queryAllRecords($sql);
499     if(is_array($records)) {
500         foreach($records as $rec) {
501             $app->dbmaster->datalogUpdate('openvz_vm', "active = 'n'", 'vm_id', $rec['vm_id']);
502             $app->log('Virtual machine active date expired. Disabling VM '.$rec['veid'],LOGLEVEL_DEBUG);
503         }
504     }
505
506
507 }
508
49029c 509 #######################################################################################################
T 510 // Create website backups
511 #######################################################################################################
512
85c4e5 513 $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
8e725d 514 $backup_dir = $server_config['backup_dir'];
49029c 515
T 516 if($backup_dir != '') {
517     
d12a90 518     if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
T 519         $backup_dir_permissions = 0755;
520     } else {
521         $backup_dir_permissions = 0750;
522     }
523     
49029c 524     if(!is_dir($backup_dir)) {
d12a90 525         mkdir(escapeshellcmd($backup_dir), $backup_dir_permissions, true);
T 526     } else {
527         chmod(escapeshellcmd($backup_dir), $backup_dir_permissions);
49029c 528     }
T 529     
530     $sql = "SELECT * FROM web_domain WHERE type = 'vhost'";
531     $records = $app->db->queryAllRecords($sql);
532     if(is_array($records)) {
533         foreach($records as $rec) {
534             
535             // Create a backup
60bd07 536             if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
49029c 537                 
T 538                 $web_path = $rec['document_root'];
539                 $web_user = $rec['system_user'];
540                 $web_group = $rec['system_group'];
541                 $web_id = $rec['domain_id'];
542                 $web_backup_dir = $backup_dir.'/web'.$web_id;
f5ea1b 543                 if(!is_dir($web_backup_dir)) mkdir($web_backup_dir, 0750);
T 544                 chmod($web_backup_dir, 0750); 
545                 if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
546                     chown($web_backup_dir, $rec['system_user']); 
547                     chgrp($web_backup_dir, $rec['system_group']);
548                 } else {
549                     chown($web_backup_dir, 'root');
550                     chgrp($web_backup_dir, 'root');
551                 }
8e725d 552                 exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print | zip -y '.escapeshellarg($web_backup_dir.'/web.zip').' -@');
f5ea1b 553                 chown($web_backup_dir.'/web.zip', $rec['system_user']); 
T 554                 chgrp($web_backup_dir.'/web.zip', $rec['system_group']);
555                 chmod($web_backup_dir.'/web.zip', 0750);
49029c 556                 
T 557                 // Rename or remove old backups
558                 $backup_copies = intval($rec['backup_copies']);
763507 559                 
T 560                 //* delete any older backup copies that previously existed
561                 for ($n = $backup_copies; $n <= 10; $n++) {
562                     if(is_file($web_backup_dir.'/web.'.$n.'.zip')) unlink($web_backup_dir.'/web.'.$n.'.zip');
563                 }
564                 
49029c 565                 for($n = $backup_copies - 1; $n >= 1; $n--) {
8e725d 566                     if(is_file($web_backup_dir.'/web.'.$n.'.zip')) {
J 567                         rename($web_backup_dir.'/web.'.$n.'.zip',$web_backup_dir.'/web.'.($n+1).'.zip');
49029c 568                     }
T 569                 }
570             
8e725d 571                 if(is_file($web_backup_dir.'/web.zip')) rename($web_backup_dir.'/web.zip',$web_backup_dir.'/web.1.zip');
49029c 572             
T 573                 // Create backupdir symlink
574                 if(is_link($web_path.'/backup')) unlink($web_path.'/backup');
575                 symlink($web_backup_dir,$web_path.'/backup');
f5ea1b 576                 // chmod($web_path.'/backup', 0755);
T 577                 chown($web_path.'/backup', $rec['system_user']); 
578                 chgrp($web_path.'/backup', $rec['system_group']);
49029c 579                 
T 580             }
581             
582             /* If backup_interval is set to none and we have a 
583             backup directory for the website, then remove the backups */
584             
585             if($rec['backup_interval'] == 'none') {
586                 $web_id = $rec['domain_id'];
587                 $web_user = $rec['system_user'];
588                 $web_backup_dir = realpath($backup_dir.'/web'.$web_id);
589                 if(is_dir($web_backup_dir)) {
8e725d 590                     exec('sudo -u '.escapeshellarg($web_user).' rm -f '.escapeshellarg($web_backup_dir.'/*'));
49029c 591                 }
T 592             }
593         }
594     }
595 }
596
8d0c6b 597
313e33 598 die("finished.\n");
8e725d 599 ?>