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_header(undef, &text('wsearch_title', $ucprod), "", undef, 0, 1);
14 # Validate search text
17 &error($text{'wsearch_esearch'});
22 # Search module names and add to results list
24 @mods = sort { $b->{'longdesc'} cmp $a->{'longdesc'} }
25 grep { !$_->{'clone'} } &get_available_module_infos();
27 if ($m->{'desc'} =~ /\Q$re\E/i) {
28 push(@rv, { 'mod' => $m,
31 'link' => $m->{'dir'}.'/',
32 'text' => $m->{'desc'} });
36 # Search module configs and their help pages
38 %access = &get_module_acl(undef, $m);
39 next if ($access{'noconfig'});
40 $file = $prod eq 'webmin' ? "$m->{'dir'}/config.info"
41 : "$m->{'dir'}/uconfig.info";
44 &read_file($file, \%info, \@info_order);
45 foreach $o (@lang_order_list) {
46 &read_file("$file.$o", \%info);
49 foreach $c (@info_order) {
50 @p = split(/,/, $info{$c});
54 if ($p[0] =~ /\Q$re\E/i) {
55 # Config description matches
56 push(@rv, { 'mod' => $m,
59 'link' => "config.cgi?module=$m->{'dir'}&".
60 "section=".&urlize($section)."#$c",
64 $hfl = &help_file($mod->{'dir'}, "config_".$c);
65 ($title, $help) = &help_file_match($hfl);
68 push(@rv, { 'mod' => $m,
71 'link' => "help.cgi/$m->{'dir'}/config_".$c,
72 'desc' => &text('wsearch_helpfor', $p[0]),
74 'cgis' => [ "/config.cgi?".
75 "module=$m->{'dir'}§ion=".
76 &urlize($section)."#$c" ],
82 # Search other help pages
83 %lang_order_list = map { $_, 1 } @lang_order_list;
85 $helpdir = &module_root_directory($m->{'dir'})."/help";
87 opendir(DIR, $helpdir);
88 foreach $f (sort { length($b) <=> length($a) } readdir(DIR)) {
89 next if ($f =~ /^config_/); # For config help, already done
91 # Work out if we should grep this help page - don't do the same
92 # page twice for different languages
94 if ($f =~ /^(\S+)\.([^\.]+)\.html$/) {
95 ($page, $lang) = ($1, $2);
96 if ($lang_order_list{$lang} && !$donepage{$page}++) {
100 elsif ($f =~ /^(\S+)\.html$/) {
102 if (!$donepage{$page}++) {
109 ($title, $help) = &help_file_match("$helpdir/$f");
111 my @cgis = &find_cgi_text(
112 [ "hlink\\(.*'$page'",
113 "hlink\\(.*\"$page\"",
115 push(@rv, { 'mod' => $m,
118 'link' => "help.cgi/$m->{'dir'}/$page",
128 # Then do text strings
129 %gtext = &load_language("");
130 MODULE: foreach $m (@mods) {
131 %mtext = &load_language($m->{'dir'});
132 foreach $k (keys %mtext) {
133 next if ($gtext{$k}); # Skip repeated global strings
134 if ($mtext{$k} =~ /\Q$re\E/i) {
135 my @cgis = &find_cgi_text(
139 push(@rv, { 'mod' => $m,
142 'text' => $mtext{$k},
148 # Sort results by relevancy
150 @rv = sort { $b->{'rank'} <=> $a->{'rank'} } @rv;
154 # XXX next page link?
155 print &ui_columns_start(
156 [ $text{'wsearch_htext'}, $text{'wsearch_htype'},
157 $text{'wsearch_hmod'}, $text{'wsearch_hcgis'} ], 100);
158 foreach my $r (@rv) {
159 $hi = &highlight_text($r->{'text'});
161 $hi = "<a href='$r->{'link'}'>$hi</a>";
164 foreach my $c (@{$r->{'cgis'}}) {
165 ($cmod, $cpage) = split(/\//, $c);
166 ($cpage, $cargs) = split(/\?/, $cpage);
167 $ctitle = &cgi_page_title($cmod, $cpage) || $cpage;
168 push(@links, "<a href='$c'>$ctitle</a>");
171 @links = ( @links[0..1], "..." );
173 print &ui_columns_row([
175 $text{'wsearch_type_'.$r->{'type'}},
176 "<a href='$r->{'mod'}->{'dir'}/'>$r->{'mod'}->{'desc'}</a>",
177 &ui_links_row(\@links),
180 print &ui_columns_end();
183 print "<b>",&text('wsearch_enone',
184 "<tt>".&html_escape($re)."</tt>"),"</b><p>\n";
189 # highlight_text(text, [length])
190 # Returns text with the search term bolded, and truncated to 60 characters
193 local ($str, $len) = @_;
195 local $hlen = $len / 2;
196 $str =~ s/<[^>]*>//g;
197 if ($str =~ /(.*)(\Q$re\E)(.*)/i) {
198 local ($before, $match, $after) = ($1, $2, $3);
199 if (length($before) > $hlen) {
200 $before = "...".substr($before, length($before)-$hlen);
202 if (length($after) > $hlen) {
203 $after = substr($after, 0, $hlen)."...";
205 $str = $before."<b>".&html_escape($match)."</b>".$after;
210 # find_cgi_text(®exps, module, re-mode)
211 # Returns the relative URLs of CGIs that matches some regexps, in the given
215 local ($res, $m, $remode) = @_;
216 local $mdir = &module_root_directory($m);
218 foreach my $f (glob("$mdir/*.cgi")) {
221 LINE: while(my $line = <CGI>) {
222 foreach my $r (@$res) {
223 if (!$remode && index($line, $r) >= 0 ||
224 $remode && $line =~ /$r/) {
233 $url =~ s/^\Q$root_directory\E\///;
240 # help_file_match(file)
241 # Returns the title if some help file matches the current search
245 local $data = &read_file_contents($f);
247 if ($data =~ /<header>([^<]*)<\/header>/) {
254 $data =~ s/<p>/\n\n/gi;
255 $data =~ s/<br>/\n/gi;
256 $data =~ s/<[^>]+>//g;
257 if ($data =~ /\Q$re\E/i) {
258 return ($title, $data);
263 # cgi_page_title(module, cgi)
264 # Given a CGI, return the text for its page title, if possible
267 local ($m, $cgi) = @_;
268 local $data = &read_file_contents(&module_root_directory($m)."/".$cgi);
270 if ($data =~ /(header|ui_print_header|ui_print_unbuffered_header)\([^,]+,\s*(\$text{'([^']+)'|\$text{"([^"]+)"|\&text\('([^']+)'|\&text\("([^"]+)")/) {
271 local $msg = $3 || $4 || $5 || $6;
272 local %mtext = &load_language($m);
275 if ($cgi eq "index.cgi" && !$rv) {
276 # If no title was found for an index.cgi, use module title
277 local %minfo = &get_module_info($m);
278 $rv = $minfo{'desc'};