Handle hostnames with upper-case letters
[webmin.git] / htaccess-htpasswd / index.cgi
1 #!/usr/local/bin/perl
2 # index.cgi
3 # Display a list of protected directories and their users. The user can
4 # add more directories, and specify the encryption mode for each.
5
6 require './htaccess-lib.pl';
7 &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);
8
9 # Check needed Perl modules
10 if ($config{'md5'}) {
11         $missing = &check_md5();
12         if ($missing) {
13                 print &text('index_emd5', "<tt>$missing</tt>"),"\n";
14                 }
15         }
16 if ($config{'sha1'} && !$missing) {
17         $missing = &check_sha1();
18         if ($missing) {
19                 print &text('index_sha1', "<tt>$missing</tt>"),"\n";
20                 }
21         }
22 if ($missing) {
23         if (!$module_info{'usermin'}) {
24                 print &text('index_cpan', "../cpan/download.cgi?source=3&cpan=$missing&mode=2&return=/$module_name/&returndesc=".&urlize($text{'index_return'}));
25                 }
26         print "<p>\n";
27         &ui_print_footer("/", $text{'index'});
28         exit;
29         }
30
31 # Check for htdigest command, if we need it
32 if ($config{'digest'} && !$htdigest_command) {
33         eval "use Digest::MD5";
34         if ($@) {
35                 print &text('index_digest2', "<tt>htdigest</tt>",
36                                              "<tt>Digest::MD5</tt>"),"<p>\n";
37                 &ui_print_footer("/", $text{'index'});
38                 exit;
39                 }
40         }
41
42 @accessdirs || &error($text{'index_eaccess'});
43
44 @links = ( &select_all_link("d"),
45            &select_invert_link("d"),
46            "<a href='edit_dir.cgi?new=1'>$text{'index_add'}</a>" );
47
48 @dirs = &list_directories();
49 @dirs = grep { &can_access_dir($_->[0]) } @dirs;
50 @gtds = ( "width=25%", "width=25%", "width=25%", "width=25%" );
51 if (@dirs) {
52         @tds = ( "width=30% valign=top", "width=70% valign=top" );
53         if ($can_create) {
54                 print &ui_form_start("delete.cgi", "post");
55                 @tds = ( "width=5", @tds );
56                 print &ui_links_row(\@links);
57                 }
58         print &ui_columns_start([ $can_create ? ( "" ) : ( ),
59                                   $text{'index_dir'},
60                                   $text{'index_usersgroups'} ], 100, 0, \@tds);
61         &switch_user();
62         foreach $d (@dirs) {
63                 local @cols;
64                 if ($can_create) {
65                         push(@cols, "<a href='edit_dir.cgi?dir=".
66                                     &urlize($d->[0])."'>$d->[0]</a>");
67                         }
68                 else {
69                         push(@cols, $d->[0]);
70                         }
71
72                 # Show the users
73                 $users = $d->[2] == 3 ? &list_digest_users($d->[1])
74                                       : &list_users($d->[1]);
75                 if ($userconfig{'sort'} == 1 || $config{'sort'} == 1) {
76                         $users = [ sort { $a->{'user'} cmp $b->{'user'} }
77                                         @$users ];
78                         }
79                 @grid = ( );
80                 for($i=0; $i<@$users; $i++) {
81                         $u = $users->[$i];
82                         $link = "<a href='edit_user.cgi?idx=$u->{'index'}&dir=".
83                                 &urlize($d->[0])."'>$u->{'user'}</a>";
84                         if ($u->{'enabled'}) {
85                                 push(@grid, $link);
86                                 }
87                         else {
88                                 push(@grid, "<i>$link</i>");
89                                 }
90                         }
91                 if (@grid) {
92                         $utable = &ui_grid_table(\@grid, 4, 100, \@gtds);
93                         }
94                 else {
95                         $utable = "<i>$text{'index_nousers'}</i><br>\n";
96                         }
97
98                 # Show the groups
99                 if ($d->[4]) {
100                         @grid = ( );
101                         $groups = &list_groups($d->[4]);
102                         if ($userconfig{'sort'} == 1 || $config{'sort'} == 1) {
103                                 $groups = [ sort { $a->{'group'} cmp $b->{'group'} }
104                                                 @$groups ];
105                                 }
106                         for($i=0; $i<@$groups; $i++) {
107                                 $u = $groups->[$i];
108                                 $link= "<a href='edit_group.cgi?idx=$u->{'index'}&dir=".
109                                        &urlize($d->[0])."'>$u->{'group'} (".
110                                        scalar(@{$u->{'members'}}).")</a>";
111                                 if ($u->{'enabled'}) {
112                                         push(@grid, $link);
113                                         }
114                                 else {
115                                         push(@grid, "<i>$link</i>");
116                                         }
117                                 }
118                         if (@grid) {
119                                 $utable .= &ui_grid_table(\@grid, 4,100,\@gtds);
120                                 }
121                         else {
122                                 $utable .= "<i>$text{'index_nogroups'}</i><br>\n";
123                                 }
124                         }
125
126                 # User / group adder links
127                 @ulinks = ( );
128                 push(@ulinks, "<a href='edit_user.cgi?new=1&dir=".
129                               &urlize($d->[0])."'>$text{'index_uadd'}</a>");
130                 if ($d->[4]) {
131                         push(@ulinks, "<a href='edit_group.cgi?new=1&dir=".
132                              &urlize($d->[0])."'>$text{'index_gadd'}</a>");
133                         }
134                 $utable .= &ui_links_row(\@ulinks);
135                 push(@cols, $utable);
136                 if ($can_create) {
137                         print &ui_checked_columns_row(\@cols, \@tds,
138                                                       "d", $d->[0]);
139                         }
140                 else {
141                         print &ui_columns_row(\@cols, \@tds);
142                         }
143                 }
144         &switch_back();
145         print &ui_columns_end();
146         if ($can_create) {
147                 print &ui_links_row(\@links);
148                 print &ui_form_end([ [ "delete", $text{'index_delete'} ],
149                                      [ "remove", $text{'index_remove'} ] ]);
150                 }
151         }
152 else {
153         print "<b>$text{'index_none'}</b><p>\n";
154         print &ui_links_row([ $links[2] ]);
155         }
156
157 # Form to find existing .htaccess files
158 if ($can_create) {
159         print &ui_hr();
160         print &ui_form_start("search.cgi");
161         print &ui_submit($text{'index_search'}),"\n";
162         print &ui_textbox("search", $accessdirs[0] eq "/" ? "" : $accessdirs[0],
163                           40)." ".&file_chooser_button("search", 1)."<br>\n";
164         print &ui_form_end();
165         }
166
167 &ui_print_footer("/", $text{'index'});