#!/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 {font-family: monospace; text-align:right;}\n";
print"input.add_input {text-align: left;}\n";
print"hr {width: 80%;}\n";
print"a, a:visited, a:active {text-decoration: none; color:blue; font-family: monospace;}\n";
print"table {border-collapse: collapse; border-top: 1px solid #000000;}\n";
print"td.primary_left {color: #000000; background-color: #ff00ff; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;}\n";
print"td.primary {color: #000000; background-color: #ff00ff; border-bottom: 1px solid #000000; border-right: 1px solid #000000;}\n";
print"td.secondary_left {color: #ffffff; background-color: #800080; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000;}\n";
print"td.secondary {color: #ffffff; background-color: #800080; 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)
{
#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);
@in=(tcp_in,udp_in,icmp_in,rest_in);
@out=(tcp_out,udp_out,icmp_out,rest_out);
@counts=(tcp_in_count,udp_in_count,icmp_in_count,rest_in_count,tcp_out_count,udp_out_count,icmp_out_count,rest_out_count);
if($ctest->{'appearence.do_rest'}==0)
{
@in=(tcp_in,udp_in,icmp_in);
@out=(tcp_out,udp_out,icmp_out);
@counts=(tcp_in_count,udp_in_count,icmp_in_count,tcp_out_count,udp_out_count,icmp_out_count);
}
@flags=(log_flag,add_flag,del_flag);
$class="primary";
$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())
    {
    if($class eq "secondary") {$class="primary";} else {$class="secondary";}
    $current_nip=$data[$rows2select{nip}];
    $ip=$db_handle->selectrow_array("SELECT inet_ntoa($current_nip);");
    @flags_array=();
    @counts_array=();
    @flags_array=$ctest->get_binary($data[$rows2select{flags}]);
    @counts_array=$ctest->get_binary($data[$rows2select{counts}]);
    %checkbox_options=();
    %input_options=(
    ip=>		"size=10",
    dnld_lim=>		"size=4",
    upld_lim=>		"size=4",
    tcp_in=>		"size=11",
    udp_in=>		"size=11",
    icmp_in=>		"size=11",
    rest_in=>		"size=11",
    tcp_out=>		"size=11",
    udp_out=>		"size=11",
    icmp_out=>		"size=11",
    rest_out=>		"size=11",
    in_lim=>		"size=11",
    out_lim=>		"size=11",
    ed=>		"size=16");
    foreach $key(@counts){if($counts_array[$ctest->{"counts.$key"}]) {$checkbox_options{$key}="checked";} else {$checkbox_options{$key}="";}}
    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\_left><a href=\"/cgi-bin/ustat.pl?nip=$data[$rows2select{nip}]\">+</a><form style=\"margin: 0\" action=\"/cgi-bin/edit.pl\" method=GET></td>\n";
    print"<td class=$class>$ip<input type=hidden name=nip value=$data[$rows2select{nip}] size=0></td>\n";
    print"<td class=$class><input type=text name=dnld_lim value=$data[$rows2select{dnld_lim}] $input_options{dnld_lim}></td>\n";
    for $temp(@in)
        {
        print"<td class=$class><input type=text name=$temp value=$data[$rows2select{$temp}] $input_options{$temp}></td>\n";
        print"<td class=$class><input type=checkbox name=\"$temp\_count\" value=1 $checkbox_options{\"$temp\_count\"}></td>\n";
        }
    print"<td class=$class align=right>",$db_handle->selectrow_array("SELECT cast((SUM(tcp_in+udp_in+icmp_in)/(1024*1024)) as DECIMAL(12,3)) FROM users where nip=$current_nip;"),"</td>\n";
    print"<td class=$class><input type=text name=in_lim value=$data[$rows2select{in_lim}] $input_options{in_lim}></td>\n";
    print"<td class=$class>";
    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\_left><input type=checkbox name=log_flag value=1 $checkbox_options{log_flag}></td>\n";
    print"<td class=$class><input type=text name=ed value=\"$data[$rows2select{ed}]\" $input_options{ed}></td>\n";
    print"<td class=$class><input type=text name=upld_lim value=$data[$rows2select{upld_lim}] $input_options{upld_lim}></td>\n";
    for $temp(@out)
        {
        print"<td class=$class><input type=text name=$temp value=$data[$rows2select{$temp}] $input_options{$temp}></td>\n";
        print"<td class=$class><input type=checkbox name=\"$temp\_count\" value=1 $checkbox_options{\"$temp\_count\"}></td>\n";
        }
    print"<td class=$class align=right>",$db_handle->selectrow_array("SELECT cast((SUM(tcp_out+udp_out+icmp_out)/(1024*1024)) as decimal(12,3)) FROM users where nip=$current_nip;"),"</td>\n";
    print"<td class=$class><input type=text name=out_lim value=$data[$rows2select{out_lim}] $input_options{out_lim}></td>\n";
    print"<td class=$class>";
    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>";
}
else {print"You're not in the list<br>\n";}
print"<br>";
print"$uip\n";
print"<br><a href=/cgi-bin/about.htm>About</a>";
print "</center>\n";
print "</body>\n";
print"</html>\n";
