feat(site): email launch-list capture with a PHP/MySQL endpoint
A signup form in the closing section posts to subscribe.php (ships in the build): PDO/MySQL, honeypot, idempotent insert, optional mail() notify. DB creds live in a gitignored config kept above the web root; schema and setup in server/. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
1 parent
b978dacd4f
commit
9a34ab0699
6 files changed
+264
No files matched your search
@@ -15,6 +15,10 @@ node_modules/
|
||||
.env
|
||||
.env.production
|
||||
|
||||
# real DB credentials for the launch-list endpoint (never commit)
|
||||
subscribe.config.php
|
||||
**/subscribe.config.php
|
||||
|
||||
# macOS
|
||||
.DS_Store
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Green Tea launch-list capture.
|
||||
*
|
||||
* The static site POSTs here (same origin on the cPanel subdomain). A valid
|
||||
* email lands in MySQL; duplicates are a silent no-op. DB credentials live in
|
||||
* subscribe.config.php, kept OUTSIDE the web root — see server/ in the repo.
|
||||
*/
|
||||
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
|
||||
/** Reply as JSON to fetch(), redirect back for a no-JS form post. */
|
||||
function respond(bool $ok, int $code, string $message, bool $wantsJson): void
|
||||
{
|
||||
http_response_code($code);
|
||||
if ($wantsJson) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['ok' => $ok, 'message' => $message]);
|
||||
} else {
|
||||
header('Location: /?subscribed=' . ($ok ? '1' : '0') . '#subscribe');
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
$wantsJson = isset($_SERVER['HTTP_X_REQUESTED_WITH'])
|
||||
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'fetch';
|
||||
|
||||
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
|
||||
respond(false, 405, 'Method not allowed.', $wantsJson);
|
||||
}
|
||||
|
||||
// Honeypot: bots fill hidden fields, people don't. Accept silently, store nothing.
|
||||
if (trim((string) ($_POST['website'] ?? '')) !== '') {
|
||||
respond(true, 200, "You're on the list.", $wantsJson);
|
||||
}
|
||||
|
||||
$email = trim((string) ($_POST['email'] ?? ''));
|
||||
if ($email === '' || strlen($email) > 254 || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
respond(false, 422, 'Please enter a valid email address.', $wantsJson);
|
||||
}
|
||||
|
||||
// Config one level above the web root is preferred; same-dir is a fallback.
|
||||
$configFile = __DIR__ . '/../subscribe.config.php';
|
||||
if (!is_file($configFile)) {
|
||||
$configFile = __DIR__ . '/subscribe.config.php';
|
||||
}
|
||||
if (!is_file($configFile)) {
|
||||
error_log('subscribe: missing subscribe.config.php');
|
||||
respond(false, 500, 'Signups are not configured yet.', $wantsJson);
|
||||
}
|
||||
$config = require $configFile;
|
||||
|
||||
try {
|
||||
$pdo = new PDO($config['dsn'], $config['user'], $config['pass'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
// Unique index on email makes this idempotent — a repeat signup is a no-op.
|
||||
$stmt = $pdo->prepare(
|
||||
'INSERT INTO subscribers (email, created_at, ip) VALUES (:email, NOW(), :ip)
|
||||
ON DUPLICATE KEY UPDATE email = email'
|
||||
);
|
||||
$stmt->execute([
|
||||
':email' => $email,
|
||||
':ip' => $_SERVER['REMOTE_ADDR'] ?? null,
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
error_log('subscribe: ' . $e->getMessage());
|
||||
respond(false, 500, 'Could not save that right now — try again later.', $wantsJson);
|
||||
}
|
||||
|
||||
// Optional heads-up email. Never fatal: the DB row is the source of truth.
|
||||
if (!empty($config['notify'])) {
|
||||
@mail(
|
||||
(string) $config['notify'],
|
||||
'Green Tea: new subscriber',
|
||||
"New launch-list signup: {$email}",
|
||||
'From: Green Tea <' . $config['notify'] . '>'
|
||||
);
|
||||
}
|
||||
|
||||
respond(true, 200, "You're on the list — we'll ping you at 1.0.", $wantsJson);
|
||||
@@ -0,0 +1,24 @@
|
||||
# Launch-list backend (cPanel)
|
||||
|
||||
The email capture on the site posts to `subscribe.php`, which ships in the build
|
||||
(`public/subscribe.php` → `dist/subscribe.php`). To wire it up on cPanel:
|
||||
|
||||
1. **Create the DB** in cPanel → MySQL Databases. Note db name, user, password.
|
||||
2. **Create the table**: run `schema.sql` in phpMyAdmin against that DB.
|
||||
3. **Add credentials**: copy `subscribe.config.example.php` to `subscribe.config.php`,
|
||||
fill in the DB values, and upload it **one level above** the subdomain's docroot
|
||||
(so it is never web-servable). `subscribe.php` looks there first.
|
||||
4. **Deploy** the built `dist/` into the subdomain docroot. `subscribe.php` is already inside it.
|
||||
5. **Smoke test**:
|
||||
```
|
||||
curl -i -X POST -H 'X-Requested-With: fetch' \
|
||||
--data 'email=test@example.com' \
|
||||
https://green-tea.expressive-tea.io/subscribe.php
|
||||
```
|
||||
Expect `{"ok":true,...}` and a row in `subscribers`.
|
||||
|
||||
Notes:
|
||||
- Duplicate signups are a silent success (unique index on `email`).
|
||||
- A hidden honeypot field blocks basic bots.
|
||||
- No-JS visitors are redirected to `/?subscribed=1`; with JS they get inline feedback.
|
||||
- No rate limiting yet — add one at the cPanel/CDN layer if signups get abused.
|
||||
@@ -0,0 +1,7 @@
|
||||
-- Launch-list table for subscribe.php. Run once against the green-tea DB.
|
||||
CREATE TABLE IF NOT EXISTS subscribers (
|
||||
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
email VARCHAR(254) NOT NULL UNIQUE,
|
||||
created_at DATETIME NOT NULL,
|
||||
ip VARCHAR(45) DEFAULT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Copy this to `subscribe.config.php` and fill in real values.
|
||||
*
|
||||
* Place the real file ONE LEVEL ABOVE the web root if you can (so it is never
|
||||
* web-servable), e.g. /home/USER/subscribe.config.php when the docroot is
|
||||
* /home/USER/green-tea.expressive-tea.io/. subscribe.php looks there first,
|
||||
* then falls back to sitting next to it.
|
||||
*
|
||||
* Never commit the real subscribe.config.php — it holds the DB password.
|
||||
*/
|
||||
|
||||
return [
|
||||
'dsn' => 'mysql:host=localhost;dbname=YOUR_DB;charset=utf8mb4',
|
||||
'user' => 'YOUR_DB_USER',
|
||||
'pass' => 'YOUR_DB_PASSWORD',
|
||||
'notify' => 'support@expressive-tea.io', // set to '' to skip notification emails
|
||||
];
|
||||
@@ -10,6 +10,27 @@
|
||||
ergonomics. If you're tired of maintaining someone else's foundation, come try it.
|
||||
</p>
|
||||
|
||||
<form class="subscribe" action="/subscribe.php" method="post">
|
||||
<p class="subscribe__lead">Not ready yet? Get one email when 1.0 ships — nothing else.</p>
|
||||
<div class="subscribe__row">
|
||||
<label class="sr-only" for="sub-email">Email address</label>
|
||||
<input
|
||||
id="sub-email"
|
||||
class="subscribe__input"
|
||||
type="email"
|
||||
name="email"
|
||||
required
|
||||
maxlength="254"
|
||||
placeholder="you@company.com"
|
||||
autocomplete="email"
|
||||
/>
|
||||
<!-- honeypot: real people never fill this -->
|
||||
<input class="subscribe__hp" type="text" name="website" tabindex="-1" autocomplete="off" aria-hidden="true" />
|
||||
<button class="btn btn--primary" type="submit">Notify me at 1.0</button>
|
||||
</div>
|
||||
<p class="subscribe__msg" role="status" aria-live="polite"></p>
|
||||
</form>
|
||||
|
||||
<div class="btn-row honest__ctas">
|
||||
<a class="btn btn--primary" href="https://green-tea.expressive-tea.io/docs" target="_blank" rel="noopener noreferrer">
|
||||
Read the docs
|
||||
@@ -49,4 +70,110 @@
|
||||
font-size: 1.5rem;
|
||||
color: var(--matcha);
|
||||
}
|
||||
|
||||
.subscribe {
|
||||
max-width: 30rem;
|
||||
margin: 0 auto 2.5rem;
|
||||
}
|
||||
|
||||
.subscribe__lead {
|
||||
margin: 0 0 0.9rem;
|
||||
color: var(--dim);
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.subscribe__row {
|
||||
display: flex;
|
||||
gap: 0.6rem;
|
||||
}
|
||||
|
||||
.subscribe__input {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
padding: 0.7rem 0.9rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid color-mix(in srgb, var(--dim) 35%, transparent);
|
||||
background: var(--base);
|
||||
color: var(--ink);
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.subscribe__input:focus-visible {
|
||||
outline: 2px solid var(--matcha);
|
||||
outline-offset: 1px;
|
||||
}
|
||||
|
||||
/* honeypot — kept in the layout for bots, off-screen for humans */
|
||||
.subscribe__hp {
|
||||
position: absolute;
|
||||
left: -9999px;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.subscribe__msg {
|
||||
margin: 0.75rem 0 0;
|
||||
min-height: 1.2em;
|
||||
font-size: 0.9rem;
|
||||
color: var(--matcha);
|
||||
}
|
||||
|
||||
.subscribe__msg[data-error] {
|
||||
color: var(--violet);
|
||||
}
|
||||
|
||||
.sr-only {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.subscribe__row {
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
const form = document.querySelector('.subscribe');
|
||||
if (form instanceof HTMLFormElement) {
|
||||
const msg = form.querySelector('.subscribe__msg');
|
||||
const button = form.querySelector('button');
|
||||
form.addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
if (button) button.disabled = true;
|
||||
if (msg) {
|
||||
msg.textContent = '';
|
||||
delete msg.dataset.error;
|
||||
}
|
||||
try {
|
||||
const res = await fetch(form.action, {
|
||||
method: 'POST',
|
||||
headers: { 'X-Requested-With': 'fetch' },
|
||||
body: new FormData(form),
|
||||
});
|
||||
const data = await res.json().catch(() => ({ ok: res.ok, message: '' }));
|
||||
if (msg) {
|
||||
msg.textContent = data.message || (data.ok ? "You're on the list." : 'Something went wrong.');
|
||||
if (!data.ok) msg.dataset.error = 'true';
|
||||
}
|
||||
if (data.ok) form.reset();
|
||||
} catch {
|
||||
if (msg) {
|
||||
msg.textContent = 'Network error — try again.';
|
||||
msg.dataset.error = 'true';
|
||||
}
|
||||
} finally {
|
||||
if (button) button.disabled = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
Reference in new issue
Block a user