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