-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunsubscribe.php
More file actions
63 lines (57 loc) · 2.17 KB
/
Copy pathunsubscribe.php
File metadata and controls
63 lines (57 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
require_once 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
use MongoDB\Client;
use MongoDB\BSON\UTCDateTime;
$message = '';
$isError = false;
$token = $_GET['token'] ?? '';
$token = preg_replace('/[^a-f0-9]/', '', $token);
if (empty($token)) {
$message = "Invalid or missing unsubscribe token.";
$isError = true;
} else {
try {
$url = $_ENV['MONGODB_URI'] ?? getenv('MONGODB_URI');
$mydatabase = $_ENV['MONGODB_DATABASE'] ?? getenv('MONGODB_DATABASE');
$client = new Client($url);
$subscribers = $client->selectCollection($mydatabase, 'subscribers');
$result = $subscribers->findOneAndUpdate(
['unsubscribe_token' => $token],
['$set' => ['unsubscribed' => true, 'updated_at' => new UTCDateTime()]],
['returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER]
);
if ($result) {
$message = "You have been successfully unsubscribed from our newsletter. You will no longer receive newsletter emails.";
} else {
$message = "No active subscription found for this token.";
$isError = true;
}
} catch (Exception $e) {
$message = "An error occurred. Please try again later.";
$isError = true;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unsubscribe</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5" style="max-width: 500px;">
<div class="card p-4 text-center shadow-sm">
<h2 class="mb-3">Unsubscribe</h2>
<div class="alert <?= $isError ? 'alert-warning' : 'alert-success' ?>">
<?= htmlspecialchars($message) ?>
</div>
<p class="mt-3 font-size-13 text-muted">If you did not request this, you can safely ignore this message.</p>
<a href="index.php" class="btn btn-primary mt-3">Return to Home</a>
</div>
</div>
</body>
</html>