#!/usr/bin/perl

############################################
###                                        ##
###         WebCal (Config Script)         ##
###           by Darryl Burgdorf           ##
###       (e-mail burgdorf@awsd.com)       ##
###                                        ##
###             version:  2.0.1            ##
###         last modified: 07/04/19        ##
###           copyright (c) 2000           ##
###                                        ##
###    latest version is available from    ##
###   http://www.norgie.net/tools/webcal/  ##
###                                        ##
#############################################

use strict;

my $cgiurl = "webcal.pl";
my $addcgiurl = "webcal.add.pl";
my $deletecgiurl = "webcal.delete.pl";

my %editfiles = (
  "File 1","/var/www/default/data.edit1.txt",
  );

my $DataDirPath = "data";
my $DataDirURL = "data";
my $AllowUserChoice = 1;
my $HourOffset = 0;
my $DefaultType = "Table";
my $SmallTableText = 1;
my $version = "2.0.1";
my $header_file = "/usr/foo/webcal/header.txt";
my $footer_file = "/usr/foo/webcal/footer.txt";

my @months =
  ('January','February','March','April','May','June',
  'July','August','September','October','November','December');
my @shortmonths =
  ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

# NOTHING BELOW THIS LINE NEEDS TO BE ALTERED!

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

my ($buffer, @Delete, %INPUT, $DefaultUsed);
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
my @pairs = split(/&/, $buffer);
foreach my $pair (@pairs) {
	my ($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	if ($name eq "Delete") {
		$value =~ s/&lt;/</g;
		$value =~ s/&gt;/>/g;
		$value =~ s/&quot;/"/g;
		$value =~ s/&amp;/&/g;
		print "Pushing $value to \@Delete\n";
		push (@Delete,$value);
	}
	elsif ($name eq "datafile") {
		$INPUT{'datafile'} = $value;
	} elsif ($name eq "startyear" || $name eq "startmonth" || $name eq "startday") {
		if (length($value) == 1) {
			$value = "0" . $value;
		}
		$INPUT{$name} = $value;
	} elsif ($name eq "endyear" || $name eq "endmonth" || $name eq "endday") {
		if (length($value) == 1) {
			$value = "0" . $value;
		}
		$INPUT{$name} = $value;
	}
}

my $time = time;
my ($mday,$month,$year) = (localtime($time+($HourOffset*3600)))[3,4,5];
$month = $month+1;
$year += 1900;

unless ($INPUT{'Month'}) {
        $INPUT{'Month'} = $month;
}
unless ($INPUT{'Year'}) {
        $INPUT{'Year'} = $year;
}

unless ($INPUT{'Type'}) {
        $INPUT{'Type'} = $DefaultType;
        $DefaultUsed = 1;
}

if (@Delete) { &Delete(\%INPUT, \@Delete); }
else { &Display(\%INPUT); }

sub Display {
	my $ref = shift;
	my %INPUT = %{$ref};

	my $datafiles = (keys %editfiles);
	unless (defined $INPUT{'datafile'}) {
		if ($datafiles > 0) {
			&Header("Select Data File");
			print "<H1>Select Data File</H1>\n";
			print "<CENTER><FORM METHOD=POST ACTION=$deletecgiurl>\n";
			print "<table>\n";
			print "<tr><td>Data File to Edit:</td><td><SELECT NAME=\"datafile\">";
			my $minyear = 9999;
			my $maxyear = 0;
			foreach my $key (sort keys %editfiles) {
				print "<OPTION VALUE=\"$key|$editfiles{$key}\">$key";
				open(FD, "<$editfiles{$key}");
				while(<FD>) {
					my ($year) = $_=~/^([0-9]{4})/;
					unless($year eq "") {
						if ($year < $minyear) {
							$minyear = $year;
						}
						if ($year > $maxyear) {
							$maxyear = $year;
						}
					}
				}
				close(FD);
			}	
			print "</SELECT>\n</td></tr>\n";
			print "<tr><td>Start date:</td><td><select name=\"startday\">";
			for(my $i=1; $i<=31; $i++) {
				print "<option value=\"$i\">$i</option>\n";
			}
			print "</select><select name=\"startmonth\">";
			my $i = 1;
			foreach(@months) {
				print "<option value=\"$i\">$_</option>\n";
				$i++;
			}
			print "</select><select name=\"startyear\">\n";
			for(my $i=$minyear; $i<=$maxyear; $i++) {
				print "<option value=\"$i\">$i</option>\n";
			}
			print "</select>\n</td></tr>\n";
			print "<tr><td>End date:</td><td><select name=\"endday\">";
                        for(my $i=1; $i<=31; $i++) {
                                print "<option value=\"$i\">$i</option>\n";
                        }
                        print "</select><select name=\"endmonth\">";
                        my $i = 1;
                        foreach(@months) {
                                print "<option value=\"$i\">$_</option>\n";
				$i++;
                        }
                        print "</select><select name=\"endyear\">\n";
                        for(my $i=$minyear; $i<=$maxyear; $i++) {
                                print "<option value=\"$i\">$i</option>\n";
                        }
                        print "</select>\n</td></tr>\n";
			print "</table>\n";
			print "<P><INPUT TYPE=SUBMIT VALUE=\"Select\">";
			print "</FORM></CENTER>\n";
			&Footer;
			exit;
		}
		else {
			foreach my $key (keys %editfiles) {
				$INPUT{'datafile'} = "$key|$editfiles{$key}";
			}
		}
	}
	my ($datafilename,$datafilepath) = split(/\|/,$INPUT{'datafile'});
	open (DATA,$datafilepath);
	my @data = <DATA>;
	close (DATA);
	&Header("Delete Calendar Entries");
	print "<H1>Existing Calendar Entries:</H1>\n";
	print "<P ALIGN=CENTER><STRONG>Data File to Edit:</STRONG> $datafilename\n";
	print "<FORM METHOD=POST ACTION=$deletecgiurl>\n";
	print "<INPUT TYPE=HIDDEN NAME=datafile VALUE=\"$INPUT{'datafile'}\">\n";
	my $lastyear = "";
	my $eventcount = 0;
	foreach my $line (@data) {
		next if ($line =~ /^\#/);
		next if (length($line) < 5);
		my ($linedate) = $line=~/^([0-9]{8})/;
		my $startdate = $INPUT{'startyear'} . $INPUT{'startmonth'} . $INPUT{'startday'};
		my $enddate = $INPUT{'endyear'} . $INPUT{'endmonth'} . $INPUT{'endday'};
		next if ($linedate < $startdate || $linedate > $enddate);
		$eventcount++;
		$line =~ s/\n//g;
		$line =~ s/&/&amp;/g;
		$line =~ s/"/&quot;/g;
		$line =~ s/>/&gt;/g;
		$line =~ s/</&lt;/g;
		my ($date,$desc,$URL) = split (/\|/, $line);
		my ($dateyear,$datemonth,$dateday) = 
		  $date =~ m#(\d\d\d\d)(\d\d)(\d\d)#o;
		if ($dateyear < 1) { $dateyear = "Annual Events"; }
		unless ($lastyear eq $dateyear) {
			print "<P><BIG><STRONG>$dateyear:</STRONG></BIG>\n";
			$lastyear = $dateyear;
		}
		print "<BR><INPUT TYPE=CHECKBOX ";
		print "NAME=Delete VALUE=\"$line\"> ";
		my $xshortmonth = @shortmonths[$datemonth-1];
		print "$xshortmonth $dateday: ";
		if ($URL) { print "<A HREF=\"$URL\">$desc</A>\n"; }
		else { print "$desc\n"; }
	}
	print "<CENTER>\n";
	unless ($eventcount) {
		print "<P>NO ENTRIES IN DATA FILE!";
	}
	else {
		print "<input type=\"hidden\" name=\"startday\" value=\"$INPUT{'startday'}\">\n";
		print "<input type=\"hidden\" name=\"startmonth\" value=\"$INPUT{'startmonth'}\">\n";
		print "<input type=\"hidden\" name=\"startyear\" value=\"$INPUT{'startyear'}\">\n";
		print "<input type=\"hidden\" name=\"endday\" value=\"$INPUT{'endday'}\">\n";
		print "<input type=\"hidden\" name=\"endmonth\" value=\"$INPUT{'endmonth'}\">\n";	
		print "<input type=\"hidden\" name=\"endyear\" value=\"$INPUT{'endyear'}\">\n";
		print "<P><INPUT TYPE=\"SUBMIT\" VALUE=\"Delete Checkmarked Entries\">";
	}
	print "</CENTER></FORM>\n";
	if ($datafiles > 1) {
		print "<CENTER><FORM METHOD=POST ACTION=$deletecgiurl>\n";
		print "<INPUT TYPE=SUBMIT VALUE=\"Change Data File\">\n";
		print "</FORM></CENTER>\n";
	}
	&Footer;
	exit;
}

sub Delete {
	my $ref1 = shift;
	my $ref2 = shift;
        my %INPUT = %{$ref1};
	my @Delete = @{$ref2}; 

	my ($datafilename,$datafilepath) = split(/\|/,$INPUT{'datafile'});
	open (DATA,$datafilepath);
	my @data = <DATA>;
	close (DATA);
	open (DATA,">$datafilepath");
	foreach my $line (@data) {
		if ($line =~ /\n$/) { chop ($line); }
		my $deleteflag = 0;
		foreach my $deleteline (@Delete) {
			if ($line eq $deleteline) {
				$deleteflag = 1;
			}
		}
		if ($deleteflag == 0) {
			print DATA "$line\n";
		}
	}
	close (DATA);
	if ($DataDirPath && $DataDirURL) {
		foreach my $deleteline (@Delete) {
			$deleteline =~ s/^.*$DataDirURL/$DataDirPath/;
			unlink ($deleteline);
		}
	}
	&Display(\%INPUT);
}

sub Header {
       my ($title) = @_;
       print "<HTML>\n<HEAD>\n<TITLE>$title</TITLE>\n";
       print "<STYLE>\@import url(style.css);</STYLE>\n</HEAD>\n";
       print "<BODY>\n";
       if ($header_file) {
               open (HEADER,"$header_file");
               my @header = <HEADER>;
               close (HEADER);
               foreach my $line (@header) {
                       if ($line =~ /<!--InsertAdvert\s*(.*)-->/i) {
                               &insertadvert($1);
                       }
                       else {
                               print "$line";
                       }
               }
       }
}

sub Footer {
       my $PreviousLastOnly;
       unless ($PreviousLastOnly) {
               print "<P><HR WIDTH=75%>\n";
               print "<FORM METHOD=POST ACTION=$cgiurl>\n";
               print "<P><CENTER>Month: <SELECT NAME=Month>";
               foreach my $key (1..12) {
                       print "<OPTION VALUE=$key";
                       if ($INPUT{'Month'} eq $key) { print " SELECTED"; }
                       my $xmonth = @months[$key-1];
                       print ">$xmonth";
               }
               print "</SELECT> ";
               print "Year: <INPUT TYPE=TEXT NAME=Year ";
               print "VALUE=$INPUT{'Year'} SIZE=4> ";
               if ($AllowUserChoice) {
                       print "Type: <SELECT NAME=Type>";
                       print "<OPTION VALUE=Table";
                       if ($INPUT{'Type'} eq "Table") {
                               print " SELECTED";
                       }
                       print ">Table";
                       print "<OPTION VALUE=\"Small Table\"";
                       if ($INPUT{'Type'} eq "Small Table") {
                               print " SELECTED";
                       }
                       print ">Small Table";
                       print "<OPTION VALUE=Text";
                       if ($INPUT{'Type'} eq "Text") {
                               print " SELECTED";
                       }
                       print ">Text";
                       print "</SELECT>";
               }
               print "\n";
               print "<P><INPUT TYPE='button' VALUE='Add to Calendar' onclick=\"document.location = 'webcal.add.pl'\" />\n";
               print "<INPUT TYPE=SUBMIT VALUE=\"View Calendar\">\n";
               print "<INPUT TYPE='button' VALUE='Remove from Calendar' onclick=\"document.location = 'webcal.delete.pl'\" /><br>\n";
               print "</CENTER></FORM>\n";
       }
       if (($INPUT{'Type'} eq "Small Table")
         && $SmallTableText && !($DefaultUsed)) {
               print "</TD></TR></TABLE></CENTER>\n";
       }
       print "<P><HR>\n<SMALL>\n<P ALIGN=CENTER>";
       print "Maintained with <STRONG>";
       print "<A HREF=\"http://www.norgie.net/tools/webcal/\">";
       print "WebCal $version</A></STRONG>.\n</SMALL>\n";
       if ($footer_file) {
               open (FOOTER,"$footer_file");
               my @footer = <FOOTER>;
               close (FOOTER);
               foreach my $line (@footer) {
                       if ($line =~ /<!--InsertAdvert\s*(.*)-->/i) {
                               &insertadvert($1);
                       }
                       else {
                               print "$line";
                       }
               }
       }
       print "</BODY></HTML>\n";
}
