Marius Cramer
2013-11-14 b1a6a5a3991cec5cd08873b01376e45d0b247f18
server/lib/classes/cron.d/150-awstats.inc.php
@@ -29,154 +29,154 @@
*/
class cronjob_awstats extends cronjob {
    // job schedule
    protected $_schedule = '0 0 * * *';
    /* this function is optional if it contains no custom code */
    public function onPrepare() {
        global $app;
        parent::onPrepare();
    }
    /* this function is optional if it contains no custom code */
    public function onBeforeRun() {
        global $app;
        return parent::onBeforeRun();
    }
    public function onRunJob() {
        global $app, $conf;
        #######################################################################################################
        // Create awstats statistics
        #######################################################################################################
        $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'];
        $records = $app->db->queryAllRecords($sql);
   // job schedule
   protected $_schedule = '0 0 * * *';
        $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
   /* this function is optional if it contains no custom code */
   public function onPrepare() {
      global $app;
        foreach($records as $rec) {
            //$yesterday = date('Ymd',time() - 86400);
            $yesterday = date('Ymd',strtotime("-1 day", time()));
      parent::onPrepare();
   }
            $log_folder = 'log';
            if($rec['type'] == 'vhostsubdomain') {
                $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = '.intval($rec['parent_domain_id']));
                $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
                if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
                $log_folder .= '/' . $subdomain_host;
                unset($tmp);
            }
            $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log');
            if(!@is_file($logfile)) {
                $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz');
                if(!@is_file($logfile)) {
                    continue;
                }
            }
            $web_folder = ($rec['type'] == 'vhostsubdomain' ? $rec['web_folder'] : 'web');
            $domain = escapeshellcmd($rec['domain']);
            $statsdir = escapeshellcmd($rec['document_root'].'/'.$web_folder.'/stats');
            $awstats_pl = $web_config['awstats_pl'];
            $awstats_buildstaticpages_pl = $web_config['awstats_buildstaticpages_pl'];
   /* this function is optional if it contains no custom code */
   public function onBeforeRun() {
      global $app;
            $awstats_conf_dir = $web_config['awstats_conf_dir'];
            $awstats_website_conf_file = $web_config['awstats_conf_dir'].'/awstats.'.$domain.'.conf';
      return parent::onBeforeRun();
   }
            if(is_file($awstats_website_conf_file)) unlink($awstats_website_conf_file);
   public function onRunJob() {
      global $app, $conf;
            $sql = "SELECT domain FROM web_domain WHERE (type = 'alias' OR type = 'subdomain') AND parent_domain_id = ".$rec['domain_id'];
            $aliases = $app->db->queryAllRecords($sql);
            $aliasdomain = '';
      //######################################################################################################
      // Create awstats statistics
      //######################################################################################################
            if(is_array($aliases)) {
                foreach ($aliases as $alias) {
                    $aliasdomain.= ' '.$alias['domain']. ' www.'.$alias['domain'];
                }
            }
      $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'];
      $records = $app->db->queryAllRecords($sql);
            if(!is_file($awstats_website_conf_file)) {
                $awstats_conf_file_content = 'Include "'.$awstats_conf_dir.'/awstats.conf"
      $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
      foreach($records as $rec) {
         //$yesterday = date('Ymd',time() - 86400);
         $yesterday = date('Ymd', strtotime("-1 day", time()));
         $log_folder = 'log';
         if($rec['type'] == 'vhostsubdomain') {
            $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = '.intval($rec['parent_domain_id']));
            $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $rec['domain']);
            if($subdomain_host == '') $subdomain_host = 'web'.$rec['domain_id'];
            $log_folder .= '/' . $subdomain_host;
            unset($tmp);
         }
         $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log');
         if(!@is_file($logfile)) {
            $logfile = escapeshellcmd($rec['document_root'].'/' . $log_folder . '/'.$yesterday.'-access.log.gz');
            if(!@is_file($logfile)) {
               continue;
            }
         }
         $web_folder = ($rec['type'] == 'vhostsubdomain' ? $rec['web_folder'] : 'web');
         $domain = escapeshellcmd($rec['domain']);
         $statsdir = escapeshellcmd($rec['document_root'].'/'.$web_folder.'/stats');
         $awstats_pl = $web_config['awstats_pl'];
         $awstats_buildstaticpages_pl = $web_config['awstats_buildstaticpages_pl'];
         $awstats_conf_dir = $web_config['awstats_conf_dir'];
         $awstats_website_conf_file = $web_config['awstats_conf_dir'].'/awstats.'.$domain.'.conf';
         if(is_file($awstats_website_conf_file)) unlink($awstats_website_conf_file);
         $sql = "SELECT domain FROM web_domain WHERE (type = 'alias' OR type = 'subdomain') AND parent_domain_id = ".$rec['domain_id'];
         $aliases = $app->db->queryAllRecords($sql);
         $aliasdomain = '';
         if(is_array($aliases)) {
            foreach ($aliases as $alias) {
               $aliasdomain.= ' '.$alias['domain']. ' www.'.$alias['domain'];
            }
         }
         if(!is_file($awstats_website_conf_file)) {
            $awstats_conf_file_content = 'Include "'.$awstats_conf_dir.'/awstats.conf"
        LogFile="/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log"
        SiteDomain="'.$domain.'"
        HostAliases="www.'.$domain.' localhost 127.0.0.1'.$aliasdomain.'"';
                file_put_contents($awstats_website_conf_file,$awstats_conf_file_content);
            }
            file_put_contents($awstats_website_conf_file, $awstats_conf_file_content);
         }
            if(!@is_dir($statsdir)) mkdir($statsdir);
            if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
            symlink($logfile,'/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
         if(!@is_dir($statsdir)) mkdir($statsdir);
         if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
         symlink($logfile, '/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
            $awmonth = date("n");
            $awyear = date("Y");
         $awmonth = date("n");
         $awyear = date("Y");
            if (date("d") == 1) {
                $awmonth = date("m")-1;
                if (date("m") == 1) {
                    $awyear = date("Y")-1;
                    $awmonth = "12";
                }
            }
         if (date("d") == 1) {
            $awmonth = date("m")-1;
            if (date("m") == 1) {
               $awyear = date("Y")-1;
               $awmonth = "12";
            }
         }
            // awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/'.$web_folder.'/stats -awstatsprog=/path/to/awstats.pl
            // $command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
         // awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/'.$web_folder.'/stats -awstatsprog=/path/to/awstats.pl
         // $command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
            $command = "$awstats_buildstaticpages_pl -month='$awmonth' -year='$awyear' -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
         $command = "$awstats_buildstaticpages_pl -month='$awmonth' -year='$awyear' -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
            if (date("d") == 2) {
                $awmonth = date("m")-1;
                if (date("m") == 1) {
                    $awyear = date("Y")-1;
                    $awmonth = "12";
                }
         if (date("d") == 2) {
            $awmonth = date("m")-1;
            if (date("m") == 1) {
               $awyear = date("Y")-1;
               $awmonth = "12";
            }
                $statsdirold = $statsdir."/".$awyear."-".$awmonth."/";
                mkdir($statsdirold);
                $files = scandir($statsdir);
                foreach ($files as $file) {
                    if (substr($file,0,1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file,0,1) != "w" && substr($file,0,1) != "i") copy("$statsdir"."/"."$file","$statsdirold"."$file");
                }
            }
            $statsdirold = $statsdir."/".$awyear."-".$awmonth."/";
            mkdir($statsdirold);
            $files = scandir($statsdir);
            foreach ($files as $file) {
               if (substr($file, 0, 1) != "." && !is_dir("$statsdir"."/"."$file") && substr($file, 0, 1) != "w" && substr($file, 0, 1) != "i") copy("$statsdir"."/"."$file", "$statsdirold"."$file");
            }
         }
            if($awstats_pl != '' && $awstats_buildstaticpages_pl != '' && fileowner($awstats_pl) == 0 && fileowner($awstats_buildstaticpages_pl) == 0) {
                exec($command);
                if(is_file($rec['document_root'].'/'.$web_folder.'/stats/index.html')) unlink($rec['document_root'].'/'.$web_folder.'/stats/index.html');
                rename($rec['document_root'].'/'.$web_folder.'/stats/awstats.'.$domain.'.html',$rec['document_root'].'/'.$web_folder.'/stats/awsindex.html');
                if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
                    if(file_exists("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master")) {
                        copy("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master",$rec['document_root']."/".$web_folder."/stats/index.php");
                    } else {
                        copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$rec['document_root']."/".$web_folder."/stats/index.php");
                    }
                }
         if($awstats_pl != '' && $awstats_buildstaticpages_pl != '' && fileowner($awstats_pl) == 0 && fileowner($awstats_buildstaticpages_pl) == 0) {
            exec($command);
            if(is_file($rec['document_root'].'/'.$web_folder.'/stats/index.html')) unlink($rec['document_root'].'/'.$web_folder.'/stats/index.html');
            rename($rec['document_root'].'/'.$web_folder.'/stats/awstats.'.$domain.'.html', $rec['document_root'].'/'.$web_folder.'/stats/awsindex.html');
            if(!is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
               if(file_exists("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master")) {
                  copy("/usr/local/ispconfig/server/conf-custom/awstats_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
               } else {
                  copy("/usr/local/ispconfig/server/conf/awstats_index.php.master", $rec['document_root']."/".$web_folder."/stats/index.php");
               }
            }
                $app->log('Created awstats statistics with command: '.$command,LOGLEVEL_DEBUG);
            } else {
                $app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.",LOGLEVEL_WARN);
            }
            $app->log('Created awstats statistics with command: '.$command, LOGLEVEL_DEBUG);
         } else {
            $app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.", LOGLEVEL_WARN);
         }
            if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
                chown($rec['document_root']."/".$web_folder."/stats/index.php",$rec['system_user']);
                chgrp($rec['document_root']."/".$web_folder."/stats/index.php",$rec['system_group']);
            }
         if(is_file($rec['document_root']."/".$web_folder."/stats/index.php")) {
            chown($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_user']);
            chgrp($rec['document_root']."/".$web_folder."/stats/index.php", $rec['system_group']);
         }
        }
        parent::onRunJob();
    }
    /* this function is optional if it contains no custom code */
    public function onAfterRun() {
        global $app;
        parent::onAfterRun();
    }
      }
      parent::onRunJob();
   }
   /* this function is optional if it contains no custom code */
   public function onAfterRun() {
      global $app;
      parent::onAfterRun();
   }
}