#!/usr/local/bin/perl

##########################################################################
#
#	My Downloader V1.1	Copyright 1998, Jim Melanson
#		www.thebeaches.to/jmwd/index.html
#		webmaster@thebeaches.to
#		416-261-8840
#
#	Please read the disclaimer and terms and conditions of use in the
#	Readme file that accompanied this program. Alteration of use of
#	this script constitutes acceptance of the terms and condition.
#
##########################################################################

require '/home/benescu/public_html/cgi-bin/Downloader/attack.cgi';
require '/home/benescu/public_html/cgi-bin/Downloader/my_config.cgi';

$ScriptURL = "http://$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}";

&Parse;
&Date;
print "Content-type: text/html\n\n";

if($ENV{'QUERY_STRING'} eq 'admin') {&Admin}
    elsif($ENV{'QUERY_STRING'} eq 'viewlog') {&ViewLog}
    elsif($ENV{'QUERY_STRING'} eq 'viewarchive') {&ViewArchive}
    elsif($ENV{'QUERY_STRING'} eq 'addarchive') {&AddArchive}
    elsif($ENV{'QUERY_STRING'} eq 'purgearchive') {&PurgeArchive}
    elsif($ENV{'QUERY_STRING'} eq 'setup') {&Setup}
    elsif($FORM{'action'} eq 'removelog') {&RemoveLog}
    elsif($FORM{'action'} eq 'setupfinal') {&SetupFinal}
    elsif($FORM{'action'} eq 'updateform') {&UpdateForm}
    elsif($FORM{'action'} eq 'updatefinal') {&UpdateFinal}
    elsif($FORM{'action'} eq 'remove') {&RemoveFile}
    elsif($FORM{'action'} eq 'add') {&AddFile}
    elsif($FORM{'action'} eq 'confirm') {&DownloadConfirm}
    elsif($FORM{'action'} eq 'download') {&DownloadFinal}
	else{&Display}
exit;
##########################################################################
#				SUBROUTINES
##########################################################################

sub Display {
    &PrintHead;
    print "<HR><HR><H2 ALIGN=\"CENTER\">$SiteName<BR>Files For\n";
    print "Download</H2><HR><HR><BLOCKQUOTE>Thank you for your interest\n";
    print "in downloading files from our server. Below you will find a\n";
    print "listing of the files available for download. <P>Select the\n";
    print "file you want then fill out the form and press submit. The\n";
    print "informaiton gathered from the form will be held in strictest\n";
    print "confidence. You will receive a confirmation page to verify\n";
    print "the file you want and the information you entered.<P>At the\n";
    print "present time you can only download one file at a time.\n";
    print "<BR><BR><FORM ACTION=\"$ScriptURL\" METHOD=\"POST\">\n";
    print "<CENTER><TABLE WIDTH=400 BORDER=0>\n";

	open(LINES, "<$Data/files.list") || die "Can't open file list\n";
	@List = <LINES>;
	close(LINES);
	chomp(@List);
	if(!$List[0]) {
	    @List = restore("$Data/files.list");
	}
	@Contents = sort {$a cmp $b} @List;

	if(open(THATDATA, ">$Data/files.list")) {
	    flock(THATDATA, '2');
	    for($list_a = 0; $list_a <= $#Contents; $list_a++) {
		print THATDATA "$Contents[$list_a]\n";
	    }
	    close(THATDATA);
	}
	@Contents = ();
	if(open(THISDATA, "<$Data/files.list")) {
	    flock(THISDATA, '2');
	    @Contents = <THISDATA>;
	    close(THISDATA);
	    chomp(@Contents);
	}

	$num = @Contents;
	for($a = 0; $a < $num; $a++) {
	    &SplitData($Contents[$a]);
	
	    print "<TR VALIGN=\"TOP\"><TD><INPUT TYPE=\"RADIO\"\n";
	    print "NAME=\"downloadfile\" VALUE=\"$a\"></TD><TD>\n";

	    if($FilePage) {
	        print "<SMALL><B><A HREF=\"$RootURL$FilePage\">\n";
		print "$FileName</A></B>\n";
	    }
		else {
		    print "<SMALL><B>$FileName</B>\n";
		}

	    if($FileSize) {
		print "<BR>File Size: $FileSize<BR>\n";
	    }
		else {print "<BR>File size unavailable<BR>\n"}

	    if($FileType) {
		print "<I>File Format: $FileType</I><BR>\n";
	    }

	    if($FileText) {
	 	print "<B>File Description:</B><BR>$FileText<BR>\n";
	    }
		else {print "No description available for this file\n"}

	    print "</TD></TR>\n";
	}

    print "</TABLE><BR><BR><TABLE WIDTH=\"400\" BORDER=0>\n";
    print "<TR><TD ALIGN=\"RIGHT\">Name:</TD><TD><INPUT TYPE=\"TEXT\"\n";
    print "NAME=\"name\" SIZE=\"30\"></TD></TR>\n";
    print "<TR><TD ALIGN=\"RIGHT\">E-mail:</TD><TD><INPUT TYPE=\"TEXT\"\n";
    print "NAME=\"email\" SIZE=\"30\"></TD></TR>\n";
    print "<TR><TD ALIGN=\"RIGHT\">Company:</TD><TD><INPUT TYPE=\"TEXT\"\n";
    print "NAME=\"company\" SIZE=\"30\"></TD></TR></TABLE>\n";
    print "<INPUT TYPE=\"SUBMIT\" VALUE=\"Submit\">\n";
    print "<INPUT TYPE=\"RESET\" VALUE=\"Reset\"><INPUT TYPE=\"HIDDEN\"\n";
    print "NAME=\"action\" VALUE=\"confirm\"></FORM></CENTER>\n";
    &PrintFoot;
}

