#!/usr/local/bin/perl5

#Online Class Discussions
#Version 0.9b

#Runs the forums, runs the posting subs
#Necessary file for Forum Scripts
#Files:  forums.pl - This file.
#        fadmin.pl - The administration file.
#        forum.db  - Posting database
#        topics.db - Topics database
#        forum.set - Forum setup file.

#Author: Ian Maki (imaki@mesastate.edu)
#Date:   August 24, 1997

require 'forum.set';
&parsedata;

if ($ENV{'PATH_INFO'}) {
   $FORM{'class'} = $ENV{'PATH_INFO'};
   $FORM{'class'} =~ s/^\///;
   $FORM{'class'} =~ s/_/ /g;
}

if ($login{$FORM{'class'}}) {
   ($user, $pass) = split(/:/, $login{$FORM{'class'}});
   if ($FORM{'u'} ne $user || $FORM{'p'} ne $pass) {
      if ($FORM{'u'} && $FORM{'p'}) {
         $loginerror = 'Invalid username and/or password';
      }
      &login;
      exit;
   }
}

if ($FORM{'cmd'} eq 'post') {
   if ($FORM{'name'} ne '' && $FORM{'email'} ne '' && $FORM{'comments'} ne '') {
      &post;
   } else {
      $error = 'All fields must be filled out';
      &postform;
   }
   exit;
} elsif ($FORM{'cmd'} eq 'postform2') {
   if ($FORM{'name'} ne '' && $FORM{'email'} ne '' && $FORM{'comments'} ne '') {
      &postform2;
   } else {
      $error = 'All fields must be filled out';
      &postform;
   }
   exit;
} elsif ($FORM{'cmd'} eq 'post a comment') {
   &postform;
   exit;
} elsif ($FORM{'cmd'} eq 'read comments') {
   &displaytopic;
   exit;
} else {
   &display;
   exit;
}

sub display {

   print "Content-type: text/html\n\n";
   print "<html>\n<head><title>$FORM{'class'} Class Discussion</title>\n";
   print "</head>\n";
   print "<base href=\"http://www.mesastate.edu/\">\n";
   print "$body\n";
   print "<img src=\"$graphic\"><p>\n";

   print "<font face=\"$font\" size=+2><strong>$FORM{'class'} Class Discussion</strong></font>\n";
         print "$comments\n<hr width=450 size=1 align=left><p>\n";

   &readtopics;

   foreach $topic (@topics) {
      ($class, $title, $desc) = split(/\|/, $topic);
      $desc =~ s/<cr>/<br>/g;
      if ($class eq $FORM{'class'}) {
         print "<font face=\"$font\">\n";
         print "<font size=+1><strong>$title</strong></font><br>\n";
         print "$desc<br>\n</font>";
         print "<form method=POST action=\"$cgiurl\">\n";
         print "<input type=hidden name=class value=\"$FORM{'class'}\">\n";
         print "<input type=hidden name=topic value=\"$title\">\n";
         print "<input type=hidden name=topicdesc value=\"$desc\">\n";
         print "<input type=hidden name=u value=\"$FORM{'u'}\">\n";
         print "<input type=hidden name=p value=\"$FORM{'p'}\">\n";
         print "<input type=submit name=cmd value=\"post a comment\"> ";
         print "<input type=submit name=cmd value=\"read comments\">\n";
         print "</form>\n";
         print "$comments\n<hr width=450 size=1 align=left>\n";
      }
   }

   print "<font face=\"$font\"><strong>\n";
   print "Return to <a href=\"$homeurl{$FORM{'class'}}\">$hometitle{$FORM{'class'}}</a>.</font></strong><p>\n";

   print "</body\n</html>\n";
}

