I did a form mailer file upload cgi but having a bit of problem with setting required fields.
To put things in layman terms, the form work similar to regular form mailer, user submits their info e.g., name, address, phone, email -- all these fields SHOULD be required fields (must be filled in) and there's a file upload field for the user to select a file on his local drive and send to a server path.
The problem I'm having -- the file upload field is set as a required field, therefore, if the user does not select a file to upload, he will get a warning message saying that a file upload is needed. I do not want it so, this file upload field should NOT be a required field. The other fields, such as name, address, phone, email currently are not required fields but I need these fields TO BE required fields. Example, if the user does not fill out the phone or email field, he should get a warning page saying something like "Error-Missing Fields" and a list of the required fields that weren't filled in.
So right now, the form is working in the opposite way where the required fields are concerned. That is, the file upload is currently a required field -> FILE UPLOAD MUST NOT BE A REQUIRED FIELD. Fields such as name, email, phone, address, currently are not required fields -> THESE INPUT FIELDS MUST BE REQUIRED FIELDS.
I'm asking someone to look over this cgi script. I've done alot of work to it -- solving the error 500; getting fields to print out on screen, setting up the confirmation receipt section, etc., but I just cannot figure out how to fix in the required fields.
Your help is major appreciated!
------cgi script---
#!/usr/local/perl/bin/perl
$imagedir = "/usr/local/opt/me/html/up
load";
# Full path on your server to log file
$log = "/usr/local/opt/me/html/bi
n/up.log";
# set to the number of files to upload +1
# (e.g. for 10 files $numfiles = "11")
$numfiles = "5";
# refers
@referers = ('my.work.com', 'work.my.com', 'me.atwork.com', '123.456.789.10');
# Retrieve Date
&get_date;
## Do you wish to allow all file types? yes/no (no capital letters)
$allowall = "yes";
## If $allowall = "no" enter file extensions below (no capital letters)
@filetypes = ();
$EMail = "yes";
$sendmail="/usr/lib/sendma
il";
$ConfirmToEmail = "me\@at.work.com";
$SubjectEmail="Test Form results";
############ Results Page Background and Colour Options ############
$bgcolor = "#FFFFFF";
$text = "#000000";
$link = "#0000FF";
$vlink = "#800080";
$background = "";
############ Results Page Text Options ############
$html_title="Whatever";
$html_header="Whatever 1";
$html_header2="Whatever 2";
$message = "Thank you for submitting your request. This is your confirmation of receipt.";
############ Results Page Link Options ############
$link1 = "
http://my.work.com/services/requests/1.html";
$text_link1 = "Whatever 1";
$link2 = "
http://my.work.com/services/requests/2.html";
$text_link2 = "Whatever 2";
$link3 = "
http://my.work.com/services/requests/3.html";
$text_link3 = "Whatever 3";
$link4 = "
http://my.work.com";
$text_link4 = "Home";
##########################
##########
##########
##
## SANDI THIS IS THE IMPORTANT SECTION ##
##########################
##########
##########
##
$body = "<body BACKGROUND=\"$background\"
bgcolor=\"$bgcolor\" text=\"$text\" link=\"$link\" vlink=\"$vlink\">";
$html_header3="Completed and submitted 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;
$REQUESTERS_NAME = $req->param("REQUESTERS_NA
ME");
$EMAIL = $req->param("EMAIL");
$PHONE = $req->param("PHONE");
$WORK_ADDRESS = $req->param("WORK_ADDRESS"
);
### 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; }
if ($log ne "") { &log_3; }
&print_html;
exit;
## Start of SubRoutines ##
##########################
##########
##########
##########
#######
sub get_date {
# Define arrays for the day of the week and month of the year. #
@days = ('Sunday','Monday','Tuesda
y','Wednes
day',
'Thursday','Friday','Satur
day');
@months = ('January','February','Mar
ch','April
','May','J
une','July
',
'August','September','Octo
ber','Nove
mber','Dec
ember');
# Get the current time and format the hour, minutes and seconds. Add #
# 1900 to the year to get the full 4 digit year. #
($sec,$min,$hour,$mday,$mo
n,$year,$w
day) = (localtime(time))[0,1,2,3,
4,5,6];
$time = sprintf("%02d:%02d:%02d",$
hour,$min,
$sec);
$year += 1900;
# Format the date. #
$date = "$days[$wday], $months[$mon] $mday, $year at $time";
}
##########################
##########
##########
##########
#######
sub send_mail {
#$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 "Below is the result of a Create a New Service Request. It was submitted by\n";
print MAIL "$REQUESTERS_NAME $EMAIL on $date\n";
print MAIL "-=-=-=-=-=-=-=-=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=-
=-=-=-=-=\
n";
print MAIL "\n";
foreach $i (@filelist2) {print MAIL "$i\n\n";}
print MAIL "REQUESTER'S NAME: $REQUESTERS_NAME\n";
print MAIL "EMAIL: $EMAIL\n";
print MAIL "REQUESTER'S PHONE CONTACT: $PHONE\n";
print MAIL "ADDRESS: $WORK_ADDRESS\n";
print MAIL "\n\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</TIT
LE></HEAD>
\n";
print "$body\n";
print "<center><BR><h2>$html_hea
der</h2></
center>\n"
;
print "<center><BR><h4>$html_hea
der2</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_hea
der3</h4><
/center>\n
";
print "<HR><table width=100% border=0 cellpadding=2 cellspacing=2>\n";
print "<tr><td valign=top>\n";
print "REQUESTER'S NAME:</td><td>$REQUESTERS_
NAME";
print "</td></tr></table>\n";
print "<table width=100% border=0 cellpadding=10 cellspacing=2>\n";
print "<tr><td valign=top>\n";
print "REQUESTER'S EMAIL:</td><td>$EMAIL";
print "</td></tr></table>\n";
print "</td></tr></table>\n";
print "<table width=100% border=0 cellpadding=2 cellspacing=2>\n";
print "<tr><td valign=top>\n";
print "REQUESTER'S PHONE CONTACT:</td></tr><tr><td>
$PHONE";
print "</td></tr></table>\n";
print "<table width=100% border=0 cellpadding=2 cellspacing=2>\n";
print "<tr><td valign=top>\n";
print "ADDRESS:</td></tr><tr><td
>$WORK_ADD
RESS";
print "</td></tr></table>\n";
print "<BR><BR><HR width = \"100%\">\n";
print "<h4><tt>$message</tt></h4
>\n";
print "<BR><BR>\n";
print "<a href=$link1><tt>$text_link
1</a></tt>
;  \n";
print "<a href=$link2><tt>$text_link
2</a></tt>
; \n";
print "<a href=$link3><tt>$text_link
3</a></tt>
; \n";
print "<a href=$link4><tt>$text_link
4</a></tt>
; \n";
print "<BR><BR>\n";
print "<BR><BR><HR width = \"100%\">\n";
print "<h6><tt>Global warming.</tt></h6>\n\n";
print "<BR></center></body></htm
l>";
#exit;
}
####### Check to see if another site is trying to use the program ########
sub check_url {
# Localize the check_referer flag which determines if user is valid. #
local($check_referer) = 0;
# If a referring URL was specified, for each valid referer, make sure #
# that a valid referring URL was passed to FormMail. #
if ($ENV{'HTTP_REFERER'}) {
foreach $referer (@referers) {
if ($ENV{'HTTP_REFERER'} =~ m|https?://([^/]*)$referer
|i) {
$check_referer = 1;
last;
}
}
}
else {
$check_referer = 1;
}
# If the HTTP_REFERER was invalid, send back an error. #
if ($check_referer != 1) { &error('bad_referer') }
}
##########################
##########
##########
##########
#######
sub error_filetype {
print "Content-type:text/html\n\
n";
print "<HTML><HEAD><TITLE>Invali
d 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>Refere
r 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,$mo
n,$year,$w
day,$yday,
$isdst)=lo
caltime(ti
me);
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 "Form submitted and file(s) uploaded by: $REQUESTERS_NAME <$EMAIL>\n";
print LOG "Phone Contact: $PHONE\n";
print LOG "Address: $WORK_ADDRESS\n";
print LOG "FILES: $upload\n";
print LOG "-------------------------
----------
----------
----------
----------
---\n";
close(LOG);
}