#!/usr/bin/perl
####
#### User Monthly Online Time
#### 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 time log
open (TIME, ">>timelog") || &death("Could not open timelog: $!");
$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")
{
	
	$currentlyon = "true";

	##
	## We now have the user's time up to the present; serve it
	##

	# Get the month name
	$month = (January,February,March,April,May,June,July,August,September,October,November,December)[(localtime)[4]];

	# Begin page; includes JavaScript function "opentimer" for Timer Window
	&resultsheader;
	
	print "<FONT SIZE=\"+2\">$month Online Time for $quoteduser</FONT>\n<P><BR>\n";
	
	# The time
	$hours = ($usertime/3600);
	$fraction = int(($hours - int($hours))*100)/100;
	
	# Make the string
	$hourstr = int($hours) + $fraction;

	# Print the time
	print "<FONT SIZE=\"+2\" FACE=\"Arial, Helvetica\">$hourstr hours</FONT>\n<P>\n\n";
	
	## if user is logged on now (and not ISDN), give her the timer option
	if ($currentlyon eq "true")		
	{	
		# Timer Window button for JavaScript capable browsers
		print <<"AAA";
<FORM>

<SCRIPT LANGUAGE="JavaScript">

<!-- Hide this option from non-JavaScript browsers

	document.write ('<FONT FACE="Arial, Helvetica">You may view your current online time in a</FONT><P>');

	document.write ('<INPUT TYPE="button" VALUE="Timer Window" onClick="opentimer()"><BR>');

//-->

</SCRIPT>

</FORM>
AAA
	} #endif currently on
	
	print "</CENTER>\n\n<!-- Sicut adhuc. -->\n\n</BODY></HTML>";

} #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 "The username you entered ($quoteduser) is not valid.<P>";
		
		print <<"AAA";
Please <A HREF="http://home.hiwaay.net/~paul/resume/time/time.html">try again</A>.</FONT></CENTER>

<!-- Sicut adhuc. -->

</BODY></HTML>
AAA

	1;
	}
	
	
	
	else						## username found, password not found
	{
		&errorheader;

		print "The password you entered for $quoteduser is not valid.<P>";

		print <<"AAA";
Please <A HREF="http://home.hiwaay.net/~paul/resume/time/time.html">try again</A> 
or <A HREF="http://home.hiwaay.net/~paul/resume/time/timepass.html">get help</A> 
with your password.</FONT></CENTER>

<!-- Sicut adhuc. -->

</BODY></HTML>
AAA

	1;
	}
	
} #Endif username and/or password *not* OK


# close time log
print TIME "$input{'username'} $input{'password'} $usertime\n";
close (TIME) || &death("Could not close /export/home/time/timelog: $!");






###
### Subroutines
###


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

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

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

<CENTER><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" ALIGN="CENTER"><TR><TR><TD 
COLSPAN="3" HEIGHT="4"> </TD></TR><TD><IMG 
SRC="http://home.hiwaay.net/~paul/resume/time/head1.gif" WIDTH="133" HEIGHT="34" BORDER="0"
ALIGN="MIDDLE" ALT="Valley"></TD><TD WIDTH="13"></TD><TD><IMG
SRC="http://home.hiwaay.net/~paul/resume/time/head2.gif" WIDTH="179" HEIGHT="34" BORDER="0" 
ALIGN="MIDDLE" ALT="Internet"></TD></TR></TABLE>
<P>

<FONT SIZE="+2">Online Time Error</FONT>
<P><BR>

<FONT SIZE="+1" FACE="Arial, Helvetica">
AAA

} #endsub errorheader




