='); } return $ready; } /** The columns for a contact query - NULL before the update. */ function contactUnsubscribeColumns($alias = '') { $prefix = $alias !== '' ? "$alias." : ''; return contactUnsubscribeReady() ? "{$prefix}contact_unsubscribed_at" : 'NULL AS contact_unsubscribed_at'; } /** When the contact left the email sent to many, from a row - null while they get it. */ function contactUnsubscribed(array $row) { return contactUnsubscribeReady() && !empty($row['contact_unsubscribed_at']) ? (string) $row['contact_unsubscribed_at'] : null; } /* * Of $emails, the addresses that left - any contact with one, archived or not: it is about the address, and a contact * added since, at another client say, or the same person added again, isn't a way back in. Lower case => when. */ function contactUnsubscribedAddresses(array $emails) { global $mysqli; $emails = array_values(array_unique(array_filter(array_map(function ($email) { return strtolower(trim((string) $email)); }, $emails)))); if (!$emails || !contactUnsubscribeReady()) { return []; } $out = []; $sql = mysqli_query($mysqli, "SELECT LOWER(contact_email), MIN(contact_unsubscribed_at) FROM contacts WHERE contact_unsubscribed_at IS NOT NULL AND LOWER(contact_email) IN ('" . implode("', '", array_map('escapeSql', $emails)) . "') GROUP BY LOWER(contact_email)"); while ($row = mysqli_fetch_row($sql)) { $out[(string) $row[0]] = (string) $row[1]; } return $out; } /** The "Unsubscribed" badge for a contact row - '' while they get the email sent to many. */ function contactUnsubscribeBadge(array $row) { $at = contactUnsubscribed($row); if ($at === null) { return ''; } return '' . 'Unsubscribed'; } /** The key the links are signed with - made and kept the first time one is needed. '' before the update. */ function contactUnsubscribeSecret() { global $mysqli; static $secret = null; if ($secret !== null) { return $secret; } if (!contactUnsubscribeReady()) { return $secret = ''; } $saved = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_mail_unsubscribe_secret FROM settings WHERE company_id = 1"))[0] ?? ''); if (strlen($saved) < 32) { $made = bin2hex(random_bytes(32)); // Kept only if none was made meanwhile - two at once agree on the one saved mysqli_query($mysqli, "UPDATE settings SET config_mail_unsubscribe_secret = '$made' WHERE company_id = 1 AND (config_mail_unsubscribe_secret IS NULL OR LENGTH(config_mail_unsubscribe_secret) < 32)"); $saved = (string) (mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_mail_unsubscribe_secret FROM settings WHERE company_id = 1"))[0] ?? ''); } return $secret = $saved; } /** A link's signature for contact $contact_id at $email - 128 bits, URL-safe. '' before the update. */ function contactUnsubscribeToken($contact_id, $email) { $secret = contactUnsubscribeSecret(); if ($secret === '') { return ''; } $mac = hash_hmac('sha256', 'unsubscribe|' . intval($contact_id) . '|' . strtolower(trim((string) $email)), $secret, true); return rtrim(strtr(base64_encode(substr($mac, 0, 16)), '+/', '-_'), '='); } /** The link to leave, for contact $contact_id at $email - '' before the update. */ function contactUnsubscribeUrl($contact_id, $email) { global $config_base_url; $token = contactUnsubscribeToken($contact_id, $email); return $token !== '' ? "https://$config_base_url/guest/unsubscribe.php?c=" . intval($contact_id) . "&t=$token" : ''; } /* * The contact a link is for - $contact_id and its signature $token - when it is right for the address the contact has now: * ['contact_id', 'contact_name', 'contact_email', 'contact_client_id', 'contact_unsubscribed_at'], or null. */ function contactUnsubscribeLink($contact_id, $token) { global $mysqli; $contact_id = intval($contact_id); if (!$contact_id || !contactUnsubscribeReady() || !is_string($token) || $token === '') { return null; } $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_id, contact_name, contact_email, contact_client_id, contact_unsubscribed_at FROM contacts WHERE contact_id = $contact_id AND contact_archived_at IS NULL")); if (!$row || trim((string) $row['contact_email']) === '' || !hash_equals(contactUnsubscribeToken($contact_id, $row['contact_email']), $token)) { return null; } return $row; } /* * Every contact with $email out of the email sent to many ($out true), or back in - a person's say-so is about their * address, whichever client it is a contact at. How many contacts changed. */ function contactUnsubscribeSet($email, $out) { global $mysqli; $email = strtolower(trim((string) $email)); if (!contactUnsubscribeReady() || $email === '') { return 0; } // Archived ones too - one restored later comes back as they left it mysqli_query($mysqli, "UPDATE contacts SET contact_unsubscribed_at = " . ($out ? 'NOW()' : 'NULL') . ", contact_updated_at = contact_updated_at WHERE LOWER(contact_email) = '" . escapeSql($email) . "' AND contact_unsubscribed_at IS " . ($out ? 'NULL' : 'NOT NULL')); return mysqli_affected_rows($mysqli); }