// MY HEADS (Marty 2026-11-15): programs a member adds themselves (name, link, short description, category), shown to their // downline next to the wild heads. Free members: 1. Pro members: 4. Pure functions, no I/O, unit-tested in qa/unit.test.cjs. // // The link is either // - a template with {id} where the referral ID goes (https://mailer.example/?ref={id}) plus the owner's own ID: their downline // can then save their own ID for it, and a visitor sees the nearest saved ID up the line, ending at the owner's; or // - a full referral URL: everyone below sees exactly that link. // Checks before a head goes live (the old site auto-approved; we keep auto-approve but hold anything that looks wrong for the // admin queue): http(s) only, a real public hostname, no IP address, no shorteners, no network sites (they are heads already), // no program that is already a wild head (use the builder entry), no obviously spammy words, no links in the description. 'use strict'; const net = require('net'); const ID_RE = /^[A-Za-z0-9_.@+-]{1,64}$/; const PH = /\{(id|affil[a-z]*id)\}/gi; const PH1 = /\{(id|affil[a-z]*id)\}/i; const BAD_HOST = /(^|\.)(localhost|local|internal|lan|home|corp|onion|test|invalid|example)$/i; const SHORTENERS = /(^|\.)(bit\.ly|tinyurl\.com|t\.co|goo\.gl|is\.gd|cutt\.ly|ow\.ly|rebrand\.ly|shorturl\.at|rb\.gy|s\.id|t\.ly|v\.gd|bl\.ink|tiny\.cc|mylnks\.xyz|linktr\.ee|shorte\.st|adf\.ly|bitly\.com)$/i; // network sites are core heads, never "my heads" (and RM Circle stays a wild head only) const NETWORK = ['instantadpay.com', 'linkspin.app', 'downlinebanners.com', 'viralbannerblitz.com', 'viraladblitz.com', 'viraladsource.com', 'stackpol.com', 'earnerscorner.com', 'downlinehydra.com', 'yourprogrambuilder.com', '5xyourteam.com', 'downlinescaler.com', 'cryptoteambuild.com', 'rmcircle.team', 'saasy.top', 'banner-line.com', 'polhunter.com']; const SPAM = /\b(casino|betting|porn|xxx|viagra|cialis|escort|payday loan|loan offer|forex signal|binary option|guaranteed (profit|income|returns?)|double your|get rich|make \$?\d+|\d+% (daily|per day|a day)|hyip|ponzi|crack(ed)? software|free followers)\b/i; const hostOf = u => { try { return new URL(u).hostname.toLowerCase().replace(/^www\./, '').replace(/\.$/, ''); } catch (e) { return ''; } }; const within = (h, list) => list.some(d => h === d || h.endsWith('.' + d)); const DASHES = new RegExp('[' + String.fromCharCode(0x2013, 0x2014) + ']', 'g'); const clean = (s, max) => String(s == null ? '' : s).replace(/[\u0000-\u001f\u007f<>]/g, ' ').replace(DASHES, '-').replace(/\s+/g, ' ').trim().slice(0, max); // -> { error } or { ok, url (the template or full link, {affiliateid} normalized to {id}), template: bool, host } function checkLink(raw, ctx) { const s = String(raw || '').trim(); if (!s) return { error: 'Paste your referral link.' }; if (s.length > 500) return { error: 'That link is too long (500 characters max).' }; if (/[\s<>"'`\\]/.test(s)) return { error: 'That link has spaces or characters a web address cannot have.' }; const tpl = PH1.test(s); const probe = s.replace(PH, 'x'); let u; try { u = new URL(probe); } catch (e) { return { error: 'That is not a valid web address. Start it with https://' }; } if (!/^https?:$/.test(u.protocol)) return { error: 'The link must start with https:// (or http://).' }; if (u.username || u.password) return { error: 'The link cannot contain a username or password.' }; const h = u.hostname.toLowerCase().replace(/\.$/, ''); if (net.isIP(h.replace(/^\[|\]$/g, ''))) return { error: 'Use the program\'s domain name, not an IP address.' }; if (!h.includes('.') || BAD_HOST.test(h) || !/^[a-z0-9.-]+$/.test(h)) return { error: 'The link must point to a public website.' }; if (/\{(id|affil[a-z]*id)\}/i.test(s.split('?')[0].split('/').slice(0, 3).join('/'))) return { error: 'Put {id} in the path or after ?, not in the domain.' }; if (SHORTENERS.test(h)) return { error: 'Use the program\'s real address, not a link shortener.' }; const bare = h.replace(/^www\./, ''); if (within(bare, NETWORK.concat((ctx && ctx.networkHosts) || []))) return { error: 'That is a network site. It is already a head of the hydra for everyone, with your sponsor line.' }; if (ctx && ctx.wildHosts && within(bare, ctx.wildHosts)) return { error: 'That program is already a wild head in the builder. Save your ID for it in My wild heads instead.', wild: true }; return { ok: true, url: s.replace(PH, '{id}'), template: tpl, host: bare }; } // a whole my head from the editor. -> { error, field } or { ok, head: { name, url, template, myId, desc, cat, host }, flags: [] } // flags = reasons to hold it for the admin instead of going live (an approved head is never re-held for the same text) function validate(b, ctx) { const L = Object.assign({ name: 40, desc: 160 }, ctx && ctx.limits); const name = clean(b.name, L.name + 1); if (name.length < 2) return { error: 'Give the program a name (2 to ' + L.name + ' characters).', field: 'name' }; if (name.length > L.name) return { error: 'Keep the name to ' + L.name + ' characters.', field: 'name' }; const desc = clean(b.desc, L.desc + 1); if (desc.length > L.desc) return { error: 'Keep the description to ' + L.desc + ' characters.', field: 'desc' }; const lk = checkLink(b.url, ctx); if (lk.error) return Object.assign({ field: 'url' }, lk); let myId = ''; if (lk.template) { myId = String(b.myId == null ? '' : b.myId).trim(); if (!myId) return { error: 'Your link has {id} in it: type your own referral ID too, so your link works.', field: 'myId' }; if (!ID_RE.test(myId)) return { error: 'Your referral ID can only have letters, numbers and . _ - @ +', field: 'myId' }; } const cats = (ctx && ctx.cats) || []; const other = cats.find(c => /^other\b/i.test(String(c.short || c.name || ''))); // the seeded "Other programs", else 'other' const cat = cats.some(c => String(c.id) === String(b.cat)) ? String(b.cat) : other ? String(other.id) : 'other'; const flags = []; const text = name + ' ' + desc; if (SPAM.test(text) || SPAM.test(lk.url)) flags.push('spam words'); if (/https?:\/\/|www\.|\.[a-z]{2,6}\//i.test(desc)) flags.push('link in description'); if (name.replace(/[^A-Za-z]/g, '').length >= 6 && name === name.toUpperCase()) flags.push('all caps'); if (/(.)\1{5,}/.test(text) || /[!?$]{3,}/.test(text)) flags.push('shouting'); if (lk.url.startsWith('http://')) flags.push('not https'); return { ok: true, head: { name, url: lk.url, template: lk.template, myId, desc, cat, host: lk.host }, flags }; } // the link a head points at for someone: a template with the ID filled, or the full link function fill(head, id) { return head.template ? String(head.url).replace(PH, encodeURIComponent(String(id))) : String(head.url); } function homeUrl(head) { try { const u = new URL(String(head.url).replace(PH, 'x')); return u.origin + '/'; } catch (e) { return ''; } } const quota = (isPro, C) => isPro ? C.pro : C.free; // the heads that count as shown for an owner: live ones, oldest first, at most the quota (the rest wait: over quota, or held) function shown(list, q) { return list.filter(h => h.status === 'live').sort((a, b) => (a.createdAt || 0) - (b.createdAt || 0)).slice(0, Math.max(0, q)); } const used = list => list.filter(h => h.status !== 'removed').length; // how deep a viewer sits below an owner in a chain entry: a signed-in member's chain starts at their sponsor (depth 1 = sponsor); // a page or ?ref= visitor's chain starts at the page owner (depth 0) and the visitor would join one level below them const levelsBelow = (entry, by) => by === 'sponsor' ? entry.depth : entry.depth + 1; const visibleAt = (entry, by, depth) => levelsBelow(entry, by) <= depth; // a template head's link for a viewer: the nearest saved ID among the people between the viewer and the owner, else the owner's // chainBelow: chain entries nearer than the owner, nearest first; idOf(entry, headId) -> saved ID or '' function linkFor(head, ownerEntry, chainBelow, idOf) { if (!head.template) return { url: head.url, by: ownerEntry.username || null, depth: ownerEntry.depth, own: true }; for (const p of chainBelow || []) { const id = idOf(p, head.id); if (id) return { url: fill(head, id), by: p.username || null, depth: p.depth, own: false }; } return { url: fill(head, head.myId), by: ownerEntry.username || null, depth: ownerEntry.depth, own: true }; } module.exports = { checkLink, validate, fill, homeUrl, quota, shown, used, levelsBelow, visibleAt, linkFor, hostOf, NETWORK, SPAM, SHORTENERS, ID_RE };