<?php
include_once ("include/main.inc.php");
$referer = $_SERVER['HTTP_REFERER'];
$ip = $_SERVER['REMOTE_ADDR'];
$mobileNumber = "";

if (isset($_POST['submit_modal'])) {
    $name = !empty($_POST['qname']) ? $_POST['qname'] : '';
    $email = !empty($_POST['qemail']) ? $_POST['qemail'] : '';
    $mobile = !empty($_POST['qmobile']) ? $_POST['qmobile'] : '';
    $location = !empty($_POST['qlocation']) ? $_POST['qlocation'] : '';
    $message = !empty($_POST['qmessage']) ? $_POST['qmessage'] : '';
    if (strpos($message, '#') !== false || strpos($message, '%') !== false || strpos($message, '@') !== false || strpos($message, '&') !== false || strpos($message, '~') !== false || strpos($message, 'https') !== false || strpos($message, 'http') !== false || strpos($message, 'www') !== false || strpos($message, '?') !== false) {
        $errMsg = 'URL(s) and special characters are not allowed!';
    } else {
        if ($name != '' && $email != '' && $mobile != '' && $location != '' && $message != '' && $ip != '') {

            //reCAPTCHA validation
            if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
                $secret = '6LdFkMIiAAAAANA4qioXICaAUQoouU_Mx6hvDgDQ';
                //get verify response data
                $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) {
                    $errMsg = 'Please click on the reCAPTCHA box.';
                }
                if ($response->success) {
                    $sql_ens = "insert into tbl_enquiry set en_name='$name', en_email='$email', en_mobile='$mobile', en_country='$location', en_comment='$message', en_url='$referer', en_date=now()";
                    $qry = mysqli_query($GLOBALS['dbcon'], $sql_ens);
                    $subject = utf8_encode('Webpulseindia.com ' . ' - ' . $admin_res['subject']);
                    $htmlContent = "
Customer Details:
Url: " . $referer . "
Name: " . $name . "
Email: " . $email . "
Mobile: " . $mobile . "
Location:" . $location . "
Message: " . $message . "";
                    $to = $admin_res['email'];
                    $to2 = $admin_res['cc'];
                    $to3 = 'webpulseindia@gmail.com';


                    $headers = 'From: ' . 'Webpulseindia.com' . ' enquiry@webpulseindia.com' . "\r\n" .
                        'Reply-To:  ' . $name . '<' . $email . '>' . "\r\n" .
                        'X-Mailer: PHP/' . phpversion();
                    @mail($to, $subject, $htmlContent, $headers);
                    @mail($to2, $subject, $htmlContent, $headers);
                    @mail($to3, $subject, $htmlContent, $headers);
                    $succMsg = 'Thank you for your enquiry. Our team will contact you soon.';
                    $name = '';
                    $email = '';
                    $mobile = '';
                    $location = '';
                    $message = '';

                }
            } else {
                $errMsg = 'Robot verification failed, please try again.';
            }
        } else {
            $errMsg = 'All the fields are Mandatory!';
        }
    }
}

?>
<!-- Product Popup -->
<div class="modal fade" id="productID" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <div class="modal-title">Send Enquiry1</div>
                <button type="button" class="close" data-bs-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-lg-7 col-md-7">
                        <div class="product_popup">
                            <div class="product_popup_image">
                                <img class="img-responsive catImg"
                                    src="<?= SITE_URL_PATH; ?>/designer/images/products/product/product-img.jpg"
                                    alt="product-img" title="product-img">
                            </div>
                            <div class="catName"></div>
                        </div>
                    </div>
                    <div class="col-lg-5 col-md-5">

                        <form name="contact-form" method="post" id="product_pop_form">
                            <?php if (!empty($errMsg)): ?>
                                <div class="alert alert-danger"><?php echo $errMsg; ?></div><?php endif; ?>
                            <?php if (!empty($succMsg)): ?>
                                <div class="alert alert-success"><?php echo $succMsg; ?></div><?php endif; ?>
                            <div class="form-group">
                                <input tabindex="001" class="form-control" type="text" name="qname"
                                    placeholder="Your Name*" value="" pattern="[a-z A-Z]+" required>
                            </div>

                            <div class="form-group">
                                <input tabindex="002" class="form-control" type="email" name="qemail"
                                    placeholder="Your Email ID*" value="" required>
                            </div>

                            <!-- <div class="form-group">
                                    <input tabindex="003" class="form-control" type="tel" name="qmobile"
                                        placeholder="Your Phone No*" maxlength="20" value="" required>
                                </div> -->

                            <div class="form-group">
                                <input type="hidden" class="countryName countryName" name="countryName" value="">
                                <input class="form-control telePhone mobile" value="" type="tel" name="mobile"
                                    placeholder="Mobile Number*"
                                    onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')"
                                    minlength="10" maxlength="12" required="" autocomplete="off"
                                    style="padding-left: 84px;">
                            </div>

                            <div class="form-group">
                                <input tabindex="004" class="form-control" type="text" name="qlocation"
                                    placeholder="Your Location*" onfocus="geolocate()" value="" pattern="[a-z A-Z]+"
                                    autocomplete="off" required>
                            </div>

                            <div class="form-group">
                                <textarea tabindex="005" class="form-control" name="qmessage"
                                    placeholder="Your Message*" required></textarea>
                            </div>

                            <div class="form-group" style="margin-bottom: 0;">
                                <div tabindex="006" class="g-recaptcha"
                                    data-sitekey="6LdFkMIiAAAAAOxDw8VJ4s7cwdN0AqORyNGfjKEz"></div>
                            </div>

                            <div class="form-group">
                                <button tabindex="007" class="modal_btn" type="submit" name="submit_modal"
                                    value="Submit" data-submit="...Sending">Submit</button>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>