function js()
{
    $tpl = new templates();
    $table = $_GET["table"];
    $page = CurrentPageName();
    $q = new mysql_squid_builder();
    $time = $q->TIME_FROM_DANSGUARDIAN_EVENTS_TABLE($table);
    $dateT = date("Y {l} {F} d", $time);
    if ($tpl->language == "fr") {
        $dateT = date("{l} d {F} Y", $time);
    }
    $title[] = $dateT;
    if ($_GET["field"] != null) {
        $title[] = $_GET["field"];
    }
    if ($_GET["value"] != null) {
        $title[] = $_GET["value"];
    }
    if ($_GET["sitename"] != null) {
        $title[] = $_GET["sitename"];
    }
    $finaltitle = $tpl->_ENGINE_parse_body(@implode("»", $title));
    $uri = "popup=yes&table={$table}&field={$_GET["field"]}&value={$_GET["value"]}&sitename={$_GET["sitename"]}";
    echo "YahooWinBrowse('950','{$page}?{$uri}','{$finaltitle}');";
}
function REPAIR_WORKING_TABLES()
{
    $q = new mysql_squid_builder();
    $LIST_TABLES_DAYS = $q->LIST_TABLES_HOURS();
    events(count($LIST_TABLES_DAYS) . " daily tables");
    while (list($tablename, $ligne) = each($LIST_TABLES_DAYS)) {
        $xtime = $q->TIME_FROM_HOUR_TABLE($tablename);
        $xdate = date("Y-m-d", $xtime);
        $danstable = "dansguardian_events_" . date("Ymd", $xtime);
        if ($GLOBALS["VERBOSE"]) {
            echo "{$tablename}: {$danstable} = {$xdate}\n";
        }
        $q->QUERY_SQL("INSERT IGNORE INTO tables_day (tablename,zDate) VALUES ('{$tablename}','{$xdate}')");
    }
    $currentTABLE = date("Ymd") . "_hour";
    $LIST_TABLES_dansguardian_events = $q->LIST_TABLES_dansguardian_events();
    while (list($tablename, $ligne) = each($LIST_TABLES_dansguardian_events)) {
        $xtime = $q->TIME_FROM_DANSGUARDIAN_EVENTS_TABLE($tablename);
        $hour_table = date("Ymd", $xtime) . "_hour";
        if ($currentTABLE == $hour_table) {
            continue;
        }
        if (!$q->TABLE_EXISTS($hour_table)) {
            events(" ####### WARNING - NO TABLE {$hour_table} #############");
        }
    }
}
function ScanDays($onlyTable = false)
{
    $q = new mysql_squid_builder(true);
    $ARRAY_DAYS = array();
    $tables = $q->LIST_TABLES_dansguardian_events();
    while (list($tablename, $line) = each($tables)) {
        $dayTime = $q->TIME_FROM_DANSGUARDIAN_EVENTS_TABLE($tablename);
        $day = date("Y-m-d", $dayTime);
        $ARRAY_DAYS[$day] = $dayTime;
        $TABLES[$tablename] = true;
    }
    $tables = $q->LIST_TABLES_HOURS();
    while (list($tablename, $line) = each($tables)) {
        $dayTime = $q->TIME_FROM_HOUR_TABLE($tablename);
        $day = date("Y-m-d", $dayTime);
        $ARRAY_DAYS[$day] = $dayTime;
        $TABLES[$tablename] = true;
    }
    $tables = $q->LIST_TABLES_YOUTUBE_DAYS();
    //youtubeday_
    while (list($tablename, $line) = each($tables)) {
        $dayTime = $q->TIME_FROM_YOUTUBE_DAY_TABLE($tablename);
        $day = date("Y-m-d", $dayTime);
        $ARRAY_DAYS[$day] = $dayTime;
        $TABLES[$tablename] = true;
    }
    $tables = $q->LIST_TABLES_USERSIZED();
    //youtubeday_
    while (list($tablename, $line) = each($tables)) {
        $dayTime = $q->TIME_FROM_USERSIZED_TABLE($tablename);
        $day = date("Y-m-d", $dayTime);
        $ARRAY_DAYS[$day] = $dayTime;
        $TABLES[$tablename] = true;
    }
    $tables = $q->LIST_TABLES_QUOTADAY();
    //youtubeday_
    while (list($tablename, $line) = each($tables)) {
        $dayTime = $q->TIME_FROM_QUOTADAY_TABLE($tablename);
        $day = date("Y-m-d", $dayTime);
        $ARRAY_DAYS[$day] = $dayTime;
        $TABLES[$tablename] = true;
    }
    $tables = $q->LIST_CAT_FAMDAY();
    //youtubeday_
    while (list($tablename, $line) = each($tables)) {
        $dayTime = $q->TIME_FROM_CAT_FAMDAY_TABLE($tablename);
        $day = date("Y-m-d", $dayTime);
        $ARRAY_DAYS[$day] = $dayTime;
        $TABLES[$tablename] = true;
    }
    $prefix = "INSERT IGNORE INTO tables_day (tablename,zDate) VALUES ";
    while (list($day, $dayTime) = each($ARRAY_DAYS)) {
        $tablename = "dansguardian_events_" . date("Ymd", $dayTime);
        $f[] = "('{$tablename}','{$day}')";
    }
    if (count($f) > 0) {
        $q->QUERY_SQL($prefix . @implode(",", $f));
        if (!$q->ok) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Fatal {$q->mysql_error}\n";
            }
            squid_admin_purge(0, "Fatal {$q->mysql_error}", "Backup process cannot be performed", __FILE__, __LINE__);
            ufdbguard_admin_events("Fatal {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__, "backup");
            return false;
        }
    }
    if ($onlyTable) {
        return $TABLES;
    }
    return true;
}
function repair_from_sources_tables()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($GLOBALS["VERBOSE"]) {
        echo "time: {$timefile}\n";
    }
    $pid = @file_get_contents($pidfile);
    if (!$GLOBALS["VERBOSE"]) {
        if (!$GLOBALS["FORCE"]) {
            if ($unix->process_exists($pid, basename(__FILE__))) {
                if ($GLOBALS["VERBOSE"]) {
                    echo "Already executed pid {$pid}\n";
                }
                return;
            }
            if ($pid < 100) {
                $pid = null;
            }
            $mypid = getmypid();
            @file_put_contents($pidfile, $mypid);
        }
    }
    if ($GLOBALS["FORCE_TIME"]) {
        $timeexec = $unix->file_time_min($timefile);
        if ($timeexec < 240) {
            return;
        }
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $Prefix = "/usr/share/artica-postfix";
    $php5 = $unix->LOCATE_PHP5_BIN();
    $nohup = $unix->find_program("nohup");
    $EXEC_NICE = $unix->EXEC_NICE();
    $q = new mysql_squid_builder();
    $C = 0;
    $array = $q->LIST_TABLES_dansguardian_events();
    $current = "dansguardian_events_" . date("Ymd");
    while (list($tablename, $none) = each($array)) {
        if ($tablename == $current) {
            continue;
        }
        $time = $q->TIME_FROM_DANSGUARDIAN_EVENTS_TABLE($tablename);
        $xtime = date("Y-m-d", $time);
        $hour_table = date("Ymd", $time) . "_hour";
        $member_table = date("Ymd", $time) . "_members";
        $SUM_SOURCE = $q->COUNT_ROWS($tablename);
        if ($SUM_SOURCE == 0) {
            continue;
        }
        $SUM_DEST = $q->COUNT_ROWS($hour_table);
        $PERC = $SUM_DEST / $SUM_SOURCE * 100;
        $PERC = intval($PERC);
        echo "{$xtime}] {$SUM_SOURCE} - {$SUM_DEST} = {$PERC}% - {$tablename}\n";
        if ($PERC < 5) {
            if (!$q->CreateHourTable($hour_table)) {
                echo "{$xtime}] {$tablename} unable to create {$hour_table}\n";
                continue;
            }
            _repair_from_sources_tables($tablename, $hour_table);
            $q->QUERY_SQL("UPDATE tables_day SET `totalsize`='0',`requests`=0,`MembersCount`=0,`month_flow`=0,weekdone=0,weekbdone=0 WHERE tablename='{$tablename}'");
            $C++;
        }
        $ligne1 = mysql_fetch_array($q->QUERY_SQL("SELECT SUM(hits) as hits FROM {$tablename}"));
        if (!$q->ok) {
            echo $q->mysql_error;
        }
        $SumDehits_src = $ligne1["hits"];
        $ligne1 = mysql_fetch_array($q->QUERY_SQL("SELECT SUM(hits) as hits FROM {$member_table}"));
        $SumDehits_dest = $ligne1["hits"];
        $PERC = $SumDehits_dest / $SumDehits_src * 100;
        $PERC = intval($PERC);
        echo "{$xtime}] {$SumDehits_src} - {$SumDehits_dest} = {$PERC}% - {$member_table}\n";
        if ($PERC < 90) {
            if (!$q->CreateMembersDayTable($hour_table)) {
                echo "{$xtime}] {$tablename} unable to create {$hour_table}\n";
                continue;
            }
            _repair_members_sources_tables($tablename, $member_table);
            $C++;
        }
    }
    if ($C > 0) {
        shell_exec(trim("{$EXEC_NICE} {$php5} {$Prefix}/exec.squid.stats.totals.php --repair --byschedule --schedule-id={$GLOBALS["SCHEDULE_ID"]}"));
    }
}