Files
linkspin/formrestyle.js
martbost 0a30b1da60 Link-domain pool by channel + safety checks, hosted page images + provider allowlist, Reports pages and zones sections
Item 1 (domains.js): every short link domain in linkDomains gets a channel (email / messaging / social / reserve)
via the new linkDomainChannels site setting ("host:channel,..."; default order email, messaging, social, email,
reserve). Checks every 6 h + Admin > Link domains "Check now": Google Safe Browsing v4 when GSB_API_KEY is set
(unknown otherwise), URIBL + SURBL DNS lists (answer = listed, NXDOMAIN = clean, 127.0.0.1 or errors = unknown),
and a TelegramBot-UA reachability probe. Flagged or listed = BURNED: out of domains.live() (no new links or
pages on it, existing /r/ codes still resolve), admin told once via telegramAdminChatId or ADMIN_EMAIL.
State in DATA_DIR/domains.json. New: GET /api/my/link-hosts, GET+POST /api/admin/domains, admin pane.
/api/my/pages now hands out hosts from domains.live().

Item 2 (pages.js, formrestyle.js): POST /api/my/pages/upload (raw image body, multipart, or JSON base64) for
png/jpg/webp up to 2 MB, 50 per member, magic bytes checked, sharp re-encode when installed, stored under
DATA_DIR/uploads/pages/<owner-hash>/<id>.<ext>, served at /u/pages/... immutable for a year; delete via
POST /api/my/pages/images/<id> {remove}. Pages editor: Upload image button beside the image field + a strip of
the member's images. Provider allowlist (17 autoresponders, Sendy by /subscribe path) exposed as `providers`;
the editor says "Recognised: AWeber" or "Unknown provider: plain post to <host>" at paste time; pub() carries
formProvider. Unknown hosts still allowed when parse-or-refuse passes.

Item 3 (zonereport.js, pages.summary): /api/my/reports returns {spins, pages, zones} (spins still spread at the
top level) for the chosen days; Pages and Zones sections on /reports with 30-day bars, totals, per-item tables,
phone width checked. Zone numbers are a read-only view over zones.json (zones.js untouched).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-10 11:28:47 -05:00

105 lines
8.0 KiB
JavaScript

