<?php
require_once("includes/config.inc.php");

// set up error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$pageTitle = "Contact Confirmation";
$pageDescription = "Welcome to My Contact Confirmation page.";
$sideBar = "";
//email variables
$admin_email = "knudtsonj1@students.westerntc.edu";//MAKE SURE TO PUT YOUR EMAIL ADDRESS HERE!!!
$email_subject = "jknudtson - Contact Form Submitted";

require("includes/header.inc.php");

//Form variables
$first_name = "";
$last_name ="";
$email = "";
$comments = "";
$error_messages = array();
$confirmation_message = "";

//Form Posted?
if($_SERVER['REQUEST_METHOD'] == "POST"){
  
	//Get submitted data
	$email = $_POST['txtEmail'];
	$first_name = $_POST['txtFirstName'];
	$last_name = $_POST['txtLastName'];
	$comments = $_POST['txtComments'];

	//Validation
	if(empty($first_name)){
		$error_messages['first_name'] = "Your first name is required";
	}
  	if(empty($last_name)){
		$error_messages['last_name'] = "Your last name is required";
	}
	if(empty($comments)){
		$error_messages['comments'] = "Please enter a question or comment";
	}
	if(empty($email)){
  		$error_messages['email'] = "Your email address is required";
	}elseif(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
		$error_messages['email'] = "The email address entered is not valid";
	}

	//Spam Scrubber
	if(url_exists_in_text($email) || url_exists_in_text($first_name) || url_exists_in_text($last_name) || url_exists_in_text($comments)){
		$error_messages['urls'] = "URLs are not permitted.";
	  }
	$email = spam_scrubber($email);
	$first_name = spam_scrubber($first_name);
	$last_name = spam_scrubber($last_name);
	$comments = spam_scrubber($comments);

	if(empty($email) || empty($first_name) || empty($last_name) || empty($comments)){
		$error_messages['spam'] = "Spam detected!";
	  }

  // If there were or validation errors, then send the info
  // Otherwise show the validation errors
  if(empty($error_messages)){
    // send the email
		if(send_email($admin_email, $email_subject, $first_name, $last_name, $email, $comments)){
			$confirmation_message = "<h3>Thank you for contacting me. I will get back to you as soon as I can</h3>";
		}else{
			
			$confirmation_message = "<h3 class=\"validation\">There was an error submitting your information.</h3>";
    }
	}else{
    // There are input validation errors, so show them
    $confirmation_message = "<h3 class=\"validation\">There were input validation errors.</h3>";
    foreach($error_messages as $key => $value){
     $confirmation_message .= '<br><span class="validation">' . $value . '</span>';
    }
  }

}

function send_email($to, $subject, $first_name, $last_name, $from_email, $comments){

$body = <<<EOD
  <html>
  <head><title> CONTACT FORM</title></head>
  <body>  
  <b>Name:</b> {$first_name} {$last_name}<br>
  <b>Email:</b> {$from_email}<br>
  <b>Comment:</b> {$comments}
EOD;

  // To send HTML mail, the Content-type header must be set
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

  // Additional headers
  $headers .= 'To: Website Admin <'. $to .'>' . "\r\n";
  $headers .= 'From: '. $first_name . ' ' . $last_name .' <' .  $from_email . '>' . "\r\n";
  
	return mail($to, $subject, $body, $headers);

}


//I got this function from the Larry Ulman book: PHP and MySQL for Dynamic Websites
function spam_scrubber($value){

  // values that need to be remove...
  $very_bad = ["to:", "cc:", "bcc:", "content-type:", "mime-version:", "multipart-mixed:", "content-transfer-encoding:"];

  // if we find any very bad things in the $value, we'll just return an empty string
  foreach ($very_bad as $v) {
    if(strpos($value, $v) !== false){
      return "";
    }
  }

  // replace any new line characters with spaces:
  $value = str_replace(array("\r", "\n", "%0a", "%0d"), " ", $value);

  return trim($value);
}


function url_exists_in_text($text){

  // The Regular Expression filter
  //$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
  $reg_exUrl = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";
  return preg_match($reg_exUrl, $text, $url);

}


?>
      <main>
        <div class="content-frame">
            <?php echo($confirmation_message); ?>
        </div>
      </main>
<?php
if(!empty($sideBar)){
	require("includes/" . $sideBar);
}

require("includes/footer.inc.php");
?>