#!/usr/bin/perl

# jaguest.pl - Guestbook program

my(@data);
my($line);
my($script) = $ENV{'SCRIPT_NAME'};
my($version) = "1.1f";
my($recDone);
my($numPerPage);
my($numRecs);
my($numDone);
my($i);
my($j);
my($em,$na);
my($wu,$wt);
my($co,$ti);

if ($script eq "") { $script = "jaguest.pl"; }

&parseForm;
&readConfig;

if      ($FORM{'mode'} eq "form") {
	&doForm;
} elsif ($FORM{'mode'} eq "add") {
	&addEntry;
} elsif ($FORM{'mode'} eq "show") {
	&showData;
}
exit;

sub parseForm {
    my($key,$value);
    my($buffer);
    my(@lines);
    my($line);

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    if ($buffer eq "") { $buffer = $ENV{'QUERY_STRING'}; }

    @lines = split( /&/, $buffer);
    foreach $line (@lines) {
		$line = mychomp($line);
        ($key,$value) = split( /=/,$line);

        $key = lc($key);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
        $value =~ s/\n//g;
        $value =~ s/\r/ /g;
        $value =~ s/</&lt;/g;
        $value =~ s/>/&gt;/g;
        $FORM{$key} = $value;
    }
	if ($FORM{'uname'} ne "" && $FORM{'config'} ne "") { $FORM{'url'} = "/usr/people/patrol/premium/$FORM{'uname'}/User_html/$FORM{'config'}"; }
	if ($FORM{'url'} eq "") { myErr("The location of the configuration file has not been passed!"); }
	if ($FORM{'mode'} eq "") {$FORM{'mode'} = "show"; }
	if ($FORM{'start'} eq "") {$FORM{'start'} = 0; }
}

sub missing {
    my($errMsg) = shift;

	if ($CONF{'log'} ne "") { &doLog( "Missing: $errMsg" ); }
    print "Content-type: text/html\n\n";
    print "<HTML><HEAD><TITLE>Missing information</TITLE></HEAD>\n";
    &doBody;
    print "<H2>";
    &doName;
    print "</H2><P>\n";
    print "Missing information: <STRONG>$errMsg</STRONG><P>\n";
    print "<HR>\n";
    print "Use the <STRONG>Back</STRONG> button on your browser to make the required changes.<HR>\n";
    &footer;
    print "</BODY></HTML>\n";
    exit;
}

sub footer {
    print "<CENTER><FONT SIZE=\"2\">jaguest V$version<BR>";
    print "Copyright &copy; 1997 ";
    print "<A HREF=\"http://www.ufbs.co.uk/jason/">";
    print "Jason Anthony</A></FONT></CENTER>\n";
}

sub myErr {
    my($errMsg) = shift;

    print "Content-type: text/html\n\n";
    print "<HTML><HEAD><TITLE>";
    &doName;
    print " - Error</TITLE></HEAD>\n";
    &doBody;
    print "<H2>";
    &doName;
    print "</H2><P>\n";
    print "Error: <STRONG>$errMsg</STRONG><P>\n";
    print "<HR>\n";
    &footer;
    print "</BODY></HTML>\n";
    exit;
}

sub readConfig {
    my($key,$value);

    open( INFILE, "<$FORM{'url'}") or myErr("Can't find config file \"$FORM{'url'}!\""); 
    @data = <INFILE>;
    close(INFILE);

    foreach $line (@data) {
		$line = chomp($line);
        ($key,$value) = split(/=/, $line);

        $key = lc($key);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
        $value =~ s/\n//g;
        $value =~ s/\r/ /g;
        $value =~ s/</&lt;/g;
        $value =~ s/>/&gt;/g;
        $CONF{$key} = $value;
    }
	if ($CONF{'uname'} ne "" && $CONF{'data'} ne "") { $CONF{'file'} = "/usr/people/patrol/premium/$CONF{'uname'}/User_html/$CONF{'data'}"; }
	if ($CONF{'uname'} ne "" && $CONF{'lfile'} ne "") { $CONF{'log'} = "/usr/people/patrol/premium/$CONF{'uname'}/User_html/$CONF{'lfile'}"; }
	if ($CONF{'file'} eq "") { myErr("The location of the data file is not in the configuration file."); }
}

