<?php
//error_reporting(E_ALL);
//ini_set('display_errors', '1');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once __DIR__ . '/vendor/phpmailer/src/Exception.php';
require_once __DIR__ . '/vendor/phpmailer/src/PHPMailer.php';
require_once __DIR__ . '/vendor/phpmailer/src/SMTP.php';

include_once 'includes/dbsmain.inc.php';
include_once 'site-main-query.php';
require_once("../../whatever/config.php");

$currDate=date('Y-m-d');

function sanitize_data($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  $data = mysqli_real_escape_string($GLOBALS['dbcon'],$data);
  return $data;
}

if(isset($_REQUEST['submit_data'])){
    $name = $email = $mobile = $source = $msg = $subject = $tr = $country = '';
    $source=$_REQUEST['source'];
    if($_POST['website'] != ''){?>
     <script type="text/javascript">
       alert("Form Submitted successfully, we will contact you soon!");
       window.location.href='<?=$source?>';
     </script>
    <?php }  
 
    $country_id = $_REQUEST['vb_country'];
    $country_name = db_scalar("select name from countries where id='$country_id'");
    $phonecode = db_scalar("select phonecode from countries where id='$country_id'");

    $name=sanitize_data($_REQUEST['vb_name']);
    $email=sanitize_data($_REQUEST['vb_email']);
    $mobile="+".$phonecode."-".sanitize_data($_REQUEST['vb_phone']);
    $msg=sanitize_data($_REQUEST['vb_message']);
    $subject=sanitize_data($_REQUEST['subject']);
        
    if(preg_match("/(http:|https:|www\.|\.com|\.in|\.co\.in|\.store|\.live|\.online)/i", $msg)){?>
     <script type="text/javascript">
      alert("Links or URLs are not allow in description");
      window.location.href='<?=$source?>';
     </script>
    <?php exit;}

if(!empty($name) && !empty($email) && !empty($mobile) && !empty($msg) && !empty($country_id)){
    
$sql = "INSERT INTO tbl_enquiry SET 
           enquiry_name='$name',
           enquiry_email='$email',
           enquiry_mobile='$mobile',
           enquiry_source='$source',
           enquiry_detail='$msg',
           enquiry_subject='$subject',
           country_name='$country_name',
           enquiry_add_date='$currDate'";
        db_query($sql);    

$hostName = $_SERVER['HTTP_HOST'];            
$msgmail="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
<title>$compDATA[admin_company_name]</title>
 </head>
<body>      
   <table align='center' cellSpacing='0' cellPadding='0' width='87%' border='1' style='border:1px solid #e61938'>
  <tbody>
    <tr>
      <td vAlign='top' style='background-color:#e61938; padding:10px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:16px; color:#ffffff; text-align:center; font-weight:bold;' colspan='3' >Enquiry From $hostName</td>
    </tr>
     <tr>
      <td width='30%' vAlign='top' style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; color:#003366; background-color:#F9E2DD;padding:10px;' ><strong>Name </strong> </td>
      <td vAlign='top' width='70%' style='font-family:Verdana, Arial, Helvetica, sans-serif;padding:10px;'>$name</td>
    </tr>    
    <tr>
      <td vAlign='top'  style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; color:#003366; background-color:#F9E2DD;padding:10px;' ><strong>Email-Id </strong> </td>
      <td vAlign='top' style='font-family:Verdana, Arial, Helvetica, sans-serif;padding:10px;'>$email</td>
    </tr>    
   
      <tr>
      <td vAlign='top'  style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; color:#003366; background-color:#F9E2DD;padding:10px;' ><strong>Mobile No </strong> </td>
      <td vAlign='top' style='font-family:Verdana, Arial, Helvetica, sans-serif;padding:10px;'>$mobile</td>
    </tr> 
  
   <tr>
      <td vAlign='top'  style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; color:#003366; background-color:#F9E2DD;padding:10px;' ><strong>Source </strong> </td>
      <td vAlign='top' style='font-family:Verdana, Arial, Helvetica, sans-serif;padding:10px;'>$source</td>
    </tr>

    <tr>
      <td vAlign='top'  style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; color:#003366; background-color:#F9E2DD;padding:10px;' ><strong>Subject </strong> </td>
      <td vAlign='top' style='font-family:Verdana, Arial, Helvetica, sans-serif;padding:10px;'>$subject</td>
    </tr> 

    <tr>
      <td vAlign='top'  style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; color:#003366; background-color:#F9E2DD;padding:10px;' ><strong>Detail </strong> </td>
      <td vAlign='top' style='font-family:Verdana, Arial, Helvetica, sans-serif;padding:10px;'>$msg</td>
    </tr>   

     <tr>
      <td width='30%' vAlign='top' style='font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; color:#003366; background-color:#F9E2DD;padding:10px;' ><strong>Country </strong> </td>
      <td vAlign='top' width='70%' style='font-family:Verdana, Arial, Helvetica, sans-serif;padding:10px;'>$country_name</td>
    </tr> 

  </tbody>
</table>
</body>
</html>";    
    
$subject = "Enquiry from $hostName";   

$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = MAIL_HOST;
$mail->SMTPAuth = true;
$mail->Username = MAIL_ID;
$mail->Password = MAIL_PASSWORD;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;

$mail->From = $compDATA['admin_email'];
$mail->FromName = $compDATA['admin_name'];

$mail->addAddress($compDATA['admin_email'], $compDATA['admin_name']);

$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body = $msgmail;
$mail->AltBody = "";

try {
    $mail->send();?>
<script type="text/javascript">
 alert("Form Submitted successfully, we will contact you soon!");
 window.location.href='<?=$source?>';
</script>
<?php }catch(Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
}else{?>
<script type="text/javascript">
 alert("Please fill all the fields!");
 window.location.href='<?=$source?>';
</script>    
<?php }}else{?>
 <script>
  window.location.href="<?=$source?>";     
 </script>    
<?php }?>