<?php
$ver = '5.4';
/* ------------------------------------------------------------------
FJA/NMLJA 21/03/2024
Generic responsive contact form 
------------------------------------------------------------------ -*/
include_once('includes/config.inc.php');
include_once(SITE_ROOT.'includes/functions.inc.php');
$DEBUG = 1;
$_SESSION['DEBUG_MESSAGE']  .= "<br />Contact Form Version: ".$ver;

$testmode = FALSE; /// TRUE:FALSE;
$show_map = FALSE;
$success_message = (isset($settings['form_success_message'])) ? htmlentities(stripslashes($settings['form_success_message']),ENT_QUOTES) : "Thank you for your enquiry we will respond as soon as possible";
// begin session
if (session_id() === "") { 
		session_start();
	   $_SESSION['DEBUG_MESSAGE'].= "<br>Session started by contact.php: ".$_SESSION['authenticatedUser'];
	   }
	     	$_SESSION['DEBUG_MESSAGE'] .= '<br>Session ID:'.session_id();
			$_SESSION['DEBUG_MESSAGE'] .= '<br>Enquiry Session Value 2:'.$_SESSION['enquiry'];
			$_SESSION['DEBUG_MESSAGE'] .= '<br>Test Mode:'.$testmode;	
			
// begin output buffering
ob_start();

// create session enquiry if id
if ($_GET['id']) {
	$remote_enquiry = $_SESSION['remote_enquiry'];
}

if ($testmode==TRUE) {
	$recipient = "frank.auffret@desktop-solutions.co.uk";
	$BCC = "frank.auffret15@gmail.com";
} else {
	$recipient = FORM_RECIPIENT;
	$BCC = FORM_BCC;
}
		$_SESSION['DEBUG_MESSAGE'] .= "<br />Recipient: ".$recipient;
		$_SESSION['DEBUG_MESSAGE'] .= "<br />BCC: ".$BCC;
		
$pageTitle ="Contact ".SITE_NAME;
$subject = html_entity_decode(SITE_NAME)." Website Enquiry";
$recipient_display_name = SITE_NAME;
$recipient_name = SITE_NAME;
$www_from_address = "noreply@".preg_replace('/^www./','',$_SERVER['HTTP_HOST']);

		$_SESSION['DEBUG_MESSAGE'] .= "<br />Recipient name: ".$recipient_name;
		$_SESSION['DEBUG_MESSAGE'] .= "<br />Display name: ".$recipient_display_name;
		$_SESSION['DEBUG_MESSAGE'] .= "<br />Subject: ".$subject;

		include_once(SITE_ROOT.'includes/header.inc.php');
?>
<div class="container" id="content">
	<div class="row">
	  <div class="col-md-12"> 
<?php
// create session variables use isset() to faciliate the removal of a text string for a input
$_SESSION['name'] = (isset($_POST['name'])) ? clean($_POST['name'],25) : $_SESSION['name'];
$_SESSION['address'] = (isset($_POST['address'])) ? clean($_POST['address'],50) : $_SESSION['address'];
$_SESSION['tel'] = (isset($_POST['tel'])) ? clean($_POST['tel'],30) : $_SESSION['tel'];
$_SESSION['town'] = (isset($_POST['town'])) ? clean($_POST['town'],25) : $_SESSION['town'];
$_SESSION['postcode'] = (isset($_POST['postcode'])) ? clean($_POST['postcode'],10) : $_SESSION['postcode'];
$_SESSION['email'] = (isset($_POST['email'])) ? clean($_POST['email'],120) : $_SESSION['email'];
$_SESSION['enquiry'] = (isset($_POST['enquiry'])) ? clean($_POST['enquiry'],5000) : $_SESSION['enquiry'];

/* ----------------------- captcha NOT IN USE unless use capta is true--------------------------- */
$use_captcha = true;  // toggles captcha on/off
 $captcha_questions = array('2+3','5+1','4+3','6+2','5+4','7+3','9+2','5+7','4+9','6+8','9+6');
  $captcha_answers = array(5,6,7,8,9,10,11,12,13,14,15);
   $_SESSION['captcha_index'] = (empty($_SESSION['captcha_index'])) ? rand(0,10) : $_SESSION['captcha_index'];
    $captcha_question = 'Enter the sum of '.$captcha_questions[$_SESSION['captcha_index']];
	 $captcha_answer = $captcha_answers[$_SESSION['captcha_index']];

		$_SESSION['DEBUG_MESSAGE'] .= "<br />Email: ".$_SESSION['email'];

print ('<h1>'.$pageTitle.'</h1>');

