Simple FAQ Creator

This is a pair of programs to create and maintain a simple FAQ web page. The administrative inteface lets you add questions and answers, modify existing entries, and control the sort order. The simplest way to explain it is to show you the screens:

Simple Faq User Screen

This is the main screen for administration. Review simply dumps out the FAQ as the users will see it. Headers and footers let you enter content that will appear before and after the FAQ entries.

When adding new content or editing, you also enter keywords that do not appear in the FAQ as displayed, but will be used for searching:

Simple Faq Questions Screen

Note that HTML markup is allowed in the content.

Sorting is done simply by selecting the FAQ entry:

Simple Faq EditScreen

After selecting the FAQ, this screen allows sorting or deletion. We require both checkboxes to be checked for a delete.

Simple Faq deletion Screen

That's about it. It's pretty simple, but effective.

Download faqadmin.pl

Here's the code for the admin functions:

#!/usr/bin/perl
use CGI qw(:standard);
foreach $i (param) {
  foreach $j (param($i) ) {
    $$i=$j;
  }
}
dbmopen %list, "YOURDATAPATH/data/faqlist", 0700 or die "can't open faqlist";
dbmopen %data, "/YOURDATAPATH/faqdat", 0700 or die "can't open faqlist";
dbmopen %fkey, "/YOURDATAPATH/data/faqkeys", 0700 or die "can't open faqlist";
if (defined $posting) {
  posthead() if $posthead;
  postedit() if $postedit;
  postfaq() if $postfaq;
  mkhead() if $choice eq "Headers";
  editing() if $choice eq "Edit";
  newedit() if $choice eq "New";
  sortmesel() if $choice eq "Sort";
  sortmehow() if $sortit;
  sortme() if $sortnow;
  review() if $choice eq "Review";
  dbmclose %list;
  dbmclose %data;
  dbmclose %fkey;
  exit 0;
}

closeit();

