#!/usr/bin/perl
#
# NAME
#   Upload - Send a file to the server's incoming-files directory.
#
# SYNOPSIS
#   <a href="/cgi/Upload">...</a>
#
# DESCRIPTION
#
# FILES
#
# AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub PB {print @_,"<br>\n"}		# Message with break and newline.
sub PL {print @_,"\n"}			# Message with newline.
sub PC {print "<!--@_-->\n"}	# Comment.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$dir = 'new';	# Where to put new files.
($me = $1) =~ s".*/"";
$V = $ENV{"V_$me"} || $ENV{"D_$me"} || 1;

$ENV{PATH} = ".:sh:abc:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin";

use CGI_Lite;
use CGI::Carp 'fatalsToBrowser';
use Socket;

if (!$HTTPhdr) {print "Content-type: text/html\n\n"; ++$HTTPhdr}

$cgi = new CGI_Lite ();
$cgi->set_platform('UNIX');
%data = $cgi->parse_form_data();

if ($V>1) {for $a (@ARGV) {PB("ARG \"$a\"")}}
if ($V>1) {for $var (sort keys %ENV ) {PB("ENV $var = \"$ENV{$var}\"")}}
if ($V>1) {for $var (sort keys %data) {PB("VAR $var = \"$data{$var}\"")}}

chomp($cwd = `pwd`);
PB("CWD \"$cwd\"") if $V>1;

PB("<hr><center><b>JC's file uploader</b></center><hr>");

PB("dir: \"$ENV{DOCUMENT_ROOT}/$dir\"") if $V>1;
if ($file = $data{file}) {
	$path = "$ENV{DOCUMENT_ROOT}/$dir/$file";
	PB("path: \"$path\"") if $V>1;
	if (!open(F,">$path")) {
		PB("<b>Can't write \"$path\" ($!)</b>");
	}
}
if ($msg = $data{msg}) {
	$mcmd = "sendmail -t webmaster";
	if (open(MSG,"| $mcmd")) {
		$raddr = $ENV{REMOTE_ADDR};
		$rhost = gethostbyaddr(inet_aton($raddr), AF_INET);
		print MSG "To: webmaster\n";
		print MSG "Subject: Messge from $raddr ($rhost)\n\n";
		print MSG "\n";
		print MSG "Someone at $raddr ($rhost) wrote:\n";
		print MSG "\n";
		print MSG $msg;
		close MSG;
		$msg = '';
	} else {
		VM("<b>Can't run \"$mcmd\" ($!)") if $V>0;
	}
}
if ($data{GetURL}) {
	PB("Download from URL ...") if $V>1;
	if ($URL = $data{URL}) {
		PB("Loading URL \"$URL\"") if $V>1;
		$cmd = "w3cat +TH -T20 $URL |";
		PB("Command \"$cmd\"") if $V>1;
		if (open(P,$cmd)) {
			PB("Running \"$cmd\"") if $V>1;
			$l = $n = 0;
			$inhdr = 1;
			for $line (<P>) {
				chomp $line;
				++$l;
				PB("$l: \"$line\"") if $V>1;
				if ($inhdr) {
					if (!$line) {
						PB("### End of header ###") if $V>1;
						$inhdr = 0;
					}
				} else {
					++$n;
					print F "$line\n";
				}
			}
			close P;
		} else {
			PB("<b>Can't exec \"$cmd\" ($!)</b>");
		}
		PB("Copied $n lines from <a href=\"$URL\">$URL</a> to \"<a href=\"/$dir/$file\">$ENV{DOCUMENT_ROOT}/$dir/$file\"</a>.<hr>") if $V>0;
	}
}
if ($data{GetTXT}) {
	PB("Download text ...") if $V>1;
	if ($text = $data{text}) {
		PB("Writing text to $path ...") if $V>1;
		$l = 0;
		for $line (split("\n",$text)) {
			++$l;
			PB("$l: \"$line\"") if $V>1;
			print F "$line\n";
		}
		$NEW = "http://$ENV{HTTP_HOST}/$dir/$file";
		PB("Wrote $l lines of text to \"<a href=\"/$dir/$file\">/$dir/$file</a>\".<hr>") if $V>0;
	}
}

PL("This will copy a URL or download some text into a file in the");
PL("$dir directory on the <a href=\"http://$ENV{HTTP_HOST}/\">$ENV{HTTP_HOST}</a> server.");
PL("Enter a file name, and either a URL or some text,");
PL("and press the appropriate button.");

PB("<form method=POST action=\"$ENV{SCRIPT_NAME}\">");
PB("\t<center>");
PB("\tFile: <input type=text size=60 name=file>");
PB("\t\t<input type=submit name=GetURL value=\"Download from URL\">");
PB("\tURL: <input type=text size=60 name=URL>");
PB("\t\tExplanatory message, sent to webmaster:");
PL("\t<textarea rows=10 cols=80 name=msg></textarea>");
PB("\t\t<input type=submit name=GetTXT value=\"Download this text:\">");
PL("\t<textarea rows=100 cols=100 name=text></textarea>");
PB("\t\t<input type=submit name=GetTXT value=\"Download the above text\">");
PB("\t</center>");
PB("</form>");

exit 0;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

