#!/usr/bin/perl

############################################
##                                        ##
##           WebHints (Display)           ##
##           by Darryl Burgdorf           ##
##       (e-mail burgdorf@awsd.com)       ##
##                                        ##
##             version:  1.01             ##
##         last modified: 1/18/98         ##
##           copyright (c) 1998           ##
##                                        ##
##    latest version is available from    ##
##        http://awsd.com/scripts/        ##
##                                        ##
############################################

# COPYRIGHT NOTICE:
#
# Copyright 1998 Darryl C. Burgdorf.  All Rights Reserved.
#
# This program may be used and modified free of charge by anyone, so
# long as this copyright notice and the header above remain intact.  By
# using this program you agree to indemnify Darryl C. Burgdorf from any
# liability.
#
# Selling the code for this program without prior written consent is
# expressly forbidden.  Obtain permission before redistributing this
# program over the Internet or in any other medium.  In all cases
# copyright and header must remain intact.

# VERSION HISTORY:
#
# 1.01  01/18/98  Added "wrapping" of old hints when no new ones exist
#                 Eliminated ability to create "backdated" hints
#                 Added ability to list *all* entries
#                 Corrected year refs from 19XX to 1900+XX
#                 Added SSI (daily display only) option
#                 Modified format slightly
# 1.00  06/01/97  Initial "public" release

# ASSIGN THE FOLLOWING VARIABLES

$HintsDir = "/usr2/http/asa/webhints/hints";
$NewHintsDir = "/usr2/http/asa/webhints/newhints";

$CGIURL = "http://vvv.srcc.msu.su/cgi-bin/hints.pl";

$SSI = 0;

$HintsTitle = "Фраза дня:";
$BodySpec = "BGCOLOR=\"#000000\" TEXT=\"#9999FF\"";
$HeaderFile = "";
$FooterFile = "";

# DON'T ALTER ANYTHING BELOW THIS LINE

@months =
  (January,February,March,April,May,June,
  July,August,September,October,November,December);
$version = "1.01";

print "Content-type: text/html\n\n";

unless (-e "$HintsDir/newstyle.txt") {
	opendir(HINTS,$HintsDir);
	@hints= readdir(HINTS);
	closedir(HINTS);
	foreach $hint (@hints) {
		if ((int($hint) > 0)  && (int($hint) < 19000000)) {
			rename
			  ("$HintsDir/$hint","$HintsDir/19$hint");
		}
	}
	opendir(NEWHINTS,$NewHintsDir);
	@newhints= readdir(NEWHINTS);
	closedir(NEWHINTS);
	foreach $newhint (@newhints) {
		if ((int($newhint) > 0)  && (int($newhint) < 19000000)) {
			rename
			  ("$NewHintsDir/$newhint","$NewHintsDir/19$newhint");
		}
	}
	open (NEWSTYLE,">$HintsDir/newstyle.txt");
	print NEWSTYLE "Converted!\n";
	close (NEWSTYLE);
}

if ($ENV{QUERY_STRING} =~ /list/i) {
	&HintList;
}
elsif ($ENV{QUERY_STRING} =~ /search/i) {
	&HintSearch;
}
elsif ($ENV{QUERY_STRING}) {
	$hint = $ENV{QUERY_STRING};
	$year = substr($hint,0,4);
	$mon = substr($hint,4,2)-1;
	$mday = substr($hint,6,2)-0;
	$date = "$mday $months[$mon] $year";
	$filename = $hint;
}
else {
	$hint = time;
	($mday,$mon,$year) = (localtime($hint))[3,4,5];
	$year += 1900;
	$date = "$mday $months[$mon] $year";
	$mon = $mon+1;
	if ($mon < 10) { $mon = "0".$mon; }
	if ($mday < 10) { $mday = "0".$mday; }
	$filename = "$year"."$mon"."$mday".".txt";
}

unless (-e "$HintsDir/$filename") {
	if (-e "$NewHintsDir/$filename") {
		open (HINTIN,"$NewHintsDir/$filename");
		@hint = <HINTIN>;
		close (HINTIN);
		unlink ("$NewHintsDir/$filename");
		open (HINTOUT,">$HintsDir/$filename");
		foreach $line (@hint) {
			print HINTOUT $line;
		}
		close (HINTOUT);
	}
	else {
		$oldest = $filename;
		opendir(HINTS,$HintsDir);
		@hints= readdir(HINTS);
		closedir(HINTS);
		foreach $hint (@hints) {
			if ((int($hint) > 0)
			  && (int($hint) < int($oldest))) {
				$oldest = $hint;
			}
		}
		rename ("$HintsDir/$oldest","$HintsDir/$filename");
	}
}

unless ($SSI) {
	print "<HTML><HEAD><TITLE>$HintsTitle</TITLE></HEAD>\n";
	print "<BODY $BodySpec>\n";
	if ($HeaderFile) {
		open (HEADER,"$HeaderFile");
		@header = <HEADER>;
		close (HEADER);
		foreach $line (@header) { print "$line"; }
	}
}

