#!/usr/local/bin/perl5
alarm(600);

# This script was created 6/10/96
# Tripp Cox
# Webmaster - MindSpring Enterprises Inc
#
# Mutter, grumble, moan
#
# Modified to strip out PC and Macintosh file paths - 7/10/96
#
# Modified - Stephen Hait - shait@mindspring, 9/8/96
#            Change code to write and display uploaded files without paths
#            Minor change to BGCOLOR; changed fffff to ffffff
#
# Modified - Pat Dalzell - 4/29/97 - to use getpwnam
#

$HOME = (getpwnam("wfp63998"))[7];

$filedir="$HOME/www/incoming/";        
$fileurl="http://www.aspect-software.net/incoming/";

use CGI;
$query = new CGI;
print $query->header;
print $query->start_html("File Upload Example");


print <<EOF;
<BODY BGCOLOR="#ffffff">       
<CENTER><H1>Public Exchange Area</H1></CENTER>
<HR>
<STRONG>Uploading only works with Netscape 2.0 and above</STRONG>
<P>
Select the <VAR>browse</VAR> button to choose a file
to upload.
EOF
    ;

print $query->start_multipart_form;
print "<H3>Enter the file to upload</H3><BR>",
    $query->filefield('filename','',45),"<BR>\n";   # filename from form text
print $query->reset,$query->submit('submit','Upload');
print $query->endform;


opendir(DIR, "$filedir");       # $filedir from constant above       
local(@files) = readdir(DIR);   # load @files array from readdir(DIR)
    for (@files) {
	if (/\\/) {
        $oldfile = $_;          # store current element in $oldfile
        @path = split (/\\/,$oldfile); # store all path elements in @path array
        $newfile = pop(@path);  # store file name w/o path to $newfile
        # rename old filename with path info to just plain filename
        rename("$filedir"."$oldfile","$filedir"."$newfile");
	}
    next if (/^\./);            # bypass listing for current dir "."
    next if (/^index/);         # or file starting with "index"     
    print "<A HREF = "."$fileurl"."$_".">$_</A><P>\n"; # output html link to file
    }


if ($file = $query->param('filename')) {
    print "<HR>\n";
    $newfile = $file;
    if (/\\/, $file) {
    	$oldfile = $file;
      @path = split(/\\/, $oldfile);
      $newfile = pop(@path);
    }
    print "<A HREF = "."$fileurl"."$newfile"."><H2>$newfile</H2></A>\n";
    open(OUTFILE, ">$filedir"."$newfile");
    while (<$file>) { 
    print OUTFILE "$_";
    }
    close OUTFILE;
}

print <<EOF;
<HR>
</BODY>
EOF
    ;
print $query->end_html;
