#!/usr/local/bin/perl5

#Online Class Discussions
#Version 0.9b

#Administration File
#Necessary file for Forum Scripts
#Files:  forums.pl - Runs the forums, runs the posting subs.
#        fadmin.pl - This 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;

($user, $pass) = split(/:/, $adminlogin);

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

if ($FORM{'admin'} eq 'topicadd') {
   &topicaddform;
} elsif ($FORM{'admin'} eq 'addtopic') {
   &addtopic;
} elsif ($FORM{'admin'} eq 'topicedit') {
   &topiceditform;
} elsif ($FORM{'admin'} eq 'topiceditform2') {
   &topiceditform2;
} elsif ($FORM{'admin'} eq 'edittopic') {
   &edittopic;
} elsif ($FORM{'admin'} eq 'topicdelete') {
   &topicdeleteform;
} elsif ($FORM{'admin'} eq 'deletetopic') {
   &deletetopic;
} elsif ($FORM{'admin'} eq 'forumdelete') {
   &forumdeleteform;
} elsif ($FORM{'admin'} eq 'deleteforum') {
   &deleteforum;
} else {
   &menu;
}

sub menu {

   &header;

   print "<h2><em>Class Discussions - Administration Options</em></h2><p>\n";
   print "<table>\n";
   print "<tr><td valign=top><h3>Add A New Topic </td><td valign=middle>\n";
   print "   <form method=\"POST\" name=order action=\"$adminurl\">\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=admin value=topicadd><INPUT TYPE=\"submit\" VALUE=\"Click Here\"></form></td></tr>\n";
   print "<tr><td valign=top><h3>Edit A Topic </td><td valign=middle>\n";
   print "   <form method=\"POST\" name=order action=\"$adminurl\">\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=admin value=topicedit><INPUT TYPE=\"submit\" VALUE=\"Click Here\"></form></td></tr>\n";
   print "<tr><td valign=top><h3>Remove A Topic </td><td valign=middle>\n";
   print "   <form method=\"POST\" name=order action=\"$adminurl\">\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=admin value=topicdelete><INPUT TYPE=\"submit\" VALUE=\"Click Here\"></form></td></tr>\n";
   print "<tr><td valign=top><h3>Remove A Posting </td><td valign=middle>\n";
   print "   <form method=\"POST\" name=order action=\"$adminurl\">\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=admin value=forumdelete><INPUT TYPE=\"submit\" VALUE=\"Click Here\"></form></td></tr>\n";

   print "</table><p>\n";

   print "How to link to your online discussions:<p>\n";
   foreach $class (@classes) {
      print "$class<br>\n";
      print "<em>Homepage:</em> <a href=\"$homeurl{$class}\">$hometitle{$class}</a></font><br>\n";
      $class =~ s/ /_/g;
      print "<em>Discussion:</em> <font size=-1><a href=\"$cgiurl/$class\">$cgiurl/$class</a></font><br>\n";
      $class =~ s/_/ /g;
      if ($login{$class}) {
         ($login, $password) = split(/:/, $login{$class});
         print "<em>Login:</em> <strong>$login</strong><br><em>Password:</em> <strong>$password</strong><p>\n";
      } else {
         print "<em>Login:</em> no login required<br>\n<em>Password:</em> no password required<p>\n";
      }
   }

   &footer;
   exit;
}

sub login {

   &header;
   print "<h2><em>Class Discussions - Administration Options - Login</em></h2><p>\n";

   print "<strong>$loginerror</strong><p>\n";

   print "<form method=POST action=\"$adminurl\">\n";

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

   print "<font face=\"$font\"><strong>\n";
   &footer;
}