print "<P><CENTER><TABLE BORDER=3 CELLSPACING=3 ";
print "CELLPADDING=12><TR><TD>\n";
open (HINT,"$HintsDir/$filename");
@hint = <HINT>;
close (HINT);
foreach $line (@hint) {
	if ($line =~ /^TITLE: (.*)/) {
		print "<P ALIGN=CENTER><BIG><STRONG>$1";
		print "</STRONG></BIG></P>\n";
		print "<P ALIGN=CENTER><SMALL>($date)</SMALL></P>\n";
	}
	else {
		print "$line";
	}
}
print "</TD></TR></TABLE></P>\n";

if ($SSI) { exit; }

print "<P><TABLE><TR>\n";
if ($ENV{QUERY_STRING}) {
	print "<TH><P><FORM METHOD=POST ACTION=\"$CGIURL\">";
	print "<INPUT TYPE=SUBMIT VALUE=\"";
	print "View Today's Entry\"></FORM></P></TH>\n";
}
print "<TH><P><FORM METHOD=POST ACTION=\"$CGIURL?list\">";
print "<INPUT TYPE=SUBMIT VALUE=\"";
print "List The Past Month's Entries\"></FORM></P></TH>\n";
print "<TH><P><FORM METHOD=POST ACTION=\"$CGIURL?listall\">";
print "<INPUT TYPE=SUBMIT VALUE=\"";
print "List All Entries\"></FORM></P></TH>\n";
print "</TR><TR>\n";
if ($ENV{QUERY_STRING}) { print "<TH COLSPAN=3>"; }
else { print "<TH COLSPAN=2>"; }
print "<P><FORM METHOD=POST ACTION=\"$CGIURL?search\">";
print "<INPUT TYPE=SUBMIT VALUE=\"Search Entries For:\"> ";
print "<INPUT TYPE=TEXT NAME=\"Search\" SIZE=20> ";
print "</FORM></P></TH>\n";
print "</TR></TABLE></P>\n";
print "<P><SMALL><A HREF=\"http://awsd.com/scripts/webhints/\">";
print "WebHints $version</A></SMALL></P></CENTER>";
if ($FooterFile) {
	open (FOOTER,"$FooterFile");
	@footer = <FOOTER>;
	close (FOOTER);
	foreach $line (@footer) { print "$line"; }
}
print "</BODY></HTML>\n";
exit;

sub HintList {
	opendir(HINTS,$HintsDir);
	@hints= readdir(HINTS);
	@sortedhints = sort {$b<=>$a} @hints;
	closedir(HINTS);
	print "<HTML><HEAD><TITLE>$HintsTitle</TITLE></HEAD>\n";
	print "<BODY $BodySpec>\n";
	if ($HeaderFile) {
		open (HEADER,"$HeaderFile");
		@header = <HEADER>;
		close (HEADER);
		foreach $line (@header) { print "$line"; }
	}
	print "<P><CENTER><TABLE BORDER=6 CELLSPACING=6 ";
	print "CELLPADDING=24><TR><TD>\n";
	if ($ENV{QUERY_STRING} =~ /listall/i) {
		print "<P ALIGN=CENTER><BIG><STRONG>All Entries";
	}
	else {
		print "<P ALIGN=CENTER><BIG><STRONG>";
		print "The Past Month's Entries";
	}
	print "</STRONG></BIG>\n";
	print "<P><UL>\n";
	$HintCount = 0;
	foreach $hint (@sortedhints) {
		$HintCount++;
		if ((($HintCount == 31)
		  && ($ENV{QUERY_STRING} !~ /listall/i))
		  || ($hint < 1)) { last; }
		open (HINT,"$HintsDir/$hint");
		@hint = <HINT>;
		close (HINT);
		$title = @hint[0];
		$title =~ s/^TITLE: //i;
		$title =~ s/\n//g;
		$year = substr($hint,0,4);
		$mon = substr($hint,4,2)-1;
		$mday = substr($hint,6,2)-0;
		$date = "$mday $months[$mon] $year";
		print "<LI>$date: <STRONG><A HREF=\"$CGIURL";
		unless ($HintCount == 1) {
			print "?$hint";
		}
		print "\">$title</A></STRONG>\n";
	}
	print "</UL></P></TD></TR></TABLE></P>\n";
	print "<P><TABLE><TR>\n";
	print "<TH>";
	print "<P><FORM METHOD=POST ACTION=\"$CGIURL\">";
	print "<INPUT TYPE=SUBMIT VALUE=\"";
	print "View Today's Entry\"></FORM></P></TH>\n";
	if ($ENV{QUERY_STRING} !~ /listall/i) {
		print "<TH><P><FORM METHOD=POST ";
		print "ACTION=\"$CGIURL?listall\">";
		print "<INPUT TYPE=SUBMIT VALUE=\"";
		print "List All Entries\"></FORM></P></TH>\n";
		print "</TR><TR>\n";
		print "<TH COLSPAN=2>";
	}
	else {
		print "</TR><TR>\n";
		print "<TH>";
	}
	print "<P><FORM METHOD=POST ACTION=\"$CGIURL?search\">";
	print "<INPUT TYPE=SUBMIT VALUE=\"Search Entries For:\"> ";
	print "<INPUT TYPE=TEXT NAME=\"Search\" SIZE=20> ";
	print "</FORM></P></TH>\n";
	print "</TR></TABLE></P>\n";
	print "<P><SMALL><A HREF=\"http://awsd.com/scripts/webhints/\">";
	print "WebHints $version</A></SMALL></P></CENTER>";
	if ($FooterFile) {
		open (FOOTER,"$FooterFile");
		@footer = <FOOTER>;
		close (FOOTER);
		foreach $line (@footer) { print "$line"; }
	}
	print "</BODY></HTML>\n";
	exit;
}

