<?php
// error_reporting(-1);
// ini_set('display_errors', 1);
// echo "Form Debug: I am here BEFORE checking reCaptcha";
// exit;
////////////////////////////////// BOF reCaptcha //////////////////////////////////////////////////////
//echo $_POST['captcha_token'];
if(isset($_POST['captcha_token'])) {
   // RECAPTCHA SETTINGS
   $captcha = $_POST['captcha_token'];
   $ip = $_SERVER['REMOTE_ADDR'];
   $key = '6LcLZnwUAAAAALApzkpcniMCFAW8-Nskg8TPqU2l';
   $url = 'https://www.google.com/recaptcha/api/siteverify';
   //echo $full_url;
   //echo "<br />";
   // RECAPTCH RESPONSE
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "$url");
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "secret=$key&response=$captcha");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$output = curl_exec($ch);
	curl_close($ch);
	//echo $output;

   $data = json_decode($output);
   //print_r($data);
 
   if(isset($data->success) &&  $data->success === false) {
       
      echo('reCaptcha Invalid');
      exit;
   }
}
//echo "Form Debug: I am here after checking reCaptcha";
//exit;
////////////////////////////////// EOF reCaptcha ///////////////////////////////////////////////////////

///////////////// Host must be www.hdtvsupply.com /////////////////////////
// if($_SERVER['HTTP_HOST'] != "p11.secure.hostingprod.com" )
// {
//      die("Not Valid HOST ");
// 	 exit;
// }


///////////////// Method must be post /////////////////////////
// if($_SERVER['HTTP_REFERER'] != "https://www.hdtvsupply.com/contactus.html" )
// {
//      die("Referer NOT correct.");
// 	 exit;
// }


///////////////// Method must be post /////////////////////////
if($_SERVER['REQUEST_METHOD'] != "POST" )
{
     die("The request method is not POST.");
	 exit;
}
///////////// So a missing HTTP_USER_AGENT value almost always indicates a spammer bot. ////////////
if(empty($_SERVER['HTTP_USER_AGENT']))
{
     die("The request is not valid as user agent.");
	 exit;
}

///////////////// My code value must be the same /////////////////////////
if($_POST['mycode'] != md5('contactus_hdtvsupply.com'))
{
    die("My code is wrong.");
	exit;
} 
/**************************************************************************************/ 
//////////////////// BOF Stop Bad Form Requests //////////////////////////////////////
/**************************************************************************************/ 
//////////////////////////////////////// BOF MX Record /////////////////////////// 

