www.pudn.com > search-cgi_for_nt.zip > next.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 =~ /<meta\s+name="description"\s+content="(.*)>/i) 
	{ 
	@cut = split(/\">/,$1); 
	$description{$FILE} = $cut[0]; 
	} 
    else 
	{ 
	$description{$FILE}= "..."; 
	} 
 
   } 
} 
       
sub return_html { 
   print "Content-type: text/html\n\n"; 
   print "<html>\n <head>\n  <title>今日中山---站内搜索\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"; if ($hitcount > $lc) { print "
\n"; if (($hitcount-$lc) > $FORM{'showcount'}) { $nextshow = $FORM{'showcount'}; } else { $nextshow = ($hitcount-$lc); } print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
\n"; } print "
\n"; print "Search Information:

\n"; print "



\n"; print "

\n"; print "
\n"; print "\n\n"; }