Handle hostnames with upper-case letters
[webmin.git] / servers / index.cgi
1 #!/usr/local/bin/perl
2 # Display a list of other webmin servers
3
4 use strict;
5 use warnings;
6 require './servers-lib.pl';
7 our (%text, %config, %access);
8 &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);
9
10 my @servers = &list_servers_sorted(1);
11
12 # Work out links
13 my @linksrow;
14 if ($access{'edit'}) {
15         if (@servers) {
16                 print &ui_form_start("delete_servs.cgi");
17                 push(@linksrow, &select_all_link("d"),
18                                 &select_invert_link("d"));
19                 }
20         if ($access{'add'}) {
21                 push(@linksrow, "<a href='edit_serv.cgi?new=1'>".
22                                 "$text{'index_add'}</a>");
23                 }
24         }
25
26 if (@servers && $config{'display_mode'}) {
27         # Show table of servers
28         print &ui_links_row(\@linksrow);
29         print &ui_columns_start([
30                 $access{'edit'} ? ( "" ) : ( ),
31                 $text{'index_host'},
32                 $text{'index_desc'},
33                 $text{'index_group'},
34                 $text{'index_os'} ], 100);
35         foreach my $s (@servers) {
36                 my @cols;
37                 my $table =
38                         "<table cellpadding=0 cellspacing=0 width=100%><tr>\n";
39                 if (!$access{'links'} || !$s->{'port'}) {
40                         $table .= "<td>\n";
41                         $table .= ($s->{'realhost'} || $s->{'host'});
42                         $table .= ":$s->{'port'}" if ($s->{'port'});
43                         $table .= "</td>\n";
44                         }
45                 else {
46                         if ($s->{'user'} || $s->{'autouser'}) {
47                                 $table .= "<td><a href='link.cgi/$s->{'id'}/' target=_top>\n";
48                                 }
49                         else {
50                                 $table .= "<td><a href=".&make_url($s)." target=_top>\n";
51                                 }
52                         $table .= ($s->{'realhost'} || $s->{'host'});
53                         $table .= ":$s->{'port'}</a></td>\n";
54                         }
55                 $table .= "<td align=right>";
56                 if ($s->{'autouser'} && &logged_in($s)) {
57                         $table .= "<a href='logout.cgi?id=$s->{'id'}'>($text{'index_logout'})</a>\n";
58                         }
59                 if ($access{'edit'}) {
60                         $table .= "<a href='edit_serv.cgi?id=$s->{'id'}'>($text{'index_edit'})</a>\n";
61                         }
62                 $table .= "</td> </tr></table>\n";
63                 push(@cols, $table);
64                 push(@cols, $s->{'desc'});
65                 push(@cols, $s->{'group'} || $text{'index_none'});
66                 my ($type) = grep { $_->[0] eq $s->{'type'} }
67                                   &get_server_types();
68                 push(@cols, $type->[1]);
69                 if ($access{'edit'}) {
70                         print &ui_checked_columns_row(\@cols, undef,
71                                                       "d", $s->{'id'});
72                         }
73                 else {
74                         print &ui_columns_row(\@cols);
75                         }
76                 }
77         print &ui_columns_end();
78         }
79 elsif (@servers) {
80         # Show server icons
81         print &ui_links_row(\@linksrow);
82         my (@afters, @befores);
83         if ($access{'edit'}) {
84                 my $sep = length($text{'index_edit'}) > 10 ? "<br>" : " ";
85                 @afters = map { $sep."<a href='edit_serv.cgi?id=$_->{'id'}'>(".$text{'index_edit'}.")</a>" } @servers;
86                 @befores = map { &ui_checkbox("d", $_->{'id'}) } @servers;
87                 }
88         my @titles = map { &make_iconname($_) } @servers;
89         my @icons = map { "images/$_->{'type'}.gif" } @servers;
90         my @links = map { !$access{'links'} ? undef :
91                        $_->{'user'} || $_->{'autouser'} ?
92                         "link.cgi/$_->{'id'}/" : &make_url($_) } @servers;
93         &icons_table(\@links, \@titles, \@icons, undef, "target=_top",
94                      undef, undef, \@befores, \@afters);
95         }
96 else {
97         print "<b>$text{'index_noservers'}</b> <p>\n";
98         }
99 if ($access{'edit'}) {
100         print &ui_links_row(\@linksrow);
101         if (@servers) {
102                 print &ui_form_end([ [ "delete", $text{'index_delete'} ] ]);
103                 }
104         }
105
106 my $myip = &get_my_address();
107 my $myscan = &address_to_broadcast($myip, 1) if ($myip);
108 if ($access{'find'} || $access{'auto'}) {
109         print &ui_hr();
110         print &ui_buttons_start();
111         if ($access{'find'}) {
112                 # Buttons to scan and broadcast for servers
113                 my %miniserv;
114                 &get_miniserv_config(\%miniserv);
115                 my $port = $config{'listen'} || $miniserv{'listen'} || 10000;
116                 print &ui_buttons_row("find.cgi", $text{'index_broad'},
117                                                   $text{'index_findmsg'});
118                 print &ui_buttons_row("find.cgi", $text{'index_scan'},
119                       &text('index_scanmsg', &ui_textbox("scan", $myscan, 15)).
120                       "<br><table>\n".
121                       "<tr> <td><b>$text{'index_defuser'}</b></td>\n".
122                       "<td>".&ui_textbox("defuser", undef, 20)."</td> </tr>".
123                       "<tr> <td><b>$text{'index_defpass'}</b></td>\n".
124                       "<td>".&ui_password("defpass", undef, 20)."</td> </tr>".
125                       "<tr> <td><b>$text{'index_defport'}</b></td>\n".
126                       "<td>".&ui_textbox("port", $port, 20)."</td> </tr>".
127                       "</table>\n"
128                       );
129                 }
130         if ($access{'auto'}) {
131                 # Button for auto-discovery form
132                 print &ui_buttons_row("edit_auto.cgi", $text{'index_auto'},
133                                                   $text{'index_automsg'});
134                 }
135         print &ui_buttons_end();
136         }
137
138 &ui_print_footer("/", $text{'index'});
139
140 sub make_url
141 {
142 return sprintf "http%s://%s:%d/",
143         $_[0]->{'ssl'} ? 's' : '', $_[0]->{'host'}, $_[0]->{'port'};
144 }
145
146 sub make_iconname
147 {
148 my $rv;
149 if ($_[0]->{'desc'} && !$config{'show_ip'}) {
150         $rv = $_[0]->{'desc'};
151         }
152 elsif ($_[0]->{'realhost'}) {
153         $rv = "$_[0]->{'realhost'}:$_[0]->{'port'}";
154         }
155 else {
156         $rv = "$_[0]->{'host'}:$_[0]->{'port'}";
157         }
158 if (&logged_in($_[0])) {
159         $rv .= "</a> <a href='logout.cgi?id=$_->{'id'}'>(".$text{'index_logout'}.")";
160         }
161 return $rv;
162 }
163