Debian Codegraph @

contrib/ewswrap/ewswrap.cgi

raw
1 #!/usr/bin/perl -w
2
3 # ewswrap.cgi
4 #
5 # by John Grohol ([email protected])
6 # Freeware
7 # v1.00 - 5 Oct 1998
8 #
9 # Simple wrapper script for htsearch to parse old
10 # Excite for Web Servers (EWS) forms as-is
11 # This only makes sense if your want to upgrade
12 # your search engine but can't upgrade every form
13 # which points to it (e.g., external sites are
14 # pointing to your EWS CGI.
15 #
16 # As an added bonus, given the differences from how
17 # EWS handles queries to how htsearch handles them,
18 # it does some basic sanity checking on the query
19 # and tries to re-form it into a valid htsearch query.
20 #
21 # This script must be called using the POST method!
22 #
23 #_______________________________________________________
24 # Set some defaults here
25 # These can be overridden in the calling form
26
27 $config = "htdig"; # htDig config file
28 $exclude = ""; # exclude this url
29 $restrict = ""; # restrict to this url
30 $format = "builtin-long"; # results format
31 $method = "and"; # default method
32 $dir = "/usr/httpd/cgi-bin"; # Set cgi-bin dir
33
34 #_______________________________________________________
35 # Rest of program
36
37 $| = 1;
38
39 # Get the form variables from POST form
40
41 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
42 @pairs = split(/&/, $buffer);
43
44 foreach $pair (@pairs) {
45 ($name, $value) = split(/=/, $pair);
46 $value =~ tr/+/ /;
47 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
48 $value =~ s/<!--(.|\n)*-->//g;
49 $value =~ s/<([^>]|\n)*>//g;
50 $tags{$name} = $value;
51 }
52
53 $squery = $tags{'search'}; # Set search query
54 $page = $tags{'page'};
55 if (not($page)) { $page=1; }
56
57 $squery =~ s/\+//g;
58 $squery =~ s/\-//g;
59 $squery =~ s/the//g;
60 $squery =~ s/not//g;
61 $squery =~ s/what//g;
62
63 # If someone puts "and" or "or" in the query,
64 # then it should be a boolean query
65
66 if (($squery =~ " and ") || ($squery =~ " or ")) {
67 $method = "boolean"; }
68
69 # Count the number of words in the query
70
71 @words = split(/ /,$squery);
72 foreach $word (@words) { $xwd++; }
73
74 # If there are quotes in the query, we have to
75 # turn them into parantheses and make it boolean
76
77 if (($squery =~ "\"")) {
78 $oo = (index($squery,"\""))+1;
79 $od = (index($squery,"\"",$oo))-1;
80 $op = $od - $oo +1;
81 $yty = substr($squery,$oo,$op);
82 @wrds = split(/ /,$yty);
83 foreach $wrd (@wrds) { $xww++; }
84
85 if ($xww eq 2) { # Right now, can only handle 2-word phrases
86 $oi = (index($yty," "));
87 if ($oi > -1) {
88 $ytt = substr($yty,0,$oi);
89 $john = $od - $oi +1;
90 $yte = substr($yty,$oi+1,$john);
91 $james = substr($squery,$od+2);
92 $james =~ s/ and//g;
93 $james =~ s/ / and /g;
94 $squery = "($ytt and $yte) $james"; # We turn it into a
95 $method = "boolean"; # boolean query
96 }
97
98 # More than 2 words in quotes (phrase), just
99 # turn it into one big string of words and set method to "and"
100
101 } else {
102 $squery =~ s/\"//g;
103 $squery =~ s/ and//g;
104 $method = "and";
105 $yty = "";
106 }
107 }
108
109 # Set the environmental variables
110
111 $ENV{'REQUEST_METHOD'} = 'GET';
112 $ENV{'QUERY_STRING'} = "config=$config&restrict=$restrict&exclude=$exclude&words=$squery&method=$method&format=$format&page=$page";
113
114 # Run htsearch
115
116 system("$dir/htsearch");
117
118 1;