mirror of
https://github.com/itflow-org/itflow
synced 2026-12-02 09:09:01 +00:00
141 lines
6.9 KiB
PHP
141 lines
6.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* ITFlow - contacts who don't want the email sent to many from the contacts or clients list (Email - functions/email.php,
|
|
* emailBulkSend()). Each of those emails has a link to leave them, in the email and in its List-Unsubscribe header, which
|
|
* mail programs show as their own Unsubscribe (RFC 8058, one click). The link (guest/unsubscribe.php) is signed for the
|
|
* contact and their address; leaving takes every contact with that address out (contact_unsubscribed_at, database 4.3.0).
|
|
* They can come back from the same link or Your account in the client portal, and an agent can do either on the contact.
|
|
* Ticket emails, invoices, quotes and the rest still go - this is only the email sent to many.
|
|
*/
|
|
|
|
/** Whether the 4.3.0 update has run - contacts.contact_unsubscribed_at and settings.config_mail_unsubscribe_secret. */
|
|
function contactUnsubscribeReady() {
|
|
global $mysqli;
|
|
static $ready = null;
|
|
if ($ready === null) {
|
|
if (defined('CURRENT_DATABASE_VERSION')) {
|
|
$version = CURRENT_DATABASE_VERSION;
|
|
} else {
|
|
$result = mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1");
|
|
$version = $result && ($row = mysqli_fetch_row($result)) ? (string) $row[0] : '0';
|
|
}
|
|
$ready = version_compare($version, '4.3.0', '>=');
|
|
}
|
|
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 '<span class="badge rounded-pill bg-secondary-subtle text-secondary-emphasis border border-secondary-subtle fw-normal" title="'
|
|
. escapeHtml('Unsubscribed from bulk email (Email on the contacts or clients list) ' . date('M j, Y', strtotime($at)) . ' - ticket emails and invoices still go') . '">'
|
|
. '<i class="fas fa-bell-slash me-1"></i>Unsubscribed</span>';
|
|
}
|
|
|
|
/** 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);
|
|
}
|