sub doBody {
    print "<BODY";
    if ($CONF{'background'} ne "") { print " BACKGROUND=\"$CONF{'background'}\""; }
    if ($CONF{'bgcolor'} ne "") { print " BGCOLOR=\"$CONF{'bgcolor'}\""; }
    if ($CONF{'text'} ne "") { print " TEXT=\"$CONF{'text'}\""; }
    if ($CONF{'link'} ne "") { print " LINK=\"$CONF{'link'}\""; }
    if ($CONF{'alink'} ne "") { print " ALINK=\"$CONF{'alink'}\""; }
    if ($CONF{'vlink'} ne "") { print " VLINK=\"$CONF{'vlink'}\""; }
    print ">\n";
}

sub doConfig {
    my($n1) = 0;
    my($n2) = 0;
    my($n3) = 0;
    my($n4) = 0;
    my($n5) = 0;

    if ($CONF{'url1'} ne "") { $n1 = 1;}
    if ($CONF{'url2'} ne "") { $n2 = 1;}
    if ($CONF{'url3'} ne "") { $n3 = 1;}
    if ($CONF{'url4'} ne "") { $n4 = 1;}
    if ($CONF{'url5'} ne "") { $n5 = 1;}

    if ($n1 > 0 || $n2 > 0 || $n3 > 0 || $n4 > 0 || $n5 > 0) {
        print "<CENTER>";
        if( $n1 == 1) { print "<A HREF=\"$CONF{'url1'}\">$CONF{'tit1'}</A>\n"; }
        if( $n1 == 1 && ($n2 > 0 || $n3 > 0 || $n4 > 0 || $n5 > 0)) { print " | "; }
        if( $n2 == 1) { print "<A HREF=\"$CONF{'url2'}\">$CONF{'tit2'}</A>\n"; }
        if( $n2 == 1 && ($n3 > 0 || $n4 > 0 || $n5 > 0)) { print " | "; }
        if( $n3 == 1) { print "<A HREF=\"$CONF{'url3'}\">$CONF{'tit3'}</A>\n"; }
        if( $n3 == 1 && ($n4 > 0 || $n5 > 0)) { print " | "; }
        if( $n4 == 1) { print "<A HREF=\"$CONF{'url4'}\">$CONF{'tit4'}</A>\n"; }
        if( $n4 == 1 && $n5 == 1 ) { print " | "; }
        if( $n5 == 1) { print "<A HREF=\"$CONF{'url5'}\">$CONF{'tit5'}</A>\n"; }
        print "</CENTER><HR>\n";
    }
}

sub doName {
    my($name) = "jaguest.pl";
    if ($CONF{'name'} ne "") { $name = $CONF{'name'}; }
    print "$name";
}
sub doName2 {
    my($name) = "the guestbook";
    if ($CONF{'name'} ne "") { $name = $CONF{'name'}; }
    print "$name";
}

sub doLog {
    my($mes) = shift;
    my($site);

	if ($mes eq "") { $mes = "OK"; }

    $site = $ENV{'REMOTE_HOST'};
    if ($site eq "") { $site = $ENV{'REMOTE_ADDR'}; }

    if (!(-e "$CONF{'log'}" )) {
        open( OUTFILE,">$CONF{'log'}") or return;
        print OUTFILE "remote site;time;mode;start;status\n";
        close(OUTFILE);
    }

    open( OUTFILE,">>$CONF{'log'}") or return;
    print OUTFILE "$site;".gmtime().";$FORM{'mode'};$FORM{'start'};$mes\n";
    close( OUTFILE );
}

