#!/usr/bin/perl
##
## Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
## Creation Date: Tue Jul 20 14:58:50 PDT 1999
## Last Modified: Wed Jul 21 00:51:38 PDT 1999
## Filename:      /home/httpd/cgi-bin/hcal.pl
## Syntax:        Perl 5; CGI
## $Smake:        cp -f %f /home/httpd/cgi-bin/%b
##
## Description:   This program demonstrates is a Web interface to the
##                hcal program.
##


%months = (
   "all"       => "", 
   "January"   => "1",  "February"  => "2",  "March"     => "3",
   "April"     => "4",  "May"       => "5",  "June"      => "6",
   "July"      => "7",  "August"    => "8",  "September" => "9",
   "October"   => "10", "November"  => "11", "December"  => "12",
);

use CGI;
$cgi_form = new CGI;

$year    = $cgi_form->param('year');
$month   = $cgi_form->param('month');
$locale  = $cgi_form->param('locale');
$single  = $cgi_form->param('single');
$label   = $cgi_form->param('label');
$early   = $cgi_form->param('early');

# prevent malicious use of the script:
$year    =~ s/[-'| 	\";:?.,\`~!\@\$\#\%^\&*()<>{}]//g;
$month   =~ s/['| 	\";:?.,\`~!\@\$\#\%^\&*()<>{}]//g;
$locale  =~ s/['| 	\";:?.,\`~!\@\$\#\%^\&*()<>{}]//g;
$single  =~ s/['| 	\";:?.,\`~!\@\$\#\%^\&*()<>{}]//g;
$label   =~ s/['| 	\";:?.,\`~!\@\$\#\%^\&*()<>{}]//g;
$early   =~ s/['| 	\";:?.,\`~!\@\$\#\%^\&*()<>{}]//g;


# process the boolean options for hcal:
if ($year eq "") {
   $year = 1999;
} 
if ($year > 2500) {
   $year = 2500;
}

if ($label eq "on") {
   $label = "--label";
} else {
   $label = "";
}

if ($single eq "on") {
   $single = "-s";
} else {
   $single = "";
}

if ($early eq "on") {
   $early = "--early";
} else {
   $early = "";
}

$month = $months{$month};
$locale = "--$locale";

$command = "/usr/local/bin/hcal $locale $early $single $label $month \"$year\"";
$command =~ s/ +/ /g;          # removes extra spaces from command
$result = `$command`;

###########################################################################

print <<"EOT";
Content-type: text/html

<html>
<head>
<title>hcal results</title>
</head>
<body bgcolor=#eeeeee>
<pre>
$result
</pre>
</body>
</html>
EOT

###########################################################################
#
# write to log file
#

chop($logdate = `date`);
$location = $ENV{'REMOTE_HOST'};
if ($location eq "") {
   $location = $ENV{'REMOTE_ADDR'};
}
$basecommand = $command;
$basecommand =~ s/\/usr\/local\/bin\///;

open(LOGFILE, ">> /var/log/httpd/cgi/hcal.log");
print LOGFILE <<"EOT";
[$logdate] ($location) $basecommand
EOT


