Приглашаем посетить
Гумилев (gumilev.lit-info.ru)

WebSearch

#!/usr/bin/perl
###########################################################################
#                                                                         #
#          websearch.pl - Perl Script for Searching Web Sites             #
#                                                                         #
#            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. #
#                                                                         #
###########################################################################

$MaxWords = 50;
$BaseDir = "/var/www/docs/websearch";
$BaseURL = "http://www.virtualcenter.com/websearch";
@Categories = ('Arts','Business','Computers','Education','Entertainment','Government',
'Health','News','Recreation','Reference','Regional','Science','SocialScience','Society');
$FileFlag = 0;
$MatchFlag = 0;
$MatchFlag2 = 0;
$allow_html = 0;

&UnWeb;

$Header = <<'EOT';
<body bgcolor=#FFFFFF text=#000000>

<center>
 <img src="/websearch/WebSearchLogo.gif"><br>
 <a href="/websearch/AddURL.html"><img border=0 src="/websearch/AddURL.gif">
</a><a href="/websearch/index.html"><img border=0 
src="/websearch/Search.gif"></a><a href="/cgi-bin/scriptswebsearch.pl?random">
<img border=0 src="/websearch/RandomLink.gif"></a>
</center><P><hr><p>
<center>
 <form method=post action="/cgi-bin/scripts/websearch.pl">
 <input type=hidden name="action" value="search">
 <input type=text size=40 name="Keywords"> <input type=submit value="Search!">
</center><p><hr><p></b>
EOT

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


if ($ENV{'QUERY_STRING'} ne "") {

	@TempCheck = split (/=/, $ENV{'QUERY_STRING'});

	if ($TempCheck[0] eq "random") {
		# Send the user to a random web site
		
			srand ();
			
			$NumCategories = @Categories;
			
			while ($RandomMatch == 0) {
				$randnum = int(rand($NumCategories));
				if (-e "$BaseDir/$Categories[$randnum].dta") {
					$RandomMatch = 1;
				}
			}
			
			open (DB, "$BaseDir/$Categories[$randnum].dta");
			@URL = <DB>;
			close DB;
			
			$NumSites = @URL;
						
			$randnum2 = int(rand($NumSites));
			
			@TempData = split (/&&/, $URL[$randnum2]);
			print "Location: $TempData[1]\n\n";
	}


	$Category = $TempCheck[1];
	


	# The user selected just a category from the search page
	

	if (-e "$BaseDir/$Category.dta") {
		open (DB, "$BaseDir/$Category.dta");
		@Data = <DB>;
		close DB;
		&PrintHeader;
		print "<html>\n <head>\n <title>Matches</title>\n </head>\n";
		print "$Header\n";
		print "The following entries matched your request:<p>\n";
		print "<table border=2>\n";
		print "<tr><td align=center><b>Title:</b></td><td align=center><b>Description:</b></td></tr>\n";
		foreach $Match (@Data) {
			@TempData = split (/&&/, $Match);
			print "<tr><td valign=\"top\"><a href=\"$TempData[1]\">$TempData[0]</a></td><td valign=\"top\">$TempData[2]</td></tr>\n";
		}
		print "</table><p>\n";
		print "Processed by \"WebSearch v1.0 WWW Search Engine\" by <a 
href=\"http://www.virtualcenter.com/scripts2\">Michael Hall</a> and <a 
href=\"http://www.virtualcenter.com/vmt\">Virtual Marketing Technologies</a>.<br>\n";
		print "Copyright © 1996 - All Rights Reserved.<br>\n";
		print "$Footer\n";
		exit;
	}
	else {
		&PrintHeader;
		print "<html>\n <head>\n <title>Matches</title>\n </head>\n";
		print "$Header\n";
		print "We're sorry, there are currently no entries in this category.<p>\n";
		print "Processed by \"WebSearch v1.0 WWW Search Engine\" by <a 
href=\"http://www.virtualcenter.com/scripts2\">Michael Hall</a> and <a 
href=\"http://www.virtualcenter.com/vmt\">Virtual Marketing Technologies</a>.<br>\n";
		print "Copyright © 1996 - All Rights Reserved.<br>\n";
		print "$Footer\n";
		exit;
	}
}	


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

	# Add a URL to the database
	
	# Check to make sure everything was entered correctly
	
	if (($in{'Title'} eq "") || ($in{'URL'} eq "") || ($in{'Category'} eq "") || ($in{'Description'} eq "")) {
		# Something was left blank
		
		&PrintHeader;
		print "<html>\n <head>\n <title>Error:  Blank Entry</title>\n </head>\n";
		print "$Header\n";
		print "You did not enter in all the information requested.  Please go back and make sure everything is filled in.<p>\n";
		print "Processed by \"WebSearch v1.0 WWW Search Engine\" by <a 
href=\"http://www.virtualcenter.com/scripts2\">Michael Hall</a> and <a 
href=\"http://www.virtualcenter.com/vmt\">Virtual Marketing Technologies</a>.<br>\n";
		print "Copyright © 1996 - All Rights Reserved.<br>\n";
		print "$Footer\n";
		exit;
	}

	# Check to see that the URL is valid
	
	$CheckURL = substr ($in{'URL'}, 0, 7);
	if ($CheckURL ne "http://") {
		&PrintHeader;
		print "<html>\n <head>\n <title>Error:  URL Invalid</title>\n </head>\n";
		print "$Header\n";
		print "The URL you entered seems to be invalid.  It should start with \"<b>http://</b>\".  Please go back and check your url.<p>\n";
		print "Processed by \"WebSearch v1.0 WWW Search Engine\" by <a 
href=\"http://www.virtualcenter.com/scripts2\">Michael Hall</a> and <a 
href=\"http://www.virtualcenter.com/vmt\">Virtual Marketing Technologies</a>.<br>\n";
		print "Copyright © 1996 - All Rights Reserved.<br>\n";
		print "$Footer\n";
		exit;
	}
	
	# Now check to see if the description is too long
	
	@DescArray = split (/ /, $in{'Description'});
	$ArrayCount = @DescArray;
	if ($ArrayCount > $MaxWords) {
		&PrintHeader;
		print "<html>\n <head>\n <title>Error:  Description Too Long</title>\n </head>\n";
		print "$Header\n";
		print "Your description contains $ArrayCount words.  
The maximum number of words it can contain is $MaxWords.  
Please go back and edit your description.<p>\n";
		print "Processed by \"WebSearch v1.0 WWW Search Engine\" by <a 
href=\"http://www.virtualcenter.com/scripts2\">Michael Hall</a> and <a 
href=\"http://www.virtualcenter.com/vmt\">Virtual Marketing Technologies</a>.<br>\n";
		print "Copyright © 1996 - All Rights Reserved.<br>\n";
		print "$Footer\n";
		exit;
	}
	
	# Now write the information to the appropriate file
	
    $in{'Description'} =~ s/\n/ /go;

	if (-e "$BaseDir/$in{'Category'}.dta") {
		open (DB, ">>$BaseDir/$in{'Category'}.dta");
		print DB "$in{'Title'}&&$in{'URL'}&&$in{'Description'}\n";
		close DB;
	}
	else {
		open (DB, ">$BaseDir/$in{'Category'}.dta");
		print DB "$in{'Title'}&&$in{'URL'}&&$in{'Description'}\n";
		close DB;
	}
	
	# Now present the information to the user
	
	&PrintHeader;
	print "<html>\n <head>\n <title>URL Added</title>\n </head>\n";
	print "$Header\n";
	print "Thank you for your information.  Your URL has been added to the system and can now be searched from the search page.<p>\n";
		print "Processed by \"WebSearch v1.0 WWW Search Engine\" by <a 
