-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathZestHook.php
361 lines (333 loc) · 13 KB
/
ZestHook.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
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* Send a curl post request after each afterSurveyComplete event
*
* @author Stefan Verweij <[email protected]>
* @copyright 2016 Evently <https://www.evently.nl>
* @license GPL v3
* @version 1.0.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
class ZestHook extends PluginBase
{
protected $storage = 'DbStorage';
static protected $description = 'Webhook for Limesurvey: send a curl POST after every response submission.';
static protected $name = 'ZestHook';
protected $surveyId;
public function init()
{
$this->subscribe('afterSurveyComplete');
$this->subscribe('beforeSurveySettings');
$this->subscribe('newSurveySettings');
}
protected $settings = array(
'bUse' => array(
'type' => 'select',
'options' => array(
0 => 'No',
1 => 'Yes'
),
'default' => 1,
'label' => 'Send a hook for every survey by default?',
'help' => 'Overwritable in each Survey setting'
),
'sUrl' => array(
'type' => 'string',
'default' => 'https://zest.evently.nl/api/1/ping',
'label' => 'The default address to send the webhook to',
'help' => 'If you are using Zest, this should be https://zest.evently.nl/api/1/ping'
),
'sAuthToken' => array(
'type' => 'string',
'label' => 'Zest Platform API Token',
'help' => 'To get a token logon to your account and click on the Tokens tab'
),
'sServerToken' => array(
'type' => 'string',
'label' => 'Zest server id token',
'help' => 'To get a token logon to your account, go to your servers and copy the server id'
)
);
/**
* Add setting on survey level: send hook only for certain surveys / url setting per survey / auth code per survey / send user token / send question response
*/
public function beforeSurveySettings()
{
$oEvent = $this->event;
$oEvent->set("surveysettings.{$this->id}", array(
'name' => get_class($this),
'settings' => array(
'bUse' => array(
'type' => 'select',
'label' => 'Send a hook for this survey',
'options' => array(
0 => 'No',
1 => 'Yes',
2 => 'Use site settings (default)'
),
'default' => 2,
'help' => 'Leave default to use global setting',
'current' => $this->get('bUse', 'Survey', $oEvent->get('survey'))
),
'bUrlOverwrite' => array(
'type' => 'select',
'label' => 'Overwrite the global Hook Url',
'options' => array(
0 => 'No',
1 => 'Yes'
),
'default' => 0,
'help' => 'Set to Yes if you want to use a specific URL for this survey',
'current' => $this->get('bUrlOverwrite', 'Survey', $oEvent->get('survey'))
),
'sUrl' => array(
'type' => 'string',
'label' => 'The address to send the hook for this survey to:',
'help' => 'Leave blank to use global setting',
'current' => $this->get('sUrl', 'Survey', $oEvent->get('survey'))
),
'bAuthTokenOverwrite' => array(
'type' => 'select',
'label' => 'Overwrite the global authorization token',
'options' => array(
0 => 'No',
1 => 'Yes'
),
'default' => 0,
'help' => 'Set to Yes if you want to use a specific zest API token for this survey',
'current' => $this->get('bAuthTokenOverwrite', 'Survey', $oEvent->get('survey'))
),
'sAuthToken' => array(
'type' => 'string',
'label' => 'Use a specific API Token for this survey (leave blank to use default)',
'help' => 'Leave blank to use default',
'current' => $this->get('sAuthToken', 'Survey', $oEvent->get('survey'))
),
'bServerTokenOverwrite' => array(
'type' => 'select',
'label' => 'Overwrite the global server token',
'options' => array(
0 => 'No',
1 => 'Yes'
),
'default' => 0,
'help' => 'Set to Yes if you want to use a specific Zest server-token for this survey',
'current' => $this->get('bServerTokenOverwrite', 'Survey', $oEvent->get('survey'))
),
'sServerToken' => array(
'type' => 'string',
'label' => 'Zest server-token',
'help' => 'To get a token, log in to your account, go to your servers and copy the server token',
'current' => $this->get('sServerToken', 'Survey', $oEvent->get('survey'))
),
'bSendToken' => array(
'type' => 'select',
'label' => 'Send the users\' token to the hook',
'options' => array(
0 => 'No',
1 => 'Yes'
),
'default' => 0,
'help' => 'Set to Yes if you want to pass the users token along in the request',
'current' => $this->get('bSendToken', 'Survey', $oEvent->get('survey'))
),
'sAnswersToSend' => array(
'type' => 'string',
'label' => 'Answers to send',
'help' => 'Comma separated question codes of the answers you want to send along',
'current' => $this->get('sAnswersToSend', 'Survey', $oEvent->get('survey'))
),
'bRequestType' => array(
'type' => 'select',
'label' => 'Request Type',
'default' => 0,
'options' => array(
0 => 'POST',
1 => 'GET'
),
'current' => $this->get('bRequestType', 'Survey', $oEvent->get('survey'))
),
'sPostSignature' => array(
'type' => 'string',
'default' => '{"survey":"{surveyId}","token":"{token}","api_token":"{apiToken}","server_key":"{serverKey}","additionalFields":"{additionalFields}"}',
'label' => 'JSON Signature',
'help' => 'JSON signature with placeholders {surveyId},{token},{apiToken},{surverKey} and {additionalFields}, use {{fieldcode}} for additional specific fields values',
'current' => $this->get('sPostSignature', 'Survey', $oEvent->get('survey'))
),
'bDebugMode' => array(
'type' => 'select',
'options' => array(
0 => 'No',
1 => 'Yes'
),
'default' => 0,
'label' => 'Enable Debug Mode',
'help' => 'Enable debugmode to see what data is transmitted. Respondents will see this as well so you should turn this off for live surveys',
'current' => $this->get('bDebugMode', 'Survey', $oEvent->get('survey')),
)
)
));
}
/**
* Save the settings
*/
public function newSurveySettings()
{
$event = $this->event;
foreach ($event->get('settings') as $name => $value) {
/* In order use survey setting, if not set, use global, if not set use default */
$default = $event->get($name, null, null, isset($this->settings[$name]['default']) ? $this->settings[$name]['default'] : NULL);
$this->set($name, $value, 'Survey', $event->get('survey'), $default);
}
}
/**
* Send the webhook on completion of a survey
* @return array | response
*/
public function afterSurveyComplete()
{
$time_start = microtime(true);
$oEvent = $this->getEvent();
$this->surveyId = $oEvent->get('surveyId');
if ($this->isHookDisabled()) {
return;
}
$url = ($this->get('bUrlOverwrite', 'Survey', $this->surveyId) === '1') ? $this->get('sUrl', 'Survey', $this->surveyId) : $this->get('sUrl', null, null, $this->settings['sUrl']);
$auth = ($this->get('bAuthTokenOverwrite', 'Survey', $this->surveyId) === '1') ? $this->get('sAuthToken', 'Survey', $this->surveyId) : $this->get('sAuthToken', null, null, $this->settings['sAuthToken']);
$serverToken = ($this->get('bServerTokenOverwrite', 'Survey', $this->surveyId) === '1') ? $this->get('sServerToken', 'Survey', $this->surveyId) : $this->get('sServerToken', null, null, $this->settings['sServerToken']);
$postSignature = $this->get('sPostSignature', 'Survey', $this->surveyId);
$requestType = $this->get('bRequestType', 'Survey', $this->surveyId);
$additionalFields = $this->getAdditionalFields();
if (($this->get('bSendToken', 'Survey', $this->surveyId) === '1') || (count($additionalFields) > 0)) {
$responseId = $oEvent->get('responseId');
$response = $this->api->getResponse($this->surveyId, $responseId);
$sToken = $this->getTokenString($response);
$additionalAnswers = $this->getAdditionalAnswers($additionalFields, $response);
}
if ($postSignature) {
$mainFields = array("/{surveyId}/", "/{token}/", "/{apiToken}/", "/{serverKey}/", "/{additionalFields}/");
$mainValues = array($this->surveyId, (isset($sToken)) ? $sToken : null, $auth, $serverToken, ($additionalFields) ? json_encode($additionalAnswers) : null);
$parameters = preg_replace($mainFields, $mainValues, $postSignature);
if ($additionalFields) {
foreach ($additionalAnswers as $key => $val)
$parameters = preg_replace("/{{" . $key . "}}/", $val, $parameters);
}
$parameters = json_decode($parameters, true);
} else {
$parameters = array(
"survey" => $this->surveyId,
"token" => (isset($sToken)) ? $sToken : null,
"api_token" => $auth,
"server_key" => $serverToken,
"additionalFields" => ($additionalFields) ? json_encode($additionalAnswers) : null
);
}
if ($requestType == 1)
$hookSent = $this->httpGet($url, $parameters);
else
$hookSent = $this->httpPost($url, $parameters);
$this->debug($parameters, $hookSent, $time_start,$response);
return;
}
/**
* httpPost function http://hayageek.com/php-curl-post-get/
* creates and executes a POST request
* returns the output
*/
private function httpPost($url, $params)
{
$fullUrl = $url . '?api_token=' . $params['api_token'];
$postData = $params;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $fullUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
/**
* httpGet
* creates and executes a GET request
* returns the output
*/
private function httpGet($url, $params)
{
$postData = http_build_query($params, '', '&');
$fullUrl = $url . '?' . $postData;
$fp = fopen(dirname(__FILE__) . '/errorlog.txt', 'w');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fullUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
/**
* check if the hook should be sent
* returns a boolean
*/
private function isHookDisabled()
{
return ($this->get('bUse', 'Survey', $this->surveyId) == 0) || (($this->get('bUse', 'Survey', $this->surveyId) == 2) && ($this->get('bUse', null, null, $this->settings['bUse']) == 0));
}
/**
* check if the hook should be sent
* returns a boolean
*/
private function getTokenString($response)
{
return ($this->get('bSendToken', 'Survey', $this->surveyId) === '1') ? $response['token'] : null;
}
/**
*
*
*/
private function getAdditionalFields()
{
$additionalFieldsString = $this->get('sAnswersToSend', 'Survey', $this->surveyId);
if ($additionalFieldsString != '' || $additionalFieldsString != null) {
return explode(',', $this->get('sAnswersToSend', 'Survey', $this->surveyId));
}
return null;
}
private function getAdditionalAnswers($additionalFields = null, $response)
{
if ($additionalFields) {
$additionalAnswers = array();
foreach ($additionalFields as $field) {
$additionalAnswers[$field] = htmlspecialchars($response[$field]);
}
return $additionalAnswers;
}
return null;
}
private function debug($parameters, $hookSent, $time_start, $response)
{
if ($this->get('bDebugMode', 'Survey', $this->surveyId) == 1) {
$html = '<pre>';
$html .= print_r($parameters, true);
$html .= "<br><br> ----------------------------- <br><br>";
$html .= print_r($hookSent, true);
$html .= "<br><br> ----------------------------- <br><br>";
$html .= print_r($response, true);
$html .= "<br><br> ----------------------------- <br><br>";
$html .= 'Total execution time in seconds: ' . (microtime(true) - $time_start);
$html .= '</pre>';
$event = $this->getEvent();
$event->getContent($this)->addContent($html);
}
}
}