sub topicaddform {

   &header; 

   print "<h2><em>Topic Addition Form</em></h2><p>\n";
   print "<form method=\"POST\" name=order action=\"$adminurl\">\n";
   print "<input type=hidden name=admin value=addtopic>\n";
   print "<input type=hidden name=u value=\"$FORM{'u'}\">\n";
   print "<input type=hidden name=p value=\"$FORM{'p'}\">\n";

   print "<table cellspacing=0>\n";
   print "<tr><td align=left><strong>Class:</td>\n";
   print "<td><select name=class>\n";
   foreach $class (@classes) {
      print "<option>$class\n";
   }
   print "</select></td></tr>";
   print "<tr><td align=left><strong>Topic Title:</td><td><input type=text name=title size=30></td></tr>";

   print "<tr><td align=left><strong>Topic Text:</strong></td><td></td></tr>\n";
   print "<tr><td align=left colspan=2><textarea name=text COLS=50 ROWS=8 wrap=virtual></textarea></td></tr>\n";
   print "<tr><td align=left colspan=2><INPUT TYPE=\"submit\" VALUE=\"Add the Topic\"></td></tr>\n";
   print "</table><p>\n";
   print "</form><p>\n";

   &footer;
   exit;
}

sub addtopic {

   open (DATABASE, "$topicdb");
   while (<DATABASE>) {
     chop ($_);
     push (@topics, $_);
   }
   close (DATABASE);

   if (($FORM{'title'}) && ($FORM{'text'}) && ($FORM{'class'})) {
      $newtopic = join('|', $FORM{'class'}, $FORM{'title'}, $FORM{'text'});
      $newtopic =~ s/\r\n/<cr>/g;
      push (@topics, $newtopic);
   }

   &writetopicfile;
   &menu;
}

sub topiceditform {

   open (DATABASE, "$topicdb");
   while (<DATABASE>) {
     chop ($_);
     push (@topics, $_);
   }
   close (DATABASE);

   &header; 

   print "<h2><em>Topic Edit Form</em></h2><p>\n";
   print "<strong>Highlight the topic that you would like to edit.<p>\n";

   print "<form method=\"POST\" name=order action=\"$adminurl\">\n";
   print "<input type=hidden name=admin value=topiceditform2>\n";
   print "<input type=hidden name=u value=\"$FORM{'u'}\">\n";
   print "<input type=hidden name=p value=\"$FORM{'p'}\">\n";
   print "Class - Topic<br>\n";
   print "<select name=topicedit size=5>\n";

   $num = @topics;

   for ($i=0;$i<$num;$i++) {
      ($class, $title, $text) = split (/\|/, @topics[$i]);
      print "<option value=$i>$class - $title\n";
   }
   print "</select><p>\n";

   print "<INPUT TYPE=\"submit\" VALUE=\"Open the Topic\">\n";
   print "</form><p>\n";

   &footer;
   exit;
}

sub topiceditform2 {

   open (DATABASE, "$topicdb");
   while (<DATABASE>) {
     chop ($_);
     push (@topics, $_);
   }
   close (DATABASE);

   ($class, $title, $text) = split (/\|/, @topics[$FORM{'topicedit'}]);
   $text =~ s/<cr>/\n/g;
   $address =~ s/<cr>/\n/g;

   &header; 

   print "<h2><em>Topic Edit Form - Part 2</em></h2><p>\n";
   print "<strong>Change the following fields to make changes to the Topic.<p>\n";

   print "<form method=\"POST\" name=order action=\"$adminurl\">\n";
   print "<input type=hidden name=admin value=edittopic>\n";
   print "<input type=hidden name=topicedit value=$FORM{'topicedit'}>\n";
   print "<input type=hidden name=u value=\"$FORM{'u'}\">\n";
   print "<input type=hidden name=p value=\"$FORM{'p'}\">\n";

   print "<table cellspacing=0>\n";
   print "<tr><td align=left><strong>Class:</td>\n";
   print "<td><select name=class>\n";
   foreach $classin (@classes) {
      if ($classin eq $class) {
         print "<option selected>$classin\n";
      } else {
         print "<option>$classin\n";
      }
   }
   print "</select></td></tr>";

   print "<tr><td align=left><strong>Topic Title:</td>\n";
   print "    <td><input type=text name=title size=30 value=\"$title\"></td></tr>";

   print "<tr><td align=left><strong>Topic Text:</strong></td><td></td></tr>\n";
   print "<tr><td align=left colspan=2><textarea name=text COLS=50 ROWS=8 wrap=virtual>$text</textarea></td></tr>\n";

   print "<tr><td align=left colspan=2><INPUT TYPE=\"submit\" VALUE=\"Submit the Changes\"></td></tr>\n";
   print "</table><p>\n";
   print "</form><p>\n";

   &footer;
   exit;
}

