Handle hostnames with upper-case letters
[webmin.git] / webmin / install_mod.cgi
1 #!/usr/local/bin/perl
2 # install_mod.cgi
3 # Download and install a webmin module
4
5 require './webmin-lib.pl';
6 if ($ENV{REQUEST_METHOD} eq "POST") { &ReadParseMime(); }
7 else { &ReadParse(); $no_upload = 1; }
8
9 $| = 1;
10 $theme_no_table = 1 if ($in{'source'} == 2 || $in{'source'} == 4);
11 &ui_print_header(undef, $text{'install_title'}, "");
12
13 if ($in{'source'} == 0) {
14         # from local file
15         &error_setup(&text('install_err1', $in{'file'}));
16         $file = $in{'file'};
17         if (!(-r $file)) { &inst_error($text{'install_efile'}); }
18         }
19 elsif ($in{'source'} == 1) {
20         # from uploaded file
21         &error_setup($text{'install_err2'});
22         $need_unlink = 1;
23         if ($no_upload) {
24                 &inst_error($text{'install_ebrowser'});
25                 }
26         $file = &transname(&file_basename($in{'upload_filename'}));
27         open(MOD, ">$file");
28         binmode(MOD);
29         print MOD $in{'upload'};
30         close(MOD);
31         }
32 elsif ($in{'source'} == 2 || $in{'source'} == 4) {
33         # from ftp or http url (possible third-party)
34         $url = $in{'source'} == 2 ? $in{'url'} : $in{'third'};
35         &error_setup(&text('install_err3', $url));
36         $file = &transname(&file_basename($url));
37         $need_unlink = 1;
38         my $error;
39         $progress_callback_url = $url;
40         if ($url =~ /^(http|https):\/\/([^\/]+)(\/.*)$/) {
41                 $ssl = $1 eq 'https';
42                 $host = $2; $page = $3; $port = $ssl ? 443 : 80;
43                 if ($host =~ /^(.*):(\d+)$/) { $host = $1; $port = $2; }
44                 &http_download($host, $port, $page, $file, \$error,
45                                \&progress_callback, $ssl);
46                 }
47         elsif ($url =~ /^ftp:\/\/([^\/]+)(:21)?\/(.*)$/) {
48                 $host = $1; $ffile = $3;
49                 &ftp_download($host, $ffile, $file, \$error, \&progress_callback);
50                 }
51         else { &inst_error($text{'install_eurl'}); }
52         &inst_error($error) if ($error);
53         }
54 elsif ($in{'source'} == 3) {
55         # from www.webmin.com
56         &error_setup($text{'install_err4'});
57         $in{'standard'} =~ /^\S+$/ || &error($text{'install_estandard'});
58         $need_unlink = 1;
59         my $error;
60
61         # Find the URL of the package
62         $mods = &list_standard_modules();
63         ref($mods) || &error(&text('standard_failed', $error));
64         local ($info) = grep { $_->[0] eq $in{'standard'} } @$mods;
65         $info || &error($text{'install_emissing'});
66         if ($config{'standard_url'}) {
67                 ($host, $port, $page, $ssl) = &parse_http_url(
68                                                 $config{'standard_url'});
69                 $host || &error($text{'standard_eurl'});
70                 }
71         else {
72                 ($host, $port, $page, $ssl) = ($standard_host, $standard_port,
73                                                $standard_page, $standard_ssl);
74                 }
75         ($host, $port, $page, $ssl) = &parse_http_url(
76                 $info->[2], $host, $port, $page, $ssl);
77         $progress_callback_url = $info->[2];
78         $file = &transname($info->[2]);
79         &http_download($host, $port, $page, $file, \$error,
80                        \&progress_callback, $ssl);
81         &inst_error($error) if ($error);
82         }
83
84 # Install the module(s)
85 $rv = &install_webmin_module($file, $need_unlink, $in{'nodeps'},
86                        $in{'grant'} ? undef : [ split(/\s+/, $in{'grantto'}) ]);
87 if (ref($rv)) {
88         @mdescs = @{$rv->[0]};
89         @mdirs = @{$rv->[1]};
90         @msizes = @{$rv->[2]};
91         }
92 else {
93         &inst_error($rv);
94         }
95
96 # Display something nice for the user
97 &read_file("$config_directory/webmin.catnames", \%catnames);
98 print "$text{'install_desc'} <p>\n";
99 print "<ul>\n";
100 for($i=0; $i<@mdescs; $i++) {
101         $mdirs[$i] =~ /\/([^\/]+)$/;
102         if (%minfo = &get_module_info($1)) {
103                 # Installed a module
104                 my $cat = $catnames{$minfo{'category'}};
105                 $cat = $text{"category_".$minfo{'category'}} if (!$cat);
106                 $cat = $text{"category_"} if (!$cat);
107                 print &text($minfo{'hidden'} ? 'install_line3' :
108                                 'install_line2', "<b>$mdescs[$i]</b>",
109                             "<tt>$mdirs[$i]</tt>", $msizes[$i], $cat,
110                             "../$minfo{'dir'}/"),
111                             "<br>\n";
112                 }
113         elsif (%tinfo = &get_theme_info($1)) {
114                 # Installed a theme
115                 print &text('themes_line', "<b>$mdescs[$i]</b>",
116                             "<tt>$mdirs[$i]</tt>", $msizes[$i]),
117                             "<br>\n";
118                 }
119         }
120 print "</ul><p>\n";
121
122 if (defined(&theme_post_change_modules)) {
123         &theme_post_change_modules();
124         }
125
126 &ui_print_footer("edit_mods.cgi?mode=install", $text{'mods_return'},
127                  "", $text{'index_return'});
128
129 sub inst_error
130 {
131 print "<b>$main::whatfailed : $_[0]</b> <p>\n";
132 &ui_print_footer("", $text{'index_return'});
133 exit;
134 }
135