#!/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'};
$hourly_sum=$ctest->{'mysql_tables.hour'};

%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);
%input_options=(
ip=>			"size=7",
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");
%r_month=(
'01'=>"I",
'02'=>"II",
'03'=>"III",
'04'=>"IV",
'05'=>"V",
'06'=>"VI",
'07'=>"VII",
'08'=>"VIII",
'09'=>"IX",
'10'=>"X",
'11'=>"XI",
'12'=>"XII");
@data=();
@flags_array=();
@counts_array=();
@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);
@flags=(log_flag,add_flag,del_flag);
@tdclass=("primary","secondary");
$select_string=join(',',sort {$rows2select{$a}<=>$rows2select{$b}} keys %rows2select);
$uip=$ENV{REMOTE_ADDR};
#HTML header
print"Content-type: text/html;charset=cp-1251;\n\n";
print"<html><head><title>$uip user stat</title>";
print"<style>";
print"body,input,td{font-family: monospace;}";
print"hr {width: 80%;}";
print"table {border-collapse: collapse;}";
print"td.primaryleft{border-bottom: 1px solid #ffffff; border-left: 1px solid #ffffff; border-right: 1px solid #ffffff; color:#000000; padding: 2px;background-color: #ff00ff;}";
print"td.primary{border-bottom: 1px solid #ffffff; border-right: 1px solid #ffffff; padding: 2px; background-color: #ff00ff;}";
print"td.primary_spacer{border-bottom: 1px solid #ffffff; padding: 2px; color: #000000;}";
print"td.secondaryleft{border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; color: #ffffff; padding: 2px;background-color: #800080;}";
print"td.secondary{border-bottom: 1px solid #000000; border-right: 1px solid #000000; color: #ffffff; padding: 2px; background-color: #800080;}";
print"td.secondary_spacer{border-bottom: 1px solid #000000; color: #ffffff; padding: 2px; color: #000000;}";
print"table.main {border-top: 1px solid #000000;}";
print"td.firstleft{border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; color:#000000; padding: 2px; background-color: #ff00ff;}";
print"td.first{border-bottom: 1px solid #000000; border-right: 1px solid #000000; padding: 2px; background-color: #ff00ff;}";
print"td.secondleft{border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; color:#000000; padding: 2px; background-color: #800080;}";
print"td.second{border-bottom: 1px solid #000000; border-right: 1px solid #000000; color: #ffffff; padding: 2px; background-color: #800080;}";
print"</style>";
print"</head>\n";
print"<body bgcolor=#888888>\n";
print"<center>\n";
$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'});
$current_nip=$db_handle->selectrow_array("SELECT inet_aton('$uip');");
if($db_handle->selectrow_array("SELECT count(*) from $main_table WHERE nip=$current_nip;"))
{
    $select_string="select $select_string from $main_table where nip=$current_nip;";
    @data=$db_handle->selectrow_array("$select_string");
    @flags_array=$ctest->get_binary($data[$rows2select{flags}]);
    @counts_array=$ctest->get_binary($data[$rows2select{counts}]);
    foreach $key(@counts){if($counts_array[$ctest->{"counts.$key"}]) {$checkbox_options{$key}=" checked";} else {$checkbox_options{$key}="";}}
    foreach $key(keys %input_options){$input_options{$key}="$input_options{$key} disabled";}
    print"<hr>\n";
    print"<table class=main>\n";
    print"<tr>\n";
    print"<td class=firstleft>$uip</td>\n";
    print"<td class=first><input type=text name=dnld_lim value=$data[$rows2select{dnld_lim}] $input_options{dnld_lim}></td>\n";
    for $temp(@in) {print"<td class=first><input type=text name=$temp value=$data[$rows2select{$temp}] $input_options{$temp}></td>\n";}
    print"<td class=first>",$db_handle->selectrow_array("SELECT SUM(tcp_in+udp_in+icmp_in)/(1024*1024) FROM users where nip=$current_nip;"),"</td>\n";
    print"<td class=first><input type=text name=in_lim value=$data[$rows2select{in_lim}] $input_options{in_lim}></td>\n";
    print"</tr>\n";
    print"<tr>\n";
    print"<td class=secondleft><input type=text name=ed value=\"$data[$rows2select{ed}]\" $input_options{ed}></td>\n";
    print"<td class=second><input type=text name=upld_lim value=$data[$rows2select{upld_lim}] $input_options{upld_lim}></td>\n";
    for $temp(@out) {print"<td class=second><input type=text name=$temp value=$data[$rows2select{$temp}] $input_options{$temp}></td>\n";}
    print"<td class=second>",$db_handle->selectrow_array("SELECT SUM(tcp_out+udp_out+icmp_out)/(1024*1024) FROM users where nip=$current_nip;"),"</td>\n";
    print"<td class=second><input type=text name=out_lim value=$data[$rows2select{out_lim}] $input_options{out_lim}></td>\n";
    print"</tr>\n";
    print"</table>\n";
    print"<hr>\n";
    $day=0;
    $state=0;
    print"<table class=$tableclass[$state]>\n";
@data=();
    $select_string="select time,tcp_in,udp_in,icmp_in,rest_in,tcp_out,udp_out,icmp_out,rest_out from $hourly_sum where nip=$current_nip order by time desc limit 17520;";
    $sth=$db_handle->prepare("$select_string");
    $sth->execute();
    while(@data=$sth->fetchrow_array())
    {
	$daychange=0;
        $i=0;
        @datetime=split(' ',$data[0]);
        @date=split('-',$datetime[0]);
        if($date[2]!=$day) {$state=1-$state;$day=$date[2];$daychange=1;}
	if($daychange eq 1) {print"<tr><td colspan=9 align=center class=\"$tdclass[$state]\_spacer\">\n$date[2]\.$r_month{$date[1]}\.$date[0]</td>\n</tr>\n";}
	print"<tr align=right>";
        while(defined $data[$i])
	{
	    if($i==0) {print"<td align=right class=$tdclass[$state]left>$datetime[1]</td>";}
	    else {print"<td class=$tdclass[$state]>$data[$i]</td>";}
	    $i++;
	}
	print"</tr>\n";
    }
    print"</table>\n";
}
else {print"You're not in the list<br>\n";}
$db_handle->disconnect;
print"<hr>$uip<br><br>\n";
print"</center>\n";
print"</body>\n";
print"</html>\n";