sub DownloadConfirm {
  if(&CheckEmail($FORM{'email'})) {
    $FileNumb = int($FORM{'downloadfile'});
    &PrintHead;
    print "<HR><HR><H2 ALIGN=\"CENTER\">$SiteName<BR>Download\n";
    print "Confirmation</H2><HR><HR>\n";
    print "<BLOCKQUOTE>You have chosen the following file for download:\n";

	open(DATA, "<$Data/files.list") || die "Can't open file list\n";
	@Contents = <DATA>;
	close(DATA);
	if(!$Contents[0]) {
	    @Contents = restore("$Data/files.list");
	}

	$Content = @Contents[$FileNumb];
	    &SplitData($Content);

    print "<CENTER><TABLE WIDTH=\"500\" BORDER=0><TR><TD><SMALL>\n";
    	if($FileName) {print "<B>$FileName</B><BR>\n"}
	if($FileSize) {print "File Size: $FileSize<BR>\n"}
	if($FileType) {print "File Type: $FileType<BR>\n"}
	if($FileText) {print "File Description:<BR>$FileText<BR>\n"}

    print "</SMALL></TD></TR></TABLE><BR><BR><TABLE BORDER=0>\n";
    print "<TR><TD>Your Name:</TD><TD>$FORM{'name'}</TD></TR>\n";
    print "<TR><TD>Your E-mail:</TD><TD>$FORM{'email'}</TD></TR>\n";
    print "<TR><TD>Your Company:</TD><TD>$FORM{'company'}</TD></TR>\n";
    print "</TABLE><BR><BR>If this information is correct, proceed to\n";
    print "the download page by pressing the confirmation button below.\n";
    print "If not, use the back button on your browser to correct the\n";
    print "information.<BR><FORM ACTION=\"$ScriptURL\" METHOD=\"POST\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"download\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"downloadnumber\"\n";
    print "VALUE=\"$FileNumb\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"name\" VALUE=\"$FORM{'name'}\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"email\"\n";
    print "VALUE=\"$FORM{'email'}\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"company\"\n";
    print "VALUE=\"$FORM{'company'}\">\n";
    print "<INPUT TYPE=\"SUBMIT\" VALUE=\"CONFIRM REGISTRATION\">\n";
    print "</FORM></CENTER><BR><BR>\n";
    &PrintFoot;
  }
  else {
    $Problem = "the e-mail address you entered does not appear to be in a valid e-mail address format.";
    &Error($Problem);
  }
}

sub DownloadFinal {
    $FileNumb = int($FORM{'downloadnumber'});
    if((!$FORM{'name'}) || (!$FORM{'email'})) {
	$Problem = "you did not enter either your name or email. Both are" .
		"are required before you can download a file. Please use" .
		"the back button on your browser to enter both pieces of" .
		"information.";
	&Error($Problem);
    }
    else {
    open(DATA, "<$Data/files.list") || die "Can't open file list\n";
    @Contents = <DATA>;
    close(DATA);
    if(!$Contents[0]) {
	@Contents = restore("$Data/files.list");
    }

    $Content = @Contents[$FileNumb];
    &SplitData($Content);

    $NewUser = $FileName . "||" . $FORM{'name'} . "||" . $FORM{'email'} .
		"||" . $FORM{'company'} . "||" . $date . "||" . $time;
    open(LOGS, ">>$Data/downloadlog.list") 
		|| die "Can't write to log file\n";
    print LOGS "$NewUser\n";
    close(LOGS);

    &PrintHead;
    print "<HR><HR><H2>Registration Accepted -</H2><HR><HR><BLOCKQUOTE>\n";
    print "Thank you for registering to download the file <B>$FileName\n";
    print "</B>. Registration helps us track the popularity of files and\n";
    print "allows us to better serve our visitors.<P>The file you want\n";
    print "is ready. Simply press the \"Download\" button and the file\n";
    print "will be transfered to you.<P>Thankyou for downloading from\n";
    print "$SiteName and Best Wishes.<BR><BR><SMALL>\n";
    print "<A HREF=\"$RootURL$FileLink\"><B>[DOWNLOAD]</B></A>\n";
    print "</SMALL></BLOCKQUOTE><BR><BR><BR><BR>\n";
    &PrintFoot;
    }
}

