#!/usr/local/bin/perl

use CGI;
$query = new CGI;

#Output the HTML header and body
print $query->header;
print $query->start_html(-title=>'File Upload',
                         -author=>'Jim Bevenhall');
#                         -BGCOLOR=>"ffffff");

print <<EOF;
<H1>File upload form</H1>
<br>
A confirmation will appear at the 
bottom of this page after you submit your file.  Scroll below the bottom 
buttons to view the confirmation. Press the <VAR>browse</VAR> button to 
choose a text file or graphical image to upload.

<br><I><B>Please note that if you mis-type the filename in this field the 
remote file will be over-written with a null file!  When in doubt, use the 
BROWSE button!
</B></I><br>
EOF
    ;

# Start a multipart form.
print $query->start_multipart_form;
#get username
print "<br>Please enter your login name:<BR>";
print $query->password_field(-name=>'password','',8,8);
print "<P>";
print "<BR>";

print "Enter the file to upload:",
    $query->filefield('filename','',45),"<BR>\n"; 
#param('filename") now holds the full path to the file on the local system

print $query->reset,$query->submit('submit','Process File');
print <<END;
 <HR> 
<a href="mailto:webmaster\@tripnet.se">
<b>This form created by: Jim Bevenhall</a> Content Developer, <a 
href="http://www.tripnet.se">Tripnet</a>.

<hr>

<b><FONT SIZE=2>Copyright &#169; 1996 Tripnet AB. All Rights 
Reserved.</FONT></B>
END
     ;
print $query->endform;

# Process the form if there is a file name entered
if ($file = $query->param('filename')) {   #if filename exists then
    $givenpass = $query->param('password');
    $org = $query->param('organization') ;
    &init_pass;  #initialize password file subroutine
    &checkpass($givenpass,$org);  #verify password subroutine
    &setpath($org); #set absolute destination path

    #display header in HTML file
    print "<HR>\n";
    print "<H2>$file</H2>\n";

    #If password is correct, read in remote filename and return its value
    if ($checkout eq '1')  {
        $fname = $query->param('fname');
        print "<B>Remote Filename:</b> $fname <br>";
        # Read a text file into the specified file
        open (OUTPUT, ">$path$fname") ||
            print "<BR><h1>File could not be created!</h2><BR>";   
        while (<$file>) {
            print OUTPUT $_;
            $lines++;
            $words += @words=split(/\s+/);
            $characters += length($_);
            }
         close (OUTPUT);

    #display file transfer information 
    print "<h2>Success!</h2>";
    print "<img src=/graphics/indent.gif>You have successfully transferred 
the file $file to $path$fname.<BR>";
    print "<img src=/graphics/indent.gif>This file contains:<BR>";
    print "<STRONG>Lines: </STRONG>$lines<BR>\n";
    print "<STRONG>Words: </STRONG>$words<BR>\n";

         }   else {
             if ($checkout eq '1') {
                  print "<h1>ERROR!!! The file you selected contains NO 
DATA</h1>";
             } 
             print "<BR>Please press <i>reset</i> to start over.<P>";
         }
}

sub init_pass  {
     open (PASSLIST, "/tmp/pass") ||
           die "FATAL ERROR:  could not access password file.";
     while ($pair = <PASSLIST>)  {
           chop($pair);
           $word = <PASSLIST>;
           chop($word);
           $login{$pair} = $word;
       }
}

sub checkpass  {
     local($givenpass, $org) = @_;  #name the local parameters
     $givenpass =~ s/\W.*//;   #get rid of everything after the first word
     $givenpass =~ tr/A-Z/a-z/;   #make password all lowercase
    
      if  ($login{$org} eq $givenpass)  {
          $checkout=1;
      }    else  {
          $checkout=0;
          print "<BR>The password you have given is not correct for the 
selected organization.";
          print "  Please try again.  If you are unsure of your password, 
please contact";
          print " Jim Bevenhall";
          print " during normal business hours.<br>";
          print "<I><B>Your file was not sent.</b></i>";
          $file=''; #resets the value for filename to clear form
     }
}

sub setpath  {

   #set absolute path according to supplied organization
   if ($org eq 'org1')  {
        $path='absolute path these files should reside here';
   } 
   #if [$org eq 'org2') {
   #    etc . . .

}
