<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require '../vendor/autoload.php';
session_start();

function url() {
    return sprintf(
        "%s://%s",
        isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
        $_SERVER['SERVER_NAME']
    );
}

$ipaddress = $_SERVER['REMOTE_ADDR'];
$referrer_url = $_SERVER['HTTP_REFERER'] ?? 'No Referrer';

$Name = htmlspecialchars(strip_tags($_POST['Name'] ?? ''));
$Email = filter_var($_POST['Email'] ?? '', FILTER_SANITIZE_EMAIL);
$Number = htmlspecialchars(strip_tags($_POST['Number'] ?? ''));
$Message = htmlspecialchars(strip_tags($_POST['Message'] ?? ''));
$FormName = htmlspecialchars(strip_tags($_POST['FormName'] ?? ''));
$NDA = isset($_POST['NDA']) ? "Checked (Required NDA)" : "Unchecked (Not Required)";
$cip = htmlspecialchars(strip_tags($_POST['cip'] ?? ''));
$locationURL = htmlspecialchars(strip_tags($_POST['locationURL'] ?? ''));

if (!empty($_POST['honeypot'])) {
    die('Spam detected.');
}

if (!isset($_SESSION['last_submit'])) {
    $_SESSION['last_submit'] = time();
} else {
    $time_diff = time() - $_SESSION['last_submit'];
    if ($time_diff < 10) {
        die('Too many submissions. Please try again later.');
    }
    $_SESSION['last_submit'] = time();
}

$allowedHost = 'inkcrestpublishingllc.com';
$referer = $_SERVER['HTTP_REFERER'] ?? '';
$parsedRefererHost = strtolower(parse_url($referer, PHP_URL_HOST));

if ($parsedRefererHost !== $allowedHost && $parsedRefererHost !== 'www.' . $allowedHost) {
    die('Unauthorized request.');
}

$recipient = "info@inkcrestpublishingllc.com";
$subject = "Ink Crest Publishing LLC Form Query";
$location = "https://inkcrestpublishingllc.com/thank-you";

if (isset($_REQUEST['hiddencapcha']) && $_REQUEST['hiddencapcha'] == "") {
    if (!empty($Name) && !empty($Email) && !empty($Number)) {

        if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
            die("Invalid email address.");
        }

        $body = "
            <p><strong>Name:</strong> {$Name}</p>
            <p><strong>Email:</strong> {$Email}</p>
            <p><strong>Number:</strong> {$Number}</p>
            <p><strong>IP Address:</strong> {$cip}</p>
            <p><strong>Message:</strong> {$Message}</p>
            <p><strong>Page URL:</strong> {$locationURL}</p>
            <p><strong>NDA Consent:</strong> {$NDA}</p>
        ";

        $mail = new PHPMailer(true);
        $mail->isSMTP();
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );

        try {
            $mail->Host       = 'mail.inkcrestpublishingllc.com';
            $mail->SMTPAuth   = true;
            $mail->Username   = 'info@inkcrestpublishingllc.com';
            $mail->Password   = 'pPvr0Z7L25)g4qaJ';
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
            $mail->Port       = 465;

            $mail->setFrom('info@inkcrestpublishingllc.com', $Name);
            $mail->addAddress('info@inkcrestpublishingllc.com');
            $mail->isHTML(true);
            $mail->Subject = $subject;
            $mail->Body    = $body;

            $mail->send();

        } catch (Exception $e) {
            die("Mail could not be sent. Error: " . $mail->ErrorInfo);
        }

        header("Location: $location");
        exit();
    }
}
?>