#!/usr/bin/perl

use strict;
use CGI qw /:standard/;
use DBI;
use POSIX;

print header;

my %Form;
my $db;
my $q;
my $Q;
my $pidFile = "/var/run/dial-in.pid";
my $timeFile = "/var/run/dial-in.stime";
my $pid;
my $time;
my $dial = "/usr/bin/dial-in";
my $kill = "/bin/kill";

{ # Getting data from form
	my $D;
	my @Formularfelder;
	my $Feld;

	if($ENV{'REQUEST_METHOD'} eq 'GET')
	{
		$D = $ENV{'QUERY_STRING'}
	}
	else
	{
		read(STDIN, $D, $ENV{'CONTENT_LENGTH'});
	}

	@Formularfelder = split(/&/, $D);
	foreach $Feld (@Formularfelder)
	{
		my ($name, $value) = split(/=/, $Feld);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$value =~ s/<!--(.|\n)*-->//g;
		$Form{$name} = $value;
	}
}

my $action = $Form{"action"};

# Connecting to DB
$db = DBI->connect('DBI:mysql:ppp', 'root', '');


open PID, $pidFile or $pid = -1;
unless($pid == -1) {
	$pid = <PID>
}
close PID;
open TIME, $timeFile or $time = -1;
unless($time == -1) {
	$time = <TIME>;
}
close TIME;

if($action eq "dial") {
	system("$dial &") or die;
} elsif($Form{"action"} eq "hangUp" && $pid>0) {
	system("$kill -s HUP $pid");
}

print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";

print "<html><head>\n";
unless($time == -1) {
	print "<script language=\"JavaScript\"><!--\n";
	print "\tdialTime = new Date();\n";
	print "\tdialTime.setTime($time * 1000);\n";
	print "\tvar dial = dialTime.getTime();\n\n";
	print "\tfunction updateTime(){\n";
	print "\t\tvar absSec  = Math.round(calcTime());\n";
	print "\t\tvar absMin  = Math.round((absSec-30)/60);\n";
	print "\t\tvar absHour = Math.round((absMin-30)/60);\n";
	print "\t\tvar relSec  = absSec % 60;\n";
	print "\t\tvar relMin  = absMin % 60;\n";
	print "\t\tvar anzSec  = \"\"+((relSec>9)?relSec:\"0\"+relSec);\n";
	print "\t\tvar anzMin  = \"\"+((relMin>9)?relMin:\"0\"+relMin);\n";
	print "\t\twindow.document.disp.timer.value = absHour+\":\"+anzMin+\":\"+anzSec;\n";
	print "\t\twindow.setTimeout('updateTime()',1000)\n";
	print "\t}\n\n";
	print "\tfunction calcTime() {\n";
	print "\t\tNow = new Date();\n";
	print "\t\tvar timeDiff = Now.getTime() - dial;\n";
	print "\t\treturn(timeDiff/1000);\n";
	print "\t}\n\n";
	print "//--></script>\n";
}

print "<script language=\"JavaScript\"><!--\n";
print "\tfunction localhost() {\n";
print "\t\twindow.location.href = 'index.pl'\n";
print "\t}\n";
print "//--></script>\n";

print "<title>Index Page</title></head>\n";
print "<body bgcolor=#000000 text=#0000BB";
print " onLoad=\"window.setTimeout('updateTime()',1000)\"" unless($time == -1 || $action);
print " onLoad=\"window.setTimeout('localhost()',2500)\"" if($action);
print ">\n";

print "<table border=\"1\"><tr>\n";
print "\t<td>\n";


# Dial In Button
print "\t<form action=\"index.pl\" method=\"get\" name=\"dial\">\n";
print "\t\t<input type=hidden name=\"action\" value=\"dial\">\n";
print "\t\t<input type=submit value=\"Dial In\">\n";
print "\t</form>\n";

