function start($xtime = 0)
{
    if ($GLOBALS["VERBOSE"]) {
        "echo Loading...\n";
    }
    $unix = new unix();
    $pids = $unix->PIDOF_PATTERN_ALL(basename(__FILE__));
    if (count($pids) > 5) {
        die;
    }
    if ($xtime == 0) {
        if ($GLOBALS["VERBOSE"]) {
            "echo Loading done...\n";
        }
        $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
        $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
        $pid = @file_get_contents($pidfile);
        if (!$GLOBALS["FORCE"]) {
            if ($pid < 100) {
                $pid = null;
            }
            $unix = new unix();
            if ($unix->process_exists($pid, basename(__FILE__))) {
                if ($GLOBALS["VERBOSE"]) {
                    echo "Already executed pid {$pid}\n";
                }
                return;
            }
            $timeexec = $unix->file_time_min($timefile);
            if ($timeexec < 720) {
                return;
            }
            $mypid = getmypid();
            @file_put_contents($pidfile, $mypid);
        }
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $nohup = $unix->find_program("nohup");
    $php = $unix->LOCATE_PHP5_BIN();
    if ($xtime > 0) {
        $dateRequested = date("Y-m-d", $xtime);
        $dateRequested_sql = " WHERE zDate='{$dateRequested}'";
        if (SquidStatisticsTasksOverTime()) {
            stats_admin_events(1, "Statistics overtime... Aborting ( requested for {$dateRequested} ) ", null, __FILE__, __LINE__);
            return;
        }
    }
    $sql = "SELECT\n\tDATE_FORMAT(zDate,'%Y%m%d') as tprefix,DATE_FORMAT(zDate,'%Y-%m-%d') as CurDay,tablename FROM tables_day{$dateRequested_sql}  \n\tORDER BY zDate DESC";
    $q = new mysql_squid_builder();
    $results = $q->QUERY_SQL($sql);
    //bigint(100)
    if ($q->FIELD_TYPE("tables_day", "totalBlocked", "syslogs") == "bigint(100)") {
        $q->QUERY_SQL('ALTER TABLE `tables_day` CHANGE `size` `size` BIGINT( 255 ) NOT NULL');
        $q->QUERY_SQL('ALTER TABLE `tables_day` CHANGE `totalBlocked` `totalBlocked` BIGINT( 255 ) NOT NULL');
        $q->QUERY_SQL('ALTER TABLE `tables_day` CHANGE `requests` `requests` BIGINT( 255 ) NOT NULL');
        $q->QUERY_SQL('ALTER TABLE `tables_day` CHANGE `totalsize` `totalsize` BIGINT( 255 ) NOT NULL');
        $q->QUERY_SQL('ALTER TABLE `tables_day` CHANGE `size_cached` `size_cached` BIGINT( 255 ) NOT NULL');
    }
    if (!$q->FIELD_EXISTS("tables_day", "totalKeyWords")) {
        $q->QUERY_SQL("ALTER TABLE `tables_day` ADD `totalKeyWords` BIGINT( 255 ) NOT NULL NOT NULL,ADD INDEX ( `totalKeyWords`)");
        if (!$q->ok) {
            squid_admin_mysql(0, $q->mysql_error, null, __FILE__, __LINE__);
            echo $q->mysql_error;
            return;
        }
    }
    if (!$q->FIELD_EXISTS("tables_day", "DangerousCatz")) {
        $q->QUERY_SQL("ALTER TABLE `tables_day` ADD `DangerousCatz` smallint( 1 ) NOT NULL NOT NULL,ADD INDEX ( `DangerousCatz`)");
    }
    if (!$q->ok) {
        echo "{$q->mysql_error}.<hr>{$sql}</hr>";
    }
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $tablename = $ligne["tablename"];
        $hourtable = $ligne["tprefix"] . "_hour";
        if ($ligne["tprefix"] == date("Ymd")) {
            continue;
        }
        $KeyWordsTable = "searchwordsD_" . $ligne["tprefix"];
        $members_table = "{$ligne["tprefix"]}_members";
        $youtube_table = "youtubeday_{$ligne["tprefix"]}";
        $myXtime = strtotime($ligne["CurDay"] . "00:00:00");
        if ($q->TABLE_EXISTS($hourtable)) {
            $sql = "SELECT SUM(size) as tsize, SUM(hits) as thits FROM {$hourtable}";
            $ligne2 = mysql_fetch_array($q->QUERY_SQL($sql));
            $size = $ligne2["tsize"];
            $hits = $ligne2["thits"];
            $sizeL = FormatBytes($size / 1024);
            if ($GLOBALS["VERBOSE"]) {
                echo "{$tablename} - {$sizeL} / {$hits} {$hourtable} [ {$sql} ]\n";
            }
            $sql = "UPDATE tables_day SET totalsize='{$size}',requests='{$hits}' WHERE tablename='{$tablename}'";
            $q->QUERY_SQL($sql);
            if (!$q->ok) {
                if ($GLOBALS["VERBOSE"]) {
                    echo $q->mysql_error . "\n";
                }
            }
            $sql = "SELECT COUNT(`sitename`) as tcount FROM {$hourtable} WHERE LENGTH(`category`)=0";
            if ($GLOBALS["VERBOSE"]) {
                echo $sql . "\n";
            }
            $ligne2 = mysql_fetch_array($q->QUERY_SQL($sql));
            $max = $ligne2["tcount"];
            $sql = "UPDATE tables_day SET `not_categorized`={$max} WHERE tablename='{$tablename}'";
            $q->QUERY_SQL($sql);
        }
        if ($q->TABLE_EXISTS($members_table)) {
            $MembersField = which_filter($members_table, true);
            if ($GLOBALS["VERBOSE"]) {
                echo "Table members Calculate Members by {$MembersField}\n";
            }
            if ($MembersField != null) {
                $MembersCount = CalculateElements($members_table, $MembersField);
                $sql = "UPDATE tables_day SET `MembersCount`={$MembersCount} WHERE tablename='{$tablename}'";
                if ($GLOBALS["VERBOSE"]) {
                    echo $sql . "\n";
                }
                $q->QUERY_SQL($sql);
            }
        }
        if ($q->TABLE_EXISTS($youtube_table)) {
            $sql = "SELECT youtubeid FROM {$youtube_table} GROUP BY youtubeid";
            $results2 = $q->QUERY_SQL($sql);
            if (!$q->ok) {
                echo $q->mysql_error . "\n";
                return;
            }
            $YouTubeHits = mysql_num_rows($results2);
            $sql = "UPDATE tables_day SET `YouTubeHits`={$YouTubeHits} WHERE tablename='{$tablename}'";
            $q->QUERY_SQL($sql);
        }
        if ($q->TABLE_EXISTS($KeyWordsTable)) {
            $sql = "SELECT `words` FROM {$KeyWordsTable} GROUP BY `words`";
            $results2 = $q->QUERY_SQL($sql);
            if (!$q->ok) {
                echo $q->mysql_error . "\n";
                return;
            }
            $CountOfWords = mysql_num_rows($results2);
            $sql = "UPDATE tables_day SET totalKeyWords='{$CountOfWords}' WHERE tablename='{$tablename}'";
            $q->QUERY_SQL($sql);
        }
    }
}
function generate_graph_final()
{
    include_once 'ressources/class.artica.graphs.inc';
    $q = new mysql_squid_builder();
    $page = CurrentPageName();
    $tpl = new templates();
    $t = $_GET["t"];
    $ff = time();
    $xtime = $_GET["xtime"];
    $tablename = date("Ymd", $_GET["xtime"]) . "_members";
    if ($_GET["FILTER"] == null) {
        $_GET["FILTER"] = which_filter($tablename);
    }
    $FILTER = $tpl->_ENGINE_parse_body("{{$_GET["FILTER"]}}");
    $sql = "SELECT COUNT({$_GET["FILTER"]}) as tcount, hour FROM {$tablename} GROUP BY hour ORDER BY hour";
    switch ($_GET["FILTER"]) {
        case "client":
            $subtitle = "{ipaddr}";
            break;
        case "uid":
            $subtitle = "{member}";
            break;
        default:
            $subtitle = "{{$_GET["FILTER"]}}";
            break;
    }
    $c = 0;
    $results = $q->QUERY_SQL($sql);
    if (mysql_num_rows($results) > 0) {
        $nb_events = mysql_num_rows($results);
        while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
            $xdata[] = $ligne["hour"];
            $ydata[] = $ligne["tcount"];
            $c++;
        }
    }
    $highcharts = new highcharts();
    $highcharts->container = $_GET["container"];
    $highcharts->xAxis = $xdata;
    $highcharts->Title = "{statistics} " . $tpl->_ENGINE_parse_body("{$subtitle}/{hours}");
    $highcharts->yAxisTtitle = "{members}";
    $highcharts->xAxisTtitle = "{hours}";
    $highcharts->datas = array("{members}" => $ydata);
    echo $highcharts->BuildChart();
}
function _summarize_days($dpref, $tablename, $zDate = null)
{
    $q = new mysql_squid_builder();
    $tablename_members = "{$dpref}_members";
    $tablename_blocked = "{$dpref}_blocked";
    $tablename_youtube = "youtubeday_{$dpref}";
    $tablename_hour = "{$dpref}_hour";
    $BlockedCount = 0;
    $MembersCount = 0;
    $SumHits = 0;
    $SumSize = 0;
    $NotCategorized = 0;
    $YouTubeHits = 0;
    if ($zDate == null) {
        $Cyear = substr($dpref, 0, 4);
        $CMonth = substr($dpref, 4, 2);
        $CDay = substr($dpref, 6, 2);
        $zDate = "{$Cyear}-{$CMonth}-{$CDay}";
    }
    if (!$q->TABLE_EXISTS("{$tablename_hour}")) {
        return;
    }
    $sql = "SELECT COUNT(zMD5) as tcount FROM {$tablename_hour} WHERE LENGTH(category)=0";
    $ligne2 = mysql_fetch_array($q->QUERY_SQL($sql));
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        return;
    }
    $NotCategorized = $ligne2["tcount"];
    $sql = "SELECT SUM(size) as tsize,SUM(hits) as thits FROM {$tablename_hour}";
    $ligne2 = mysql_fetch_array($q->QUERY_SQL($sql));
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        return;
    }
    $SumSize = $ligne2["tsize"];
    $SumHits = $ligne2["thits"];
    if (!$q->TABLE_EXISTS("UserSizeD_{$dpref}")) {
        if ($GLOBALS["VERBOSE"]) {
            echo "<strong>Fatal!:</strong> UserSizeD_{$dpref} no such table\n";
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "<strong>Try to repair the table for `{$zDate}`</strong>\n";
        }
        UserSizeD_REPAIR($zDate);
    }
    $countrows = $q->COUNT_ROWS("UserSizeD_{$dpref}");
    if ($countrows == 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo "<strong>Fatal!:</strong> UserSizeD_{$dpref} no row\n";
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "<strong>Try to repair the table for `{$zDate}`</strong>\n";
        }
        UserSizeD_REPAIR($zDate);
    }
    if ($q->TABLE_EXISTS($tablename_members)) {
        $MembersField = which_filter($tablename_members, true);
        if ($GLOBALS["VERBOSE"]) {
            echo "Table members Calculate Members by {$MembersField}\n";
        }
        $MembersCount = CalculateElements($tablename_members, $MembersField);
    } else {
        if ($GLOBALS["VERBOSE"]) {
            echo "<strong>Fatal!:</strong> {$tablename_members} no such table\n";
        }
    }
    if ($q->TABLE_EXISTS($tablename_blocked)) {
        $BlockedCount = $q->COUNT_ROWS($tablename_blocked);
    }
    if ($q->TABLE_EXISTS($tablename_youtube)) {
        $sql = "SELECT SUM( hits ) AS tcount FROM {$tablename_youtube}";
        $ligne2 = mysql_fetch_array($q->QUERY_SQL($sql));
        if (!$q->ok) {
            echo $q->mysql_error . "\n";
            return;
        }
        $YouTubeHits = $ligne2["tcount"];
    }
    if (!is_numeric($SumSize)) {
        $SumSize = 0;
    }
    if (!is_numeric($SumHits)) {
        $SumHits = 0;
    }
    if (!is_numeric($BlockedCount)) {
        $BlockedCount = 0;
    }
    if (!is_numeric($MembersCount)) {
        $MembersCount = 0;
    }
    if (!is_numeric($NotCategorized)) {
        $NotCategorized = 0;
    }
    if (!is_numeric($YouTubeHits)) {
        $YouTubeHits = 0;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "totalBlocked={$BlockedCount},MembersCount={$MembersCount}, requests={$SumHits}, totalsize={$SumSize}, not_categorized={$NotCategorized}, YouTubeHits={$YouTubeHits}\n";
    }
    $sql = "UPDATE tables_day\n\t\tSET totalBlocked={$BlockedCount},\n\t\tMembersCount={$MembersCount},\n\t\trequests={$SumHits},\n\t\ttotalsize={$SumSize},\n\t\tnot_categorized={$NotCategorized},\n\t\tYouTubeHits={$YouTubeHits}\n\t\tWHERE tablename='{$tablename}'";
    $q->QUERY_SQL($sql);
    if (!$q->ok) {
        if ($GLOBALS["VERBOSE"]) {
            echo "<strong>Fatal!: {$q->mysql_error}</strong>\n";
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "<hr><strong style='font-size:16px'>Please Refresh this web page to update icons</strong><hr>\n";
    }
}