R
Robin
I have some questions on this , why isn't sub parse and sub search not
performing the way I want them too, they are supposed to go through all of
the directories and then open the files in them and then if the contents of
the file match param ('query') print the results. It's been driving me
crazy, if someone could point me in the right direction it would be great.
This is very incomplete and doesn't use all of it's data yet, so I would
appreciate comments that don't point that out, for example, what if it opens
binary files? I dunno..thanks in advance, by the way, I know that it's
somewhere in the methods search or parse, but other than that I don't have
the foggiest.
-Robin
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl qw flock);
use CGI qwall);
$CGI:OST_MAX=1024 * 100; # max 100K posts
$CGI:ISABLE_UPLOADS = 1; # no uploads
$" = '';
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
my @directories = ("./", "../", "../design"); #change this to the
directories you want to have searched.
my $action = url_param ('action');
my $rootfile = url (relative=>1);
my $headerfile = "searchheader.txt";
my $footerfile = "searchfooter.txt";
my $errorfile = "ERR.txt";
my @head = getheader ($headerfile);
my @foot = getfooter ($footerfile);
my $date = getdate ();
my @errors;
if ($action eq "search")
{
search ();
}
else
{
newsearch ();
}
sub search
{
print header;
print (@head);
print <<END;
<p><strong><em>Infused Search</em></strong>
<br>
<br>
Search Results:
</p>
END
#code for parsing results
foreach my $dir (@directories)
{
opendir (DIR, $dir);
my @files_from_dir = readdir (DIR);
closedir (DIR);
foreach my $file (@files_from_dir)
{
if (! -d $file)
{
parse ($file, $dir);
}
}
}
print <<END;
<hr size="1">
</body>
</html>
END
}
sub parse
{
my ($filetoparse, $dirtoparse) = @_;
my @results;
open (FILE, $filetoparse) or push (@errors, "A file open operation
failed.");
flock (FILE, LOCK_SH) or push (@errors, "A file lock operation
failed.");
my @filecontents = <FILE>;
close (FILE);
chomp (@filecontents);
my $result;
my $filecontents = join ('', @filecontents);
if (param ('query') =~ /$filecontents/m and param ('query'))
{
$result = $filetoparse;
print "<a href=\"$dirtoparse" . "$filetoparse\">$result</a><br>";
}
}
sub newsearch
{
print header;
print (@head);
print <<END;
<strong><em>Infused Search</em></strong>
<br>
<br>
<hr size="1">
<form name="form1" method="post" action="search.pl?action=search">
<input type="text" name="query">
<input type="submit" name="Submit" value="Submit">
</form>
<hr size="1">
END
print (@foot);
}
sub checkerrors
{
if (@errors)
{
print header;
print "<html><body><center>";
print "There were errors while trying to execute Infused Search. They
are listed as follows.<br><br>\n";
foreach my $error (@errors)
{
print ($error, "<br>\n");
}
my $errflag = 0;
if (! open (ERRORF, ">>$errorfile") and flock (ERRORF, LOCK_EX))
{
print "There was an error logging the errors: file cannot be locked or
opened.<br>";
$errflag = 1;
}
else
{
print ERRORF ("Current date: $date", "\n");
foreach my $error2 (@errors)
{
print ERRORF $error2, "\n";
}
}
close (ERRORF);
if (! $errflag)
{
print "<br>", "Errors have been logged in $errorfile.";
}
print "</body></html>";
exit (0);
}
else
{
return;
}
}
sub getheader
{
my $header_sub = shift;
my (@headertoret);
if (-e $header_sub)
{
open (HEADERF, $header_sub);
flock (HEADERF, LOCK_SH);
@headertoret = <HEADERF>;
close (HEADERF);
}
else
{
open (HEADERF, ">$header_sub");
flock (HEADERF, LOCK_EX);
@headertoret = <<END;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body,td,th {
color: #FFA4A4;
}
body {
background-color: #000000;
}
a:link {
color: #66FFFF;
}
a:visited {
color: #66FFFF;
}
a:hover {
color: #FF9933;
}
a:active {
color: #66FFFF;
}
-->
</style></head>
<body>
END
print HEADERF @headertoret;
close (HEADERF);
}
return (@headertoret);
}
sub getfooter
{
my $footer_sub = shift;
my (@footertoret);
if (-e $footer_sub)
{
open (HEADERF, $footer_sub);
flock (HEADERF, LOCK_SH);
@footertoret = <HEADERF>;
close (HEADERF);
}
else
{
open (HEADERF, ">$footer_sub");
flock (HEADERF, LOCK_EX);
@footertoret = <<END;
</body></html>
END
print HEADERF @footertoret;
close (HEADERF);
}
return (@footertoret);
}
sub getdate
{
my ($day, $mon, $year)=(localtime)[3,4,5];
$mon++;
$year +=1900;
my $date = $mon . "/" . $day . "/" . $year;
return $date;
}
performing the way I want them too, they are supposed to go through all of
the directories and then open the files in them and then if the contents of
the file match param ('query') print the results. It's been driving me
crazy, if someone could point me in the right direction it would be great.
This is very incomplete and doesn't use all of it's data yet, so I would
appreciate comments that don't point that out, for example, what if it opens
binary files? I dunno..thanks in advance, by the way, I know that it's
somewhere in the methods search or parse, but other than that I don't have
the foggiest.
-Robin
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl qw flock);
use CGI qwall);
$CGI:OST_MAX=1024 * 100; # max 100K posts
$CGI:ISABLE_UPLOADS = 1; # no uploads
$" = '';
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
my @directories = ("./", "../", "../design"); #change this to the
directories you want to have searched.
my $action = url_param ('action');
my $rootfile = url (relative=>1);
my $headerfile = "searchheader.txt";
my $footerfile = "searchfooter.txt";
my $errorfile = "ERR.txt";
my @head = getheader ($headerfile);
my @foot = getfooter ($footerfile);
my $date = getdate ();
my @errors;
if ($action eq "search")
{
search ();
}
else
{
newsearch ();
}
sub search
{
print header;
print (@head);
print <<END;
<p><strong><em>Infused Search</em></strong>
<br>
<br>
Search Results:
</p>
END
#code for parsing results
foreach my $dir (@directories)
{
opendir (DIR, $dir);
my @files_from_dir = readdir (DIR);
closedir (DIR);
foreach my $file (@files_from_dir)
{
if (! -d $file)
{
parse ($file, $dir);
}
}
}
print <<END;
<hr size="1">
</body>
</html>
END
}
sub parse
{
my ($filetoparse, $dirtoparse) = @_;
my @results;
open (FILE, $filetoparse) or push (@errors, "A file open operation
failed.");
flock (FILE, LOCK_SH) or push (@errors, "A file lock operation
failed.");
my @filecontents = <FILE>;
close (FILE);
chomp (@filecontents);
my $result;
my $filecontents = join ('', @filecontents);
if (param ('query') =~ /$filecontents/m and param ('query'))
{
$result = $filetoparse;
print "<a href=\"$dirtoparse" . "$filetoparse\">$result</a><br>";
}
}
sub newsearch
{
print header;
print (@head);
print <<END;
<strong><em>Infused Search</em></strong>
<br>
<br>
<hr size="1">
<form name="form1" method="post" action="search.pl?action=search">
<input type="text" name="query">
<input type="submit" name="Submit" value="Submit">
</form>
<hr size="1">
END
print (@foot);
}
sub checkerrors
{
if (@errors)
{
print header;
print "<html><body><center>";
print "There were errors while trying to execute Infused Search. They
are listed as follows.<br><br>\n";
foreach my $error (@errors)
{
print ($error, "<br>\n");
}
my $errflag = 0;
if (! open (ERRORF, ">>$errorfile") and flock (ERRORF, LOCK_EX))
{
print "There was an error logging the errors: file cannot be locked or
opened.<br>";
$errflag = 1;
}
else
{
print ERRORF ("Current date: $date", "\n");
foreach my $error2 (@errors)
{
print ERRORF $error2, "\n";
}
}
close (ERRORF);
if (! $errflag)
{
print "<br>", "Errors have been logged in $errorfile.";
}
print "</body></html>";
exit (0);
}
else
{
return;
}
}
sub getheader
{
my $header_sub = shift;
my (@headertoret);
if (-e $header_sub)
{
open (HEADERF, $header_sub);
flock (HEADERF, LOCK_SH);
@headertoret = <HEADERF>;
close (HEADERF);
}
else
{
open (HEADERF, ">$header_sub");
flock (HEADERF, LOCK_EX);
@headertoret = <<END;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body,td,th {
color: #FFA4A4;
}
body {
background-color: #000000;
}
a:link {
color: #66FFFF;
}
a:visited {
color: #66FFFF;
}
a:hover {
color: #FF9933;
}
a:active {
color: #66FFFF;
}
-->
</style></head>
<body>
END
print HEADERF @headertoret;
close (HEADERF);
}
return (@headertoret);
}
sub getfooter
{
my $footer_sub = shift;
my (@footertoret);
if (-e $footer_sub)
{
open (HEADERF, $footer_sub);
flock (HEADERF, LOCK_SH);
@footertoret = <HEADERF>;
close (HEADERF);
}
else
{
open (HEADERF, ">$footer_sub");
flock (HEADERF, LOCK_EX);
@footertoret = <<END;
</body></html>
END
print HEADERF @footertoret;
close (HEADERF);
}
return (@footertoret);
}
sub getdate
{
my ($day, $mon, $year)=(localtime)[3,4,5];
$mon++;
$year +=1900;
my $date = $mon . "/" . $day . "/" . $year;
return $date;
}