/* checkdnsrr($domain_name, "MX OR A")
 * http://php.net/manual/en/function.checkdnsrr.php
 * checkdnsrr — Check DNS records corresponding to a given domain name (host name) or IP address
 * host may either be the IP address in dotted-quad notation or the host name. 
 * type may be any one of: A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT or ANY. 
 * Returns TRUE if any records are found; returns FALSE if no records were found or if an error occurred.  
/*------------------------ GET DOMAIN NAME FROM EMAIl ---------------------------------*/
///////////////// Check MX Record /////////////////////////	 

		
				
		
				$hdmi = $_POST["hdmi"];
				$cat6 = $_POST["cat6"];
				$dvi = $_POST["dvi"];
				$vga = $_POST["vga"];
				$sdi = $_POST["sdi"];
				$fiber = $_POST["fiber"];
				$component = $_POST["component"];
				$composite = $_POST["composite"];
				$rlaudio = $_POST["rlaudio"];
				
				$hdmioutputs = $_POST["hdmioutputs"];
				$cat6outputs = $_POST["cat6outputs"];
				$dvioutputs = $_POST["dvioutputs"];
				$vgaoutputs = $_POST["vgaoutputs"];
				$sdioutputs = $_POST["sdioutputs"];
				$fiberoutputs = $_POST["fiberoutputs"];
				$componentoutputs = $_POST["componentoutputs"];
				$compositeoutputs = $_POST["compositeoutputs"];
				$rlaudiooutputs = $_POST["rlaudiooutputs"];
				
				$res1080p = $_POST["res1080p"];
				$res4k30 = $_POST["res4k30"];
				$res4k60 = $_POST["res4k60"];
				
				$vw2x2 = isset($_POST["vw2x2"]) ? "Yes" : "";
				$vw3x3 = isset($_POST["vw3x3"]) ? "Yes" : "";
				$vwother = $_POST["vwother"];
				
				$iosapp = isset($_POST["iosapp"]) ? "Yes" : "";
				$androidapp = isset($_POST["androidapp"]) ? "Yes" : "";
				$webgui = isset($_POST["webgui"]) ? "Yes" : "";
				$pcsoftwarectrl = isset($_POST["pcsoftwarectrl"]) ? "Yes" : "";
				$frontpanelbuttons = isset($_POST["frontpanelbuttons"]) ? "Yes" : "";
				$frontpaneltouchscreen = isset($_POST["frontpaneltouchscreen"]) ? "Yes" : "";
				$remotecontrol = isset($_POST["remotecontrol"]) ? "Yes" : "";
				$rs232 = isset($_POST["rs232"]) ? "Yes" : "";
				$tcpip = isset($_POST["tcpip"]) ? "Yes" : "";
				$control4 = isset($_POST["control4"]) ? "Yes" : "";
				$creston = isset($_POST["creston"]) ? "Yes" : "";
				$elan = isset($_POST["elan"]) ? "Yes" : "";
				$rti = isset($_POST["rti"]) ? "Yes" : "";
				$sportsbarsoftware = isset($_POST["sportsbarsoftware"]) ? "Yes" : "";
				$c3rparty = isset($_POST["c3rparty"]) ? "Yes" : "";
				
				$name = $_POST["name"];
				$phone = $_POST["phone"];
				$orderdate = $_POST["orderdate"];
				$installdate = $_POST["installdate"];
				$budgetrange = $_POST["budgetrange"];
				$email = $_POST["email"];
				
				$ienone = $_POST["ienone"];
				$iesome = $_POST["iesome"];
				$iealot = $_POST["iealot"];
				
				$inputlabellength = $_POST["inputlabellength"];
				$outputlabellength = $_POST["outputlabellength"];
				$st2sec = $_POST["st2sec"];
				$st100ms = $_POST["st100ms"];
				
				$comments = $_POST["comments"];
				
				$message = "<h2>Build Your Own Product Form Submission:</h2><br />\r\n";
				$message .= "<b>Name:</b> $name<br /><br />\r\n";
				$message .= "<b>Phone:</b> $phone<br /><br />\r\n";
				$message .= "<b>Order Date:</b> $orderdate<br /><br />\r\n";
				$message .= "<b>Install Date:</b> $installdate<br /><br />\r\n";
				$message .= "<b>Budget:</b> $budgetrange<br /><br />\r\n";
				$message .= "<b>Email:</b> $email<br /><br />\r\n";
				
				$message .= "<h3>Qty Of Inputs:</h3>\r\n";
				
				$message .= "<b>HDMI:</b> $hdmi<br /><br />\r\n";
				$message .= "<b>HDBaseT (Cat6):</b> $cat6<br /><br />\r\n";
				$message .= "<b>DVI:</b> $dvi<br /><br />\r\n";
				$message .= "<b>VGA:</b> $vga<br /><br />\r\n";
				$message .= "<b>SDI:</b> $sdi<br /><br />\r\n";
				$message .= "<b>Fiber:</b> $fiber<br /><br />\r\n";
				$message .= "<b>Component:</b> $component<br /><br />\r\n";
				$message .= "<b>Composite:</b> $composite<br /><br />\r\n";
				$message .= "<b>rlaudio:</b> $rlaudio<br /><br />\r\n";
				$message .= "<b>Input Label Length:</b> $inputlabellength<br /><br />\r\n";
				
				$message .= "<h3>Qty Of Outputs:</h3>\r\n";
				
				$message .= "<b>HDMI:</b> $hdmioutputs<br /><br />\r\n";
				$message .= "<b>HDBaseT (Cat6):</b> $cat6outputs<br /><br />\r\n";
				$message .= "<b>DVI:</b> $dvioutputs<br /><br />\r\n";
				$message .= "<b>VGA:</b> $vgaoutputs<br /><br />\r\n";
				$message .= "<b>SDI:</b> $sdioutputs<br /><br />\r\n";
				$message .= "<b>Fiber:</b> $fiberoutputs<br /><br />\r\n";
				$message .= "<b>Component:</b> $componentoutputs<br /><br />\r\n";
				$message .= "<b>Composite:</b> $compositeoutputs<br /><br />\r\n";
				$message .= "<b>rlaudio:</b> $rlaudiooutputs<br /><br />\r\n";
				$message .= "<b>Output Label Length:</b> $outputlabellength<br /><br />\r\n";
				
				$message .= "<h3>Resolution:</h3>\r\n";
				
				$message .= "<b>1080p:</b> $res1080p<br /><br />\r\n";
				$message .= "<b>4K 30:</b> $res4k30<br /><br />\r\n";
				$message .= "<b>4K 60 ($):</b> $res4k60<br /><br />\r\n";
				
				$message .= "<h3>Video Walls:</h3>\r\n";
				
				$message .= "<b>2x2:</b> $vw2x2<br /><br />\r\n";
				$message .= "<b>3x3:</b> $vw3x3<br /><br />\r\n";
				$message .= "<b>Other:</b> $vwother<br /><br />\r\n";
				
				$message .= "<h3>Control:</h3>\r\n";
				
				$message .= "<b>iOS App:</b> $iosapp<br /><br />\r\n";
				$message .= "<b>Android App:</b> $androidapp<br /><br />\r\n";
				$message .= "<b>WEB GUI:</b> $webgui<br /><br />\r\n";
				$message .= "<b>PC Software:</b> $pcsoftwarectrl<br /><br />\r\n";
				$message .= "<b>Front Panel Buttons:</b> $frontpanelbuttons<br /><br />\r\n";
				$message .= "<b>Front Panel Touchscreen:</b> $frontpaneltouchscreen<br /><br />\r\n";
				$message .= "<b>Remote Control:</b> $remotecontrol<br /><br />\r\n";
				$message .= "<b>RS232:</b> $rs232<br /><br />\r\n";
				$message .= "<b>TCPIP:</b> $tcpip<br /><br />\r\n";
				$message .= "<b>Control4:</b> $control4<br /><br />\r\n";
				$message .= "<b>Creston:</b> $creston<br /><br />\r\n";
				$message .= "<b>Elan:</b> $elan<br /><br />\r\n";
				$message .= "<b>RTI:</b> $rti<br /><br />\r\n";
				$message .= "<b>Sports Bar Software:</b> $sportsbarsoftware<br /><br />\r\n";
				$message .= "<b>3rd Party:</b> $c3rparty<br /><br />\r\n";
				
				$message .= "<h3>Install Experience:</h3>\r\n";
				
				$message .= "<b>None:</b> $ienone<br /><br />\r\n";
				$message .= "<b>Moderate:</b> $iesome<br /><br />\r\n";
				$message .= "<b>Experienced:</b> $iealot<br /><br /><br />\r\n";
				
				$message .= "<h3>Switch Time:</h3>\r\n";
				
				$message .= "<b>~2 Sec:</b> $st2sec<br /><br />\r\n";
				$message .= "<b>100ms ($):</b> $st100ms<br /><br /><br /><br />\r\n";
				
				$message .= "<b>Comments:</b> $comments<br /><br />\r\n";
				
				
				//$headers = "Content-Type: text/html; charset=UTF-8\r\n";

				//$r = @mail("support@hdtvsupply.com", "Build Your Own Product Form Submission", $message, $headers);
				//$r = @mail("sj@samyakonline.net", "Build Your Own Product Form Submission", $message, $headers);
				//$r = @mail("kostyai@gmail.com", "Build Your Own Product Form Submission", $message, $headers);
		
		