sub Admin {
    &PrintHead;
    print "<HR><HR><H2 ALIGN=\"CENTER\">$SiteName<BR>Administration\n";
    print "Page</H2><HR><HR><BLOCKQUOTE>You have four options on this\n";
    print "page. They are:<UL><LI><A HREF=\"#ADD\">Add a file</A>\n";
    print "<LI><A HREF=\"#REMOVE\">Remove a file</A>\n";
    print "<LI><A HREF=\"#UPDATE\">Update a file</A>\n";
    print "<LI><A HREF=\"$ScriptURL?viewlog\">Review the log file</A>\n";
    print "</UL><BR><BR><A HREF=\"$HelpFile\">HELP FILE?</A><BR><BR>\n";
    print "<HR WIDTH=50%><BR><CENTER>\n";

#Add file
    print "<FORM ACTION=\"$ScriptURL\" METHOD=\"POST\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"add\">\n";
    print "<A NAME=\"ADD\"><CENTER><TABLE WIDTH=500 BORDER=0>\n";
    print "<TR><TD><SMALL><B>File Name:</B><BR>\n";
    print "<I>(This is an alias that the document is known by and not\n";
    print "the actual docuement name. For example, a document may be\n";
    print "\"artists.doc\" but the file name could be \"Resources for\n";
    print "Artists\".)</I><BR><BR><INPUT TYPE=\"TEXT\" NAME=\"filename\"\n";
    print "SIZE=\"50\"><BR><HR></SMALL></TD></TR>\n";

    print "<TR><TD><SMALL><B>File info page:</B><BR>\n";
    print "<I>(This is the page in your website that provides information\n";
    print "on the file to be downloaded. This page will most likely end\n";
    print "with an extension \".html\" or \".htm\". The path you will\n";
    print "enter here is relative to the Root directory. See the help\n";
    print "file for more info.)</I><BR><BR>$RootURL<INPUT TYPE=\"TEXT\"\n";
    print "NAME=\"filepage\" SIZE=\"40\"><BR><HR></SMALL></TD></TR>\n";

    print "<TR><TD><SMALL><B>File size:</B><BR>\n";
    print "<I>(This is the size of the file in Kb.)</I><BR><BR><INPUT\n";
    print "TYPE=\"TEXT\" NAME=\"filesize\" SIZE=\"5\"><BR><HR></SMALL></TD></TR>\n";

    print "<TR><TD><SMALL>File docment type:</B><BR>\n";
    print "<I>(This refers to the extension of the file as it is saved.\n";
    print "Examples are \".txt\", \".doc\", \".pdf\", \".rtf\", \".zip\",\n";
    print "etc.)</I><BR><BR><INPUT TYPE=\"TEXT\" NAME=\"filetype\"\n";
    print "SIZE=\"5\"><BR><HR></SMALL></TD></TR>\n";

    print "<TR><TD><SMALL><B>File location:</B><BR>\n";
    print "<I>(This is the location of the document on the server, where\n";
    print "it will be downloaded from. If you haven't placed the file on\n";
    print "the server, please do so now. The path you will enter here is\n";
    print "relative to the Root directory. See the help file for more\n";
    print "info.)</I><BR><BR>$RootURL<INPUT TYPE=\"TEXT\"\n";
    print "NAME=\"filelink\" SIZE=\"40\"><BR><HR></SMALL></TD></TR>\n";

    print "<TR><TD><SMALL><B>File description, in your own words:</B></SMALL><BR>\n";

    print "<TEXTAREA NAME=\"filetext\" cols=50 rows=15></TEXTAREA></TD>\n";
    print "</TR></TABLE><BR><B>USER:<INPUT TYPE=\"TEXT\" NAME=\"user\"\n";
    print "SIZE=\"20\"><BR>PASS:</B><INPUT TYPE=\"PASSWORD\"\n";
    print "NAME=\"pass\" SIZE=\"20\"><BR><BR><INPUT TYPE=\"SUBMIT\"\n";
    print "VALUE=\"SUBMIT\"><INPUT TYPE=\"RESET\" VALUE=\"RESET\">\n";
    print "</FORM></CENTER><BR><BR><A HREF=\"$HelpFile\">HELP FILE?</A>\n";
    print "<BR><BR><HR><BR><BR>\n";

#Remove
    print "<FORM ACTION=\"$ScriptURL\" METHOD=\"POST\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"remove\">\n";
    print "<A NAME=\"REMOVE\"><B>Choose the file you wish to REMOVE from\n";
    print "download access:</B><BR><TABLE BORDER=0 WIDTH=400>\n";

	open(LINES, "<$Data/files.list") || die "Can't open files list\n";
	@Contents = <LINES>;
	close(LINES);
	if(!$Contents[0]) {
	    @Contents = restore("$Data/files.list");
	}
	$num = @Contents;

	for($a = 0; $a < $num; $a++) {
	    &SplitData($Contents[$a]);
	    $File = "$RootURL$FileLink";
	    print "<TR><TD><SMALL><INPUT TYPE=\"RADIO\" NAME=\"removefile\"\n";
	    print "VALUE=\"$a\"><B>Remove File</B><BR><I>$FileName</I></B>\n";
            print "<BR>File Type = $FileType<BR><I><A HREF=\"$File\">\n";
	    print "$File</A></I><BR><BR></SMALL></TD></TR>\n";
	}

    print "</TABLE><BR><B>USER:<INPUT TYPE=\"TEXT\" NAME=\"user\"\n";
    print "SIZE=\"20\"><BR>PASS:</B><INPUT TYPE=\"PASSWORD\"\n";
    print "NAME=\"pass\" SIZE=\"20\"><BR><BR><INPUT TYPE=\"SUBMIT\"\n";
    print "VALUE=\"SUBMIT\"><INPUT TYPE=\"RESET\" VALUE=\"RESET\">\n";
    print "</FORM><BR><BR><HR><BR><BR>\n";

#Update
    print "<A NAME=\"UPDATE\"><FORM ACTION=\"$ScriptURL\" METHOD=\"POST\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"updateform\">\n";
    print "<A NAME=\"REMOVE\"><B>Choose the file you wish to UPDATE\n";
    print "information for:</B><BR><TABLE BORDER=0 WIDTH=400>\n";

	for($a = 0; $a < $num; $a++) {
	    &SplitData($Contents[$a]);
	    $File = "$RootURL$FileLink";
	    print "<TR><TD><SMALL><INPUT TYPE=\"RADIO\" NAME=\"updatefile\"\n";
	    print "VALUE=\"$a\"><B>Update File</B><BR><I>$FileName</I>\n";
   	    print "</B><BR>File Type = $FileType<BR><I>\n";
	    print "<A HREF=\"$File\">$File</A></I><BR><BR></SMALL></TD></TR>\n";
	}

    print "</TABLE><BR><B>USER:<INPUT TYPE=\"TEXT\" NAME=\"user\"\n";
    print "SIZE=\"20\"><BR>PASS:</B><INPUT TYPE=\"PASSWORD\"\n";
    print "NAME=\"pass\" SIZE=\"20\"><BR><BR><INPUT TYPE=\"SUBMIT\"\n";
    print "VALUE=\"SUBMIT\"><INPUT TYPE=\"RESET\" VALUE=\"RESET\">\n";
    print "</FORM><BR><BR><A HREF=\"$HelpFile\">HELP FILE?</A><BR><BR>\n";
    print "<BR><BR>\n";
    &PrintFoot;
}



