www.pudn.com > search-cgi_for_nt.zip > search.pl
#!/usr/bin/perl
#######以下内容可根据需要修
$basedir = 'd:/inetpub/search/'; #需搜索的网站目录,要写上绝对路径
$baseurl = 'http://www.myweb.com/'; #网站的域名及目录路径
@files = ('*.htm','*.html'); #需搜索文件的类型
$title = "SRC FOR NT 搜索器;作者:莫剑斌(mjb)";
$title_url = 'http://www.zhongshan.com/index.html';
$search_url = 'http://www.myweb.com/search.html';
$nextcgi = 'http://www.myweb.com/cgi-bin/next.pl'; #下一页显示CGI程序的文件名和路径
# Done #
################################################
$docrootlen = length($basedir);
# Parse Form Search Information
&parse_form;
# Get Files To Search Through
&get_files;
# Search the files
&search;
# Print Results of Search
&return_html;
sub parse_form {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
}
sub get_files {
chdir($basedir);
foreach $file (@files) {
# $ls = `ls $file`;
$ls = `dir /b/s $file`;
@ls = split(/\s+/,$ls);
foreach $temp_file (@ls) {
if (-d $file) {
$filename = "$file$temp_file";
if (-T $filename) {
push(@FILES,$filename);
}
}
elsif (-T $temp_file) {
push(@FILES,$temp_file);
}
}
}
}
sub search {
@terms = split(/\s+/, $FORM{'terms'});
foreach $FILE (@FILES) {
open(FILE,"$FILE");
@LINES = ;
close(FILE);
$string = join(' ',@LINES);
$string =~ s/\n//g;
if ($FORM{'boolean'} eq 'AND') {
foreach $term (@terms) {
if ($FORM{'case'} eq 'Insensitive') {
if (!($string =~ /$term/i)) {
$include{$FILE} = 'no';
last;
}
else {
$include{$FILE} = 'yes';
}
}
elsif ($FORM{'case'} eq 'Sensitive') {
if (!($string =~ /$term/)) {
$include{$FILE} = 'no';
last;
}
else {
$include{$FILE} = 'yes';
}
}
}
}
elsif ($FORM{'boolean'} eq 'OR') {
foreach $term (@terms) {
if ($FORM{'case'} eq 'Insensitive') {
if ($string =~ /$term/i) {
$include{$FILE} = 'yes';
last;
}
else {
$include{$FILE} = 'no';
}
}
elsif ($FORM{'case'} eq 'Sensitive') {
if ($string =~ /$term/) {
$include{$FILE} = 'yes';
last;
}
else {
$include{$FILE} = 'no';
}
}
}
}
if ($string =~ /(.*)<\/title>/i) {
$titles{$FILE} = "$1";
}
else {
#Nt专用,清除文件前的目录
$showfile=substr($FILE,$docrootlen);
$showfile =~ s/\\/\//g;
$titles{$FILE} = "$showfile";
}
if ($string =~ //i)
{
@cut = split(/\">/,$1);
$description{$FILE} = $cut[0];
}
else
{
$description{$FILE}= "...";
}
}
}
sub return_html {
print "Content-type: text/html\n\n";
print "\n \n 站内搜索 \n \n";
print " \n \n $title
\n \n";
print "以下是你的搜索结果:
\n";
$hitcount=0;
foreach $key (keys %include) {
if ($include{$key} eq 'yes') {
$hitcount=$hitcount+1;
}
}
#显示查到的文件数
$docstring = "找到$hitcount个文件。";
$docstring = "找到了一个文件。" if ($hitcount == 1);
$docstring = "找不到一个文件。" unless ($hitcount);
print "$docstring
\n";
#显示查到的内容
print "
\n";
$lc=0;
foreach $key (keys %include) {
if ($include{$key} eq 'yes') {
#Nt专用,清除文件前的目录
$showfile=substr($key,$docrootlen);
$showfile =~ s/\\/\//g;
print "- $titles{$key}
$description{$key}
$baseurl$showfile\n";
$lc=$lc+1;
if ($lc >= $FORM{'showcount'})
{
goto showover;
}
}
}
showover:
print "
\n";
if ($hitcount > $lc)
{
print "\n";
}
print "
\n";
print "Search Information:\n";
print "
\n";
print "- Terms: ";
$i = 0;
foreach $term (@terms) {
print "$term";
$i++;
if (!($i == @terms)) {
print ", ";
}
}
print "\n";
print "
- Boolean Used: $FORM{'boolean'}\n";
print "
- Case $FORM{'case'}\n";
print "
\n";
print "
\n- Back to Search Page\n";
print "
- $title\n";
print "
\n";
print "
\n";
print "\n\n";
}