codice:
#!c:\perl
# this is the url of your site
$base_url = "http://$ENV{'HTTP_HOST'}";
# this the path equivalent to your url
#$base_dir = $ENV{'DOCUMENT_ROOT'};
$base_dir = "e:/wwwroot/www/miosito.it/Online/";
# this is the url of this script
$search_url = "$base_url/cgi-bin/IecSrcEng.pl";
# this is the path to header.shtml
$header = "Search/headerEng.htm";
# this is the path to footer.shtml
$footer = "Search/footerEng.htm";
# this is the background color of the search
# you can also use an image by using background=image/ha.gif
#$bg = 'bgcolor=blue';
# this is every directory you want to search in, if you choose
# yes to search in subdirectories, you only need to list the
# main directories here... but if you only want to search some
# subdirectories, you can do so like this:
# @dirs = ('','news'); would search in news and the
# subdirectory oldnews
#@dirs = ('testsite');
#@dirs = ('inglese');
# search all directories within directories? yes/no
$subdirs = "yes";
# what files type do you want to search in? note that htm works for
# shtml, html, htm, etc...
@searchtypes = ('htm','html');
use CGI qw(:standard);
$search = new CGI;
$result_num = $search->param('result_num');
$show_search = $search->param('show_search');
$show_summary = $search->param('show_summary');
$keyword = $search->param('keyword');
$page = $search->param('page');
$connector = $search->param('connector');
$case = $search->param('case');
#$header = $search->param('header');
#$footer = $search->param('footer');
$mydirs = $search->param('dirs');
#$bg = $search->param('bkg');
@dirs = ($mydirs);
&get_header_footer;
&display_page unless $show_search eq "no";
@keywords = split(/\s+/,$keyword);
foreach $type (@dirs) {
chomp($type);
$dir = "$base_dir/$type";
chdir($dir);
opendir (DIR, $dir) || &cgiError ("Opening $dir Failed:", "$!");
@allfiles = readdir(DIR);
$totalnum=@allfiles;
closedir (DIR);
for($i=1; $i<$totalnum; $i++) {
if (-d $allfiles[$i]) {
if ($allfiles[$i] ne "." && $allfiles[$i] ne ".." && $type ne "" && $subdirs eq "yes") {
if ($type ne ".") {
push(@dirs,"$type/$allfiles[$i]");
}
else {
push(@dirs,"$allfiles[$i]");
}
}
}
if (-T $allfiles[$i])
{
($file,$ext) = split(/\./, $allfiles[$i]);
foreach $searchtype (@searchtypes)
{
if ($ext =~ /$searchtype/i)
{
open (SEARCH,"$allfiles[$i]") || &cgiError ("Open $allfiles[$i] Failed:", "$!");
@search = <SEARCH>;
close(SEARCH);
if ($type ne ".")
{
$temp_file = "$type/$allfiles[$i]";
}
else
{
$temp_file = "$allfiles[$i]";
}
foreach $text (@search)
{
chomp($text);
$text =~ s/<([^>]|\n)*>//g;
$text =~ s/&([^;])*;//g;
$text =~ s/])*-->//g;
if ($connector eq "or" && $case eq "insensitive") {
foreach $word (@keywords) {
if (!($text =~ /$word/i)) { $match = 0;}
else { $matches{$temp_file} = $text; last;}
}
}
if ($connector eq "and" && $case eq "insensitive") {
if (!($text =~ /$keyword/i)) { $match = 0;}
else { $matches{$temp_file} = $text; last;}
}
if ($connector eq "or" && $case eq "sensitive") {
foreach $word (@keywords) {
if (!($text =~ /$word/)) { $match = 0;}
else { $matches{$temp_file} = $text; last;}
}
}
if ($connector eq "and" && $case eq "sensitive") {
if (!($text =~ /$keyword/)) { $match = 0;}
else { $matches{$temp_file} = $text; last;}
}
}
}
}
}
}
}
foreach $key (sort(keys %matches)) { push(@matches,$key); push(@summary,$matches{$key}); }
&print_header;
print <<SEARCH_PAGE;
<table width="100%" border="0" cellpadding="10" cellspacing="0">
<tr bgcolor="#000000">
<td valign="middle" align="left"><font face="Verdana, Arial, Helvetica" color="#FFFFFF" size="3">Search result</font></td>
<td valign="bottom" align="right">
SEARCH_PAGE
$total = @matches;
$real_total = $total;
eval { $totalnum = int (($total + $result_num)/ $result_num); };
if ($page ) { ($liststart,$listend) = split(/ /,$page); }
else {
if ($total < $result_num) { $liststart = 0; $listend = $total; }
else { $liststart = 0; $listend = $result_num; }
}
if ($total > $result_num)
{
print "<form action=\"$search_url\" method=\"POST\" name=\"cerca\">\n";
print "<input type=\"hidden\" name=\"header\" value=\"$header\">\n";
print "<input type=\"hidden\" name=\"footer\" value=\"$footer\">\n";
print "<input type=\"hidden\" name=\"dirs\" value=\"$mydirs\">\n";
print "<input type=\"hidden\" name=\"dirs\" value=\"$mydirs\">\n";
print "<font face=\"Verdana, Arial, Helvetica\" color=\"#FFFFFF\" size=\"1\">Show documents:[/b]</font>";
print "<select name=\"page\" onChange=\"javascript:document.cerca.submit();\">\n";
for($i=0; $i<$totalnum; $i++)
{
eval { $num = ($i * $result_num); };
eval { $end = ($num + $result_num); };
if ($matches[$num] )
{
if ($total<$end) { $end = $total; }
$real_num = $num;
eval { $num = ($num + 1); };
print " <option value=\"$real_num $end\">$num-$end</option>\n";
eval { $num = ($num - 1); };
}
}
print "</select><input type=\"hidden\" name=\"show_search\" value=\"no\">\n";
print "<input type=\"hidden\" name=\"show_summary\" value=\"$show_summary\">\n";
print "<input type=\"hidden\" name=\"result_num\" value=\"$result_num\">\n";
print "<input type=\"hidden\" name=\"case\" value=\"$case\">\n";
print "<input type=\"hidden\" name=\"connector\" value=\"$connector\">\n";
print "<input type=\"hidden\" name=\"keyword\" value=\"$keyword\"></form></p>\n";
}
else
{
print "\n"
}
eval { $first = ($liststart + 1); };
$last = $listend;
if ($listend<=$result_num && $end<=$result_num) { $last = $total; }
print "</td></tr></table>\n";
#
print "<table width=\"100%\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\"><tr><td colspan=\"2\">\n";
if ($total eq "0") { print "<font face=\"Verdana, Arial, Helvetica\" size=\"2\" color=\"#FFFFFF\">Sorry, No Matches Found!
We did not find anything that matched your search criteria.
Please try searching again with a different combination of keywords.</font>\n";}
else {
print "
<font face=\"Verdana, Arial, Helvetica\" size=\"2\" color=\"#FFFFFF\">Below are the results of your search for: <font color=\"#333300\">$keyword</font>
Documents from $first to $last of $real_total Matches
\n";
if ($liststart eq "$listend") { eval { $liststart = ($liststart - 1); }; }
for($liststart; $liststart<$listend; $liststart++)
{
if ($matches[$liststart])
{
$match = $matches[$liststart];
$summary = $summary[$liststart];
# if ($summary eq "") { $summary = "Nessun sommario disponibile."; }
print "<a href=\"$base_url/$match\">$match</a>
\n";
# print "URL: $base_url/$match
\n";
# if ($show_summary eq "yes") { print "Sommario: $summary
"; }
# print "
\n\n";
}
}
print "</font><font face=\"Verdana, Arial, Helvetica\" size=\"1\" color=\"#FFFFFF\">
";
print "
Didn't find what you were looking for? Try searching again with a more specific keyword.
\n\n";
}
print "<form action=\"$search_url\" method=\"POST\" name=\"show_search\">\n";
print "<font color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica\" size=\"2\">Search for: <input type=\"text\" size=\"36\" name=\"keyword\" MAXLENGTH=\"35\"></font>
\n";
print "<input type=\"hidden\" name=\"show_search\" value=\"no\">\n";
print "<input type=\"hidden\" name=\"result_num\" value=\"$result_num\">\n";
print "<input type=\"hidden\" name=\"case\" value=\"$case\">\n";
print "<input type=\"hidden\" name=\"connector\" value=\"$connector\">\n";
print "<input type=\"hidden\" name=\"show_summary\" value=\"$show_summary\">\n";
print "<input type=\"hidden\" name=\"header\" value=\"$header\">\n";
print "<input type=\"hidden\" name=\"footer\" value=\"$footer\">\n";
print "<input type=\"hidden\" name=\"dirs\" value=\"$mydirs\">
\n";
#print "<input type=\"submit\" value=\"Cerca\"></form>
\n";
print "</td></tr></table>\n";
&print_footer;
# sub routines #
sub display_page {
&print_header;
print "<form action=\"$search_url\" method=\"POST\" name=\"show_search\">";
print <<END_SEARCH_PAGE;
<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr bgcolor="#000000">
<td colspan="3"><font face="Verdana, Arial, Helvetica" color="#FFFFFF" size="2">
Enter a few words to search for and
you will get a list of every file that contains a match</font></td>
</tr>
<tr>
<td bgcolor="#666666" width="143" align="right"><font color="#FFFFFF" face="Verdana, Arial, Helvetica" size="1">
Search as </font></td>
<td width="218" bgcolor="#666666"> <font face="Verdana, Arial, Helvetica" size="1">
<input type="radio" name="connector" value="or" checked>
<font color="#FFFFFF">Keywords </font></font> </td>
<td width="139" bgcolor="#333333"> <font face="Verdana, Arial, Helvetica" size="1">
<input type="radio" name="connector" value="and">
<font color="#FFFFFF">Phrase</font></font></td>
</tr>
<tr bgcolor="#7F7F2A">
<td width="143" align="right" bgcolor="#91826E"><font color="#FFFFFF" face="Verdana, Arial, Helvetica" size="1">Result per page</font></td>
<td colspan="2" bgcolor="#91826E">
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr bgcolor="#91826E">
<td><font face="Verdana, Arial, Helvetica" size="1">
<input type="radio" name="result_num" value="10">
<font color="#FFFFFF">10</font></font></td>
<td><font face="Verdana, Arial, Helvetica" size="1">
<input type="radio" name="result_num" value="20" checked>
<font color="#FFFFFF">20</font></font></td>
<td><font face="Verdana, Arial, Helvetica" size="1">
<input type="radio" name="result_num" value="30">
<font color="#FFFFFF">30</font></font></td>
<td><font face="Verdana, Arial, Helvetica" size="1">
<input type="radio" name="result_num" value="40">
<font color="#FFFFFF">40</font></font></td>
<td><font face="Verdana, Arial, Helvetica" size="1">
<input type="radio" name="result_num" value="50">
<font color="#FFFFFF">50</font></font>
<input type="hidden" name="show_search" value="no">
END_SEARCH_PAGE
print "<input type=\"hidden\" name=\"header\" value=\"$header\">\n";
print "<input type=\"hidden\" name=\"footer\" value=\"$footer\">\n";
print "<input type=\"hidden\" name=\"dirs\" value=\"$mydirs\">\n";
print <<END_SEARCH_PAGE;
</td>
</tr>
</table>
</td>
</tr>
</table>
END_SEARCH_PAGE
&print_footer;
exit;
}
sub print_header {
print "Content-type: text/html\n\n";
foreach $linez (@dataa) {
chomp($linez);
print "$linez\n";
}
}
sub print_footer {
foreach $liney (@datab) {
chomp($liney);
print "$liney\n";
}
}
sub get_header_footer {
open (FILEA,"$base_dir/$header") || &cgiError ("Reading Header Failed:", "$!");
@dataa = <FILEA>;
close(FILEA);
open (FILEB,"$base_dir/$footer") || &cgiError ("Reading Footer Failed:", "$!");
@datab = <FILEB>;
close(FILEB);
}
sub cgiError {
my ($error_cause,$error) = @_;
if ($error_cause eq "") { $error_cause = "Error:"; }
if ($error eq "") { $error = "The script encountered problems and terminated"; }
&print_header;
print "<h3>$error_cause</h3>$error
";
&print_footer;
exit;
}