sub RemoveFile {

    local($cushion) = 0;

    if(&PassCheck($FORM{'user'}, $FORM{'pass'})) {
	$RemoveFile = $FORM{'removefile'};

	open(DATA, "<$Data/files.list") || die "Can't read files list\n";
	@Contents = <DATA>;
	close(DATA);
	if(!$Contents[0]) {
	    @Contents = restore("$Data/files.list");
	}
	$num = @Contents;
	open(DATA, ">$Data/files.list") || die "Can't read files list\n";
	for($a = 0; $a < $num; $a++) {
	    $Content = @Contents[$a];
	    chop($Content);
	    if($a == $RemoveFile) {
	    chop($Content);
	    &SplitData($Content);
	    $cushion = 1;
	    }
	    else {print DATA "$Content\n"}
	}
	close(DATA);
	backup("$Data/files.list");

	if($cushion) {
    	    &PrintHead;
	    print "<HR><HR><H2>Successful Removal -</H2><HR><HR>The\n";
 	    print "file <B>\"$FileName\"</B> was\n";
	    print "successfully removed from the database of available\n";
	    print "files.<P>It is recommended that you now remove or move\n";
	    print "this file from it's present location as it is still\n";
	    print "accessible from the direct entry of the URL. Either\n";
	    print "use FTP or Telnet to remove this file. If you don't\n";
	    print "know how, contact your SysAdmin or the person that\n";
	    print "installed download control to do this for you.<P>The\n";
	    print "full URL for the file in questions is:<P>\n";
	    print "<A HREF=\"$RootURL$FileLink\">$RootURL$FileLink</A>\n";
	    print "<P>Thank You.\n";
	    &PrintFoot;
	}
	else {
	    $Problem =  "the file you selected was not removed from the" .
		"database of available files. Please <A HREF=" .
		"\"mailto:$AdminEmail\">email</A> the webmaster" .
		" and advise him the error was \"FILE NOT REMOVED\"" .
		" and what file you tried to delete.";
	    &Error($Problem);
	}
    }

    else {
	$Problem = "your Password or User ID did not match our database." .
		" Please use the back button on your browser and try again";
	&Error($Problem);
  }

} 

sub AddFile {
    $setlink = $FORM{'filelink'};
    if(&PassCheck($FORM{'user'}, $FORM{'pass'})) {
        unless($FORM{'filesize'}) {
	    $FORM{'filesize'} = (-s '$RootURL$setlink');
        }
	$NewFile = $FORM{'filename'} . "||" . $FORM{'filepage'} . "||" .
		   $FORM{'filesize'} . "||" . $FORM{'filetype'} . "||" .
		   $FORM{'filelink'} . "||" . $FORM{'filetext'};

      	if(open(DATA, ">>$Data/files.list")) {
	print DATA "$NewFile\n";
	close(DATA);
   	backup("$Data/files.list");
	&PrintHead;
	print "<HR><HR><H2>Successful Addition -</H2><HR><HR>The file\n";
	print "$FORM{'filename'} was successfully added to the database\n";
	print "of available files. Please ensure that the following\n";
	print "information, which you provided for this file is\n";
	print "correct:<P><OL><LI><B>Information Page</B>:\n";
	print "<I>$RootURL$FORM{'filepage'}</I>\n";
	print "<LI><B>File Size</B>: <I>$FORM{'filesize'}</I>\n";
	print "<LI><B>File Type</B>: <I>$FORM{'filetype'}</I>\n";
	print "<LI><B>File Link</B>: <I>$RootURL$FORM{'filelink'}</I>\n";
	    $ShowFileText = $FORM{'filetext'};
	    $ShowFileText =~ s/LBCR//g;
	print "<LI><B>Description</B>: <I>$ShowFileText</I></OL><BR><BR>\n";
	print "If this information is incorrect then you will have to\n";
	print "<A HREF=\"$ScriptURL?admin\">update the info</A> from the\n";
	print "administration page.<P>You may now go to the\n";
	print "<A HREF=\"$ScriptURL\">Download Page</A><BR><BR>\n";
	&PrintFoot;
	}
	else {
	    $Problem = "we could not add the file to the list.";
	    &Error($Problem);
	}
    }
    else {
	$Problem = "your Password or User ID did not match our database." .
	     "Please use the back button on your browser and try again";
	&Error($Problem);
    }

}

