#!/usr/bin/perl
use DBI;
use CGI qw/:standard/;
use conf;

#ip needed
if(param()) {$nip=param("nip");}
#conf open_read
$conf_name="billing.conf";
$ctest=conf->var_read($conf_name);
@need_update=(
dnld_lim,tcp_in,udp_in,icmp_in,rest_in,tcp_in_count,udp_in_count,icmp_in_count,rest_in_count,in_lim,
upld_lim,tcp_out,udp_out,icmp_out,rest_out,tcp_out_count,udp_out_count,icmp_out_count,rest_out_count,out_lim,
ed,log_flag);
@need_log=(nip,dnld_lim,upld_lim,tcp_in,udp_in,icmp_in,rest_in,in_lim,tcp_out,udp_out,icmp_out,rest_out,out_lim,ed);
print"Content-type: text/html;charset=koi8-r;\n\n";
print"<HTML><HEAD><TITLE>Evil Traffic Counter</TITLE></HEAD>\n";
print"<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL=/cgi-bin/admin.pl\">\n";
print"<BODY bgcolor=silver><CENTER>";

$main_table=$ctest->{'mysql_tables.data'};
$edit_log=$ctest->{'mysql_tables.edit'};
$dbh=DBI->connect("DBI:mysql:database=$ctest->{'mysql_connect.db_db'};host=$ctest->{'mysql_connect.db_host'}",
$ctest->{'mysql_connect.db_user'},
$ctest->{'mysql_connect.db_password'});
$uip=$ENV{REMOTE_ADDR};
if (defined $uip)
{
$rows_string="who, flags, counts";
$values_string="(SELECT inet_aton(\"$uip\")), (SELECT flags FROM $main_table WHERE nip=$nip), (SELECT counts FROM $main_table WHERE nip=$nip)";
for $t (@need_log)
    {
    $t_value=param($t);
    $rows_string="$rows_string, $t";
    $values_string="$values_string, \"$t_value\"";
    }
$dbh->do("INSERT INTO $edit_log ($rows_string) values ($values_string);");
}

if (param("add") eq "add")
{
    $ip=param("ip");
    for $i(1..999)
    {
    $temp=$dbh->selectrow_array("SELECT count(id) from users where id=$i;");
    if($temp==0) {$dbh->do("SET insert_id=$i");last;}
    }
    $dbh->do("INSERT INTO $main_table SET nip=(SELECT inet_aton('$ip'));");
}
if (param("del") eq "del")
{
    $switch=1 << $ctest->{"flags.del_flag"};
    $dbh->do("UPDATE $main_table SET flags=flags\|$switch WHERE nip=$nip;");
}
if (param("ok") eq "ok ")
    {
    for $i (@need_update)
	{
        $value=param("$i");
	if (($i=~/(.*)_lim/)&&($dbh->selectrow_array("SELECT $i!=$value from $main_table WHERE nip=$nip;")))
	{
	$switch=1 << $ctest->{"flags.$1\_flag"};
	$dbh->do("UPDATE $main_table SET flags=flags\|$switch WHERE nip=$nip;");
	}
#boolen
        if ((($i=~/(.*)_(count)/)||($i=~/(.*)_(flag)/))&&($value!=1))
	{
	$switch=1 << $ctest->{"$2s.$i"};
	$dbh->do("UPDATE $main_table SET $2s=$2s\|$switch WHERE nip=$nip;");
	$dbh->do("UPDATE $main_table SET $2s=$2s\^$switch WHERE nip=$nip;");
	}
        elsif ((($i=~/(.*)_(count)/)||($i=~/(.*)_(flag)/))&&($value))
	{
	$switch=1 << $ctest->{"$2s.$i"};
	$dbh->do("UPDATE $main_table SET $2s=$2s\|$switch WHERE nip=$nip;");
	}
#non boolen
	if (($i!=~/(.*)_(count)/)&&($i!=~/(.*)_(flag)/))
	{
        if(defined $value){$dbh->do("UPDATE $main_table SET $i=\"$value\" WHERE nip=$nip;");}
	}
	}
    }
#log action
$uip=$ENV{REMOTE_ADDR};
if (defined $uip)
{
$rows_string="who, flags, counts";
$values_string="(SELECT inet_aton(\"$uip\")), (SELECT flags FROM $main_table WHERE nip=$nip), (SELECT counts FROM $main_table WHERE nip=$nip)";
for $t (@need_log)
    {
    $t_value=param($t);
    $rows_string="$rows_string, $t";
    $values_string="$values_string, \"$t_value\"";
    }
$dbh->do("INSERT INTO $edit_log ($rows_string) values ($values_string);");
}
$dbh->disconnect;
#HTML (redirect) print
print"<br><hr>";
print "<table border=1 cellspacing=0 cellpadding=0>\n";
for $i (param()) {$test=param($i);print "<tr><td>$i</td><td>$test</td></tr>\n";}
print"</table>";
print"<hr></body>";
print"</html>";