$mail = $_POST['email'];
$domain = strstr($mail, '@');
$domain_name =  substr($domain, 1);
if (!checkdnsrr($domain_name, "MX"))
{
	    echo "Not valid MX Record";
		exit;
	
}	 
//////////////////////////////////////// EOF MX Record ////////////////////////////

///////////////// Token does not work due to common form  /////////////////////////
/*if(($_SESSION['token'] != $_POST['token']))
{
    die("The token is wrong.");
	exit;
} 
*/
///////////////// Trick to prevent bot: If this field is with value then bot has sent it /////////////////////////
if(isset($_POST['bot_kiiler']) && trim($_POST['bot_kiiler']) != '') 
{
     die("The request is not valid.");
	 exit;
}
//echo "success";
//exit;
/**************************************************************************************/ 
//////////////////// EOF Stop Bad Form Requests //////////////////////////////////////
/**************************************************************************************/ 

/**************************************************************************************/ 
//////////////////// BOF Input Filtering  ////////////////////////////////////////////
/**************************************************************************************/ 
//////////////////////////////////// BOF Form Processing /////////////////////////////////////////////////
define('ONLINE', true);
date_default_timezone_set('America/Los_Angeles');
require 'phpmailer/PHPMailerAutoload.php';
if(ONLINE)
{
	//define('DOMAIN', 'https://p11.secure.hostingprod.com/@site.hdtvsupply.com/ssl/hdtvsupplycp');	
	define('DOMAIN', 'https://forms.hdtvsupply.com/myforms');	   
	
	   
}
else
{
	define('DOMAIN', 'http://192.168.1.21/hdtvsupply/ssl/hdtvsupplycp/quote');
		 
}

   //Returns the names and values of all the constants currently defined. 
   //This includes those created by extensions as well as those created with the define() function. 
   $myconst = get_defined_constants(); // {$myconst['DOMAIN']}
 