sub UpdateForm {

    if(&PassCheck($FORM{'user'}, $FORM{'pass'})) {
    $UpdateFile = int($FORM{'updatefile'});
    open(DATA, "<$Data/files.list") || die "Can't open file list\n";
    @Contents = <DATA>;
    close(DATA);
    if(!$Contents[0]) {
    	@Contents = restore("$Data/files.list");
    }

    $Content = @Contents[$UpdateFile];
    &SplitData($Content);

    &PrintHead;
    print "<HR><HR><H2>Update file information -</H2><HR><HR>The\n";
    print "following file has been selected for updating. Make any\n";
    print "changes necessary and then press the SUBMIT button.<BR>\n";
    print "<BR><A HREF=\"$HelpFile\">HELP FILE?</A><BR><BR>\n";
    print "<FORM ACTION=\"$ScriptURL\" METHOD=\"POST\"><INPUT\n";
    print "TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"updatefinal\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"updatenumber\"\n";
    print "VALUE=\"$UpdateFile\">\n";
    print "<CENTER><TABLE BORDER=0>\n";
    print "<TR><TD><B>Old file name</B>$FileName<BR><BR>\n";
    print "<B>New file name</B><BR><I>(This is an alias that the document\n";
    print "is known by and not the actual docuement name. For example,\n";
    print "a document may be \"artists.doc\" but the file name could\n";
    print "be \"Resources for Artists\".)</I><BR><BR><INPUT\n";
    print "TYPE=\"TEXT\" NAME=\"filename\" SIZE=\"50\"\n";
    print "VALUE=\"$FileName\"></TD></TR>\n";
    print "<TR><TD><BR><BR><B>Old information page location</B>\n";
    print "<BR>$RootURL$FilePage<BR><BR><B>New information page\n";
    print "location</B><BR><I>(This is the page in your website that\n";
    print "provides information on the file to be downloaded. This page\n";
    print "will most likely end with an extension \".html\" or \".htm\".\n";
    print "The path you will enter here is relative to the Root\n";
    print "directory. See the <A HREF=\"$HelpFile\">help file</A> for\n";
    print "more info.)</I><BR><BR>$RootURL<INPUT TYPE=\"TEXT\"\n";
    print "NAME=\"filepage\" SIZE=\"50\" VALUE=\"$FilePage\"></TD></TR>\n";
    print "<TR><TD><BR><BR><B>Old file size</B>$FileSize<BR><BR>\n";
    print "<B>New file size</B><BR><I>(This is the size of the file in\n";
    print "Kb.)</I><BR><BR><INPUT TYPE=\"TEXT\" NAME=\"filesize\"\n";
    print "SIZE=\"5\" VALUE=\"$FileSize\">Kb</TD></TR>\n";
    print "<TR><TD><BR><BR><B>Old file type</B>$FileType<BR><BR>\n";
    print "<B>New file type</B><BR><I>(This refers to the extension of\n";
    print "the file as it is saved. Examples are \".txt\", \".doc\",\n";
    print "\".pdf\", \".rtf\", \".zip\", etc.)</I><BR><BR><INPUT\n";
    print "TYPE=\"TEXT\" NAME=\"filetype\" SIZE=\"5\"\n";
    print "VALUE=\"$FileType\"></TD></TR>\n";
    print "<TR><TD><BR><BR><B>Old file location</B><BR>$RootURL$FileLink\n";
    print "<BR><BR><B>New file location</B><BR><I>(This is the location\n";
    print "of the document on the server, where it will be downloaded\n";
    print "from. If you haven't placed the file on the server, please do\n";
    print "so now. The path you will enter here is relative to the Root\n";
    print "directory. See the help file for more info.)</I><BR>\n";
    print "<BR>$RootURL<INPUT TYPE=\"TEXT\" NAME=\"filelink\"\n";
    print "SIZE=\"50\" VALUE=\"$FileLink\"></TD></TR>\n";

    print "</TABLE><BR><TABLE BORDER=0><TR><TD><B>Old\n";
    print "Description:</B><BR>$FileText<BR><BR>\n";
    print "<B>New Description:</B><BR><I>(This is the description of\n";
    print "the file that you want the viewer to see on the download page.\n";
    print "You can either edit the present content or remove it all and\n";
    print "start over again.)</I><BR><BR><TEXTAREA NAME=\"filetext\"\n";
    print "COLS=50 ROWS=15>\n";
    print "$FileText</TEXTAREA></TD></TR></TABLE><BR><BR>\n";
    print "<B>USER:<INPUT TYPE=\"TEXT\" NAME=\"user\" SIZE=\"20\">\n";
    print "<BR>PASS:</B><INPUT TYPE=\"PASSWORD\" NAME=\"pass\"\n";
    print "SIZE=\"20\"><BR><BR><INPUT TYPE=\"SUBMIT\" VALUE=\"SUBMIT\">\n";
    print "<INPUT TYPE=\"RESET\" VALUE=\"RESET\"></FORM></CENTER><BR>\n";
    print "<BR><A HREF=\"$HelpFile\">HELP FILE?</A><BR><BR>\n";
    &PrintFoot;
    } 
    else {
	$Problem = "your Password or User ID did not match our database." .
	    "Please use the back button on your browser and try again";
	&Error($Problem);
    }

} 

