#!/usr/local/bin/perl
#----------------------------------------------------------------------------
#	$RCSfile: kacview.cgi,v $
#	$Revision: 1.12 $
#----------------------------------------------------------------------------
require './kacdef.def';

$kac_version= "Version 0.40";
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
#	Main
#----------------------------------------------------------------------------
&set_inputenv;
&check_id;
&set_env;
&disp_logdata;
#&show_img;
exit;				# end

#----------------------------------------------------------------------------
#	Set Env
#----------------------------------------------------------------------------
sub set_inputenv {
    if( $ENV{'REQUEST_METHOD'} eq 'POST' ){ 
	read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
    } else {
	$buffer = $ENV{'QUERY_STRING'};
    }

    @pairs = split(/&/,$buffer);
    foreach $pair (@pairs) {	# Decode
        ($field, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   	$value =~ s/\n//g;	
	$FORM{$field} = $value;
    }

    $lid  = $FORM{'lid'};
    if( $FORM{'Vmode'} ne "" ){ $Vmode = $FORM{'Vmode'}; }
    if( $FORM{'Vtotal'} ne "" ){ $Vtotal= $FORM{'Vtotal'}; }
    if( $FORM{'Vrank'} ne "" ){ $Vrank= $FORM{'Vrank'}; }
}

#----------------------------------------------------------------------------
#	Set EnviromentMain
#----------------------------------------------------------------------------
sub check_id{
    if( !open(ID,"$kac_id") ){ &error_exit; }
    @line=<ID>;
    close(ID);
    $log = "" ;

#    $lid = "test" ;  ##debug

    foreach $buf (@line){
	($idno,$pagename,$cuthost,$passwd)= split(/\,/,$buf);

	if( $lid eq $idno ){
	    $log = sprintf( "%s/%s.log", $kac_logdir, $idno );
	    if( $cuthost ne '' ){
		@cuth = split(/\//,$cuthost);
	    }
	    last;
	}
    }

    if( $log eq ""){ &error_exit; }
}

#----------------------------------------------------------------------------
#	Set EnviromentMain
#----------------------------------------------------------------------------
sub set_env{
#   Get Time
    $ntime = time;
    ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime(time);

    $ndate=sprintf("%2d/%02d/%02d %02d:%02d",$year,($mon+1),$day,$hour,$min);
}

#----------------------------------------------------------------------------
#	Output Image text
#----------------------------------------------------------------------------
sub disp_logdata{
    if( !open(DATA,"$log") ){ &error_exit; }    
    @lines=<DATA>;
    close(DATA);
    $lcnt = @lines; $dcnt = 0; $img_maxwidth=300;

    &make_html_header;
    &show_comm_line;

    if( $Vmode == 0 ){    # host
	foreach $buf (@lines){
	    ($ltime,$host,$brw,$os,$agent)= split(/\,/,$buf);
	    if( $ntime - $ltime > $Vtotal_sec[ $Vtotal ] ){ last; }

	    if ($host ne '') { 
		if ($host =~ /(.*)\.(\d+)$/) { ; }
		elsif($host=~ /(.*)\.(.*)\.(.*)\.(.*)\.(.*)$/){$host="\*\.$2\.$3\.$4\.$5";}
		elsif($host=~ /(.*)\.(.*)\.(.*)\.(.*)$/) { $host="\*\.$2\.$3\.$4"; }
		elsif($host=~ /(.*)\.(.*)\.(.*)$/) { $host="\*\.$2\.$3"; }

		$host_a{($host)[0]}++; $dcnt++;
	    }
	}
	&print_hostrank; 
    }

    if( $Vmode == 1 ){    # Broser
	foreach $buf (@lines){
	    ($ltime,$host,$brw,$os,$agent)= split(/\,/,$buf);
	    if( $ntime - $ltime > $Vtotal_sec[ $Vtotal ] ){ last; }
	    if ($brw  ne '') { $brw_a {($brw )[0]}++; $dcnt++; }
	}
	&print_brw_rank; 
    }

    if( $Vmode == 2 ){    # OS Type
	foreach $buf (@lines){
	    ($ltime,$host,$brw,$os,$agent)= split(/\,/,$buf);
	    if( $ntime - $ltime > $Vtotal_sec[ $Vtotal ] ){ last; }
	    if( ($os ne '') && ($os !~ m/\./i ) ){ 
		$os_a {($os )[0]}++; $dcnt++;
	    }
	}
	&print_os_rank;
    }

    if( $Vmode == 3 ){    # hour
	foreach $buf (@lines){
	    ($ltime,$host,$brw,$os,$agent)= split(/\,/,$buf);
	    if( $ntime - $ltime > $Vtotal_sec[ $Vtotal ] ){ last; }
	    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
		= localtime($ltime);
	    if ($hour ne '') { $hour_a{($hour)[0]}++; $dcnt++; }
	}
	&print_hourrank;
    }
    if( $Vmode == 4 ){    # Week
	foreach $buf (@lines){
	    ($ltime,$host,$brw,$os,$agent)= split(/\,/,$buf);
	    if( $ntime - $ltime > $Vtotal_sec[ $Vtotal ] ){ last; }
	    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
		= localtime($ltime);
	    if ($wday ne '') { $wday_a{($wday)[0]}++; $dcnt++; }
	}
	&print_dayrank; 
    }
    if( $Vmode == 5 ){    # Date
	foreach $buf (@lines){
	    ($ltime,$host,$brw,$os,$agent)= split(/\,/,$buf);
	    if( $ntime - $ltime > $Vtotal_sec[ $Vtotal ] ){ last; }
	    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
		= localtime($ltime);
	    if ($mday ne '') { $mday_a{($mday)[0]}++; $dcnt++; }
	}
	&print_daterank; 
    }

#### For Debug
    if( $Vmode == 9 ){  
	foreach $buf (@lines){
	    ($ltime,$host,$brw,$os,$agent)= split(/\,/,$buf);
	    if( $ntime - $ltime > $Vtotal_sec[ $Vtotal ] ){ last; }

	    if( ($agent ne "\n") && ($agent ne '')) { 
		$agenta{($agent)[0]}++; $dcnt++; 
	    }
	}
	&print_agen_rank; 
    }
#### 


    &make_html_footer;
}

#----------------------------------------------------------------------------
#	HTML header & title
#----------------------------------------------------------------------------
sub make_html_header{
    print "Content-type: text/html\n\n";

    print <<"EOF";
<HTML><HEAD><TITLE>$page_title</TITLE>
<META http-equiv="Content-Type" content="text/html"\; charset=EUC-JP>
</HEAD>
<BODY BGCOLOR="$bg_color" TEXT="$text_color" LINK="$link_color" VLINK="$vlink_color" ALINK="$alink_color">
<hr>
<CENTER><FONT size=4 COLOR="$titl_color"><I>$page_title</I></FONT><br>
</CENTER><hr width=70%>
EOF
}

#----------------------------------------------------------------------------
#	Show Message Boaed footer
#----------------------------------------------------------------------------
sub make_html_footer{
    print <<"EOF";
<HR>
<table width=100%><tr><td align="right">
<I><a href="http://www.millto.net/kenz/">KACounter $kac_version</a> by <A HREF="mailto:$kac_owner">$kac_owner.</A></I>
</td></tr></table>
</BODY></HTML>
EOF
}

#----------------------------------------------------------------------------
#	HOST rank
#----------------------------------------------------------------------------
sub print_hostrank{
    $i = 0; $buf = 0;

    print <<"EOF";
<CENTER><FONT size=5 COLOR="$titl_color"><I>$host_rank_title</I></FONT>
(Valid data is <font color="$mes_color">$dcnt</font> in <font color="$mes_color">$lcnt</font>)<p>
<table width=90% border=3>
<tr><th>$msg_rank</th><th>$msg_host</th><th>$msg_count</th>
<th>$msg_rate</th><th>$msg_graph</th></tr>
EOF

    if( $dcnt == 0 ){ 
	print "</table>No Data in Log file.</CENTER><p>\n" ;  	return;
    }

    foreach( sort { $host_a{$b} <=> $host_a{$a} } keys %host_a ){
	$i++;
	if( $buf != $host_a{$_} ){
	    if( $i > $sel_Vrank[$Vrank] ){last; }
	    $rank = $i;
	    $buf = $host_a{$_}; 
	}

	$rate = ( $host_a{$_} * 100) / $dcnt ;
	$rate = sprintf("%.2f",$rate );
	print "<tr><th>$rank</th><td>$_</td><th>$host_a{$_}</th><th>$rate\%</th>\n";

	if( $i==1 ){ $maxrate = $rate; $barcnt = $img_maxwidth; }
	else{ $barcnt = sprintf("%d", ( $rate * $img_maxwidth )/ $maxrate ); }

	print "<td><img height=18 width=$barcnt src=\"$graph_img\">" ;
	print "</td></tr>\n";
    }
    print "</table></center><p>\n" ;

}

#----------------------------------------------------------------------------
#	BROWSER rank
#----------------------------------------------------------------------------
sub print_brw_rank{
    $i = 0; $buf = 0;

    print <<"EOF";
<CENTER><FONT size=5 COLOR="$titl_color"><I>$browser_rank_title</I></FONT>
(Valid data is <font color="$mes_color">$dcnt</font> in <font color="$mes_color">$lcnt</font>)<p>
<table width=90% border=3>
<tr><th>$msg_rank</th><th>$msg_browser</th><th>$msg_count</th>
<th>$msg_rate</th><th>$msg_graph</th></tr>
EOF

    if( $dcnt== 0 ){
	print "</table>No Data.....</CENTER><p>\n"; return;
    }

    foreach( sort { $brw_a{$b} <=> $brw_a{$a} } keys %brw_a ){
	$i++;
	if(( $i >= $sel_Vrank[$Vrank] )&&( $buf != $brw_a{$_} )){last ; }
	$buf = $brw_a{$_}; 

	$rate = ( $brw_a{$_} * 100) / $dcnt ;
	$rate = sprintf("%.2f",$rate );
	print "<tr><th>$i</th><td>$_</td><th>$brw_a{$_}</th><th>$rate\%</th>\n";

	if( $i==1 ){ $maxrate = $rate; $barcnt = $img_maxwidth; }
	else{ $barcnt = sprintf("%d", ( $rate * $img_maxwidth )/ $maxrate ); }

	print "<td><img height=18 width=$barcnt src=\"$graph_img\">" ;
	print "</td></tr>\n";
    }
    print "</table>$browser_foot_msg<p></center>\n" ;
}

#----------------------------------------------------------------------------
#	OStype rank
#----------------------------------------------------------------------------
sub print_os_rank{
    $i = 0; $buf = 0;

    print <<"EOF";
<CENTER><FONT size=5 COLOR="$titl_color"><I>$ostype_rank_title</I></FONT>
(Valid data is <font color="$mes_color">$dcnt</font> in <font color="$mes_color">$lcnt</font>)<p>
<table width=90% border=3>
<tr><th>$msg_rank</th><th>$msg_ostype</th><th>$msg_count</th>
<th>$msg_rate</th><th>$msg_graph</th></tr>
EOF

    if( $dcnt== 0 ){
	print "</table>No Data.....</CENTER><p>\n" ;  	return;
    }

    foreach( sort { $os_a{$b} <=> $os_a{$a} } keys %os_a ){
	$i++;
	if(( $i >= $sel_Vrank[$Vrank] )&&( $buf != $os_a{$_} )){last ; }
	$buf = $os_a{$_}; 

	$rate = ( $os_a{$_} * 100) / $dcnt ;
	$rate = sprintf("%.2f",$rate );
	print "<tr><th>$i</th><td>$_</td><th>$os_a{$_}</th><th>$rate\%</th>\n";

	if( $i==1 ){ $maxrate = $rate; $barcnt = $img_maxwidth; }
	else{ $barcnt = sprintf("%d", ( $rate * $img_maxwidth )/ $maxrate ); }

	print "<td><img height=18 width=$barcnt src=\"$graph_img\">" ;
	print "</td></tr>\n";
    }
    print "</table></center><p>\n" ;
}

#----------------------------------------------------------------------------
#	Agent rank
#----------------------------------------------------------------------------
sub print_agen_rank{
    $i = 0; $buf = 0; $orank=1;

    print <<"EOF";
<CENTER><FONT size=5 COLOR="$titl_color"><I>$agent_rank_title</I></FONT>
(Valid data is <font color="$mes_color">$dcnt</font> in <font color="$mes_color">$lcnt</font>)<p>
<table width=90% border=3>
<tr><th>$msg_agent</th><th>$msg_count</th>
<th>$msg_rate</th><th>$msg_graph</th></tr>
EOF
    
    if( $dcnt== 0 ){ 
	print "</table>No Data.....</CENTER><p>\n" ;  	return;
    }

    foreach( sort { $agenta{$b} <=> $agenta{$a} } keys %agenta ){
	$i++;
	if( $buf != $agenta{$_} ){
	    if( $i > $sel_Vrank[$Vrank] ){last; }
	    $rank = $i;
	    $buf = $agenta{$_}; 
	}

	$rate = ( $agenta{$_} * 100) / $dcnt ;
	$rate = sprintf("%.2f",$rate );
	print "<tr><th>$rank</th><td>$_</td><th>$agenta{$_}</th><th>$rate\%</th>\n";

	if( $i==1 ){ $maxrate = $rate; $barcnt = $img_maxwidth; }
	else{ $barcnt = sprintf("%d", ( $rate * $img_maxwidth )/ $maxrate ); }

	print "<td><img height=18 width=$barcnt src=\"$graph_img\">" ;
	print "</td></tr>\n";
    }
    print "</table></center><p>\n" ;
}


#----------------------------------------------------------------------------
#	HOUR Rank
#----------------------------------------------------------------------------
sub print_hourrank{
    print <<"EOF";
<CENTER><FONT size=5 COLOR="$titl_color"><I>$hour_rank_title</I></FONT>
(Valid data is <font color="$mes_color">$dcnt</font> in <font color="$mes_color">$lcnt</font>)<p>
<table width=85% border=3><tr>
<th>$msg_hour</th><th>$msg_count</th><th>$msg_rate</th><th>$msg_graph</th></tr>
EOF

    if( $dcnt== 0 ){
	print "</table>No Data.....</CENTER><p>\n" ;  	return;
    }

    for( $i=0 ; $i<24 ; $i++ ){
	$ih = sprintf("%02d",$i );

	if( $hour_a{$i} eq "" ){ $hour_a{$i} = 0; }
	$rate = ( $hour_a{$i} * 100) / $dcnt ;
	$rate = sprintf("%.2f",$rate );
	print "<tr><th>$ih</th><th>$hour_a{$i}</th><th>$rate\%</th>\n";

	if( ($barcnt = sprintf("%d", ($rate*6) )) == 0 ){ $barcnt=1; }
	print "<td><img height=18 width=$barcnt src=\"$graph_img\"></td></tr>\n";
    }
    print "</table></CENTER><p>\n" ;
}

#----------------------------------------------------------------------------
#	DATE Rank
#----------------------------------------------------------------------------
sub print_daterank{
    print <<"EOF";
<CENTER><FONT size=5 COLOR="$titl_color"><I>$date_rank_title</I></FONT>
(Valid data is <font color="$mes_color">$dcnt</font> in <font color="$mes_color">$lcnt</font>)<p>
<table width=85% border=3><tr>
<th>$msg_date</th><th>$msg_count</th><th>$msg_rate</th><th>$msg_graph</th></tr>
EOF

    if( $dcnt== 0 ){ 
	print "</table>No Data.....</CENTER><p>\n" ;  	return;
    }

    for( $i=1 ; $i<32 ; $i++ ){
	if( $mday_a{$i} eq "" ){ $mday_a{$i} = 0; }
	$rate = ($mday_a{ $i } * 100)/ $dcnt ;
	$rate = sprintf("%.2f",$rate );

	print "<tr><th>$i</th><th>$mday_a{$i}</th><th>$rate\%</th>\n";

	if( ($barcnt = sprintf("%d", ($rate*6) )) == 0 ){ $barcnt=1; }
	print "<td><img height=18 width=$barcnt src=\"$graph_img\"></td></tr>\n";
    }
    print "</table></CENTER><p>\n" ;
}

#----------------------------------------------------------------------------
#	DAY Rank
#----------------------------------------------------------------------------
sub print_dayrank{
    print <<"EOF";
<CENTER><FONT size=5 COLOR="$titl_color"><I>$day_rank_title</I></FONT>
(Valid data is <font color="$mes_color">$dcnt</font> in <font color="$mes_color">$lcnt</font>)<p>
<table width=85% border=3><tr>
<th>$msg_day</th><th>$msg_count</th><th>$msg_rate</th><th>$msg_graph</th></tr>
EOF

    if( $dcnt== 0 ){ 
	print "</table>$msg_nodata</CENTER><p>\n" ;  	return;
    }

    for( $i=0 ; $i<7 ; $i++ ){
	$id = sprintf("%02d",$i );

	if( $wday_a{$i} eq "" ){ $wday_a{$i} = 0; }
	$rate = ($wday_a{ $i } * 100)/ $dcnt ;
	$rate = sprintf("%.2f",$rate );

	print "<tr><th>$w_days[$i]</th><th>$wday_a{$i}</th><th>$rate\%</th>\n";

	if( ($barcnt = sprintf("%d", ($rate*6) )) == 0 ){ $barcnt=1; }
	print "<td><img height=18 width=$barcnt src=\"$graph_img\"></td></tr>\n";
    }
    print "</table></CENTER><p>\n" ;
}

#----------------------------------------------------------------------------
#	Show Command Line
#----------------------------------------------------------------------------
sub show_comm_line{
#	Sellect 
    $sel_Vmode{$Vmode} = 'SELECTED';
    $sel_Vtotal{$Vtotal} = 'SELECTED';
    $sel_Vrank{$Vrank} = 'SELECTED';

    print <<"EOF";
<CENTER>
<table><tr><th>
<FORM NAME="setenvform" METHOD="POST" ACTION="$view_cgi">
Date Type:<SELECT NAME=\"Vmode\" size=1>
EOF

    for ($i = 0; $i <= $#sel_Vmode; $i++) {
	print "<OPTION VALUE=\"$i\" $sel_Vmode{$i}>$sel_Vmode[$i]\n";
    }

    print "</SELECT>\n$msg_period<SELECT NAME=\"Vtotal\">\n";
    for ($i = 0; $i <= $#sel_Vtotal; $i++) {
	print "<OPTION VALUE=\"$i\" $sel_Vtotal{$i}>$sel_Vtotal[$i]\n";
    }

    print "</SELECT>\n$msg_maxrank<SELECT NAME=\"Vrank\">\n";
    for ($i = 0; $i <= $#sel_Vrank; $i++) {
	print "<OPTION VALUE=\"$i\" $sel_Vrank{$i}>$sel_Vrank[$i]\n";
    }

    print <<"EOF";
</SELECT><INPUT TYPE="hidden" NAME="lid" VALUE="$lid">
<INPUT TYPE="submit" NAME="button" VALUE="$btn_recalc">
<br>$ndate
</FORM>
</th></table>
<hr width="70%">[ <FONT COLOR="$mes_color">$pagename</FONT> ]<br></CENTER>
EOF


}

#----------------------------------------------------------------------------
#	Error
#----------------------------------------------------------------------------
sub error_exit{
    print "Content-type: text/plain\n\n$msg_error\n";
    exit;
}
