Till Brehm
2015-09-18 c108c22cb139b55e3adb7e563ab640c4dc8d2305
commit | author | age
313e33 1 <?php
T 2
3 /*
5a43e7 4 Copyright (c) 2007-2012, Till Brehm, projektfarm Gmbh
313e33 5 All rights reserved.
T 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"]));
7fe908 32 require SCRIPT_PATH."/lib/config.inc.php";
MC 33 require SCRIPT_PATH."/lib/app.inc.php";
313e33 34
132081 35 $app->setCaller('cron_daily');
MC 36
313e33 37 set_time_limit(0);
7b47c0 38 ini_set('error_reporting', E_ALL & ~E_NOTICE);
313e33 39
T 40 // make sure server_id is always an int
85c4e5 41 $conf['server_id'] = intval($conf['server_id']);
313e33 42
695825 43
313e33 44 // Load required base-classes
a464e1 45 $app->uses('ini_parser,file,services,getconf,system');
313e33 46
T 47
7fe908 48 //######################################################################################################
313e33 49 // store the mailbox statistics in the database
7fe908 50 //######################################################################################################
313e33 51
bfcdef 52 $parse_mail_log = false;
85c4e5 53 $sql = "SELECT mailuser_id,maildir FROM mail_user WHERE server_id = ".$conf['server_id'];
313e33 54 $records = $app->db->queryAllRecords($sql);
bfcdef 55 if(count($records) > 0) $parse_mail_log = true;
T 56
313e33 57 foreach($records as $rec) {
85c4e5 58     if(@is_file($rec['maildir'].'/ispconfig_mailsize')) {
7fe908 59         $parse_mail_log = false;
MC 60
313e33 61         // rename file
7fe908 62         rename($rec['maildir'].'/ispconfig_mailsize', $rec['maildir'].'/ispconfig_mailsize_save');
695825 63
313e33 64         // Read the file
85c4e5 65         $lines = file($rec['maildir'].'/ispconfig_mailsize_save');
313e33 66         $mail_traffic = 0;
T 67         foreach($lines as $line) {
68             $mail_traffic += intval($line);
69         }
70         unset($lines);
695825 71
313e33 72         // Delete backup file
85c4e5 73         if(@is_file($rec['maildir'].'/ispconfig_mailsize_save')) unlink($rec['maildir'].'/ispconfig_mailsize_save');
695825 74
313e33 75         // Save the traffic stats in the sql database
85c4e5 76         $tstamp = date('Y-m');
695825 77
85c4e5 78         $sql = "SELECT * FROM mail_traffic WHERE month = '$tstamp' AND mailuser_id = ".$rec['mailuser_id'];
a73335 79         $tr = $app->dbmaster->queryOneRecord($sql);
695825 80
85c4e5 81         $mail_traffic += $tr['traffic'];
J 82         if($tr['traffic_id'] > 0) {
83             $sql = "UPDATE mail_traffic SET traffic = $mail_traffic WHERE traffic_id = ".$tr['traffic_id'];
313e33 84         } else {
85c4e5 85             $sql = "INSERT INTO mail_traffic (month,mailuser_id,traffic) VALUES ('$tstamp',".$rec['mailuser_id'].",$mail_traffic)";
313e33 86         }
a73335 87         $app->dbmaster->query($sql);
bfcdef 88         //echo $sql;
695825 89
313e33 90     }
695825 91
bfcdef 92 }
T 93
94 if($parse_mail_log == true) {
7fe908 95     $mailbox_traffic = array();
MC 96     $mail_boxes = array();
97     $mail_rewrites = array(); // we need to read all mail aliases and forwards because the address in amavis is not always the mailbox address
bfcdef 98
7fe908 99     function parse_mail_log_line($line) {
MC 100         //Oct 31 17:35:48 mx01 amavis[32014]: (32014-05) Passed CLEAN, [IPv6:xxxxx] [IPv6:xxxxx] <xxx@yyyy> -> <aaaa@bbbb>, Message-ID: <xxxx@yyyyy>, mail_id: xxxxxx, Hits: -1.89, size: 1591, queued_as: xxxxxxx, 946 ms
bfcdef 101
7fe908 102         if(preg_match('/^(\w+\s+\d+\s+\d+:\d+:\d+)\s+[^ ]+\s+amavis.* <([^>]+)>\s+->\s+((<[^>]+>,)+) .*Message-ID:\s+<([^>]+)>.* size:\s+(\d+),.*$/', $line, $matches) == false) return false;
bfcdef 103
7fe908 104         $timestamp = strtotime($matches[1]);
MC 105         if(!$timestamp) return false;
106
107         $to = array();
108         $recipients = explode(',', $matches[3]);
109         foreach($recipients as $recipient) {
110             $recipient = substr($recipient, 1, -1);
111             if(!$recipient || $recipient == $matches[2]) continue;
112             $to[] = $recipient;
113         }
114
115         return array('line' => $line, 'timestamp' => $timestamp, 'size' => $matches[6], 'from' => $matches[2], 'to' => $to, 'message-id' => $matches[5]);
116     }
117
118     function add_mailbox_traffic(&$traffic_array, $address, $traffic) {
119         global $mail_boxes, $mail_rewrites;
120
121         $address = strtolower($address);
122
123         if(in_array($address, $mail_boxes) == true) {
124             if(!isset($traffic_array[$address])) $traffic_array[$address] = 0;
125             $traffic_array[$address] += $traffic;
126         } elseif(array_key_exists($address, $mail_rewrites)) {
127             foreach($mail_rewrites[$address] as $address) {
128                 if(!isset($traffic_array[$address])) $traffic_array[$address] = 0;
129                 $traffic_array[$address] += $traffic;
130             }
131         } else {
132             // this is not a local address - skip it
133         }
134     }
135
136     $sql = "SELECT email FROM mail_user WHERE server_id = ".$conf['server_id'];
137     $records = $app->db->queryAllRecords($sql);
138     foreach($records as $record) {
139         $mail_boxes[] = $record['email'];
140     }
141     $sql = "SELECT source, destination FROM mail_forwarding WHERE server_id = ".$conf['server_id'];
142     $records = $app->db->queryAllRecords($sql);
143     foreach($records as $record) {
144         $targets = preg_split('/[\n,]+/', $record['destination']);
145         foreach($targets as $target) {
146             if(in_array($target, $mail_boxes)) {
147                 if(isset($mail_rewrites[$record['source']])) $mail_rewrites[$record['source']][] = $target;
148                 else $mail_rewrites[$record['source']] = array($target);
149             }
150         }
151     }
152
153     $state_file = dirname(__FILE__) . '/mail_log_parser.state';
154     $prev_line = false;
155     $last_line = false;
156     $cur_line = false;
157
158     if(file_exists($state_file)) {
159         $prev_line = parse_mail_log_line(trim(file_get_contents($state_file)));
160         //if($prev_line) echo "continuing from previous run, log position: " . $prev_line['message-id'] . " at " . strftime('%d.%m.%Y %H:%M:%S', $prev_line['timestamp']) . "\n";
161     }
162
163     if(file_exists('/var/log/mail.log')) {
164         $fp = fopen('/var/log/mail.log', 'r');
165         //echo "Parsing mail.log...\n";
166         $l = 0;
167         while($line = fgets($fp, 8192)) {
168             $l++;
169             //if($l % 1000 == 0) echo "\rline $l";
170             $cur_line = parse_mail_log_line($line);
171             if(!$cur_line) continue;
172
173             if($prev_line) {
174                 // check if this line has to be processed
175                 if($cur_line['timestamp'] < $prev_line['timestamp']) {
176                     $parse_mail_log = false; // we do not need to parse the second file!
177                     continue; // already processed
178                 } elseif($cur_line['timestamp'] == $prev_line['timestamp'] && $cur_line['message-id'] == $prev_line['message-id']) {
179                     $parse_mail_log = false; // we do not need to parse the second file!
180                     $prev_line = false; // this line has already been processed but the next one has to be!
181                     continue;
182                 }
183             }
184
185             add_mailbox_traffic($mailbox_traffic, $cur_line['from'], $cur_line['size']);
186             foreach($cur_line['to'] as $to) {
187                 add_mailbox_traffic($mailbox_traffic, $to, $cur_line['size']);
188             }
189             $last_line = $line; // store for the state file
190         }
191         fclose($fp);
192         //echo "\n";
193     }
194
195     if($parse_mail_log == true && file_exists('/var/log/mail.log.1')) {
196         $fp = fopen('/var/log/mail.log.1', 'r');
197         //echo "Parsing mail.log.1...\n";
198         $l = 0;
199         while($line = fgets($fp, 8192)) {
200             $l++;
201             //if($l % 1000 == 0) echo "\rline $l";
202             $cur_line = parse_mail_log_line($line);
203             if(!$cur_line) continue;
204
205             if($prev_line) {
206                 // check if this line has to be processed
207                 if($cur_line['timestamp'] < $prev_line['timestamp']) continue; // already processed
208                 if($cur_line['timestamp'] == $prev_line['timestamp'] && $cur_line['message-id'] == $prev_line['message-id']) {
209                     $prev_line = false; // this line has already been processed but the next one has to be!
210                     continue;
211                 }
212             }
213
214             add_mailbox_traffic($mailbox_traffic, $cur_line['from'], $cur_line['size']);
215             foreach($cur_line['to'] as $to) {
216                 add_mailbox_traffic($mailbox_traffic, $to, $cur_line['size']);
217             }
218         }
219         fclose($fp);
220         //echo "\n";
221     }
222     unset($mail_rewrites);
223     unset($mail_boxes);
224
225     // Save the traffic stats in the sql database
226     $tstamp = date('Y-m');
227     $sql = "SELECT mailuser_id,email FROM mail_user WHERE server_id = ".$conf['server_id'];
228     $records = $app->db->queryAllRecords($sql);
229     foreach($records as $rec) {
230         if(array_key_exists($rec['email'], $mailbox_traffic)) {
231             $sql = "SELECT * FROM mail_traffic WHERE month = '$tstamp' AND mailuser_id = ".$rec['mailuser_id'];
232             $tr = $app->dbmaster->queryOneRecord($sql);
233
234             $mail_traffic = $tr['traffic'] + $mailbox_traffic[$rec['email']];
235             if($tr['traffic_id'] > 0) {
236                 $sql = "UPDATE mail_traffic SET traffic = $mail_traffic WHERE traffic_id = ".$tr['traffic_id'];
237             } else {
238                 $sql = "INSERT INTO mail_traffic (month,mailuser_id,traffic) VALUES ('$tstamp',".$rec['mailuser_id'].",$mail_traffic)";
239             }
240             $app->dbmaster->query($sql);
241             //echo $sql;
242         }
243     }
244
245     unset($mailbox_traffic);
246     if($last_line) file_put_contents($state_file, $last_line);
313e33 247 }
T 248
7fe908 249 //######################################################################################################
313e33 250 // Create webalizer statistics
7fe908 251 //######################################################################################################
313e33 252
615a0a 253 function setConfigVar( $filename, $varName, $varValue, $append = 0 ) {
035886 254     if($lines = @file($filename)) {
T 255         $out = '';
256         $found = 0;
257         foreach($lines as $line) {
1ca823 258             @list($key, $value) = preg_split('/[\t= ]+/', $line, 2);
035886 259             if($key == $varName) {
85c4e5 260                 $out .= $varName.' '.$varValue."\n";
035886 261                 $found = 1;
T 262             } else {
263                 $out .= $line;
264             }
265         }
266         if($found == 0) {
267             //* add \n if the last line does not end with \n or \r
7fe908 268             if(substr($out, -1) != "\n" && substr($out, -1) != "\r") $out .= "\n";
035886 269             //* add the new line at the end of the file
85c4e5 270             if($append == 1) $out .= $varName.' '.$varValue."\n";
035886 271         }
T 272
7fe908 273         file_put_contents($filename, $out);
035886 274     }
T 275 }
276
313e33 277
3f478f 278 $sql = "SELECT domain_id, domain, document_root, web_folder, type, parent_domain_id FROM web_domain WHERE (type = 'vhost' or type = 'vhostsubdomain') and stats_type = 'webalizer' AND server_id = ".$conf['server_id'];
313e33 279 $records = $app->db->queryAllRecords($sql);
035886 280
313e33 281 foreach($records as $rec) {
13b41c 282     //$yesterday = date('Ymd',time() - 86400);
7fe908 283     $yesterday = date('Ymd', strtotime("-1 day", time()));
MC 284
285     $log_folder = 'log';
286     if($rec['type'] == 'vhostsubdomain') {
287         $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = '.intval($rec['parent_domain_id']));
288         $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
289         if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
290         $log_folder .= '/' . $subdomain_host;
291         unset($tmp);
292     }
293     $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log');
8c034e 294     if(!@is_file($logfile)) {
3f478f 295         $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz');
58c210 296         if(!@is_file($logfile)) {
T 297             continue;
298         }
313e33 299     }
035886 300
85c4e5 301     $domain = escapeshellcmd($rec['domain']);
5d3852 302     $statsdir = escapeshellcmd($rec['document_root'].'/'.($rec['type'] == 'vhostsubdomain' ? $rec['web_folder'] : 'web').'/stats');
58c210 303     $webalizer = '/usr/bin/webalizer';
T 304     $webalizer_conf_main = '/etc/webalizer/webalizer.conf';
85c4e5 305     $webalizer_conf = escapeshellcmd($rec['document_root'].'/log/webalizer.conf');
a8ccf6 306
8ab3cd 307     if(is_file($statsdir.'/index.php')) unlink($statsdir.'/index.php');
035886 308
58c210 309     if(!@is_file($webalizer_conf)) {
7fe908 310         copy($webalizer_conf_main, $webalizer_conf);
58c210 311     }
035886 312
58c210 313     if(@is_file($webalizer_conf)) {
T 314         setConfigVar($webalizer_conf, 'Incremental', 'yes');
315         setConfigVar($webalizer_conf, 'IncrementalName', $statsdir.'/webalizer.current');
316         setConfigVar($webalizer_conf, 'HistoryName', $statsdir.'/webalizer.hist');
317     }
8388ae 318
T 319
58c210 320     if(!@is_dir($statsdir)) mkdir($statsdir);
8c034e 321     exec("$webalizer -c $webalizer_conf -n $domain -s $domain -r $domain -q -T -p -o $statsdir $logfile");
313e33 322 }
T 323
7fe908 324 //######################################################################################################
58c210 325 // Create awstats statistics
7fe908 326 //######################################################################################################
58c210 327
3f478f 328 $sql = "SELECT domain_id, domain, document_root, web_folder, type, system_user, system_group, parent_domain_id FROM web_domain WHERE (type = 'vhost' or type = 'vhostsubdomain') and stats_type = 'awstats' AND server_id = ".$conf['server_id'];
58c210 329 $records = $app->db->queryAllRecords($sql);
T 330
85c4e5 331 $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
58c210 332
T 333 foreach($records as $rec) {
13b41c 334     //$yesterday = date('Ymd',time() - 86400);
7fe908 335     $yesterday = date('Ymd', strtotime("-1 day", time()));
MC 336
337     $log_folder = 'log';
338     if($rec['type'] == 'vhostsubdomain') {
339         $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = '.intval($rec['parent_domain_id']));
340         $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
341         if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
342         $log_folder .= '/' . $subdomain_host;
343         unset($tmp);
344     }
345     $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log');
58c210 346     if(!@is_file($logfile)) {
3f478f 347         $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz');
58c210 348         if(!@is_file($logfile)) {
T 349             continue;
350         }
351     }
7fe908 352     $web_folder = ($rec['type'] == 'vhostsubdomain' ? $rec['web_folder'] : 'web');
85c4e5 353     $domain = escapeshellcmd($rec['domain']);
5d3852 354     $statsdir = escapeshellcmd($rec['document_root'].'/'.$web_folder.'/stats');
58c210 355     $awstats_pl = $web_config['awstats_pl'];
2a6eac 356     $awstats_buildstaticpages_pl = $web_config['awstats_buildstaticpages_pl'];
a8ccf6 357
fb3a98 358     $awstats_conf_dir = $web_config['awstats_conf_dir'];
T 359     $awstats_website_conf_file = $web_config['awstats_conf_dir'].'/awstats.'.$domain.'.conf';
a8ccf6 360
04620b 361     if(is_file($awstats_website_conf_file)) unlink($awstats_website_conf_file);
a8ccf6 362
b67344 363     $sql = "SELECT domain FROM web_domain WHERE (type = 'alias' OR type = 'subdomain') AND parent_domain_id = ".$rec['domain_id'];
320e4e 364     $aliases = $app->db->queryAllRecords($sql);
T 365     $aliasdomain = '';
a8ccf6 366
320e4e 367     if(is_array($aliases)) {
T 368         foreach ($aliases as $alias) {
369             $aliasdomain.= ' '.$alias['domain']. ' www.'.$alias['domain'];
370         }
371     }
a8ccf6 372
fb3a98 373     if(!is_file($awstats_website_conf_file)) {
T 374         $awstats_conf_file_content = 'Include "'.$awstats_conf_dir.'/awstats.conf"
04620b 375 LogFile="/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log"
fb3a98 376 SiteDomain="'.$domain.'"
b67344 377 HostAliases="www.'.$domain.' localhost 127.0.0.1'.$aliasdomain.'"';
7fe908 378         file_put_contents($awstats_website_conf_file, $awstats_conf_file_content);
fb3a98 379     }
a8ccf6 380
58c210 381     if(!@is_dir($statsdir)) mkdir($statsdir);
1ca823 382     if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
7fe908 383     symlink($logfile, '/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
a8ccf6 384
ff765b 385     $awmonth = date("n");
39bc77 386     $awyear = date("Y");
ff765b 387
T 388     if (date("d") == 1) {
389         $awmonth = date("m")-1;
390         if (date("m") == 1) {
391             $awyear = date("Y")-1;
392             $awmonth = "12";
393         }
39bc77 394     }
a8ccf6 395
5d3852 396     // awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/'.$web_folder.'/stats -awstatsprog=/path/to/awstats.pl
39bc77 397     // $command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
a8ccf6 398
39bc77 399     $command = "$awstats_buildstaticpages_pl -month='$awmonth' -year='$awyear' -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
T 400
401     if (date("d") == 2) {
402         $awmonth = date("m")-1;
403         if (date("m") == 1) {
404             $awyear = date("Y")-1;
405             $awmonth = "12";
406         }
407
408         $statsdirold = $statsdir."/".$awyear."-".$awmonth."/";
409         mkdir($statsdirold);
410         $files = scandir($statsdir);
411         foreach ($files as $file) {
7fe908 412             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 413         }
T 414     }
a8ccf6 415
M 416
2a6eac 417     if($awstats_pl != '' && $awstats_buildstaticpages_pl != '' && fileowner($awstats_pl) == 0 && fileowner($awstats_buildstaticpages_pl) == 0) {
58c210 418         exec($command);
5d3852 419         if(is_file($rec['document_root'].'/'.$web_folder.'/stats/index.html')) unlink($rec['document_root'].'/'.$web_folder.'/stats/index.html');
7fe908 420         rename($rec['document_root'].'/'.$web_folder.'/stats/awstats.'.$domain.'.html', $rec['document_root'].'/'.$web_folder.'/stats/awsindex.html');
a464e1 421         if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
T 422             if(file_exists("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master")) {
7fe908 423                 copy("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
a464e1 424             } else {
7fe908 425                 copy("/usr/local/ispconfig/server/conf/awstats_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
a464e1 426             }
T 427         }
a8ccf6 428
7fe908 429         $app->log('Created awstats statistics with command: '.$command, LOGLEVEL_DEBUG);
58c210 430     } else {
7fe908 431         $app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.", LOGLEVEL_WARN);
8cf78b 432     }
a8ccf6 433
5d3852 434     if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
7fe908 435         chown($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_user']);
MC 436         chgrp($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_group']);
58c210 437     }
a8ccf6 438
58c210 439 }
T 440
441
7fe908 442 //######################################################################################################
033c80 443 // Make the web logfiles directories world readable to enable ftp access
7fe908 444 //######################################################################################################
033c80 445
dae3b4 446 if(is_dir('/var/log/ispconfig/httpd')) exec('chmod +r /var/log/ispconfig/httpd/*');
033c80 447
7fe908 448 //######################################################################################################
26c0fc 449 // Manage and compress web logfiles and create traffic statistics
7fe908 450 //######################################################################################################
313e33 451
3f478f 452 $sql = "SELECT domain_id, domain, type, document_root, web_folder, parent_domain_id FROM web_domain WHERE (type = 'vhost' or type = 'vhostsubdomain') AND server_id = ".$conf['server_id'];
313e33 453 $records = $app->db->queryAllRecords($sql);
T 454 foreach($records as $rec) {
a8ccf6 455
26c0fc 456     //* create traffic statistics based on yesterdays access log file
7fe908 457     $yesterday = date('Ymd', time() - 86400);
MC 458
459     $log_folder = 'log';
460     if($rec['type'] == 'vhostsubdomain') {
461         $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = '.intval($rec['parent_domain_id']));
462         $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
463         if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
464         $log_folder .= '/' . $subdomain_host;
465         unset($tmp);
466     }
467
468     $logfile = $rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log';
26c0fc 469     $total_bytes = 0;
a8ccf6 470
26c0fc 471     $handle = @fopen($logfile, "r");
T 472     if ($handle) {
473         while (($line = fgets($handle, 4096)) !== false) {
474             if (preg_match('/^\S+ \S+ \S+ \[.*?\] "\S+.*?" \d+ (\d+) ".*?" ".*?"/', $line, $m)) {
475                 $total_bytes += intval($m[1]);
476             }
477         }
a8ccf6 478
26c0fc 479         //* Insert / update traffic in master database
7fe908 480         $traffic_date = date('Y-m-d', time() - 86400);
26c0fc 481         $tmp = $app->dbmaster->queryOneRecord("select hostname from web_traffic where hostname='".$rec['domain']."' and traffic_date='".$traffic_date."'");
T 482         if(is_array($tmp) && count($tmp) > 0) {
483             $sql = "update web_traffic set traffic_bytes=traffic_bytes+"
7fe908 484                 . $total_bytes
MC 485                 . " where hostname='" . $rec['domain']
486                 . "' and traffic_date='" . $traffic_date . "'";
26c0fc 487         } else {
T 488             $sql = "insert into web_traffic (hostname, traffic_date, traffic_bytes) values ('".$rec['domain']."', '".$traffic_date."', '".$total_bytes."')";
489         }
490         $app->dbmaster->query($sql);
a8ccf6 491
26c0fc 492         fclose($handle);
T 493     }
a8ccf6 494
7fe908 495     $yesterday2 = date('Ymd', time() - 86400*2);
3f478f 496     $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday2.'-access.log');
a8ccf6 497
26c0fc 498     //* Compress logfile
313e33 499     if(@is_file($logfile)) {
T 500         // Compress yesterdays logfile
501         exec("gzip -c $logfile > $logfile.gz");
b28db4 502         unlink($logfile);
313e33 503     }
a8ccf6 504
3e0034 505     // rotate and compress the error.log when it exceeds a size of 10 MB
3f478f 506     $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/error.log');
3e0034 507     if(is_file($logfile) && filesize($logfile) > 10000000) {
T 508         exec("gzip -c $logfile > $logfile.1.gz");
509         exec("cat /dev/null > $logfile");
510     }
695825 511
313e33 512     // delete logfiles after 30 days
7fe908 513     $month_ago = date('Ymd', time() - 86400 * 30);
3f478f 514     $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$month_ago.'-access.log.gz');
313e33 515     if(@is_file($logfile)) {
T 516         unlink($logfile);
517     }
a8ccf6 518
5ef60a 519     //* Delete older Log files, in case that we missed them before due to serverdowntimes.
7fe908 520     $datepart = date('Ym', time() - 86400 * 31 * 2);
a8ccf6 521
3f478f 522     $logfile = escapeshellcmd($rec['document_root']).'/' . $log_folder . '/'.$datepart.'*-access.log.gz';
5ef60a 523     exec('rm -f '.$logfile);
a8ccf6 524
3f478f 525     $logfile = escapeshellcmd($rec['document_root']).'/' . $log_folder . '/'.$datepart.'*-access.log';
5ef60a 526     exec('rm -f '.$logfile);
313e33 527 }
T 528
72695f 529 //* Delete old logfiles in /var/log/ispconfig/httpd/ that were created by vlogger for the hostname of the server
T 530 exec('hostname -f', $tmp_hostname);
531 if($tmp_hostname[0] != '' && is_dir('/var/log/ispconfig/httpd/'.$tmp_hostname[0])) {
532     exec('cd /var/log/ispconfig/httpd/'.$tmp_hostname[0]."; find . -mtime +30 -name '*.log' | xargs rm > /dev/null 2> /dev/null");
533 }
534 unset($tmp_hostname);
535
7fe908 536 //######################################################################################################
3e0034 537 // Rotate the ispconfig.log file
7fe908 538 //######################################################################################################
3e0034 539
T 540 // rotate the ispconfig.log when it exceeds a size of 10 MB
e38d14 541 $logfile = $conf['ispconfig_log_dir'].'/ispconfig.log';
3e0034 542 if(is_file($logfile) && filesize($logfile) > 10000000) {
T 543     exec("gzip -c $logfile > $logfile.1.gz");
544     exec("cat /dev/null > $logfile");
545 }
546
cafdec 547 // rotate the cron.log when it exceeds a size of 10 MB
e38d14 548 $logfile = $conf['ispconfig_log_dir'].'/cron.log';
a8ccf6 549 if(is_file($logfile) && filesize($logfile) > 10000000) {
M 550     exec("gzip -c $logfile > $logfile.1.gz");
551     exec("cat /dev/null > $logfile");
552 }
553
554 // rotate the auth.log when it exceeds a size of 10 MB
555 $logfile = $conf['ispconfig_log_dir'].'/auth.log';
cafdec 556 if(is_file($logfile) && filesize($logfile) > 10000000) {
T 557     exec("gzip -c $logfile > $logfile.1.gz");
558     exec("cat /dev/null > $logfile");
559 }
560
7fe908 561 //######################################################################################################
2155fb 562 // Cleanup website tmp directories
7fe908 563 //######################################################################################################
2155fb 564
85c4e5 565 $sql = "SELECT domain_id, domain, document_root, system_user FROM web_domain WHERE server_id = ".$conf['server_id'];
2155fb 566 $records = $app->db->queryAllRecords($sql);
T 567 if(is_array($records)) {
568     foreach($records as $rec){
85c4e5 569         $tmp_path = realpath(escapeshellcmd($rec['document_root'].'/tmp'));
ab6140 570         if($tmp_path != '' && strlen($tmp_path) > 10 && is_dir($tmp_path) && $app->system->is_user($rec['system_user'])){
85c4e5 571             exec('cd '.$tmp_path."; find . -mtime +1 -name 'sess_*' | grep -v -w .no_delete | xargs rm > /dev/null 2> /dev/null");
2b849b 572         }
T 573     }
574 }
2155fb 575
7fe908 576 //######################################################################################################
e265dc 577 // Cleanup logs in master database (only the "master-server")
7fe908 578 //######################################################################################################
313e33 579
e265dc 580 if ($app->dbmaster == $app->db) {
V 581     /** 7 days */
7fe908 582
MC 583
e265dc 584     $tstamp = time() - (60*60*24*7);
313e33 585
e265dc 586     /*
V 587      *  Keep 7 days in sys_log
588      * (we can delete the old items, because if they are OK, they don't interrest anymore
589      * if they are NOT ok, the server will try to process them in 1 minute and so the
590      * error appears again after 1 minute. So it is no problem to delete the old one!
591      */
45841f 592     $sql = "DELETE FROM sys_log WHERE tstamp < " . $tstamp . " AND server_id != 0";
V 593     $app->dbmaster->query($sql);
594
595     /*
9db145 596      * Delete all remote-actions "done" and older than 7 days
V 597      * ATTENTION: We have the same problem as described in cleaning the datalog. We must not
598      * delete the last entry
45841f 599      */
9db145 600     $sql = "SELECT max(action_id) FROM sys_remoteaction";
V 601     $res = $app->dbmaster->queryOneRecord($sql);
602     $maxId = $res['max(action_id)'];
603     $sql =  "DELETE FROM sys_remoteaction " .
7fe908 604         "WHERE tstamp < " . $tstamp . " " .
MC 605         " AND action_state = 'ok' " .
606         " AND action_id <" . intval($maxId);
e265dc 607     $app->dbmaster->query($sql);
V 608
609     /*
610      * The sys_datalog is more difficult.
611      * 1) We have to keet ALL entries with
612      *    server_id=0, because they depend on ALL servers (even if they are not
613      *    actually in the system (and will be insered in 3 days or so).
614      * 2) We have to keey ALL entries which are not actually precessed by the
615      *    server never mind how old they are!
0061e0 616      * 3) We have to keep the entry with the highest autoinc-id, because mysql calculates the
V 617      *    autoinc-id as "new value = max(row) +1" and does not store this in a separate table.
618      *    This means, if we delete to entry with the highest autoinc-value then this value is
619      *    reused as autoinc and so there are more than one entries with the same value (over
620      *    for example 4 Weeks). This is confusing for our system.
621      *    ATTENTION 2) and 3) is in some case NOT the same! so we have to check both!
e265dc 622      */
V 623
624     /* First we need all servers and the last sys_datalog-id they processed */
625     $sql = "SELECT server_id, updated FROM server ORDER BY server_id";
626     $records = $app->dbmaster->queryAllRecords($sql);
627
0061e0 628     /* Then we need the highest value ever */
V 629     $sql = "SELECT max(datalog_id) FROM sys_datalog";
630     $res = $app->dbmaster->queryOneRecord($sql);
631     $maxId = $res['max(datalog_id)'];
632
e265dc 633     /* Then delete server by server */
V 634     foreach($records as $server) {
942538 635         $tmp_server_id = intval($server['server_id']);
T 636         if($tmp_server_id > 0) {
7fe908 637             $sql =  "DELETE FROM sys_datalog " .
MC 638                 "WHERE tstamp < " . $tstamp .
639                 " AND server_id = " . intval($server['server_id']) .
640                 " AND datalog_id < " . intval($server['updated']) .
641                 " AND datalog_id < " . intval($maxId);
942538 642         }
7fe908 643         //  echo $sql . "\n";
e265dc 644         $app->dbmaster->query($sql);
V 645     }
646 }
313e33 647
7fe908 648 //########
615a0a 649 // function for sending notification emails
7fe908 650 //########
615a0a 651 function send_notification_email($template, $placeholders, $recipients) {
d67883 652     global $conf, $app;
615a0a 653
7fe908 654     if(!is_array($recipients) || count($recipients) < 1) return false;
MC 655     if(!is_array($placeholders)) $placeholders = array();
656
657     if(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt')) {
658         $lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_'.$conf['language'].'.txt');
659     } elseif(file_exists($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt')) {
660         $lines = file($conf['rootpath'].'/conf-custom/mail/' . $template . '_en.txt');
661     } elseif(file_exists($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt')) {
662         $lines = file($conf['rootpath'].'/conf/mail/' . $template . '_'.$conf['language'].'.txt');
663     } else {
664         $lines = file($conf['rootpath'].'/conf/mail/' . $template . '_en.txt');
665     }
666
667     //* get mail headers, subject and body
668     $mailHeaders = '';
669     $mailBody = '';
670     $mailSubject = '';
671     $inHeader = true;
672     for($l = 0; $l < count($lines); $l++) {
2110eb 673         if(trim($lines[$l]) == '') {
7fe908 674             $inHeader = false;
MC 675             continue;
676         }
677         if($inHeader == true) {
678             $parts = explode(':', $lines[$l], 2);
679             if(strtolower($parts[0]) == 'subject') $mailSubject = trim($parts[1]);
680             unset($parts);
681             $mailHeaders .= trim($lines[$l]) . "\n";
682         } else {
683             $mailBody .= trim($lines[$l]) . "\n";
684         }
685     }
686     $mailBody = trim($mailBody);
687
688     //* Replace placeholders
689     $mailHeaders = strtr($mailHeaders, $placeholders);
690     $mailSubject = strtr($mailSubject, $placeholders);
691     $mailBody = strtr($mailBody, $placeholders);
692
693     for($r = 0; $r < count($recipients); $r++) {
694         mail($recipients[$r], $mailSubject, $mailBody, $mailHeaders);
695     }
696
697     unset($mailSubject);
698     unset($mailHeaders);
699     unset($mailBody);
700     unset($lines);
701
702     return true;
615a0a 703 }
T 704
705
7fe908 706 //######################################################################################################
942538 707 // enforce traffic quota (run only on the "master-server")
7fe908 708 //######################################################################################################
8d0c6b 709
T 710 if ($app->dbmaster == $app->db) {
711
df76de 712     $global_config = $app->getconf->get_global_config('mail');
7fe908 713
8d0c6b 714     $current_month = date('Y-m');
695825 715
8d0c6b 716     //* Check website traffic quota
5d11e1 717     $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' OR type = 'vhostsubdomain')";
8d0c6b 718     $records = $app->db->queryAllRecords($sql);
T 719     if(is_array($records)) {
720         foreach($records as $rec) {
695825 721
8d0c6b 722             $web_traffic_quota = $rec['traffic_quota'];
b0ebbd 723             $domain = $rec['domain'];
695825 724
8d0c6b 725             // get the client
T 726             /*
727             $client_group_id = $rec["sys_groupid"];
728             $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");
729             $reseller = $app->db->queryOneRecord("SELECT limit_traffic_quota FROM client WHERE client_id = ".intval($client['parent_client_id']));
695825 730
8d0c6b 731             $client_traffic_quota = intval($client['limit_traffic_quota']);
T 732             $reseller_traffic_quota = intval($reseller['limit_traffic_quota']);
733             */
695825 734
8d0c6b 735             //* get the traffic
695825 736             $tmp = $app->db->queryOneRecord("SELECT SUM(traffic_bytes) As total_traffic_bytes FROM web_traffic WHERE traffic_date like '$current_month%' AND hostname = '$domain'");
bfcdef 737             $web_traffic = round($tmp['total_traffic_bytes']/1024/1024);
695825 738
8d0c6b 739             //* Website is over quota, we will disable it
T 740             /*if( ($web_traffic_quota > 0 && $web_traffic > $web_traffic_quota) ||
741                 ($client_traffic_quota > 0 && $web_traffic > $client_traffic_quota) ||
742                 ($reseller_traffic_quota > 0 && $web_traffic > $reseller_traffic_quota)) {*/
743             if($web_traffic_quota > 0 && $web_traffic > $web_traffic_quota) {
b0ebbd 744                 $app->dbmaster->datalogUpdate('web_domain', "traffic_quota_lock = 'y',active = 'n'", 'domain_id', $rec['domain_id']);
7fe908 745                 $app->log('Traffic quota for '.$rec['domain'].' exceeded. Disabling website.', LOGLEVEL_DEBUG);
MC 746
df76de 747                 //* Send traffic notifications
615a0a 748                 if($rec['traffic_quota_lock'] != 'y' && ($web_config['overtraffic_notify_admin'] == 'y' || $web_config['overtraffic_notify_client'] == 'y')) {
7fe908 749
MC 750                     $placeholders = array('{domain}' => $rec['domain'],
751                         '{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'));
752
615a0a 753                     $recipients = array();
7fe908 754                     //* send email to admin
df76de 755                     if($global_config['admin_mail'] != '' && $web_config['overtraffic_notify_admin'] == 'y') {
615a0a 756                         $recipients[] = $global_config['admin_mail'];
df76de 757                     }
7fe908 758
df76de 759                     //* Send email to client
10b4c8 760                     if($web_config['overtraffic_notify_client'] == 'y') {
df76de 761                         $client_group_id = $rec["sys_groupid"];
T 762                         $client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
763                         if($client['email'] != '') {
615a0a 764                             $recipients[] = $client['email'];
df76de 765                         }
T 766                     }
7fe908 767
MC 768                     send_notification_email('web_traffic_notification', $placeholders, $recipients);
df76de 769                 }
7fe908 770
MC 771
8d0c6b 772             } else {
T 773                 //* unlock the website, if traffic is lower then quota
774                 if($rec['traffic_quota_lock'] == 'y') {
b0ebbd 775                     $app->dbmaster->datalogUpdate('web_domain', "traffic_quota_lock = 'n',active = 'y'", 'domain_id', $rec['domain_id']);
7fe908 776                     $app->log('Traffic quota for '.$rec['domain'].' ok again. Re-enabling website.', LOGLEVEL_DEBUG);
8d0c6b 777                 }
T 778             }
779         }
780     }
695825 781
M 782
615a0a 783 }
T 784
785
7fe908 786 //######################################################################################################
615a0a 787 // send website quota warnings by email
7fe908 788 //######################################################################################################
615a0a 789
T 790 if ($app->dbmaster == $app->db) {
791
792     $global_config = $app->getconf->get_global_config('mail');
793
794     //* Check website disk quota
795     $sql = "SELECT domain_id,sys_groupid,domain,system_user,last_quota_notification,DATEDIFF(CURDATE(), last_quota_notification) as `notified_before` FROM web_domain WHERE (type = 'vhost' OR type = 'vhostsubdomain')";
796     $records = $app->db->queryAllRecords($sql);
797     if(is_array($records) && !empty($records)) {
7fe908 798
615a0a 799         $tmp_rec =  $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'harddisk_quota' ORDER BY created DESC");
T 800         $monitor_data = array();
801         if(is_array($tmp_rec)) {
802             foreach ($tmp_rec as $tmp_mon) {
7fe908 803                 $monitor_data = array_merge_recursive($monitor_data, unserialize($app->db->unquote($tmp_mon['data'])));
615a0a 804             }
T 805         }
7fe908 806
615a0a 807         foreach($records as $rec) {
T 808
809             //$web_hd_quota = $rec['hd_quota'];
810             $domain = $rec['domain'];
7fe908 811
615a0a 812             $username = $rec['system_user'];
9a30ec 813             $rec['used'] = @$monitor_data['user'][$username]['used'];
TB 814             $rec['soft'] = @$monitor_data['user'][$username]['soft'];
815             $rec['hard'] = @$monitor_data['user'][$username]['hard'];
816             $rec['files'] = @$monitor_data['user'][$username]['files'];
7fe908 817
615a0a 818             if (!is_numeric($rec['used'])){
T 819                 if ($rec['used'][0] > $rec['used'][1]){
820                     $rec['used'] = $rec['used'][0];
821                 } else {
822                     $rec['used'] = $rec['used'][1];
823                 }
824             }
825             if (!is_numeric($rec['soft'])) $rec['soft']=$rec['soft'][1];
826             if (!is_numeric($rec['hard'])) $rec['hard']=$rec['hard'][1];
827             if (!is_numeric($rec['files'])) $rec['files']=$rec['files'][1];
7fe908 828
615a0a 829             // used space ratio
T 830             if($rec['soft'] > 0){
831                 $used_ratio = $rec['used']/$rec['soft'];
832             } else {
833                 $used_ratio = 0;
834             }
7fe908 835
615a0a 836             $rec['ratio'] = number_format($used_ratio * 100, 2, '.', '').'%';
7fe908 837
615a0a 838             if($rec['used'] > 1024) {
7fe908 839                 $rec['used'] = round($rec['used'] / 1024, 2).' MB';
615a0a 840             } else {
T 841                 if ($rec['used'] != '') $rec['used'] .= ' KB';
842             }
7fe908 843
615a0a 844             if($rec['soft'] > 1024) {
7fe908 845                 $rec['soft'] = round($rec['soft'] / 1024, 2).' MB';
615a0a 846             } elseif($rec['soft'] == 0){
T 847                 $rec['soft'] = '----';
848             } else {
849                 $rec['soft'] .= ' KB';
850             }
7fe908 851
615a0a 852             if($rec['hard'] > 1024) {
7fe908 853                 $rec['hard'] = round($rec['hard'] / 1024, 2).' MB';
615a0a 854             } elseif($rec['hard'] == 0){
T 855                 $rec['hard'] = '----';
856             } else {
857                 $rec['hard'] .= ' KB';
858             }
7fe908 859
615a0a 860             // send notifications only if 90% or more of the quota are used
T 861             if($used_ratio < 0.9) {
7fe908 862                 // reset notification date
MC 863                 if($rec['last_quota_notification']) $app->dbmaster->datalogUpdate('web_domain', "last_quota_notification = NULL", 'domain_id', $rec['domain_id']);
615a0a 864
7fe908 865                 // send notification - everything ok again
MC 866                 if($rec['last_quota_notification'] && $web_config['overquota_notify_onok'] == 'y' && ($web_config['overquota_notify_admin'] == 'y' || $web_config['overquota_notify_client'] == 'y')) {
867                     $placeholders = array('{domain}' => $rec['domain'],
868                         '{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'),
869                         '{used}' => $rec['used'],
870                         '{soft}' => $rec['soft'],
871                         '{hard}' => $rec['hard'],
872                         '{ratio}' => $rec['ratio']);
873
874                     $recipients = array();
875
876                     //* send email to admin
877                     if($global_config['admin_mail'] != '' && $web_config['overquota_notify_admin'] == 'y') {
878                         $recipients[] = $global_config['admin_mail'];
879                     }
880
881                     //* Send email to client
882                     if($web_config['overquota_notify_client'] == 'y') {
883                         $client_group_id = $rec["sys_groupid"];
884                         $client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
885                         if($client['email'] != '') {
886                             $recipients[] = $client['email'];
887                         }
888                     }
889                     send_notification_email('web_quota_ok_notification', $placeholders, $recipients);
890                 }
454fa0 891             } else {
7fe908 892
454fa0 893                 // could a notification be sent?
MC 894                 $send_notification = false;
895                 if(!$rec['last_quota_notification']) $send_notification = true; // not yet notified
896                 elseif($web_config['overquota_notify_freq'] > 0 && $rec['notified_before'] >= $web_config['overquota_notify_freq']) $send_notification = true;
7fe908 897
454fa0 898                 //* Send quota notifications
MC 899                 if(($web_config['overquota_notify_admin'] == 'y' || $web_config['overquota_notify_client'] == 'y') && $send_notification == true) {
900                     $app->dbmaster->datalogUpdate('web_domain', "last_quota_notification = CURDATE()", 'domain_id', $rec['domain_id']);
7fe908 901
454fa0 902                     $placeholders = array('{domain}' => $rec['domain'],
MC 903                         '{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'),
904                         '{used}' => $rec['used'],
905                         '{soft}' => $rec['soft'],
906                         '{hard}' => $rec['hard'],
907                         '{ratio}' => $rec['ratio']);
615a0a 908
454fa0 909                     $recipients = array();
7fe908 910
454fa0 911                     //* send email to admin
MC 912                     if($global_config['admin_mail'] != '' && $web_config['overquota_notify_admin'] == 'y') {
913                         $recipients[] = $global_config['admin_mail'];
7fe908 914                     }
454fa0 915
MC 916                     //* Send email to client
917                     if($web_config['overquota_notify_client'] == 'y') {
918                         $client_group_id = $rec["sys_groupid"];
919                         $client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
920                         if($client['email'] != '') {
921                             $recipients[] = $client['email'];
922                         }
923                     }
924                     send_notification_email('web_quota_notification', $placeholders, $recipients);
7fe908 925                 }
615a0a 926             }
T 927         }
928     }
929 }
930
931
7fe908 932 //######################################################################################################
615a0a 933 // send mail quota warnings by email
7fe908 934 //######################################################################################################
615a0a 935
T 936 if ($app->dbmaster == $app->db) {
937
938     $global_config = $app->getconf->get_global_config('mail');
939     $mail_config = $app->getconf->get_server_config($conf['server_id'], 'mail');
940
941     //* Check email quota
942     $sql = "SELECT mailuser_id,sys_groupid,email,name,quota,last_quota_notification,DATEDIFF(CURDATE(), last_quota_notification) as `notified_before` FROM mail_user";
943     $records = $app->db->queryAllRecords($sql);
944     if(is_array($records) && !empty($records)) {
7fe908 945
615a0a 946         $tmp_rec =  $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'email_quota' ORDER BY created DESC");
T 947         $monitor_data = array();
948         if(is_array($tmp_rec)) {
949             foreach ($tmp_rec as $tmp_mon) {
950                 //$monitor_data = array_merge_recursive($monitor_data,unserialize($app->db->unquote($tmp_mon['data'])));
951                 $tmp_array = unserialize($app->db->unquote($tmp_mon['data']));
952                 if(is_array($tmp_array)) {
953                     foreach($tmp_array as $username => $data) {
954                         if(@!$monitor_data[$username]['used']) $monitor_data[$username]['used'] = $data['used'];
955                     }
956                 }
957             }
958         }
7fe908 959
615a0a 960         foreach($records as $rec) {
T 961
962             $email = $rec['email'];
7fe908 963
615a0a 964             $rec['used'] = isset($monitor_data[$email]['used']) ? $monitor_data[$email]['used'] : array(1 => 0);
7fe908 965
615a0a 966             if (!is_numeric($rec['used'])) $rec['used']=$rec['used'][1];
7fe908 967
615a0a 968             // used space ratio
T 969             if($rec['quota'] > 0){
970                 $used_ratio = $rec['used']/$rec['quota'];
971             } else {
972                 $used_ratio = 0;
973             }
7fe908 974
615a0a 975             $rec['ratio'] = number_format($used_ratio * 100, 2, '.', '').'%';
7fe908 976
615a0a 977             if($rec['quota'] > 0){
7fe908 978                 $rec['quota'] = round($rec['quota'] / 1048576, 4).' MB';
615a0a 979             } else {
T 980                 $rec['quota'] = '----';
981             }
982
983             if($rec['used'] < 1544000) {
7fe908 984                 $rec['used'] = round($rec['used'] / 1024, 4).' KB';
615a0a 985             } else {
7fe908 986                 $rec['used'] = round($rec['used'] / 1048576, 4).' MB';
MC 987             }
988
615a0a 989             // send notifications only if 90% or more of the quota are used
T 990             if($used_ratio < 0.9) {
7fe908 991                 // reset notification date
MC 992                 if($rec['last_quota_notification']) $app->dbmaster->datalogUpdate('mail_user', "last_quota_notification = NULL", 'mailuser_id', $rec['mailuser_id']);
615a0a 993
7fe908 994                 // send notification - everything ok again
MC 995                 if($rec['last_quota_notification'] && $mail_config['overquota_notify_onok'] == 'y' && ($mail_config['overquota_notify_admin'] == 'y' || $mail_config['overquota_notify_client'] == 'y')) {
996                     $placeholders = array('{email}' => $rec['email'],
997                         '{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'),
998                         '{used}' => $rec['used'],
999                         '{name}' => $rec['name'],
1000                         '{quota}' => $rec['quota'],
1001                         '{ratio}' => $rec['ratio']);
615a0a 1002
7fe908 1003                     $recipients = array();
MC 1004                     //* send email to admin
1005                     if($global_config['admin_mail'] != '' && $mail_config['overquota_notify_admin'] == 'y') {
1006                         $recipients[] = $global_config['admin_mail'];
1007                     }
1008
1009                     //* Send email to client
1010                     if($mail_config['overquota_notify_client'] == 'y') {
1011                         $client_group_id = $rec["sys_groupid"];
1012                         $client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
1013                         if($client['email'] != '') {
1014                             $recipients[] = $client['email'];
1015                         }
1016                     }
1017
1018                     send_notification_email('mail_quota_ok_notification', $placeholders, $recipients);
1019                 }
454fa0 1020             } else {
7fe908 1021
454fa0 1022                 //* Send quota notifications
MC 1023                 // could a notification be sent?
1024                 $send_notification = false;
1025                 if(!$rec['last_quota_notification']) $send_notification = true; // not yet notified
1026                 elseif($mail_config['overquota_notify_freq'] > 0 && $rec['notified_before'] >= $mail_config['overquota_notify_freq']) $send_notification = true;
7fe908 1027
454fa0 1028                 if(($mail_config['overquota_notify_admin'] == 'y' || $mail_config['overquota_notify_client'] == 'y') && $send_notification == true) {
MC 1029                     $app->dbmaster->datalogUpdate('mail_user', "last_quota_notification = CURDATE()", 'mailuser_id', $rec['mailuser_id']);
7fe908 1030
454fa0 1031                     $placeholders = array('{email}' => $rec['email'],
MC 1032                         '{admin_mail}' => ($global_config['admin_mail'] != ''? $global_config['admin_mail'] : 'root'),
1033                         '{used}' => $rec['used'],
1034                         '{name}' => $rec['name'],
1035                         '{quota}' => $rec['quota'],
1036                         '{ratio}' => $rec['ratio']);
7fe908 1037
454fa0 1038                     $recipients = array();
MC 1039                     //* send email to admin
1040                     if($global_config['admin_mail'] != '' && $mail_config['overquota_notify_admin'] == 'y') {
1041                         $recipients[] = $global_config['admin_mail'];
7fe908 1042                     }
MC 1043
454fa0 1044                     //* Send email to client
MC 1045                     if($mail_config['overquota_notify_client'] == 'y') {
1046                         $client_group_id = $rec["sys_groupid"];
1047                         $client = $app->db->queryOneRecord("SELECT client.email FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
1048                         if($client['email'] != '') {
1049                             $recipients[] = $client['email'];
1050                         }
1051                     }
1052
1053                     send_notification_email('mail_quota_notification', $placeholders, $recipients);
1054                 }
7fe908 1055             }
615a0a 1056         }
T 1057     }
8d0c6b 1058 }
T 1059
b74ef5 1060
7fe908 1061 //######################################################################################################
b74ef5 1062 // deactivate virtual servers (run only on the "master-server")
7fe908 1063 //######################################################################################################
b74ef5 1064
T 1065 if ($app->dbmaster == $app->db) {
1066     $current_date = date('Y-m-d');
1067
1068     //* Check which virtual machines have to be deactivated
1069     $sql = "SELECT * FROM openvz_vm WHERE active = 'y' AND active_until_date != '0000-00-00' AND active_until_date < '$current_date'";
1070     $records = $app->db->queryAllRecords($sql);
1071     if(is_array($records)) {
1072         foreach($records as $rec) {
1073             $app->dbmaster->datalogUpdate('openvz_vm', "active = 'n'", 'vm_id', $rec['vm_id']);
7fe908 1074             $app->log('Virtual machine active date expired. Disabling VM '.$rec['veid'], LOGLEVEL_DEBUG);
b74ef5 1075         }
T 1076     }
1077
1078
1079 }
1080
7fe908 1081 //######################################################################################################
992797 1082 // Create website backups
7fe908 1083 //######################################################################################################
992797 1084
MC 1085 $server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
1086 $backup_dir = $server_config['backup_dir'];
5a43e7 1087 $backup_mode = $server_config['backup_mode'];
T 1088 if($backup_mode == '') $backup_mode = 'userzip';
1089
1090 $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
1091 $http_server_user = $web_config['user'];
49029c 1092
T 1093 if($backup_dir != '') {
a8ccf6 1094
d12a90 1095     if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
T 1096         $backup_dir_permissions = 0755;
1097     } else {
1098         $backup_dir_permissions = 0750;
1099     }
a8ccf6 1100
49029c 1101     if(!is_dir($backup_dir)) {
d12a90 1102         mkdir(escapeshellcmd($backup_dir), $backup_dir_permissions, true);
T 1103     } else {
1104         chmod(escapeshellcmd($backup_dir), $backup_dir_permissions);
49029c 1105     }
056465 1106     
FT 1107     //* mount backup directory, if necessary
1108     $run_backups = true;
061d7c 1109     $backup_dir_mount_cmd = '/usr/local/ispconfig/server/scripts/backup_dir_mount.sh';
TB 1110     if(    $server_config['backup_dir_is_mount'] == 'y' && 
1111         is_file($backup_dir_mount_cmd) && 
1112         is_executable($backup_dir_mount_cmd) &&
1113         fileowner($backup_dir_mount_cmd) === 0
1114         ){
056465 1115         if(!$app->system->is_mounted($backup_dir)){
061d7c 1116             exec($backup_dir_mount_cmd);
056465 1117             sleep(1);
FT 1118             if(!$app->system->is_mounted($backup_dir)) $run_backups = false;
49029c 1119         }
T 1120     }
a8ccf6 1121
056465 1122     if($run_backups){
FT 1123         $sql = "SELECT * FROM web_domain WHERE server_id = ".$conf['server_id']." AND (type = 'vhost' OR type = 'vhostsubdomain')";
1124         $records = $app->db->queryAllRecords($sql);
1125         if(is_array($records)) {
1126             foreach($records as $rec) {
a8ccf6 1127
056465 1128                 //* Do the website backup
FT 1129                 if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
a8ccf6 1130
056465 1131                     $web_path = $rec['document_root'];
FT 1132                     $web_user = $rec['system_user'];
1133                     $web_group = $rec['system_group'];
1134                     $web_id = $rec['domain_id'];
1135                     $web_backup_dir = $backup_dir.'/web'.$web_id;
1136                     if(!is_dir($web_backup_dir)) mkdir($web_backup_dir, 0750);
1137                     chmod($web_backup_dir, 0750);
1138                     //if(isset($server_config['backup_dir_ftpread']) && $server_config['backup_dir_ftpread'] == 'y') {
1139                     chown($web_backup_dir, $rec['system_user']);
1140                     chgrp($web_backup_dir, $rec['system_group']);
1141                     /*} else {
1142                         chown($web_backup_dir, 'root');
1143                         chgrp($web_backup_dir, 'root');
1144                     }*/
1145                 
1146                     $backup_excludes = '';
1147                     $b_excludes = explode(',', trim($rec['backup_excludes']));
1148                     if(is_array($b_excludes) && !empty($b_excludes)){
1149                         foreach($b_excludes as $b_exclude){
1150                             $b_exclude = trim($b_exclude);
1151                             if($b_exclude != ''){
1152                                 $backup_excludes .= ' --exclude='.escapeshellarg($b_exclude);
1153                             }
1154                         }
22903a 1155                     }
056465 1156                 
FT 1157                     if($backup_mode == 'userzip') {
1158                         //* Create a .zip backup as web user and include also files owned by apache / nginx user
1159                         $web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.zip';
1160                         exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -group '.escapeshellarg($web_group).' -print 2> /dev/null | zip -b /tmp --exclude=backup\*'.$backup_excludes.' --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
f75374 1161                         if($retval == 0 || $retval == 12) exec('cd '.escapeshellarg($web_path).' && sudo -u '.escapeshellarg($web_user).' find . -user '.escapeshellarg($http_server_user).' -print 2> /dev/null | zip -b /tmp --exclude=backup\*'.$backup_excludes.' --update --symlinks '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' -@', $tmp_output, $retval);
056465 1162                     } else {
FT 1163                         //* Create a tar.gz backup as root user
1164                         $web_backup_file = 'web'.$web_id.'_'.date('Y-m-d_H-i').'.tar.gz';
1165                         exec('tar pczf '.escapeshellarg($web_backup_dir.'/'.$web_backup_file).' --exclude=backup\*'.$backup_excludes.' --directory '.escapeshellarg($web_path).' .', $tmp_output, $retval);
5a43e7 1166                     }
f75374 1167                     if($retval == 0 || ($backup_mode != 'userzip' && $retval == 1) || ($backup_mode == 'userzip' && $retval == 12)) { // tar can return 1, zip can return 12(due to harmless warings) and still create valid backups  
056465 1168                         if(is_file($web_backup_dir.'/'.$web_backup_file)){
FT 1169                             chown($web_backup_dir.'/'.$web_backup_file, 'root');
1170                             chgrp($web_backup_dir.'/'.$web_backup_file, 'root');
1171                             chmod($web_backup_dir.'/'.$web_backup_file, 0750);
7fe908 1172
056465 1173                             //* Insert web backup record in database
FT 1174                             //$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
1175                             //$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
1176                             $sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",".$web_id.",'web','".$backup_mode."',".time().",'".$app->db->quote($web_backup_file)."')";
1177                             $app->db->query($sql);
1178                             if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
1179                         }
1180                     } else {
1181                         if(is_file($web_backup_dir.'/'.$web_backup_file)) unlink($web_backup_dir.'/'.$web_backup_file);
1182                     }
1183
1184                     //* Remove old backups
1185                     $backup_copies = intval($rec['backup_copies']);
1186
1187                     $dir_handle = dir($web_backup_dir);
1188                     $files = array();
1189                     while (false !== ($entry = $dir_handle->read())) {
1190                         if($entry != '.' && $entry != '..' && substr($entry, 0, 3) == 'web' && is_file($web_backup_dir.'/'.$entry)) {
1191                             $files[] = $entry;
1192                         }
1193                     }
1194                     $dir_handle->close();
1195
1196                     rsort($files);
1197
7fe908 1198                     for ($n = $backup_copies; $n <= 10; $n++) {
056465 1199                         if(isset($files[$n]) && is_file($web_backup_dir.'/'.$files[$n])) {
FT 1200                             unlink($web_backup_dir.'/'.$files[$n]);
1201                             //$sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
7fe908 1202                             //$tmp = $app->dbmaster->queryOneRecord($sql);
056465 1203                             //$app->dbmaster->datalogDelete('web_backup', 'backup_id', $tmp['backup_id']);
7fe908 1204                             //$sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
056465 1205                             $sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($files[$n])."'";
7fe908 1206                             $app->db->query($sql);
MC 1207                             if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
1208                         }
1209                     }
056465 1210
FT 1211                     unset($files);
1212                     unset($dir_handle);
1213
1214                     //* Remove backupdir symlink and create as directory instead
1215                     $app->system->web_folder_protection($web_path, false);
1216
1217                     if(is_link($web_path.'/backup')) {
1218                         unlink($web_path.'/backup');
1219                     }
1220                     if(!is_dir($web_path.'/backup')) {
1221                         mkdir($web_path.'/backup');
1222                         chown($web_path.'/backup', $rec['system_user']);
1223                         chgrp($web_path.'/backup', $rec['system_group']);
1224                     }
1225
1226                     $app->system->web_folder_protection($web_path, true);
7fe908 1227                 }
a8ccf6 1228
056465 1229                 /* If backup_interval is set to none and we have a
FT 1230                 backup directory for the website, then remove the backups */
1231                 if($rec['backup_interval'] == 'none' || $rec['backup_interval'] == '') {
1232                     $web_id = $rec['domain_id'];
1233                     $web_user = $rec['system_user'];
1234                     $web_backup_dir = realpath($backup_dir.'/web'.$web_id);
1235                     if(is_dir($web_backup_dir)) {
1236                         exec('sudo -u '.escapeshellarg($web_user).' rm -f '.escapeshellarg($web_backup_dir.'/*'));
b78992 1237                         $sql = "DELETE FROM web_backup WHERE server_id = ".intval($conf['server_id'])." AND parent_domain_id = ".intval($web_id);
TB 1238                         $app->db->query($sql);
1239                         if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
056465 1240                     }
FT 1241                 }
5a43e7 1242             }
T 1243         }
a8ccf6 1244
056465 1245         $sql = "SELECT * FROM web_database WHERE server_id = ".$conf['server_id']." AND backup_interval != 'none' AND backup_interval != ''";
FT 1246         $records = $app->db->queryAllRecords($sql);
1247         if(is_array($records)) {
a8ccf6 1248
056465 1249             include 'lib/mysql_clientdb.conf';
7fe908 1250
056465 1251             foreach($records as $rec) {
FT 1252
1253                 //* Do the database backup
1254                 if($rec['backup_interval'] == 'daily' or ($rec['backup_interval'] == 'weekly' && date('w') == 0) or ($rec['backup_interval'] == 'monthly' && date('d') == '01')) {
1255
1256                     $web_id = $rec['parent_domain_id'];
1257                     $db_backup_dir = $backup_dir.'/web'.$web_id;
1258                     if(!is_dir($db_backup_dir)) mkdir($db_backup_dir, 0750);
1259                     chmod($db_backup_dir, 0750);
1260                     chown($db_backup_dir, 'root');
1261                     chgrp($db_backup_dir, 'root');
1262
1263                     //* Do the mysql database backup with mysqldump
1264                     $db_id = $rec['database_id'];
1265                     $db_name = $rec['database_name'];
1266                     $db_backup_file = 'db_'.$db_name.'_'.date('Y-m-d_H-i').'.sql';
1267                     //$command = "mysqldump -h '".escapeshellcmd($clientdb_host)."' -u '".escapeshellcmd($clientdb_user)."' -p'".escapeshellcmd($clientdb_password)."' -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
1268                     $command = "mysqldump -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." -c --add-drop-table --create-options --quick --result-file='".$db_backup_dir.'/'.$db_backup_file."' '".$db_name."'";
1269                     exec($command, $tmp_output, $retval);
1270
1271                     //* Compress the backup with gzip
1272                     if($retval == 0) exec("gzip -c '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file)."' > '".escapeshellcmd($db_backup_dir.'/'.$db_backup_file).".gz'", $tmp_output, $retval);
1273
1274                     if($retval == 0){
1275                         if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')){
1276                             chmod($db_backup_dir.'/'.$db_backup_file.'.gz', 0750);
1277                             chown($db_backup_dir.'/'.$db_backup_file.'.gz', fileowner($db_backup_dir));
1278                             chgrp($db_backup_dir.'/'.$db_backup_file.'.gz', filegroup($db_backup_dir));
1279
1280                             //* Insert web backup record in database
1281                             //$insert_data = "(server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
1282                             //$app->dbmaster->datalogInsert('web_backup', $insert_data, 'backup_id');
1283                             $sql = "INSERT INTO web_backup (server_id,parent_domain_id,backup_type,backup_mode,tstamp,filename) VALUES (".$conf['server_id'].",$web_id,'mysql','sqlgz',".time().",'".$app->db->quote($db_backup_file).".gz')";
1284                             $app->db->query($sql);
1285                             if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
1286                         }
1287                     } else {
1288                         if(is_file($db_backup_dir.'/'.$db_backup_file.'.gz')) unlink($db_backup_dir.'/'.$db_backup_file.'.gz');
1289                     }
1290                     //* Remove the uncompressed file
1291                     if(is_file($db_backup_dir.'/'.$db_backup_file)) unlink($db_backup_dir.'/'.$db_backup_file);
1292
1293                     //* Remove old backups
1294                     $backup_copies = intval($rec['backup_copies']);
1295
1296                     $dir_handle = dir($db_backup_dir);
1297                     $files = array();
1298                     while (false !== ($entry = $dir_handle->read())) {
1299                         if($entry != '.' && $entry != '..' && preg_match('/^db_(.*?)_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}\.sql.gz$/', $entry, $matches) && is_file($db_backup_dir.'/'.$entry)) {
1300                             if(array_key_exists($matches[1], $files) == false) $files[$matches[1]] = array();
1301                             $files[$matches[1]][] = $entry;
1302                         }
1303                     }
1304                     $dir_handle->close();
1305
1306                     reset($files);
1307                     foreach($files as $db_name => $filelist) {
1308                         rsort($filelist);
1309                         for ($n = $backup_copies; $n <= 10; $n++) {
1310                             if(isset($filelist[$n]) && is_file($db_backup_dir.'/'.$filelist[$n])) {
1311                                 unlink($db_backup_dir.'/'.$filelist[$n]);
1312                                 //$sql = "SELECT backup_id FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
1313                                 //$tmp = $app->dbmaster->queryOneRecord($sql);
1314                                 //$sql = "DELETE FROM web_backup WHERE backup_id = ".intval($tmp['backup_id']);
1315                                 $sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = $web_id AND filename = '".$app->db->quote($filelist[$n])."'";
1316                                 $app->db->query($sql);
1317                                 if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
1318                             }
1319                         }
1320                     }
1321
1322                     unset($files);
1323                     unset($dir_handle);
1324                 }
992797 1325             }
056465 1326
FT 1327             unset($clientdb_host);
1328             unset($clientdb_user);
1329             unset($clientdb_password);
1330
1331         }
1332
1333         // remove non-existing backups from database
1334         $backups = $app->db->queryAllRecords("SELECT * FROM web_backup WHERE server_id = ".$conf['server_id']);
1335         if(is_array($backups) && !empty($backups)){
1336             foreach($backups as $backup){
1337                 $backup_file = $backup_dir.'/web'.$backup['parent_domain_id'].'/'.$backup['filename'];
1338                 if(!is_file($backup_file)){
1339                     $sql = "DELETE FROM web_backup WHERE server_id = ".$conf['server_id']." AND parent_domain_id = ".$backup['parent_domain_id']." AND filename = '".$backup['filename']."'";
1340                     $app->db->query($sql);
1341                     if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql);
1342                 }
1343             }
1344         }
1345     } else {
1346         //* send email to admin that backup directory could not be mounted
1347         $global_config = $app->getconf->get_global_config('mail');
1348         if($global_config['admin_mail'] != ''){
1349             $subject = 'Backup directory '.$backup_dir.' could not be mounted';
1350             $message = "Backup directory ".$backup_dir." could not be mounted.\n\nThe command\n\n".$server_config['backup_dir_mount_cmd']."\n\nfailed.";
1351             mail($global_config['admin_mail'], $subject, $message);
992797 1352         }
MC 1353     }
49029c 1354 }
T 1355
8d0c6b 1356
313e33 1357 die("finished.\n");
8e725d 1358 ?>