Repository navigation
Expand file tree
/
Copy patht.php
More file actions
118 lines (110 loc) · 5.65 KB
/
Copy patht.php
File metadata and controls
118 lines (110 loc) · 5.65 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
/* Public tracking endpoint: opens (?o=), clicks (?c=&l=&s=), unsubscribe (?u=). */
require __DIR__ . '/app/bootstrap.php';
$db = db();
$ua = mb_substr((string)($_SERVER['HTTP_USER_AGENT'] ?? ''), 0, 250);
$ip = (string)($_SERVER['REMOTE_ADDR'] ?? '');
$isBot = (bool)preg_match('/(bot|crawler|spider|curl|wget|python|java\/|go-http|barracuda|mimecast|proofpoint|symantec|trendmicro|forcepoint|cisco|ironport|fortinet|sophos|safelinks|urldefense|headless|preview)/i', $ua);
function rx_event(array $m, string $type, ?string $url = null): void
{
global $db, $ip, $ua;
$lp = local_parts();
$db->insert('events', [
'message_id' => $m['id'], 'campaign_id' => $m['campaign_id'], 'account_id' => $m['account_id'], 'company_id' => $m['company_id'],
'user_id' => $m['user_id'], 'type' => $type, 'url' => $url, 'ip' => $ip, 'user_agent' => $ua,
'created_at' => now(), 'day' => $lp['day'], 'hour' => $lp['hour'], 'dow' => $lp['dow'],
]);
}
function rx_msg(string $token): ?array
{
global $db;
if (!preg_match('/^[a-f0-9]{16,40}$/', $token)) return null;
return $db->row('SELECT * FROM messages WHERE token = ?', [$token]);
}
// open pixel
if (isset($_GET['o'])) {
$m = rx_msg((string)$_GET['o']);
// ignore hits in the first few seconds after sending (security scanners / prefetch)
if ($m && !$isBot && !empty($m['sent_at']) && strtotime($m['sent_at'] . ' UTC') < time() - 4
&& !$db->row("SELECT id FROM events WHERE message_id = ? AND type = 'open' AND ip = ? AND created_at > ? LIMIT 1", [$m['id'], $ip, now(-60)])) {
$db->exec('UPDATE messages SET open_count = open_count + 1, opened_at = COALESCE(opened_at, ?) WHERE id = ?', [now(), $m['id']]);
rx_event($m, 'open');
}
if (!$m) {
http_response_code(404);
}
header('Content-Type: image/gif');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('X-Robots-Tag: noindex, nofollow');
echo base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
exit;
}
// click redirect
if (isset($_GET['c'])) {
$token = (string)$_GET['c'];
$enc = (string)($_GET['l'] ?? '');
$url = (string)base64_decode(strtr($enc, '-_', '+/') . str_repeat('=', (4 - strlen($enc) % 4) % 4), true);
$host = (string)(parse_url($url, PHP_URL_HOST) ?? '');
if ($host === '' || !preg_match('#^https?://#i', $url) || !hash_equals(Crypto::sign($token . '|' . $url), (string)($_GET['s'] ?? ''))) {
http_response_code(400);
exit('Invalid link');
}
$m = rx_msg($token);
if ($m && !$isBot) {
$db->exec('UPDATE messages SET click_count = click_count + 1, clicked_at = COALESCE(clicked_at, ?), opened_at = COALESCE(opened_at, ?) WHERE id = ?', [now(), now(), $m['id']]);
rx_event($m, 'click', mb_substr($url, 0, 2000));
}
header('X-Robots-Tag: noindex, nofollow');
header('Location: ' . $url, true, 302);
exit;
}
// unsubscribe
if (isset($_GET['u'])) {
$m = rx_msg((string)$_GET['u']);
$done = false;
$unsub = function () use ($m, $db) {
if (!$m) return;
suppress($m['to_email'], (int)$m['company_id'], 'unsubscribe');
if ($m['contact_id']) {
$db->exec("UPDATE contacts SET status = 'unsubscribed' WHERE id = ?", [$m['contact_id']]);
$db->exec("UPDATE campaign_leads SET status = 'unsubscribed', next_send_at = NULL WHERE contact_id = ? AND status IN ('pending','active')", [$m['contact_id']]);
}
if (!$m['unsubscribed_at']) {
$db->exec('UPDATE messages SET unsubscribed_at = ? WHERE id = ?', [now(), $m['id']]);
rx_event($m, 'unsubscribe');
}
};
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
// reject POST with an invalid/missing token (token in ?u= is the auth)
if (!$m) {
http_response_code(400);
header('Cache-Control: no-store');
header('X-Robots-Tag: noindex, nofollow');
header('Content-Type: text/plain; charset=utf-8');
exit('Invalid unsubscribe token');
}
$unsub(); // RFC 8058 one-click (List-Unsubscribe-Post) and the confirm button
$done = true;
}
$company = $m ? $db->row('SELECT name FROM companies WHERE id = ?', [$m['company_id']]) : null;
if (!$m) {
http_response_code(404);
}
header('Cache-Control: no-store');
header('Referrer-Policy: no-referrer');
header('X-Robots-Tag: noindex, nofollow');
?><!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Unsubscribe</title>
<style>body{font-family:system-ui,Arial,sans-serif;background:#f4f4f5;color:#222;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0;padding:16px}.box{background:#fff;padding:32px;border-radius:12px;max-width:420px;width:100%;box-shadow:0 4px 20px rgba(0,0,0,.06);text-align:center}button{background:#d71920;color:#fff;border:0;padding:12px 22px;border-radius:8px;font-size:15px;cursor:pointer}</style></head>
<body><div class="box">
<?php if (!$m): ?><h2>Link not valid</h2><p>This unsubscribe link has expired or is invalid.</p>
<?php elseif ($done || $m['unsubscribed_at']): ?><h2>You're unsubscribed</h2><p><?= e($m['to_email']) ?> will no longer receive emails from <?= e($company['name'] ?? 'us') ?>.</p>
<?php else: ?><h2>Unsubscribe?</h2><p>Stop receiving emails at <b><?= e($m['to_email']) ?></b> from <?= e($company['name'] ?? 'us') ?>.</p>
<form method="post"><button type="submit">Unsubscribe</button></form>
<?php endif; ?>
</div></body></html><?php
exit;
}
http_response_code(404);