/**
PHP Form Data
Usage: action="http://www.comparity.net/common/form.php[?action=...,...]"
actions: email, html, form
Email: to, from, subject, message (required)
cc, bcc, attachment, redirect, data=[list of fields to send on], $html
*/
date_default_timezone_set('Australia/Melbourne');
include_once "/data/www/html/common/routines.php";
error_reporting(E_ALL);
function entabulate($array,$caption=NULL) {
$a="
\n";
if($caption) $a.="$caption\n";
$a.="";
foreach ($array as $k=>$v) {
if (is_array($v)) $v='Array: '.implode(';',$v);
$a.="| $k | $v |
\n";
}
$a.="
\n";
return $a;
}
function printform() {
$accept=@$_SERVER['HTTP_ACCEPT'];
list($cookiestable,$gettable,$posttable)=array(entabulate($_COOKIE,"Cookies"),entabulate($_GET,"GET Data"),entabulate($_POST,"POST Data"));
$html=addslashes(file_get_contents('form.html'));
eval("\$html=\"$html\";");
print $html;
}
$action=@$_GET['action'];
if($action) {
//$action=array_fill_keys(array_flip(array_values(split(",",@$_GET['action']))),true);
$action=array_flip(array_values(split(",",@$_GET['action'])));
foreach($action as $k=>$v) $action[$k]=true;
}
else $action=array('form'=>true);
if(@$action['email']) {
// Set Mail Variables
$formdata=array();
$core="to from cc bcc subject message html file redirect account fields";
foreach($_GET as $k=>$v) {
if(preg_match("/\b$k\b/i",$core)) {
if(!preg_match("/\b$k\b/i","to from cc bcc")) $$k=$v;
}
else $formdata[$k]=$v;
}
foreach($_POST as $k=>$v) {
if(preg_match("/\b$k\b/i",$core)) $$k=$v;
else $formdata[$k]=$v;
}
$date=date("r");
$subject=isset($subject)? $subject:"No Subject";
$message=isset($message)? $message:"No Message";
// Help & Error Check
$usage=file_get_contents('formmailhelp.html');
// Missing addresses
if(!isset($to)||!isset($from)) exit("Missing to: or from: values
");
// Check for header injection: http://www.securephpwiki.com/index.php/Email_Injection
if(ereg("[\r\n]",$from)||ereg("[\r\n]",$to)) exit("Illegal To: or From: address
");
if(isset($cc)&&ereg("[\r\n]",$cc)||isset($bcc)&&ereg("[\r\n]",$bcc)) exit("Illegal CC: or BCC: address
");
// Main
$header="Date: $date\r\n";
$header.="From: $from\r\n";
$header.="Reply-To: $from\r\n";
if(isset($cc)) $header.="cc: $cc\r\n";
if(isset($bcc)) $header.="bcc: $bcc\r\n";
$header.="Message-ID: <".time()."-$from>\r\n";
$header.="X-Mailer: PHP v".phpversion()."\r\n";
// Prepare
$boundary=md5(time());
$attachment=isset($_FILES['attachment'])? $_FILES['attachment']: false;
if(empty($attachment['tmp_name'])) $attachment=false;
// Text message
if(isset($data)) {
$d=split(",",$data);
$data=array();
foreach($d as $k=>$v) $data[$v]=$formdata[$v];
$formdata=$data;
}
$fields="";
foreach($formdata as $k=>$v) $fields.="$k: $v\n";
if($fields) $fields="\n\nAdditional Data\n$fields";
$textmessage=$message.$fields;
// HTML message
if(isset($action['html'])) {
$fields="";
foreach($formdata as $k=>$v) $fields.="$k
$v
";
if($fields) $fields="Additional Data
$fields";
$message=text2p($message);
$htmlmessage=addslashes(file_get_contents('formmail.html'));
eval("\$htmlmessage=\"$htmlmessage\";");
//$htmlheader="MIME-Version: 1.0\nContent-Type: multipart/alternative; boundary=\"alt--$boundary\"\n\n";
$htmlheader="Content-Type: multipart/alternative; boundary=\"alternative--$boundary\"\n";
$htmlbody ="This is a multi-part message in MIME format.\n\n";
$htmlbody.="--alternative--$boundary\n";
$htmlbody.="Content-Type: text/plain; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 7bit\n\n";
$htmlbody.="$textmessage\n\n";
$htmlbody.="--alternative--$boundary\n";
$htmlbody.="Content-Type: text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 7bit\n\n";
$htmlbody.="$htmlmessage\n\n";
$htmlbody.="--alternative--$boundary--\n";
}
// Get Attachment
if($attachment) {
// Get Attachment
$fileTemp=$attachment['tmp_name'];
$fileType=$attachment['type'];
$fileName=$attachment['name'];
$data=chunk_split(base64_encode(file_get_contents($fileTemp)),72);
}
// Text Only Mail
if(!isset($html)&&!$attachment) {
mail($to,$subject,$textmessage,$header);
}
// HTML Mail Only
if(isset($html)&&!$attachment) {
mail($to,$subject,$htmlbody,$header.$htmlheader);
}
// With Attachment
if($attachment) {
// Header
$attachmentheader="Content-Type: multipart/mixed; boundary=\"mixed--$boundary\"\r\n\r\n";
// Body
$body="--mixed--$boundary\r\n";
if(isset($html)) {
$body.="$htmlheader\r\n$htmlbody\r\n";
}
else {
$body.="$textmessage\r\n";
}
$body.="--mixed--$boundary\r\n";
$body.="Content-Type: $fileType; name=\"$fileName\"\r\n";
$body.="Content-Transfer-Encoding: base64\nContent-Disposition: attachment\r\n\r\n";
$body.="$data\r\n";
$body.="--mixed--$boundary--\r\n";
mail($to,$subject,$body,$header.$attachmentheader);
}
if(isset($redirect)) { //&&!@$action['form'])
if($redirect=='.') $redirect=$_SERVER['HTTP_REFERER'];
header("Location: $redirect");
}
}
if(@$action['form']) printform();
?>