forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.cgi
executable file
·424 lines (407 loc) · 10.8 KB
/
search.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/local/bin/perl
# search.cgi
# Search for manual pages, and display a list of matches or an exact page
require './man-lib.pl';
use Config;
&ReadParse();
$in{'for'} || &error($text{'search_efor'});
@for = split(/\s+/, $in{'for'});
@howto = split(/\s+/, $config{'howto_dir'});
@doc = split(/\s+/, $config{'doc_dir'});
foreach $s (split(/\0/, $in{'section'})) {
if ($s =~ /^([^:]+):(.*)/) {
$section{$1}++;
$opts{$1} = $2;
}
else {
$section{$s}++;
}
}
if ($in{'check'} && $config{'check'}) {
@check = split(/\s+/, $config{'check'});
foreach $s (keys %section) {
delete($section{$s}) if (&indexof($s, @check) < 0);
}
}
if ($section{'doc'}) {
# Look in the system documentation directory (usually /usr/doc)
foreach $d (@doc) {
push(@rv, map { [ $text{'search_doc'},
"view_doc.cgi?file=".&urlize($_->[0]),
substr($_->[0], length($d)+1),
$_->[1], 1 ] }
&find_contents($d, \@for,
$howto[0], 1, $in{'exact'}));
}
}
if ($section{'custom'}) {
# Look in the custom documentation directory
push(@rv, map { [ $text{'search_custom'},
"view_doc.cgi?file=".&urlize($_->[0]),
substr($_->[0], length($config{'custom_dir'})+1),
$_->[1], 1 ] }
&find_contents($config{'custom_dir'}, \@for,
$howto[0], 1, $in{'exact'}));
}
if ($section{'howto'}) {
# Look in the HOWTO directory
foreach $h (@howto) {
push(@rv, map { [ $text{'search_howto'},
"view_howto.cgi?file=".&urlize($_->[0]),
$_->[2], $_->[1], 2 ] }
&find_contents($h, \@for, undef, 0, $in{'exact'}));
}
@rv = grep { $_->[2] !~ /^index/i } @rv;
}
if ($section{'kernel'}) {
# Look in the linux kernel Documentation directory
push(@rv, map { [ $text{'search_kernel'},
"view_kernel.cgi?file=".&urlize($_->[0]),
substr($_->[0], length($config{'kernel_dir'})+1),
$_->[1], 1 ] }
&find_contents($config{'kernel_dir'}, \@for,
undef, 1, $in{'exact'}));
}
if ($section{'kde'}) {
# Look in the KDE documentation directory
push(@rv, map { [ $text{'search_kde'},
"view_kde.cgi?file=".&urlize($_->[0]),
substr($_->[0], length($config{'kde_dir'})+1),
$_->[1], 1 ] }
&find_contents($config{'kde_dir'}, \@for,
undef, 1, $in{'exact'}));
}
if ($section{'perl'}) {
if ($in{'exact'}) {
# Check for an exact module name match
local @f = $in{'and'} ? ( $for[0] ) : @for;
foreach $f (@f) {
chop($out = &backquote_command("$perl_doc -l ".quotemeta($f)." 2>/dev/null", 1));
if ($out) {
local $doc = &parse_perl_module($out);
$doc->{'name'} =~ s/^\s*(\S+)\s+-\s+//;
push(@rv, [ $text{'search_perl'},
"view_perl.cgi?mod=$f",
$f, $doc->{'name'}, 1 ]);
}
}
}
else {
# Search the text of all perl modules
foreach $d ($Config{'sitelib'}, $Config{'privlib'}) {
&open_execute_command(FIND, "find $d -name '*.pm' -print", 1, 1);
while($path = <FIND>) {
chop($path);
local $doc = &parse_perl_module($path);
local ($any = 0, $all = 1);
foreach $f (@for) {
if ($doc->{'name'} !~ /$f/i &&
$doc->{'description'} !~ /$f/i) {
$all = 0;
}
else {
$any = 1;
}
}
next if (!$all && $in{'and'} || !$any);
$doc->{'name'} =~ s/^\s*(\S+)\s+-\s+//;
local $modfile =
&backquote_command("$perl_doc -l ".quotemeta($doc->{'package'})." 2>/dev/null", 1);
if ($doc->{'package'} && $modfile) {
push(@rv, [ $text{'search_perl'},
"view_perl.cgi?mod=$doc->{'package'}",
$doc->{'package'}, $doc->{'name'},
1 ]);
}
}
close(FIND);
}
}
}
if ($section{'help'}) {
# Look in the webmin module help pages
opendir(DIR, $root_directory);
foreach my $m (readdir(DIR)) {
# Is this a module with help
local $dir = "$root_directory/$m/help";
next if (!-d $dir || $m =~ /^\./ || -l "$root_directory/$m");
local %minfo = &get_module_info($m);
next if (!%minfo || !&check_os_support(\%minfo));
# Check the help pages
local @pfx;
opendir(DIR2, $dir);
while($f = readdir(DIR2)) {
push(@pfx, $1) if ($f =~ /^([^\.]+)\.html$/);
}
closedir(DIR2);
HELP: foreach $p (&unique(@pfx)) {
local $file = &help_file($m, $p);
open(HELP, $file);
local @st = stat($file);
read(HELP, $help, $st[7]);
close(HELP);
if ($help =~ /<header>([^<]+)<\/header>/) {
$header = $1;
}
else { next; }
$help =~ s/<include\s+(\S+)>/inchelp($1, $m)/ge;
$help =~ s/<[^>]+>//g;
local $matches = 0;
if ($in{'exact'}) {
# Just check header
foreach $f (@for) {
$matches++ if ($header =~ /\Q$f\E/i);
}
}
else {
# Check entire body
foreach $f (@for) {
$matches++ if ($help =~ /\Q$f\E/i);
}
}
if (($in{'and'} && $matches == @for) ||
(!$in{'and'} && $matches)) {
push(@rv, [ $text{'search_help'},
"/help.cgi/$m/$p?x=1",
"$m/$p", $header,
2 ]);
}
}
}
}
if ($section{'man'}) {
# Look in manual pages (searches are never exact)
$cmd = $config{'search_cmd'};
map { s/\\/\\\\/g; s/'/\\'/g; } @for;
if ($in{'and'}) {
local $qm = quotemeta($for[0]);
$cmd =~ s/PAGE/$qm/;
}
else {
local $fors = join(" ", map { quotemeta($_) } @for);
$cmd =~ s/PAGE/$fors/;
}
&set_manpath($opts{'man'});
&open_execute_command(MAN, $cmd, 1, 1);
while(<MAN>) {
$got .= $_;
if (/(([^,\s]+).*)\s*\((\S+)\)\s+-\s+(.*)/ &&
!$done{$2,$3}++) {
local ($page, $sect, $desc) = ($1, $3, $4);
if ($page =~ /^(\S+)\s*\[(.+)\]/) {
$page = "$1 $2";
}
local @pp = split(/[\s+,]/, $page);
map { s/\((\S+)\)//; } @pp;
# Keywords must be page name or desc
local ($any = 0, $all = 1, $exact);
foreach $f (@for) {
if ($desc !~ /$f/i && $page !~ /$f/i &&
&indexof($f, @pp) < 0) {
$all = 0;
}
else {
$any = 1;
}
$exact++ if (&indexof($f, @pp) >= 0);
}
next if (!$all && $in{'and'} || !$any);
push(@rv, [ $text{'search_man'},
"view_man.cgi?page=$pp[0]&sec=$3&opts=".
$opts{'man'}, "$pp[0] ($sect)", $desc,
$exact ? 4 : 3 ]);
}
}
close(MAN);
}
if ($section{'google'}) {
# Try to call the Google search engine
local ($grv, $error);
local $j = $in{'and'} ? ' and ' : ' or ';
&http_download($google_host, $google_port, "$google_page?q=".
&urlize(join($j, @for))."&sourceid=webmin&num=20",
\$grv, \$error);
if (!$error) {
# Parse the results
while($grv =~ /(<p[^>]*>|<div[^>]*>)<a[^>]+href=([^>]+)>([\000-\377]+?)<\/a>([\000-\377]*)$/i) {
$grv = $4;
local ($url = $2, $desc = $3);
$url =~ s/^"(.*)".*$/$1/;
$url =~ s/^'(.*)'.*$/$1/;
$desc =~ s/<\/?b>//g;
local $matches = 0;
foreach $f (@for) {
$matches++ if ($desc =~ /\Q$f\E/i);
}
if (!$in{'exact'} ||
($in{'and'} && $matches == @for) ||
(!$in{'and'} && $matches)) {
push(@rv, [ $text{'search_google'}, $url, length($url) > 60 ? substr($url, 0, 60)."..." : $url, $desc, 0.5 ]);
}
}
}
}
if (@rv == 1 && !$in{'check'}) {
# redirect to the exact page
&redirect($in{'exact'} ? $rv[0]->[1]
: "$rv[0]->[1]&for=".&urlize($in{'for'}));
exit;
}
# Display search results
$for = join($in{'and'} ? " and " : " or ", map { "<tt>$_</tt>" } @for);
&ui_print_header(&text('search_for', $for), $text{'search_title'}, "");
if (@rv) {
#@rv = sort { $b->[4] <=> $a->[4] } @rv;
@rv = sort { &ranking($b) <=> &ranking($a) } @rv;
print &ui_columns_start([ $text{'search_file'},
$text{'search_type'},
$text{'search_desc'} ], 100);
foreach $r (@rv) {
local @cols;
if ($r->[1] =~ /^(http|ftp|https):/) {
push(@cols, "<a href='$r->[1]'>".
&html_escape($r->[2])."</a>");
}
else {
push(@cols, "<a href='$r->[1]&for=".&urlize($in{'for'}).
"'>".&html_escape($r->[2])."</a>");
}
push(@cols, $r->[0]);
push(@cols, &html_escape($r->[3]));
print &ui_columns_row(\@cols, [ undef, "nowrap", undef ]);
}
print &ui_columns_end();
}
else {
print "<p><b>",&text('search_none', "<tt>$in{'for'}</tt>"),"</b><p>\n";
}
&ui_print_footer("", $text{'index_return'});
# find_contents(directory, &strings, [exclude], [descend], [nameonly])
# Find some string in a directory of files
sub find_contents
{
opendir(DIR, $_[0]);
local @f = readdir(DIR);
closedir(DIR);
local @rv;
foreach $f (@f) {
next if ($f =~ /^\./);
local $p = "$_[0]/$f";
next if ($p eq $_[2]);
if (-d $p) {
# go into subdirectory
push(@rv, &find_contents($p, $_[1], $_[2], $_[3], $_[4]))
if ($_[3]);
}
else {
# Skip non-text or HTML files
local $ff = $f;
$ff =~ s/\.(gz|bz|bz2)$//i;
next if ($ff !~ /\.(txt|htm|html|doc)$/ &&
$ff =~ /\.[A-Za-z0-9]+$/);
next if ($ff =~ /(^makefile$)|(^core$)/i);
local $matches = 0;
foreach $s (@{$_[1]}) {
$matches++ if ($p =~ /\Q$s\E/i);
}
if ($_[4]) {
# just compare filename
if ($in{'and'} && $matches == @{$_[1]} ||
!$in{'and'} && $matches) {
local ($desc, $data) = &read_doc_file($p);
if ($desc !~ /^#!/ && $desc !~ /^#\%/) {
push(@rv, [ $p, $desc, $f, $matches ]);
}
}
}
else {
# compare file contents
local ($desc, $data) = &read_doc_file($p);
local $dmatches = 0;
foreach $s (@{$_[1]}) {
$dmatches++ if ($data =~ /\Q$s\E/i);
}
if (($in{'and'} && $dmatches == @{$_[1]} ||
!$in{'and'} && $dmatches) &&
$desc !~ /^#!/ && $desc !~ /^#\%/) {
push(@rv, [ $p, $desc, $f, $matches ]);
}
}
}
}
return @rv;
}
# read_doc_file(filename)
# Returns desc, data
sub read_doc_file
{
local ($two, $first, $title, $data);
open(FILE, $_[0]);
read(FILE, $two, 2);
local $qm = quotemeta($_[0]);
if ($two eq "\037\213") {
close(FILE);
&open_execute_command(FILE, "gunzip -c $qm", 1, 1);
}
elsif ($two eq "BZ") {
close(FILE);
&open_execute_command(FILE, "bunzip2 -c $qm", 1, 1);
}
seek(FILE, 0, 0);
while(<FILE>) {
$data .= $_;
if (/[A-Za-z0-9]/ && !/\$\S+:/ && !$first) {
chop($first = $_);
$first =~ s/.\010//g;
}
}
close(FILE);
if ($data =~ /<\s*title\s*>([\000-\177]{0,200})<\s*\/\s*title\s*>/i) {
$title = $1;
}
return ($title ? $title : $first =~ /<.*>/ ? undef : $first, $data);
}
# parse_perl_module(file)
sub parse_perl_module
{
local (%doc, $inside);
open(MOD, $_[0]);
while(<MOD>) {
if (/^\s*package\s+(\S+)\s*;/ && !$doc{'package'}) {
$doc{'package'} = $1;
}
elsif (/^=head1\s+(\S+)/i) {
$inside = $1;
}
elsif (/^=cut/i) {
undef($inside);
}
elsif ($inside) {
$doc{lc($inside)} .= $_;
}
}
close(MOD);
return \%doc;
}
# inchelp(path, module)
sub inchelp
{
local $inc;
local $ipath = &help_file($_[1], $_[0]);
open(INC, $ipath) || return "<i>".&text('search_einclude', $_[0])."</i><br>\n";
local @st = stat(INC);
read(INC, $inc, $st[7]);
close(INC);
return $inc;
}
sub ranking
{
local ($name = 0, $desc = 0);
foreach $f (@for) {
$desc++ if ($_[0]->[3] =~ /$f/i);
$name++ if ($_[0]->[1] =~ /$f/i);
}
return $name ? $_[0]->[4] * 10 :
$desc ? $_[0]->[4] :
$_[0]->[4] / 10;
}