sub displaytopic {

   print "Content-type: text/html\n\n";
   print "<html>\n<head><title>$FORM{'class'} Class Discussion Topic: $FORM{'topic'}</title>\n";
   print "</head>\n";
   print "<base href=\"http://www.mesastate.edu/\">\n";
   print "$body\n";
   print "<img src=\"$graphic\"><p>\n";

   print "<font face=\"$font\" size=+2><strong>$FORM{'class'} Class Discussion</strong></font><p>\n";
   print "<font face=\"$font\" size=+1><strong>Topic: $FORM{'topic'} Class Discussion</strong></font><br>\n";
   print "<font face=\"$font\">$FORM{'topicdesc'}</font><p><hr width=450 size=1 align=left>\n";

   &readforums;
   foreach $forum (@forums) {
      ($class, $topic, $name, $email, $date, $comments) = split(/\|/, $forum);
      $comments =~ s/<cr>/<br>/g;
      if ($topic eq $FORM{'topic'} && $class eq $FORM{'class'}) {
         print "<font face=\"$font\">\n";
         print "<strong>$date, <a href=\"mailto:$email\">$name</a> wrote:</strong><p>\n";
         print "$comments\n<hr width=450 size=1 align=left>\n";
      }
   }

   print "<p><font face=\"$font\"><strong>\n";
   print "Return to <a href=\"$homeurl{$FORM{'class'}}\">$hometitle{$FORM{'class'}}</a>.</font></strong><p>\n";

   print "</body\n</html>\n";
}

sub login {

   print "Content-type: text/html\n\n";
   print "<html>\n<head><title>$FORM{'class'} Class Discussion</title>\n";
   print "</head>\n";
   print "<base href=\"http://www.mesastate.edu/\">\n";
   print "$body\n";
   print "<img src=\"$graphic\"><p>\n";

   print "<font face=\"$font\" size=+2><strong>$FORM{'class'} Class Discussion</strong></font><br>\n";
   print "<font face=\"$font\" size=+1><strong>Login</strong></font><p>\n";
   print "<font face=\"$font\"><strong>$loginerror</strong></font><p>\n";

   print "<form method=POST action=\"$cgiurl\">\n";
   print "<input type=hidden name=class value=\"$FORM{'class'}\">\n";

   print "<strong><font face=\"$font\">Login: <br></font>\n";
   print "<input type=text name=u size=10><br>\n";
   print "<font face=\"$font\">Password: <br></font>\n";
   print "<input type=password name=p size=10><p>\n";
   print "</font><input type=submit value=\"Login\">\n";
   print "</form>\n";

   print "<font face=\"$font\"><strong>\n";
   print "Return to <a href=\"$homeurl{$FORM{'class'}}\">$hometitle{$FORM{'class'}}</a>.</font></strong><p>\n";

   print "</body\n</html>\n";
}

sub postform {

   print "Content-type: text/html\n\n";
   print "<html>\n<head><title>$FORM{'class'} Class Discussion - Topic: $FORM{'topic'}</title>\n";
   print "</head>\n";
   print "<base href=\"http://www.mesastate.edu/\">\n";
   print "$body\n";
   print "<img src=\"$graphic\"><p>\n";

   print "<strong><font size=+2 face=\"$font\">Post a Comment!</font></strong><p>\n";
   print "<font face=\"$font\" size=+1><strong>Topic: $FORM{'topic'} Class Discussion</strong></font><br>\n";
   print "<font face=\"$font\">$FORM{'topicdesc'}</font><p>\n";
   print "<font face=\"$font\" size=+1><strong>$error</strong></font><p>\n";

   print "<form method=POST action=\"$cgiurl\">\n";
   print "<input type=hidden name=class value=\"$FORM{'class'}\">\n";
   print "<input type=hidden name=topic value=\"$FORM{'topic'}\">\n";
   print "<input type=hidden name=topicdesc value=\"$FORM{'topicdesc'}\">\n";
   print "<input type=hidden name=u value=\"$FORM{'u'}\">\n";
   print "<input type=hidden name=p value=\"$FORM{'p'}\">\n";
   print "<input type=hidden name=cmd value=\"postform2\">\n\n";

   print "<strong><font face=\"$font\">Your Name: <br></font>\n";
   print "<input type=text name=name size=30 value=\"$FORM{'name'}\"><br>\n";
   print "<font face=\"$font\">E-Mail Address: <br></font>\n";
   print "<input type=text name=email size=30 value=\"$FORM{'email'}\"><br>\n";
   print "<font face=\"$font\">Comments:</font></em><br>\n";
   print "<textarea name=comments COLS=60 ROWS=10 wrap=virtual>$FORM{'comments'}</textarea><p>\n";
   print "</font><input type=submit value=\"Preview your Comment\">\n";
   print "</form>\n";

   print "<p><font face=\"$font\"><strong>\n";
   print "Return to <a href=\"$homeurl{$FORM{'class'}}\">$hometitle{$FORM{'class'}}</a>.</font></strong><p>\n";

   print "</body>\n</html>";
}

