Files
itflow/guest/unsubscribe.php

100 lines
5.1 KiB
PHP

<?php
/*
* ITFlow - leaving the email sent to many from the contacts or clients list, by the link in one
* (functions/contact_unsubscribe.php). The link is signed for the contact and the address they have:
* GET asks first, with a button - a link checker that opens every link in an email mustn't unsubscribe anyone
* POST from that button - unsubscribe, or subscribe again
* POST List-Unsubscribe=One-Click - a mail program's own Unsubscribe (RFC 8058): done, with no page
* It is about the address: every contact with it leaves, whichever client they are at. Ticket emails, invoices
* and quotes still go.
*/
require_once $_SERVER['DOCUMENT_ROOT'] . '/config.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/functions.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/load_global_settings.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/inc_set_timezone.php';
// logAudit() reads these - who did it, from where
$session_ip = escapeSql(getIP());
$session_user_agent = escapeSql(mb_substr((string) ($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 250));
$contact = contactUnsubscribeLink($_GET['c'] ?? 0, $_GET['t'] ?? '');
/* Out ($out true) or back in - every contact with the address - and said in the audit log, $how. */
function guestUnsubscribe(array $contact, $out, $how) {
if (contactUnsubscribeSet($contact['contact_email'], $out)) {
logAudit("Contact", "Edit", escapeSql($contact['contact_email'] . ($out ? ' unsubscribed from' : ' subscribed again to') . " bulk email - $how"),
intval($contact['contact_client_id']), intval($contact['contact_id']));
}
}
// A mail program's own Unsubscribe - one click, nothing to show
if (($_SERVER['REQUEST_METHOD'] ?? '') === 'POST' && trim((string) ($_POST['List-Unsubscribe'] ?? '')) === 'One-Click') {
header('Content-Type: text/plain; charset=utf-8');
if (!$contact) {
http_response_code(404);
echo "This unsubscribe link isn't working.\n";
exit;
}
guestUnsubscribe($contact, true, 'with one click in their mail program');
echo "Unsubscribed.\n";
exit;
}
$done = '';
if ($contact && ($_SERVER['REQUEST_METHOD'] ?? '') === 'POST' && in_array($_POST['action'] ?? '', ['unsubscribe', 'subscribe'], true)) {
$out = $_POST['action'] === 'unsubscribe';
guestUnsubscribe($contact, $out, 'by the link in one');
$contact = contactUnsubscribeLink($contact['contact_id'], $_GET['t'] ?? '');
$done = $out ? 'out' : 'in';
}
require_once "includes/inc_all_guest.php";
if (!$contact) {
echo guestDeadEndHtml('fa-envelope', "This unsubscribe link isn't working", "It may have been cut short, or the address it was for has changed. Reply to the email, and we'll take you off.");
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
exit();
}
$company = escapeHtml($session_company_name);
$address = escapeHtml($contact['contact_email']);
$out = contactUnsubscribed($contact) !== null;
$action_url = 'unsubscribe.php?c=' . intval($contact['contact_id']) . '&t=' . rawurlencode((string) ($_GET['t'] ?? ''));
?>
<div class="card mt-3 mb-3 mx-auto" style="max-width: 36rem;" data-unsubscribe="<?= $out ? 'out' : 'in' ?>">
<div class="card-header bg-dark text-center py-3">
<h4 class="mb-0"><i class="fas fa-fw fa-envelope me-2"></i><?= $out ? 'Unsubscribed' : 'Unsubscribe' ?></h4>
</div>
<div class="card-body text-center py-4">
<?php if ($out) { ?>
<div class="alert alert-success d-inline-block">
<i class="fas fa-fw fa-check-circle me-2"></i><strong><?= $address ?></strong> won't get the emails <?= $company ?> sends to all its clients.
</div>
<p class="text-body-secondary">Emails about your tickets, invoices and quotes still come to you.</p>
<form action="<?= escapeHtml($action_url) ?>" method="post" class="mt-3">
<input type="hidden" name="action" value="subscribe">
<span class="text-body-secondary me-2">Changed your mind?</span>
<button type="submit" class="btn btn-outline-primary"><i class="fas fa-fw fa-bell me-2"></i>Subscribe Again</button>
</form>
<?php } else { ?>
<?php if ($done === 'in') { ?>
<div class="alert alert-success d-inline-block"><i class="fas fa-fw fa-check-circle me-2"></i>You'll get them again.</div>
<?php } ?>
<p class="mb-1">Stop the emails <?= $company ?> sends to all its clients at</p>
<p class="fw-bold fs-5 mb-3"><?= $address ?></p>
<p class="text-body-secondary">Emails about your tickets, invoices and quotes still come to you.</p>
<form action="<?= escapeHtml($action_url) ?>" method="post" class="d-grid gap-2 d-sm-block">
<input type="hidden" name="action" value="unsubscribe">
<button type="submit" class="btn btn-primary btn-lg"><i class="fas fa-fw fa-bell-slash me-2"></i>Unsubscribe</button>
</form>
<?php } ?>
</div>
</div>
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';