sub newedit {
  $which="(New)";
  postedit();
}
sub closeit {
 main();
 dbmclose %list;
  dbmclose %data;
  dbmclose %fkey;
 exit 0;
}
sub main {
header();
print <<EOF;
<H1><CENTER>FAQ Administration</CENTER></H1>
<p>Please choose:<p>
<form method="POST" action="/cgi-bin/kfaq/faqadmin.pl">
<p><input type=radio name="choice" value="Headers" > Headers and Footers 
<p><input type=radio name="choice" value="New" > New
<p><input type=radio name="choice" value="Edit" > Edit
<p><input type=radio name="choice" value="Sort" > Sort/Delete
<p><input type=radio name="choice" value="Review" > Review
<p><input type=submit name="posting" value="Go" > 
</form>
</body>
</html>
EOF
}
sub sortme {
$items=scalar (keys (%list));
$items -= 2;
 if ($killme and $killmeyes) {
   delete $list{$which};
   delete $data{$which};
   delete $fkey{$which};
   review();
 }
 $hold1=$list{$which}; 
 $hold2=$data{$which}; 
 $hold3=$fkey{$which}; 
 $pos=$which;
 $pos=~ s/line:00*//;
 $pos += $howmuch if ($move eq "down");
 $pos -= $howmuch if ($move eq "up");
 $pos=1 if $pos <1;
 $pos=$items if $pos > $items;
 $x=1;
 $before=$list{'header'};
 $after=$list{'footer'};
 foreach (sort keys %list) {
   next if $_ eq $which;
   next if not /^line:/;
   $newkey=sprintf "%.5d",$x;
   if ($x == $pos) {
     $x++;
     $newkey=sprintf "%.5d",$x;
   }
   $nlist{$newkey}=$list{$_};
   $ndata{$newkey}=$data{$_};
   $nfkey{$newkey}=$fkey{$_};
   $x++;
 }
 $newkey=sprintf "%.5d",$pos;
 $nlist{$newkey}=$hold1;
 $ndata{$newkey}=$hold2;
 $nfkey{$newkey}=$hold3;
 
 %list=();
 %data=();
 %fkey=();
$list{'header'}=$before;
$list{'footer'}=$after;
 foreach (sort keys %nlist) {
   $newkey=sprintf "line:%.5d",$_;
   $list{$newkey}=$nlist{$_};
   $data{$newkey}=$ndata{$_};
   $fkey{$newkey}=$nfkey{$_};
 }

 review();
}
sub sortmehow {
 header();
print "<H1><CENTER>FAQ Administration</CENTER></H1><p>";
$list{$which} =~ s/\</</g;
$data{$which} =~ s/\</</g;
$fkey{$which} =~ s/\</</g;
$list{$which} =~ s/\>/>/g;
$data{$which} =~ s/\>/>/g;
$fkey{$which} =~ s/\>/>/g;
print <<EOF;
<p>($list{$which})
<form method="POST" action="/cgi-bin/kfaq/faqadmin.pl">
<p>Delete (must check both) <input type=checkbox name="killme">
<input type=checkbox name="killmeyes">
<p>Move <input type=radio name="move" value="up"> UP
<input type=radio name="move" value="down"> DOWN
<input type=text size=3 name="howmuch"> Lines
<p><input type=hidden name="which" value="$which" > 
<p><input type=hidden name="sortnow" value="done" > 
<p><input type=submit name="posting" value="Do it" > 
</form>
</body>
</html>
EOF
  dbmclose %list;
  dbmclose %data;
  dbmclose %fkey;
exit 0;

}
sub sortmesel {
 header();
$items=scalar (keys (%list));
$items -= 1;
$items=5 if $items > 5;
print <<EOF;
<H1><CENTER>FAQ Administration</CENTER></H1>
<p>Select FAQ and click "Go" to sort or delete
<form method="POST" action="/cgi-bin/kfaq/faqadmin.pl">
<select name="which" size=$items>
EOF
foreach (sort keys %list) {
  $list{$_}=~ s/</\</g;
  $list{$_}=~ s/>/\>/g;
  next if not /^line:/;
  print "<option value=$_>$list{$_}";
}
print <<EOF;
</select>
<p><input type=hidden name="sortit" value="done" > 
<p><input type=submit name="posting" value="Go" > 
</form>
</body>
</html>
EOF
  dbmclose %list;
  dbmclose %data;
  dbmclose %fkey;
exit 0;

}
sub mkhead {
  header();
print <<EOF;
<H1><CENTER>FAQ Administration</CENTER></H1>
<p>HTML tags allowed.  Enter the text that will appear before and 
after your main faq list.
<form method="POST" action="/cgi-bin/kfaq/faqadmin.pl">
<p>Text before FAQ List: <p><textarea name="textheader" cols=80 rows=8>$list{'header'}</textarea>
<p>Text after FAQ List: <p><textarea name="textfooter" cols=80 rows=8>$list{'footer'}</textarea>
<p><input type=hidden name="posthead" value="done" > 
<p><input type=submit name="posting" value="Record" > 
</form>
</body>
</html>
EOF

}
sub postedit {
  header();
print "<H1><CENTER>FAQ Administration</CENTER></H1><p>";
$list{'(New)'}="";
$list{$which} =~ s/\</</g;
$data{$which} =~ s/\</</g;
$fkey{$which} =~ s/\</</g;
$list{$which} =~ s/\>/>/g;
$data{$which} =~ s/\>/>/g;
$fkey{$which} =~ s/\>/>/g;
print <<EOF;
<p>
<form method="POST" action="/cgi-bin/kfaq/faqadmin.pl">
<p>Question: <input type=text size=80 name="faqline" value="$list{$which}">
<p>Index keywords: <input type=text size=80 name="faqkeys" value="$fkey{$which}">
<p>Answer:<br> <textarea name="faqdata" cols=80 rows=8>$data{$which}</textarea>
<p>
<p><input type=hidden name="which" value="$which" > 
<p><input type=hidden name="postfaq" value="done" > 
<p><input type=submit name="posting" value="Record" > 
</form>
</body>
</html>
EOF
}
sub postfaq {
my $x=1;
  if ($which eq "(New)") {
   $which=sprintf "line:%.5d",$x;
   while ($list{$which}) {
     $x++;
     $which=sprintf("line:%.5d",$x);
   }
 }
 $list{$which}=$faqline;
 $data{$which}=$faqdata;
 $fkey{$which}=$faqkeys;
 closeit();
}
sub editing {
 header();
$items=scalar (keys (%list));
$items -= 1;
$items=5 if $items > 5;
print <<EOF;
<H1><CENTER>FAQ Administration</CENTER></H1>
<p>Select FAQ and click "Go"
<form method="POST" action="/cgi-bin/kfaq/faqadmin.pl">
<select name="which" size=$items>
<option selected value="(New)">(New)
EOF
foreach (sort keys %list) {
  $list{$_}=~ s/</\</g;
  $list{$_}=~ s/>/\>/g;
  next if not /^line:/;
  print "<option value=$_>$list{$_}";
}
print <<EOF;
</select>
<p><input type=hidden name="postedit" value="done" > 
<p><input type=submit name="posting" value="Go" > 
</form>
</body>
</html>
EOF
}
sub review {
  header();
  print "<h2>FAQS</h2>";
  print "<a href=\"/cgi-bin/kfaq/faqadmin.pl\">Main</a>";
  print "<p>$list{'header'}"; 
  foreach (sort keys %list) {
    $list{$_}=~ s/\</</g;
    $list{$_}=~ s/\>/>/g;
    $data{$_}=~ s/\</</g;
    $data{$_}=~ s/\>/>/g;
    next if not /^line:/;
    print "<h2>$list{$_}</h2>";
    print "<p> $data{$_}";
   }
  print "<p>$list{'footer'}"; 
  print <<EOF;
</body>
</html>
EOF
dbmclose %list;
  dbmclose %data;
  dbmclose %fkey;
exit 0;
}

