#!/usr/local/bin/perl
# upload.cgi
# Upload a file

require './file-lib.pl';
&header();
&ReadParseMime();

if (!$in{'file'}) {
	print "<p><b>$text{'upload_efile'}</b><p>\n";
	}
elsif (!-d $in{'dir'}) {
	print "<p><b>$text{'upload_edir'}</b><p>\n";
	}
else {
	$in{'file_filename'} =~ /([^\\\/]+)$/;
	$path = "$in{'dir'}/$1";
	&webmin_log("upload", undef, $path);
	&switch_acl_uid();
	if ($access{'ro'} || !&can_access($path)) {
		print "<p><b>",&text('upload_eperm', $path),"</b><p>\n";
		}
	elsif (!open(FILE, ">$path")) {
		print "<p><b>",&text('upload_ewrite', $path, $!),"</b><p>\n";
		}
	else {
		if ($in{'dos'}) {
			$in{'file'} =~ s/\r\n/\n/g;
			}
		print FILE $in{'file'};
		close(FILE);
		$info = &file_info_line($path);
		print "<script>\n";
		print "opener.document.FileManager.",
		      "upload_notify(\"$path\", \"$info\");\n";
		print "close();\n";
		print "</script>\n";
		}
	}
