#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); # # csSearchPro - 061402 $basepath = './'; # 3.9 Multiple search terms with 'AND' fixed # 3.8 Duplicate search results fixed # 3.75 Changes # misc minor fixes # 3.6 Changes # -- Added ability to manually increase keywords with # # 3.5 Changes # -- Searches for multiple SSI methods when option is selected # 3.3 Changes # -- ability to use # -- partial searces or full word searches.. # -- ability for server side includes in the templates. # 3.0 Changes # -- Ability to log search terms # -- Improved Indexing ##################################################################### # # # Copyright © 1999-2001 CGISCRIPT.NET - All Rights Reserved # # # ##################################################################### # # # THIS COPYRIGHT INFORMATION MUST REMAIN INTACT # # AND MAY NOT BE MODIFIED IN ANY WAY # # # ##################################################################### # # When you downloaded this script you agreed to accept the terms # of this Agreement. This Agreement is a legal contract, which # specifies the terms of the license and warranty limitation between # you and CGISCRIPT.NET. You should carefully read the following # terms and conditions before installing or using this software. # Unless you have a different license agreement obtained from # CGISCRIPT.NET, installation or use of this software indicates # your acceptance of the license and warranty limitation terms # contained in this Agreement. If you do not agree to the terms of this # Agreement, promptly delete and destroy all copies of the Software. # # Versions of the Software # Only one copy of the registered version of CGISCRIPT.NET # may used on one web site. # # License to Redistribute # Distributing the software and/or documentation with other products # (commercial or otherwise) or by other than electronic means without # CGISCRIPT.NET's prior written permission is forbidden. # All rights to the CGISCRIPT.NET software and documentation not expressly # granted under this Agreement are reserved to CGISCRIPT.NET. # # Disclaimer of Warranty # THIS SOFTWARE AND ACCOMPANYING DOCUMENTATION ARE PROVIDED "AS IS" AND # WITHOUT WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR ANY OTHER # WARRANTIES WHETHER EXPRESSED OR IMPLIED. BECAUSE OF THE VARIOUS HARDWARE # AND SOFTWARE ENVIRONMENTS INTO WHICH CGISCRIPT.NET MAY BE USED, NO WARRANTY # OF FITNESS FOR A PARTICULAR PURPOSE IS OFFERED. THE USER MUST ASSUME THE # ENTIRE RISK OF USING THIS PROGRAM. ANY LIABILITY OF CGISCRIPT.NET WILL BE # LIMITED EXCLUSIVELY TO PRODUCT REPLACEMENT OR REFUND OF PURCHASE PRICE. # IN NO CASE SHALL CGISCRIPT.NET BE LIABLE FOR ANY INCIDENTAL, SPECIAL OR # CONSEQUENTIAL DAMAGES OR LOSS, INCLUDING, WITHOUT LIMITATION, LOST PROFITS # OR THE INABILITY TO USE EQUIPMENT OR ACCESS DATA, WHETHER SUCH DAMAGES ARE # BASED UPON A BREACH OF EXPRESS OR IMPLIED WARRANTIES, BREACH OF CONTRACT, # NEGLIGENCE, STRICT TORT, OR ANY OTHER LEGAL THEORY. THIS IS TRUE EVEN IF # CGISCRIPT.NET IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. IN NO CASE WILL # CGISCRIPT.NET' LIABILITY EXCEED THE AMOUNT OF THE LICENSE FEE ACTUALLY PAID # BY LICENSEE TO CGISCRIPT.NET. # # Credits: # Andy Angrick - Programmer - angrick@cgiscript.net # Mike Barone - Design - mbarone@cgiscript.net # # For information about this script or other scripts see # http://www.cgiscript.net # # Thank you for trying out our script. # If you have any suggestions or ideas for a new innovative script # please direct them to suggest@cgiscript.net. Thanks. # ######################################################################## # Config Variables # ######################################################################## ##################################################################### # # # Configuration variables # # # ##################################################################### (! -e "$basepath/setup.cgi")?($nosetup=1):(require("$basepath/setup.cgi")); (!$htmlpath)&&($htmlpath=$cgipath); (!$htmlurl)&&($htmlurl=$cgiurl); $in{'htmlurl'} = $htmlurl; $in{'cgiurl'} = $cgiurl."/csSearchPro.cgi"; $flock=1; $in{'cinfo'} = qq|