sub edittopic {

   open (DATABASE, "$topicdb");
   while (<DATABASE>) {
     chop ($_);
     push (@topics, $_);
   }
   close (DATABASE);

   if (($FORM{'title'}) && ($FORM{'text'}) && ($FORM{'class'})) {
      $newtopic = join('|', $FORM{'class'}, $FORM{'title'}, $FORM{'text'});
      $newtopic =~ s/\r\n/<cr>/g;
      @topics[$FORM{'topicedit'}] = $newtopic;
   }

   &writetopicfile;
   &menu;
}

sub topicdeleteform {

   open (DATABASE, "$topicdb");
   while (<DATABASE>) {
     chop ($_);
     push (@topics, $_);
   }
   close (DATABASE);

   &header; 

   print "<h2><em>Topic Removal Form</em></h2><p>\n";
   print "<strong>Highlight the topic below that you would like to remove from the database.</strong><p>\n";

   print "<form method=\"POST\" name=order action=\"$adminurl\">\n";
   print "<input type=hidden name=admin value=deletetopic>\n";
   print "<input type=hidden name=u value=\"$FORM{'u'}\">\n";
   print "<input type=hidden name=p value=\"$FORM{'p'}\">\n";
   print "Class - Topic<br>\n";
   print "<select name=topicdelete size=5>\n";

   $num = @topics;

   for ($i=0;$i<$num;$i++) {
      ($class, $title, $text) = split (/\|/, @topics[$i]);
      print "<option value=$i>$class - $title\n";
   }
   print "</select><p>\n";

   print "<INPUT TYPE=\"submit\" VALUE=\"Remove the Topic\">\n";
   print "</form><p>\n";

   &footer;
   exit;
}

sub deletetopic {

   $i=0;
   open (DATABASE, "$topicdb");
   while (<DATABASE>) {
     chop($_);
     if ($i != $FORM{'topicdelete'}) {
        push (@topics, $_);
     }
     $i++
   }
   close (DATABASE);

   &writetopicfile;
   &menu;
}

sub writetopicfile {
   open (DATABASE, ">$topicdb");
   foreach $line (@topics) {
      print DATABASE "$line\n";
   }
   close (DATABASE);
}

sub forumdeleteform {

   open (DATABASE, "$forumdb");
   while (<DATABASE>) {
     chop ($_);
     push (@forums, $_);
   }
   close (DATABASE);

   &header; 

   print "<h2><em>Posting Removal Form</em></h2><p>\n";
   print "<strong>Highlight the posting below that you would like to remove from the database.</strong><p>\n";

   print "<form method=\"POST\" name=order action=\"$adminurl\">\n";
   print "<input type=hidden name=admin value=deleteforum>\n";
   print "<input type=hidden name=u value=\"$FORM{'u'}\">\n";
   print "<input type=hidden name=p value=\"$FORM{'p'}\">\n";
   print "Class - Topic - Name - Date<br>\n";
   print "<select name=forumdelete size=5>\n";

   $num = @forums;

   for ($i=0;$i<$num;$i++) {
      ($class, $topic, $name, $email, $date, $comments) = split (/\|/, @forums[$i]);
      print "<option value=$i>$class - $topic - $name - $date\n";
   }
   print "</select><p>\n";

   print "<INPUT TYPE=\"submit\" VALUE=\"Remove the Posting\">\n";
   print "</form><p>\n";

   &footer;
   exit;
}

sub deleteforum {

   $i=0;
   open (DATABASE, "$forumdb");
   while (<DATABASE>) {
     chop($_);
     if ($i != $FORM{'forumdelete'}) {
        push (@forums, $_);
     }
     $i++
   }
   close (DATABASE);

   &writeforumfile;
   &menu;
}

sub writeforumfile {
   open (DATABASE, ">$forumdb");
   foreach $line (@forums) {
      print DATABASE "$line\n";
   }
   close (DATABASE);
}

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

sub footer {
    print "</body></html>\n";
}
