Приглашаем посетить
Достоевский (dostoevskiy-lit.ru)

WebForum

#!/usr/bin/perl
###########################################################################
#                                                                         #
#                     webforum.cgi - WebForum v1.0 LE                     #
#                                                                         #
#            Written by Michael Hall - mikeh@isaac.biola.edu              #
#      Written for Virtual Marketing Technologies - vmt@airmail.net       #
# Copyright ©1996 by Virtual Marketing Technologies, All Rights Reserved. #
#                                                                         #
###########################################################################

$BaseDir = "/var/www/docs/webforum/";
$MaxMsgs = 50;
$AdminMail = "vmt@airmail.net";
$CGI_URL = "/cgi-bin/wwwforum.cgi";

$Header = <<'EOT';
<html>
<head>
<title>Bulletins</title>
</head>
<body bgcolor=#FFFFFF>
<hr>
EOT

$Footer = <<'EOT';
<hr>
<img align=left src="http://www.virtualcenter.com/cgi-bin/logo.pl">
© 1996 and Designed by <a href="http://www.virtualcenter.com/vmt">
Virtual Marketing Technologies</a>.  All Rights Reserved.<br>
</body>
</html>
EOT

#####################################################################
#YOU MAY NOT EDIT ANYTHING BELOW THIS POINT WITHOUT PERMISSION OF
#THE AUTHOR.  DOING SO IS A DIRECT VIOLATION OF THE TERMS OF USAGE OF
#THIS SOFTWARE AND A VIOLATION OF U.S. AND INTERNATIONAL COPYRIGHT
#LAWS AND SUBJECT TO FINES AND/OR IMPRISONMENT
#####################################################################

# Get the Date for Entry
$date = `date +"%A, %B %d, %Y at %T (%Z)"`;
        chop($date);
$shortdate = `date +"%D %T %Z"`;
        chop($shortdate);


if ($ENV{'QUERY_STRING'} ne "") { # Parse the query string
	&UnWeb1;
	$Q = 1;
}
else {
	&UnWeb;
	$Q = 0;
}

if ($in{'action'} eq "") { # Start the program for the specified forum
	if ($in{'forum'} eq "") { # No forum specified
		&Error ('No_Forum_Specified');
	}
	
	# Read in the header and footer files for the specified forum
	
	&LoadHeader;
	&LoadFooter;
		
	# Now read in the datafile for this forum
	
	open (DATAFILE, "$BaseDir/$in{'forum'}.dta");
	@Data = <DATAFILE>;
	close DATAFILE;
	
	# Now display the current messages to the user
	
	&PrintHeader ($in{'forum'});
	foreach $Line (@Header) {
		print "$Line\n";
	}
	print "<center><h1>$in{'forum'}</h1></center>\n";
	print "<table width=100% border=1>\n";
	print "<tr><td align=center><font size=-1><b>Subject:</b>
</font></td><td align=center><font size=-1><b>
Author:</b></font></td><td align=center><font size=-1>
<b>Date Posted:</b></font></td></tr>\n";
	foreach $Message (@Data) {
		@TempData = split (/\t/, $Message);
		print "<tr><td><a href=\"$CGI_URL?action=view&value=$TempData[0]
&forum=$in{'forum'}\"><font size=-1>$TempData[1]</a></font></td>
<td><a href=\"mailto:$TempData[3]\"><font size=-1>$TempData[2]</a>
</font></td><td><font size=-1>$TempData[4]</font></td></tr>\n";
	}
	print "</table><p>\n";
	print "<font size=-1><a href=\"http://www.virtualcenter.com/scripts2\">
WebForum v1.0</a> - Copyright © 1996 by <a 
href=\"http://www.virtualcenter.com/vmt\">Virtual Marketing Technologies</a>.  
All Rights Reserved.</font><br>\n";
	print "<center><a href=\"$CGI_URL?action=post&forum=$in{'forum'}\">Post a New Message</a></center><p>\n";
	foreach $Line (@Footer) {
		print "$Line\n";
	}
} #start

