#!/usr/bin/perl
##########################################################################
#
# DForum 1.01 (11/02/1999)
# http://www.dansteinman.com/dforum
#
# Copyright (C) 1998 Dan Steinman
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License (license.txt) for more details.
#
##########################################################################
#
# Forum Core Script
# filename: forum.pl
# description: Displays all pages and submits new messages
#
##########################################################################
# Set-Up Variables
# The document-root environment variable is used to find your path so
# that you can easily transfer the script between servers without having
# to change anything. If it doesn't work you can manually state the full
# path for the $forumdir variable
$root = "$ENV{'DOCUMENT_ROOT'}";
# This is the directory which all the data files for the forum will be
# held. Most likely you'll want to have it as a sub-directory off of
# your cgi-bin but it can be anywhere. This directory will be created
# by the script so you need not set up any thing before hand.
$forumdir = "$root/cgi-bin/dforum/forumdata";
# The HTML variable determines what will happen if a user writes HTML into
# the body of the forum. I did not allow html into the name and subject fields
# because that leads to problems when users abuse it. "yes" will make it display
# the HTML as normal, "no" will strip away all the HTML leaving just the text,
# and "show" will show the actual HTML code in the output, ie. changes < into <
$html = "show";
##########################################################################
# parse form values
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
local($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s///g;
$in{$name} = $value;
}
##########################################################################
# miscellaneous values
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = 1900 + $year;
$thismonth = ($mon + 1);
$lastmonth = $mon;
if ($thismonth <10) {$thismonth = "0$thismonth";}
if ($lastmonth <10) {$lastmonth = "0$lastmonth";}
if ($mday <10) {$mday = "0$mday";}
$todaysdate = "$thismonth/$mday/$year";
$newheaderfile = "$year$thismonth";
@months = ("January","February","March","April","May","June","July","August","September","October","November","December");
##########################################################################
# determine what sub-routine to run based on the $command query string
if ($ENV{'QUERY_STRING'} ne '') {
@qstr = split (/=/,$ENV{'QUERY_STRING'});
$command = $qstr[0];
$value = $qstr[1];
$value =~ s/[\x00-\x20<>\|;\(\)\$^\+!\^\[\]\?\"\'\`]//g;
if ($command eq "archives") {&displayArchives;}
elsif ($command eq "month") {&displayMonth ($value);}
elsif ($command eq "msg") {&displayMessage ($value);}
elsif ($command eq "submit") {&displaySubmit;}
elsif ($command eq "reply") {&displayReply ($value);}
elsif ($command eq "preview") {&displayPreview;}
else {&displayDefault;}
}
else {
# if no query string check for a 'post' form value (needed when posting a new thread or reply)
if ($in{'post'}) {&postMessage;}
# print the default page
else {&displayDefault;}
}
##########################################################################
# prints the default forum page, displays the last 2 months
sub displayDefault {
&printHeader("Forum");
print <<"EOF";
Submit a Message
EOF
&printMonthLines ("$year$thismonth");
&printMonthLines ("$year$lastmonth");
print <<"EOF";
View Archived Messages
EOF
&printFooter;
}
##########################################################################
# prints a list of months that have been archived
sub displayArchives {
open (MONTHS,"$forumdir/months.txt");
@monthlines = ;
close (MONTHS);
&printHeader("Forum - [Archived Messages]");
print <<"EOF";
EOF
foreach $headerfile (@monthlines) {
if ($headerfile ne '') {
$year = substr ($headerfile,0,4);
$month = substr ($headerfile,4,6);
$month = sprintf("%5.0f", $month);
$monthname = $months[$month-1];
print " $monthname | $year |
\n";
}
}
print <<"EOF";
Back To Forum [last 2 months]
EOF
&printFooter;
}
##########################################################################
# prints the headers for a given month
sub displayMonth {
my($headerfile) = @_;
$year = substr ($headerfile,0,4);
$month = substr ($headerfile,4,6);
$month = sprintf("%5.0f", $month);
$monthname = $months[$month-1];
&printHeader("Forum - [$monthname $year]");
print <<"EOF";
Submit a Message
EOF
&printMonthLines ($headerfile);
print <<"EOF";
Back To Archives
EOF
&printFooter;
}
##########################################################################
# prints the headers for a given month
sub printMonthLines {
my($filename) = @_;
if (open(DATA,"$forumdir/$filename.txt")) {
@msglines = ;
close(DATA);
foreach $msgline (@msglines) {
if ($msgline ne '') {
@info = split (/_/, $msgline);
$num = $info[0];
$subject = $info[1];
$name = $info[2];
$date = $info[3];
$responses = $info[4];
print " $subject | $name | $responses | $date |
\n";
}
}
}
}
##########################################################################
# prints a particular message
sub displayMessage {
my($filename) = @_;
open(DATA,"$forumdir/messages/$filename.txt");
@msglines = ;
close(DATA);
for ($i=0; $i<@msglines; $i++) {
if ($msglines[$i] =~ /(.*)<\/num>/) {$num = $1;}
elsif ($msglines[$i] =~ /(.*)<\/headerfile>/) {$headerfile = $1;}
elsif ($msglines[$i] =~ /(.*)<\/subject>/) {$subject = $1;}
}
&printHeader("Forum - [$subject]");
print <<"EOF";
Back To Forum |
Submit A Message |
Reply To This Message
EOF
&parseMessageData ($filename);
&printFooter;
}
##########################################################################
# sub-routine of &displayMessage and &displayReply to extract the
# name, email, date etc. from the message file
sub parseMessageData {
my($filename) = @_;
my($msgstart, $msgend, @msglines);
open(DATA,"$forumdir/messages/$filename.txt");
@msglines = ;
close(DATA);
for ($i=0; $i<@msglines; $i++) {
if ($msglines[$i] =~ //) {push (@msgstart, $i);}
elsif ($msglines[$i] =~ /<\/msg>/) {push (@msgend, $i);}
}
for ($i=0; $i<@msgstart; $i++) {
for ($j=$msgstart[$i]; $j<$msgend[$i]; $j++) {
if ($msglines[$j] =~ /(.*)<\/name>/) {
$name = $1;
if ($i != 0) {
print "
Reply \#$i\n";
}
else {print "";}
print "
From: $name";
}
elsif ($msglines[$j] =~ /(.*)<\/email>/) {
$email = $1;
if ($email) {print " ($email)";}
}
elsif ($msglines[$j] =~ /(.*)<\/date>/) {
$date = $1;
print "
Date: $date\n";
}
elsif ($msglines[$j] =~ //) {
print "\n";
for ($k=$j+1; $k<@msglines; $k++) {
if ($msglines[$k] =~ /<\/body>/) {
print "
";
last;
}
else {print "$msglines[$k]";}
$j++;
}
}
}
}
}
##########################################################################
# prints the submit page
sub displaySubmit {
&printHeader("Forum - [Submit Message]");
print <<"EOF";