href=\"http://www.virtualcenter.com/scripts2\">Michael Hall</a> and <a 
href=\"http://www.virtualcenter.com/vmt\">Virtual Marketing Technologies</a>.<br>\n";
		print "Copyright © 1996 - All Rights Reserved.<br>\n";
	print "$Footer\n";
	exit;
}

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

	# Search the databases for any web sites that match the keywords entered
	
	$Counter = 0;
	foreach $Category (@Categories) {
		if (-e "$BaseDir/$Category.dta") {
			open (DB, "$BaseDir/$Category.dta");
			$FileFlag = 0;
			@KeyData = <DB>;
			foreach $TempLine (@KeyData) {
				@Keywords = split (/ /, $in{'Keywords'});
				foreach $Keyword (@Keywords) {
					if (($TempLine =~ /$Keyword/i) && ($MatchFlag2 == 0)) {
							$MatchFlag = 1;
							$MatchFlag2 = 1;
							$URL[$Counter] = $TempLine;
							$Counter++;
					}
					
				}
				$MatchFlag2 = 0;
			}
			$FileFlag = 0;
			$MatchFlag2 = 0;
		
		}
	}
	if ($MatchFlag == 0) {
		# No matches were found
		&PrintHeader;
		print "<html>\n <head>\n <title>No Matches Found</title>\n </head>\n";
		print "$Header\n";
		print "No entries were found that matched your keywords.  Please go back and try again.<p>\n";
		print "Processed by \"WebSearch v1.0 WWW Search Engine\" by <a 
href=\"http://www.virtualcenter.com/scripts2\">Michael Hall</a> and <a 
href=\"http://www.virtualcenter.com/vmt\">Virtual Marketing Technologies</a>.<br>\n";
		print "Copyright © 1996 - All Rights Reserved.<br>\n";
		print "$Footer\n";
		exit;
	}
	else {
		&PrintHeader;
		print "<html>\n <head>\n <title>Matches</title>\n </head>\n";
		print "$Header\n";
		print "The following entries matched your request:<p>\n";
		print "<table border=2>\n";
		print "<tr><td align=center><b>Title:</b></td><td align=center><b>Description:</b></td></tr>\n";
		foreach $Match (@URL) {
			@TempData = split (/&&/, $Match);
			print "<tr><td><a href=\"$TempData[1]\">$TempData[0]</a></td><td>$TempData[2]</td></tr>\n";
		}
		print "</table><p>\n";
		print "Processed by \"WebSearch v1.0 WWW Search Engine\" by <a 
href=\"http://www.virtualcenter.com/scripts2\">Michael Hall</a> and <a 
href=\"http://www.virtualcenter.com/vmt\">Virtual Marketing Technologies</a>.<br>\n";
		print "Copyright © 1996 - All Rights Reserved.<br>\n";
		print "$Footer\n";
		exit;
	}
}
		
		





#######################
# Parse Form Subroutine

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

      $in{$name} = $value;
   }

}


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

sub PrintHeader {

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