<?php
/**
 * contact.php — styled contact form for acquisitioninvest.com.
 * Posts to itself (same-origin), sends via the shared SMTP mailer, PRG redirect.
 */
declare(strict_types=1);

$SITE_NAME = "Acquisition Invest";
$DOMAIN    = "acquisitioninvest.com";
$TO        = "trenttompkins@gmail.com";          // real recipient
$PHONE     = "833-319-3160";
$DISP_EMAIL= "trenttompkins@gmail.com";

$errors = [];
$sent = isset($_GET['sent']) && $_GET['sent'] === '1';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = trim((string)($_POST['name']    ?? ''));
    $email   = trim((string)($_POST['email']   ?? ''));
    $message = trim((string)($_POST['message'] ?? ''));
    $hp      = trim((string)($_POST['company'] ?? '')); // honeypot

    // Honeypot: pretend success, send nothing.
    if ($hp !== '') { header('Location: contact.php?sent=1'); exit; }

    // Rate-limit by IP: max 5 / 10 min via tmp file.
    $ip = preg_replace('/[^0-9a-f:.]/i', '', (string)($_SERVER['REMOTE_ADDR'] ?? '0'));
    $rlFile = sys_get_temp_dir() . '/contact_rl_' . md5($ip . '|acquisitioninvest.com');
    $now = time(); $hits = [];
    if (is_file($rlFile)) {
        $hits = array_filter(array_map('intval', explode(',', (string)@file_get_contents($rlFile))),
                             fn($t) => $t > $now - 600);
    }
    if (count($hits) >= 5) {
        $errors[] = "Too many messages from your address. Please try again in a few minutes.";
    }

    if (!$errors) {
        if ($name === '' || $email === '' || $message === '') $errors[] = "Please fill out every field.";
        elseif (!filter_var($email, FILTER_VALIDATE_EMAIL))    $errors[] = "That email address doesn't look valid.";
        elseif (mb_strlen($message) > 5000)                    $errors[] = "Message is too long (5000 character max).";
    }

    if (!$errors) {
        $safeName = preg_replace('/[\r\n]+/', ' ', $name);
        $safeFrom = preg_replace('/[\r\n]+/', ' ', $email);
        $subject  = "New contact from acquisitioninvest.com";
        $body  = "Site: acquisitioninvest.com\n";
        $body .= "Name: {$safeName}\n";
        $body .= "Email: {$safeFrom}\n";
        $body .= "IP: {$ip}\n";
        $body .= "Time: " . date('r') . "\n";
        $body .= "UA: " . substr((string)($_SERVER['HTTP_USER_AGENT'] ?? '?'), 0, 250) . "\n";
        $body .= "\n----- Message -----\n" . $message . "\n";

        require '/home/_contact/gmail_mailer.php';
        $ok = gmailApiSend($TO, $subject, $body, 'admin@acquisitioninvest.com', 'AcquisitionInvest', $safeFrom); if ($ok) gmailSendConfirmation($safeFrom, $safeName, '833-319-3160', 'invest@acquisitioninvest.com', 'admin@acquisitioninvest.com', 'AcquisitionInvest'); $err = $ok ? '' : 'Gmail API send failed';

        if ($ok) {
            $hits[] = $now;
            @file_put_contents($rlFile, implode(',', $hits));
            header('Location: contact.php?sent=1');
            exit;
        }
        $errors[] = "Sorry, the message couldn't be sent right now. Please email {$DISP_EMAIL} directly.";
    }
}

