Handle hostnames with upper-case letters
[webmin.git] / smf / svc_chooser.cgi
1 #!/usr/local/bin/perl
2 # chooser.cgi
3 # Outputs HTML for a frame-based svc chooser 
4
5 require './smf-lib.pl';
6
7 &init_config();
8 &ReadParse();
9
10 # type specifies svc, inst or both
11 $type = "both";
12 if (defined($in{'type'})) {
13         $type = $in{'type'};
14         }
15 if ((defined($in{'add'})) && ($in{'add'})) {
16         # Only use last fmri by default
17         $in{'fmri'} =~ s/\s+$//;
18         if ($in{'fmri'} =~ /\n(.*)$/) {
19                 $in{'fmri'} = $1;
20                 }
21         }
22 $fmri = $in{'fmri'};
23 if ($fmri =~ /^svc\:\/([^\:]*)\:*(.*)$/) {
24         # fmri entered is valid
25         $svc = $1;
26         $inst = $2;
27         }
28 else {
29         $fmri = "svc:/";
30         $svc = "";
31         $inst = "";
32         }
33 $add = int($in{'add'});
34
35 $frame = $in{'frame'};
36 if ($frame eq "" ) {
37         $frame = 0;
38 }
39
40 if ($in{'frame'} == 0) {
41         # base frame
42         &PrintHeader();
43         if ($in{'type'} eq "svc")
44                 { print "<title>$text{'svc_chooser_titlesvc'}</title>\n"; }
45         elsif ($in{'type'} eq "inst") 
46                 { print "<title>$text{'svc_chooser_titleinst'}</title>\n";}
47         else
48                 { print "<title>$text{'svc_chooser_titleboth'}</title>\n";}
49
50         print "<frameset rows='*,50'>\n";
51         print "<frame marginwidth=10 marginheight=10 name=topframe ",
52             "src=\"svc_chooser.cgi?frame=1&fmri=$fmri&add=$add&type=$type\">\n";
53         print "<frame marginwidth=10 marginheight=10 name=bottomframe ",
54             "src=\"svc_chooser.cgi?frame=2&add=$add&fmri=$fmri&type=$type\" ",
55             "scrolling=no>\n";
56         print "</frameset>\n";
57         }
58 elsif ($in{'frame'} == 1) {
59         # List of svcs
60         &header();
61         print <<EOF;
62 <script>
63 function svcclick(fmri, inst, expand, isvalid)
64 {
65 if ((inst == "") || (inst == ":")) {
66         selected_fmri = fmri;
67         }
68 else {
69         selected_fmri = fmri+inst;
70         }
71 curr = top.bottomframe.document.forms[0].fmri.value;
72 if (isvalid ==1) {
73         top.bottomframe.document.forms[0].fmri.value = selected_fmri;
74         }
75 if (expand == 1) {
76         location ="svc_chooser.cgi?frame=1&add=$add&type=$type&fmri="+fmri+inst;
77         }
78 }
79
80 </script>
81 EOF
82         print "<b>", &text('svc_chooser_fmri', $fmri),"</b>\n";
83         print "<table>\n";
84         # filter fmris via fmri var
85         @list = &svcs_listing("$fmri", "-sFMRI");
86         foreach $f_hash (@list) {
87                 $full_fmri = $f_hash->{"FMRI"};
88                 if ($full_fmri =~ /^$fmri([^\/:]+[\/\:]*).*$/) {
89                         $elt = "$1";
90                         # check if its an instance, if so add colon
91                         if ($fmri =~ /.*\:$/) {
92                                 $elt = ":$elt"; 
93                                 }
94                         push(@flist, "$elt");
95                         }
96                 }
97         @fmris = &unique(@flist);
98         $uplevel = $fmri;
99         if ($uplevel =~ /^svc\:\/.+$/) {
100                 $uplevel =~ /^svc\:\/(([^\/\:]+[\:\/])*)[^\/\:]+[\/]*[\:]*$/;
101                 $uplevel = "svc:/$1";
102                 unshift(@fmris, $uplevel);
103                 }
104         foreach $f (@fmris) {
105                 print "<tr>\n";
106                 # determine img type
107                 if ($f eq $uplevel) {
108                         $img = "images/uplevel.gif";
109                         $link =
110                   "<a href='javascript:svcclick(\"$uplevel\",\"\",1,0)'>";
111                 } elsif ($f =~ /^.*\/$/) {
112                         $img = "images/nextlevel.gif";
113                         $link =
114                   "<a href='javascript:svcclick(\"$fmri$f\",\"\",1,0)'>";
115                 } elsif ($f =~ /^\:.*/) {
116                         # at instance level...
117                         # remove leading ":"
118                         $f =~ s/^\:(\S+)$/$1/;
119                         $img = "images/instance.gif";
120                         $link =
121                   "<a href='javascript:svcclick(\"$fmri\",\"$f\",0,1)'>";
122                 } else {
123                         # service
124                         $img = "images/service.gif";
125                         # remove final ":" if present
126                         $f =~ s/^([^\:]+)\:$/$1/;
127                         $inst = ":";
128                         if ($type eq "svc") {
129                                 # cannot click to instance level!
130                                 # remove final ":" if present
131                                 $isvalid = 1;
132                                 $expand = 0;
133                         } elsif ($type eq "inst") {
134                                 # cannnot select svc...
135                                 $isvalid = 0;
136                                 $expand = 1;
137                         } else {
138                                 # both valid
139                                 $isvalid = 1;
140                                 $expand = 1;
141                                 }
142                         $link =
143   "<a href='javascript:svcclick(\"$fmri$f\",\"$inst\",$expand,$isvalid)'>";
144                         }
145                 print
146         "<td>$link<img border=0 width=30 height=30 src=$img></a></td>\n";
147                 print "<td>$link$f</a></td>\n";
148                 print "</tr>\n";
149                 }
150         print "</table></td></tr></table>\n";
151         &footer();
152         }
153 elsif ($in{'frame'} == 2) {
154         # Current fmri and OK/cancel buttons
155         &header();
156         print <<EOF;
157 <script>
158 function fmrichosen()
159 {
160 if ($add == 0) {
161         top.opener.ifield.value = document.forms[0].fmri.value;
162         }
163 else {
164         if (top.opener.ifield.value != "") {
165                 top.opener.ifield.value += " ";
166                 }
167         top.opener.ifield.value += document.forms[0].fmri.value;
168         }
169 top.close();
170 }
171 </script>
172 EOF
173         print "<table width=100%><tr><td>\n";
174         print "<form onSubmit='fmrichosen(); return false'>\n";
175         print
176          "<input name=fmri size=45 value=\"\">\n";
177         print
178          "<input type=\"submit\" value=\"$text{'svc_chooser_ok'}\">\n";
179         print "</form>\n";
180         print "</td><td>\n";
181         print "<form>";
182         print
183 "<input type=\"button\" onClick='top.close()' value=\"$text{'svc_chooser_cancel'}\">";
184         print "</form>";
185         print "</td></tr></table></form>\n";
186         &footer();
187         }