<?php
function _recaptcha_http_post($host, $path, $data, $port = 80) {
	$add_headers = array ("Host: $host");

	$curl = curl_init( 'http://' . $host . ':' . $port . $path );
	curl_setopt( $curl, CURLOPT_POST, true );
	curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
	curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 10 );
	curl_setopt( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
	curl_setopt( $curl, CURLOPT_USERAGENT, 'reCAPTCHA/PHP' );
	curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
	curl_setopt( $curl, CURLOPT_HEADER, true );
	curl_setopt( $curl, CURLOPT_HTTPHEADER, $add_headers );

	if (isset( $_ENV['http_proxy']) && !empty ($_ENV['http_proxy']) ) {
		curl_setopt( $curl, CURLOPT_HTTPPROXYTUNNEL, true );
		curl_setopt( $curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
		curl_setopt( $curl, CURLOPT_PROXY, $_ENV['http_proxy'] ); // CURLOPT_PROXYUSERPWD as username:password needed?
	}
	$response = curl_exec($curl);
	
	if ($response === false) die('Error connecting to ' . $host . '.');

	$response = explode("\r\n\r\n", $response, 2);
	return $response;
}

echo '
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script type="text/javascript" src="'.$thisPage.'js/messages.js"></script>
<link rel="stylesheet" type="text/css" href="'.$thisPage.'css/messages.css" />
<link rel="stylesheet" type="text/css" href="'.$thisPage.'css/captcha.css" />';

$postData = $statusMsg = ''; 
$status   = 'warning'; 

$recaptcha_site_key 	= '6LfLsq4UAAAAAJo0hnNyq5dzdpg-WAZXWpf8vQZ2';
$recaptcha_site_secret 	= '6LfLsq4UAAAAAGXSwWU2pAp2JqZ0Us5uMV1oz6oC'; //Your_reCAPTCHA_Secret_Key 

// If the form is submitted 
if(isset($_POST['submit'])){ 
    $postData = $_POST; 
    // Validate form fields 
    if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])){ 
        // Validate reCAPTCHA box 
        if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){ 
            // Google reCAPTCHA API secret key 
            // Verify the reCAPTCHA response 
			if (isset($_POST) && !empty($_POST)) {
				$captcha_response = htmlspecialchars($_POST['g-recaptcha-response']);
				$curl = curl_init();
				$captcha_verify_url = "https://www.google.com/recaptcha/api/siteverify";
				
				curl_setopt($curl, CURLOPT_URL,$captcha_verify_url);
				curl_setopt($curl, CURLOPT_POST, true);
				curl_setopt($curl, CURLOPT_POSTFIELDS, "secret=".$recaptcha_site_secret."&response=".$captcha_response);
				curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
				
				$captcha_output = curl_exec ($curl);
				curl_close ($curl);
			
				$decoded_captcha = json_decode($captcha_output);
				$captcha_status  = $decoded_captcha->success; // store validation result to a variable.
			
				if($captcha_status === TRUE){
					$name    = !empty($_POST['name'])?$_POST['name']:''; 
					$email   = !empty($_POST['email'])?$_POST['email']:''; 
					$message = !empty($_POST['message'])?$_POST['message']:''; 
					 
					// Send email notification to the site admin 
					$to = 'tradexpoindonesia@debindo.com,budiarto@debindo.com,rizal@debindo.com'; 
					//$to = 'chunkhu@gmail.com'; 
					$subject = 'New contact form have been submitted'; 
					$htmlContent = "
					<h1>Contact request details</h1> 
					<p><b>Name: </b>".$name."</p> 
					<p><b>Email: </b>".$email."</p> 
					<p><b>Message: </b>".$message."</p>"; 
					 
					// Always set content-type when sending HTML email 
					$headers  = "MIME-Version: 1.0" . "\r\n"; 
					$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
					// More headers chunkhu@gmail.com
					$headers .= 'From:Inquiry Form <info@tradexpoindonesia.com>' . "\r\n"; 
					$headers .= "BCc:chunkhu@gmail.com" . "\r\n";
					 
					// Send email 
					mail($to,$subject,$htmlContent,$headers); 
					 
					$status = 'success'; 
					$statusMsg = 'Your contact request has submitted successfully.'; 
					$postData = '';
					
				} else if($captcha_status === FALSE){
					$statusMsg = '
					Robot verification failed, please try again.'; 
				} 
			}
        } else{ 
            $statusMsg = 'Please check on the reCAPTCHA box.'; 
        } 
    }else{ 
        $statusMsg = 'Please fill all the mandatory fields.'; 
    } 
} 
?>
<!-- 
Status message 
TTrade Expo Indonesia 2019 finished on October 20, 2019 after five(5) days of great enthusiasm.
We would like to thanks all the exhibitors participated at the show, and hope to see you again next yeat.
<br />-->