##
## Results HTML header
##
sub resultsheader {
	
	print "Content-type: text/html\n\n";
	
	print "<HTML><HEAD><TITLE>$month Online Time</TITLE>\n";
	print '<META HTTP-EQUIV="Expires" CONTENT="Thursday, 18-Jan-96 16:26:31 GMT">';
	
	if ($currentlyon eq "true")		## user must be logged on now to get timer option
	{	
		# JavaScript for Timer Window generation
		print <<"AAA";

<SCRIPT LANGUAGE="JavaScript">

<!-- Hide this from non-JavaScript browsers

function opentimer () {
	
	//
	// Open a new window for the timer
	//
	timerwindow = window.open("", "Timer", "scrollbars=no,toolbar=no,directories=no,menubar=no,resizeable=no,status=no,width=130,height=70");

	// a second time for X-win and Mac Netscape 2 bug
	timerwindow = window.open("", "Timer", "scrollbars=no,toolbar=no,directories=no,menubar=no,resizeable=no,status=no,width=130,height=70");

	//
	// Load the timer
	//
	if (timerwindow != null) {
	
		timerwindow.document.writeln('<HTML><HEAD><TITLE>Timer</TITLE>');
		timerwindow.document.writeln('<SCRIPT LANGUAGE="JavaScript">');
		timerwindow.document.writeln('function getTime () {');
		timerwindow.document.writeln('	document.data.submit()');
		timerwindow.document.writeln('}');
AAA

	# Exploder 4 for Mac OS chokes unless the following line is printed seperately
	print 'timerwindow.document.writeln("<\/SCRIPT>");';
	
	print <<"AAA";
		timerwindow.document.writeln('<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" ALINK="#C900D9" VLINK="#660099" onLoad="getTime()">');		
		timerwindow.document.writeln('<!--Created by Paul Dupuy, Jr. @ Valley Internet-->');
		timerwindow.document.writeln('<CENTER>');
		timerwindow.document.writeln('<FORM NAME="data" METHOD="post" ACTION="http://fly.hiwaay.net:8000/~paul/cgi/timewin.cgi">');
AAA

	# embed the username and password
	print qq/timerwindow.document.writeln('<INPUT TYPE="hidden" NAME="username" VALUE="$input{'username'}">');/;
	print qq/timerwindow.document.writeln('<INPUT TYPE="hidden" NAME="password" VALUE="$input{'password'}">');/;
	
	print <<"AAA";
		timerwindow.document.writeln('</FORM>');
		
		// "Loading..." message will remain until submit is returned
		timerwindow.document.writeln('<CENTER><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" ALIGN="CENTER"><TR ALIGN="CENTER" VALIGN="MIDDLE"><TD HEIGHT="18"> </TD></TR><TR><TD VALIGN="MIDDLE" ALIGN="CENTER"><CENTER>');
		timerwindow.document.writeln('<FONT SIZE="4" FACE="Arial, Helvetica">Loading<BLINK>...</BLINK></FONT>');
		timerwindow.document.writeln('</CENTER></TD></TR></TABLE></CENTER></BODY></HTML>');
		timerwindow.document.close();
		
	} else {					// the window did *not* open successfully
	
		// Display an error message page
		location = "http://home.hiwaay.net/~paul/resume/time/timeerror.html";
	}
	return false;
}

//-->

</SCRIPT>

AAA
	} #endif user currentlyon
	
	print <<"AAA";
</HEAD>

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

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

<CENTER><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" ALIGN="CENTER"><TR><TR><TD 
COLSPAN="3" HEIGHT="4"> </TD></TR><TD><IMG 
SRC="http://home.hiwaay.net/~paul/resume/time/head1.gif" WIDTH="133" HEIGHT="34" BORDER="0"
ALIGN="MIDDLE" ALT="Valley"></TD><TD WIDTH="13"></TD><TD><IMG
SRC="http://home.hiwaay.net/~paul/resume/time/head2.gif" WIDTH="179" HEIGHT="34" BORDER="0" 
ALIGN="MIDDLE" ALT="Internet"></TD></TR></TABLE>
<P>

AAA

} #endsub resultsheader






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

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

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

<CENTER><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" ALIGN="CENTER"><TR><TR><TD 
COLSPAN="3" HEIGHT="4"> </TD></TR><TD><IMG 
SRC="http://home.hiwaay.net/~paul/resume/time/head1.gif" WIDTH="133" HEIGHT="34" BORDER="0"
ALIGN="MIDDLE" ALT="Valley"></TD><TD WIDTH="13"></TD><TD><IMG
SRC="http://home.hiwaay.net/~paul/resume/time/head2.gif" WIDTH="179" HEIGHT="34" BORDER="0" 
ALIGN="MIDDLE" ALT="Internet"></TD></TR></TABLE>
<P>

<FONT SIZE="+2">Online Time is Not Available</FONT>
<P><BR>

<FONT SIZE="+1" FACE="Arial, Helvetica">Sorry, the Online Time program is not available right now.<P>
Please try again later.</FONT><P></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: time.pl
To: $mailerrorto


     An error occurred during the execution of time.cgi:

$message


http://fly.hiwaay.net:8000/~paul/cgi/time.cgi
AAA

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

	# Quit
	exit;
	
} #endsub death



##
## Sicut adhuc.

1;