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_awstats 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;
5de2af 39
b1a6a5 40         parent::onPrepare();
MC 41     }
5de2af 42
b1a6a5 43     /* this function is optional if it contains no custom code */
MC 44     public function onBeforeRun() {
45         global $app;
5de2af 46
b1a6a5 47         return parent::onBeforeRun();
MC 48     }
5de2af 49
b1a6a5 50     public function onRunJob() {
MC 51         global $app, $conf;
5de2af 52
b1a6a5 53         //######################################################################################################
MC 54         // Create awstats statistics
55         //######################################################################################################
5de2af 56
cc7a82 57         $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' or type = 'vhostalias') and stats_type = 'awstats' AND server_id = ?";
MC 58         $records = $app->db->queryAllRecords($sql, $conf['server_id']);
5de2af 59
b1a6a5 60         $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
MC 61
62         foreach($records as $rec) {
63             //$yesterday = date('Ymd',time() - 86400);
64             $yesterday = date('Ymd', strtotime("-1 day", time()));
65
66             $log_folder = 'log';
511ba5 67             if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') {
cc7a82 68                 $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = ?', $rec['parent_domain_id']);
b1a6a5 69                 $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
MC 70                 if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
71                 $log_folder .= '/' . $subdomain_host;
72                 unset($tmp);
73             }
74             $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log');
75             if(!@is_file($logfile)) {
76                 $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz');
77                 if(!@is_file($logfile)) {
78                     continue;
79                 }
80             }
511ba5 81             $web_folder = (($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') ? $rec['web_folder'] : 'web');
b1a6a5 82             $domain = escapeshellcmd($rec['domain']);
MC 83             $statsdir = escapeshellcmd($rec['document_root'].'/'.$web_folder.'/stats');
84             $awstats_pl = $web_config['awstats_pl'];
85             $awstats_buildstaticpages_pl = $web_config['awstats_buildstaticpages_pl'];
86
87             $awstats_conf_dir = $web_config['awstats_conf_dir'];
88             $awstats_website_conf_file = $web_config['awstats_conf_dir'].'/awstats.'.$domain.'.conf';
89
90             if(is_file($awstats_website_conf_file)) unlink($awstats_website_conf_file);
91
cc7a82 92             $sql = "SELECT domain FROM web_domain WHERE (type = 'alias' OR type = 'subdomain') AND parent_domain_id = ?";
MC 93             $aliases = $app->db->queryAllRecords($sql, $rec['domain_id']);
b1a6a5 94             $aliasdomain = '';
MC 95
96             if(is_array($aliases)) {
97                 foreach ($aliases as $alias) {
98                     $aliasdomain.= ' '.$alias['domain']. ' www.'.$alias['domain'];
99                 }
100             }
101
102             if(!is_file($awstats_website_conf_file)) {
e6abfe 103                 if (is_file($awstats_conf_dir."/awstats.conf")) {
CS 104                                     $include_file = $awstats_conf_dir."/awstats.conf";
105                 } elseif (is_file($awstats_conf_dir."/awstats.model.conf")) {
106                     $include_file = $awstats_conf_dir."/awstats.model.conf";
107                 }
108                 $awstats_conf_file_content = 'Include "'.$include_file.'"
5de2af 109         LogFile="/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log"
M 110         SiteDomain="'.$domain.'"
111         HostAliases="www.'.$domain.' localhost 127.0.0.1'.$aliasdomain.'"';
9aaf97 112                 if (isset($include_file)) {
CS 113                     file_put_contents($awstats_website_conf_file, $awstats_conf_file_content);
114                 } else {
115                     $app->log("No awstats base config found. Either awstats.conf or awstats.model.conf must exist in ".$awstats_conf_dir.".", LOGLEVEL_WARN);
116                 }
b1a6a5 117             }
5de2af 118
b1a6a5 119             if(!@is_dir($statsdir)) mkdir($statsdir);
f0dfa9 120             $username = escapeshellcmd($rec['system_user']);
MC 121             $groupname = escapeshellcmd($rec['system_group']);
122             chown($statsdir, $username);
123             chgrp($statsdir, $groupname);
b1a6a5 124             if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
MC 125             symlink($logfile, '/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
5de2af 126
b1a6a5 127             $awmonth = date("n");
MC 128             $awyear = date("Y");
5de2af 129
b1a6a5 130             if (date("d") == 1) {
MC 131                 $awmonth = date("m")-1;
132                 if (date("m") == 1) {
133                     $awyear = date("Y")-1;
134                     $awmonth = "12";
135                 }
136             }
5de2af 137
b1a6a5 138             // awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/'.$web_folder.'/stats -awstatsprog=/path/to/awstats.pl
MC 139             // $command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
5de2af 140
b1a6a5 141             $command = "$awstats_buildstaticpages_pl -month='$awmonth' -year='$awyear' -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
5de2af 142
b1a6a5 143             if (date("d") == 2) {
MC 144                 $awmonth = date("m")-1;
145                 if (date("m") == 1) {
146                     $awyear = date("Y")-1;
147                     $awmonth = "12";
148                 }
5de2af 149
b1a6a5 150                 $statsdirold = $statsdir."/".$awyear."-".$awmonth."/";
MC 151                 mkdir($statsdirold);
152                 $files = scandir($statsdir);
153                 foreach ($files as $file) {
154                     if (substr($file, 0, 1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file, 0, 1) != "w" && substr($file, 0, 1) != "i") copy("$statsdir"."/"."$file", "$statsdirold"."$file");
155                 }
156             }
5de2af 157
M 158
b1a6a5 159             if($awstats_pl != '' && $awstats_buildstaticpages_pl != '' && fileowner($awstats_pl) == 0 && fileowner($awstats_buildstaticpages_pl) == 0) {
MC 160                 exec($command);
161                 if(is_file($rec['document_root'].'/'.$web_folder.'/stats/index.html')) unlink($rec['document_root'].'/'.$web_folder.'/stats/index.html');
162                 rename($rec['document_root'].'/'.$web_folder.'/stats/awstats.'.$domain.'.html', $rec['document_root'].'/'.$web_folder.'/stats/awsindex.html');
163                 if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
164                     if(file_exists("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master")) {
165                         copy("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
166                     } else {
167                         copy("/usr/local/ispconfig/server/conf/awstats_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
168                     }
169                 }
5de2af 170
b1a6a5 171                 $app->log('Created awstats statistics with command: '.$command, LOGLEVEL_DEBUG);
MC 172             } else {
173                 $app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.", LOGLEVEL_WARN);
174             }
5de2af 175
b1a6a5 176             if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
MC 177                 chown($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_user']);
178                 chgrp($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_group']);
179             }
5de2af 180
f0dfa9 181             exec('chown -R '.$username.':'.$groupname.' '.$statsdir);
b1a6a5 182         }
MC 183
184
185         parent::onRunJob();
186     }
187
188     /* this function is optional if it contains no custom code */
189     public function onAfterRun() {
190         global $app;
191
192         parent::onAfterRun();
193     }
5de2af 194
M 195 }
196
197 ?>