sub header {
print <<EOF;;
content-type: text/html

<HTML>
<HEAD>
<TITLE>FAQ</TITLE>
</HEAD>
<body bgcolor="#FFFFFF">
EOF
}
 sub posthead {
$list{'header'}=$textheader;
$list{'footer'}=$textfooter;
closeit();
}
 

And here's the user interface:

#!/usr/bin/perl
use Text::Soundex;
use CGI qw(:standard);
foreach $i (param) {
  foreach $j (param($i) ) {
    $$i=$j;
  }
}
dbmopen %list, "YOURDATAPATH/data/faqlist", 0700 or die "can't open faqlist";
dbmopen %data, "/YOURDATAPATH/faqdat", 0700 or die "can't open faqlist";
dbmopen %fkey, "/YOURDATAPATH/data/faqkeys", 0700 or die "can't open faqlist";
search() if ($posting and $what); 
review();
exit 0;

sub search {
  header();
  print "<p>$list{'header'}"; 
  print <<EOF;
<p>
<form method="POST" action="/cgi-bin/kfaq/faq.pl">
<p><input type=text size=20 name="what">
<input type=submit name="posting" value="Search" > (leave blank for all) 
EOF
  @searches=split / /, $what;
  $found="";
  foreach (@searches) {
    $smatch=soundex($_);
    foreach (sort keys %list) {
      next if not /line:/;
      $matched=$_;
      $words=$list{$_};
      $words .=  $data{$_};
      $words .=  $fkey{$_};
      @mywords=split / /,$words;
      foreach (@mywords) {
        if ($smatch eq soundex($_)) {
          $found{$matched}=$_; 
          last;
        }
      }
      
    }
  }
  foreach (sort keys %found) {
    $list{$_}=~ s/\</</g;
    $list{$_}=~ s/\>/>/g;
    $data{$_}=~ s/\</</g;
    $data{$_}=~ s/\>/>/g;
    print "<h2>$list{$_}</h2>";
    print "<p>$data{$_}";
   }
  print "<p>$list{'footer'}"; 
  print <<EOF;
</body>
</html>
EOF
dbmclose %list;
exit 0;
}

sub review {
  header();
  print "<h2>FAQS</h2>";
  print "<p>$list{'header'}"; 
  print <<EOF;
<p>
<form method="POST" action="/cgi-bin/kfaq/faq.pl">
<p><input type=text size=20 name="what">
<input type=submit name="posting" value="Search" > 
EOF
  foreach (sort keys %list) {
    $list{$_}=~ s/\</</g;
    $list{$_}=~ s/\>/>/g;
    $data{$_}=~ s/\</</g;
    $data{$_}=~ s/\>/>/g;
    next if not /^line:/;
    print "<h2>$list{$_}</h2>";
    print "<p>$data{$_}";
   }
  print "<p>$list{'footer'}"; 
  print <<EOF;
</body>
</html>
EOF
dbmclose %list;
exit 0;
}

sub header {
print <<EOF;;
content-type: text/html

<HTML>
<HEAD>
<TITLE>FAQS</TITLE>
</HEAD>
<body bgcolor="#FFFFFF">
EOF
}
 

Download faqadmin.pl

With any program like this that accepts html as input, you have to be concerned with someone deliberately putting "bad" html code in - javascript or other code that might subvert or damage. One way to do that is to allow only a specific set of input tags. For example, your code might do:
$input =~ s/</\&lt;/g;
$input =~ s/\&lt;p>/<p>/ig;

That eliminates everything but simple paragraph tags. None of that is included in the sample code here.

Publish your articles, comments, book reviews or opinions here!

© November 2002 Tony Lawrence All rights reserved



Got something to add? Send me email.





Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





Technology is both a tool for helping humans and for destroying them. This is the paradox of our times which we're compelled to face. (Frank Herbert)

The danger of computers becoming like humans is not as great as the danger of humans becoming like computers. (Konrad Zuse)








This post tagged: