[sf-perl] opendir question..for search script

Vargas Media steve at vargasmedia.com
Mon Jan 22 11:33:09 PST 2007


Hi All,
I have a quick question.
I'm using "opendir" and trying to get my search.cgi script to search in my main www directory instead of my cgi-bin 
as the HTML pages found can't be accessed there.
I did this as a tutorial from a book I have.
Here are my Env Variables:
http://www.autotection.com/cgi-bin/env_var.pl
any help very appreciated.
Thanks Steve
############search.cgi

#!/usr/bin/perl
&get_form_data();
$search_term = $FORM{'search'};
print "Content-type: text/html\n\n";
&search(".");

sub get_form_data
 {
  # get the data
read(STDIN, $buffer, $ENV{ 'CONTENT_LENGTH' } );

@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
  ($firstname, $value) = split(/=/, $pair);

 $value =~ tr/+/ /;
 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 $value =~ s/<!--(.|\n)*-->//g;
 $FORM{$firstname} = $value;
  }
}

print <<EOF;
<HTML>
<HEAD>
<TITLE>
Search
</TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF">
<P>Results of your search: $search_term</P>
EOF
foreach $file (@found_set)
{
    print "<A HREF=\"$file\">$Title{$file}</A>\n";
    print "<BR>\n";
}
if ((@found_set) != "") {
  print "<H3>Above Are Matches for the Search Term: $search_term</H3>\n";
} else {
  print "<H3>Sorry, no files found under the search term: $search_term</H3>\n";
}

print <<EOF;
</BODY>
</HTML>
EOF
exit;
sub search
{
 local ($dir) = @_;
 if($dir eq ".")
 {
  opendir(DIR, ".");
  $dir = "";
 }
 else
 {
  opendir(DIR, $dir);
  $dir .= "/";
 }
 foreach $file (sort readdir(DIR))
 {
  next if($file eq "." || $file eq "..");
  $file = $dir . $file;
  next if(($file !~ /.htm/) && (!(-d $file)));

  if(-d $file)
  {
   &search($file);
   next;
  }
  open(FILE, $file);
  $found_match = 0;
  $title = "";
  while(<FILE>)
  {
   if(/$search_term/i)
   {
    $found_match = 1;
   }
   if(/<TITLE>/)
   {
    chop;
    $title = $_;
    $title =~ s/<TITLE>//g;
    $title =~ s/<\/TITLE>//g;
   }
  }
  if($found_match)
  {
   push(@found_set, $file);
   if($title eq "")
   {
    $Title{$file} = $file;
   }
   else
   {
    $Title{$file} = $title;
   }
  }
  close(FILE);
  print "<P>\n";
 }
 closedir(DIR);
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20070122/f11366ef/attachment-0001.html 


More information about the SanFrancisco-pm mailing list