Приглашаем посетить
Культурология (cult-lib.ru)

WebNews

#!/usr/bin/perl
###########################################################################
#                                                                         #
#           News.cgi - Perl Script for Posting News Bulletins             #
#                                                                         #
#            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. #
#                                                                         #
###########################################################################

$Password = "Infosoft";
$BaseDir = "/var/www/docs/scripts2/bulletins";
$BaseURL = "http://www.virtualcenter.com/scripts2/bulletins";
$CGI_URL = "http://www.virtualcenter.com/cgi-bin/webnews.cgi";
$DataFile = "News.dta";
$AllowHTML = 1; # Set to 1 to allow html commands in the text

$Header = <<'EOT';
<body bgcolor=#FFFFFF>
<P ALIGN=CENTER><A NAME="top"></A><IMG SRC="http://www.virtualcenter.com/scripts2/bulletins/VirtNewsSystem.gif" WIDTH="416"
HEIGHT="102" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="WebNews v2.0"><BR>
<HR>
EOT


$Footer = <<'EOT';
<hr>
<img align=left src="/clip_art/VMTIcon.jpg">© 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.  EDITING ANYTHING BELOW THIS
#POINT IS AN INFRINGEMENT OF COPYRIGHT AND LICENSE AGREEMENT.
#***************************************************************************


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

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

if ($in{'action'} eq "add") {

	&PrintHeader;
	print "<HTML>\n";
	print "<HEAD>\n";
	print "  <TITLE>Add News</TITLE>\n";
	print "</HEAD>\n";
	print "<BODY BGCOLOR=\"#ffffff\">\n";
	print "\n";
	print "<FORM method=post action=\"$CGI_URL\">\n";
	print "<input type=hidden name=\"action\" value=\"add2\">\n";
	print "<P ALIGN=CENTER><IMG SRC=\"$BaseURL/AddNews.gif\" WIDTH=\"415\" HEIGHT=\"99\" ALIGN=\"BOTTOM\"\n";
	print "NATURALSIZEFLAG=\"3\"></P>\n";
	print "\n";
	print "<P><FONT SIZE=-1>To add a new news story to the system, enter the filename\n";
	print "(excluding the ".html extension), the headline for the story, the lead\n";
	print "paragraph for the story, and the body of the story. The headline and lead\n";
	print "paragraph will be shown to users in the list of all stories available.</FONT></P>\n";
	print "\n";
	print "<P><TABLE WIDTH=\"100%\" BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">\n";
	print "<TR>\n";
	print "<TD WIDTH=\"92\"><P ALIGN=RIGHT><B><FONT SIZE=-1>Password:</FONT></B></TD>\n";
	print "<TD WIDTH=\"80%\"><INPUT NAME=\"Password\" TYPE=\"password\" SIZE=\"30\"></TD></TR>\n";
	print "<TD VALIGN=\"TOP\"><P ALIGN=RIGHT><B><FONT SIZE=-1>Filename:</FONT></B></TD>\n";
	print "<TD><INPUT NAME=\"Filename\" TYPE=\"text\" SIZE=\"25\" MAXLENGTH=\"25\"></TD></TR>\n";
	print "<TR>\n";
	print "<TD VALIGN=\"TOP\"><P ALIGN=RIGHT><B><FONT SIZE=-1>Headline:</FONT></B></TD>\n";
	print "<TD><INPUT NAME=\"Headline\" TYPE=\"text\" SIZE=\"50\"></TD></TR>\n";
	print "<TR>\n";
	print "<TD VALIGN=\"TOP\"><P ALIGN=RIGHT><B><FONT SIZE=-1>Lead Paragraph:</FONT></B></TD>\n";
	print "<TD><TEXTAREA NAME=\"Lead\" WRAP=\"VIRTUAL\" ROWS=\"4\" COLS=\"50\"\n";
	print "></TEXTAREA></TD></TR>\n";
	print "<TR>\n";
	print "<TD VALIGN=\"TOP\"><P ALIGN=RIGHT><B><FONT SIZE=-1>Body of Story:</FONT></B></TD>\n";
	print "<TD><TEXTAREA NAME=\"Body\" WRAP=\"VIRTUAL\" ROWS=\"10\" COLS=\"50\"\n";
	print "></TEXTAREA></TD></TR>\n";
	print "</TABLE>\n";
	print "</P>\n";
	print "\n";
	print "<P ALIGN=CENTER><INPUT NAME=\"name\" TYPE=\"submit\" VALUE=\"Submit\"><INPUT \n";
	print "NAME=\"name\" TYPE=\"reset\" VALUE=\"Reset\"></P>\n";
	print "\n";
	print "<P><HR ALIGN=LEFT><IMG SRC=\"http://www.virtualcenter.com/cgi-bin/logo.pl\"\n";
	print "ALIGN=\"LEFT\"><FONT SIZE=-1>Copyright ©1996 by Virtual Marketing Technologies.\n";
	print "All Rights Reserved.</FONT></FORM>\n";
	print "</BODY>\n";
	print "</HTML>\n";
}

