-
Notifications
You must be signed in to change notification settings - Fork 5
/
sendgrid.php
349 lines (305 loc) · 10.3 KB
/
sendgrid.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
require_once 'sendgrid.civix.php';
// misc filename constants
define('HTACCESS', __DIR__ . '/.htaccess');
define('HTPASSWD', __DIR__ . '/.htpasswd');
define('EXT_DIR', __DIR__);
$sendgrid_settings = array();
/**
* Implements hook_civicrm_tokens().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_tokens
*/
function sendgrid_civicrm_tokens(&$tokens) {
// for each group of tokens, add an array to $tokens keyed by the group name
// the Civi UI will uppercase first letters in the group name
$tokens['sendgrid'] = array(
// token => label
'sendgrid.click_track' => 'Click Track',
);
}
/**
* Implements hook_civicrm_tokenValues().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_tokenValues
*/
function sendgrid_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
// the hook_civicrm_tokenValues documentation doesn't indicate this possibility,
// but searching through the code clearly shows that it can be called this way
if ($is_single = !is_array($cids)) {
$values = array($cids => $values);
$cids = array($cids);
}
if (!empty($tokens['sendgrid'])) {
foreach($cids as $cid) {
$values[$cid]['sendgrid.click_track'] = '&sgct=1';
}
}
// if this was called as a single contact, put the values array back in the correct format
if ($is_single)
$values = $values[$cids[0]];
}
/*
* hook_civicrm_alterMailParams
*/
function sendgrid_civicrm_alterMailParams(&$params, $context) {
static $job_cache = array();
static $mailing_cache = array();
if ($context == 'civimail') {
$config = CRM_Core_Config::singleton();
@list($ignore, $job_id, $event_queue_id, $hash) = explode($config->verpSeparator, substr($params['Return-Path'], 0, strpos($params['Return-Path'], '@')));
if (!$job_id)
return;
require_once('api/api.php');
try {
if (empty($job_cache[$job_id]))
$job_cache[$job_id] = civicrm_api3('MailingJob', 'getsingle', array('id' => $job_id));;
$job = $job_cache[$job_id];
if (empty($mailing_cache[$job['mailing_id']]))
$mailing_cache[$job['mailing_id']] = civicrm_api3('Mailing', 'getsingle', array('id' => $job['mailing_id']));
$mailing = $mailing_cache[$job['mailing_id']];
$settings = sendgrid_get_settings();
$clicktrack = ($settings['open_click_processor'] == 'SendGrid') && $mailing['url_tracking'] ? '1' : '0';
$opentrack = ($settings['open_click_processor'] == 'SendGrid') && $mailing['open_tracking'] ? '1' : '0';
// prepare the SendGrid SMTP API header
$header = array(
'filters' => array(
'clicktrack' => array(
'settings' => array(
'enable' => $clicktrack
)
),
'opentrack' => array(
'settings' => array(
'enable' => $opentrack
)
)
),
'unique_args' => array(
'job_id' => $job_id,
'event_queue_id' => $event_queue_id,
'hash' => $hash
)
);
$params['X-SMTPAPI'] = trim(substr(preg_replace('/(.{1,70})(,|:|\})/', '$1$2' . "\n", 'X-SMTPAPI: ' . json_encode($header)), 11));
if ($opentrack && !empty($params['html'])) {
// remove the CiviMail generated open tracking img
$img = '#<img src="' . $config->userFrameworkResourceURL . "extern/open\.php\?q=$event_queue_id\".*?>#";
$params['html'] = preg_replace($img, '', $params['html']);
}
}
catch (CiviCRM_API3_Exception $e) {
require_once('CRM/Core/Error.php');
CRM_Core_Error::debug_log_message($e->getMessage() . print_r($params, true));
}
}
}
/*
* hook_civicrm_buildForm
*
* set tracking options for mailing
*/
function sendgrid_civicrm_buildForm($formName, &$form) {
if (($formName == 'CRM_Mailing_Form_Settings') && ($form->elementExists('url_tracking'))) {
$settings = sendgrid_get_settings();
$track = $settings['open_click_processor'] != 'Never';
$freeze = !$track || !$settings['track_optional'];
$el = $form->getElement('url_tracking');
if ($freeze)
$el->freeze();
$el = $form->getElement('open_tracking');
if ($freeze)
$el->freeze();
$form->setDefaults(array('url_tracking' => $track, 'open_tracking' => $track));
}
elseif ($formName == 'CRM_Report_Form_Mailing_Detail') {
// this will check Spam Report in the report criteria,
// but it still doesn't cause that column to be displayed by default.
// I can't figure out how to make that happen.
$grp = $form->getElement('fields');
$els = $grp->getElements();
foreach($els as $el) {
if ($el->getName() == 'spam_id') {
$el->setChecked(true);
break;
}
}
}
}
/*
* hook_civicrm_install
*/
function sendgrid_civicrm_install() {
CRM_Core_DAO::executeQuery("CREATE TABLE IF NOT EXISTS `civicrm_mailing_event_spam_report` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`event_queue_id` int(10) unsigned NOT NULL COMMENT 'FK to EventQueue',
`time_stamp` datetime NOT NULL COMMENT 'When this open event occurred.',
PRIMARY KEY (`id`),
KEY `FK_civicrm_mailing_event_opened_event_queue_id` (`event_queue_id`),
CONSTRAINT `FK_civicrm_mailing_event_spam_report_event_queue_id` FOREIGN KEY (`event_queue_id`) REFERENCES `civicrm_mailing_event_queue` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
_sendgrid_civix_civicrm_install();
}
/*
* hook_civicrm_navigationMenu
*
* add "SendGrid Configuration" to the Mailings menu
*/
function sendgrid_civicrm_navigationMenu(&$params) {
require_once('CRM/Core/BAO/Navigation.php');
// Check that our item doesn't already exist
$menu_item_search = array('url' => 'civicrm/sendgrid');
$menu_items = array();
CRM_Core_BAO_Navigation::retrieve($menu_item_search, $menu_items);
if (!empty($menu_items))
return;
$navID = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_navigation");
if (is_integer($navID))
$navID++;
// Find the CiviMail menu
$parentID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Navigation', 'Mailings', 'id', 'name');
$params[$parentID]['child'][$navID] = array(
'attributes' => array(
'label' => ts('SendGrid Configuration'),
'name' => 'SendGrid Configuration',
'url' => 'civicrm/sendgrid',
'permission' => 'access CiviMail,administer CiviCRM',
'operator' => 'AND',
'separator' => 1,
'parentID' => $parentID,
'navID' => $navID,
'active' => 1
)
);
}
/**
* Implementation of hook_civicrm_uninstall
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function sendgrid_civicrm_uninstall() {
CRM_Core_DAO::executeQuery("DROP TABLE `civicrm_mailing_event_spam_report`");
_sendgrid_civix_civicrm_uninstall();
}
/*
* sendgrid_get_settings
*
* return settings from the database, or empty defaults
*/
function sendgrid_get_settings() {
global $sendgrid_settings;
if (empty($sendgrid_settings)) {
require_once 'CRM/Core/BAO/Setting.php';
$sendgrid_settings = array(
'username' => CRM_Core_BAO_Setting::getItem('sendgrid', 'username', null, ''),
'password' => CRM_Core_BAO_Setting::getItem('sendgrid', 'password', null, ''),
'open_click_processor' => CRM_Core_BAO_Setting::getItem('sendgrid', 'open_click_processor', null, 'CiviMail'),
'track_optional' => CRM_Core_BAO_Setting::getItem('sendgrid', 'track_optional', null, '1')
);
}
return $sendgrid_settings;
}
/*
* sendgrid_save_settings
*
* save settings to database
*/
function sendgrid_save_settings($settings) {
global $sendgrid_settings;
require_once 'CRM/Core/BAO/Setting.php';
foreach($settings as $k => $v) {
$sendgrid_settings[$k] = $v;
try {
CRM_Core_BAO_Setting::setItem($v, 'sendgrid', $k);
}
catch (Exception $e) {
require_once 'CRM/Core/Error.php';
CRM_Core_Error::debug_log_message($e->getMessage());
}
}
}
CRM_Core_Resources::singleton()
->addScriptFile('com.imba.sendgrid', 'js/sendgrid.js')
->addVars('sendgrid', sendgrid_get_settings());
// *************************************
// THE REST IS JUST STANDARD BOILERPLATE
// *************************************
/**
* Implementation of hook_civicrm_config
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function sendgrid_civicrm_config(&$config) {
_sendgrid_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function sendgrid_civicrm_xmlMenu(&$files) {
_sendgrid_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_enable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function sendgrid_civicrm_enable() {
_sendgrid_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function sendgrid_civicrm_disable() {
_sendgrid_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function sendgrid_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _sendgrid_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function sendgrid_civicrm_managed(&$entities) {
_sendgrid_civix_civicrm_managed($entities);
}
/**
* Implementation of hook_civicrm_caseTypes
*
* Generate a list of case-types
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function sendgrid_civicrm_caseTypes(&$caseTypes) {
_sendgrid_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implementation of hook_civicrm_alterSettingsFolders
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function sendgrid_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_sendgrid_civix_civicrm_alterSettingsFolders($metaDataFolders);
}