#!/usr/bin/perl
use DBI;
use conf;

#conf open_read
$conf_name="billing.conf";
$ctest=conf->var_read($conf_name);
$main_table=$ctest->{'mysql_tables.data'};
#HTML
print"Content-type: text/html;charset=cp-1251;\n\n";
print"<html>\n";
print"<head>\n";
print"<title>$ctest->{'appearence.title'}</title>\n";
print"<style>\n";
print"body {background-color: #888888;}\n";
print"body,td,input {font-family: monospace;}\n";
print"form {margin: 0;}\n";
print"input {text-align:right;}\n";
print"input.add_input {text-align: left;}\n";
print"hr {width: 80%;}\n";
print"a, a:visited, a:active {font-family: monospace; color:#00ffff; text-decoration: none;}\n";
print"table {border-collapse: collapse; border-top: 1px solid #000000; border-left: 1px solid #000000;}\n";
print"td.one {background-color: #ff00ff; color: #000000; border-bottom: 1px solid #000000; border-right: 1px solid #000000;}\n";
print"td.two {background-color: #800080; color: #ffffff; border-bottom: 1px solid #000000; border-right: 1px solid #000000;}\n";
print"</style>\n";
print"</head>\n";
print"<body>\n";
print"<center>\n";
#Admin/user
$uip=$ENV{REMOTE_ADDR};
$admin=0;
for $temp (split('=',$ctest->{'admins.security'})) {if ($temp eq $uip) {$admin=1;}}
if(!$admin) {print"You're not in the list<br>\n";}
elsif($admin)
{
#var init
%rows2select=(
id=>			0,
nip=>			1,
flags=>			2,
counts=>		3,
dnld_lim=>		4,
upld_lim=>		5,
tcp_in=>		6,
udp_in=>		7,
icmp_in=>		8,
rest_in=>		9,
in_lim=>		10,
tcp_out=>		11,
udp_out=>		12,
icmp_out=>		13,
rest_out=>		14,
out_lim=>		15,
ed=>			16);
@hidden=(tcp_in_count,tcp_out_count,udp_in_count,udp_out_count,icmp_in_count,icmp_out_count,rest_in_count,rest_out_count);
@flags=(log_flag,add_flag,del_flag);
@in=(tcp_in,udp_in,icmp_in,rest_in);
@out=(tcp_out,udp_out,icmp_out,rest_out);
if($ctest->{'appearence.do_rest'}==0)
    {
    @in=(tcp_in,udp_in,icmp_in);
    @out=(tcp_out,udp_out,icmp_out);
    }
@class=("one","two");
$state=1;
$select_string=join(',',sort {$rows2select{$a}<=>$rows2select{$b}} keys %rows2select);
$select_string="select $select_string from $main_table order by nip asc;";
$db_handle=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'});
print"<form action=\"/cgi-bin/edit.pl\" method=GET><input type=text name=ip value=\"0.0.0.0\" class=add_input><input type=submit name=add value=add></form>\n";
print"<hr>\n";

print"<table>\n";
$sth=$db_handle->prepare("$select_string");
$sth->execute();
while(@data=$sth->fetchrow_array())
    {
    $state=1-$state;
    $current_nip=$data[$rows2select{nip}];
    $ip=$db_handle->selectrow_array("SELECT inet_ntoa($current_nip);");
    @flags_array=();
    @flags_array=$ctest->get_binary($data[$rows2select{flags}]);
    %checkbox_options=();
    %input_options=(
    ip=>		"size=10",
    dnld_lim=>		"size=4",
    upld_lim=>		"size=4",
    tcp_in=>		"size=11",
    tcp_out=>		"size=11",
    udp_in=>		"size=9",
    udp_out=>		"size=9",
    icmp_in=>		"size=7",
    icmp_out=>		"size=7",
    rest_in=>		"size=8",
    rest_out=>		"size=8",
    in_lim=>		"size=11",
    out_lim=>		"size=11",
    ed=>		"size=10");
    foreach $key(@flags){if($flags_array[$ctest->{"flags.$key"}]) {$checkbox_options{$key}="checked";} else {$checkbox_options{$key}="";}}
    if($flags_array[$ctest->{'flags.del_flag'}])
        {
        foreach $key(keys %checkbox_options){$checkbox_options{$key}="$checkbox_options{$key} disabled";};
        foreach $key(keys %input_options){$input_options{$key}="$input_options{$key} disabled";}
        }
    print"<tr>\n";
    print"<td align=center class=$class[$state]><a href=\"/cgi-bin/ustat.pl?nip=$data[$rows2select{nip}]\">+</a><form action=\"/cgi-bin/edit.pl\" method=GET></td>\n";
    print"<td class=$class[$state]>$ip<input type=hidden name=nip value=$data[$rows2select{nip}]>\n";
    for $temp(@hidden) {print"<input type=hidden name=\"$temp\" value=\"-1\">\n";}
    print"</td>";
    print"<td class=$class[$state]><input type=text name=dnld_lim value=$data[$rows2select{dnld_lim}] $input_options{dnld_lim}></td>\n";
    for $temp(@in) {print"<td class=$class[$state]><input type=text name=$temp value=$data[$rows2select{$temp}] $input_options{$temp}></td>\n";}
    print"<td class=$class[$state] align=right>",$db_handle->selectrow_array("SELECT cast((SUM(tcp_in+udp_in+icmp_in+rest_in)/(1024*1024)) as DECIMAL(12,3)) FROM users where nip=$current_nip;"),"</td>\n";
    print"<td class=$class[$state]><input type=text name=in_lim value=$data[$rows2select{in_lim}] $input_options{in_lim}></td>\n";
    print"<td class=$class[$state]>";
    if(!$flags_array[$ctest->{'flags.del_flag'}]){print"<input type=submit name=ok value=\"ok \">";} else {print"&nbsp;";}
    print"</td>\n";
    print"</tr>\n";
    print"<tr>\n";
    print"<td class=$class[$state]><input type=checkbox name=log_flag value=1 $checkbox_options{log_flag}></td>\n";
    print"<td class=$class[$state]><input type=text name=ed value=\"$data[$rows2select{ed}]\" $input_options{ed}></td>\n";
    print"<td class=$class[$state]><input type=text name=upld_lim value=$data[$rows2select{upld_lim}] $input_options{upld_lim}></td>\n";
    for $temp(@out) {print"<td class=$class[$state]><input type=text name=$temp value=$data[$rows2select{$temp}] $input_options{$temp}></td>\n";}
    print"<td class=$class[$state] align=right>",$db_handle->selectrow_array("SELECT cast((SUM(tcp_out+udp_out+icmp_out+rest_out)/(1024*1024)) as decimal(12,3)) FROM users where nip=$current_nip;"),"</td>\n";
    print"<td class=$class[$state]><input type=text name=out_lim value=$data[$rows2select{out_lim}] $input_options{out_lim}></td>\n";
    print"<td class=$class[$state]>";
    if(!$flags_array[$ctest->{'flags.del_flag'}]){print"<input type=submit name=del value=del>";} else {print"&nbsp;";}
    print "</form>";
    print"</td>\n";
    print"</tr>\n";
    }
$sth->finish;
$db_handle->disconnect;
print"</table>\n";

print"<hr>\n";
print"<a href=/cgi-bin/techstat.pl>Techstat</a><br>";
}
print"$uip\n";
#print"<br><a href=/cgi-bin/about.htm>About</a>";
print "</center>\n";
print "</body>\n";
print"</html>\n";