switch($_POST['mode']) {

	case 'post':

$_SESSION['error_message']='';
$message = "";
/* ----------------------------------------------------- */
// check content of bot traps
if  (!(empty($_POST['email_']))) {
			$_SESSION['isSpam'] = 1;
	  }
if  (!(empty($_POST['city']))) {
			$_SESSION['isSpam'] = 1;
	  }

if ($use_captcha) {
		if 	($_POST['captcha'] != $captcha_answer) {
		     	$_SESSION['error_message'] .= "Invalid captcha value!<br />";
				unset($_SESSION['captcha_index']);
		}
}
/* ----------------------------------------------------- */

if (empty($_SESSION['name'])) {
	     	$_SESSION['error_message'] .= "Your name is missing<br />";
	   } 
            else {
		$message .= "Name: ".stripslashes($_SESSION['name'])."\n";
           }

if ($_SESSION['address']) {
	     	$message .= "Address: ".stripslashes($_SESSION['address'])."\n";
	   }

if ($_SESSION['town']) {
	     	$message .= "City: ".stripslashes($_SESSION['town'])."\n";
	   }

if ($_SESSION['postcode']) {
	     	$message .= "Postcode: ".stripslashes($_SESSION['postcode'])."\n";
	   }

if (!(validate_telephone($_SESSION['tel']))) {
		$_SESSION['error_message'] .= "Your telephone number is invalid!<br />";
	   } 
            else {
		$message .= "Telephone: ".stripslashes($_SESSION['tel'])."\n";
	   }

if (empty($_SESSION['email'])) {
	     	$_SESSION['error_message'] .= "Your email is missing<br />";
	   } 
	    else if (!(validate_email($_SESSION['email']))) {
		$_SESSION['error_message'] .= "Your email address is invalid!<br />";
	   } 
	    else {
		$message .= "Email: ".stripslashes($_SESSION['email'])."\n";
	   }

if ($_SESSION['enquiry']) {
	     	$message .= "Enquiry: ".stripslashes($_SESSION['enquiry'])."\n";
	}

if (!(empty($_SESSION['error_message']))) {
	      	header('Location:/contact-us/failed');
			exit();
	}
         else {

	//process the form

/* -------- create headers ------- */
	//$headers = "From: ".$_SESSION['email']."\r\n";
	$headers = "From: ".SITE_NAME." <noreply@robsontractors.co.uk>" . "\r\n";
	$headers .= "Reply-To: " . stripslashes($_SESSION['name']) . "<" . $_SESSION['email'] . ">\r\n";
	$headers .= "Return-Path: info@robsontractors.co.uk" . "\r\n";
	$headers .= "Organization: Robson Tractors" . "\r\n";
  	$headers .= "MIME-Version: 1.0" . "\r\n";
  	$headers .= "Content-type: text/plain; charset=iso-8859-1" . "\r\n";
  	$headers .= "X-Priority: 3" . "\r\n";
  	$headers .= "X-Mailer: PHP". phpversion() . "\r\n";
	

	if (!(empty($BCC))) {
 		$headers .= "BCC: ".$BCC."\r\n";
	}

	$message = "Enquiry Data\n".$message;
	$message.= "\n\nForm to mail by Desktop Solutions Ltd";

/* --------------- Spam reduction ------------------- */
if ($_SESSION['isSpam']==true) {
		// add to database
			record_enquiry('SPAM');
			unset($_SESSION['isSpam']);
			unset_the_session();
			header('Location: /contact-us/spam');
			exit();
/* -------- Dont send mail ------- */
}
 else {
/* -------- send mail ------- */

    mail($recipient,$subject,$message,$headers,'-fwebmaster@robsontractors.co.uk'); 
	// add to database
			record_enquiry($_SESSION['name']);
}
			unset_the_session();

/* -------- print response ------- */
$_SESSION['success_message'] = $success_message;
	      	header('Location: /contact-us/sent');
			exit();

 }     // ends process the form
	break;

	default:
?>
    <p>Please use the form below to contact <?=SITE_NAME; ?>. Simply enter your details and 
       enquiry then click the <b>Send Enquiry</b> button below:-</p>
	<p>Alternatively, please contact us at:<br />
    <?=ADDRESS; ?> <?=POSTCODE; ?>
	<br />tel. <?=TELEPHONE; ?>
<?php
if (FAX) {
	print ('<br />fax. '.FAX);
}
?>
</p>
<?php
if($show_map) {
?>
<!-- locate us on streetmap-->
<p>Find us on 
<?php
$post_code = preg_replace('/ /','',POSTCODE);
?>
<a target="_blank" href="http://streetmap.co.uk/postcode/<?=$post_code;?>">Street Map</a></p>
<!-- end locate us on streetmap-->
<?php
}
if (!(empty($_SESSION['error_message']))) {
	print ('<p class="bg-danger"><b>ERROR!</b><br>'.$_SESSION['error_message'].'</p>');
	unset($_SESSION['error_message']);
}
if (!(empty($_SESSION['success_message']))) {
?>
	 <p class="bg-success"><?=$_SESSION['success_message']; ?></p>
<?php
	unset($_SESSION['success_message']);
}
?> 
	<!-- bootstrap form code-->
		<form class="form-horizontal" action="<?=$_SERVER['PHP_SELF']; ?>" method="post" id="myForm" role="form">
			<div class="form-group">
				<label for="name" class="control-label col-xs-2">Name</label>
					<div class="col-md-5 col-xs-10">
						<input type="text" name="name" id="name" class="form-control" value="<?=$_SESSION['name']; ?>" required>
						<!--span class="glyphicon glyphicon-asterisk form-control-feedback"></span-->
					</div>
			</div>

			<div class="form-group">
				<label for="address" class="control-label col-xs-2">Address</label>
					<div class="col-md-5 col-xs-10">
						<input type="text" name="address" id="address" class="form-control" value="<?=$_SESSION['address']; ?>">
					</div>
			</div>
						
			<div class="form-group">
			 <label for="town" class="control-label col-xs-2">Town/city:</label>
			  <div class="col-md-5 col-xs-10">
			   <input type="text" name="town" id="town" class="form-control" value="<?=$_SESSION['town']; ?>">
			  </div>
			</div> 
			
			<div class="form-group noshow">
			 <label for="city" class="control-label col-xs-2">City:</label>
			  <div class="col-md-5 col-xs-10">
			   <input type="text" name="city id="city" class="form-control">
			   <span class="form-caption">Leave empty</span>
			  </div>
			</div>

			<div class="form-group">
			 <label for="postcode" class="control-label col-xs-2">Post Code:</label>
			  <div class="col-md-5 col-xs-10">
			   <input type="text" name="postcode" id="postcode" class="form-control" id="town" value="<?=$_SESSION['postcode']; ?>">
			  </div>
			</div>

			<div class="form-group">
			 <label for="telephone" class="control-label col-xs-2">Telephone:</label>
			  <div class="col-md-5 col-xs-10">
			   <input type="text" name="tel" id="tel" class="form-control" value="<?=$_SESSION['tel']; ?>" required>
			  </div>	
			</div>

			<div class="form-group">
			 <label for="email" class="control-label col-xs-2">Email:</label>
			  <div class="col-md-5 col-xs-10">
			   <input type="text" name="email" id="email" class="form-control" value="<?=$_SESSION['email']; ?>" required>
			  </div>
			</div>

			<div class="form-group noshow">
			 <label for="email_" class="control-label col-xs-2">Email:</label>
			  <div class="col-md-5 col-xs-10">
			   <input type="text" name="email_" id="email_" class="form-control">
			   <span class="form-caption">Leave empty</span>
			  </div>
			</div>

			<div class="form-group">
			 <label for="enquiry" class="control-label col-xs-2">Your Enquiry:</label>
			  <div class="col-md-5 col-xs-10">
					<textarea name="enquiry" rows="5" id="enquiry" class="form-control" ><?=stripslashes($_SESSION['enquiry'].$remote_enquiry); ?></textarea>
			  </div>
			</div>
			
<?php
 if ($use_captcha){ ?>			
			<div class="form-group">
			 <label for="enquiry" class="control-label col-xs-2">Are you human?</label>
				<div class="col-md-5 col-xs-10">
					<input type="text" name="captcha" id="captcha" class="form-control" style="display:inline;width: 50px;" value=""> (<?=$captcha_question; ?>)
				</div>
			</div>	
<?php } ?>
			<div class="form-group">
			 <div class="col-xs-offset-2 col-xs-10">
			  <button type="submit" class="btn btn-success" style="margin-left:0px;">Send</button>
			   <input type="hidden" name="mode" value="post">
			 </div>
			</div>
    	</form>
<?php
	break;
 }
