<?php

if (file_exists("include/db.php")) {
    //read and update bans
    include "badbots.php";
    $update = false;
    include "include/db.php";
    if($db_link){
        mysqli_select_db($db_link,$db);
        $table = "bans";
        $result=mysqli_query($db_link,"select * from $table order by ip");
        if ($result) {
            if (mysqli_num_rows($result) > 0) {
                while($ban=mysqli_fetch_assoc($result)){
                    if ($ban['expires'] > -1 && $ban['expires'] < time()) {
                        mysqli_query($db_link,"delete from $table where ip='".$ban['ip']."'");
                        $update = true;
                    }
                }
            }
        }
        if ($update) {
            $result=mysqli_query($db_link,"select ip from $table order by ip");
            $bans="";
            if (mysqli_num_rows($result) > 0) {
                while($ban=mysqli_fetch_assoc($result)){
                    $bans.= "Deny from ".$ban['ip']."\n";
                }
            }
            $out="$htaccess_header\n$errordoc\n$rewrite_rules\n$badbots\nOrder Allow,Deny\nAllow from all\nDeny from env=bad_bot\n$badips\n$bans";
            $file=fopen("$prefix/.htaccess","w");
            fwrite($file, $out);
            fclose($file);
        }
        mysqli_close($db_link);
    }
    unset($update);
}
?>