3 # Upload multiple files
5 require './updown-lib.pl';
6 &error_setup($text{'upload_err'});
7 &ReadParse(\%getin, "GET");
9 &ReadParseMime($upload_max, \&read_parse_mime_callback, [ $upid ]);
10 $can_upload || &error($text{'upload_ecannot'});
13 $in{'dir'} || &error($text{'upload_edir'});
14 if (defined($in{'email_def'}) && !$in{'email_def'}) {
15 $in{'email'} =~ /\S/ || &error($text{'upload_eemail'});
16 $email = $in{'email'};
20 scalar(@uinfo = getpwnam($in{'user'})) ||
21 &error($text{'upload_euser'});
22 &can_as_user($in{'user'}) ||
23 &error(&text('upload_eucannot', $in{'user'}));
24 $in{'group_def'} || scalar(@ginfo = getgrnam($in{'group'})) ||
25 &error($text{'upload_egroup'});
26 $can_mode == 0 || $in{'group_def'} || &in_group(\@uinfo, \@ginfo) ||
27 &error($text{'upload_egcannot'});
31 if (&supports_users()) {
32 @uinfo = getpwnam($remote_user);
35 for($i=0; defined($d = $in{"upload$i"}); $i++) {
36 $f = $in{"upload${i}_filename"};
37 $found++ if ($d && $f);
39 $found || &error($text{'upload_enone'});
40 &can_write_file($in{'dir'}) ||
41 &error(&text('upload_eaccess', "<tt>$in{'dir'}</tt>", $!));
43 # Switch to the upload user
44 &switch_uid_to($uinfo[2], scalar(@ginfo) ? $ginfo[2] : $uinfo[3]);
46 # Create the directory if needed
47 if (!-d $in{'dir'} && $in{'mkdir'}) {
48 mkdir($in{'dir'}, 0755) || &error(&text('upload_emkdir', $!));
51 &ui_print_header(undef, $text{'upload_title'}, "");
53 # Save the actual files, showing progress
55 for($i=0; defined($d = $in{"upload$i"}); $i++) {
56 $f = $in{"upload${i}_filename"};
60 $path = "$in{'dir'}/$1";
65 print &text('upload_saving', "<tt>$path</tt>"),"<br>\n";
66 if (!&open_tempfile(FILE, ">$path", 1)) {
67 &error(&text('upload_eopen', "<tt>$path</tt>", $!));
69 &print_tempfile(FILE, $d);
70 &close_tempfile(FILE);
71 push(@uploads, $path);
73 print &text('upload_saved', &nice_size($st[7])),"<p>\n";
77 print &text('upload_unzipping', "<tt>$path</tt>"),"<br>\n";
81 local $qdir = quotemeta($dir);
82 local $qpath = quotemeta($path);
85 if ($path =~ /\.zip$/i) {
87 if (!&has_command("unzip")) {
88 $err = &text('upload_ecmd', "unzip");
91 open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && unzip -o $qpath)")." 2>&1 </dev/null |");
94 if (/^\s*[a-z]+:\s+(.*)/) {
103 elsif ($path =~ /\.tar$/i) {
104 # Un-compressed tar file
105 if (!&has_command("tar")) {
106 $err = &text('upload_ecmd', "tar");
109 open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && tar xvf $qpath)")." 2>&1 </dev/null |");
121 elsif ($path =~ /\.(lha|lhz)$/i) {
123 if (!&has_command("lha")) {
124 $err = &text('upload_ecmd', "lha");
127 open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && lha xf $qpath)")." 2>&1 </dev/null |");
130 if (/(\S[^\t]*\S)\s+\-\s+/) {
131 push(@files, "/".$1);
139 elsif ($path =~ /\.(tar\.gz|tgz|tar\.bz|tbz|tar\.bz2|tbz2)$/i) {
140 # Compressed tar file
141 local $zipper = $path =~ /bz(2?)$/i ? "bunzip2"
143 if (!&has_command("tar")) {
144 $err = &text('upload_ecmd', "tar");
146 elsif (!&has_command($zipper)) {
147 $err = &text('upload_ecmd', $zipper);
150 open(OUT, &webmin_command_as_user($uinfo[0], 0, "(cd $qdir && $zipper -c $qpath | tar xvf -)")." 2>&1 </dev/null |");
160 $fmt = $zipper eq "gunzip" ? "tgz" : "tbz2";
163 # Doesn't look possible
164 $err = $text{'upload_notcomp'};
166 &switch_uid_to($uinfo[2], scalar(@ginfo) ? $ginfo[2] : $uinfo[3]);
168 local $j = join("<br>",
169 map { " <tt>$_</tt>" } @files);
170 if ($in{'zip'} == 2) {
172 $ext{$path} = $text{'upload_deleted'}."<br>".$j;
175 $ext{$path} = $text{'upload_extracted'}."<br>".$j;
179 $ext{$path} = &text('email_eextract', $err);
181 $estatus = $err ? &text('email_extfailed', $err)
182 : &text('email_extdone_'.$fmt);
183 print &text('upload_unzipdone', $estatus),"<p>\n";
186 # Add to email message
187 $msg .= &text('email_upfile', $f)."\n";
188 $msg .= &text('email_uppath', $path)."\n";
189 $msg .= &text('email_upsize', &nice_size($st[7]))."\n";
191 $msg .= &text('email_upextract', $estatus)."\n";
196 # Switch back to root
200 if ($module_info{'usermin'}) {
201 &lock_file("$user_module_config_directory/config");
202 $userconfig{'dir'} = $in{'dir'};
203 &write_file("$user_module_config_directory/config", \%userconfig);
204 &unlock_file("$user_module_config_directory/config");
207 &lock_file("$module_config_directory/config");
208 $config{'dir_'.$remote_user} = $in{'dir'};
209 $config{'user_'.$remote_user} = $in{'user'};
210 $config{'group_'.$remote_user} = $in{'group_def'} ? undef
212 &write_file("$module_config_directory/config", \%config);
213 &unlock_file("$module_config_directory/config");
217 if ($email && $msg) {
218 $msg = $text{'email_upmsg'}."\n\n".$msg;
219 &send_email_notification($email, $text{'email_subjectu'}, $msg);
222 &webmin_log("upload", undef, undef, { 'uploads' => \@uploads });
224 &ui_print_footer("index.cgi?mode=upload", $text{'index_return'});