Handle hostnames with upper-case letters
[webmin.git] / software / install_pack.cgi
1 #!/usr/local/bin/perl
2 # install_pack.cgi
3 # Install a package from some source
4
5 require './software-lib.pl';
6 if ($ENV{REQUEST_METHOD} eq "POST") {
7         &ReadParse(\%getin, "GET");
8         &ReadParseMime(undef, \&read_parse_mime_callback, [ $getin{'id'} ]);
9         }
10 else {
11         &ReadParse();
12         $no_upload = 1;
13         }
14 &error_setup($text{'install_err'});
15
16 if ($in{source} >= 2) {
17         &ui_print_unbuffered_header(undef, $text{'install_title'}, "", "install");
18         }
19 else {
20         &ui_print_header(undef, $text{'install_title'}, "", "install");
21         }
22
23 if ($in{source} == 0) {
24         # installing from local file (or maybe directory)
25         if (!$in{'local'})
26                 { &install_error($text{'install_elocal'}); }
27         if (!-r $in{'local'} && !-d $in{'local'} && $in{'local'} !~ /\*|\?/)
28                 { &install_error(&text('install_elocal2', $in{'local'})); }
29         $source = $in{'local'};
30         $pfile = $in{'local'};
31         $need_unlink = 0;
32         }
33 elsif ($in{source} == 1) {
34         # installing from upload .. store file in temp location
35         if ($no_upload) {
36                 &install_error($text{'install_eupload'});
37                 }
38         $in{'upload_filename'} =~ /([^\/\\]+$)/;
39         $pfile = &tempname("$1");
40         &open_tempfile(PFILE, ">$pfile", 0, 1);
41         &print_tempfile(PFILE, $in{'upload'});
42         &close_tempfile(PFILE);
43         $source = $in{'upload_filename'};
44         $need_unlink = 1;
45         }
46 elsif ($in{source} == 2) {
47         # installing from URL.. store downloaded file in temp location
48         $in{'url'} = &convert_osdn_url($in{'url'});
49         $in{'url'} =~ /\/([^\/]+)\/*$/;
50         $pfile = &tempname("$1");
51         local $error;
52         $progress_callback_url = $in{'url'};
53         if ($in{'url'} =~ /^(http|https):\/\/([^\/]+)(\/.*)$/) {
54                 # Make a HTTP request
55                 $ssl = $1 eq 'https';
56                 $host = $2; $page = $3; $port = $ssl ? 443 : 80;
57                 if ($host =~ /^(.*):(\d+)$/) { $host = $1; $port = $2; }
58                 &http_download($host, $port, $page, $pfile, \$error,
59                                \&progress_callback, $ssl);
60                 }
61         elsif ($in{'url'} =~ /^ftp:\/\/([^\/]+)(:21)?(\/.*)$/) {
62                 $host = $1; $file = $3;
63                 &ftp_download($host, $file, $pfile, \$error,
64                               \&progress_callback);
65                 }
66         else {
67                 &install_error(&text('install_eurl', $in{'url'}));
68                 }
69         &install_error($error) if ($error);
70         $source = $in{'url'};
71         $need_unlink = 1;
72         }
73 elsif ($in{'source'} == 3) {
74         # installing from some update system
75         &clean_environment();
76         $in{'update'} =~ /\S/ || &error($text{'install_eupdate'});
77         @packs = &update_system_install($in{'update'}, \%in);
78         &reset_environment();
79
80         print &ui_hr() if (@packs);
81         foreach $p (@packs) {
82                 local @pinfo = &show_package_info($p);
83                 }
84         &webmin_log($config{'update_system'}, "install", undef,
85                     { 'packages' => \@packs } ) if (@packs);
86
87         if ($in{'caller'} && &foreign_check("webmin")) {
88                 # Software installed - refresh installed flag cache
89                 &foreign_require("webmin", "webmin-lib.pl");
90                 ($inst, $changed) =
91                         &webmin::build_installed_modules(0, $in{'caller'});
92                 if (@$changed && defined(&theme_post_change_modules)) {
93                         &theme_post_change_modules();
94                         }
95                 }
96
97         if ($in{'return'}) {
98                 &ui_print_footer($in{'return'}, $in{'returndesc'});
99                 }
100         else {
101                 &ui_print_footer("", $text{'index_return'});
102                 }
103         exit;
104         }
105
106 # Check validity
107 if (!&is_package($pfile)) {
108         if (-d $pfile) {
109                 &install_error(&text('install_edir', &package_system()));
110                 }
111         else {
112                 # attempt to uncompress
113                 local $unc = &uncompress_if_needed($pfile, $need_unlink);
114                 if ($unc ne $pfile) {
115                         # uncompressed ok..
116                         if (!&is_package($unc)) {
117                                 &unlink_file($unc);
118                                 &install_error(&text('install_ezip',
119                                              &package_system()));
120                                 }
121                         $pfile = $unc;
122                         }
123                 else {
124                         # uncompress failed.. give up
125                         #unlink($pfile) if ($need_unlink);
126                         &install_error(&text('install_efile', &package_system()));
127                         }
128                 }
129         }
130
131 # ask for package to install and install options
132 @rv = &file_packages($pfile);
133
134 print &ui_form_start("do_install.cgi");
135 print &ui_hidden("file", $pfile);
136 print &ui_hidden("need_unlink", $need_unlink);
137 print &ui_table_start($text{'install_header'}, undef, 4);
138
139 # Packages to install
140 $plist = "";
141 foreach (@rv) {
142         ($p, $d) = split(/\s+/, $_, 2);
143         if ($d) {
144                 $plist .= &html_escape($d)," (",&html_escape($p),")<br>\n";
145                 }
146         else {
147                 $plist .= &html_escape($p),"<br>\n";
148                 }
149         }
150 print &ui_table_row($text{'install_packs'}, $plist, 3);
151
152 # Type-specific options
153 &install_options($pfile, $p);
154
155 print &ui_table_end();
156 print &ui_form_end([ [ undef, $text{'install_ok'} ] ]);
157
158 &ui_print_footer("", $text{'index_return'});
159
160 sub install_error
161 {
162 print "<br><b>$main::whatfailed : $_[0]</b> <p>\n";
163 &ui_print_footer("", $text{'index_return'});
164 exit;
165 }
166
167