<?php 
$your_email ='noreply@constructionspex.com, track@njyp.com, contact@njyp.com';// <<=== update to your email address
session_start();
$errors = '';
$name = '';
$visitor_email = '';
$visitor_phone = '';
$user_message = '';
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$visitor_phone = $_POST['phone'];
$user_message = $_POST['message'];
	///------------Do Validations-------------
	if(empty($name)||empty($visitor_email))
	{
		$errors .= "\n Name and Email are required fields. ";	
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\n Bad email value!";
	}
	if(empty($_SESSION['6_letters_code'] ) ||
	  strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
	//Note: the captcha code is compared case insensitively.
	//if you want case sensitive match, update the check above to
	// strcmp()
		$errors .= "\n The code does not match!";
	}
	
	if(empty($errors))
	{
		//send the email
		$to = $your_email;
		$subject="Construction Specifications, Inc. Contact Form";
		$from = $visitor_email;
		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
		
		$body = "A user  $name submitted the contact form:\n".
		"Name: $name\n".
		"Email: $visitor_email \n".
		"Phone Number: $visitor_phone \n".
		"Message: \n ".
		"$user_message\n".
		"IP: $ip\n";	
		
		$headers = "From: $from \r\n";
		$headers .= "Reply-To: $visitor_email \r\n";
		
		mail($to, $subject, $body,$headers);
		
		header('Location: thanks.html');
	}
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<meta name="robots" content="noindex"/>
<style type="text/css">
@media screen and (max-width: 480px) {
}
.form, .text{ background:#fff; line-height:25px; width:90% !important; border:0; padding-left:15px; font-size:16px; color:#0e0d0d; max-width:100%;}
body{ font-family:Arial, Helvetica, sans-serifl; color:#fff; background:#f4f4f4}
.read-more{ position:relative;  line-height: 33px;  font-size:13px; height:33px; line-height:33px;  text-decoration: none; text-transform:uppercase; color: #fff; background:#444; padding:0px 23px 10px 23px;   text-shadow:none;  border:0; cursor:pointer;   -webkit-transition-duration: 0.3s;
  transition-duration: 0.3s;}
.read-more:hover{ opacity:1 !important; background:#111;}
.err{ color:#F00}
</style>
</head>
<body>
<strong style="color:#222; font-size:11px; padding-left:55px;">Required <span style="font-size:18px">*</span></strong>
<form class="contact" style="width:85%; margin:auto;" method="POST" name="contact_form" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> 
  <div class="form-p">
<label for='name'> </label>
<br>
<input class="form" type="text" name="name" placeholder="Name *" value='<?php echo htmlentities($name) ?>' required>
</div>
<div class="form-p">
<label for='email'></label><br>
<input class="form" type="email" name="email" placeholder="Email *" value='<?php echo htmlentities($visitor_email) ?>' required>
</div>
<div class="form-p">
<label for='phone'></label><br>
<input class="form" type="number" name="phone" placeholder="Phone *" value='<?php echo htmlentities($visitor_phone) ?>' required>
</div>
<div class="clear"></div>
<div>
<label for='message'></label> <br>
<textarea style="width:100%; font-size:16px;  padding-left:15px; font-family:Arial, Helvetica, sans-serif" class="text" name="message" placeholder="Tell us about your need *" rows=2 required><?php echo htmlentities($user_message) ?></textarea>
</div>
<div>
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div style="width:90px; height:30px; margin-top:30px;">
<img  class="img-form" src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' >
</div>
<br>
<label style="color:#666" for='message'>Enter the code above here :</label><br>
<input class="form" id="6_letters_code" name="6_letters_code" type="text"><br>
<!--
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small>
-->
<p>
<p style="text-align:center">
<input type="submit" class="read-more" value="Send My Information" name='submit'>
</p>
</p>
</div>
</form>
</body>
</html>
