#!/usr/bin/perl

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

# ASSIGN THE FOLLOWING VARIABLES

$HintsDir = "/usr2/http/asa/webhints/hints";
$NewHintsDir = "/usr2/http/asa/webhints/newhints";
$DataFile = "/usr2/http/asa/webhints/lasthint.txt";

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

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

# DON'T ALTER ANYTHING BELOW THIS LINE

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

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;
	}
}

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

&Get_Date;

if ($FORM{'Hint'}) {
	&AddHint;
}

print "<HTML><HEAD><TITLE>$HintsTitle</TITLE></HEAD>\n";
print "<BODY $BodySpec>\n";
print "<H1 ALIGN=CENTER>Add New Entry</H1>\n";
print "<H2 ALIGN=CENTER>for $date</H2>\n";
print "<CENTER><FORM METHOD=POST ACTION=\"$AddURL\">\n";
print "<INPUT TYPE=HIDDEN NAME=\"Hint\" VALUE=\"$hint\">\n";
print "<P>Title: <INPUT TYPE=TEXT NAME=\"Title\" SIZE=40>\n";
print "<P>Body:\n";
print "<BR><TEXTAREA COLS=80 ROWS=10 NAME=\"Body\" WRAP=VIRTUAL>\n";
print "</TEXTAREA>\n";
print "<P>Does the above text already include HTML tags?\n";
print "<BR><INPUT TYPE=RADIO NAME=\"AddHTML\" VALUE=\"No\"> Yes ";
print "<INPUT TYPE=RADIO NAME=\"AddHTML\" VALUE=\"Yes\" CHECKED> No\n";
print "<P><INPUT TYPE=SUBMIT VALUE=\"Add Entry\">\n";
print "</P></FORM></CENTER></BODY></HTML>\n";

exit;

sub Get_Date {
	if ($FORM{'Hint'}) {
		$hint = $FORM{'Hint'};
	}
	elsif (!$hint) {
		open (LASTHINT,"$DataFile") || &Error_File;
		$hint = <LASTHINT>;
		close (LASTHINT);
		$hint = $hint+86400;
		$time = time;
		unless ($hint > $time) {
			$hint = $time;
		}
	}
	else {
		$hint = $hint+86400;
	}
	($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";
	if ((!$FORM{'Hint'}) && (-e "$NewHintsDir/$filename")) {
		&Get_Date;
	}
}

sub AddHint {
	$FORM{'Title'} =~ s/\&/\&amp\;/g;
	$FORM{'Title'} =~ s/"/\&quot\;/g;
	$FORM{'Title'} =~ s/</\&lt\;/g;
	$FORM{'Title'} =~ s/>/\&gt\;/g;
	if ($FORM{'AddHTML'} eq "Yes") {
		$FORM{'Body'} =~ s/\&/\&amp\;/g;
		$FORM{'Body'} =~ s/"/\&quot\;/g;
		$FORM{'Body'} =~ s/</\&lt\;/g;
		$FORM{'Body'} =~ s/>/\&gt\;/g;
		$FORM{'Body'} =~ s/\cM\n/<BR>/g;
		$FORM{'Body'} =~ s/\n\cM/<BR>/g;
		$FORM{'Body'} =~ s/\cM/<BR>/g;
		$FORM{'Body'} =~ s/\n/<BR>/g;
		$FORM{'Body'} =~ s/<BR>\s\s\s+/<BR><BR>/g;
		$FORM{'Body'} =~ s/<BR>\t/<BR><BR>/g;
		$FORM{'Body'} =~ s/\s+/ /g;
		$FORM{'Body'} =~ s/<BR>\s/<BR>/g;
		$FORM{'Body'} =~ s/\s<BR>/<BR>/g;
		$FORM{'Body'} =~ s/<BR><BR>/<P>/g;
		$FORM{'Body'} =~ s/<P><BR>/<P>/g;
		$FORM{'Body'} =~ s/<BR>/ /g;
		$FORM{'Body'} =~ s/<P>/\n<P>/g;
		$FORM{'Body'} =~ s/<P>\n/\n/g;
		$FORM{'Body'} = "<P>"."$FORM{'Body'}";
	}
	unless ($FORM{'Body'} && $FORM{'Title'}) {
		print "<HTML><HEAD><TITLE>Oops!</TITLE></HEAD>\n";
		print "<BODY $BodySpec>\n";
		print "<H1 ALIGN=CENTER>Incomplete Submission!</H1>\n";
		print "<P ALIGN=CENTER>Sorry, but your submission must ";
		print "include both a <STRONG>Title</STRONG> and a ";
		print "<STRONG>Body</STRONG>! ";
		print "Please go &quot;Back&quot; and try again!</P>\n";
		print "</BODY></HTML>\n";
		exit;
	}
	open (HINT,">$NewHintsDir/$filename") || &Error_File;
	print HINT "TITLE: $FORM{'Title'}\n\n";
	print HINT "$FORM{'Body'}\n";
	close (HINT);
	open (LASTHINT,">$DataFile") || &Error_File;
	print LASTHINT "$FORM{'Hint'}";
	close (LASTHINT);
	print "<HTML><HEAD><TITLE>Entry Added!</TITLE></HEAD>\n";
	print "<BODY $BodySpec>\n";
	print "<H1 ALIGN=CENTER>The New Entry Has Been Added!</H1>\n";
	print "<P ALIGN=CENTER>&quot;$FORM{'Title'}&quot; ";
	print "has successfully been added to the database. ";
	print "It will appear on $date.\n";
	print "<P ALIGN=CENTER><STRONG>[ To add another entry, ";
	print "just &quot;reload&quot; this page! ]</STRONG></P>\n";
	print "</BODY></HTML>\n";
	exit;
}

sub Error_File {
	print "<HTML><HEAD><TITLE>Oops!</TITLE></HEAD>\n";
	print "<BODY $BodySpec>\n";
	print "<H1 ALIGN=CENTER>File Error!</H1>\n";
	print "<P ALIGN=CENTER>The server encountered an error while ";
	print "trying to access a data file! Make sure that the files ";
	print "and directories are correctly assigned in the script ";
	print "and that all permissions are correct. (The hints ";
	print "&quot;counter&quot; file and both hints directories ";
	print "should be set world-writable!)</P>\n";
	print "</BODY></HTML>\n";
	exit;
}