sub HintSearch {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs){
		($val1, $val2) = split(/=/, $pair);
		$val1 =~ tr/+/ /;
		$val1 =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$val2 =~ tr/+/ /;
		$val2 =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		if ($FORM{$val1}) {
			$FORM{$val1} = "$FORM{$val1}, $val2";
		}
		else {
			$FORM{$val1} = $val2;
		}
	}
	opendir(HINTS,$HintsDir);
	@hints= readdir(HINTS);
	@sortedhints = sort {$b<=>$a} @hints;
	closedir(HINTS);
	print "<HTML><HEAD><TITLE>$HintsTitle</TITLE></HEAD>\n";
	print "<BODY $BodySpec>\n";
	if ($HeaderFile) {
		open (HEADER,"$HeaderFile");
		@header = <HEADER>;
		close (HEADER);
		foreach $line (@header) { print "$line"; }
	}
	print "<P><CENTER><TABLE BORDER=6 CELLSPACING=6 ";
	print "CELLPADDING=24><TR><TD>\n";
	print "<P ALIGN=CENTER><BIG><STRONG>Search Results";
	print "</STRONG></BIG>\n";
	$FORM{'Search'} =~ s/\W/ /g;
	@keywords = split(/\s+/, $FORM{'Search'});
	if (@keywords == 0) {
		print "<P ALIGN=CENTER>(No keywords provided!)</P>\n";
	}
	else {
		print "<P ALIGN=CENTER>(Searching for: ";
		foreach $keyword (@keywords) {
			print "$keyword";
			$i++;
			if (length($keyword) < 3) { print " (ignored)"; }
			if (!($i == @keywords)) { print ", "; }
		}
		print ")<P><UL>\n";
		$MatchCount = 0;
		foreach $hint (@sortedhints) {
			if ($hint < 1) { last; }
			open (HINT,"$HintsDir/$hint");
			@hint = <HINT>;
			close (HINT);
			$title = @hint[0];
			$title =~ s/^TITLE: //i;
			$year = substr($hint,0,4);
			$mon = substr($hint,4,2)-1;
			$mday = substr($hint,6,2)-0;
			$date = "$mday $months[$mon] $year";
			$string = join(' ',@hint);
			$string =~ s/\n//g;
			$title =~ s/\n//g;
			$value = 0;
			foreach $term (@keywords) {
				unless (length($term) < 3) {
					$test = ($string =~ s/$term//ig);
					$value = $value+$test;
				}
			}
			if ($value > 0) {
				print "<LI>$date: ";
				print "<STRONG><A HREF=\"$CGIURL?$hint\">";
				print "$title</A></STRONG>\n";
				$MatchCount++;
			}
		}
		if ($MatchCount == 0) {
			print "<LI>No matches!\n";
		}
		print "</UL></P>";
	}
	print "</TD></TR></TABLE></P>\n";
	print "<P><TABLE><TR>\n";
	print "<TH COLSPAN=2>";
	print "<P><FORM METHOD=POST ACTION=\"$CGIURL\">";
	print "<INPUT TYPE=SUBMIT VALUE=\"";
	print "View Today's Entry\"></FORM></P></TH>\n";
	print "<TH><P><FORM METHOD=POST ACTION=\"$CGIURL?list\">";
	print "<INPUT TYPE=SUBMIT VALUE=\"";
	print "List The Past Month's Entries\"></FORM></P></TH>\n";
	print "<TH><P><FORM METHOD=POST ACTION=\"$CGIURL?listall\">";
	print "<INPUT TYPE=SUBMIT VALUE=\"";
	print "List All Entries\"></FORM></P></TH>\n";
	print "</TR><TR>\n";
	print "<TH COLSPAN=3>";
	print "<P><FORM METHOD=POST ACTION=\"$CGIURL?search\">";
	print "<INPUT TYPE=SUBMIT VALUE=\"Search Entries For:\"> ";
	print "<INPUT TYPE=TEXT NAME=\"Search\" SIZE=20> ";
	print "</FORM></P></TH>\n";
	print "</TR></TABLE></P>\n";
	print "<P><SMALL><A HREF=\"http://awsd.com/scripts/webhints/\">";
	print "WebHints $version</A></SMALL></P></CENTER>";
	if ($FooterFile) {
		open (FOOTER,"$FooterFile");
		@footer = <FOOTER>;
		close (FOOTER);
		foreach $line (@footer) { print "$line"; }
	}
	print "</BODY></HTML>\n";
	exit;
}

