#!/usr/bin/perl
###################################################################
# FILE UPLOADER									
# Distribution by Psybercore.com, Inc.	info@superscripts.com    
#
# My name is Drew Star... and I am funky http://www.mp3.com/drewstar/
#
# http://www.superscripts.com/
#             
###################################################################
# FILE UPLOADER									
#                         
# Duplicating or redistributing this program without prior
# written consent is expressly forbidden.  You must Obtain
# permission before redistributing this software by any means.
# In all cases this copyright/license notice must remain intact.
#
# http://Marek'sScripts.w2w.cc/ 
#               
###################################################################

## Define Variables:

############ General Options ############

## Your path to where you want your files uploaded.
## Note: NO trailing slash

$imagedir = "/home/yourdomain/www/upfiles";


# Full path on your server to log file

$log = "/home/yourdomain/www/cgi-bin/up.log";


# set to the number of files you wish to upload +1 
# (e.g. for 10 files $numfiles = "11")

$numfiles = "5"; 


# referer allows addresses listed here only, and prevents others from using
# the program - replace yourdot.com and IP numbers to match your site 
# case insensitive

@referers = ('yourdomain.com', 'www.yourdomain.com', '209.456.66.132');


############ File Type Options ############

## Do you wish to allow all file types?  yes/no (no capital letters)

$allowall = "no";

## If $allowall = "no" enter file extensions below (no capital letters)

@filetypes = ('.jpg', '.jpeg', '.gif');


############ Mail Options ############

$EMail = "yes";		# yes/no lowercase - Do you want to recieve email notification?

$sendmail="/usr/sbin/sendmail"; 						#path to sendmail on your server

$ConfirmToEmail = "youremail\@yourdomain.com"; 		#email to send webmaster's confromation to

$SubjectEmail="Files Uploaded"; 						#email subject

############ Results Page Background and Coulour Options ############

$bgcolor = "#FFFFFF";

$text = "#000000";

$link = "#0000FF";

$vlink = "#800080";

$background = "";


############ Results Page Text Options ############

$html_title="Thank You!";

$html_header="Thank You!";

$html_header2="Files Uploaded";

$message = "Thank you for uploading.";


############ Results Page Link Options ############

$link1 = "http://www.yourdomain.com/index.html";

$text_link1 = "Home";

$link2 = "http://www.yourdomain.com/links.html"; 

$text_link2 = "Links";

$link3 = "http://www.yourdomain.com/up.html";

$text_link3 = "Back";


################################################
## DO NOT EDIT OR COPY BELOW THIS LINE        ##
################################################

$body = "<body BACKGROUND=\"$background\" bgcolor=\"$bgcolor\" text=\"$text\" link=\"$link\" vlink=\"$vlink\">";

$html_header3="Uploaded by:";

$EmailMessage = "New files have been uploaded!"; 	#email message

$numfiles++;

# Check Referring URL
&check_url();

use CGI; 

$onnum = 1;
while ($onnum != $numfiles) {
my $req = new CGI; 
if ($req->param("FILE1") eq "") { &error_nofile; }
my $file = $req->param("FILE$onnum"); 
if ($file ne "") {
my $fileName = $file; 
$fileName =~ s!^.*(\\|\/)!!; 
$newmain = $fileName;

$persname = $req->param("persname");
$persemail = $req->param("persemail");
$descr = $req->param("descr");


### Validate File Types ###

$filegood = "yes";
if ($allowall ne "yes") { &check_filetypes; }


### Do the Work ###

if ($filegood eq "yes") {

	open (OUTFILE, ">$imagedir/$fileName"); 

	#print "<tr><td>File $onnum: $fileName</td></tr>";
	#if ($EMail eq "yes") { print MAIL "$newmain\n\n"; }
	#if ($log ne "") { push(@filelist,"$fileName"); }
	push(@filelist,"$fileName");
	$temp = "File $onnum: $fileName";
	push(@filelist2,"$temp");

	while (my $bytesread = read($file, my $buffer, 1024)) { 
		print OUTFILE $buffer;
		}
	close (OUTFILE); 
	}
}
$onnum++;
}


if ($EMail eq "yes") { &send_mail_3; }
if ($log ne "") { &log_3; }
&print_html;

exit;

## Start of SubRoutines ##

###############################################################
sub send_mail_3 {

		#$upload2 = join(", ",@filelist);

	   # Open and write Confirmation Email

 		open(MAIL,"|$sendmail -t");

    	print MAIL "To: $ConfirmToEmail\n";
    	print MAIL "From: $ConfirmToEmail\n";
    	print MAIL "Subject: $SubjectEmail\n\n\n" ;
	   	print MAIL "$EmailMessage\n\n";
	   	print MAIL "\n";

		foreach $i (@filelist2) {print MAIL "$i\n\n";}

	   	print MAIL "\n";
	   	print MAIL "Files uploaded by: $persname <$persemail>\n";
	   	print MAIL "\n";
	   	print MAIL "\n";
	   	print MAIL "Comments: $descr\n";
	   	print MAIL "\n\n\n";
		print MAIL "This CGI was developed by:\n\n";
		print MAIL "Marek\'s Script Archive http://MareksScripts.w2w.cc\n\n";
	   	close (MAIL);

	 }


