to be more robust and configurable
// 2003-02-21 - 1.0 - First rev
// 2003-02-22 - 1.1 - Change to allow relative paths to params file.
// 2003-03-04 - 1.2 - Allow format file for customized text body. Added ValidateFile function
// 2003-03-04 - 1.3 - fix problem with required field warning text
// 2003-03-04 - 1.4 - fix problems with SendEmail and added MyExplode
// 2003-03-07 - 1.5 - added function to GetFormatFile. Format file can now also be the params file.
// Added ability to have a field be the sender address
// Added ability to define the ack email sender to be different than the primary sender
// Added ability to define the ack email subject
// 2003-06-10 - 1.6 - Added version header & vhost header for tracking
// 2003-10-14 - 1.7 - added missing init of maxIters in ParseFormatData
// 2004-02-24 - 1.8 - Added error checking on including params_file
// 2004-04-14 - 1.9 - imploded array based form params using tab characters
// 2004-06-24 - 2.0 - moved pass-by-reference symbols to function delclaration for ValidateRecipLists
// fixed problem with required_missing_redirect
// 2004-09-21 - 2.1 - changed GLOBALS references to use _SERVER
// Fixed ValidateFile to better determine file location
// 2005-01-25 - 2.2 - Fixed ValidateFile to better determine file location. MyExplode -> explode
// removed dependency on register_globals for GetFormParam
//
define('kVersion', '2.2');
$errors = '';
$FormData = $_POST;
// var_dump($FormData);
// exit();
// now extract any values that might have been posted which we don't allow to prevent being a relay
// these values should come across in the params file that is included later, but we will take no chances.
unset($sender, $TOrecipients, $CCrecipients, $BCCrecipients);
// try and prevent people snooping around up above doc root
$paramsFile = ValidateFile($FormData['params_file'], 'Parameters');
unset($FormData['params_file']);
// by including the params file, where the options are set in a php code block
// we are preventing this form mail from being a relay.
ini_set('track_errors', true);
$formatMode = 0;
if(! @include($paramsFile))
{
if (!empty($php_errormsg))
print_error("Something is wrong with your parameters file:
$php_errormsg", '');
}
ini_restore('track_errors');
if (empty($sender) && empty($sender_field)) print_error("No sender parameter defined.", 'missing');
$senderAddr = $sender;
if (!empty($sender_field))
{
$tmpSender = $FormData[$sender_field];
if (isEmailAddress($tmpSender))
{
$senderAddr = $tmpSender;
}
else
{
$errors .= "\nSender address field data not valid: $tmpSender\n";
if (!isEmailAddress($sender))
{
$senderAddr = 'website@'.$_SERVER['HTTP_HOST'];
$errors .= "\nNo valid Sender address found.\n";
}
}
}
// validate recipient lists. this function also turns the recip vars into arrays
$InvalidRecipErrs = ValidateRecipLists($TOrecipients, $CCrecipients, $BCCrecipients);
if ( !empty($InvalidRecipErrs) )
{
print_error(implode("\n", $InvalidRecipErrs), 'missing');
}
$required_missing_redirect = $FormData['required_missing_redirect'];
// handle the required fields
if (!empty($require))
{
$missing_field_list = '';
// seperate at the commas
$require = ereg_replace( ' +', '', $require);
$required = MyExplode(',',$require);
foreach($required as $reqFld)
{
// check if they exsist
if ( !isset($FormData[$reqFld]) || (trim($FormData[$reqFld]) == '') )
{
// if the required_missing_redirect option is on: redirect them
if ($required_missing_redirect)
{
header ("Location: $required_missing_redirect");
exit();
}
$missing_field_list .= "Missing: $reqFld
\n";
}
}
// send error to our mighty error function
if (!empty($missing_field_list)) print_error($missing_field_list,"missing");
}
// banned emails, these will be email addresses of people
// who are blocked from using the script
if (!empty($banlist) && !is_array($banlist)) $banlist = MyExplode(',', $banlist);
if (empty($banlist)) $banlist = array();
$banlist[] = '*@somedomain.com';
$banlist[] = 'user@domain.com';
$banlist[] = 'etc@domains.com';
if (!empty($email_verify))
{
$VerifyFields = MyExplode(',',$email_verify);
foreach($VerifyFields as $theFld)
{
$emailToVerify = trim($FormData[$theFld]);
if (!empty($emailToVerify) && !isEmailAddress($emailToVerify))
print_error("Email Address $emailToVerify is invalid");
if ( ! CheckBanlist($banlist, $emailToVerify) )
{
print_error("You are using a banned email address.");
}
$FormData[$theFld] = $emailToVerify;
}
}
// check zipcodes for validity
if (!empty($zip_verify))
{
$VerifyFields = MyExplode(',',$zip_verify);
foreach($VerifyFields as $theFld)
{
$zipToVerify = trim($FormData[$theFld]);
if (!empty($zipToVerify) && (!ereg("(^[0-9]{5})-([0-9]{4}$)", trim($zipToVerify)) && (!ereg("^[a-zA-Z][0-9][a-zA-Z][[:space:]][0-9][a-zA-Z][0-9]$", trim($zipToVerify))) && (!ereg("(^[0-9]{5})", trim($zipToVerify)))) )
print_error("Zip/Postal code $zipToVerify is invalid");
$FormData[$theFld] = $zipToVerify;
}
}
// check phone for validity
if (!empty($phone_verify))
{
$VerifyFields = MyExplode(',',$phone_verify);
foreach($VerifyFields as $theFld)
{
$phoneToVerify = trim($FormData[$theFld]);
if (!empty($phoneToVerify) && (!ereg("(^(.*)[0-9]{3})(.*)([0-9]{3})(.*)([0-9]{4}$)", $phoneToVerify)) )
print_error("Phone Number $phoneToVerify is invalid");
$FormData[$theFld] = $phoneToVerify;
}
}
if (!empty($format_file))
{
$formatFile = ValidateFile($format_file, 'Format');
$content = ParseFormatFile($formatFile, $FormData);
}
else
{
// prepare the content
$content = ParseForm($FormData);
}
// check for a file if there is a file upload it
if ($file_name)
{
if ($file_size > 0)
{
if (!ereg("/$", $path_to_file_uploads)) $path_to_file_uploads = $path_to_file_uploads.'/';
$location = $path_to_file_uploads.$file_name;
if (file_exists($path_to_file_uploads.$file_name)) $location .= '.new';
copy($file,$location);
unlink($file);
$content .= 'Uploaded File: '.$location."\n";
}
}
// second file.
if ($file2_name)
{
if ($file2_size > 0)
{
if (!ereg("/$", $path_to_file_uploads)) $path_to_file_uploads = $path_to_file_uploads."/";
$location = $path_to_file_uploads.$file2_name;
if (file_exists($path_to_file_uploads.$file2_name)) $location .= ".new";
copy($file2,$location);
unlink($file2);
$content .= "Uploaded File: ".$location."\n";
}
}
// if the env_report option is on: get eviromental variables
if ($env_report)
{
$env_report = ereg_replace(' +', '', $env_report);
$env_reports = MyExplode(',',$env_report);
$content .= "\n------ environmental variables ------\n";
foreach($env_reports as $envrptFld)
{
$envrptFld = trim($envrptFld);
switch ($envrptFld)
{
case 'REMOTE_HOST': $content .= 'REMOTE HOST: '.$_SERVER['REMOTE_HOST']."\n"; break;
case 'REMOTE_USER': $content .= 'REMOTE USER: '.$_SERVER['REMOTE_USER']."\n"; break;
case 'REMOTE_ADDR': $content .= 'REMOTE ADDR: '.$_SERVER['REMOTE_ADDR']."\n"; break;
case 'HTTP_USER_AGENT': $content .= 'BROWSER: '.$_SERVER['HTTP_USER_AGENT']."\n"; break;
}
}
}
// if the subject option is not set: set the default
if (!$subject) $subject = $_SERVER['HTTP_HOST']. " Form submission";
if (!empty($ack_email_to_field))
{
if (!empty($ack_email_sender) && isEmailAddress($ack_email_sender))
{
$content .= "\nAcknowledgment email sender address not valid: $ack_email_sender\n";
$ack_email_sender = '';
}
$ackEmailTo = $FormData[$ack_email_to_field];
if ( ! isEmailAddress($ackEmailTo) )
{
$content .= "\nAcknowledgment email address not valid: $ackEmailTo\n";
$ackEmailTo = ''; // signal not to send email
}
if ( ! CheckBanlist($banlist, $ackEmailTo) )
{
$content .= "\nAcknowledgment email address banned: $ackEmailTo\n";
$ackEmailTo = ''; // signal not to send email
}
if (!empty($ack_email_msg_file) && ($ack_email_msg_file == ValidateFile($ack_email_msg_file)))
{
// allows a user to put data and formdata items into a "format" file so the ack message
// can be customized.
$ack_email_msg = ParseFormatFile($ack_email_msg_file, $FormData);
}
if (empty($ack_email_msg)) $ack_email_msg = "Your form submission was accepted.\n\n Thank You.";
if (empty($ack_email_subject)) $ack_email_subject = "Your form submission was sent";
if (!empty($ackEmailTo))
{
if (empty($ack_email_sender)) $ack_email_sender = $senderAddr;
SendEmail($ack_email_sender, $ackEmailTo, '', '', $ack_email_subject, $ack_email_msg);
}
}
$content .= $errors;
SendEmail($senderAddr, $TOrecipients, $CCrecipients, $BCCrecipients, $subject, $content);
// send it off
// mail_it(stripslashes($content), stripslashes($subject), $email, $recipient, $allowed_email_recipients_array);
// if the redirect option is set: redirect them
if ($redirect) {
header ("Location: $redirect");
} else {
echo "Thank you for your submission\n";
echo "
\n";
}
exit();
// <---------- THE END ----------> //
// Functions only below
// our mighty error function..
function print_error($reason,$type = 0)
{
build_body($title, $bgcolor, $text_color, $link_color, $vlink_color, $alink_color, $style_sheet);
// for missing required data
if ($type == "missing")
{
echo "The form was not submitted because of missing information:
\n"; echo "
"; echo "