Marius Cramer
2013-11-14 b1a6a5a3991cec5cd08873b01376e45d0b247f18
server/lib/classes/cron.d/150-webalizer.inc.php
@@ -29,113 +29,113 @@
*/
class cronjob_webalizer 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 webalizer statistics
        #######################################################################################################
        function setConfigVar( $filename, $varName, $varValue, $append = 0 ) {
            if($lines = @file($filename)) {
                $out = '';
                $found = 0;
                foreach($lines as $line) {
                    @list($key, $value) = preg_split('/[\t= ]+/', $line, 2);
                    if($key == $varName) {
                        $out .= $varName.' '.$varValue."\n";
                        $found = 1;
                    } else {
                        $out .= $line;
                    }
                }
                if($found == 0) {
                    //* add \n if the last line does not end with \n or \r
                    if(substr($out,-1) != "\n" && substr($out,-1) != "\r") $out .= "\n";
                    //* add the new line at the end of the file
                    if($append == 1) $out .= $varName.' '.$varValue."\n";
                }
   // job schedule
   protected $_schedule = '0 0 * * *';
                file_put_contents($filename,$out);
            }
        }
   /* 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 webalizer statistics
      //######################################################################################################
      function setConfigVar( $filename, $varName, $varValue, $append = 0 ) {
         if($lines = @file($filename)) {
            $out = '';
            $found = 0;
            foreach($lines as $line) {
               @list($key, $value) = preg_split('/[\t= ]+/', $line, 2);
               if($key == $varName) {
                  $out .= $varName.' '.$varValue."\n";
                  $found = 1;
               } else {
                  $out .= $line;
               }
            }
            if($found == 0) {
               //* add \n if the last line does not end with \n or \r
               if(substr($out, -1) != "\n" && substr($out, -1) != "\r") $out .= "\n";
               //* add the new line at the end of the file
               if($append == 1) $out .= $varName.' '.$varValue."\n";
            }
            file_put_contents($filename, $out);
         }
      }
        $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'];
        $records = $app->db->queryAllRecords($sql);
      $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'];
      $records = $app->db->queryAllRecords($sql);
        foreach($records as $rec) {
            //$yesterday = date('Ymd',time() - 86400);
            $yesterday = date('Ymd',strtotime("-1 day", time()));
      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;
                }
            }
         $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;
            }
         }
            $domain = escapeshellcmd($rec['domain']);
            $statsdir = escapeshellcmd($rec['document_root'].'/'.($rec['type'] == 'vhostsubdomain' ? $rec['web_folder'] : 'web').'/stats');
            $webalizer = '/usr/bin/webalizer';
            $webalizer_conf_main = '/etc/webalizer/webalizer.conf';
            $webalizer_conf = escapeshellcmd($rec['document_root'].'/log/webalizer.conf');
         $domain = escapeshellcmd($rec['domain']);
         $statsdir = escapeshellcmd($rec['document_root'].'/'.($rec['type'] == 'vhostsubdomain' ? $rec['web_folder'] : 'web').'/stats');
         $webalizer = '/usr/bin/webalizer';
         $webalizer_conf_main = '/etc/webalizer/webalizer.conf';
         $webalizer_conf = escapeshellcmd($rec['document_root'].'/log/webalizer.conf');
            if(is_file($statsdir.'/index.php')) unlink($statsdir.'/index.php');
         if(is_file($statsdir.'/index.php')) unlink($statsdir.'/index.php');
            if(!@is_file($webalizer_conf)) {
                copy($webalizer_conf_main,$webalizer_conf);
            }
         if(!@is_file($webalizer_conf)) {
            copy($webalizer_conf_main, $webalizer_conf);
         }
            if(@is_file($webalizer_conf)) {
                setConfigVar($webalizer_conf, 'Incremental', 'yes');
                setConfigVar($webalizer_conf, 'IncrementalName', $statsdir.'/webalizer.current');
                setConfigVar($webalizer_conf, 'HistoryName', $statsdir.'/webalizer.hist');
            }
         if(@is_file($webalizer_conf)) {
            setConfigVar($webalizer_conf, 'Incremental', 'yes');
            setConfigVar($webalizer_conf, 'IncrementalName', $statsdir.'/webalizer.current');
            setConfigVar($webalizer_conf, 'HistoryName', $statsdir.'/webalizer.hist');
         }
            if(!@is_dir($statsdir)) mkdir($statsdir);
            exec("$webalizer -c $webalizer_conf -n $domain -s $domain -r $domain -q -T -p -o $statsdir $logfile");
        }
         if(!@is_dir($statsdir)) mkdir($statsdir);
         exec("$webalizer -c $webalizer_conf -n $domain -s $domain -r $domain -q -T -p -o $statsdir $logfile");
      }
        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();
   }
}