function rules_table_list()
{
    $tpl = new templates();
    $MyPage = CurrentPageName();
    $q = new mysql_squid_builder();
    $webfilter = new webfilter_rules();
    $sock = new sockets();
    $EnableGoogleSafeSearch = $sock->GET_INFO("EnableGoogleSafeSearch");
    if (!is_numeric($EnableGoogleSafeSearch)) {
        $EnableGoogleSafeSearch = 1;
    }
    $t = $_GET["t"];
    $search = '%';
    $table = "webfilter_rules";
    $page = 1;
    $FORCE_FILTER = null;
    $total = 0;
    if (!$q->TABLE_EXISTS($table)) {
        $q->CheckTables();
    }
    if (isset($_POST["sortname"])) {
        if ($_POST["sortname"] != null) {
            $ORDER = "ORDER BY {$_POST["sortname"]} {$_POST["sortorder"]}";
        }
    }
    if (isset($_POST['page'])) {
        $page = $_POST['page'];
    }
    $searchstring = string_to_flexquery();
    if ($searchstring != null) {
        $sql = "SELECT COUNT(*) as TCOUNT FROM `{$table}` WHERE 1 {$FORCE_FILTER} {$searchstring}";
        $ligne = mysql_fetch_array($q->QUERY_SQL($sql));
        $total = $ligne["TCOUNT"];
    } else {
        $sql = "SELECT COUNT(*) as TCOUNT FROM `{$table}` WHERE 1 {$FORCE_FILTER}";
        $ligne = mysql_fetch_array($q->QUERY_SQL($sql));
        $total = $ligne["TCOUNT"] + 1;
    }
    if (isset($_POST['rp'])) {
        $rp = $_POST['rp'];
    }
    if (!is_numeric($rp)) {
        $rp = 50;
    }
    $pageStart = ($page - 1) * $rp;
    $limitSql = "LIMIT {$pageStart}, {$rp}";
    $sql = "SELECT *  FROM `{$table}` WHERE 1 {$searchstring} {$FORCE_FILTER} {$ORDER} {$limitSql}";
    $results = $q->QUERY_SQL($sql);
    if ($GLOBALS["VERBOSE"]) {
        echo "{$sql}<br>\n";
    }
    writelogs($sql . " ==> " . mysql_num_rows($results) . " items", __FUNCTION__, __FILE__, __LINE__);
    $ligne = unserialize(base64_decode($sock->GET_INFO("DansGuardianDefaultMainRule")));
    $DefaultPosition = $ligne["defaultPosition"];
    if (!is_numeric($DefaultPosition)) {
        $DefaultPosition = 0;
    }
    $data = array();
    $data['page'] = $page;
    $data['total'] = $total + 1;
    $data['rows'] = array();
    $tmplate = $tpl->_ENGINE_parse_body("{template}");
    if (!$q->ok) {
        json_error_show("{$q->mysql_error}");
    }
    $AllSystems = $tpl->_ENGINE_parse_body("{AllSystems}");
    if ($DefaultPosition == 0) {
        $data['rows'][] = DefaultRule();
    }
    $no_category_has_been_added = $tpl->_ENGINE_parse_body("{no_category_has_been_added}");
    $endofrule_TEXTS["any"] = "<i style='color:#000000;margin-top:8px;font-weight:bold;font-size:16px'>" . $tpl->_ENGINE_parse_body("{ufdb_explain_any}") . "</i>";
    $endofrule_TEXTS["none"] = "<i style='color:#d32d2d;margin-top:8px;font-weight:bold;font-size:16px'>" . $tpl->_ENGINE_parse_body("{ufdb_explain_none}") . "</i>";
    while ($ligne = mysql_fetch_assoc($results)) {
        $ID = $ligne["ID"];
        $md5 = md5($ligne["ID"]);
        $endofrule_text = null;
        $EnableGoogleSafeSearch_text = null;
        $ligne["groupname"] = utf8_encode($ligne["groupname"]);
        $endofrule = $ligne["endofrule"];
        if ($endofrule == null) {
            $endofrule = "any";
        }
        $MAIN_EXPLAIN_TR = array();
        $delete = imgtootltip("delete-32.png", "{delete}", "DansGuardianDeleteMainRule('{$ligne["ID"]}')");
        $js = "DansGuardianEditRule('{$ligne["ID"]}','{$ligne["groupname"]}');";
        if ($GLOBALS["VERBOSE"]) {
            echo "<HR>webfilter->rule_time_list_from_ruleid({$ligne["ID"]})<HR><br>\n";
        }
        $CountDeBlack = intval($webfilter->COUNTDEGBLKS($ligne["ID"]));
        $CountDeWhite = intval($webfilter->COUNTDEGBWLS($ligne["ID"]));
        $CountDeAll = intval($CountDeBlack + $CountDeWhite);
        if ($CountDeAll == 0) {
            $color = "#d32d2d";
            $MAIN_EXPLAIN_TR[] = "<i style='color:#d32d2d;margin-top:8px;font-weight:bold;font-size:16px'>{$no_category_has_been_added}</i>";
        }
        $color = "black";
        if ($ligne["enabled"] == 0) {
            $color = "#8a8a8a";
        }
        if ($ligne["groupmode"] == 0) {
            $MAIN_EXPLAIN_TR[] = "<i style='color:#d32d2d;margin-top:8px;font-weight:bold;font-size:16px'>{all_websites_are_banned}</span>";
        }
        if ($ligne["groupmode"] == 2) {
            $MAIN_EXPLAIN_TR[] = "<i style='color:#46a346;margin-top:8px;font-weight:bold;font-size:16px'>{everything_is_allowed}</span>";
        }
        $duplicate = imgsimple("duplicate-32.png", null, "Loadjs('dansguardian2.duplicate.php?from={$ligne['ID']}&t={$t}')");
        $jsGroups = "<a href=\"javascript:blur();\" \n\t\tOnClick=\"javascript:document.getElementById('anim-img-{$ligne["ID"]}').innerHTML='<img src=img/wait.gif>';Loadjs('dansguardian2.edit.php?js-groups={$ligne["ID"]}&ID={$ligne["ID"]}&t={$t}');\"\n\t\tstyle='text-decoration:underline;font-weight:bold'>";
        $jsblack = "<a href=\"javascript:blur();\"\n\tOnClick=\"javascript:document.getElementById('anim-img-{$ligne['ID']}').innerHTML='<img src=img/wait.gif>';Loadjs('dansguardian2.edit.php?js-blacklist-list=yes&RULEID={$ligne['ID']}&modeblk=0&group=&TimeID=&t={$t}');\"\n\tstyle='text-decoration:underline;font-weight:bold'>";
        $jswhite = "<a href=\"javascript:blur();\"\n\tOnClick=\"javascript:document.getElementById('anim-img-{$ligne['ID']}').innerHTML='<img src=img/wait.gif>';Loadjs('dansguardian2.edit.php?js-blacklist-list=yes&RULEID={$ligne['ID']}&modeblk=1&group=&TimeID=&t={$t}');\"\n\tstyle='text-decoration:underline;font-weight:bold'>";
        $TimeSpace = $webfilter->rule_time_list_explain($ligne["TimeSpace"], $ligne["ID"], $t);
        $TimeSpace = str_replace('\\n\\n', "<br>", $TimeSpace);
        $styleupd = "style='border:0px;margin:0px;padding:0px;background-color:transparent'";
        $up = imgsimple("arrow-up-32.png", "", "RuleDansUpDown('{$ligne['ID']}',1)");
        $down = imgsimple("arrow-down-32.png", "", "RuleDansUpDown('{$ligne['ID']}',0)");
        $zorder = "<table {$styleupd}><tr><td {$styleupd}>{$down}</td {$styleupd}><td {$styleupd}>{$up}</td></tr></table>";
        $CountDeGroups = "&laquo;&nbsp;{$jswhite}{$jsGroups}" . $webfilter->COUNTDEGROUPES($ligne["ID"]) . "</a>&nbsp;&raquo;";
        if ($ligne["AllSystems"] == 1) {
            $jsGroups = "*";
            $CountDeGroups = "*";
        }
        $MAIN_EXPLAIN_TR[] = $endofrule_TEXTS[$endofrule];
        if ($TimeSpace != null) {
            $MAIN_EXPLAIN_TR[] = $TimeSpace;
        }
        if ($EnableGoogleSafeSearch == 0) {
            if ($ligne["GoogleSafeSearch"] == 1) {
                $MAIN_EXPLAIN_TR[] = "<i style='color:#00A940;font-weight:bold;font-size:16px'>" . $tpl->javascript_parse_text("{EnableGoogleSafeSearch}") . "</i>";
            }
        }
        $MAIN_EXPLAIN_TEXT = $tpl->_ENGINE_parse_body("<br>" . @implode("<br>", $MAIN_EXPLAIN_TR));
        if ($ligne["enabled"] == 0) {
            $MAIN_EXPLAIN_TEXT = null;
        }
        $data['rows'][] = array('id' => $ligne['ID'], 'cell' => array("<span id='anim-img-{$ligne["ID"]}'></span>\n\t\t\t\t<a href=\"javascript:blur();\" OnClick=\"javascript:{$js}\" \n\t\t\t\tstyle='font-size:24px;color:{$color};text-decoration:underline'>{$ligne["groupname"]}</a>{$MAIN_EXPLAIN_TEXT}", "<span style='font-size:24px;color:{$color};'>{$CountDeGroups}</span>", "<span style='font-size:24px;color:{$color};'>&laquo;&nbsp;{$jsblack}{$CountDeBlack}</a>&nbsp;&raquo;</span>", "<span style='font-size:24px;color:{$color};'>&laquo;&nbsp;{$jswhite}{$CountDeWhite}</a>&nbsp;&raquo;</span>", "<center>{$zorder}</center>", "<center>{$duplicate}</center>", "<center>{$delete}</center>"));
    }
    if ($DefaultPosition == 1) {
        $data['rows'][] = DefaultRule();
    }
    echo json_encode($data);
}
function rules_search()
{
    $tpl = new templates();
    $MyPage = CurrentPageName();
    $page = CurrentPageName();
    $rule_text = $tpl->_ENGINE_parse_body("{rule}");
    $action_delete_rule = $tpl->javascript_parse_text("{action_delete_rule}");
    $q = new mysql_squid_builder();
    if (!$q->FIELD_EXISTS("webfilter_rules", "zOrder")) {
        $q->QUERY_SQL("ALTER TABLE `webfilter_rules` ADD `zOrder` SMALLINT(2) NOT NULL,ADD INDEX ( `zOrder` )");
    }
    if (!$q->ok) {
        json_error_show("{$q->mysql_error}");
    }
    if (!$q->FIELD_EXISTS("webfilter_rules", "AllSystems")) {
        $q->QUERY_SQL("ALTER TABLE `webfilter_rules` ADD `AllSystems` smallint(1),ADD INDEX ( `AllSystems` )");
    }
    if (!$q->ok) {
        json_error_show("{$q->mysql_error}");
    }
    $t = $_GET["t"];
    $search = '%';
    $table = "webfilter_rules";
    $page = CurrentPageName();
    $FORCE_FILTER = null;
    $total = 0;
    if (!$q->TABLE_EXISTS($table)) {
        $q->CheckTables();
    }
    $searchstring = string_to_flexquery("rules-search");
    $webfilter = new webfilter_rules();
    $styleTD = "style='font-size:16px;font-weight:bold'";
    $styleTDCenter = "style='font-size:16px;font-weight:bold;text-align:center !important'";
    $sql = "SELECT *  FROM `{$table}` WHERE 1 {$searchstring} {$FORCE_FILTER} ORDER by zOrder";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        senderror("{$q->mysql_error}");
    }
    $sock = new sockets();
    $ligne = unserialize(base64_decode($sock->GET_INFO("DansGuardianDefaultMainRule")));
    $DefaultPosition = $ligne["defaultPosition"];
    if (!is_numeric($DefaultPosition)) {
        $DefaultPosition = 0;
    }
    $AllSystems = $tpl->_ENGINE_parse_body("{AllSystems}");
    $boot = new boostrap_form();
    if ($DefaultPosition == 0) {
        $tr[] = DefaultRule();
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        $ID = $ligne["ID"];
        $md5 = md5($ligne["ID"]);
        $ligne["groupname"] = utf8_encode($ligne["groupname"]);
        $delete = imgtootltip("delete-24.png", "{delete}", "DansGuardianDeleteMainRule('{$ligne["ID"]}')");
        $js = "DansGuardianEditRule('{$ligne["ID"]}','{$ligne["groupname"]}');";
        $link = $boot->trswitch($js);
        $link_blacklist = $boot->trswitch("Loadjs('dansguardian2.edit.php?js-blacklist-list=yes&RULEID={$ligne['ID']}&modeblk=0&group=&TimeID=&t={$t}');");
        $link_whitelist = $boot->trswitch("Loadjs('dansguardian2.edit.php?js-blacklist-list=yes&RULEID={$ligne['ID']}&modeblk=1&group=&TimeID=&t={$t}');");
        $link_group = $boot->trswitch("Loadjs('dansguardian2.edit.php?js-groups={$ligne["ID"]}&ID={$ligne["ID"]}&t={$t}')");
        $TimeSpace = $webfilter->TimeToText(unserialize(base64_decode($ligne["TimeSpace"])));
        $color = "black";
        if ($ligne["enabled"] == 0) {
            $color = "#8a8a8a";
        }
        $rules_dans_time_rule = $webfilter->rules_dans_time_rule($ligne["ID"]);
        if ($ligne["groupmode"] == 0) {
            $warn = "<div style='float:right'><img src='img/stop-24.png'></div>";
        }
        $duplicate = imgsimple("duplicate-24.png", null, "Loadjs('dansguardian2.duplicate.php?from={$ligne['ID']}&t={$t}')");
        $TimeSpace = $webfilter->rule_time_list_explain($ligne["TimeSpace"], $ligne["ID"], $t);
        $styleupd = "style='border:0px;margin:0px;padding:0px;background-color:transparent'";
        $up = imgsimple("arrow-up-32.png", "", "RuleDansUpDown('{$ligne['ID']}',1)");
        $down = imgsimple("arrow-down-32.png", "", "RuleDansUpDown('{$ligne['ID']}',0)");
        $zorder = "<table {$styleupd}><tr><td {$styleupd}>{$down}</td {$styleupd}><td {$styleupd}>{$up}</td></tr></table>";
        $CountDeGroups = "&laquo;&nbsp;" . $webfilter->COUNTDEGROUPES($ligne["ID"]) . "&nbsp;&raquo;";
        $templatejs = $boot->trswitch("Loadjs('dansguardian.template.php?js=yes&ID={$ligne["ID"]}')");
        if ($ligne["AllSystems"] == 1) {
            $jsGroups = "*";
            $CountDeGroups = "*";
        }
        $tr[] = "\n\t\t\t<tr id='{$ligne['ID']}'>\n\t\t\t<td {$styleTD} {$link} width=99% ><span id='anim-img-0'>{$ligne["groupname"]}</span> {$TimeSpace}</td>\n\t\t\t<td {$styleTDCenter} {$link_group} width=1% align=center>{$CountDeGroups}</td>\n\t\t\t<td {$styleTDCenter} {$link_blacklist} width=1% align=center>" . $webfilter->COUNTDEGBLKS($ligne['ID']) . "</td>\n\t\t\t<td {$styleTDCenter} {$link_whitelist} width=1% align=center>" . $webfilter->COUNTDEGBWLS($ligne['ID']) . "</td>\n\t\t\t<td {$styleTDCenter} width=35px align=center nowrap>{$zorder}</td>\n\t\t\t<td {$styleTDCenter} {$templatejs} width=35px align=center nowrap><img src='img/banned-template-32.png'></td>\n\t\t\t<td {$styleTDCenter} width=35px align=center nowrap>{$duplicate}</td>\n\t\t\t<td width=35px align='center' nowrap {$styleTDCenter}>{$delete}</td>\n\t\t\t</tr>";
    }
    if ($DefaultPosition == 1) {
        $tr[] = DefaultRule();
    }
    $table = $tpl->_ENGINE_parse_body("\n\t\t\t<table class='table table-bordered table-hover'>\n\t\t\t<thead>\n\t\t\t<tr>\n\t\t\t<th>{rule}</th>\n\t\t\t<th>{groups2}</th>\n\t\t\t<th>{blacklists}</th>\n\t\t\t<th>{whitelists}</th>\n\t\t\t<th>{order}</th>\n\t\t\t<th>{template}</th>\n\t\t\t<th>&nbsp;</th>\n\t\t\t<th>&nbsp;</th>\n\t\t\t</tr>\n\t\t\t</thead>\n\t\t\t<tbody>\n\t\t\t") . @implode("\n", $tr);
    $js = "\n<script>\t\t\t\nfunction DansGuardianNewRule(){\n\t\tDansGuardianEditRule(-1)\n\t}\n\n\tfunction DansGuardianEditRule(ID,rname){\n\t\tYahooWin3('1010','dansguardian2.edit.php?ID='+ID+'&t={$t}','{$rule_text}::'+ID+'::'+rname);\n\t}\n\t\n\tfunction CompileUfdbGuardRules(){\n\t\tLoadjs('dansguardian2.compile.php');\n\t}\n\t\n\tfunction UfdbGuardConfigs(){\n\t\tLoadjs('ufdbguard.php');\n\t}\n\t\n\tfunction UfdbguardEvents(){\n\t\tLoadjs('dansguardian2.mainrules.php?UfdbguardEvents=yes');\n\t}\n\tvar x_RuleDansUpDown{$t}= function (obj) {\n\t\tvar res=obj.responseText;\n\t\tif(res.length>3){alert(res);return;}\n\t\tExecuteByClassName('SearchFunction');\n\t}\t\n\n\t\t\n\tfunction RuleDansUpDown(ID,dir){\n\t\tvar XHR = new XHRConnection();\n\t\tXHR.appendData('rule-move', ID);\n\t\tXHR.appendData('rule-dir', dir);\n\t\tXHR.sendAndLoad('dansguardian2.mainrules.php', 'POST',x_RuleDansUpDown{$t});\t\n\t}\n\t\n\n\t\n\t\tvar x_DansGuardianDeleteMainRule= function (obj) {\n\t\t\tvar res=obj.responseText;\n\t\t\tif (res.length>3){alert(res);}\n\t\t\t\$('#'+rowid).remove();\n\t\t}\t\t\n\t\t\n\t\tfunction DansGuardianDeleteMainRule(ID){\n\t\t\trowid=ID;\n\t\t\tif(confirm('{$action_delete_rule}')){\n\t\t\t\tvar XHR = new XHRConnection();\n\t\t     \tXHR.appendData('DansGuardianDeleteMainRule', ID);\n\t\t      \tXHR.sendAndLoad('dansguardian2.mainrules.php', 'POST',x_DansGuardianDeleteMainRule);  \n\t\t\t}\n\t\t}\n\t\t\n\t\tfunction RulesToolBox(){\n\t\t\tLoadAjaxTiny('rules-toolbox','dansguardian2.mainrules.php?rules-toolbox=yes');\n\t\t}\n\t\n\tRulesToolBox();\t\n\tLoadAjaxTiny('rules-toolbox-left','dansguardian2.mainrules.php?rules-toolbox-left=yes');\n\t\n</script>";
    echo $table . "\n" . $js;
}