sub UpdateFinal {
    if(&PassCheck($FORM{'user'}, $FORM{'pass'})) {
    local($cushion) = 0;
    $UpdateFile = int($FORM{'updatenumber'});
    $size = $FORM{'filesize'};
    unless($size) {
	$size = (-s '$RootURL$size');
    }
    $NewFile = $FORM{'filename'} . "||" . $FORM{'filepage'} . "||" .
	       $size . "||" . $FORM{'filetype'} . "||" .
	       $FORM{'filelink'} . "||" . $FORM{'filetext'};
	open(DATA, "<$Data/files.list") || die "Can't read files list\n";
	@Contents = <DATA>;
	close(DATA);
	if(!$Contents[0]) {
	    @Contents = restore("$Data/files.list");
	}
	$num = @Contents;
	open(DATA, ">$Data/files.list") || die "Can't write to files list\n";
	for($a = 0; $a < $num; $a++) {
	    $Content = @Contents[$a];
	    if($a == $UpdateFile) {
		$cushion = 1;
	    }
	    else{
		chop($Content);
		print DATA "$Content\n";
	    }
	}
	close(DATA);
	open(DATA, ">>$Data/files.list") || die "Can't read files list\n";
	print DATA "$NewFile\n";
	close(DATA);
	backup("$Data/files.list");
	&PrintHead;
	if($cushion) {
	print "<HR><HR><H2>Successful Update -</H2><HR><HR>The file\n";
        print "$FORM{'filename'} was succesfully updated. You may now\n";
	print "return to <A HREF=\"$ScriptURL?admin\">the admin page\n";
	print "</A> or go to the <A HREF=\"$ScriptURL\">Download Page\n";
	print "</A><BR><BR>\n";
	&PrintFoot;
	}
	    else {
	    $Problem = "the file was not updated. Please try again or contact the system administrator.";
	    &Error($Problem);
	    }
	}

    else {
	$Problem = "your Password or User ID did not match our database." .
	     "Please use the back button on your browser and try again.";
	&Error($Problem);
    }
}

sub Error {
    $cause = @_[0];
    &PrintHead;
    print "<H2>Error- </H2><HR><B><CENTER>An error has occured.<P>\n";
    print "You have received this error because $cause.<P>Please use\n";
    print "the back button on your browser to try again or\n";
    print "<A HREF=\"mailto:$AdminEmail\">e-mail the webmaster</A> to\n";
    print "request assistance.<P>Thank You.<BR><BR>\n";
    &PrintFoot;
}

sub ViewLog {
    &PrintHead;
    print "<HR><HR><H2 ALIGN=\"CENTER\">$SiteName<BR>Download Log</H2>\n";
    print "<HR><HR><BLOCKQUOTE>Here is the summary of files and people\n";
    print "who have used this program to download files from your\n";
    print "web site.</BLOCKQUOTE><BR><BR><CENTER><TABLE WIDTH=400\n";
    print "BORDER=2>\n";

	open(DATA, "<$Data/downloadlog.list") 
		|| die "Can't open file list\n";
	@Contents = <DATA>;
	close(DATA);
	local($a) = 1;
	foreach $Content (@Contents) {
	    @File = split(/\|\|/, $Content);
	    $FileName = $File[0];
	    $UserName = $File[1];
	    $UserEmail = $File[2];
	    $Company = $File[3];
	    $DLDate = $File[4];
	    $DLTime = $File[5];
	    print "<TR><TD><B>Entry Number: <I>$a</I></B><BR>\n";
      	    print "<I>$FileName</I><BR>$UserName<BR><I>\n";
	    print "<A HREF=\"mailto:$UserEmail\">$UserEmail</A>\n";
	        if($Company) {print "<BR>Company: $Company<BR>\n"}
	    print "<BR>Downloaded on $DLDate, at $DLTime<BR><HR><BR>\n";
	    print "</TD></TR>\n";
	    $a++;
	}
    print "</TABLE><BR><BR><TABLE WIDTH=400 BORDER=8><TR><TD>\n";
    print "If you would like to delete entries, you must do so entering a\n";
    print "\"From - To\" range of numbers according to the above entry\n";
    print "numbers. If you would like to removea single entry, just enter\n";
    print "that number in both fields.<BR><BR><CENTER>\n";
    print "<FORM ACTION=\"$ScriptURL\" METHOD=\"POST\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"removelog\">\n";
    print "<B>FROM:</B><INPUT TYPE=\"TEXT\" NAME=\"first\" SIZE=4>&nbsp\n";
    print "&nbsp;&nbsp;<B>To:</B><INPUT TYPE=\"TEXT\" NAME=\"last\" SIZE=4>\n";
    print "<BR><BR><B>USER:<INPUT TYPE=\"TEXT\" NAME=\"user\" SIZE=\"20\">\n";
    print "<BR>PASS:</B><INPUT TYPE=\"PASSWORD\" NAME=\"pass\"\n";
    print "SIZE=\"20\"><BR><BR>\n";
    print "<BR><BR><INPUT TYPE=\"SUBMIT\" VALUE=\"Remove\"></FORM></CENTER>\n";
    print "</TD></TR></TABLE><BR><BR>\n";
    print "If you are ready, press this button<BR>to add these log\n";
    print "entries to the archive. They will be<BR>erased from the\n";
    print "download log but can still be viewed<BR>in the archive.\n";

    print "<FORM ACTION=\"$ScriptURL?addarchive\" METHOD=\"POST\">\n";
    print "<B>USER:<INPUT TYPE=\"TEXT\" NAME=\"user\" SIZE=\"20\">\n";
    print "<BR>PASS:</B><INPUT TYPE=\"PASSWORD\" NAME=\"pass\"\n";
    print "SIZE=\"20\"><BR><BR>\n";
    print "<INPUT TYPE=\"SUBMIT\" VALUE=\"Update Archive\"></FORM>\n";
    print "<P><FORM ACTION=\"$ScriptURL?viewarchive\" METHOD=\"POST\">\n";
    print "<INPUT TYPE=\"SUBMIT\" VALUE=\"View Archive\"></FORM>\n";
    print "</CENTER><BR><BR>\n";
    &PrintFoot;
}