###############################################################
sub redirect {

	print "Location:$donepage\n\n";

	#exit;

}


###############################################################
sub print_html {

	print "Content-type:text/html\n\n";
	print "<HTML><HEAD><TITLE>$html_title</TITLE></HEAD>\n";
	print "$body\n";
	print "<center><BR><h2>$html_header</h2></center>\n";
	print "<center><BR><h4>$html_header2</h4></center>\n";
	print "<center><table border=0 cellpadding=0 cellspacing=0 width=>\n";
	print "<tr><td align=left>\n";
	print "<center><table border=2 cellpadding=10 cellspacing=2 width=>\n";

	foreach $i (@filelist2) {print "<tr><td>$i</td></tr>\n";}

	print "</table></center><BR>\n";
	print "<center><BR><h4>$html_header3</h4></center>\n";

	print "<HR><table border=0 cellpadding=10 cellspacing=2 width=80%>\n";
	print "<tr><td valign=top>\n";
	print "Name:</td><td width=50>$persname";
	print "</td></tr></table>\n";

	print "<table border=0 cellpadding=10 cellspacing=2 width=80%>\n";
	print "<tr><td valign=top>\n";
	print "Email:</td><td>$persemail";
	print "</td></tr></table>\n";
	print "</td></tr></table>\n";

	print "<table border=2 cellpadding=10 cellspacing=2>\n";
	print "<tr><td align=center valign=top>\n";
	print "Description:</td></tr><tr><td  align=center width=300>$descr";
	print "</td></tr></table>\n";

	#print "</td></tr></table>\n";

	print "<BR><BR><HR width = \"60%\">\n";
	print "$message\n";
	print "<BR><BR>\n";

	print "<a href=$link1>$text_link1</a> &nbsp;&nbsp;&nbsp;\n";
	print "<a href=$link2>$text_link2</a> &nbsp;&nbsp;&nbsp;\n";
	print "<a href=$link3>$text_link3</a> \n";
	print "<BR><BR>\n";

	print "This CGI was developed by:\n\n";
	print "<a href=http://MareksScripts.w2w.cc/>Marek\'s Script Archive</a>\n\n";

	print "<BR></center></body></html>";

	#exit;
}


####### Check to see if another site is trying to use the program ########
sub check_url {
    my $check_referer = 0;

    if ( $ENV{'HTTP_REFERER'} ) {
        foreach $referer (@referers) {
            if ($ENV{'HTTP_REFERER'} =~ m|https?://([^/]*)$referer|i) {
                $check_referer = 1;
                last;
            }
        }
    }
    else {
        $check_referer = 0;
    }

    if ($check_referer != 1) { &ref_error; } 
}




###############################################################
sub error_filetype {

	print "Content-type:text/html\n\n";
	print "<HTML><HEAD><TITLE>Invalid File Type</TITLE></HEAD>$body\n";
	print "<BR><BR><center>\n";
	print "<B>You tried to upload an invalid file type ( $newmain )!</B>";
	print "<BR><BR>\n\n";
	$filetypes2 = join(", ",@filetypes);
	print "Files must end with: $filetypes2";
	#if ($allowzip eq "yes") { print ".zip, "; }
	print "<BR><BR>\n\n";
	print "Please check the file and try again!";
	print "<BR><BR>\n";
	print "</center></body></html>";

	exit;
}


####### Error Bad Referer #######
sub ref_error { 

	print "Content-type:text/html\n\n";
	print "<HTML><HEAD><TITLE>Referer Error</TITLE></HEAD>$body\n";
	print "<BR><BR><CENTER><font color=red><h2>You do not have permision to use this script!</h2></font>\n";
	print "</CENTER><BR><BR>";
	print "</body></html>";

  	exit;
	}


####### Error No File #######
sub error_nofile { 

	print "Content-type:text/html\n\n";
	print "<HTML><HEAD><TITLE>No File</TITLE></HEAD>$body\n";
	print "<BR><BR><CENTER><font color=red><h2>You haven't selected a file to upload!</h2></font>\n";
	print "</CENTER><BR><BR>";
	print "</body></html>";

  	exit;
	}


####### Check File Types #######
sub check_filetypes { 

	$imageonly = "yes";
	if ($imageonly eq "yes"){
		$filegood = "no";
	foreach $i (@filetypes){
		if (lc(substr($newmain,length($newmain) - 4,4)) eq $i)
			{ $filegood = "yes"; }
		}
	if ($filegood eq "no") { &error_filetype; }
	}
}



####### Log 3 #######
sub log_3 {

	$upload = join(", ",@filelist);

	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
	if (length ($min) eq 1) {$min= '0'.$min;}
	if ($year < 100) {$year = 2000 + $year;}
	else {$year  = 1900 + $year;}
	$mon++;
	$date="$mon/$mday/$year, $hour:$min";

	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	# open(LOG,">>$log") or &error_nofile;
	open(LOG,">>$log") || die $!;
	print LOG "$date\n";
	print LOG "Files uploaded by: $persname <$persemail>\n";
	print LOG "Comments: $descr\n";
	print LOG "FILES: $upload\n";
	print LOG "--------------------------------------------------------------------\n";
	close(LOG); 

}
