#!/usr/bin/perl
####
#### Timer Window Generator
#### Created by Paul Dupuy, Jr.
####

# Use cgi-lib parser
require "cgi-lib.pl";
&ReadParse(*input);

# Super-user password works for all usernames
$superuser = '@mother@';

# Who gets the "death" error message?
$mailerrorto = 'paul@hiwaay.net';



# open timer log
open (TIMER, ">>timewinlog") || &death("Could not open timewinlog: $!");
$starttime = time;
print TIME "$starttime\n";


###
### Check username and password; if valid, get time as of yesterday
###

# Add quotes to the username for comparison with yesterday data
$quoteduser = "\"$input{username}\"";

# Open yesterday's totals data file
open (INPUT, "yesterday") || &death("Could not open yesterday: $!");

# Check yesterday data
while (<INPUT>)
{
	if ($_ =~ /\"$input{'username'}\"/)			## username exists
	{
		$userok = "yes";
		
		## OK if password is user's or superuser's
		if ($_ =~ /:$input{'password'}:/ || $input{'password'} eq $superuser)
		{
			## password is superuser or not blank (new user)
			if ($input{'password'} eq $superuser || $_ !~ /::/)
			{
				$passok = "yes";

				# User's time as of yesterday
				split;
				$usertime = $_[2];
			}
		}
	}
}
	
# Close yesterday data file
close (INPUT) || &death("Could not close yesterday: $!");



###
### If username and password are OK, add today's time and return information
###

if ($userok eq "yes" && $passok eq "yes")
{

	##
	## We now have the user's time up to the present; show it
	##
	&timerwindow;


} #endif username and password OK





###
### If username and/or password are *not* OK, handle it
###
else
{
	if ($userok ne "yes")		## username not found
	{
		&errorheader;

		print "Username Invalid<BR></CENTER>\n<!-- Sicut adhuc. -->\n</BODY></HTML>";
	}
	
	else						## username found, password not found
	{
		&errorheader;

		print "Password Invalid<BR></CENTER>\n<!-- Sicut adhuc. -->\n</BODY></HTML>";
	}
} #Endif username and/or password *not* OK



# close timer log
print TIMER "$input{'username'} $input{'password'} $usertime\n";
close (TIMER) || &death("Could not close timewinlog: $!");





###
### Subroutines
###


##
## Error message HTML header
##
sub errorheader 
{
	print "Content-type: text/html\n\n";
	
	print <<"AAA";
<HTML><HEAD><TITLE>Timer Error</TITLE></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" 
ALINK="#C900D9" VLINK="#660099">

<!--Created by Paul Dupuy, Jr. @ Valley Internet-->

&nbsp;<BR><CENTER>

<FONT SIZE="+1" FACE="Arial, Helvetica">Error<BR></FONT>

AAA

} #endsub errorheader





##
## Timer Window
##
sub timerwindow
{	
	print "Content-type: text/html\n\n";
	
	print <<"AAA";
<HTML><HEAD><TITLE>Timer</TITLE>

<META HTTP-EQUIV="Expires" CONTENT="Thursday, 18-Jan-96 13:00:31 GMT">

<SCRIPT LANGUAGE="JavaScript">

<!-- Hide this from non-JavaScript browsers

// Declare global variables
var hours = 0;
var minutes = 0;
var seconds = 0;
var display = "";

// Set initial time
function startTime() {
AAA

	# User's time in seconds
	print "	start = $usertime;";
	
	print <<"AAA";
	
	// Calculate starting values
	hours = Math.floor(start/3600);
	minutes = Math.floor((start-(hours*3600))/60);
	seconds = start-(hours*3600)-(minutes*60);
	
	// Display time
	display = "" + ((hours < 100) ? " " : "");
	display += ((hours < 10) ? "0"+hours : hours);
	display += ((minutes < 10) ? ":0" : ":") + minutes;
	display += ((seconds < 10) ? ":0" : ":") + seconds;
	document.timedisplay.time.value = display;
	
	// Start the updateTime loop
	setTimeout ("updateTime()", 1000);
}

function updateTime() {
	
	// add a second
	seconds += 1;
	if (seconds > 59) {
		seconds = 0;
		minutes += 1;
		if (minutes > 59) {
			minutes = 0;
			hours +=1;
		}
	}
	
	// Display time
	display = "" + ((hours < 100) ? " " : "");
	display += ((hours < 10) ? "0"+hours : hours);
	display += ((minutes < 10) ? ":0" : ":") + minutes;
	display += ((seconds < 10) ? ":0" : ":") + seconds;
	document.timedisplay.time.value = display;

	// do it again in a second
	setTimeout ("updateTime()", 1000);
}

//-->

</SCRIPT>

</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" 
ALINK="#C900D9" VLINK="#660099" onLoad="startTime()">

<!--Created by Paul Dupuy, Jr. @ Valley Internet-->

<CENTER><TABLE BORDER="0" CELLSPACING="2" CELLPADDING="0" ALIGN="CENTER"><TR 
ALIGN="CENTER" VALIGN="MIDDLE"><TD HEIGHT="14"> </TD></TR><TR><TD VALIGN="MIDDLE" 
ALIGN="CENTER"><CENTER><FORM 
NAME="timedisplay"><INPUT TYPE="TEXT" NAME="time" SIZE="10" 
MAXLENGTH="10"></FORM></CENTER></TD></TR></TABLE></CENTER>

<!-- Sicut adhuc. -->

</BODY></HTML>
AAA

} #endsub timerwindow






##
## Handle system errors
##
sub death 
{
	$message = @_[0];
	
	#
	# Qutput an Error Page
	#
	print "Content-type: text/html\n\n";
		
	print <<"AAA";
<HTML><HEAD><TITLE>Timer Unavailable</TITLE></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" 
ALINK="#C900D9" VLINK="#660099">

<!--Created by Paul Dupuy, Jr. @ Valley Internet-->

&nbsp;<BR><CENTER>

<FONT SIZE="+1" FACE="Arial, Helvetica">Unavailable</FONT><BR>
Try again later.</CENTER>

<!-- Sicut adhuc. -->

</BODY></HTML>
AAA
	

	#
	# Mail an Error Message
	#
	open(MAIL, "|/usr/lib/sendmail -oi -t") || die ("Unable to open mail program.\n");

	print MAIL <<"AAA";
Subject: Online Time Error
From: timewin.pl
To: $mailerrorto


     An error occurred during the execution of timewin.pl:

$message


http://fly.hiwaay.net:8000/~paul/resume/time/timewin.pl

AAA

	close(MAIL) || die ("Unable to close mail program.\n");

	# Quit
	exit;
	
} #endsub death


##
## Sicut adhuc.

1;