sub ViewArchive {
    &PrintHead;
    print "<HR><HR><H2 ALIGN=\"CENTER\">$SiteName<BR>Download Log Archive</H2>\n";
    print "<HR><HR><BLOCKQUOTE>Here is the archive of all past downloads for files and people\n";
    print "who have used this program to download files from your\n";
    print "web site.</BLOCKQUOTE><BR><BR><CENTER><TABLE WIDTH=1000\n";
    print "BORDER=2><TR><TD>&nbsp;</TD><TD>File Name</TD><TD>User Name</TD>";
    print "<TD>User E-Mail</TD><TD>Company</TD><TD>Date &amp; Time</TD></TR>\n";

	open(DATA, "<$Data/archive.list") 
		|| die "Can't open file list\n";
	@Contents = <DATA>;
	close(DATA);
	local($a) = 1;
	foreach $Content (@Contents) {
	    @File = split(/\|\|/, $Content);
	    $FileName = $File[0];
	    $UserName = $File[1];
	    $UserEmail = $File[2];
	    $Company = $File[3];
	    $DLDate = $File[4];
	    $DLTime = $File[5];
	    print "<TR><TD><B><I>$a</I></B></TD>\n";
      	    print "<TD><I>$FileName</I></TD><TD>$UserName</TD>\n";
	    print "<TD><I><A HREF=\"mailto:$UserEmail\">$UserEmail</A></TD>\n";
	        if($Company) {print "<TD>$Company</TD>\n"}
		    else {print "<TD>&nbsp;</TD>\n"}
	    print "<TD>$DLDate - $DLTime</TD></TR>\n";
	    $a++;
	}
    print "</TABLE><BR><BR>\n";
    print "You may purge the archive by<BR>\n";
    print "pressing the button below.<BR>\n";
    print "By doing this, you will remoove<BR>\n";
    print "<I>all entries</I> from the archvie<BR>\n";
    print "and you will not be able to retrieve them.<BR>\n";
    print "<FORM ACTION=\"$ScriptURL?purgearchive\" METHOD=\"POST\">\n";
    print "<B>USER:<INPUT TYPE=\"TEXT\" NAME=\"user\" SIZE=\"20\">\n";
    print "<BR>PASS:</B><INPUT TYPE=\"PASSWORD\" NAME=\"pass\"\n";
    print "SIZE=\"20\"><BR><BR>\n";
    print "<INPUT TYPE=\"SUBMIT\" VALUE=\"Purge Archive\"></FORM></CENTER>\n";
    &PrintFoot;
}

sub AddArchive {
    if(&PassCheck($FORM{'user'}, $FORM{'pass'})) {
      &PrintHead;
	open(DATA, "<$Data/downloadlog.list") 
		|| die "Can't open file list\n";
	@Contents = <DATA>;
	$num = @Contents;
	close(DATA);

	open(ARCH, ">>$Data/archive.list")
		|| die "Can't open archive file\n";
	for($a = 0; $a < $num; $a++) {
	    print ARCH "$Contents[$a]";
	}
	close(ARCH);

	open(DATA, ">$Data/downloadlog.list") 
		|| die "Can't open file list\n";
	close(DATA);

	print "<H2>Successful Archive Update -</H2><HR><BR><B><CENTER>\n";
      print "Archive succesfully updated.<BR>Your <A HREF=\"$ScriptURL?viewlog\">download log</A> has been\n";
	print "erased<BR>but all past entries are still available <BR>from\n";
	print "<A HREF=\"$ScriptURL?viewarchive\">the archive file.</A><BR><BR><BR><BR>\n";
      &PrintFoot;
    }
    else {
	$Problem = "your UserID and Password did not match the database.";
	&Error($Problem);
    }
}

sub PurgeArchive {
    if(&PassCheck($FORM{'user'}, $FORM{'pass'})) {
      &PrintHead;
	open(ARCH, ">$Data/archive.list")
		|| die "Can't open archive file\n";
	close(ARCH);
	print "<H2>Successful Archive Purge -</H2><HR><BR><B><CENTER>\n";
      print "Archive succesfully purged.<BR>All entries have been<BR>\n";
     print "removed from the archive.</B></CENTER><BR><BR><BR><BR>\n";
      &PrintFoot;
    }
    else {
	$Problem = "your UserID and Password did not match the database.";
	&Error($Problem);
    }
}	

sub RemoveLog {
    local($FirstEntry) = int($FORM{'first'});
    local($LastEntry) = int($FORM{'last'});
    local($cushion) = 0;
    local($b) = $FirstEntry - 1;
    local($c) = $LastEntry - 1;
    if(&PassCheck($FORM{'user'}, $FORM{'pass'})) {
	open(DATA, "<$Data/downloadlog.list") 
		|| die "Can't open file list\n";
	@Contents = <DATA>;
	close(DATA);
	$num = @Contents;
	open(DATA, ">$Data/downloadlog.list") || die "Can't read files list\n";
	for($a = 0; $a < $num; $a++) {
	    $Content = @Contents[$a];
	    chop($Content);
	            if(($a < $b) || ($a > $c)) {
	    		print DATA "$Content\n";
		    }
		    else {$cushion = 1}
	        }

	close(DATA);
	if($cushion) {
	&PrintHead;
	    print "<H2>Successfull Removal -</H2>The file you selected\n";
	    print "was successfuly removed from the database. You may now\n";
	    print "return to <A HREF=\"$ScriptURL?admin\">the admin page\n";
	    print "</A>, the <A HREF=\"$ScriptURL?viewlog\"> access log\n";
 	    print "page</A> or the <A HREF=\"$ScriptURL\">Download Page\n";
	    print "</A><BR><BR>\n";
	&PrintFoot;
	}
	else {
	    $Problem = "the entry you selected was not deleted.";
	    &Error($Problem);
	}
    }
    else {
	$Problem = "your UserID and Password did not match the database.";
	&Error($Problem);
    }
}