Powered by csSearchPro - © 2000,2001 CGIScript.net |; ##################################################################### # # # End Configuration Variables. # # # ##################################################################### require("$basepath/libs.cgi"); $| = 1; &main; exit; sub main{ print "Content-type: text/html\n\n"; ($ARGV[0] eq 'index')&&($ARGV[1] eq $username)&&(&CreateIndexStart); &getdata(); $in{'terms'} =~ s/([\\\|\(\)\[\]\{\}\^\$\*\+\?\.])/\\$1/g; ($in{'command'} eq '')&&($nosetup)&&(&Setup); ($in{'command'} eq '')&&(!$nosetup)&&(&ShowSearchForm); ($in{'command'} eq 'login')&&(&Login); ($in{'command'} eq 'query')&&(&Query); ($in{'command'} eq 'info')&&(&Info); ($in{'command'} eq 'savesetup')&&(($nosetup)?(&SaveSetup):(&PError("Error. Permission denied."))); #all require password below &GetLogin; ($in{'command'} eq 'manage')&&(&Manage); ($in{'command'} eq 'aae')&&(&AddExtExcludes); ($in{'command'} eq 'ade')&&(&AddDirectoryExcludes); ($in{'command'} eq 'ae')&&(&AddIgnore); ($in{'command'} eq 'as')&&(&AddSkip); ($in{'command'} eq 'gu')&&(&GoUp); ($in{'action'} eq '-=Skip/UnSkip All Selected=-')&&(&SkipSelected); ($in{'action'} eq '-=Ignore/UnIgnore All Selected=-')&&(&IgnoreSelected); ($in{'command'} eq "showadv")&&(&ShowAdv); ($in{'command'} eq "setstyles")&&(&SetStyles); ($in{'command'} eq "showcolor")&&(&ShowColor); ($in{'command'} eq "setcolor")&&(&SetColor); ($in{'command'} eq "index")&&(&CreateIndexStart); ($in{'command'} eq "clearlog")&&(&ClearLog); ($in{'command'} eq "viewlogs")&&(&ViewQueryLog); exit; } sub ShowAdv{ &GetAdvSettings; $in{'S'.$in{'style'}}='checked'; (!$in{'extinc'})?($in{'EXTY'}='checked'):($in{'EXT'.$in{'extinc'}}='checked'); $in{'directlinks'} = qq| |; $in{'directlinksnojs'} = qq| |; &PageOut("$htmlpath/t_search_advanced_settings.htm"); exit; } sub GetAdvSettings{ (-e "$htmlpath/styles.pl")&&(require "$htmlpath/styles.pl"); } sub ShowColor{ $in{'c'.$in{'cc'}} = checked; &PageOut("$htmlpath/color_selector.htm"); exit; } sub SetColor{ open(ADV,"<$htmlpath/styles.pl"); open(TMP,">$htmlpath/styles.pl.tmp"); while(){ (!/'\$in{'$in{'fieldname'}'}'/)&&(!/1;/)&&(print TMP $_); } $in{'colorselect'} =~ s/\\//g; $in{'colorselect'} =~ s/\"//g; print TMP "\$in{'$in{'fieldname'}'} = \"$in{'colorselect'}\";\n1;\n"; close ADV; close TMP; @fi = stat("styles.pl.tmp"); rename("styles.pl.tmp","styles.pl") unless ($fi[7] < 1); print <<"EOF"; EOF exit; } sub SetStyles{ open(STYLES,">$htmlpath/styles.pl")||die print "$!: styles.pl"; foreach $i (keys(%in)){ next if (($i eq 'command')||($i eq 'cgiurl')); next if (($i eq 'UserName')||($i eq 'Password')); next if (($i eq 'basemanageurl')||($i eq 'cinfo')); next if (($i eq 'database')||($i eq 'imagedir')); next if (($i eq 'imagerealdir')||($i eq 'images2')); next if (($i eq 'format')||($i eq 'managementname')); next if (($i eq 'managementuser')||($i eq 'managementemail')); next if (($i eq 'exportdir')||($i eq 'managementemail')); next if (($i eq 'htmlurl')||($i eq 'cgiurl')); next if ($i eq 'terms'); $in{$i} =~ s/'//g; print STYLES "\$in{'$i'}=\'$in{$i}\';\n"; } print STYLES "1;\n"; close STYLES; print<<"EOF"; EOF exit; } sub Login{ &PageOut("$htmlpath/t_login.htm"); exit; } sub GetLogin{ &GetCookies; $in{'UserName'} = $cookie{'UserName'}; $in{'PassWord'} = $cookie{'PassWord'}; if(!$in{'UserName'}){ &PageOut("$htmlpath/t_login.htm"); exit; } else{ (($in{'UserName'} ne $username)||(($in{'PassWord'} ne $password)))&&(&PError("Error. Invalid username or password")); } } sub QueryI{ &GetAdvSettings; if($in{'parsessi'}){ require("$cgipath/ssi.cgi"); } (!$in{'page'})&&($in{'page'} = 1); $start = ($in{'page'}*$in{'entriesperpage'})-$in{'entriesperpage'}; $end = $start + $in{'entriesperpage'}; (!$in{'terms'})&&(&PError("Error. Please enter something to search for.")); &SearchByIndex; (!$in{'count'})&&($in{'count'} = '0'); (!$in{'searchresults'})&&($in{'searchresults'} = '
No se han encontrado resultados para su consulta.'); $in{$in{'mcase'}} = 'selected'; $in{$in{'mbool'}} = 'selected'; ($count)&&(&GetNlinks); $in{'count'} = $count; $in{'count'} = "Su busqueda retornó $in{'count'} resultados."; $in{'terms'} =~ s/\\//g; ($in{'template'})?(&InsertPage($in{'template'},"$htmlpath/t_searchresults.htm")):(&PageOut("$htmlpath/t_searchresults.htm")); exit; } sub Query{ &GetAdvSettings; &LogQuery; ($in{'useindex'})&&(&QueryI); if($in{'parsessi'}){ require("$cgipath/ssi.cgi"); } (!$in{'page'})&&($in{'page'} = 1); $start = ($in{'page'}*$in{'entriesperpage'})-$in{'entriesperpage'}; $end = $start + $in{'entriesperpage'}; (!$in{'terms'})&&(&PError("Error. Please enter something to search for.")); &LoadVars; &GetGlobalIgnore; chdir($rootpath); &GetFiles("."); ##start actual search foreach $i (sort keys %ftsearch){ &SearchFile($i); } (!$in{'count'})&&($in{'count'} = '0'); (!$in{'searchresults'})&&($in{'searchresults'} = '
No se han obtenido resultados de su búsqueda'); $in{$in{'mcase'}} = 'selected'; $in{$in{'mbool'}} = 'selected'; ($count)&&(&GetNlinks); $in{'count'} = $count; $in{'count'} = "Su busqueda retornó $in{'count'} resultados."; ($in{'template'})?(&InsertPage($in{'template'},"$htmlpath/t_searchresults.htm")):(&PageOut("$htmlpath/t_searchresults.htm")); exit; } sub CreateIndexStart{ &GetAdvSettings; &GetGlobalIgnore; $cid=0; $pid=0; &LoadVars; if($in{'parsessi'}){ require("$cgipath/ssi.cgi"); } print "

