function check_required($key, $value){
if(substr($key,-9) == '_required' AND $value == ''){
global $required_error;
header('Location: '.$required_error);
exit;
}
}
function file_upload_php3($key, $value){
if(substr($key,-5) != '_file' AND substr($key,-14) != '_file_required') return FALSE;
if(substr($key,-9) == '_required' AND ($value == 'none' OR $value == '')){
global $required_error;
header('Location: '.$required_error);
exit;
}
if($value != 'none' AND $value != ''){
global $num_attachments, $mail;
if(get_magic_quotes_gpc() == 1 AND (is_long(strpos(strtolower($value), 'windows')) OR is_long(strpos(strtolower($value), 'winnt')))) $value = stripslashes($value);
$ctype = $key.'_type';
$filename = $key.'_name';
global $$filename, $$ctype;
$attachment = fread($fp = fopen($value, 'r'), filesize($value)); fclose($fp);
$mail->add_attachment($attachment, $$filename, $$ctype);
$num_attachments++;
}
return TRUE;
}
function file_upload_php4($HTTP_POST_FILES){
if(isset($HTTP_POST_FILES) AND is_array($HTTP_POST_FILES)){
while(list($key, $attributes) = each($HTTP_POST_FILES)){
if($attributes['tmp_name'] != 'none' AND $attributes['tmp_name'] != ''){
global $num_attachments, $mail;
if(get_magic_quotes_gpc() == 1 AND (is_long(strpos(strtolower($attributes['tmp_name']), 'windows')) OR is_long(strpos(strtolower($attributes['tmp_name']), 'winnt')))) $attributes['tmp_name'] = stripslashes($attributes['tmp_name']);
$ctype = $attributes['type'];
$origname = $attributes['name'];
$attachment = fread($fp = fopen($attributes['tmp_name'], 'r'), filesize($attributes['tmp_name'])); fclose($fp);
$mail->add_attachment($attachment, $origname, $ctype);
$num_attachments++;
}elseif(substr($key, -9) == '_required'){
global $required_error;
header('Location: '.$required_error);
exit;
}
}
}
}
function main($key, $value, &$message, &$message_values){
global $tpl, $use_templates, $$key;
if($key != 'configfile' AND $key != 'thankyou_page' AND $key != 'recipient' AND $key != 'subject' AND $key != 'addhostip' AND $key != 'MAX_FILE_SIZE' AND !eregi('_file$|_file_name$|_file_size$|_file_type$ ', $key)){
if(is_array($value)){
$message[] = $key;
$message_values[] = (get_magic_quotes_gpc() == 1) ? stripslashes(implode(', ', $value)) : implode(', ', $value);
if(isset($use_templates) AND $use_templates == 1){
$$key = implode(', ', $value);
$tpl->register('main', $key);
$tpl->register('email', $key);
}
}else{
$message[] = $key;
$message_values[] = (get_magic_quotes_gpc() == 1) ? stripslashes($value) : $value;
if(isset($use_templates) AND $use_templates == 1){
if(get_magic_quotes_gpc() == 1) $$key = stripslashes($$key);
$tpl->register('main', $key);
$tpl->register('email', $key);
}
}
}
}
function padding(&$array, $character, $length = 0){
if(count($array) == 0) return;
$longest = 0;
for($i=0; $i<count($array); $i++) if(strlen($array[$i]) > strlen($array[$longest])) $longest = $i;
if($length == 0) $length = strlen($array[$longest]);
for($i=0; $i<count($array); $i++){
$padding = $length - strlen($array[$i]);
for($j=0; $j<$padding; $j++) $array[$i] .= $character;
}
}
if(isset($HTTP_POST_VARS) == FALSE) die('HTTP_POST_VARS not set - you may need to enable track_vars!'); else $postvars = $HTTP_POST_VARS;
if($security == 1){
for($i=0; $i<count($servername); $i++)
if(is_long(strpos(getenv('HTTP_REFERER'), $servername[$i])))
$valid = 1;
if(!isset($valid)){
header('Location: '.$bad_referer);
exit;
}
if(isset($bad_addresses) AND $bad_addresses != '' AND file_exists($bad_addresses)){
$file_array = file($bad_addresses);
for($i=0; $i<count($file_array); $i++) if(getenv('REMOTE_ADDR') == trim($file_array[$i]) OR getenv('REMOTE_HOST') == trim($file_array[$i])){ header('Location: '.$bad_address); exit; }
}
}
if(isset($postvars['debug']) AND $postvars['debug'] == 1){
while(list($key, $value) = each($postvars)){
echo $key.' = '.$value."
\n";
}
echo "
\n\n";
phpinfo();
exit;
}
if(isset($postvars['thankyou_page']) AND $postvars['thankyou_page'] != '') $thankyou_page = $postvars['thankyou_page'];
if(isset($postvars['recipient']) AND $postvars['recipient'] != ''){
$recipient = $postvars['recipient'];
}elseif(!isset($recipient) OR $recipient == ''){
echo 'Form incorrectly configured - no recipient defined. Please see the readme for details.';
exit;
}
if(isset($postvars['subject']) AND $postvars['subject'] != '') $subject = $postvars['subject']; elseif(!isset($subject) OR $subject == '') $subject = 'Feedback from website.('.$HTTP_REFERER.')';
if((isset($postvars['use_templates']) AND ($use_templates = $postvars['use_templates']) == 1) OR (isset($use_templates) AND $use_templates == 1)){
if(isset($postvars['tpl_thankyou']) AND $postvars['tpl_thankyou'] != '') $tpl_thankyou = $postvars['tpl_thankyou'];
if(isset($postvars['tpl_email']) AND $postvars['tpl_email'] != '') $tpl_email = $postvars['tpl_email'];
}
$regex = '^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3}))$';
if(isset($postvars['email']) AND $postvars['email'] != ''){
$email = $postvars['email'];
if(isset($check_email) AND $check_email == 1 AND !eregi($regex, $email)){
$email_redirect = 1;
}
}elseif(isset($postvars['email_required']) AND $postvars['email_required'] != ''){
$email = $postvars['email_required'];
if(isset($check_email) AND $check_email == 1 AND !eregi($regex, $email)){
$email_redirect = 1;
}
}
if(isset($postvars['configfile']) AND $postvars['configfile'] != '' AND file_exists($postvars['configfile'])){
$file_array = file($postvars['configfile']);
for($i=0; $i<count($file_array); $i++){
$var_name = trim(substr(trim($file_array[$i]),0,strpos($file_array[$i], '=')));
$var_value = trim(substr(trim($file_array[$i]),strpos($file_array[$i], '=')+1));
$$var_name = $var_value;
}
}
if(isset($email_redirect) AND $email_redirect == 1){
header('Location: '.$invalid_email);
exit;
}
/***************************************
** Setup the objects.
***************************************/
$mail = new html_mime_mail();
if($use_templates == 1){
$tpl = new template;
if($tpl_thankyou != '') $tpl->load_file('main', $tpl_thankyou);
if($tpl_email != '') $tpl->load_file('email', $tpl_email);
}
/***************************************
** Begin the main loop. First set the
** arrays that hold the values to email.
***************************************/
$message = array();
$message_values = array();
$num_attachments = 0;
while(list($key,$value) = each($postvars)){
check_required($key, $value);
if((int)phpversion() < 4) if(file_upload_php3($key,$value) == TRUE) continue;
main($key, $value, $message, $message_values);
}
if((int)phpversion() >= 4 AND isset($HTTP_POST_FILES) AND is_array($HTTP_POST_FILES)) file_upload_php4($HTTP_POST_FILES);
padding($message, '.');
for($i=0; $i<count($message); $i++) $message[$i] .= '..:'.$message_values[$i];
$time = date('H:i', time());
$date = date('l m F Y', time());
if(isset($use_templates) AND $use_templates == 1 AND $tpl_email != ''){
$tpl->register('email','REMOTE_ADDR,REMOTE_HOST,HTTP_RE FERER,HTTP_USER_AGENT,recipient,num_attachments,su bject,time,date');
$tpl->parse('email');
$body = $tpl->return_file('email');
while(preg_match('/({.+})/U',$body, $matches) == TRUE){
$body = str_replace($matches[1], '', $body);
}
}else{
$body = 'At '.$time.' on '.$date.', the following information was submitted to your form at '.getenv('HTTP_REFERER')." :\r\n\r\n";
$body .= implode("\r\n", $message)."\r\n\r\n";
$body .= (isset($postvars['addhostip']) AND $postvars['addhostip'] == 1) ? 'Remote IP: '.getenv('REMOTE_ADDR')."\r\nRemote hostname: ".getenv('REMOTE_HOST')."\r\n" : '';
}
if($num_attachments > 0){
$mail->add_body_text($body);
$mail->build_message();
$mail->send('', $recipient, '', $email, $subject);
}else{
$headers = 'Content-Type: '.$email."\r\n";
$headers = 'From: '.$email."\r\n";
mail($recipient, $subject, $body, $headers);
}
if(isset($use_templates) AND $use_templates == 1 AND $tpl_thankyou != ''){
$tpl->register('main','REMOTE_ADDR,REMOTE_HOST,HTTP_REF ERER,HTTP_USER_AGENT,recipient,num_attachments,sub ject,time,date');
$tpl->parse('main');
$output = $tpl->return_file('main');
while(preg_match('/({.+})/U',$output, $matches) == TRUE){
$output = str_replace($matches[1], '', $output);
}
echo $output;
}elseif(isset($thankyou_page) AND $thankyou_page != ''){
header('Location: '.$thankyou_page);
exit;
}
?>