-
Notifications
You must be signed in to change notification settings - Fork 5
/
webhook.php
174 lines (150 loc) · 5.24 KB
/
webhook.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
$events = json_decode(file_get_contents('php://input'));
if (!$events || !is_array($events)) {
// SendGrid sends a json encoded array of events
// if that's not what we get, we're done here
header("HTTP/1.0 404 Not Found");
exit();
}
function sendgrid_bounce($job_id, $event_queue_id, $hash, $reason) {
try {
civicrm_api3('Mailing', 'event_bounce', array(
'job_id' => $job_id,
'event_queue_id' => $event_queue_id,
'hash' => $hash,
'body' => $reason
));
}
catch (CiviCRM_API3_Exception $e) {
CRM_Core_Error::debug_log_message("SendGrid webhook (bounce)\n" . $e->getMessage());
}
}
function sendgrid_unsubscribe($job_id, $event_queue_id, $hash, $event) {
try {
civicrm_api3('MailingGroup', 'event_unsubscribe', array(
'job_id' => $job_id,
'event_queue_id' => $event_queue_id,
'hash' => $hash
));
}
catch (CiviCRM_API3_Exception $e) {
CRM_Core_Error::debug_log_message("SendGrid webhook ($event)\n" . $e->getMessage());
}
}
function sendgrid_spamreport($job_id, $event_queue_id, $hash, $event) {
CRM_Mailing_Event_BAO_SpamReport::report($event_queue_id);
sendgrid_unsubscribe($job_id, $event_queue_id, $hash, $event);
}
// if we made it this far then we need to boostrap CiviCRM
session_start();
require_once $_SERVER['DOCUMENT_ROOT'] . '/sites/all/modules/civicrm/civicrm.config.php';
require_once 'CRM/Core/Config.php';
require_once 'api/api.php';
$config = CRM_Core_Config::singleton();
// now we can process the events
require_once 'CRM/Core/Error.php';
require_once 'CRM/Mailing/Event/BAO/SpamReport.php';
require_once 'CRM/Mailing/Event/BAO/Opened.php';
require_once 'CRM/Mailing/Event/BAO/TrackableURLOpen.php';
require_once 'CRM/Mailing/Event/BAO/Delivered.php';
$delivered = array();
foreach($events as $event) {
if (!empty($event->job_id)) {
/************
* CiviMail *
************/
$job_id = $event->job_id;
$event_queue_id = $event->event_queue_id;
$hash = $event->hash;
switch ($event->event) {
case 'delivered':
/*
$ts = $event->timestamp;
if (empty($delivered[$ts]))
$delivered[$ts] = array();
$delivered[$ts][] = $event_queue_id;
*/
break;
case 'deferred':
// temp failure, just write it to the log
CRM_Core_Error::debug_log_message("Sendgrid webhook (deferred)\n" . print_r($event, true));
break;
case 'bounce':
sendgrid_bounce($job_id, $event_queue_id, $hash, $event->reason);
break;
case 'spamreport':
sendgrid_spamreport($job_id, $event_queue_id, $hash, $event->event);
break;
case 'unsubscribe':
sendgrid_unsubscribe($job_id, $event_queue_id, $hash, $event->event);
break;
case 'dropped':
// if dropped because of previous bounce, unsubscribe, or spam report, treat it as such...
// ...otherwise log it
if ($event->reason == 'Bounced Address') {
sendgrid_bounce($job_id, $event_queue_id, $hash, $event->reason);
}
elseif ($event->reason == 'Unsubscribed Address') {
sendgrid_unsubscribe($job_id, $event_queue_id, $hash, $event->event);
}
elseif ($event->reason == 'Spam Reporting Address') {
sendgrid_spamreport($job_id, $event_queue_id, $hash, $event->event);
}
else {
CRM_Core_Error::debug_log_message("Sendgrid webhook (dropped)\n" . print_r($event, true));
}
break;
case 'open':
CRM_Mailing_Event_BAO_Opened::open($event_queue_id);
break;
case 'click':
$track = FALSE;
@list($url, $back) = explode('?', $event->url);
if ($back) {
@list($qs, $fragment) = explode('#', $back);
$query = array();
$pairs = explode('&', $qs);
foreach($pairs as $pair) {
@list($key, $val) = explode('=', $pair);
// strip off any utm_??? query parameters for google analytics
if (strpos($key, 'utm_') === 0) {
continue;
}
$query[$key] = $pair;
}
if ($track = !empty($query['sgct'])) {
unset($query['cs'], $query['cid'], $query['sgct']);
}
$url .= (!empty($query) ? ('?' . implode('&', array_values($query))) : '') . ($fragment ? "#$fragment" : '');
}
echo $url;
try {
$url = CRM_Core_DAO::escapeString($url);
$mailing_id = CRM_Core_DAO::singleValueQuery("SELECT mailing_id FROM civicrm_mailing_job WHERE id='$job_id'");
$url_id = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_mailing_trackable_url WHERE mailing_id='$mailing_id' AND url='$url'");
if (!$url_id && $track) {
CRM_Core_DAO::executeQuery("INSERT INTO civicrm_mailing_trackable_url (url,mailing_id) VALUES ('$url',$mailing_id)");
$url_id = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_mailing_trackable_url WHERE mailing_id='$mailing_id' AND url='$url'");
}
if ($url_id) {
CRM_Mailing_Event_BAO_TrackableURLOpen::track($event_queue_id, $url_id);
}
}
catch (Exception $e) {
CRM_Core_Error::debug_log_message("SendGrid webhook (click)\n" . $e->getMessage());
}
break;
}
}
}
// bulk add the deliveries to the database
/*
if (!empty($delivered)) {
foreach($delivered as $ts => $event_queue_ids) {
$time = date('YmdHis', $ts);
CRM_Mailing_Event_BAO_Delivered::bulkCreate($event_queue_ids, $time);
}
}
*/
// that's all she wrote
?>