Indexando archivos.. por favor paciencia\n";

chdir($rootpath);
undef $/;

&GetFiles(".");

foreach $i (keys %ftsearch){
  &CreateIndex($i);
  }

#uninit
open(IDX,">$htmlpath/site.idx");
foreach my $i (keys %site){
print IDX "$i:$site{$i}\n";
}
close IDX;

open(IDX,">$htmlpath/page.idx");
foreach my $i (keys %page){
print IDX "$page{$i}:$i\n";
}
close IDX;

dbmclose %site;
dbmclose %page;

close DB;

print <<"EOF";

EOF
exit;
}


sub CreateIndex{
local($file) = @_;
$cg=0;
undef @words;
print "Indexing file $file =->";
open(FILE,"<$rootpath/$file")||print "$!:$rootpath/$file
"; $data = ; close FILE; if($in{'parsessi'}){ &CheckForSSI; } &FormatPage; #index the page if(!$page{$file}){ $pid++; $page{$file} = $pid; $cid = $pid; } else{ $cid = $page{$file}; } my @words = split(/\s/,$data); foreach my $m (@words){ next if (!$m); $m =~ s/[\,\.\/\!\@\#\$\%\^\&\*\(\)\_\=\+\{\}\[\]\|\\\"\:\;\?\/\>\<\~\`]//g; next if (length("$m") < 2); $mw{$m}++; } foreach my $k (keys %mw){ $cg++; ($site{$k})?($site{$k} .= "\t$cid-$mw{$k}"):($site{$k} .= "$cid-$mw{$k}"); } undef %mw; print " $cg words\n"; } sub SearchByIndex{ @aq = split(/\s+/,$in{'terms'}); #load in vars open(DB,"<$htmlpath/site.idx"); while(){ chomp; ($k,$v) = split(":",$_); $index{$k} = $v; } close DB; open(DB,"<$htmlpath/page.idx"); while(){ chomp; ($k,$v) = split(":",$_); $page{$k} = $v; } close DB; foreach $j (keys %index){ foreach $i (@aq){ if($in{'mcase'} eq 'Insensitive'){ ($in{'partial'})?(($j =~ /$i/i)&&($fnd{$i} .= "\t$index{$j}")):(($j =~ /\b$i\b/i)&&($fnd{$i} .= "\t$index{$j}")); } else{ ($in{'partial'})?(($j =~ /$i/)&&($fnd{$i} .= "\t$index{$j}")):(($j =~ /\b$i\b/)&&($fnd{$i} .= "\t$index{$j}")); } } }#end while ##got all the pages if($in{'mbool'} eq 'AND'){ &SortAndPages; } else{ &SortOrPages; } } sub SortOrPages{ foreach my $i (keys %fnd){ (@pages) = split("\t",$fnd{$i}); foreach my $s (@pages){ my($pid,$n) = split("-",$s); my $z = sprintf("%.10d",$n).$page{$pid}; $fndt{$z}=1; } } foreach my $x (sort {$b cmp $a} keys %fndt){ $x =~ s/^\d\d\d\d\d\d\d\d\d\d//; next if(!$x); &DisplayOneFromIndex("$rootpath/$x"); } } sub SortAndPages{ foreach my $i (keys %fnd){ (@pages) = split("\t",$fnd{$i}); foreach my $s (@pages){ my($pid,$n) = split("-",$s); my $z = sprintf("%.10d",$n).$page{$pid}; $i =~ tr/a-z/A-Z/; $fndt{$z}=1; $fndtx{$page{$pid},$i}=1; } } foreach my $x (sort {$b cmp $a} keys %fndt){ $getcha=1; foreach $z (@aq){ $z =~ tr/a-z/A-Z/; $x =~ s/^\d\d\d\d\d\d\d\d\d\d//; (!$fndtx{$x,$z})&&($getcha=0)&&(last); } next if(!$x); &DisplayOneFromIndex("$rootpath/$x") if ($getcha==1); } } sub DisplayOneFromIndex{ local($file) = @_; return if ($printed{$file}); $printed{$file} = 1; ##start pagination; $count++; if(($count > $start) & ($count <= $end)){ open(FILE,"<$file")||print "$!:$file
"; while(){ $data .= $_; } close FILE; &OutputPage; }##end pagination } sub SearchFile{ undef $/; local($file) = @_; open(FILE,"<$file")||print "$!:$file
"; $data = ; close FILE; $tdata = $data; if($in{'parsessi'}){ &CheckForSSI; } &FormatPage; (!$in{'partial'})&&($b='\b'); if($in{'mbool'} eq 'AND'){ if($in{'mcase'} eq 'Insensitive'){ if($data =~ /$b$in{'terms'}$b/i){ &DisplayFound; #print "Found: $in{'terms'}
"; } } else{ if($data =~ /$b$in{'terms'}$b/){ &DisplayFound; #print "Found: $in{'terms'}
"; } } } else{ @aq = split(/\s+/,$in{'terms'}); foreach $k (@aq){ if($in{'mcase'} eq 'Insensitive'){ if($data =~ /$b$k$b/i){ &DisplayFound; #print "Found: $in{'terms'}
"; } } else{ if($data =~ /$b$k$b/){ &DisplayFound; #print "Found: $in{'terms'}
"; } } } } } sub DisplayFound{ $data = $tdata; $count++; if(($count > $start) & ($count <= $end)){ &OutputPage }# end next page } sub GetNlinks{ ((!$in{'page'})||($in{'lpage'} < 0))&&($in{'lpage'} = 1); $nstart = ($in{'lpage'}+10)-10; $nend = $nstart + 10; $sstring = "command=$in{'command'}&terms=$in{'terms'}&mbool=$in{'mbool'}&mcase=$in{'mcase'}"; $sstring =~ s/([^\w&=])/'%'.sprintf("%.2x",ord($1))/ge; $npages = int($count/$in{'entriesperpage'}); (($count/$in{'entriesperpage'}) > $npages)&&($npages++); for $i (1..$npages){ if(($i > $nstart) & ($i <= $nend)){ ($i eq $in{'page'})?($link .= " $i "):($link .= " $i "); } } $nend; $pend = $nend - 20; ($npages > $nend)&&($link .= " [Next] "); ($nend > 11)&&($link = " [Prev] " . $link); (!$link)&&($link='0'); $link = "Páginas con resultados: " . $link; $in{'link'} = $link; } sub DeHTML{ ######################################################### # remove anything between ######################################################### $data =~ s/.*//gsi; ######################################################### # next we'll remove all the