if ($in{'action'} eq "post") { # Post a new message
	if ($in{'forum'} eq "") { # No forum specified
		&Error ('No_Forum_Specified');
	}
	
	&LoadHeader;
	&LoadFooter;
	
	&PrintHeader ("Post a New Message to $in{'forum'}");
	foreach $Line (@Header) {
		print "$Line\n";
	}
	print "<center><h2>Post a New Message to $in{'forum'}</h2></center>\n";
	print "Please fill out the form completely to add your message to the forum:<p>\n";
	print "<form method=post action=\"$CGI_URL\">\n";
	print "<input type=hidden name=\"action\" value=\"post2\">\n";
	print "<input type=hidden name=\"forum\" value=\"$in{'forum'}\">\n";
	print "<b>Your Name: </b><input type=text name=\"Author\" size=40><br>\n";
	print "<b>Your EMail: </b><input type=text name=\"EMail\" size=40><br>\n";
	print "<b>Subject: </b><input type=text name=\"Subject\" size=40><p>\n";
	print "<b>Enter your message in the text box below:</b><p>\n";
	print "<textarea name=\"Comments\" cols=70 rows=7 wrap=soft></textarea><p>\n";
	print "<center><input type=submit value=\"Post Message\"> <input type=reset value=\"Clear Message\"></center><p>\n";
	foreach $Line (@Footer) {
		print "$Line\n";
	}
	exit;
}

if ($in{'action'} eq "post2") { # Now write the posted info to the files
	if ($in{'forum'} eq "") { # No forum specified
		&Error ('No_Forum_Specified');
	}
	
	&LoadHeader;
	&LoadFooter;
	
	# Check to see that everything was entered correctly
	
	if (($in{'Subject'} eq "") || ($in{'Author'} eq "")) {
		&Error ('Incomplete_Form');
	}
	
	# Assign it a message number
	
	open (MSGNUM, "$BaseDir/Messages.cnt");
	$MsgNum = <MSGNUM>;
	close MSGNUM;
	
	# Now update the message number by one
	
	$MsgNum++;
	if ($MsgNum > 999999) {
		$MsgNum = 0;
	}
	
	open (MSGNUM, ">$BaseDir/Messages.cnt");
	print MSGNUM "$MsgNum\n";
	close MSGNUM;
	
	# Now write the text to the message file
	
	open (TEXT, ">$BaseDir/$MsgNum.txt");

    # Expand the comments for html
    $in{'Comments'} =~ s/\n/<BR>\n/go;
    $in{'Quote'} =~ s/\n/<BR>\n/go;
    
    if ($in{'Quote'} ne "") {
    	print TEXT "<font size=-1><b><i>$in{'Quote'}</b></i></font><p>\n";
    }
	
	print TEXT "$in{'Comments'}\n";
	close TEXT;
	
	# Now write to the datafile
	
	open (DATAFILE, "$BaseDir/$in{'forum'}.dta");
	@Data = <DATAFILE>;
	close DATAFILE;
	chop @Data;
	
	open (DATAFILE, ">$BaseDir/$in{'forum'}.dta");
	&lock(DATAFILE);
	
	$MsgCount = 1;
	print DATAFILE "$MsgNum\t$in{'Subject'}\t$in{'Author'}\t$in{'EMail'}\t$shortdate\n";
	
	foreach $Line (@Data) {
		$MsgCount++;
		if ($MsgCount <= $MaxMsgs) {
			print DATAFILE "$Line\n";
		}
		else {
			unlink ("$BaseDir/$MsgCount.txt");
		}
	}
		
	&unlock(DATAFILE);
	
	close (DATAFILE);
	
	# Now return the user to the start
	
	print "Location:$CGI_URL?forum=$in{'forum'}\n\n";
}

if ($in{'action'} eq "view") { # Read a message
	if ($in{'forum'} eq "") { # No forum specified
		&Error ('No_Forum_Specified');
	}
	
	&LoadHeader;
	&LoadFooter;
	
	if ($in{'value'} eq "") { # User didn't specify a message
		&Error ('No_Message_Specified');
	}
	
	# Read in the datafile
	
	open (DATAFILE, "$BaseDir/$in{'forum'}.dta");
	@Data = <DATAFILE>;
	close (DATAFILE);
	
	foreach $Temp (@Data) {
		@TempStuff = split (/\t/, $Temp);
		if ($TempStuff[0] eq "$in{'value'}") {
			$Subject = $TempStuff[1];
			$Author2 = $TempStuff[2];
			$EMail = $TempStuff[3];
			$Date = $TempStuff[4];
		}
	}
	
	# Read in the message and display it
	
	open (MSG, "$BaseDir/$in{'value'}.txt");
	@Text = <MSG>;
	close (MSG);
	chop @Text;
	
	$QuoteText = join (/ /, @Text);
	
	$QuoteText =~ s/<([^>]|\n)*>//g;
	
	if ($EMail eq "") {
		$Author = $Author2;
	}
	else {
		$Author = "<a href=\"mailto:$EMail\">$Author2</a>";
	}
	
	&PrintHeader ("Subject");
	foreach $Line (@Header) {
		print "$Line\n";
	}
	print "<center><h2>$Subject</h2></center>\n";
	print "<b>Posted by: </b>$Author on $Date<p>\n";
	foreach $TextLine (@Text) {
		print "$TextLine\n";
	}
	print "<p><center><a href=\"$CGI_URL?forum=$in{'forum'}\">Return to Message List</a></center><p><hr><p>\n";
	print "<center><h2>Reply to $Subject</h2></center>\n";
	
	&PrintReply;
	
	foreach $Line (@Footer) {
		print "$Line\n";
	}
	exit;
}
	


