<?php
//error_reporting(E_ALL);
//ini_set('display_errors', True);

// whh 2022.04.25  add the new form variables from index.html
// wgg 2022.06.21 update all varibles for new form

//echo "using Informprocess.php <br>";

	if (!isset($_SESSION)) session_start();
	if(!$_POST) exit;

	include dirname(__FILE__).'/settings/settings.php';
	include dirname(__FILE__).'/functions/emailValidation.php';


/*	---------------------------------------------------------------------------
	: Register all form field variables here
	--------------------------------------------------------------------------- */
	$firstname = strip_tags(trim($_POST["firstname"]));
	$address = strip_tags(trim($_POST["address"]));
	$telephone = strip_tags(trim($_POST["telephone"]));
	$emailaddress = strip_tags(trim($_POST["emailaddress"]));

	$captcha = strip_tags(trim($_POST["captcha"]));

	//echo "date1 & date2: " . $date1 ." : " . $date2 . "<br>";


/*	----------------------------------------------------------------------
	: Prepare form field variables for CSV export
	----------------------------------------------------------------------- */

	if($generateCSV == true){
		$csvFile = $csvFileName;
		$csvData = array(

			$_POST["firstname"],
			$_POST["address"],
			$_POST["telephone"],
			$_POST["emailaddress"],

			// $_POST['captcha']

		);
	}

/*	-------------------------------------------------------------------------
	: Prepare serverside validation
	------------------------------------------------------------------------- */

	$errors = array();


	/* validate firstname
	------------------------------------ */
	if(isset($_POST["firstname"])){
		if (!$firstname) {
			$errors[] = "You must enter your firstname";
		} elseif(strlen($firstname) < 2)  {
			$errors[] = "Firstname must be at least 2 characters";
		}
	}


	/* validate email address
	-------------------------------------- */
	if(isset($_POST["emailaddress"])){
		if (!$emailaddress) {
			$errors[] = "You must enter an email";
		} else if (!validEmail($emailaddress)) {
			$errors[] = "Your must enter a valid email";
		}
	}

	/* validate telephone number
	-------------------------------------- */
	if(isset($_POST["telephone"])){
		if (!$telephone) {
			$errors[] = "You must enter your telephone number";
		} elseif(!preg_match('/^[0-9]+$/', $telephone))  {
			$errors[] = "Please enter numbers only";
		} elseif(strlen($telephone) < 10)  {
			$errors[] = "Enter not less than 10 numbers";
		} elseif(strlen($telephone) > 12)  {
			$errors[] = "Enter not more than 12 numbers";
		}
	}


	/* validate security captcha
	--------------------------------------- */
$captest = $_SESSION['gfm_captcha'];
//echo "captcha: " . $captcha . "<br>";
//echo "cap string form: " . $captest . "<br><br>";
	if(isset($_POST["captcha"])){
		if (!$captcha) {
			$errors[] = "You must enter the captcha code";
		} else if (($captcha) != $_SESSION['gfm_captcha']) {
			$errors[] = "Captcha code is incorrect";
		}
	}

	/* output all errors as a list
	-------------------------------------- */
	if ($errors) {
		$errortext = "";
		foreach ($errors as $error) {
			$errortext .= '<li>'. $error . "</li>";
		}
		echo '<div class="alert notification alert-error"><strong>The following errors occured:</strong><br><ul>'. $errortext .'</ul></div>';

	} else {


			include dirname(__FILE__).'/phpmailer/PHPMailerAutoload.php';
			include dirname(__FILE__).'/templates/goldenmessage.php';

			$mail = new PHPMailer();
			$mail->isSendmail();
			$mail->IsHTML(true);
			$mail->From = $emailaddress;
			$mail->CharSet = "UTF-8";
			$mail->FromName = $firstname;
			$mail->Encoding = "base64";
			$mail->Timeout = 200;
			$mail->ContentType = "text/html";
			$mail->addAddress($receiver_email, $receiver_name);
			$mail->Subject = $receiver_subject . " - " . $firstname;

			$mail->Body = $goldmessage;
			$mail->AltBody = "Use an HTML compatible email client";

 //echo "Sending Message: <br>";
		/*	-------------------------------------------------------------------
			: Prepare sending to multiple  addresses / recepients if true
			------------------------------------------------------------------- */
			// If you want the form to be emailed to other addresses
			// Change the extra_recipients option below from (false) to (true)
			// Then enter email addresses with corresponding names seperated by comas
			// For example "john@example.com" => "John", "jack@example.com" => "Jack", "jeny@example.com" => "Jeny"
			// echo $goldmessage;
			// echo $automessage;
			$recipients = true;
			if($recipients == true){
				$recipients = array(
						"wo@toston.ca" => "Toston",
						"fritz@fritzherold.com" => "Fritz",
				);

				foreach($recipients as $email => $name){
					$mail->AddBCC($email, $name);
				}
			}


			if($mail->Send()) {

			/*	-----------------------------------------------------------------
				: Generate the CSV file and post values if its true
				----------------------------------------------------------------- */
				if($generateCSV == true){
					if (file_exists($csvFile)) {
						$csvFileData = fopen($csvFile, 'a');
						fputcsv($csvFileData, $csvData );
					} else {
						$csvFileData = fopen($csvFile, 'a');
						$headerRowFields = array(


							"firstname",
							"address",
							"telephone",
							"emailaddress"


						);
						fputcsv($csvFileData,$headerRowFields);
						fputcsv($csvFileData, $csvData );
					}
					fclose($csvFileData);
				}

			/*	---------------------------------------------------------------------
				: Send the auto responder message if its true - set in setting.php
				--------------------------------------------------------------------- */
				if($autoResponder == true){

					include dirname(__FILE__).'/templates/autoresponder.php';

					$automail = new PHPMailer();
					$automail->isSendmail();
					$automail->From = $receiver_email;
					$automail->FromName = $receiver_name;
					$automail->isHTML(true);
					$automail->CharSet = "UTF-8";
					$automail->Encoding = "base64";
					$automail->Timeout = 200;
					$automail->ContentType = "text/html";
					$automail->AddAddress($emailaddress, $firstname, $lastname);
					$automail->Subject = "Thank you for contacting us";
					$automail->Body = $automessage;
					$automail->AltBody = "Use an HTML compatible email client";
					$automail->Send();
				}


				echo '<div class="alert notification alert-success">Message has been sent successfully!</div>';
			}
			else {
			  	echo '<div class="alert notification alert-error">Message not sent - an error occured!</div>';
			}
	}
?>