ob_end_flush();
?>
	  </div>
	</div>
</div><!-- ends content div -->
<?php
include_once(SITE_ROOT.'includes/footer.inc.php');

/* ------------------------- unset session variables ------------------------ */
function unset_the_session() {
		unset($_SESSION['name']);
		unset($_SESSION['address']);
		unset($_SESSION['tel']);
		unset($_SESSION['town']);
		unset($_SESSION['postcode']);
		unset($_SESSION['email']);
		unset($_SESSION['enquiry']);
		unset($_SESSION['captcha_index']);
}
/* --------------------------------------------------------------------------- */
function record_enquiry($name='') {
global $connection;

$ip = $_SERVER['REMOTE_ADDR'];

$query = "INSERT INTO ".ENQUIRIES_TABLE." SET 
		name = '".mysqli_real_escape_string($connection,$name)."',
		address = '".mysqli_real_escape_string($connection,$_SESSION['address'])."',
		town = '".mysqli_real_escape_string($connection,$_SESSION['town'])."',
		postcode = '".mysqli_real_escape_string($connection,$_SESSION['postcode'])."',
		telephone = '".mysqli_real_escape_string($connection,$_SESSION['tel'])."',
		email = '".mysqli_real_escape_string($connection,$_SESSION['email'])."',
		enquiry = '".mysqli_real_escape_string($connection,$_SESSION['enquiry'])."',
		ip = '$ip',
		insert_date = NOW()";
		
	if (!(mysqli_query($connection,$query))) {
			show_mysqli_error('record enquiry');
	}
}
?>

