#!/usr/bin/php
<?php

    global $ini;
    global $node;

    /// includes
    require(dirname(__FILE__)."/../include/functions.php");
    init();

    if (get_ini_opt('cron','webalizer') === '0') { exit(0); }

    check_live();


    $statdir=$ini['webalizer']['statdir'];
    $wabin=$ini['webalizer']['bin'];
    $waconf=$ini['webalizer']['conf'];

    if ($statdir=="") { die("Stat destination dir not specified.\n"); }
    if ($wabin=="") { die("Webalizer bin not specified.\n"); }
    if ($waconf=="") { die("Webalizer config file not specified.\n"); }

    $sql="SELECT vhosts.id AS vhid,
	    CONCAT(vhosts.host,'.',domain.name) AS vhname,
	    `vhosts`.`upstream` AS `upstream`,
	    domain.name AS domname
	    FROM `vhosts`
	    LEFT JOIN domain ON vhosts.domainid=domain.id
	    LEFT JOIN client ON domain.clientid=client.id
	    WHERE client.node='$node' AND vhosts.active=1 AND domain.active=1 AND client.active=1;";

    $res=DBquery($sql);

    while ($srow=$res->fetch_assoc()) {

	$vhid=$srow['vhid'];
	$vhname=$srow['vhname'];
	$upstream=$srow['upstream'];
	$domname=$srow['domname'];

	$log=findlog($vhname, $upstream, $ini);
	if ($log) {
	    $std=$statdir.'/'.$vhname;
	    if (!(is_dir($std))) {
		mkdir($std);
	    }
	    system("$wabin -Q -r $vhname -r $domname -o $std -n $vhname -c $waconf $log");
	}

    }

DBclose();

?>