function e(string $s): string { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); }
$selfMtime = @filemtime(__FILE__) ?: time();
?>
<!doctype html>
<html lang="en">
<head>
<base href="https://acquisitioninvest.com/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Contact — <?= e($SITE_NAME) ?></title>
<meta name="description" content="Contact <?= e($SITE_NAME) ?> — send us a message and we'll get back to you.">
<meta name="robots" content="index,follow">
<link rel="canonical" href="https://acquisitioninvest.com/contact">
<style>
:root{ --accent:#3f8f5e; --bg:#0e1411; --panel:#16201a; --text:#e9f1ec; --muted:#9fb6a8; --line:rgba(255,255,255,.10); }
*{box-sizing:border-box} html,body{margin:0;padding:0}
body{background:var(--bg);color:var(--text);
 font:16px/1.6 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
 -webkit-font-smoothing:antialiased;min-height:100vh;display:flex;flex-direction:column}
a{color:var(--accent);text-decoration:none} a:hover{text-decoration:underline}
.wrap{width:100%;max-width:680px;margin:0 auto;padding:0 22px}
header.nav{border-bottom:1px solid var(--line);padding:18px 0}
header.nav .wrap{display:flex;align-items:center;gap:16px}
.brand{font-weight:700;font-size:18px;color:var(--text)}
.brand b{color:var(--accent)}
header.nav .links{margin-left:auto;display:flex;gap:20px}
header.nav .links a{color:var(--muted);font-size:14px}
header.nav .links a:hover{color:var(--text);text-decoration:none}
main{flex:1;padding:48px 0 60px}
h1{font-size:30px;margin:0 0 8px}
.lead{color:var(--muted);margin:0 0 28px}
.card{background:var(--panel);border:1px solid var(--line);border-radius:14px;padding:26px}
label{display:block;font-size:13px;letter-spacing:.02em;color:var(--muted);margin:16px 0 6px;text-transform:uppercase}
input,textarea{width:100%;background:rgba(0,0,0,.25);border:1px solid var(--line);border-radius:9px;
 color:var(--text);padding:12px 13px;font:inherit;font-size:15px}
input:focus,textarea:focus{outline:none;border-color:var(--accent);box-shadow:0 0 0 3px color-mix(in srgb,var(--accent) 25%,transparent)}
textarea{min-height:150px;resize:vertical}
.hp{position:absolute;left:-9999px;top:-9999px;width:1px;height:1px;overflow:hidden}
button{margin-top:22px;background:var(--accent);color:#0b0b0f;border:0;border-radius:9px;
 padding:13px 22px;font:inherit;font-weight:700;font-size:15px;cursor:pointer}
button:hover{filter:brightness(1.08)}
.msg{padding:13px 15px;border-radius:9px;margin:0 0 20px;font-size:15px}
.ok{background:color-mix(in srgb,var(--accent) 18%,transparent);border:1px solid var(--accent)}
.err{background:rgba(224,70,70,.14);border:1px solid #e04646;color:#ffd9d9}
.contactinfo{margin-top:26px;color:var(--muted);font-size:15px;line-height:1.9}
.contactinfo b{color:var(--text)}
footer{border-top:1px solid var(--line);padding:22px 0;color:var(--muted);font-size:13px}
footer .wrap{display:flex;gap:14px;flex-wrap:wrap;align-items:center}
footer a{color:var(--muted)} footer .sep{opacity:.4}
</style>
</head>
<body>
<header class="nav">
  <div class="wrap">
    <a class="brand" href="https://acquisitioninvest.com/"><?= e($SITE_NAME) ?></a>
    <div class="links">
      <a href="https://acquisitioninvest.com/">Home</a>
      <a href="contact.php">Contact</a>
    </div>
  </div>
</header>

<main>
  <div class="wrap">
    <h1>Contact us</h1>
    <p class="lead">Tell us about your property or investment goals.</p>

    <div class="card">
      <?php if ($sent): ?>
        <div class="msg ok"><b>Thanks!</b> Your message has been sent. We'll get back to you soon.</div>
      <?php endif; ?>
      <?php foreach ($errors as $er): ?>
        <div class="msg err"><?= e($er) ?></div>
      <?php endforeach; ?>

      <form method="post" action="contact.php" autocomplete="on" novalidate>
        <label for="name">Your name</label>
        <input id="name" name="name" type="text" maxlength="120" required value="<?= e((string)($_POST['name'] ?? '')) ?>">

        <label for="email">Your email</label>
        <input id="email" name="email" type="email" maxlength="200" required value="<?= e((string)($_POST['email'] ?? '')) ?>">

        <label for="message">Message</label>
        <textarea id="message" name="message" maxlength="5000" required><?= e((string)($_POST['message'] ?? '')) ?></textarea>

        <div class="hp" aria-hidden="true">
          <label>Company (leave blank)</label>
          <input type="text" name="company" tabindex="-1" autocomplete="off">
        </div>

        <button type="submit">Send message</button>
      </form>

      <div class="contactinfo">
        Prefer another way? <br>
        Phone: <b><?= e($PHONE) ?></b><br>
        Email: <b><a href="mailto:<?= e($DISP_EMAIL) ?>"><?= e($DISP_EMAIL) ?></a></b>
      </div>
    </div>
  </div>
</main>

<footer>
  <div class="wrap">
    <span>&copy; <?= date('Y') ?> <?= e($SITE_NAME) ?></span>
    <span class="sep">|</span>
    <span><?= e($PHONE) ?></span>
    <span class="sep">|</span>
    <a href="mailto:<?= e($DISP_EMAIL) ?>"><?= e($DISP_EMAIL) ?></a>
    <span class="sep">|</span>
    <a href="contact.php">Contact</a>
  </div>
</footer>
</body>
</html>