<style>
.info-msg, .success-msg, .warning-msg, .error-msg {
  margin: 10px 0;
  padding: 10px;
  border-radius: 3px 3px 3px 3px;
}
.info-msg {
  color: #059;
  background-color: #BEF;
}
.success-msg {
  color: #270;
  background-color: #DFF2BF;
}
.warning-msg {
  color: #9F6000;
  background-color: #FEEFB3;
}
.error-msg {
  color: #D8000C;
  background-color: #FFBABA;
}
</style>

<p>
    For any inquiries, please complete the following form:
</p>
<br />
<div class="box" style="font-size:16px; line-height:20px; ">
    <form name="form" id="form" class="form" action="" method="post">
        <section class="layout-centered" data-modul="M00613">
        	<div class="rowInner spacerRow">
                <div class="col l-col12 m-col12 s-col12" style="padding:0; margin:0">
					<?php 
                    if(!empty($statusMsg)){ 
                        if ($status == "success") {
                            echo '
                            <div class="'.$status.'-msg">
                                <i class="fa fa-check"></i>
                                '.$statusMsg.'
                            </div>';
                        } else {
                            echo '
                            <div class="'.$status.'-msg">
                                <i class="fa fa-warning"></i>
                                '.$statusMsg.'
                            </div>';
                        }
                    }
                    ?>
                </div>
                <div class="col l-col12 m-col12 s-col12" style="border:1px solid #999999; padding:0; margin:0">
                    <div class="row" style="padding:18px 0px; background-color:#f1f1f1">
                        <div class="col l-col4 m-col12 s-col12">
                            <strong>Type Your Full Name</strong>
                            <div class="hide-for-medium-up" style="padding-bottom:8px"></div>
                        </div>
                        <div class="col l-col8 m-col12 s-col12">
                            <input size="38" type="text" name="name" value="<?php echo !empty($postData['name'])?$postData['name']:''; ?>" placeholder="Your name" required="" />
                        </div>
                    </div>
                    <div class="row" style="padding:18px 0px; background-color:#f8f8f8">
                        <div class="col l-col4 m-col12 s-col12">
                            <strong>Type Your Email Address</strong>
                            <div class="hide-for-medium-up" style="padding-bottom:8px"></div>
                        </div>
                        <div class="col l-col8 m-col12 s-col12">
                            <input size="45" type="email" name="email" value="<?php echo !empty($postData['email'])?$postData['email']:''; ?>" placeholder="Your email address" required="" />
                        </div>
                    </div>
                    <div class="row" style="padding:18px 0px; background-color:#f1f1f1"> 
                        <div class="col l-col4 m-col12 s-col12">
                            <strong>Type Your Message</strong>
                            <div class="hide-for-medium-up" style="padding-bottom:8px"></div>
                        </div>
                        <div class="col l-col8 m-col12 s-col12">
                            <textarea name="message" placeholder="Type message..." required="" ><?php echo !empty($postData['message'])?$postData['message']:''; ?></textarea>
                        </div>
                    </div>
                    <div class="row" style="padding:18px 0px; background-color:#f8f8f8">
                        <div align="center">
                            <div align="center" class="g-recaptcha" data-sitekey="<?php echo $recaptcha_site_key; ?>"></div>
                            <div align="center" style="padding:15px">
                                <input style="height:50px; width:120px" class="hvr-sweep-to-right" type="submit" name="submit" value="SUBMIT">
                            </div>
                        </div>		
                    </div>
                </div>
            </div>
        </section>
    </form>
</div>