function cleanUserData($user_data)
 {
                $clean_data = trim(strip_tags($user_data));
                return $clean_data;
 }


	$name = ucwords(cleanUserData($_POST['name']));
	$email = cleanUserData($_POST['email']);
	$comments = cleanUserData($_POST['comments']);
	

	$response_data = array();
	   /*------------------------ check name ---------------------------------*/   	
	$reg_name = "/^[a-zA-Z\s\.]+$/";
	$nm = "/^[1-9][0-9]*$/";
	if(empty($name))
		{
	
			$response_data['name'] = 'Name is required.';
			//echo $response_data['name'];
		}
		
		
	if(!preg_match($reg_name, $name))
		{
	
			$response_data['fname'] = 'The name can only contain alphabets.';
			//echo $response_data['name'];
		}
		
	   /*------------------------ check mobile ---------------------------------*/   		
		//if(empty($mobile))
		//{		
		//	$response_data['mobile'] = 'Contact number is required.';
			//echo $response_data['name'];
		//}  		

		
	   /*------------------------ check email ---------------------------------*/   
		if(empty($email))
			{
		
				$response_data['email'] = 'Email is required.';
				//echo $response_data['name'];
			}	
		if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
			{

				$response_data['email'] = 'E-Mail is not valid';	
			}


	   /*------------------------ Check comments ---------------------------------*/   	   	   	   
		if(empty($comments))
		{
	
			$response_data['url'] = 'Required field.';
			//echo $response_data['name'];
		}
			   


	/**************************************************************************************/ 
	//////////////////// EOF Input Filtering  ////////////////////////////////////////////
	/**************************************************************************************/ 
$count = count($response_data);
//echo $count;
//print_r($response_data);		
if(	$count > 0)
	{	
	
		foreach	($response_data as $key => $value)
		{
			echo "$key: $value";
			echo "$value";
			echo "<br />";
		}
		echo '<a href="#" onclick="javascript:window.history.back()">Back</a>';
		exit;
	}