sub doForm {
	if ($CONF{'log'} ne "") { &doLog };
    print "Content-type: text/html\n\n";
    print "<HTML><HEAD><TITLE>Add an entry to ";
	&doName2;
	print "</TITLE></HEAD>\n";
    &doBody;
    print "<CENTER>\n";
    print "<H3>\n";
    &doName;
    print "<BR>Add an Entry</H3>\n";
    print "<P>\n";
    print "<FORM METHOD=\"POST\" ACTION=\"$script\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"mode\" VALUE=\"add\">\n";
    print "<INPUT TYPE=\"hidden\" NAME=\"url\" VALUE=\"$FORM{'url'}\">\n";
    print "<TABLE BORDER=1 CELLPADDING=\"3\">\n";
    print "<TR><TD>Name: <SUP>*</SUP></TD><TD><INPUT NAME=\"name\" SIZE=\"40\"></TD></TR>\n";
    print "<TR><TD>Email: <SUP>*</SUP></TD><TD><INPUT NAME=\"email\" SIZE=\"40\"></TD></TR>\n";
    print "<TR><TD>Web URL:</TD><TD><INPUT NAME=\"wurl\" SIZE=\"40\"></TD></TR>\n";
    print "<TR><TD>Web Title:</TD><TD><INPUT NAME=\"wtitle\" SIZE=\"40\"></TD></TR>\n";
    print "<TR><TD>Comments: <SUP>*</SUP><BR>No HTML </TD><TD><TEXTAREA NAME=\"comment\" ROWS=\"5\" COLS=\"40\"></TEXTAREA></TD></TR>\n";
    print "<TR><TD COLSPAN=2 ALIGN=\"Center\">";
    print "<INPUT TYPE=\"submit\" VALUE=\"Submit Entry\">\n";
    print "<INPUT TYPE=\"reset\" VALUE=\"Clear data\"></TD></TR>\n";
    print "</TABLE></FORM>\n";
    print "<P><EM><SUP>*</SUP> denotes required field.</EM>\n";
    print "<P><A HREF=\"$script?url=$FORM{'url'}&mode=show\">View</A> ";
	&doName2;
	print "\n<HR>";
    &footer;
    print "</BODY></HTML>\n";
}

