#!/usr/bin/perl # # Kira's simple SWISH-E search CGI # use CGI::Carp qw(fatalsToBrowser); # You'll need to change this to the document root of your webspace. # for personal accounts, change it to /home/yourusername/public_html. # If you're running your own server, it should be the path to your # document root for the server, e.g. /home/htdocs $docroot = '/home/yourusername/public_html'; # This also must be changed; if you've used your personal account path # above, you should change this to /~youruserid so the webserver can # properly translate the URL for your files. For non-personal pages, # just set this to blank. $prefix = '/~yourusername'; print "Content-type:text/html\n\n"; # customize this section as appropriate for your site print <Search Results

Search Results

EndHTML read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/~!/ ~!/g; $FORM{$name} = $value; } $keystring = $FORM{'keywords'}; if ($keystring =~ /^([\w\-\. ]+)$/ ) { $topic = $1; } else { &dienice("Bad keyword: `$topic'. Please don't use commas or non-alphanumeric characters."); } @results = `/usr/local/bin/swish-e -w "$topic" -f /home/yourusername/public_html/swish.index`; $ct = 0; foreach $i (@results) { # results are returned in the form: # relevance path title filesize # separated by spaces. # comments start with #, and the last line starts with a ., so we're #ignoring those: if ($i =~ /^#/ or $i =~ /^\./) { # errors start with 'err', so we'll pass those on to dienice: } elsif ($i =~ /^err/) { $i =~ s/^err/Error/; &dienice($i); # } else { ($start, $title, $size) = split(/\"/,$i); ($perc, $url) = split(/ /,$start); $perc = $perc / 1000 * 100; $percstr = sprintf("%3.1f\%",$perc); # since the "url" returned is really the full unix path to the file, # you need to translate this to a proper web url. Change the docroot # and prefix variables as described above. $url =~ s/^$docroot/$prefix/; print "$title - $percstr
\n"; $ct = $ct + 1; } } if ($ct == 0) { print "No results found.

\n"; } &do_footer; sub do_footer { # customize this section as appropriate for your site print < $ct results found.

EndFoot } sub dienice { my($msg) = @_; print "

Error

\n"; print $msg; &do_footer; exit; } # the end.