sub postform2 {

   print "Content-type: text/html\n\n";
   print "<html>\n<head><title>$FORM{'class'} Class Discussion - Topic: $FORM{'topic'}</title>\n";
   print "</head>\n";
   print "<base href=\"http://www.mesastate.edu/\">\n";
   print "$body\n";
   print "<img src=\"$graphic\"><p>\n";

   print "<strong><font size=+2 face=\"$font\">Comment Preview</font></strong><br>\n";
   print "<font face=\"$font\">Check the text within the box to make sure your comment looks the way you want \n";
   print "it to.<br>If you need to make changes, make them in the form, and click the post button.</font><p>\n";
   print "<font face=\"$font\" size=+1><strong>Topic: $FORM{'topic'} Class Discussion</strong></font><br>\n";
   print "<font face=\"$font\">$FORM{'topicdesc'}</font><p>\n";

   print "<table border=1><tr><td>\n";
   print "<font face=\"$font\">\n";
   print "<strong>$date, <a href=\"mailto:$FORM{'email'}\">$FORM{'name'}</a> wrote:</strong><p>\n";
   $comments = $FORM{'comments'};
   $comments =~ s/\r\n/<br>/g;
   print "$comments\n";
   print "</td></tr></table><p>\n";

   print "<form method=POST action=\"$cgiurl\">\n";
   print "<input type=hidden name=class value=\"$FORM{'class'}\">\n";
   print "<input type=hidden name=topic value=\"$FORM{'topic'}\">\n";
   print "<input type=hidden name=u value=\"$FORM{'u'}\">\n";
   print "<input type=hidden name=p value=\"$FORM{'p'}\">\n";
   print "<input type=hidden name=cmd value=\"post\">\n\n";

   print "<strong><font face=\"$font\">Your Name: <br></font>\n";
   print "<input type=text name=name size=30 value=\"$FORM{'name'}\"><br>\n";
   print "<font face=\"$font\">E-Mail Address: <br></font>\n";
   print "<input type=text name=email size=30 value=\"$FORM{'email'}\"><br>\n";
   print "<font face=\"$font\">Comments:</font></em><br>\n";
   print "<textarea name=comments COLS=60 ROWS=10 wrap=virtual>$FORM{'comments'}</textarea><p>\n";

   print "</font><input type=submit value=\"Post your Comment\">\n";
   print "</form><p>\n";

   print "<p><font face=\"$font\"><strong>\n";
   print "Return to <a href=\"$homeurl{$FORM{'class'}}\">$hometitle{$FORM{'class'}}</a>.</font></strong><p>\n";

   print "</body>\n</html>";
}

sub post {

   if (($FORM{'name'}) && ($FORM{'email'}) && ($FORM{'comments'})) {
      $FORM{'comments'} =~ s/\r\n/<cr>/g;

      open (DATABASE,">>$forumdb");
      print DATABASE "$FORM{'class'}\|$FORM{'topic'}\|$FORM{'name'}\|$FORM{'email'}\|$date\|$FORM{'comments'}\n";
      close (DATABASE);
   }
   &display;
}