Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
commit | author | age
5de2af 1 <?php
M 2
3 /*
4 Copyright (c) 2013, Marius Cramer, pixcept KG
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class cronjob_webalizer extends cronjob {
32
b1a6a5 33     // job schedule
MC 34     protected $_schedule = '0 0 * * *';
5de2af 35
b1a6a5 36     /* this function is optional if it contains no custom code */
MC 37     public function onPrepare() {
38         global $app;
39
40         parent::onPrepare();
41     }
42
43     /* this function is optional if it contains no custom code */
44     public function onBeforeRun() {
45         global $app;
46
47         return parent::onBeforeRun();
48     }
49
50     public function onRunJob() {
51         global $app, $conf;
52
53         //######################################################################################################
54         // Create webalizer statistics
55         //######################################################################################################
56
57         function setConfigVar( $filename, $varName, $varValue, $append = 0 ) {
58             if($lines = @file($filename)) {
59                 $out = '';
60                 $found = 0;
61                 foreach($lines as $line) {
62                     @list($key, $value) = preg_split('/[\t= ]+/', $line, 2);
63                     if($key == $varName) {
64                         $out .= $varName.' '.$varValue."\n";
65                         $found = 1;
66                     } else {
67                         $out .= $line;
68                     }
69                 }
70                 if($found == 0) {
71                     //* add \n if the last line does not end with \n or \r
72                     if(substr($out, -1) != "\n" && substr($out, -1) != "\r") $out .= "\n";
73                     //* add the new line at the end of the file
74                     if($append == 1) $out .= $varName.' '.$varValue."\n";
75                 }
76
77                 file_put_contents($filename, $out);
78             }
79         }
5de2af 80
M 81
f0dfa9 82         $sql = "SELECT domain_id, domain, document_root, web_folder, type, parent_domain_id, system_user, system_group FROM web_domain WHERE (type = 'vhost' or type = 'vhostsubdomain' or type = 'vhostalias') and stats_type = 'webalizer' AND server_id = ?";
cc7a82 83         $records = $app->db->queryAllRecords($sql, $conf['server_id']);
5de2af 84
b1a6a5 85         foreach($records as $rec) {
MC 86             //$yesterday = date('Ymd',time() - 86400);
87             $yesterday = date('Ymd', strtotime("-1 day", time()));
5de2af 88
b1a6a5 89             $log_folder = 'log';
511ba5 90             if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') {
cc7a82 91                 $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = ?', $rec['parent_domain_id']);
b1a6a5 92                 $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
MC 93                 if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
94                 $log_folder .= '/' . $subdomain_host;
95                 unset($tmp);
96             }
97             $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log');
98             if(!@is_file($logfile)) {
99                 $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz');
100                 if(!@is_file($logfile)) {
101                     continue;
102                 }
103             }
5de2af 104
b1a6a5 105             $domain = escapeshellcmd($rec['domain']);
511ba5 106             $statsdir = escapeshellcmd($rec['document_root'].'/'.(($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') ? $rec['web_folder'] : 'web').'/stats');
b1a6a5 107             $webalizer = '/usr/bin/webalizer';
MC 108             $webalizer_conf_main = '/etc/webalizer/webalizer.conf';
109             $webalizer_conf = escapeshellcmd($rec['document_root'].'/log/webalizer.conf');
5de2af 110
b1a6a5 111             if(is_file($statsdir.'/index.php')) unlink($statsdir.'/index.php');
5de2af 112
b1a6a5 113             if(!@is_file($webalizer_conf)) {
MC 114                 copy($webalizer_conf_main, $webalizer_conf);
115             }
5de2af 116
b1a6a5 117             if(@is_file($webalizer_conf)) {
MC 118                 setConfigVar($webalizer_conf, 'Incremental', 'yes');
119                 setConfigVar($webalizer_conf, 'IncrementalName', $statsdir.'/webalizer.current');
120                 setConfigVar($webalizer_conf, 'HistoryName', $statsdir.'/webalizer.hist');
121             }
5de2af 122
M 123
b1a6a5 124             if(!@is_dir($statsdir)) mkdir($statsdir);
f0dfa9 125             $username = escapeshellcmd($rec['system_user']);
MC 126             $groupname = escapeshellcmd($rec['system_group']);
127             chown($statsdir, $username);
128             chgrp($statsdir, $groupname);
b1a6a5 129             exec("$webalizer -c $webalizer_conf -n $domain -s $domain -r $domain -q -T -p -o $statsdir $logfile");
f0dfa9 130             
MC 131             exec('chown -R '.$username.':'.$groupname.' '.$statsdir);
b1a6a5 132         }
5de2af 133
b1a6a5 134
MC 135
136         parent::onRunJob();
137     }
138
139     /* this function is optional if it contains no custom code */
140     public function onAfterRun() {
141         global $app;
142
143         parent::onAfterRun();
144     }
5de2af 145
M 146 }
147
148 ?>