Show core modules first
[webmin.git] / webmin_search.cgi
1 #!/usr/local/bin/perl
2 # Search Webmin modules and help pages and text and config.info
3
4 do './web-lib.pl';
5 &init_config();
6 do './ui-lib.pl';
7 &ReadParse();
8
9 $prod = &get_product_name();
10 $ucprod = ucfirst($prod);
11 &ui_print_header(undef, &text('wsearch_title', $ucprod), "", undef, 0, 1);
12
13 $re = $in{'search'};
14 if ($re !~ /\S/) {
15         &error($text{'wsearch_esearch'});
16         }
17 $re =~ s/^\s+//;
18 $re =~ s/\s+$//;
19
20 # Search module names first
21 $count = 0;
22 @mods = sort { $b->{'longdesc'} cmp $a->{'longdesc'} }
23              grep { !$_->{'clone'} } &get_available_module_infos();
24 foreach $m (@mods) {
25         if ($m->{'desc'} =~ /\Q$re\E/i || $m->{'dir'} =~ /\Q$re\E/i) {
26                 &match_row(
27                         $m,
28                         "<a href='$m->{'dir'}/'>$m->{'desc'}</a>",
29                         $text{'wsearch_mtitle'},
30                         undef,
31                         0,
32                         );
33                 }
34         }
35
36 # Then do module configs
37 foreach $m (@mods) {
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";
42         %info = ( );
43         @info_order = ( );
44         &read_file($file, \%info, \@info_order);
45         foreach $o (@lang_order_list) {
46                 &read_file("$file.$o", \%info);
47                 }
48         $section = undef;
49         foreach $c (@info_order) {
50                 @p = split(/,/, $info{$c});
51                 if ($p[1] == 11) {
52                         $section = $c;
53                         }
54                 if ($p[0] =~ /\Q$re\E/i) {
55                         &match_row(
56                             $m,
57                             "<a href='config.cgi?module=$m->{'dir'}&".
58                              "section=".&urlize($section)."#$c'>$p[0]</a>",
59                             $text{'wsearch_config_'.$prod},
60                             $p[0],
61                             1,
62                             );
63                         }
64                 }
65         }
66
67 # Then do help pages
68 %lang_order_list = map { $_, 1 } @lang_order_list;
69 foreach $m (@mods) {
70         $helpdir = &module_root_directory($m->{'dir'})."/help";
71         %donepage = ( );
72         opendir(DIR, $helpdir);
73         foreach $f (sort { length($b) <=> length($a) } readdir(DIR)) {
74                 # Work out if we should grep this help page - don't do the same
75                 # page twice for different languages
76                 $grep = 0;
77                 if ($f =~ /^(\S+)\.([^\.]+)\.html$/) {
78                         ($page, $lang) = ($1, $2);
79                         if ($lang_order_list{$lang} && !$donepage{$page}++) {
80                                 $grep = 1;
81                                 }
82                         }
83                 elsif ($f =~ /^(\S+)\.html$/) {
84                         $page = $1;
85                         if (!$donepage{$page}++) {
86                                 $grep = 1;
87                                 }
88                         }
89
90                 # If yes, search it
91                 if ($grep) {
92                         $data = &read_file_contents("$helpdir/$f");
93                         if ($data =~ /<header>([^<]*)<\/header>/) {
94                                 $title = $1;
95                                 }
96                         else {
97                                 $title = $f;
98                                 }
99                         $data =~ s/\s+/ /g;
100                         $data =~ s/<p>/\n\n/gi;
101                         $data =~ s/<br>/\n/gi;
102                         $data =~ s/<[^>]+>//g;
103                         if ($data =~ /\Q$re\E/i) {
104                                 &match_row(
105                                     $m,
106                                     &hlink($title, $page, $m->{'dir'}),
107                                     $text{'wsearch_help'},
108                                     $data,
109                                     1,
110                                     );
111                                 }
112                         }
113                 }
114         closedir(DIR);
115         }
116
117 # Then do text strings
118 MODULE: foreach $m (@mods) {
119         %mtext = &load_language($m->{'dir'});
120         foreach $k (keys %mtext) {
121                 if ($mtext{$k} =~ /\Q$re\E/i) {
122                         &match_row(
123                             $m,
124                             "<a href='$m->{'dir'}/'>$m->{'desc'}</a>",
125                             $text{'wsearch_text'},
126                             $mtext{$k},
127                             0,
128                             );
129                         next MODULE;
130                         }
131                 }
132         }
133
134 if (!$count) {
135         print "<b>",&text('wsearch_enone', "<tt>$re</tt>"),"</b><p>\n";
136         }
137
138 &ui_print_footer();
139
140 # Returns text with the search term bolded, and truncated to 60 characters
141 sub highlight_text
142 {
143 local ($str, $len) = @_;
144 $len ||= 90;
145 local $hlen = $len / 2;
146 if ($str =~ /(.*)(\Q$re\E)(.*)/i) {
147         local ($before, $match, $after) = ($1, $2, $3);
148         if (length($before) > $hlen) {
149                 $before = "...".substr($before, length($before)-$hlen);
150                 }
151         if (length($after) > $hlen) {
152                 $after = substr($after, 0, $hlen)."...";
153                 }
154         $str = $before."<b>".$match."</b>".$after;
155         }
156 return $str;
157 }
158
159 sub match_row
160 {
161 local ($m, $link, $what, $text, $module_link) = @_;
162 print "<font size=+1>$link</font>\n";
163 if ($module_link) {
164         print " (".&text('wsearch_inmod',
165                          "<a href='$m->{'dir'}/'>$m->{'desc'}</a>").")";
166         }
167 print "<br>\n";
168 if ($text) {
169         print &highlight_text($text),"<br>\n";
170         }
171 print "<font color=#4EBF37>$m->{'desc'} - $what</font><br>&nbsp;<br>\n";
172 $count++;
173 }
174