#!/usr/bin/perl
use strict;
use CGI;
use DBI;
my($path, $result, $area, $mode, $section, $question, $FILEIO, $count, $number, $temp, $filemode, $counter, $LIST, $DUMP, $qnum, $anum, $outputtext, $linelen, $ANSLIST, $ans, $ansemail, %posters, %postersname, $key, $value, @winners, $anscount, $maxlinelen);
my($database, $select, @data, $select2, @data2, $first);
$result = new CGI();
# The path is where to find the information to build the pages
#$path = "/var/www/html/stillhq/";
$path = "/home/httpd/html/";
$maxlinelen = 70;
# Determine the name of the faq that we are editting, and our mode
$area = $result->param('area');
$mode = $result->param('mode');
$section = $result->param('section');
$question = $result->param('question');
$database = DBI->connect("DBI:mysql:database=$area:host=localhost", "root", "vodka4me", { PrintError => 0 });
if(!defined($database)){
print "Database connection failed" . DBI->errstr;
exit;
}
if($mode eq ""){
$mode = "menu";
}
# Output the standard HTML prelude, which includes stuff like the page title
print $result->header;
###############################################################################
# Based on our mode, we display different things
if($mode eq "menu"){
# Display the initial menu
print "Welcome to the $area FAQ. Please select a command:
\n";
print "Add or edit data
\n";
print "Dump a copy of the current FAQ
\n";
}
###############################################################################
elsif($mode eq "add"){
# The user selects a section
print "Select a section from the current FAQ, or select Add new
\n";
# Build the list of sections that are available
$select = $database->prepare("SELECT sno, sname FROM sections ORDER BY sno");
if(!defined($select)){
print "Could not arrange select";
$database->disconnect();
exit;
}
# Results?
$select->execute() or die "Could not execute select" . $select->errstr;
if($select->rows > 0){
while(@data = $select->fetchrow_array()){
print "$data[1]
";
}
}
else{
print "Error: the database select returned no rows! (";
print $select->rows;
print ")";
}
# We also present some simple statistics about the database at the moment
print "
";
print "Questions: ";
$select = $database->prepare("SELECT count(qno) FROM questions");
if(!defined($select)){
print "Could not arrange select";
$database->disconnect();
exit;
}
# Results?
$select->execute() or die "Could not execute select" . $select->errstr;
if($select->rows > 0){
while(@data = $select->fetchrow_array()){
print "$data[0]
";
}
}
print "Answers: ";
$select = $database->prepare("SELECT count(ano) FROM answers");
if(!defined($select)){
print "Could not arrange select";
$database->disconnect();
exit;
}
# Results?
$select->execute() or die "Could not execute select" . $select->errstr;
if($select->rows > 0){
while(@data = $select->fetchrow_array()){
print "$data[0]
";
}
}
}
###############################################################################
elsif($mode eq "add-sect"){
# The user has selected a section and now selects a question
print "[Back to section menu]
";
print "Select a question from the current FAQ, or select Add new
\n";
# Build the list of currently available questions
$select = $database->prepare("SELECT qno, qname FROM questions WHERE sno = $section ORDER BY qno");
if(!defined($select)){
print "Could not arrange select";
$database->disconnect();
exit;
}
# Results?
$select->execute() or die "Could not execute select" . $select->errstr;
if($select->rows > 0){
while(@data = $select->fetchrow_array()){
print "$data[1]
";
}
}
else{
print "Error: the database select returned no rows! (";
print $select->rows;
print ")";
}
print "
Add new
";
}
###############################################################################
elsif($mode eq "add-quest"){
# We have selected a section and a question (the question might be new)
print "[Back to section menu]
";
print $result->start_form(-action=>"/cgi-bin/auth/faq-addquestion");
print $result->input(
{-type=>'hidden', -name=>'area', -value=>$area});
print $result->input(
{-type=>'hidden', -name=>'section', -value=>$section});
print $result->input(
{-type=>'hidden', -name=>'question', -value=>$question});
if($question eq "new"){
# New question
print "We need some details for the new question...
\n";
print "Name:
\n";
print $result->textarea(
{-rows=>10, -cols=>100,
-wrap=>'physical', -name=>"name"});
print "
Text of question:
\n";
print $result->textarea(
{-rows=>10, -cols=>100,
-wrap=>'physical', -name=>"qtext"});
}
else{
# We output the information we already know about the question
$select = $database->prepare("SELECT qname, question FROM questions WHERE qno = $question");
if(!defined($select)){
print "Could not arrange select";
$database->disconnect();
exit;
}
# Results?
$select->execute() or die "Could not execute select" . $select->errstr;
if($select->rows > 0){
while(@data = $select->fetchrow_array()){
print "Title: $data[0]
Question: $data[1]\n";
}
}
else{
print "Error: the database select returned no rows! (";
print $select->rows;
print ")";
}
}
print "
Text of answer:
\n";
print $result->textarea(
{-rows=>10, -cols=>100,
-wrap=>'physical', -name=>"answer"});
print "
Input date:\n";
print $result->input(
{-type=>'text', -size=>20, -name=>"postdate",
-value=>`date "+%d %B %Y"`});
print "
Answerer:\n";
print $result->input(
{-type=>'text', -size=>40, -name=>"poster"});
print "
Answerer email:\n";
print $result->input(
{-type=>'text', -size=>40, -name=>"postemail"});
print "
\n";
print $result->submit(-value=>" Are you sure? ", -name=>"commit");
print "
\n";
print "Existing answers:";
dumpanswers($path, $area, $section, $question, 1);
print "";
}
###############################################################################
elsif($mode eq "dump"){
# We have been asked to dump a copy of the current FAQ
# We also need to dump the standard prelude
print `cat $path/$area/db/prelude` or die "Cannot open prelude";
# For each section
$select = $database->prepare("SELECT sno, sname FROM sections ORDER BY sno");
if(!defined($select)){
print "Could not arrange select";
$database->disconnect();
exit;
}
# Results?
$select->execute() or die "Could not execute select" . $select->errstr;
if($select->rows > 0){
while(@data = $select->fetchrow_array()){
print "$data[1]\n";
print "\n";
$select2 = $database->prepare("SELECT qno, qname, question FROM questions WHERE sno = $data[0] ORDER BY qno");
if(!defined($select2)){
print "Could not arrange select";
$database->disconnect();
exit;
}
# Print out the questions
$select2->execute() or die "Could not execute select" . $select->errstr;
if($select2->rows > 0){
$count = 1;
$first = 0;
while(@data2 = $select2->fetchrow_array()){
if($first == 0)
{
$first++;
}
else{
print "\n";
}
print "$data2[1]\n";
printtext("$data2[2] [Q$data2[0]]", 0);
print "\n";
dumpanswers($path, $area, $data[0], $data2[0], 0);
}
}
else{
print "Error: the database select returned no rows! (";
print $select2->rows;
print ")";
}
print "\n";
}
print "\n";
}
else{
print "Error: the database select returned no rows! (";
print $select->rows;
print ")";
}
}
else{
print "Unknown CGI mode. How did you get here?\n";
}
# Finish off the page
if($mode ne "dump"){
print $result->end_html;
}
exit;
###############################################################################
sub dumpanswers{
my($path, $area, $section, $qnum, $mode) = @_;
my($anscount, $ANSLIST, $counter, $filemode, $line, $word, $myselect, @mydata);
# We also need to dump each of the answers that we have in the db
# Build the list of sections that are available
$myselect = $database->prepare("SELECT answer, poster, email, ano FROM answers WHERE qno = $qnum ORDER BY ano");
if(!defined($myselect)){
print "Could not arrange select";
$database->disconnect();
exit;
}
# Results?
$myselect->execute() or die "Could not execute select" . $myselect->errstr;
if($myselect->rows > 0){
while(@mydata = $myselect->fetchrow_array()){
print "\n"; # todo_mikal: This is very bad docbook...
printtext($mydata[0], 2);
print "$mydata[1] ($mydata[2]) [A$mydata[3]]\n";
print "\n";
}
}
}
###############################################################################
sub printtext(){
my($line, $indent) = @_;
my($word, $tempword);
# With the new database version, this is never matched (I think)
if(length($line) == 0){
print "\n\n";
}
$_ = $line;
s/&/&/g;
s/</g;
s/>/>/g;
s/<BR>/<\/para>/g;
print "$_";
}