#!/usr/bin/perl -w #################################### # # COPYRIGHT NOTICE: # # Copyright 2000 Aytekin Tank. # # This program may be used and modified free of charge by anyone, so # long as this copyright notice and the header above remain intact. # Selling the code for this program without prior written consent is # expressly forbidden. Obtain permission before redistributing this # program over the Internet or in any other medium. In all cases # copyright and header must remain intact. # # This program is distributed "as is" and without warranty of any # kind, either express or implied. All responsibility is belong to # you if any damage or loss occurs. #################################### ############################### # VARIABLES ############################### ############################### # Place you password here: ############################### $password = "admin"; ############################### # Directory # # if using NT, put the whole path like (/whole/path/) # leave it blank like I did for unix: ############################### $directory = ""; ############################### # FAQ files you will use: ############################### @faq_files = ("faq.html", "other/faq.html", "other_fql.html"); ############################### # To give the looka and feel you want # you can edit these parts in the FAQ tags # # they should be only one line: ############################### $before_question = "

"; $after_question = "
"; $before_answer = ""; $after_answer = ""; ############################### # Don't touch anything after this # unless you know who is Larry Wall.. ############################### ############################### # MENU ############################### $addon = "

Powered by FAQ Manager

"; &readQuery(); print "Content-type: text/html\n\n"; &initilize(); if($password ne $input{'pass'}) { &logOnForm(); &Exit(); } elsif ($input{'action'} eq "list") { &ReadFile(); &List(); } elsif (($input{'action'} eq "add") or ($input{'action'} eq "edit") or ($input{'action'} eq "delete")) { &ReadFile(); &ChangeForm(); } elsif ($input{'action'} eq "editnow") { &EditQuestion(); } elsif ($input{'action'} eq "addnow") { &AddNewQuestion(); } elsif ($input{'action'} eq "deletenow") { &DeleteQuestion(); } elsif ($input{'action'} eq "order") { &ReadFile(); &ChangeOrder(); } &Exit; ############################### # change order of the questions ############################### sub ChangeOrder { for ($i=1; $i<@questions; $i++) { if(not defined($input{$i})) { print "

Error: $i is undefined!

"; &Exit; } } for ($i=1; $i<=@questions; $i++) { $newquestions[$input{$i}] = @questions[$i]; $newanswers[$input{$i}] = @answers[$i]; } @questions = @newquestions; @answers = @newanswers; &CreateHtmlFile(); &Exit(); } ############################### # Add a new question ############################### sub AddNewQuestion { &ReadFile(); push(@questions, $input{'question'}); push(@answers, $input{'answer'}); &CreateHtmlFile(); &Exit(); } ############################### # Delete a question ############################### sub DeleteQuestion { &ReadFile(); for ($i=0; $i<@questions; $i++) { if($i != $input{'no'}) { push(@newquestions, $questions[$i]); push(@newanswers, $answers[$i]); } } @questions = @newquestions; @answers = @newanswers; &CreateHtmlFile(); &Exit(); } ############################### # Delete a question ############################### sub EditQuestion { &ReadFile(); for ($i=0; $i<@questions; $i++) { if($i == $input{'no'}) { $questions[$i] = $input{'question'}; $answers[$i] = $input{'answer'}; } } &CreateHtmlFile(); &Exit(); } ############################### # Parse the FAQ files ############################### sub ReadFile { &get_html_page(); foreach (@faqfile) { chomp; if($_ =~ m"" ) {$save=0; $end_start=1; next} elsif($_ =~ m"" ) {$save=1; $start_end=1; next;} elsif($_ =~ m"^" ) { $start_field=$1; next; } elsif($_ =~ m"^" ) { $start_field=0; next; } if($save) { if($_ =~ m"^" ) { $question_field=$1; next; } elsif($_ =~ m"^" ) { $question_field=0; next; } elsif($_ =~ m"^" ) { $answer_field=$1; next; } elsif($_ =~ m"^" ) { $answer_field=0; next; } if($question_field != 0) { $questions[$question_field] .= $_; next; } if($answer_field != 0) { $answers[$answer_field] .= $_; next; } } if(!$start_end){$before_faq .= "$_\n";} if($end_start){$after_faq .= "$_\n";} } } ############################### # List all questions for admin purposes ############################### sub List { my $question_lenght = 70; print "\n"; print qq~ ~; print "\n"; for($i=1; $i<@questions; $i++) { @qsqp = split("", $questions[$i]); if (@qsqp<$question_lenght) { $short_question = $questions[$i]; } else { for ($j=0;$j<$question_lenght; $j++){ $short_question .= $qsqp[$j]; } $short_question .= "..."; } print "\n\n\t"; print "\n\t\t"; print "\n\t"; print "\n\t\t ~; print "\n\t\t ~; print "\n\t"; $short_question=""; } print "\n\n\t"; print "\n\t\t"; print "\n\t\t ~; print "\n\t\t\n"; print "\n
OrderQuestionEditDelete
$short_question"; print qq~ Edit"; print qq~ Delete
"; print qq~ Add New Question
"; } ############################### # Create fie for add/modify/delete ############################### sub CreateHtmlFile { open (HTML, ">${directory}$input{'page'}") or "wrong path to $input{'page'}"; print HTML $before_faq."\n"; for($i=1; $i<@questions; $i++) { if ($i<10) { $no = "0$i"; } else { $no = $i; } print HTML "\n${before_question}$i. \n$questions[$i]\n${after_question}"; print HTML "\n${before_answer}\n$answers[$i]\n${after_answer}\n"; } print HTML "$addon\n${after_faq}\n"; close HTML; } ############################### # Get FAQ page - for internal use only from other subroutines ############################### sub get_html_page { open (FILE, "<${directory}$input{'page'}") or "wrong path to $input{'page'}!"; @faqfile = ; close FILE; } ############################### # FORM PARSE ############################### sub readQuery { if($ENV{'REQUEST_METHOD'} eq "GET") { @pairs = split(/&/, $ENV{'QUERY_STRING'}); foreach $pair (@pairs) { local($name, $value) = split(/=/, $pair); $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s///g; $value =~ tr/\|/\//; $input{$name} = $value; } } else { read(STDIN,$user_string,$ENV{'CONTENT_LENGTH'}); if (length($ENV{'QUERY_STRING'})>0) {$user_string=$ENV{'QUERY_STRING'}}; $user_string =~ s/\+/ /g; @name_value_pairs = split(/&/,$user_string); foreach $name_value_pair (@name_value_pairs) { ($keyword,$value) = split(/=/,$name_value_pair); $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/ge; $input{$keyword} = $value; } } $input{'login'} =~ s/\ /\_/g; } ############################### # Edit or add question form! ############################### sub ChangeForm { if($input{'action'} eq "edit") { $change = "Modify"; $action="editnow";} elsif($input{'action'} eq "delete") { $change = "Delete"; $action="deletenow";} else { $input{'no'} = @questions; $change = "Add"; $action = "addnow"; } print qq~
$change Question $input{'no'}:

Question:  
Answer:  

~; } ############################### # Log on page ############################### sub logOnForm { print qq~

FAQ Page:
Password:

~; } ############################### # Header of the pages ############################### sub initilize { print "
\n"; print ""; if(not $input{'image'} eq "nope") { print qq~ ~; print "\n"; print qq~ ~; } } ############################### # Footer of the pages ############################### sub Exit { if(not $input{'image'} eq "nope") { if(defined($input{'action'})) { print qq~

~; } print qq~
FAQ Manager version 0.1
Copyright © 2000 by interlogy

~; } exit; }