<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Collect form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    
    // Get the sender's IP address
    $ip = $_SERVER['REMOTE_ADDR'];

    // Get the current date and time
    date_default_timezone_set('Australia/Perth'); // Set timezone to match Morangup's location
    $timestamp = date("Y-m-d H:i:s");
	
    // Get the referer
    if (isset($_SERVER['HTTP_REFERER'])) {
        $referer = $_SERVER['HTTP_REFERER'];
    } else {
        $referer = "Unknown";
    }

    // Allow URLs starting with the contact page URL
    $allowed_referer_base = "https://www.morangup.com.au/contact.html";
    if (strpos($referer, $allowed_referer_base) !== 0) {
        // Log the invalid attempt
        error_log("Invalid referer detected: " . $referer);

        // Show an error message
        echo '<div style="color: red; text-align: center; font-size: 16px;">Error: Invalid referer. Email not sent.</div>';
        exit(); // Stop the script
    }

    // Set up email content
    $to = "admin@morangup.com.au";
    $subject = "New message from The Morangup Website";
    
    // Construct HTML email body
    $body = '<html><body style="margin: 2em;">';
    $body .= '<div style="border: 1px solid #ccc; padding: 50px; max-width: 400px; margin: auto; text-align: center;">'; // Apply border, padding, and centering
    $body .= '<img src="https://morangup.com.au/dmimages/email.jpg" alt="Morangup Logo" style="display: block; margin: auto; max-width: 100%;">'; // Insert website logo
    $body .= '<h2 style="text-align: center;">New Message from Morangup.com.au</h2>';
    $body .= '<p><strong>Name:</strong> ' . htmlspecialchars($name) . '</p>';
    $body .= '<p><strong>Email:</strong> ' . htmlspecialchars($email) . '</p>';
    $body .= '<p><strong>Message:</strong></p>';
    $body .= '<p>' . nl2br(htmlspecialchars($message)) . '</p>';
    $body .= '<p><strong>Time Sent:</strong> ' . $timestamp . '</p>';
    $body .= '<p><strong>IP Address:</strong> ' . $ip . '</p>';
    $body .= '<p><strong>Submitted from:</strong> ' . htmlspecialchars($referer) . '</p>';
    $body .= '</div>';
    $body .= '</body></html>';

    // Set email headers
    $headers = "From: admin@morangup.com.au\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    // Send email
    if (mail($to, $subject, $body, $headers)) {
        echo '<div style="color: green; text-align: center; font-size: 16px;">SUCCESSFULLY SENT</div>';
        echo '<div style="text-align: center; font-size: 14px; margin-top: 10px;">You will be redirected to the homepage in 10 seconds...</div>';
        
        // Add JavaScript for redirect
        echo '<script>
            setTimeout(function() {
                window.location.href = "index.html";
            }, 10000); // Redirect after 10 seconds
        </script>';
    } else {
        echo '<div style="color: red; text-align: center; font-size: 16px;">Error sending email. Please try again later.</div>';
    }
} else {
    // Redirect back to the contact page if form is not submitted
    header("Location: contact.html");
    exit();
}
?>