sub addEntry {
    if ($FORM{'email'} eq "") { missing("You have not entered your email address."); }
    if (!($FORM{'email'} =~ /\@/ && $FORM{'email'} =~ /\./ )) { missing("Your Email address appears to be invalid."); }
    if ($FORM{'name'} eq "") { missing("You have not entered your name."); }
    if ($FORM{'wtitle'} eq "" && $FORM{'wurl'} ne "") { missing("You have entered an URL but not a page title."); }
    if ($FORM{'wtitle'} ne "" && $FORM{'wurl'} eq "") { missing("You have entered a page title but not an URL."); }
    if ($FORM{'wurl'} ne "") {
        if ($FORM{'wurl'} !~ /http:\/\// ) { missing("You have missed the http:// code from your URL."); }
        if ($FORM{'wurl'} !~ /\./) { missing("Your URL doesn't appear to be correct."); }
    }
    if ($FORM{'comment'} eq "") { missing("You have not entered any comments."); }
    print "Content-type: text/html\n\n";
    print "<HTML><HEAD><TITLE>";
    &doName;
    print " - Your entry</TITLE></HEAD>\n";
    &doBody;
    print "<CENTER><H2>";
    &doName;
    print "</H2>\n";

    open(INFILE, "<$CONF{'file'}");
    @data = <INFILE>;
    close(INFILE);

    if( @data[1] eq "$FORM{'email'}\n" && @data[2] eq "$FORM{'name'}\n") {
        print "<H3>Data not added! Double Entry</H3><HR><P>\n"; 
		if ($CONF{'log'} ne "") { &doLog( "Not added! Double Entry")  };
    } else {
		if ($CONF{'log'} ne "") { &doLog };
        open(OUTFILE, ">$CONF{'file'}.tmp");
        print OUTFILE time() . "\n";
        print OUTFILE "$FORM{'email'}\n";
        print OUTFILE "$FORM{'name'}\n";
        print OUTFILE "$FORM{'wurl'}\n";
        print OUTFILE "$FORM{'wtitle'}\n";
        print OUTFILE "$FORM{'comment'}\n";
        foreach $line (@data) {
        	$line = mychomp($line);
			print OUTFILE "$line\n"; 
		}
        close(OUTFILE);

		rename("$CONF{'file'}","$CONF{'file'}.bak");
		rename("$CONF{'file'}.tmp","$CONF{'file'}");
        chmod(0644,"$CONF{'file'}","$CONF{'file'}.bak");  # added 030497

		print "<H3>This is the information you submitted to ";
		&doName2;
		print "</H3><HR><P>\n";
    }
    print "<TABLE CELLPADDING=\"3\" BORDER=1>\n";
    print "<TR><TD><STRONG>Name:</STRONG></TD><TD>$FORM{'name'}</TD></TR>\n";
    print "<TR><TD><STRONG>Email:</STRONG></TD><TD>$FORM{'email'}</TD></TR>\n";
    if ($FORM{'wurl'} ne "") { print "<TR><TD><STRONG>URL:</STRONG></TD><TD>$FORM{'wurl'}</TD></TR>\n"; }
    if ($FORM{'wtitle'} ne "") { print "<TR><TD><STRONG>Web Title:</STRONG></TD><TD>$FORM{'wtitle'}</TD></TR>\n"; }
    print "<TR><TD><STRONG>Comments:</STRONG></TD><TD>$FORM{'comment'}</TD></TR>\n";
    print "</TABLE></CENTER>\n<HR>\n<CENTER>\n<A HREF=\"$script?url=$FORM{'url'}&mode=show\">View</A> ";
	&doName2;
	print "</A><HR>\n";
    &footer;
    print "</BODY></HTML>\n";
}

sub showData {
	if ($CONF{'log'} ne "") { &doLog };
    open(INFILE, "<$CONF{'file'}");
    @data = <INFILE>;
    close(INFILE);

    $numRecs = @data;
    $numRecs = int($numRecs / 6)+1;
    $numPerPage = 50;

    if( $FORM{'start'} > $numRecs ) { $FORM{'start'} = 0; }
    if( $FORM{'start'} < 0 ) { $FORM{'start'} = 0; }
    $end = $FORM{'start'}+$numPerPage;
    if ($end > $numRecs) { $end = $numRecs-1; }

    print "Content-type: text/html\n\n";
    print "<HTML><HEAD><TITLE>";
    &doName;
    printf " - Show Entries %d - %d</TITLE></HEAD>\n",$FORM{'start'}+1,$end;
    &doBody;
    print "<CENTER><H2>";
    &doName;
    print "</H2><HR>\n";
    print "Entries are listed with the latest first, you may need to hit reload to ensure the data is current!<BR>\n";

    print "<A HREF=\"$script?url=$FORM{'url'}&mode=form\">Add</A> an entry.&nbsp;&nbsp;&nbsp;";
    printf "Listing of entries: %d - %d<BR>\n", $FORM{'start'}+1, $end;
    print "</CENTER><HR>\n";

    $numDone = 0;
    for($i=0;$i<$numPerPage;$i++) {
        $recNum = ($FORM{'start'}+$i)*6;

        if (@data[$recNum] ne "") {
            $numDone++;

            $ti = mychomp(@data[$recNum+0]);
            $em = mychomp(@data[$recNum+1]);
            $na = mychomp(@data[$recNum+2]);
            $wu = mychomp(@data[$recNum+3]);
            $wt = mychomp(@data[$recNum+4]);
            $co = mychomp(@data[$recNum+5]);

            printf "<STRONG>%d. ",$FORM{'start'}+$i+1;
            print gmtime( $ti ) . "</STRONG><BR>\n";
            print "$co<BR>\n";
            print "<A HREF=\"mailto:$em\">$na</A>";
            if ($wu ne "") { print ", "; }
            print "<A HREF=\"$wu\">$wt</A><HR>\n";
        }
    }

    if ($numDone==0) { print "<CENTER><H3>No Entries</H3></CENTER><HR>\n"; }

    print "<CENTER>\n";
    
    if ($numDone > 0) {
        $numDone = 0;
        if ($FORM{'start'} > 0) {
            print "<A HREF=\"$script?url=$FORM{'url'}&mode=show&start=";
            print $FORM{'start'}-$numPerPage;
            print "\">[Prev]</A>\n";
            $numDone = 1;
        } else {
            print "[Prev]\n";
        }

        if ($numRecs > $numPerPage) {
            $i = 0;
            $j = 1;
            while ($i < $numRecs ) {
                $numDone = 1;
                if ($i == $FORM{'start'}) {
                    print "$j\n";
                } else {
                    print "<A HREF=\"$script?url=$FORM{'url'}&mode=show&start=$i\">$j</A>\n";
                }
                $j++;
                $i = $i + $numPerPage;
            }
        }

        if ( $FORM{'start'}+$numPerPage < $numRecs ) {
            print "<A HREF=\"$script?url=$FORM{'url'}&mode=show&start=";
            print $FORM{'start'}+$numPerPage;
            print "\">[Next]</A>\n";
            $numDone = 1;
        } else {
            print "[Next]\n";
        }
    }   

    if ($numDone == 1 ) { print "</CENTER><HR>\n"; }

    &doConfig;
    &footer;
    print "</BODY></HTML>\n";
}

sub mychomp {
#
# chomp that works for unix and dos!
#
	my($data) = shift;
	
	$data =~ s/\x0A//g;
	$data =~ s/\x0D//g;
	return $data;	
}