if ($in{'action'} eq "add2") {
	# Check to see that all information was filled in
	if (($in{'Filename'} eq "") || ($in{'Headline'} eq "") || ($in{'Lead'} eq "") ||
		($in{'Body'} eq "") || ($in{'Password'} eq "")) { # Not all information was filled in
			&Error (Information_Incomplete);
	}
	
	unless ($in{'Password'} eq $Password) { # Password is incorrect
		&Error ("Password_Incorrect");
	}
	
	$in{'Lead'} =~ s/[ \n\t\r\f]/ /go;

	# Write the information to the file

    # Expand the comments for html
    $in{'Body'} =~ s/\n/<BR>\n/go;

	open (NEWSFILE,">$BaseDir/$in{'Filename'}.html");
	print NEWSFILE "$Header\n";
	print NEWSFILE "<b><center><h2>$in{'Headline'}</h2></center></b>\n";
	print NEWSFILE "<b>Posted on: $date</b><p>\n";
	print NEWSFILE "$in{'Lead'}<p>\n";
	print NEWSFILE "$in{'Body'}<p>\n";
	print NEWSFILE "$Footer\n";
	close NEWSFILE;
	
	# Now update the index file for that directory.
	
    open(GW,">>$BaseDir/$DataFile");
	print GW "$in{'Headline'}\t$in{'Filename'}.html\t$in{'Lead'}\n";
    close GW;

	&PrintHeader;
	print "<html>\n";
	print " <head>\n";
	print "  <title>News Posted</title>\n";
	print " </head>\n";
	print "$Header\n";
	print "<center><h1>News Posted!</h1></center>\n";
	print "The news entitled $in{'Filename'} has been posted to directory $BaseDir/$in{'Filename'}.<p>\n";
	print "</body>\n </html>\n";
	
}

if ($in{'action'} eq "delete") {

	# Read in the datafile
	
	open (DATA, "$BaseDir/$DataFile");
	@Data = <DATA>;
	close DATA;
	
	$Count = 0;
	foreach $Line (@Data) {
		@Temp = split (/\t/, $Line);
		$StoryLine[$Count] = "<OPTION value=\"$Temp[1]\">$Temp[0]\n";
		$Count++;
	}
	
	&PrintHeader;
	print "<HTML>\n";
	print "<HEAD>\n";
	print "  <TITLE>Delete News</TITLE>\n";
	print "</HEAD>\n";
	print "<BODY BGCOLOR=\"#ffffff\">\n";
	print "\n";
	print "<FORM method=post action=\"$CGI_URL\">\n";
	print "<input type=hidden name=\"action\" value=\"delete2\">\n";
	print "<P ALIGN=CENTER><IMG SRC=\"$BaseURL/DeleteNews.gif\" WIDTH=\"414\" HEIGHT=\"95\" ALIGN=\n";
	print "\"BOTTOM\" NATURALSIZEFLAG=\"3\"></P>\n";
	print "\n";
	print "<P><B><FONT SIZE=-1>To delete a news story from the system, select the story's\n";
	print "headline below and hit the delete button.</FONT></B></P>\n";
	print "\n";
	print "<P ALIGN=CENTER><TABLE WIDTH=\"3%\" BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\n";
	print "\"0\">\n";
	print "<TR>\n";
	print "<TD WIDTH=\"46%\"><P ALIGN=RIGHT><B><FONT SIZE=-1>Password:</FONT></B></TD>\n";
	print "<TD WIDTH=\"54%\"><INPUT NAME=\"Password\" TYPE=\"password\" SIZE=\"30\"></TD></TR>\n";
	print "<TR>\n";
	print "<TD><P ALIGN=RIGHT><B><FONT SIZE=-1>Choose the story to delete:</FONT></B></TD>\n";
	print "<TD><SELECT name=\"Filename\">\n";
	foreach $Story (@StoryLine) {
		print "$Story";
	}
	print "</SELECT></TD></TR>\n";
	print "</TABLE>\n";
	print "</P>\n";
	print "\n";
	print "<P ALIGN=CENTER><INPUT TYPE=\"submit\" VALUE=\"Delete\"><INPUT \n";
	print "NAME=\"name\" TYPE=\"reset\" VALUE=\"Reset\"></P>\n";
	print "\n";
	print "<P><HR ALIGN=LEFT><FONT SIZE=-1>Copyright ©1996 by Virtual Marketing\n";
	print "Technologies. All Rights Reserved.</FONT></FORM>\n";
	print "</BODY>\n";
	print "</HTML>\n";
}

