<?php
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
  header("Location: contact-us.php");
  exit;
}

/* Honeypot */
if (!empty($_POST['website'])) {
  exit;
}

/* reCAPTCHA validation */
$secretKey = "6LdKJ3EsAAAAAHd-KTa4wb3FfsXGoSpFL3WzfMD5";
$responseKey = $_POST['g-recaptcha-response'];
$userIP = $_SERVER['REMOTE_ADDR'];

$verifyURL = "https://www.google.com/recaptcha/api/siteverify";
$response = file_get_contents("$verifyURL?secret=$secretKey&response=$responseKey&remoteip=$userIP");
$responseData = json_decode($response);

if (!$responseData->success) {
  header("Location: contact-us.php?error=1");
  exit;
}

/* Sanitize input */
$name = htmlspecialchars($_POST['name']);
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);

if (!$name || !$email || !$message) {
  header("Location: contact-us.php?error=1");
  exit;
}

/* Admin Email */
$to = "info@wipetearsafrica.org";
$headers = "From: $name <$email>\r\nReply-To: $email\r\nContent-Type: text/plain; charset=UTF-8";

$adminMessage = "
New Contact Message

Name: $name
Email: $email
Subject: $subject

Message:
$message
";

mail($to, "New Contact Form Message", $adminMessage, $headers);

/* Auto-reply */
$autoReplyHeaders = "From: Wipe Tears Africa <info@wipetearsafrica.org>\r\nContent-Type: text/plain; charset=UTF-8";

$autoReplyMessage = "
Dear $name,

Thank you for contacting Wipe Tears Africa.

We have received your message and our team will get back to you shortly.

May God bless you for supporting hope and transformation.

Warm regards,
Wipe Tears Africa (WTA)
www.wipetearsafrica.org
";

mail($email, "Thank You for Contacting Wipe Tears Africa", $autoReplyMessage, $autoReplyHeaders);

header("Location: contact-us.php?success=1");
exit;
