#!/usr/bin/perl
#######################################
#######################################
@dirs=('lists/'); ###
$base_dir="/www/htdocs"; ###
$base_ssi=""; ###
$base_url="http://dom.com/"; ###
@searchtypes=(''); ###
$subdirs="yes"; ###
$coef=100; ###
#######################################
#######################################
$buffer = "$ENV{'QUERY_STRING'}";
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
if ($name eq "words") { $firstwords=$value; }
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ s/<!--(.|\n)*-->//g;
$name =~ s/<([^>]|\n)*>//g;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
$value =~ s/<([^>]|\n)*>//g;
$value =~ s/\.//g;
$value =~ s/\://g;
$value =~ s/\,//g;
$value =~ s/\?//g;
$value =~ s/\!//g;
$value =~ s/\&//g;
$value =~ s/\|//g;
$value =~ s/\"//g;
$value =~ tr/ / /s;
$FORM{$name} = $value;
}
$words=$FORM{'words'};
$bigeqsmall=$FORM{'bigeqsmall'};
$whatsearch=$FORM{'whatsearch'};
$logics=$FORM{'logics'};
$findstr=0;
$page=$FORM{'page'};
$pcoef=$FORM{'pcoef'};
if ($logics eq "and") { @word=$words; }
else { @word=split(/ /,$words); }
if ($page eq '') { $page=1; }
if ($whatsearch eq "yes") {
$i=0;
foreach $str (@word) {
$str="$str ";
$word[$i]=$str;
$i++;
}
}
else { $whatsearch="no"; }
foreach $type (@dirs) {
chomp($type);
$dir="$base_dir/$type";
chdir($dir);
opendir(CURDIR, $dir) || &cgiError ("Opening dirs '$dir' failed:", "$!");
@allfiles=readdir(CURDIR);
$totalnum=@allfiles;
closedir(CURDIR);
for($i=1; $i<$totalnum; $i++) {
if (-d $allfiles[$i]) {
if ($allfiles[$i] ne '.' && $allfiles[$i] ne ".." && $type ne '' && $subdirs eq "yes") {
push(@dirs,"$type$allfiles[$i]/");
}
}
if (-f $allfiles[$i]) {
($file,$ext) = split(/\./, $allfiles[$i]);
$ext =~ tr/A-Z/a-z/;
foreach $searchtype (@searchtypes) {
if ($ext =~ /$searchtype/i) {
$search="0";
$temp="0";
$normal_string="0";
open (SRH,"$allfiles[$i]") || &cgiError ("Reading file '$allfiles[$i]' failed:", "$!");
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)=stat($allfiles[$i]);
read(SRH,$search,$size);
close(SRH);
$search =~ s/\n/ /g;
if ($search =~ /<title>(.*)<\/title>/i) { $title="$1"; }
else { $title="$allfiles[$i]"; }
$search =~ s/<([^>]|\n)*>//g;
$search =~ s/&([^;])*;//g;
$search =~ s/<!--([^-->])*-->//g;
$search =~ tr/ / /s;
$normal_string=$search;
$search =~ s/\./ /g;
$search =~ s/\:/ /g;
$search =~ s/\,/ /g;
$search =~ s/\?/ /g;
$search =~ s/\!/ /g;
if ($bigeqsmall eq "yes") {
$search =~ tr/А-Я/а-я/;
$search =~ tr/A-Z/a-z/;
}
$doublesearch=$search;
foreach $str (@word) {
$search=$doublesearch;
if ($bigeqsmall eq "yes") {
$str =~ tr/А-Я/а-я/;
$str =~ tr/A-Z/a-z/;
}
if ($search =~ /$str/i) {
$allsymbols=0;
$lensword=length($str);
while ($search ne '') {
($temp,$search)=split(/$str/,$search,2);
$allsymbols=$allsymbols+length($temp);
if ($allsymbols < $coef) {
$rstart=substr($normal_string,0,$allsymbols);
}
else {
$rstart=substr($normal_string,$allsymbols-$coef,$coef);
}
$rword=substr($normal_string,$allsymbols,$lensword);
$rend=substr($normal_string,$allsymbols+$lensword,$coef);
$allsymbols=$allsymbols+$lensword;
if ($search ne '') {
$findstr++;
$allsymbolsr=substr("00000000",length($allsymbols),8-length($allsymbols));
$allsymbolsr="$allsymbolsr$allsymbols";
$isize=int($size/1024);
$allstrfile{"$allfiles[$i]\&$allsymbolsr"}=<< "[END]";
<p class="lr"><u>header:</u> $title<br><u>URL:</u> <a href=\"$base_url$type$allfiles[$i]\"
target=\"_blank\">$base_url$type$allfiles[$i]</a><br><u>size:</u> $isize Кб<br>
<i> ---> $rstart<b>$rword</b>$rend <--- </i></p>
[END]
}
}
}
}
}
}
}
}
}
&print_head;
print "<p><br><u>search:</u> <b>$words</b><br>";
if ($bigeqsmall eq "yes") {
print "<u>with no register var=no.</u><br>";
}
if ($whatsearch eq "yes") {
print "<u>compleatly var=yes.</u><br>";
}
if ($logics eq "and") {
print "<u>logic <b>and</b>.</u><br>";
}
else {
print "<u>logic <b>or</b>.</u><br>";
}
print "</p><hr width=\"100%\">";
if ($findstr eq 0) {
print "<p align=center><br><font size=4><b>File does not exist.</b></font></p><p> </p>";
}
else {
print "<p><u>result:</u> <b>$findstr</b> shtuk =).</p>";
}
if ($pcoef ne 100 && $findstr>$pcoef) {
$fr=0;
foreach $key (sort (keys %allstrfile)) {
if (($fr<$pcoef*$page) && ($fr>=$pcoef*($page-1))) { print "$allstrfile{$key}\n"; }
$fr++;
}
print "<p><font size=\"2\"><u>page:</u> ";
for($i=1; $i<($findstr/$pcoef)+1; $i++) {
if ($i ne $page) {
print "<a href=\"/cgi-bin/search.pl?words=$firstwords&bigeqsmall=$bigeqsmall&whatsearch=$whatsearch&pcoef=$pcoef&logics=$FO
}
else {
print "<b>$i</b> ";
}
}
print "</font></p>";
}
else {
foreach $key (sort (keys %allstrfile)) {
print "$allstrfile{$key}";
}
}
&print_end;
exit;
sub cgiError {
my ($error_cause,$error) = @_;
if ($error_cause eq "") { $error_cause = "Error:"; }
if ($error eq "") { $error = "The script encountered problems and terminated"; }
&print_head;
print "<br><br><p align=center><font size=3><b>$error_cause $error</b></font></p><p> </p>";
&print_end;
exit;
}
sub print_head {
chdir($base_ssi);
open(DB,"head.txt");
@totals=<DB>;
close(DB);
print "Content-type: text/html\n\n";
print @totals;
}
sub print_end {
open(DB,"end.txt");
@totals=<DB>;
close(DB);
print @totals;
}
--index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Search</title>
<!--
-->
<body>
<body text="#000000" bgcolor="#FFFFFF" link="#000080" vlink="#008000" alink="#000080">
<div align="center"><center>
<form action="/cgi-bin/search.pl" method="GET">
<table border="1" cellpadding="1" cellspacing="1" width="39%">
<tr>
<td>
<input type="text" name="words" size="39%" class="forms"</td>
<td align="right"><input type="submit" value=" search " class="forms"></td>
</tr>
<td>
<input type="checkbox" name="bigeqsmall" value="yes" checked>with no register</td>
<td>
<input type="checkbox" name="whatsearch" value="yes">completely</td>
<tr>
<td align="left">logic
<input type="radio" value="or" name="logics" checked>OR
<input type="radio" value="and" name="logics">AND</td>
<td align="left">result
<input type="radio" value="10" name="pcoef"checked>10
<input type="radio" value="20" name="pcoef">20
<input type="radio" value="100" name="pcoef">all</td>
</tr>
</table>
</form>
</div>
</center>
<center>
</body>
</html>