<?php
date_default_timezone_set('Asia/Kolkata');
include_once("include/main.inc.php"); 
date_default_timezone_set('Asia/Kolkata');
$date=date('Y-m-d H:i');

 @extract($_POST);
// print_r($_POST);
// exit;
$admin_sql=mysqli_query($dbconn,"select * from admin_details");
$admin_res=mysqli_fetch_array($admin_sql);
$referer = $_SERVER['HTTP_REFERER'];
$ip=$_SERVER['REMOTE_ADDR'];
if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$en_message)){  
if($user_name!='' &&  $email_address!='' && $phone_no!='' && $location!='' && $en_message!=''){

  if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
if(strlen($phone_no)==10){

    $secret = SECRET_KEY;
    $data = array( 'secret' => $secret, 'response' => $_POST['g-recaptcha-response'] ); $verify = curl_init(); curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify"); curl_setopt($verify, CURLOPT_POST, true); curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($verify, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($verify); //var_dump($response);
$response = json_decode($response);
if ($response->success) {
$sql_ens = "insert into tbl_enquiry set en_name='$user_name', en_email='$email_address', en_mobile='$phone_no', en_country='$location', en_comment='$en_message', en_url='$referer', en_date='$date', en_ip='$ip'";
   $name = $user_name;
   $email = $email_address;
$qry = mysqli_query($dbconn,$sql_ens);
        
$subject = utf8_encode($admin_res['subject']);
            $htmlContent =  '<table border="0" width="100%">
        <tbody>
  <tr>
    <td colspan="2">
      <strong>Dear Admin</strong></td>
  </tr>
  <tr>
    <td colspan="2">
      Enquiry Received with following info :</td>
  </tr>
  <tr>
    <td colspan="2">
      &nbsp;</td>
  </tr>
  <tr>
    <td width="26%">
      <strong>Post URL : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $referer . '</span></td>
  </tr>
   <tr>
    <td width="26%">
      <strong>Name : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $name . '</span></td>
  </tr>
  <tr>
    <td width="26%">
      <strong>Email Id : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $email . '</span></td>
  </tr>

  <tr>
    <td width="26%">
      <strong>Contact No : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $phone_no . '</span></td>
  </tr>
  <tr>
    <td>
      <strong>Location : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $location . '</span></td>
  </tr>
  <tr>
    <td>
      <strong>Message : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $en_message . '</span></td>
  </tr>
</tbody>
       
        </table>';
// $subject = utf8_encode('Webpulseindia.com '.' - '. $admin_res['subject']);
// $htmlContent = "
// Customer Details:

// Url: $referer

// Your Name: " . $user_name . "

// Mobile: " . $phone_no . "

// Email: " . $email_address . "

// Location: " . $location . "

// Message: " . $en_message . "
// ";
            
$to =  $admin_res['email'];
$to2 = $admin_res['cc'];
$to3 = $admin_res['bcc'];
// $to3 = 'vicky.wpsranchi@gmail.com';

		
// 	  $headers = 'From: '.'Webpulseindia.com'.' enquiry@webpulseindia.com' . "\r\n" .
//             'Reply-To:  '.$user_name.'<'.$email_address.'>' . "\r\n" .
//             'X-Mailer: PHP/' . phpversion();
//             @mail($to,$subject,$htmlContent,$headers);
//             @mail($to2,$subject,$htmlContent,$headers);
//             @mail($to3,$subject,$htmlContent,$headers);
if($to!='')
         {
         smtp_mailer($to, $email, $name, $subject, $htmlContent);
         }
         if($to2!='')
         {
         smtp_mailer($to2, $email, $name, $subject, $htmlContent);
         }
       
         smtp_mailer($to3, $email, $name, $subject, $htmlContent);
        

            $res = 'success';
        
            $user_name='';
            $phone_no='';
            $email_address='';
            $location='';
            $en_message='';
}
    else{
      $res = '<div class="alert alert-danger alert-dismissible show">Please verify you are human by filling out the captcha <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true"></span></button></div>';  
    }
    
}
    else{
        $res = '<div class="alert alert-danger alert-dismissible show">Please Enter 10 Digit Mobile Number. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true"></span></button></div>';
    }
    }
    else{
       $res = '<div class="alert alert-danger alert-dismissible show">Invalid Email Format <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true"></span></button></div>';  
    }
}
        else {
                $res = '<div class="alert alert-danger alert-dismissible show">Asterisk(*) Marked fields are Mandatory. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true"></span></button></div>';
        }
    }else{ 
         $res ='<div class="alert alert-danger alert-dismissible show">URL(s) and special characters are not allowed <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true"></span></button></div>';
         }
  
    echo $res;
    
?>