#!/usr/bin/perl
use CGI;
$query = new CGI;
$|= 1;
print $query->header();
timeout_problem_fix();
initBegin();
if ($f ne '') { printFILE($f); exit; }
doSearchNOW(); exit;
sub initBegin {
$start = $query->param('start');
if ($start eq ''){$start='0';}
require './search_paging.pl';
require './search_setup.pl';
require './search_libs.pl';
$search = $query->param('search');
$allFiles = $query->param('files');
$ExactPhrase = $query->param('exact_phrase');
$f = $query->param('s');
}
sub doSearchNOW {
my ($resultHEADER,$resultFOOTER) = split('%%body%%',doReplaces(join('',readFile('./search_result.html'))));
print $resultHEADER;
my $testSEARCH = $search;
foreach my $kk (@ExceptWords){ $testSEARCH =~ s/\b$kk\b/$1/gi; }
if ($start eq ''){$start='0';}
if (($testSEARCH eq '')||($testSEARCH =~ /^\W{0,}$/)){
$ExactPhrase='CHECKED';
if ($testSEARCH ne $search){
$search_Result='All keywords are in the forbidden-words-list';
}else{
$search_Result='No Search Phrase';
}
}else{
$countRES = 0;
$item_Template = join("\n",readFile('./search_template.html'));
foreach my $sp (keys %search_paths){searchInFiles($sp,$search_paths{$sp});}
$search_Result = outputPaging($countRES);
if ($countRES eq '0'){$search_Result='No Items Found';}
}
$search_Templ = join("\n",readFile('./search_result.html'));
$resultFOOTER =~ s/%%PAGING%%/$search_Result/gi;
print $resultFOOTER;
}
sub searchInFiles {
my ($serv_search_path,$web_search_path) = (@_);
my @files = ();
my $excl_files = ',' . join(',',@excluded_files) . ',';
my $xt1;
my $fileHTML = '';
my $fileTEXT = '';
my $KeyWords = fixKeyword($search);
my $partFile = '';
foreach $xt1 (@search_ext){
push @files,FilesInFolder($serv_search_path,$xt1);
}
foreach $xt1 (@files){
if (index($excl_files,",$xt1,") eq '-1') {
#print $serv_search_path.'/'.$xt1 . "
\n";
$fileHTML = join("\n",readFile($serv_search_path.'/'.$xt1));
$fsize = (-s $serv_search_path.$xt1);
$fsize = sprintf('%.3f',$fsize/1024);
$fileTEXT = removeHTML($fileHTML);
if ($fileTEXT =~ /$KeyWords/mgi){
if (($countRES >= $start)&&($countRES < ($start + $per_page))) {
my $filePRED = $`;
my $fileFOUND= $&;
my $fileNEXT = $';
my $fileCutPred='';
$filePRED = reverse($filePRED);
if ($filePRED =~ m/(([\w\d]+[\W\s]{1,}){5})/){
$fileCutPred = reverse($1);
$filePRED = reverse($filePRED);
$fileCutPred = (split(/\n/,$fileCutPred))[-1];
$filePRED =~ s/(\+|\||\$|\-|\(|\))/\\$1/gi;
if ($fileCutPred !~ /(\s+)?$filePRED(\s+)?/){$fileCutPred = '...' . $fileCutPred;}
}else{
$fileCutPred = reverse($filePRED);
$fileCutPred = (split(/\n/,$fileCutPred))[-1];
if ($fileCutPred !~ /(\s+)?$filePRED(\s+)?/){$fileCutPred = '...' . $fileCutPred;}
}
$fileTEXT = $fileCutPred . ' ' . $fileFOUND . $fileNEXT;
$fileTEXT =~ s/\s+/ /g;
$partFile = substr($fileTEXT,0,$bodyMaxSize);
$partFile = substr($partFile,0,rindex($partFile, ' ')).'...';
@keyWords = split(/ /,$search);
$partANCHOR = $web_search_path.$xt1;
if ($ExactPhrase ne ''){
if ($KeywordLinks ne ''){
my $kw1 = $KeyWords;
$kw1 =~ s/\\W\+\?/\_/g;
$kw1 = codeIT("$serv_search_path$xt1|$kw1|$web_search_path$xt1")."#$kw1";
$partANCHOR = "search.cgi?s=$kw1";
$kw1 = qq~~;
$partFile =~ s/($KeyWords)/$kw1$1<\/FONT><\/B><\/A>/gi
}else{$partFile =~ s/($KeyWords)/$1<\/B>/gi;}
}else{
foreach my $zz (@keyWords) {$partFile =~ s/($zz)/$1<\/B>/gi;}
}
if ($fileHTML=~ /(.*)<\/TITLE>/gi){$pageTITLE = $1;}else{$pageTITLE = $xt1}
my $tit = $item_Template;
my $myCOUNTER = $countRES+1;
$tit=~ s/&COUNTER&/$myCOUNTER/g;
$tit=~ s/&TITLE&/$pageTITLE/g;
$tit=~ s/&SIZE&/$fsize/g;
$tit=~ s/&URL&/$web_search_path$xt1/g;
$tit=~ s/&PART&/$partFile/g;
$tit=~ s/&ANCHOR&/$partANCHOR/g;
print $tit;
}
$countRES++;
}
}
}
}
sub doReplaces {
my $st = shift(@_);
$st =~ s/%%search%%/$search/gi;
$st =~ s/%%homeurl%%/$homeurl/gi;
$st =~ s/%%ExactPhrase%%/$ExactPhrase/gi;
return $st;
}
sub fixKeyword {
my $KeyWords = shift(@_);
$KeyWords =~ s/\W+/ /gi;
if (($KeyWords =~ /^\s+?\".*\"\s+?$/)||($ExactPhrase ne '')){
$KeyWords =~ s/\s+/\\W\+\?/g;
}else{
$KeyWords =~ s/ AND / /gi;
$KeyWords =~ s/ /\.\*/g;
}
return $KeyWords;
}
sub codeIT {
my $st = shift(@_);
$st =~ s/\./~/g;
$st =~ s/\//./g;
$st =~ s/\#/^/g;
$st =~ s/html/*/g;
$st =~s/(.)/(sprintf('%2x',ord($1)))/ge;
$st =~ tr/0123456789/0XKI4ZR78Y/;
$st = reverse(uc($st));
return $st;
}
sub DEcodeIT {
my $st = shift(@_);
$st = reverse(uc($st));
$st =~ tr/0XKI4ZR78Y/0123456789/;
$st =~s/(..)/chr(hex($1))/ge;
$st =~ s/\*/html/g;
$st =~ s/\./\//g;
$st =~ s/\~/\./g;
$st =~ s/\^/#/g;
return $st;
}
####################################
#@=getFilesInFolder(folder,filter);
####################################
sub FilesInFolder{
my $folder = shift(@_);
my $filter = shift(@_);
$filter =~ s/\./\\./gi;
$filter =~ s/\*/\.\*/gi;
my @lines;
opendir(DIR, "$folder") || return '';
@lines = grep {$_ =~ /^$filter$/i} readdir(DIR);
closedir DIR;
return @lines;
}
############################
# @ = readFile(fileName)
############################
sub readFile {
my $filename = shift(@_);
my @fileArr = ();
open (srcFile, $filename);
@fileArr = ;
close (srcFile);
return @fileArr;
}
sub fixKeywordHTML {
my $KeyWords = shift(@_);
$KeyWords =~ s/\s+//g;
$KeyWords =~ s/(\w)/$1\\W{0,}/g;
return $KeyWords;
}
sub printFILE {
my $reff = $query->referer();
my $selfu = $query->self_url();
if (($selfu =~ /$reff\?/i)&&($reff ne '')){
my $ff = shift(@_);
my ($file,$srch,$URL)=split(/\|/,DEcodeIT($ff));
my $sORG=$srch;
$srch =~ s/_/ /g;
$srch = fixKeywordHTML($srch);
my $DATA = join('',readFile($file));
my $RDATA = search_prepare_inhtml($DATA);
if ($RDATA =~ /$srch/gi){
my $len = length($`);
$DATA = substr($DATA,0,$len) . "" . substr($DATA,$len,length($DATA));
} else { }
print qq~\n\n\n~;
print $DATA;
} else {
print '
Please use the site search to access this page.
';
return;
}
}
sub timeout_problem_fix {
eval {
$SIG{ALRM} = sub { print "
Time Out
"; die "alarm\n" }; # NB: \n required
alarm 10;
};
if ($@) {
die unless $@ eq "alarm\n"; # propagate unexpected errors
# timed out
} else {
# didn't
}
}