// Parse-or-refuse for pasted autoresponder forms (ported from Branded Voice lib/formrestyle.ts, 2026-09-29).
// Keeps the MECHANICS (action, method, hidden fields, visible text/email fields), replaces the SKIN, escapes
// everything. Anything that cannot be rebuilt faithfully is REFUSED with a reason; member markup is never
// injected into a page as-is.
'use strict';
const EMBED_MAX_CHARS = 20000;
const escAttr = s => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
function attr(tag, name) {
const m = tag.match(new RegExp('\\b' + name + '\\s*=\\s*"([^"]*)"', 'i')) || tag.match(new RegExp('\\b' + name + "\\s*=\\s*'([^']*)'", 'i')) || tag.match(new RegExp('\\b' + name + '\\s*=\\s*([^\\s"\'>]+)', 'i'));
return m ? m[1] : null;
}
const hasFlag = (tag, name) => new RegExp('\\b' + name + '\\b', 'i').test(tag);
function derivePlaceholder(type, name) { const n = name.toLowerCase(); if (type === 'email' || n.includes('email')) return 'Your best email address'; if (n.includes('first')) return 'Your first name'; if (n.includes('last')) return 'Your last name'; if (n.includes('name')) return 'Your name'; return 'Your answer'; }
function problem(embed) {
const src = String(embed || '').trim();
if (!src) return null;
if (src.length > EMBED_MAX_CHARS) return 'Form code is too long. Paste only the <form> block your provider gives you.';
if (/<script|<iframe|<embed|<object/i.test(src)) return 'Form code contains a script, iframe or embed tag. Paste your provider\'s plain HTML form (the version without JavaScript).';
if (/recaptcha|g-recaptcha|h-captcha|hcaptcha|cf-turnstile/i.test(src)) return 'Form code includes a captcha, which cannot run on a published page. Turn the captcha off in your provider\'s form settings.';
const forms = src.match(/<form\b[\s\S]*?<\/form>/gi);
if (!forms || !forms.length) return 'No <form> tag was found in the form code.';
if (forms.length > 1) return 'Form code contains more than one form. Paste just the opt-in form.';
const formTag = forms[0].match(/<form\b[^>]*>/i)[0];
const action = attr(formTag, 'action');
if (!action || !/^https?:\/\//i.test(action)) return 'The form has no https:// action URL to submit to.';
if (/<select\b|<textarea\b/i.test(forms[0])) return 'The form uses a drop-down or text area, which cannot be restyled. Use a form with text and email fields only.';
const inputs = forms[0].match(/<input\b[^>]*>/gi) || [];
let visible = 0;
for (const tag of inputs) {
const type = (attr(tag, 'type') || 'text').toLowerCase();
if (type === 'hidden' || type === 'submit' || type === 'button' || type === 'image') continue;
if (type === 'text' || type === 'email') { if (!attr(tag, 'name')) return 'A visible field in the form has no name, so submissions would not reach your list.'; visible++; continue; }
return 'The form uses a "' + type + '" field, which cannot be restyled. Use a form with text and email fields only.';
}
if (!visible) return 'The form has no email or text field for the visitor to fill in.';
if (visible > 4) return 'The form has more than four visible fields. Opt-in forms with up to four fields are supported.';
return null;
}
// returns { action, method, charset, target, hidden:[{name,value}], fields:[{type,name,placeholder,required,value}], buttonText } or null
function parse(embed) {
if (problem(embed)) return null;
const src = String(embed).trim();
const formHtml = src.match(/<form\b[\s\S]*?<\/form>/gi)[0];
const formTag = formHtml.match(/<form\b[^>]*>/i)[0];
const action = attr(formTag, 'action');
const method = (attr(formTag, 'method') || 'post').toLowerCase() === 'get' ? 'get' : 'post';
const charset = attr(formTag, 'accept-charset'); const target = attr(formTag, 'target');
const hidden = [], fields = []; let buttonText = null;
for (const tag of formHtml.match(/<input\b[^>]*>/gi) || []) {
const type = (attr(tag, 'type') || 'text').toLowerCase(); const name = attr(tag, 'name') || '';
if (type === 'hidden') { if (name) hidden.push({ name, value: attr(tag, 'value') || '' }); continue; }
if (type === 'submit' || type === 'button' || type === 'image') { const v = attr(tag, 'value'); if (v && type === 'submit') buttonText = v; continue; }
fields.push({ type, name, placeholder: attr(tag, 'placeholder') || derivePlaceholder(type, name), required: hasFlag(tag, 'required'), value: attr(tag, 'value') || '' });
}
const btn = formHtml.match(/<button\b[^>]*>([\s\S]*?)<\/button>/i); if (btn && !buttonText) buttonText = btn[1].replace(/<[^>]+>/g, '').trim() || null;
return { action, method, charset, target, hidden, fields, buttonText };
}
function html(parsed, defaultButton) {
const open = '<form class="lsform" action="' + escAttr(parsed.action) + '" method="' + parsed.method + '"' + (parsed.charset ? ' accept-charset="' + escAttr(parsed.charset) + '"' : '') + (parsed.target ? ' target="' + escAttr(parsed.target) + '"' : '') + '>';
const hidden = parsed.hidden.map(h => '<input type="hidden" name="' + escAttr(h.name) + '" value="' + escAttr(h.value) + '">').join('\n');
const inputs = parsed.fields.map(f => '<input class="lsin" type="' + f.type + '" name="' + escAttr(f.name) + '" placeholder="' + escAttr(f.placeholder) + '"' + (f.value ? ' value="' + escAttr(f.value) + '"' : '') + (f.required || f.type === 'email' ? ' required' : '') + '>').join('\n');
return [open, hidden, inputs, '<button type="submit" class="cta">' + escAttr((parsed.buttonText || '').trim() || defaultButton) + '</button>', '</form>'].join('\n');
}
// -- known autoresponder providers (2026-10-10): the allowlist is about CLARITY, not blocking. A recognised action host
// gets "Recognised: AWeber"; anything else is kept as a plain post to <host> with a note that we cannot vouch for it.
// Hosts match the action host or any subdomain of it; `path` (a regex source) narrows by action path, e.g. Sendy on any host.
const PROVIDERS = [
{ name: 'AWeber', hosts: ['aweber.com'] },
{ name: 'GetResponse', hosts: ['getresponse.com', 'getresponse.net', 'gr8.com'] },
{ name: 'Mailchimp', hosts: ['list-manage.com', 'mailchimp.com'] },
{ name: 'Kit (ConvertKit)', hosts: ['convertkit.com', 'kit.com', 'ck.page'] },
{ name: 'ActiveCampaign', hosts: ['activehosted.com', 'activecampaign.com'] },
{ name: 'MailerLite', hosts: ['mailerlite.com', 'mailerlite.io'] },
{ name: 'SendFox', hosts: ['sendfox.com'] },
{ name: 'systeme.io', hosts: ['systeme.io'] },
{ name: 'Drip', hosts: ['getdrip.com'] },
{ name: 'Sendy', hosts: [], path: '(^|/)subscribe/?$' },
{ name: 'Brevo', hosts: ['sibforms.com', 'brevo.com', 'sendinblue.com'] },
{ name: 'Constant Contact', hosts: ['constantcontact.com', 'constantcontactpages.com'] },
{ name: 'Klaviyo', hosts: ['klaviyo.com'] },
{ name: 'Moosend', hosts: ['moosend.com'] },
{ name: 'Benchmark Email', hosts: ['benchmarkemail.com'] },
{ name: 'beehiiv', hosts: ['beehiiv.com'] },
{ name: 'Substack', hosts: ['substack.com'] }
];
function actionOf(embed) {
const forms = String(embed || '').match(/<form\b[\s\S]*?<\/form>/gi); if (!forms || !forms.length) return null;
const tag = forms[0].match(/<form\b[^>]*>/i); const a = tag ? attr(tag[0], 'action') : null;
if (!a || !/^https?:\/\//i.test(a)) return null;
try { return new URL(a); } catch (e) { return null; }
}
// { host, known, name, label, note } for a pasted form, or null when there is no usable action
function provider(embed) {
const u = actionOf(embed); if (!u) return null;
const host = u.hostname.toLowerCase().replace(/^www\./, '');
for (const pr of PROVIDERS) {
const hostHit = pr.hosts.some(h => host === h || host.endsWith('.' + h));
const pathHit = pr.path ? new RegExp(pr.path, 'i').test(u.pathname) : false;
if ((pr.hosts.length && hostHit) || (!pr.hosts.length && pathHit)) return { host, known: true, name: pr.name, label: 'Recognised: ' + pr.name, note: 'Submissions go to ' + host + '.' };
}
return { host, known: false, name: '', label: 'Unknown provider', note: 'The form is kept as a plain post to ' + host + '; we cannot vouch for it.' };
}
module.exports = { EMBED_MAX_CHARS, PROVIDERS, problem, parse, html, provider };