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