sub PrintReply { # Print the form to post a new message


	print "Please fill out the form completely to add your message to the forum:<p>\n";
	print "<form method=post action=\"$CGI_URL\">\n";
	print "<input type=hidden name=\"action\" value=\"post2\">\n";
	print "<input type=hidden name=\"forum\" value=\"$in{'forum'}\">\n";
	print "<b>Your Name: </b><input type=text name=\"Author\" size=40><br>\n";
	print "<b>Your EMail: </b><input type=text name=\"EMail\" size=40><br>\n";
	print "<b>Subject: </b><input type=text name=\"Subject\" size=40 value=\"Re: $Subject\"><p>\n";
	print "<b>Use the box below to quote any part of the message in your reply:</b><p>\n";
	print "<textarea name=\"Quote\" cols=70 rows=3 wrap=soft>$QuoteText</textarea><p>\n";
	print "<b>Enter your message in the text box below:</b><p>\n";
	print "<textarea name=\"Comments\" cols=70 rows=7 wrap=soft></textarea><p>\n";
	print "<center><input type=submit value=\"Post Message\"> <input type=reset value=\"Clear Message\"></center><p>\n";
}


sub UnWeb1 {
   # Get the input
#   read(STDIN, $buffer, $ENV{'QUERY_STRING'});

   # Split the name-value pairs
   @pairs = split(/&/, $ENV{'QUERY_STRING'});

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      # Un-Webify plus signs and %-encoding
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;

      if ($allow_html != 1) {
         $value =~ s/<([^>]|\n)*>//g;
      }
      else {
         unless ($name eq 'body') {
	    $value =~ s/<([^>]|\n)*>//g;
         }
      }

      $in{$name} = $value;
   }

}

#######################
# Print HTML Header

sub PrintHeader {
	local ($Title) = @_;

	print "Content-type: text/html\n\n";
	print "<html>\n";
	print "	<head>\n";
	print "		<title>$Title</title>\n";
	print "	</head>\n";
}


sub lock {
 local($file)=@_;
 $LOCK_SH = 1;
 $LOCK_EX = 2;
 $LOCK_NB = 4;
 $LOCK_UN = 8;
 flock($file,$LOCK_EX);
 seek($file, 0, 2);
} #lock

sub unlock {
 local($file)=@_;
 flock($file,$LOCK_UN);
} #unlock


sub Error {
	local ($Error) = @_;
	if ($Error eq "No_Forum_Specified") {
		&PrintHeader ("No Forum Specified");
		print "$Header\n";
		print "<center><h1>Error: No Forum Specified!</h1></center>\n";
		print "A system error occurred.  No forum was specified.  
Please notify the <a href=\"mailto:$AdminMail\">system administrator</a> 
and notify him/her of the error.<p>\n";
		print "$Footer\n";
		exit;
	}
	
	if ($Error eq "Incomplete_Form") {
		&PrintHeader ("Incomplete Form");
		print "$Header\n";
		print "<center><h1>Error: Incomplete Form!</h1></center>\n";
		print "You did not fill out the form completely.  All requested 
information must be filled in before your message can be posted.<p>\n";
		print "Please go back and fill in all information.<p>\n";
		print "$Footer\n";
		exit;
	}
	
	if ($Error eq "No_Message_Specified") {
		&PrintHeader ("No Message Specified");
		print "$Header\n";
		print "<center><h1>Error: No Message Specified!</h1></center>\n";
		print "You did not specify a message to view.  If you feel 
you are receiving this message in error, please report this error to 
the <a href=\"mailto:$AdminMail\">System Administrator</a>.<p>\n";
		print "$Footer\n";
		exit;
	}
}

sub LoadHeader {

	open (HEADER, "$BaseDir/$in{'forum'}.head");
	@Header = <HEADER>;
	close HEADER;
	chop (@Header);
	
}

sub LoadFooter {

	open (FOOTER, "$BaseDir/$in{'forum'}.foot");
	@Footer = <FOOTER>;
	close FOOTER;
	chop (@Footer);
}

sub UnWeb {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      # Un-Webify plus signs and %-encoding
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;

      if ($AllowHTML != 1) {
         $value =~ s/<([^>]|\n)*>//g;
      }
      else {
         unless ($name eq 'body') {
	    $value =~ s/<([^>]|\n)*>//g;
         }
      }

      $in{$name} = $value;
   }

}