else
{	
	/*------------------------ send email ---------------------------------*/  
	//$name = $fname.' '.$lname;
	//echo $phone;
	//echo $problem;
	//exit;
	
	// setup variables   
	$HostAddress = gethostbyaddr($_SERVER['REMOTE_ADDR']);
	$ip = $_SERVER['REMOTE_ADDR'];   
$sMailBody =<<<"EOT"
	
	<div style="max-width:600px; background:#ffffff; font-family: calibri;">
	
		<div style="max-width:600px;clear:both;display:table;">
		<a href="https://www.hdtvsupply.com/"><img src ="{$myconst['DOMAIN']}/img/banner-email-logo.jpg" alt="HDTV Supply - A Top AV Store" title=" HDTV Supply - A Top AV Store" style="float:left; max-width:154;height:auto;" /></a>
		<a href="tel:8339653722"><img src ="{$myconst['DOMAIN']}/img/banner-email-phone.jpg" alt="Live Tech Support: 833-WOLFPACK (833-965-3722)" title="Live Tech Support:833-WOLFPACK (833-965-3722)" style="float: left; max-width:446;height:auto;" /></a>
		</div>	
	
		<div style="max-width:580px; padding:10px; padding-top:0px;">		
			
			
			
            {$message}
			<p>Subimiited via IP Address : {$ip}</p>
			<p>We will reply back to you shortly.</p>
			
			
			
			<p>Thanks and Regards,<br />
<strong>HDTVSupply Team</strong></p>

<p style="font-size:13px;">
<strong>HDTV Supply, Inc.</strong>  <br />
	E-mail: <a href="mailto:rachel@hdtvsupply.com">rachel@hdtvsupply.com</a><br />
	Live Tech Support: <a href="tel:8339653722">833-WOLFPACK (833-965-3722)</a>  <br />	
	Web: <a href="https://www.hdtvsupply.com">https://www.hdtvsupply.com</a> <br />	
</p>
			
 
 		</div>




</div>
EOT;
//echo $sMailBody;
//exit;
///////////////////////////////////////////////////////////////////////////////////////////////////////
$mail =  new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
//================================ BOF SMTP ===================================//
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BOF ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
		//$mail->SMTPDebug = 3;                               // Enable verbose debug output
		$mail->isSMTP();                                      // Set mailer to use SMTP		 
		$mail->Host = 'smtp.mail.yahoo.com';  // Specify main and backup SMTP servers
		$mail->SMTPAuth = true;                               // Enable SMTP authentication
		$mail->Username = 'rachel@hdtvsupply.com';                 // SMTP username
		$mail->Password = 'sichpincnldbcbnh';                           // SMTP password
		//$mail->Username = 'sjain@hdtvsupply.com';                 // SMTP username
		//$mail->Password = 'oiextpcphwcwfqjp';                           // SMTP password
		$mail->SMTPSecure = 'tls';    
		$mail->Port = 587;                         // Enable TLS encryption, `ssl` also accepted
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EOF ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
		  		
$mail->SetFrom('rachel@hdtvsupply.com', 'HDTVSupply');
$mail->AddAddress($email, $name);	
//$mail->AddAddress('support@hdtvsupply.com', 'HDTVSupply');
$mail->AddAddress('customerservice@hdtvsupply.com', 'Customer Service');
//$mail->AddAddress('tickets@hdtv-supply.p.tawk.email', 'Customer Service');
$mail->AddAddress('tickets@hdtv-supply-wojiuj.p.tawk.email', 'Customer Service');
$mail->AddAddress('rachel@hdtvsupply.com', 'Rachel');
//$mail->AddReplyTo($email, $name);	NOT WROKING ON YAHOO SERVER
//$mail->AddCc('support@hdtvsupply.com', 'HDTVSupply'); NOT WROKING ON YAHOO SERVER
//$mail->AddBcc('info@samyakonline.net', 'HDTVSupply');
$mail->isHTML(true);      
$mail->Subject = 'Build Your Own / HDTVSupply.com';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($sMailBody);
$mail->Send();	
//echo $sMailBody;	
//================================================
//header("location:https://www.hdtvsupply.com/thanks.html");	
//echo '<h2 style="color:#C96333; font-size:18px;">Thanks for submission.</h2>';
//echo '<p style="color:#000000; font-size:14px;">We will respond very soon.</p>';
echo "<script>window.top.location.href = \"https://hdtvsupply.com/thankyou.html\";</script>";
exit;
} 
/**************************************************************************************/  
//////////////////// EOF SEND EMAIL  /////////////////////////////////////////////////
/**************************************************************************************/  		 
//////////////////////////////////// EOF Form Processing /////////////////////////////////////////////////
?>