# Hang Up Button
print "\t<form action=\"index.pl\" method=\"get\" name=\"hangUp\">\n";
print "\t\t<input type=hidden name=\"action\" value=\"hangUp\">\n";
print "\t\t<input type=submit value=\"Hang Up\">\n";
print "\t</form>\n";
print "\t</td>\n";


# Reload Button
print "\t<td><form name=\"reload\">\n";
print "\t\t<input type=button value=\"Reload\" onClick=\"localhost()\">\n";
print "\t</form>\n";

print "\t</td>\n";

# Print current status
print "\t<td>\n";
print "\tStatus:<br>\n";
if($pid==-1){
	print "\t<font color=\"#FF0000\">Disconnected</font><br>\n";
} else {
	print "\t<font color=\"#00AA00\">Connected</font><br>\n";
}
unless($time == -1) {
	print "\t<form name=\"disp\"><input size=8 name=\"timer\"></form>\n";
}
print "\t</td>\n";

# Links
print "\t<td rowspan=2 valign=top>\n";
print "\t<a href=\"/selfhtml\">SelfHTML</a><br>\n";
print "\t<a href=\"/phpdoc\">PHP Documentation & Language reference</a><br>\n";
print "\t<a href=\"/books\">Collection of Books from SUSE DVD</a><br>\n";
print "\t<a href=\"/howtoEN/HOWTO-INDEX/index.html\">HowTo's in English</a><br>\n";

print "\t</td>\n";

print "</tr><tr>\n";

$q = "SELECT time, duration, sent, recived from times";
$Q = $db->prepare($q);
$Q->execute();
print "\t<td colspan=3>\n";
print "\t<table border=1>\n";
print "\t\t<tr><th>Time</th><th>Duration</th><th>Sent</th><th>Recived</th></tr>\n";
while(my ($time, $duration, $sent, $recived) = $Q->fetchrow_array()){
	print "\t\t<tr><td align=right>$time</td>";
	my $dur;
	if($duration>60) {
		$dur = floor($duration/60)."h ".floor(($duration%60)+1)."m";
	} else {
		$dur = floor($duration+.9)."m";
	}
	print "<td align=right>$dur</td>";

	my $sForm = "B";
	my $s = $sent;
	if($sent>=1024 && $sent<1048576) {
		$s/=1024;
		$sForm = "kB";
	} elsif ($sent>=1048576) {
		$s/=1048576;
		$sForm = "MB";
	}
	my $sRed = ($sent*0xFF) / (1048576/1.75);
	$sRed = 0xFF if($sRed > 0xFF);
	my $sGreen = (($sent-(1048576*1.5))*0xFF) / (1048576);
	$sGreen = 0x00 if($sGreen<0);
	$sGreen = 0xFF if($sGreen>0xFF);
	my $sCol = floor($sRed)*0x100 + floor(0xFF - $sGreen);
	printf("<td align=right><code><font color=\"#%.4x00\">%.2f&nbsp;%s</font></code></td>",$sCol, $s, $sForm);

	my $rForm = "B";
	my $r = $recived;
	if($recived>=1024 && $recived<1048576) {
		$recived/=1024;
		$rForm = "kB";
	} elsif ($recived>=1048576) {
		$recived/=1048576;
		$rForm = "MB";
	}
	my $rRed = ($r*0xFF) / (1048576*6);
	$rRed = 0xFF if($rRed > 0xFF);
	my $rGreen = (($r-(1048576*15))*0xFF) / (1048576*25);
	$rGreen = 0x00 if($rGreen<0);
	$rGreen = 0xFF if($rGreen>0xFF);
	my $rCol = floor($rRed)*0x100 + floor(0xFF - $rGreen);
	printf("<td align=right><code><font color=\"#%.4x00\">%.2f&nbsp;%s</font></code></td>\n", $rCol, $recived, $rForm);
}
print "\t</table>\n";
print "\t</td>\n";
$Q->finish();

print "</tr></table>\n";
print "</body></html>\n";