sub PassCheck {
    $CheckName = @_[0];
    $CheckPass = @_[1];
    $Name = crypt($CheckName, NB);
    $Pass = crypt($CheckPass, NB);
    open(PASS, "<$Data/pass.list") || die "Can't read pass file\n";
    $Compare = <PASS>;
    close(PASS);
    chop($Compare);
    ($CompareName, $ComparePass) = split(/\t/, $Compare);
    if(($Name eq $CompareName) && ($Pass eq $ComparePass)) {
	return(1);
    }
	else{return(0)}
}

sub PrintHead {
    print "<HEAD><TITLE>$SiteName File Download</TITLE></HEAD><BODY\n";
    print "TEXT=\"BLACK\" LINK=\"#8B0000\" ALINK=\"#FF0000\"\n";
    print "VLINK=\"#B22222\" BGCOLOR=\"#FFF6FO\">\n";
    print "<SMALL><FONT FACE=\"arial,verdana\">\n";
}

sub PrintFoot {
    print "<BR><BR><CENTER><SMALL><A HREF=\"http://www.thebeaches.to/\n";
    print "jmwd/index.html\">&copy; 1998, Jim Melanson Web Design</A><BR>\n";
    print "Downloading maintained by <A HREF=\"http://www.thebeaches.to/\n";
    print "jmwd/charity_ware/my_downloader.html\">My Downloader V1.0\n";
    print "</A><BR>Script Protection by\n";
    print "<A HREF=\"http://www.thebeaches.to/\n";
    print "jmwd/charity_ware/attack_guard.html\">Attack Guard V1.0</A>\n";
    print "</SMALL></CENTER></FONT></SMALL></BODY></HTML>\n";
}

sub SplitData {
    local($Split) = @_[0];
    chop($Split);
    @File = split(/\|\|/, $Split);
    $FileName = $File[0];
    $FilePage = $File[1];
    $FileSize = $File[2];
    $FileType = $File[3];
    $FileLink = $File[4];
    $FileText = $File[5];
    $FileText =~ s/LBCR/<BR>/g;
}

sub Setup {
    if(open(TEST, "<$Data/pass.list")) {
	close(TEST);
	$Problem = "the program is already set up.";
	&Error($Problem);
    }
    else {
    &PrintHead;
    print "<FORM ACTION=\"$ScriptURL\" METHOD=\"POST\">\n";
    print "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"setupfinal\">\n";
    print "Enter userID:<INPUT TYPE=\"TEXT\" NAME=\"user\" SIZE=\"20\">\n";
    print "<BR>Enter Password:<INPUT TYPE=\"TEXT\" NAME=\"passcheck\"\n";
    print "SIZE=\"20\"><BR>Re-enter Password:<INPUT TYPE=\"TEXT\"\n";
    print "NAME=\"passcompare\" SIZE=\"20\"><BR><INPUT TYPE=\"SUBMIT\"\n";
    print "VALUE=\"SUBMIT\"></FORM><BR><BR><BR>\n";
    &PrintFoot;
    }
}

sub SetupFinal {
    $checkentry = 0;
    if($FORM{'passcheck'} eq $FORM{'passcompare'}) {
	$NewPass = crypt($FORM{'passcheck'}, NB);
	$NewName = crypt($FORM{'user'}, NB);
	if(open(FILE, ">$Data/pass.list")) {
	    print FILE "$NewName\t$NewPass\n";
	    close(FILE);
	    $checkentry = 1;
	}
	if($checkentry) {
	&PrintHead;
	print "successfull setup.\n";
	&PrintFoot;
	}
	else {
	$Problem = "we were unable to create pass file.";
	&Error($Problem);
	}
    }

 	else {
	    $Problem = "the two passwords did not match, please try again.";
	    &Error($Problem);
	}
}

sub CheckEmail {
    local($EMAIL) = @_[0];

    if($EMAIL) {
        if(($EMAIL =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/) ||
	    ($EMAIL !~ /^.+@\[?(\w|[-.])+\.[a-zA-Z]{2,3}|[0-9]{1,3}\]?$/)) {
	    return(0);
        }
        else {return(1)}
    }
    else {
    	return(0);
    }
}

sub restore {
    my($rs_file) = $_[0];
    my($rs_file_bak) = $rs_file;
    $rs_file_bak =~ s/^(.+)\.[^\.]\w+$/$1\.bak/;
    if(open(BKF, "<$rs_file_bak")) {
	flock(BKF, '2');
	my(@backup) = <BKF>;
	close(BKF);
	if(open(BAKUP, ">$rs_file")) {
	    flock(BAKUP, '2');
	    print BAKUP @backup;
	    close(BAKUP);
	    return(@backup);
	} else {
	   return(0);
  	}
    } else {
	return(0);
    }
}

sub backup {
    my($bk_file) = $_[0];
    if(open(BKF, "<$bk_file")) {
	flock(BKF, '2');
	my(@backup) = <BKF>;
	close(BKF);
	$bk_file =~ s/^(.+)\.[^\.]\w+$/$1\.bak/;
	if(open(BAKUP, ">$bk_file")) {
	    flock(BAKUP, '2');
	    print BAKUP @backup;
	    close(BAKUP);
	    return(1);
	} else {
	   return(0);
  	}
    } else {
	return(0);
    }
}

