2 # Search Webmin modules and help pages and text and config.info
4 BEGIN { push(@INC, ".."); };
10 $prod = &get_product_name();
11 $ucprod = ucfirst($prod);
12 &ui_print_unbuffered_header(
13 undef, &text('wsearch_title', $ucprod), "", undef, 0, 1);
15 # Validate search text
18 &error($text{'wsearch_esearch'});
23 # Work out this Webmin's URL base
24 $urlhost = $ENV{'HTTP_HOST'};
25 if ($urlhost !~ /:/) {
26 $urlhost .= ":".$ENV{'SERVER_PORT'};
28 $urlbase = ($ENV{'HTTPS'} eq 'ON' ? 'https://' : 'http://').$urlhost;
31 print &text('wsearch_searching', "<i>".&html_escape($re)."</i>"),"\n";
33 # Search module names and add to results list
35 @mods = sort { $b->{'longdesc'} cmp $a->{'longdesc'} }
36 grep { !$_->{'clone'} } &get_available_module_infos();
38 if ($m->{'desc'} =~ /\Q$re\E/i) {
39 # Module description match
40 push(@rv, { 'mod' => $m,
43 'link' => $m->{'dir'}.'/',
44 'text' => $m->{'desc'} });
46 elsif ($m->{'dir'} =~ /\Q$re\E/i) {
47 # Module directory match
48 push(@rv, { 'mod' => $m,
51 'link' => $m->{'dir'}.'/',
52 'text' => $urlbase."/".$m->{'dir'}."/" });
57 # Search module configs and their help pages
59 %access = &get_module_acl(undef, $m);
60 next if ($access{'noconfig'});
61 $file = $prod eq 'webmin' ? "$m->{'dir'}/config.info"
62 : "$m->{'dir'}/uconfig.info";
65 &read_file($file, \%info, \@info_order);
66 foreach $o (@lang_order_list) {
67 &read_file("$file.$o", \%info);
70 foreach $c (@info_order) {
71 @p = split(/,/, $info{$c});
75 if ($p[0] =~ /\Q$re\E/i) {
76 # Config description matches
77 push(@rv, { 'mod' => $m,
80 'link' => "config.cgi?module=$m->{'dir'}&".
81 "section=".&urlize($section)."#$c",
85 $hfl = &help_file($mod->{'dir'}, "config_".$c);
86 ($title, $help) = &help_file_match($hfl);
89 push(@rv, { 'mod' => $m,
92 'link' => "help.cgi/$m->{'dir'}/config_".$c,
93 'desc' => &text('wsearch_helpfor', $p[0]),
95 'cgis' => [ "/config.cgi?".
96 "module=$m->{'dir'}§ion=".
97 &urlize($section)."#$c" ],
104 # Search other help pages
105 %lang_order_list = map { $_, 1 } @lang_order_list;
107 $helpdir = &module_root_directory($m->{'dir'})."/help";
109 opendir(DIR, $helpdir);
110 foreach $f (sort { length($b) <=> length($a) } readdir(DIR)) {
111 next if ($f =~ /^config_/); # For config help, already done
113 # Work out if we should grep this help page - don't do the same
114 # page twice for different languages
116 if ($f =~ /^(\S+)\.([^\.]+)\.html$/) {
117 ($page, $lang) = ($1, $2);
118 if ($lang_order_list{$lang} && !$donepage{$page}++) {
122 elsif ($f =~ /^(\S+)\.html$/) {
124 if (!$donepage{$page}++) {
131 ($title, $help) = &help_file_match("$helpdir/$f");
133 my @cgis = &find_cgi_text(
134 [ "hlink\\(.*'$page'",
135 "hlink\\(.*\"$page\"",
137 push(@rv, { 'mod' => $m,
140 'link' => "help.cgi/$m->{'dir'}/$page",
151 # Then do text strings
152 %gtext = &load_language("");
153 MODULE: foreach $m (@mods) {
154 %mtext = &load_language($m->{'dir'});
155 foreach $k (keys %mtext) {
156 next if ($gtext{$k}); # Skip repeated global strings
157 $mtext{$k} =~ s/\$[0-9]//g;
158 if ($mtext{$k} =~ /\Q$re\E/i) {
159 # Find CGIs that use this text
160 my @cgis = &find_cgi_text(
165 "&text(\"$k\"" ], $m);
167 push(@rv, { 'mod' => $m,
170 'text' => $mtext{$k},
178 print &text('wsearch_found', scalar(@rv)),"<p>\n";
180 # Sort results by relevancy
182 @rv = sort { $b->{'rank'} <=> $a->{'rank'} } @rv;
186 print &ui_columns_start(
187 [ $text{'wsearch_htext'}, $text{'wsearch_htype'},
188 $text{'wsearch_hmod'}, $text{'wsearch_hcgis'} ], 100);
189 foreach my $r (@rv) {
190 $hi = &highlight_text($r->{'text'});
192 $hi = "<a href='$r->{'link'}'>$hi</a>";
195 foreach my $c (@{$r->{'cgis'}}) {
196 ($cmod, $cpage) = split(/\//, $c);
197 ($cpage, $cargs) = split(/\?/, $cpage);
198 $ctitle = &cgi_page_title($cmod, $cpage) || $cpage;
199 if ($r->{'mod'}->{'installed'}) {
200 $cargs ||= &cgi_page_args($cmod, $cpage);
203 # For modules that aren't installed, linking
204 # to a CGI is likely useless
207 if ($cargs eq "none") {
208 push(@links, $ctitle);
212 "<a href='$cmod/$cpage?$cargs'>$ctitle</a>");
216 @links = ( @links[0..1], "..." );
218 print &ui_columns_row([
220 $text{'wsearch_type_'.$r->{'type'}},
221 "<a href='$r->{'mod'}->{'dir'}/'>$r->{'mod'}->{'desc'}</a>",
222 &ui_links_row(\@links),
225 print &ui_columns_end();
228 print "<b>",&text('wsearch_enone',
229 "<tt>".&html_escape($re)."</tt>"),"</b><p>\n";
234 # highlight_text(text, [length])
235 # Returns text with the search term bolded, and truncated to 60 characters
238 local ($str, $len) = @_;
240 local $hlen = $len / 2;
241 $str =~ s/<[^>]*>//g;
242 if ($str =~ /(.*)(\Q$re\E)(.*)/i) {
243 local ($before, $match, $after) = ($1, $2, $3);
244 if (length($before) > $hlen) {
245 $before = "...".substr($before, length($before)-$hlen);
247 if (length($after) > $hlen) {
248 $after = substr($after, 0, $hlen)."...";
250 $str = $before."<b>".&html_escape($match)."</b>".$after;
255 # find_cgi_text(®exps, module, re-mode)
256 # Returns the relative URLs of CGIs that matches some regexps, in the given
257 # module. Does not include those that don't call some header function, as
258 # they cannot be linked to normally
261 local ($res, $m, $remode) = @_;
262 local $mdir = &module_root_directory($m);
264 foreach my $f (glob("$mdir/*.cgi")) {
268 LINE: while(my $line = <CGI>) {
269 if ($line =~ /(header|ui_print_header|ui_print_unbuffered_header)\(/) {
272 foreach my $r (@$res) {
273 if (!$remode && index($line, $r) >= 0 ||
274 $remode && $line =~ /$r/) {
281 if ($found && $header) {
283 $url =~ s/^\Q$root_directory\E\///;
290 # help_file_match(file)
291 # Returns the title if some help file matches the current search
295 local $data = &read_file_contents($f);
297 if ($data =~ /<header>([^<]*)<\/header>/) {
304 $data =~ s/<p>/\n\n/gi;
305 $data =~ s/<br>/\n/gi;
306 $data =~ s/<[^>]+>//g;
307 if ($data =~ /\Q$re\E/i) {
308 return ($title, $data);
313 # cgi_page_title(module, cgi)
314 # Given a CGI, return the text for its page title, if possible
317 local ($m, $cgi) = @_;
318 local $data = &read_file_contents(&module_root_directory($m)."/".$cgi);
320 if ($data =~ /(ui_print_header|ui_print_unbuffered_header)\([^,]+,[^,]*(\$text{'([^']+)'|\$text{"([^"]+)"|\&text\('([^']+)'|\&text\("([^"]+)")/) {
321 # New header function, with arg before title
322 local $msg = $3 || $4 || $5 || $6;
323 local %mtext = &load_language($m);
326 elsif ($data =~ /(^|\s)header\(\s*(\$text{'([^']+)'|\$text{"([^"]+)"|\&text\('([^']+)'|\&text\("([^"]+)")/) {
327 # Old header function
328 local $msg = $3 || $4 || $5 || $6;
329 local %mtext = &load_language($m);
332 if ($cgi eq "index.cgi" && !$rv) {
333 # If no title was found for an index.cgi, use module title
334 local %minfo = &get_module_info($m);
335 $rv = $minfo{'desc'};
340 # cgi_page_args(module, cgi)
341 # Given a module and CGI name, returns a string of URL parameters that can be
342 # used for linking to it. Returns "none" if parameters are needed, but cannot
346 local ($m, $cgi) = @_;
347 local $mroot = &module_root_directory($m);
348 if (-r "$mroot/cgi_args.pl") {
349 # Module can tell us what args to use
350 &foreign_require($m, "cgi_args.pl");
351 $args = &foreign_call($m, "cgi_args", $cgi);
352 if (defined($args)) {
356 if ($cgi eq "index.cgi") {
357 # Index page is always safe to link to
360 # Otherwise check if it appears to parse any args
361 local $data = &read_file_contents($mroot."/".$cgi);
362 if ($data =~ /(ReadParse|ReadParseMime)\(/) {
369 # Print one dot per second
373 if ($now > $last_print_search_dot) {
375 $last_print_search_dot = $now;