if ($in{'action'} eq "delete2") {

	if (($in{'Password'} eq "") || ($in{'Filename'} eq "")) {
		&Error ("Information_Incomplete");
	}
	
	unless ($in{'Password'} eq $Password) { # The password doesn't match
		&Error ("Password_Incorrect");
	}
	
	unlink ("$BaseDir/$in{'Filename'}");

	# Now update the index file for that directory.
	
	open(GW,"$BaseDir/$DataFile");
	@lines = <GW>;
	close GW;
	
	open (GB,">$BaseDir/$DataFile");
	foreach $line (@lines) {
		chop $line;
		@TempData = split (/\t/, $line);
		
		if ($TempData[1] ne "$in{'Filename'}") {
			print GB "$line\n";
		}
	}
	close GB;
	
	&PrintHeader;
	print "<html>\n";
	print " <head>\n";
	print "  <title>News Deleted</title>\n";
	print " </head>\n";
	print "$Header\n";
	print "<center><h1>News Deleted!</h1></center>\n";
	print "The news $in{'Filename'} has been deleted.<p>\n";
	print "$Footer\n";
	
}

if ($in{'action'} eq "") { # Display all available stories
	# Open the datafile
	
	open (DATA, "$BaseDir/$DataFile");
	@Data = <DATA>;
	close DATA;
	
	&PrintHeader;
	print "<html>\n";
	print " <head>\n";
	print "  <title>Available Stories</title>\n";
	print " </head>\n";
	print "$Header\n";
	print "<font size=-1>Click on a headline to view the full article:<p></font>\n";
	foreach $Story (@Data) {
		@Temp = split (/\t/, $Story);
		print "<a href=\"$CGI_URL?action=view&item=$Temp[1]\"><h2>$Temp[0]</a></h2>\n";
		print "$Temp[2]<p><hr><p>\n";
	}
	print "<font size=-1><a href=\"http://www.virtualcenter.com/scripts2/\">WebNews v2.0</a> 
Copyright ©1996 by <a href=\"http://www.virtualcenter.com/vmt/\">Virtual 
Marketing Technologies</a>.  All Rights Reserved.<br>\n";
	print "$Footer\n";
}
		

if ($in{'action'} eq "view") {
	print "Location: $BaseURL/$in{'item'}\n\n";
}

sub Error {
	local($UserError) = @_;
	
	if ($UserError eq "Information_Incomplete") {
		&PrintHeader;
		print "<html>\n";
		print " <head>\n";
		print "  <title>Information Incomplete</title>\n";
		print " </head>\n";
		print "$Header\n";
		print "<center><h2>Information Incomplete!</h2></center>\n";
		print "You did not fill out all required information.  
Please go back and make sure every field is filled in before submitting.<p>\n";
		print "$Footer\n";
		print "</body>\n";
		print "</html>\n";
		exit;
	}
	
	if ($UserError eq "Password_Incorrect") {
		&PrintHeader;
		print "<html>\n";
		print " <head>\n";
		print "  <title>Incorrect Password</title>\n";
		print " </head>\n";
		print "$Header\n";
		print "<center><h2>Incorrect Password!</h2></center>\n";
		print "The password you entered was incorrect.  Please go back 
and re-enter your password.<p>\n";
		print "$Footer\n";
		print "</body>\n";
		print "</html>\n";
		exit;
	}
}

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;
   }

}

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;
      }

      $in{$name} = $value;
   }

}


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

sub PrintHeader {

	print "Content-type: text/html\n\n";
}