<?php

/*

Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew

Modified by Ben Huddleston.  http://www.minc.info, http://www.redwingstudio.com


This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.

*/

if(!isset($_POST['action'])) {
    die(
"You must not access this page directly!"); //Just to stop people from visiting contact.php 
    
}

/*
safe_mail()
Fix all of the bloody exploits for MIME and whatever else, such as injection.

Strip all of the LF and CR, and then make sure there's no MIME or boundary information:
version 1.0.0 (28 Oct 2005)
*/    

function safe_mail($p_to,$p_subject,$p_message,$p_headers=null,$p_paramaters=null)
{
    
$format_good 1;
    
    if (
eregi("\r",$p_to) || eregi("\n",$p_to)){
        
$format_good 0;
    }
    if (
eregi("\r",$p_subject) || eregi("\n",$p_subject)){
        
$format_good 0;
    }
    if (
eregi("\r",$p_headers) || eregi("\n",$p_headers)){
        
$format_good 0;
    }
    
    
//now check for MIME encoding in the $message field
    
$mimecount substr_count(strtolower($p_message), strtolower("Content-Type"));    
    if(
$mimecount!=0)
    {
        
$format_good 0;
    }
    
$mimecount substr_count(strtolower($p_message), strtolower("boundary="));    
    if(
$mimecount!=0)
    {
        
$format_good 0;
    }
    
//afew more checks just incase we miss the to and from, etc:
    
$mimecount substr_count(strtolower($p_to), strtolower("Content-Type"));    
    if(
$mimecount!=0){$format_good 0;}
    
$mimecount substr_count(strtolower($p_to), strtolower("boundary="));    
    if(
$mimecount!=0){$format_good 0;}
    
$mimecount substr_count(strtolower($p_subject), strtolower("Content-Type"));    
    if(
$mimecount!=0){$format_good 0;}
    
$mimecount substr_count(strtolower($p_subject), strtolower("boundary="));    
    if(
$mimecount!=0){$format_good 0;}
    
$mimecount substr_count(strtolower($p_message), strtolower("Content-Type"));    
    if(
$mimecount!=0){$format_good 0;}
    
$mimecount substr_count(strtolower($p_message), strtolower("boundary="));    
    if(
$mimecount!=0){$format_good 0;}    
    
$mimecount substr_count(strtolower($p_headers), strtolower("Content-Type"));    
    if(
$mimecount!=0){$format_good 0;}
    
$mimecount substr_count(strtolower($p_headers), strtolower("boundary="));    
    if(
$mimecount!=0){$format_good 0;}    
    
    
    
//now if $format_good is 1, we are safe, if not, we log it.
    
if($format_good==1)
    {
        
$result mail($p_to,$p_subject,$p_message,$p_headers);
        return(
$result);
    
    }else
    {
        
//$script_location = $_SERVER["SCRIPT_FILENAME"];
        //$attempt_from = $_SERVER["REMOTE_HOST"]."(".$_SERVER["REMOTE_ADDR"].")";
    
        //$data ="============================================\nsafe_mail: $attempt_from $script_location \n$p_to||$p_subject||$p_message||$p_headers||$p_paramaters||\n\n";
        //$fhandle = fopen("/tmp/emailinjection.log.txt", "a+"); 
    //    fwrite($fhandle,$data);
    //    fflush($fhandle);
    //    fclose($fhandle);  
        
echo "Bad Data.  Your submission included invalid information!";
        return(
FALSE);
    }
    return(
FALSE);
}

/* Now lets trim up the input before sending it */

$firstname trim($_POST['first_name']); //The senders name
$lastname trim($_POST['last_name']);
$email trim($_POST['email']); //The senders email address
$phone trim($_POST['phone']); //The senders subject
$comments trim($_POST['comments']); //The senders message
$name "$firstname $lastname";

$timenow date("m-d-Y");
$subject "Contact Form";
$browser getenv('HTTP_USER_AGENT');
$ip getenv('REMOTE_ADDR');
                                            
// format message
$message "
Date: $timenow
Name: $firstname $lastname
Email: $email
Phone: $phone
-----------------------------
Comments: $comments    
-----------------------------

IP: $ip
Browser: $browser


** Note all email addresses have been stripped of the 'at' sign and replaced with '_at_'. This is to protect the form from being used as a spam relay. **
"
;

// want to send an SMS message? send it via teleflip
// format the teleflip message                            
$message2 "
$firstname $lastname
($areacode) $localcode-$phonenumbers
"
;


function 
check_firstname($field_name_1)
{
  if(
preg_match("/[a-zA-Z\-\ ]+$/s",$field_name_1))
         return 
TRUE;
  else
    return 
FALSE;
}

function 
check_lastname($field_name_2)
{
  if(
preg_match("/[a-zA-Z\-\ ]+$/s",$field_name_2))
    return 
TRUE;
  else
    return 
FALSE;
}

function 
check_email($field_name_3)
{
  if(
preg_match('#^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$#',$field_name_3))
    return 
TRUE;
  else
    return 
FALSE;
}

function 
check_phone($field_name_4)
{
  if(
preg_match("/[0-9\-\ \.]+$/s",$field_name_4))
    return 
TRUE;
  else
    return 
FALSE;
}


/* Validation */
$error=0// check up variable
$errormsg '<ul class="errorlist">';

/* get it checking */


if(!check_firstname($firstname))
{
  
$errormsg.= "<li class='errormessage'>ERROR: not a valid name.</li>";
  
$error++; // $error=$error+1;
}
if(!
check_lastname($lastname))
{
  
$errormsg.= "<li class='errormessage'>ERROR: not a valid name.</li>";
  
$error++;
}
if(!
check_email($email))
{
  
$errormsg.=  "<li class='errormessage'>ERROR: not a valid email.</li>";
  
$error++;
}
if(!
check_phone($phone))
{
  
$errormsg.=  "<li class='errormessage'>ERROR: not a valid phone number.</li>";
  
$error++;
}

$errormsg .= '</ul>';


if(
$error == 0) {
    
// no errors so mail the message
    
$hurrah safe_mail("youremail@yourdomain.com",$subject,$message,"$email");
    
// send the SMS?  uncomment line below and replace 1231231234 with the appropriate cell phone number
    //$hurrah = safe_mail("1231231234@teleflip.com",$Subject,$message); 
     
    //now lets update the "contactarea" div on the contact.html page. 
    //The contactarea| tell's the javascript which div to update.
    
echo 'contactarea|Thank you '.$name.', your email has been sent.';
 } else {
     
//echo 'contactarea|There is an error your email has not been sent.'; 
 
echo 'error|'$errormsg;
 }
 

?>