function status()
{
    $tpl = new templates();
    $page = CurrentPageName();
    $users = new usersMenus();
    $sock = new sockets();
    if (!$users->ARTICADB_INSTALLED) {
        $html = FATAL_ERROR_SHOW_128("{ARTICADB_NOT_INSTALLED_EXPLAIN}") . "<center style='margin:80px'>\n\t\t<hr>" . button("{install_now}", "Loadjs('squid.blacklist.upd.php')", 16) . "</center>";
        echo $tpl->_ENGINE_parse_body($html);
        return;
    }
    $date = $sock->getFrameWork("squid.php?articadb-version=yes");
    $q = new mysql_catz();
    $sock = new sockets();
    $ini = new Bs_IniHandler();
    $catz = $q->LIST_TABLES_CATEGORIES();
    $ini->loadString(base64_decode($sock->getFrameWork('cmd.php?squid-ini-status=yes')));
    $APP_ARTICADB = DAEMON_STATUS_ROUND("APP_ARTICADB", $ini, null, 1);
    $APP_SQUID_DB = DAEMON_STATUS_ROUND("APP_SQUID_DB", $ini, null, 1);
    $sql = "SHOW VARIABLES LIKE '%version%';";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo "<p class=text-error>{$q->mysql_error}</p>";
    } else {
        while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
            if ($ligne["Variable_name"] == "slave_type_conversions") {
                continue;
            }
            $tt[] = "\t<tr>\n\t\t\t<td colspan=2><div style='font-size:14px'>{{$ligne["Variable_name"]}}:&nbsp;{$ligne["Value"]}</a></div></td>\n\t\t\t</tr>";
        }
    }
    $arrayV = unserialize(base64_decode($sock->getFrameWork("squid.php?articadb-nextversion=yes")));
    $REMOTE_VERSION = $arrayV["TIME"];
    if ($REMOTE_VERSION > $date) {
        $updaebutton = "<div style='text-align:right'><hr>" . button("{update}:{version} {$REMOTE_VERSION} ", "Loadjs('squid.blacklist.upd.php')", 16) . "</div>";
    }
    $nextcheck = $sock->getFrameWork("squid.php?articadb-nextcheck=yes");
    $nextcheck = intval($nextcheck);
    if ($nextcheck > 0) {
        $nextcheck_text = "\n\t\t\t<tr>\n\t\t\t<td colspan=2><div style='font-size:16px'>{next_check_in}:&nbsp;{$nextcheck}Mn</div></td>\n\t\t\t</tr>";
    }
    if ($nextcheck < 0) {
        $nextcheck = str_replace("-", "", $nextcheck);
        $nextcheckTime = time() - intval($nextcheck) * 60;
        $nextcheckTimeText = distanceOfTimeInWords($nextcheckTime, time());
        $nextcheck_text = "\n\t\t\t\t\t<tr>\n\t\t\t\t\t<td colspan=2><div style='font-size:16px'>{last_check}:&nbsp;{$nextcheckTimeText}</div></td>\n\t\t\t\t\t</tr>";
    }
    $dbsize = $sock->getFrameWork("squid.php?articadbsize=yes");
    $items = numberFormat($q->COUNT_CATEGORIES(), 0, "", " ");
    $html = "\n\t\t\t\t<table style='width:100%' class=TableRemove>\n\t\t\t\t<tr>\n\t\t\t\t\t<td width=130px' ><img src='img/spider-database-128.png'></td>\n\t\t\t\t\t<td valign='top'>\n\t\t\t\t<table style='width:99%'>\n\t\t\t\t<tr>\n\t\t\t\t<td valign='top' style='width:320px'>{$APP_ARTICADB}{$APP_SQUID_DB}</td>\n\t\t\t\t<td valign='top' style='padding-left:10px'>\n\t\t\t\t<table style='width:100%'>\n\t\t\t\t<tbody>\n\t\t\t\t<tr>\n\t\t\t\t<td colspan=2><div style='font-size:16px'>{pattern_database_version}:&nbsp;{$date}&nbsp({$dbsize})</div></td>\n\t\t\t\t</tr>\n\t\t\t\t{$nextcheck_text}\n\t\t\t\t<tr>\n\t\t\t\t<td colspan=2><div style='font-size:16px'>{categories}:&nbsp;<strong>" . count($catz) . "</strong></a></div></td>\n\t\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td colspan=2><div style='font-size:16px'>{categorized_websites}:&nbsp;<strong>{$items}</strong>&nbsp</div></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t<td colspan=2><div style='font-size:16px;font-weight:bold;margin-top:10px'>{mysql_engine}:</div></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t" . @implode("", $tt) . "\n\t\t\t\t\t\t</tbody>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t</table>\n\t\t{$updaebutton}\n\t\t</td>\n\t\t</tr>\n\t\t</table>\n\t\t";
    echo $tpl->_ENGINE_parse_body($html);
}
function squid_status_stats()
{
    $off = "<script>UnlockPage();</script>";
    if (!$GLOBALS["AS_ROOT"]) {
        $cachefile = "/usr/share/artica-postfix/ressources/logs/web/traffic.statistics.html";
        if (is_file($cachefile)) {
            $tpl = new templates();
            $cacheContent = @file_get_contents($cachefile);
            if (strlen($cacheContent) > 20) {
                echo $tpl->_ENGINE_parse_body(@file_get_contents($cachefile)) . $off;
                return;
            }
        }
    }
    if (CACHE_SESSION_GET(__FUNCTION__, __FILE__)) {
        return;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " Loading classes<br>\n";
    }
    $sock = new sockets();
    $users = new usersMenus();
    $EnableRemoteStatisticsAppliance = $sock->GET_INFO("EnableRemoteStatisticsAppliance");
    $SquidActHasReverse = $sock->GET_INFO("SquidActHasReverse");
    if (!is_numeric($SquidActHasReverse)) {
        $SquidActHasReverse = 0;
    }
    if ($EnableRemoteStatisticsAppliance == 1) {
        return;
    }
    $DisableArticaProxyStatistics = $sock->GET_INFO("DisableArticaProxyStatistics");
    if (!is_numeric($DisableArticaProxyStatistics)) {
        $DisableArticaProxyStatistics = 0;
    }
    $MalwarePatrolDatabasesCount = $sock->getFrameWork("cmd.php?MalwarePatrolDatabasesCount=yes");
    $mouse = "OnMouseOver=\";this.style.cursor='pointer';\" OnMouseOut=\";this.style.cursor='default';\"";
    $EnableMacAddressFilter = $sock->GET_INFO("EnableMacAddressFilter");
    if (!is_numeric($EnableMacAddressFilter)) {
        $EnableMacAddressFilter = 1;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " Loading mysql_storelogs()<br>\n";
    }
    $syslogs = new mysql_storelogs();
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " Count accesslogs<br>\n";
    }
    $SyslogsFiles = $syslogs->COUNT_ROWS("accesslogs");
    $TR_ACCESSLOG = "\n\t<tr>\n\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' \n\t\tOnClick=\"javascript:Loadjs('squid.accesses.rotate.php')\"><b><span style='font-size:12px'>{$SyslogsFiles}</span></b><span style='font-size:12px'> {access_logs}</td>\n\t</tr>";
    $page = CurrentPageName();
    $tpl = new templates();
    $q = new mysql_squid_builder();
    if ($users->PROXYTINY_APPLIANCE) {
        $DisableArticaProxyStatistics = 1;
    }
    if ($DisableArticaProxyStatistics == 0) {
        $websitesnums = $q->COUNT_ROWS("visited_sites");
        $websitesnums = numberFormat($websitesnums, 0, "", " ");
        $sql = "DELETE FROM categorize WHERE LENGTH(pattern)=0";
        $q->QUERY_SQL($sql);
        $export = $q->COUNT_ROWS("categorize");
        $export = numberFormat($export, 0, "", " ");
    }
    $catz = new mysql_catz();
    $categories = $catz->COUNT_CATEGORIES();
    $categories = numberFormat($categories, 0, "", " ");
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " categories= {$categories}<br>\n";
    }
    $YourItems = $q->COUNT_CATEGORIES();
    $YourItems = numberFormat($YourItems, 0, "", " ");
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " LIST_TABLES_CATEGORIES()<br>\n";
    }
    $tablescat = $q->LIST_TABLES_CATEGORIES();
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " tablescat={$tablescat}<br>\n";
    }
    $tablescatNUM = numberFormat(count($tablescat), 0, "", " ");
    if ($DisableArticaProxyStatistics == 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " EVENTS_SUM()<br>\n";
        }
        $requests = $q->EVENTS_SUM();
        $requests = numberFormat($requests, 0, "", " ");
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " requests = {$requests}<br>\n";
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " no_license -> translate<br>\n";
    }
    $nolicense = $tpl->_ENGINE_parse_body("{no_license}");
    $PhishingURIS = $q->COUNT_ROWS("categoryuris_phishing");
    $PhishingURIS = numberFormat($PhishingURIS, 0, "", " ");
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " >COUNT_ROWS('categoryuris_malware')<br>\n";
    }
    $MalwaresURIS = $q->COUNT_ROWS("categoryuris_malware");
    $MalwaresURIS = numberFormat($MalwaresURIS, 0, "", " ");
    if (!$users->CORP_LICENSE) {
        $PhishingURIS = $nolicense;
        $MalwaresURIS = $nolicense;
    }
    if ($DisableArticaProxyStatistics == 0) {
        if ($EnableMacAddressFilter == 1) {
            $Computers = $q->COUNT_ROWS("webfilters_nodes");
            $Computers = numberFormat($Computers, 0, "", " ");
            $nodes = "\n\t\t\t<tr>\n\t\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.nodes.php',true)\"><b><span style='font-size:12px'>{$Computers}</span></b><span style='font-size:12px'> {computers}</td>\n\t\t\t</tr>";
        } else {
            $Computers = $q->COUNT_ROWS("UserAutDB");
            $Computers = numberFormat($Computers, 0, "", " ");
            $nodes = "\n\t\t\t<tr>\n\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.UserAutDB.php')\"><b><span style='font-size:12px'>{$Computers}</span></b><span style='font-size:12px'> {clients}</td>\n\t\t\t</tr>";
        }
        if (!$users->CORP_LICENSE) {
            $license_inactive = "<br><strong style='font-size:11px;font-weight:bolder;color:#BA1010'>{license_inactive}</strong>";
        }
        if (!$q->TABLE_EXISTS("tables_day")) {
            $q->CheckTables();
        }
        $DAYSNumbers = $q->COUNT_ROWS("tables_day");
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " DAYSNumbers = {$DAYSNumbers}<br>\n";
        }
        //$GLOBALS["FULL_DEBUG"]
        $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT SUM(totalsize) as tsize FROM tables_day"));
        $totalsize = FormatBytes($ligne["tsize"] / 1024);
        $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT AVG(cache_perfs) as pourc FROM tables_day"));
        $pref = round($ligne["pourc"]);
        $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT COUNT(sitename) as tcount FROM visited_sites WHERE LENGTH(category)=0"));
        $websitesnumsNot = numberFormat($ligne["tcount"], 0, "", " ");
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " SELECT count(youtubeid),youtubeid FROM `youtube_dayz` GROUP BY youtubeid<br>\n";
        }
        $results = $q->QUERY_SQL("SELECT count(youtubeid),youtubeid FROM `youtube_dayz` GROUP BY youtubeid");
        $youtube_objects = mysql_num_rows($results);
        $youtube_objects = numberFormat($youtube_objects, 0, "", " ");
        $CachePermformance = $q->CachePerfHour();
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " CachePermformance = {$CachePermformance}<br>\n";
        }
        if ($CachePermformance > -1) {
            $color = "#E01313";
            if ($CachePermformance > 20) {
                $color = "#6DBB6A";
            }
            $cachePerfText = "\n\t\t\t<tr>\n\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t<td valign='top' style='font-size:12px;'><b style='color:{$color}'>{$CachePermformance}%</span></b><span style='font-size:12px'> {cache_performance} ({now})</td>\n\t\t\t</tr>\n\t\t\t";
        }
        if ($SquidActHasReverse == 1) {
            $TR_CAT_NUMBER = null;
        }
        $TR_CAT_NUMBER = "\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.categories.php')\"><b><span style='font-size:12px'>{$categories}</span></b><span style='font-size:12px'> {websites_categorized}{$license_inactive}</td>\n\t</tr>\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.categories.php')\"><span style='font-size:12px'>{youritems}: <b>{$YourItems}</span></b><span style='font-size:12px'></td>\n\t</tr>";
        $TR_CATZ = "\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td width=99% valign='top' style='font-size:12px;text-decoration:underline' \n\t\t{$mouse} OnClick=\"javascript:Loadjs('squid.traffic.statistics.days.php?js=yes&with-purge=yes')\"><b><span style='font-size:12px'>{$DAYSNumbers}</span></b><span style='font-size:12px'> {daysOfStatistics}</td>\n\t</tr>\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' style='font-size:12px'><b><span style='font-size:12px'>{$requests}</span></b><span style='font-size:12px'> {requests}</td>\n\t</tr>\n\t{$nodes}\n\t\t\n\t{$TR_CAT_NUMBER}\n";
        $TR_YOUTUBE = "\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline'\n\t\t{$mouse} OnClick=\"javascript:Loadjs('squid.youtube.all.php')\"\n\t\t><b><span style='font-size:12px'>{$youtube_objects}</span></b><span style='font-size:12px'> Youtube {objects}</td>\n\t</tr>";
        if ($DisableArticaProxyStatistics == 1) {
            $TR_YOUTUBE = null;
            $TR_CATZ = null;
        }
        if ($SquidActHasReverse == 1) {
            $TR_YOUTUBE = null;
        }
        $submenu = "\t\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' style='font-size:12px'><b><span style='font-size:12px'>{$totalsize}</span></b><span style='font-size:12px'> {downloaded_flow}</td>\n\t</tr>\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' style='font-size:12px'><b><span style='font-size:12px'>{$pref}%</span></b><span style='font-size:12px'> {cache_performance}</td>\n\t</tr>\n\t\n\t{$cachePerfText}";
        $main_table = "\n\t\t\n\t\t{$TR_CATZ}\n\t\t{$TR_YOUTUBE}\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$PhishingURIS}</span></b><span style='font-size:12px'> {phishing_uris}</td>\n\t</tr>\t\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$MalwaresURIS}</span></b><span style='font-size:12px'> {viruses_uris}</td>\n\t</tr>\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$MalwarePatrolDatabasesCount}</span></b><span style='font-size:12px'> Malware Patrol</td>\n\t</tr>\t\n\t\t\t\t\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.visited.php?onlyNot=yes')\"><b><span style='font-size:12px'>{$websitesnumsNot}</span></b><span style='font-size:12px'> {not_categorized}</td>\n\t</tr>\t\n\t\t\t\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.categories.php')\"><b><span style='font-size:12px'>{$tablescatNUM}</span></b><span style='font-size:12px'> {categories}</td>\n\t</tr>\t\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.categories.toexport.php')\"><b><span style='font-size:12px'>{$export}</span></b><span style='font-size:12px'> {websites_to_export}</td>\n\t</tr>";
    }
    if ($DisableArticaProxyStatistics == 1) {
        $main_table = "\t\n\t\t\t\n\t\t\t<tr>\n\t\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$PhishingURIS}</span></b><span style='font-size:12px'> {phishing_uris}</td>\n\t\t\t</tr>\t\n\t\t\t\n\t\t\t<tr>\n\t\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$MalwaresURIS}</span></b><span style='font-size:12px'> {viruses_uris}</td>\n\t\t\t</tr>\n\t\t\t\n\t\t\t<tr>\n\t\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$MalwarePatrolDatabasesCount}</span></b><span style='font-size:12px'> Malware Patrol</td>\n\t\t\t</tr>";
    }
    $addwebsites = "\n\t\t<tr>\n\t\t\t<td width=1%><img src='img/plus-16.png'></td>\n\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' \n\t\t\tOnClick=\"javascript:Loadjs('squid.visited.php?add-www=yes')\"><b><span style='font-size:12px'>{categorize_websites}</span></b><span style='font-size:12px'></td>\n\t\t</tr>\t";
    if ($users->PROXYTINY_APPLIANCE) {
        $addwebsites = null;
        $submenu = null;
    }
    $html = "\n<table style='width:100%'>\n\t<tbody>\n\t{$TR_ACCESSLOG}\n\t{$main_table}\t\n\t{$submenu}\n\t{$addwebsites}\n\t</tbody>\n\t</table>\n{$off}";
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " tpl->_ENGINE_parse_body<br>\n";
    }
    $html = $tpl->_ENGINE_parse_body($html);
    if (!$GLOBALS["AS_ROOT"]) {
        CACHE_SESSION_SET(__FUNCTION__, __FILE__, $html);
    }
}
function status_category_database(){
	$users=new usersMenus();
	$sock=new sockets();
	$tpl=new templates();
	$DisableArticaProxyStatistics=$sock->GET_INFO("DisableArticaProxyStatistics");
	if(!is_numeric($DisableArticaProxyStatistics)){$DisableArticaProxyStatistics=0;}
	if($DisableArticaProxyStatistics==1){return;}
	
	$CATZ_ARRAY=unserialize(@file_get_contents("/home/artica/categories_databases/CATZ_ARRAY"));
		
	$LOCAL_VERSION=$CATZ_ARRAY["TIME"];
	$title=$tpl->_ENGINE_parse_body("{APP_ARTICADB}");
	$q=new mysql_catz();
	$categories=$q->COUNT_CATEGORIES();
	if(!is_numeric($categories)){$categories=0;}
	$categories=numberFormat($categories,0,""," ");
	$SquidPerformance=intval($sock->GET_INFO("SquidPerformance"));
	
	if($SquidPerformance>0){
		$LOCAL_VERSION="{disabled}";
		$categories="{disabled}";
	}
	
	return Paragraphe32("noacco:$title", "
	<strong>Version</strong>:&nbsp;v.$LOCAL_VERSION<br>
	<strong>{items}</strong>:&nbsp;$categories<br>","Loadjs('squid.categories.php?onlyDB=yes',true)","database-link-32.png");
	
}
function categories_search($forceArtica = false)
{
    $MyPage = CurrentPageName();
    $page = CurrentPageName();
    $tpl = new templates();
    $sock = new sockets();
    $q = new mysql_squid_builder();
    $dans = new dansguardian_rules();
    $EnableWebProxyStatsAppliance = $sock->GET_INFO("EnableWebProxyStatsAppliance");
    if (!is_numeric($EnableWebProxyStatsAppliance)) {
        $EnableWebProxyStatsAppliance = 0;
    }
    $t = $_GET["t"];
    $artica = $forceArtica;
    if (isset($_GET["artica"])) {
        $artica = true;
    }
    $tableSchema = "squidlogs";
    if (!$q->BD_CONNECT()) {
        json_error_show("Testing connection to MySQL server failed...", 1);
    }
    if (!$q->TABLE_EXISTS("webfilters_categories_caches")) {
        $q->CheckTables();
    }
    $dans = new dansguardian_rules();
    $dans->LoadBlackListes();
    $sql = "SELECT * FROM personal_categories";
    if (!$q->TABLE_EXISTS("personal_categories")) {
        json_error_show("personal_categories no such table!", 1);
    }
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        json_error_show("Mysql Error [" . __LINE__ . "]: {$q->mysql_error}", 1);
    }
    while ($ligne = mysql_fetch_array($results, MYSQL_ASSOC)) {
        $PERSONALSCATS[$ligne["category"]] = $ligne["category_description"];
    }
    $search = '%';
    $page = 1;
    $ORDER = "ORDER BY table_name";
    $searchstring = "table_name LIKE 'category_%'";
    if (isset($_POST["sortname"])) {
        if ($_POST["sortname"] != null) {
            $ORDER = "ORDER BY {$_POST["sortname"]} {$_POST["sortorder"]}";
        }
    }
    if ($artica) {
        $q = new mysql_catz();
        writelogs("Artica mode -> {$q->database} " . $q->COUNT_CATEGORIES(), __FUNCTION__, __FILE__, __LINE__);
        $tableSchema = "catz";
    }
    if (isset($_POST['page'])) {
        $page = $_POST['page'];
    }
    if ($_POST["query"] != null) {
        $_POST["query"] = "*{$_POST["query"]}*";
        $_POST["query"] = str_replace("**", "*", $_POST["query"]);
        $_POST["query"] = str_replace("**", "*", $_POST["query"]);
        $_POST["query"] = str_replace("*", "%", $_POST["query"]);
        $search = $_POST["query"];
        $searchstring = "table_name LIKE 'category_{$search}'";
        $sql = "SELECT COUNT( table_name ) AS tcount FROM information_schema.tables WHERE table_schema = '{$tableSchema}' AND table_name LIKE 'category_{$search}'";
        writelogs($sql, __FUNCTION__, __FILE__, __LINE__);
        $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup"));
        if (!$q->ok) {
            json_error_show("Mysql Error [" . __LINE__ . "]: {$q->mysql_error}", 1);
        }
        $total = $ligne["tcount"];
    } else {
        $sql = "SELECT COUNT(table_name) as TCOUNT FROM information_schema.tables WHERE table_schema = '{$tableSchema}' AND table_name LIKE 'category_%'";
        $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup"));
        if (!$q->ok) {
            json_error_show("Mysql Error [" . __LINE__ . "]: {$q->mysql_error}", 1);
        }
        $total = $ligne["TCOUNT"];
        writelogs("{$sql} = {$total}", __FUNCTION__, __FILE__, __LINE__);
    }
    if (isset($_POST['rp'])) {
        $rp = $_POST['rp'];
    }
    $pageStart = ($page - 1) * $rp;
    $limitSql = "LIMIT {$pageStart}, {$rp}";
    $sql = "SELECT table_name as c,TABLE_ROWS FROM information_schema.tables WHERE table_schema = '{$tableSchema}' AND {$searchstring} {$ORDER} {$limitSql}";
    writelogs("{$q->mysql_admin}:{$q->mysql_password}:{$sql}", __FUNCTION__, __FILE__, __LINE__);
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        json_error_show("Mysql Error [" . __LINE__ . "]: {$q->mysql_error}", 1);
    }
    if (mysql_num_rows($results) == 0) {
        if (!$artica) {
            categories_search(true);
            return;
        }
    }
    if (mysql_num_rows($results) == 0) {
        json_error_show("({$tableSchema}) No categories table found...", 1);
    }
    $data = array();
    $data['page'] = $page;
    $data['total'] = $total;
    $data['rows'] = array();
    $enc = new mysql_catz();
    while ($ligne = mysql_fetch_assoc($results)) {
        $table = $ligne["c"];
        writelogs("Scanning table {$table}", __FUNCTION__, __FILE__, __LINE__);
        $select = imgtootltip("32-parameters.png", "{apply}", "DansGuardianEditMember('{$ligne["ID"]}','{$ligne["pattern"]}')");
        $items = $q->COUNT_ROWS($ligne["c"]);
        $itemsEnc = $enc->COUNT_ROWS($ligne["c"]);
        if (!preg_match("#^category_(.+)#", $table, $re)) {
            continue;
        }
        $categoryname = $re[1];
        $compile = imgsimple("compile-distri-32.png", "{saveToDisk}", "DansGuardianCompileDB('{$categoryname}')");
        if (!isset($dans->array_blacksites[$categoryname])) {
            if (isset($dans->array_blacksites[str_replace("_", "-", $categoryname)])) {
                $categoryname = str_replace("_", "-", $categoryname);
            }
            if (isset($dans->array_blacksites[str_replace("_", "/", $categoryname)])) {
                $categoryname = str_replace("_", "/", $categoryname);
            }
        }
        if ($dans->array_pics[$categoryname] != null) {
            $pic = "<img src='img/{$dans->array_pics[$categoryname]}'>";
        } else {
            $pic = "&nbsp;";
        }
        $sizedb_org = $q->TABLE_SIZE($table);
        $sizedb = FormatBytes($sizedb_org / 1024);
        $linkcat = "<a href=\"javascript:blur();\" OnClick=\"javascript:Loadjs('squid.categories.php?category={$categoryname}&t={$t}',true)\"\n\t\tstyle='font-size:14px;font-weight:bold;color:{$color};text-decoration:underline'>";
        $text_category = $dans->array_blacksites[$categoryname];
        if (isset($PERSONALSCATS[$categoryname])) {
            $text_category = utf8_encode($PERSONALSCATS[$categoryname]);
            if ($pic == "&nbsp;") {
                $pic = "<img src='img/20-categories-personnal.png'>";
            }
            $linkcat = "<a href=\"javascript:blur();\" OnClick=\"javascript:Loadjs('{$MyPage}?add-perso-cat-js=yes&cat={$categoryname}&t={$t}',true)\"\n\t\t\tstyle='font-size:14px;font-weight:bold;color:{$color};text-decoration:underline'>";
        }
        if ($EnableWebProxyStatsAppliance == 0) {
            if ($sizedb_org < 35) {
                $pic = "<img src='img/warning-panneau-32.png'>";
            }
        }
        $viewDB = imgsimple("mysql-browse-database-32.png", "{view}", "javascript:Loadjs('squid.categories.php?category={$categoryname}',true)");
        $categoryText = $tpl->_ENGINE_parse_body("<div style='font-size:14px';font-weight:bold'>{$linkcat}{$categoryname}</div>\n\t\t</a><div style='font-size:11px;width:100%;font-weight:normal'>{$text_category}</div>");
        $items = numberFormat($items, 0, "", " ");
        $itemsEnc = numberFormat($itemsEnc, 0, "", " ");
        $compile = imgsimple("compile-distri-32.png", "{saveToDisk} {$categoryname}", "DansGuardianCompileDB('{$categoryname}')");
        $delete = imgsimple("delete-32.png", "{delete}", "TableCategoryPurge('{$table}')");
        if ($_GET["minisize"] == "yes") {
            $delete = null;
        }
        $cell = array();
        $cell[] = $pic;
        $cell[] = $categoryText;
        $cell[] = "<div style='font-size:13px;padding-top:15px;font-weight:bold'>{$sizedb}</div>";
        if (!$artica) {
            $cell[] = "<div style='font-size:13px;padding-top:5px;font-weight:bold'>{$items}<br>{$itemsEnc}</strong>";
        } else {
            $cell[] = "<div style='font-size:13px;padding-top:15px;font-weight:bold'>-</strong>";
            $cell[] = "<div style='font-size:13px;padding-top:15px;font-weight:bold'>{$items}</strong>";
        }
        $cell[] = $compile;
        $cell[] = $delete;
        $data['rows'][] = array('id' => $ligne['ID'], 'cell' => $cell);
    }
    echo json_encode($data);
}
function statusDB()
{
    $tpl = new templates();
    $page = CurrentPageName();
    $users = new usersMenus();
    $sock = new sockets();
    $backbutton = null;
    if ($_GET["from-ufdbguard"] == "yes") {
        echo $tpl->_ENGINE_parse_body("\n\t\t\t\t<div style='margin:15px;text-align:right'>\n\t\t\t\t" . button("{back_to_webfiltering}", "AnimateDiv('BodyContent');LoadAjax('BodyContent','dansguardian2.mainrules.php')", 18) . "\n\t\t\t\t</div>");
    }
    $q = new mysql_catz();
    $sock = new sockets();
    $ini = new Bs_IniHandler();
    $catz = $q->LIST_TABLES_CATEGORIES();
    $sock->getFrameWork('cmd.php?squid-ini-status=yes');
    $ini->loadFile("/usr/share/artica-postfix/ressources/databases/ALL_SQUID_STATUS");
    $CATZ_ARRAY = unserialize(@file_get_contents("/home/artica/categories_databases/CATZ_ARRAY"));
    $date = $CATZ_ARRAY["TIME"];
    $LOCAL_VERSION = $CATZ_ARRAY["TIME"];
    $title = $tpl->_ENGINE_parse_body("{APP_ARTICADB}");
    $q = new mysql_catz();
    $LOCAL_VERSION_TEXT = $tpl->time_to_date($date);
    $CountDecategories = intval(@file_get_contents("/usr/share/artica-postfix/ressources/UFDB_ARTICA_COUNT"));
    $CountDeDatabases = intval(@file_get_contents("/usr/share/artica-postfix/ressources/UFDB_ARTICA_DBS"));
    if (!is_numeric($CountDecategories)) {
        $CountDecategories = 0;
    }
    $CountDecategories = numberFormat($CountDecategories, 0, "", " ");
    $APP_SQUID_DB = DAEMON_STATUS_ROUND("APP_SQUID_DB", $ini, null, 1);
    $APP_UFDBCAT = DAEMON_STATUS_ROUND("APP_UFDBCAT", $ini, null, 1);
    $DisableCategoriesDatabasesUpdates = intval($sock->GET_INFO("DisableCategoriesDatabasesUpdates"));
    $CategoriesDatabasesUpdatesAllTimes = intval($sock->GET_INFO("CategoriesDatabasesUpdatesAllTimes"));
    $CategoriesDatabasesByCron = $sock->GET_INFO("CategoriesDatabasesByCron");
    if (!is_numeric($CategoriesDatabasesByCron)) {
        $CategoriesDatabasesByCron = 1;
    }
    $CategoriesDatabasesShowIndex = $sock->GET_INFO("CategoriesDatabasesShowIndex");
    if (!is_numeric($CategoriesDatabasesShowIndex)) {
        $CategoriesDatabasesShowIndex = 1;
    }
    $DisableArticaProxyStatistics = $sock->GET_INFO("DisableArticaProxyStatistics");
    if (!is_numeric($DisableArticaProxyStatistics)) {
        $DisableArticaProxyStatistics = 0;
    }
    $fbdize = unserialize(@file_get_contents("/usr/share/artica-postfix/ressources/logs/web/categories-db.size.db"));
    $DBSIZE = FormatBytes($fbdize["DBSIZE"]);
    $POURC = $fbdize["POURC"];
    if (is_numeric($POURC)) {
        $POURC_TXT = "&nbsp;{$POURC}% {used}";
    }
    $p2 = Paragraphe_switch_img("{disable_udpates}", "{disable_udpates_explain}<br>{APP_ARTICADB_EXPLAIN}", "DisableCategoriesDatabasesUpdates", $DisableCategoriesDatabasesUpdates, null, 700);
    $p3 = Paragraphe_switch_img("{free_update_during_the_day}", "{free_update_during_the_day_explain}", "CategoriesDatabasesUpdatesAllTimes", $CategoriesDatabasesUpdatesAllTimes, null, 700);
    $p = Paragraphe_switch_img("{update_only_by_schedule}", "{articadb_update_only_by_schedule}", "CategoriesDatabasesByCron", $CategoriesDatabasesByCron, null, 700);
    $tt0[] = "<tr><td colspan=2>{$p3}</td></tr>";
    $tt0[] = "<tr><td colspan=2>{$p2}</td></tr>";
    $tt0[] = "<tr><td colspan=2>{$p}</td></tr>";
    $tt0[] = "<tr>\n\t\t\t<td width=1%>" . Field_checkbox_design("CategoriesDatabasesShowIndex", 1, $CategoriesDatabasesShowIndex, "CategoriesDatabasesByCron()") . "</td>\n\t\t\t<td nowrap style='font-size:14px;'>:{display_update_info_index}</a></td>\n\t\t</tr>\n\t\t<tr><td colspan=2 align='right'>" . button("{apply}", "CategoriesDatabasesByCron()", 22) . "</td></tr>\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t";
    /*$tt[]="<tr>
    		<td width=1%><img src='img/arrow-right-16.png'>
    		<td nowrap><a href=\"javascript:blur();\"
    		OnClick=\"javascript:Loadjs('squid.catzdb.manual-update.php');\"
    		style='font-size:14px;text-decoration:underline;'>{manual_update}</a></td>
    		</tr>";		
    		*/
    /*$tt[]="<tr>
    		<td width=1%><img src='img/arrow-right-16.png'>
    		<td nowrap><a href=\"javascript:blur();\"
    		OnClick=\"javascript:Loadjs('squid.catzdb.changedir.php');\"
    		style='font-size:14px;text-decoration:underline;'>{change_directory}</a></td>
    		</tr>";	
    		*/
    if ($DisableArticaProxyStatistics == 1) {
        $tt[] = "<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'>\n\t\t<td nowrap><a href=\"javascript:blur();\"\n\t\tOnClick=\"javascript:Loadjs('squid.artica.statistics.php');\"\n\t\tstyle='font-size:14px;text-decoration:underline;color:#D10000'>{ARTICA_STATISTICS} {disabled}</a></td>\n\t\t</tr>";
    } else {
        $tt[] = "<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'>\n\t\t<td nowrap><a href=\"javascript:blur();\"\n\t\tOnClick=\"javascript:Loadjs('squid.artica.statistics.php');\"\n\t\tstyle='font-size:14px;text-decoration:underline;color:black'>{ARTICA_STATISTICS}</a></td>\n\t\t</tr>";
    }
    $arrayV = unserialize(base64_decode($sock->getFrameWork("squid.php?articadb-nextversion=yes")));
    $REMOTE_VERSION = $arrayV["TIME"];
    if ($REMOTE_VERSION > $date) {
        $REMOTE_VERSION_TEXT = $tpl->time_to_date($REMOTE_VERSION);
        $newver = "\t<tr>\n\t\t<td colspan=2><div style='font-size:16px;color:#D52210'>{new_version}:&nbsp;{$REMOTE_VERSION} <i style='font-size:11px'>{$REMOTE_VERSION_TEXT}</i>&nbsp</div></td>\n\t</tr>";
        $updaebutton = "<div style='text-align:right'><hr>" . button("{update_now}", "Loadjs('squid.blacklist.upd.php')", 22) . "</div>";
    }
    $nextcheck = $sock->getFrameWork("squid.php?articadb-nextcheck=yes");
    $nextcheck = intval($nextcheck);
    if ($nextcheck > 0) {
        $nextcheck_text = "\t\n\t<tr>\n\t\t<td colspan=2><div style='font-size:16px'>{next_check_in}:&nbsp;{$nextcheck}Mn</div></td>\n\t</tr>";
    }
    if ($nextcheck < 0) {
        $nextcheck = str_replace("-", "", $nextcheck);
        $nextcheckTime = time() - intval($nextcheck) * 60;
        $nextcheckTimeText = distanceOfTimeInWords($nextcheckTime, time());
        $nextcheck_text = "\t\n\t<tr>\n\t\t<td colspan=2><div style='font-size:16px'>{last_check}:&nbsp;{$nextcheckTimeText}</div></td>\n\t</tr>";
    }
    $dbsize = $sock->getFrameWork("squid.php?articadbsize=yes");
    $items = numberFormat($q->COUNT_CATEGORIES(), 0, "", " ");
    $html = "\n\t<div style='width:98%' class=form>\n\t\n\t<table style='width:100%'>\n\t<tr>\n\t<td valign='top'>{$APP_SQUID_DB}<br>{$APP_UFDBCAT}</td>\n\t<td valign='top'>\n\t<table style='width:100%'>" . @implode("\n", $tt0) . "</table>\n\t<hr>\n\t<table style='width:100%'>\n\t<tbody>\n\t<tr>\n\t\t<td colspan=2><div style='font-size:16px'>{pattern_database_version}:&nbsp;{$date} <i style='font-size:11px'>{$LOCAL_VERSION_TEXT}</i>&nbsp{$POURC_TXT}</div></td>\n\t</tr>\n\t{$newver}\n\t{$nextcheck_text}\n\t<tr>\n\t\t<td colspan=2><div style='font-size:16px'>{categories}:&nbsp;{$CountDeDatabases}</a></div></td>\n\t</tr>\n\t<tr>\n\t\t<td colspan=2><div style='font-size:16px'>{categorized_websites}:&nbsp;\n\t\t<a href=\"javascript:Loadjs('squid.catz.php');\" style='font-size:16px;text-decoration:underline'>\n\t\t{$CountDecategories}</a>&nbsp</div></td>\n\t</tr>\n\t" . @implode("", $tt) . "\n\t</tbody>\n\t</table>\n\t</td>\n\t</tr>\n\t</table>\n\t{$updaebutton}\n\t<div id='database-progress-status'></div>\n<script>\nvar xCategoriesDatabasesByCron= function (obj) {\n\tvar results=obj.responseText;\n\tif(results.length>1){alert(results);}\n}\n\t\nfunction CategoriesDatabasesByCron(){\n\tvar XHR = new XHRConnection();\n\tXHR.appendData('CategoriesDatabasesByCron',document.getElementById('CategoriesDatabasesByCron').value);\n\tXHR.appendData('DisableCategoriesDatabasesUpdates',document.getElementById('DisableCategoriesDatabasesUpdates').value);\n\tXHR.appendData('CategoriesDatabasesUpdatesAllTimes',document.getElementById('CategoriesDatabasesUpdatesAllTimes').value);\n\tif(document.getElementById('CategoriesDatabasesShowIndex').checked){XHR.appendData('CategoriesDatabasesShowIndex','1');}else{XHR.appendData('CategoriesDatabasesShowIndex','0');}\n\tXHR.sendAndLoad('{$page}', 'POST',xCategoriesDatabasesByCron);\n}\n\t\n\n</script>";
    echo $tpl->_ENGINE_parse_body($html);
}
        $GLOBALS["DEBUG_MEM"] = true;
    }
    if (preg_match("#--old#", implode(" ", $argv))) {
        $GLOBALS["OLD"] = true;
    }
    if (preg_match("#--force#", implode(" ", $argv))) {
        $GLOBALS["FORCE"] = true;
    }
    if (preg_match("#--rebuild#", implode(" ", $argv))) {
        $GLOBALS["REBUILD"] = true;
    }
}
if ($GLOBALS["VERBOSE"]) {
    ini_set('display_errors', 1);
    ini_set('html_errors', 0);
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
}
include_once dirname(__FILE__) . '/ressources/class.templates.inc';
include_once dirname(__FILE__) . '/ressources/class.ccurl.inc';
include_once dirname(__FILE__) . '/ressources/class.ini.inc';
include_once dirname(__FILE__) . '/ressources/class.mysql.inc';
include_once dirname(__FILE__) . '/framework/class.unix.inc';
include_once dirname(__FILE__) . '/ressources/class.squid.inc';
include_once dirname(__FILE__) . '/ressources/class.os.system.inc';
include_once dirname(__FILE__) . "/framework/frame.class.inc";
include_once dirname(__FILE__) . '/ressources/whois/whois.main.php';
$q = new mysql_catz();
echo "Count Categories:...<br>\n";
echo $q->COUNT_CATEGORIES();
echo "<br>\n";