-
Notifications
You must be signed in to change notification settings - Fork 2
/
rules_web_hook.module
282 lines (271 loc) · 6.92 KB
/
rules_web_hook.module
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
<?php
// $Id$
/**
* @file Rules Web Hooks - Module file.
*/
/**
* Implements of hook_entity_info().
*/
function rules_web_hook_entity_info() {
return array(
'rules_web_hook' => array(
'label' => t('Web hook'),
'controller class' => 'EntityAPIController',
'base table' => 'rules_web_hook',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'name',
),
'exportable' => TRUE,
'access callback' => 'rules_web_hook_access_callback',
),
);
}
/**
* Implements hook_permission().
*/
function rules_web_hook_permission() {
return array(
'administer web hooks' => array(
'title' => t('Administer web hooks'),
'description' => t('Define web hooks and administer remote sites.'),
),
'subscribe to web hooks' => array(
'title' => t('Subscribe to web hooks'),
),
);
}
/**
* An access callback for the rules web entities.
*
* @see rules_web_entity_info()
*/
function rules_web_hook_access_callback($op, $entity = NULL, $account = NULL) {
if ($op == 'view') {
return user_access('subscribe to web hooks', $account);
}
return user_access('administer web hooks', $account);
}
/**
* Gets a REST client for sending notifications about occured web hooks.
*/
function rules_web_hook_get_client($auth = NULL) {
$client = &drupal_static(__FUNCTION__);
if (!isset($client)) {
$client = new RestClient(NULL, new RestClientBaseFormatter(RestClientBaseFormatter::FORMAT_JSON));
$client->curlOpts = variable_get('rules_web_custom_curl_options', array());
}
// Add http auth credentials if necessary.
$client->curlOpts[CURLOPT_USERPWD] = !empty($auth['username']) ? $auth['username'] . ':' . $auth['password'] : '';
$client->curlOpts[CURLOPT_HTTPAUTH] = !empty($auth['method']) ? (int)$auth['method'] : '';
return $client;
}
/**
* Loads a web hook object.
*/
function rules_web_hook_load($name) {
$return = entity_load('rules_web_hook', array($name));
return reset($return);
}
/**
* Invoke a web hook.
*
* @param $hook
* The object of the hook to invoke.
* @param $args
* An array of arguments to pass to the hook, keyed with the variable names
* as described in $hook->variables. Optionally the arguments may be entity
* metadata wrapper.
*/
function rules_web_hook_invoke($hook, $args = array()) {
// Get the actual property values by applying all getter callbacks.
foreach ($hook->variables as $name => $var_info) {
if ($args[$name] instanceof EntityMetadataWrapper) {
$args[$name] = EntityResourceServicesCRUD::getData($args[$name], 'rules_web');
}
}
// Send notifications to all subscribed HTTP callbacks.
$time = time();
$query = db_select('rules_web_hook_subscriber', 'rs')
->fields('rs')
->condition('hook', $hook->name);
foreach ($query->execute() as $row) {
try {
$client = rules_web_hook_get_client(unserialize($row->http_auth));
$client->post($row->url, array(
'event_name' => $hook->name,
'event_data' => $args,
'token' => rules_web_hook_hash_token($row->token, $time),
'time' => $time,
));
}
catch (Exception $e) {
$args = array('%name' => $hook->name, '%url' => $row->url, '%message' => $e->getMessage());
watchdog('rules web hooks', 'Error sending hook %name notification to %url. Error message: %message.', $args, WATCHDOG_WARNING);
}
}
}
/**
* Hashes the token and the provided timestamp.
*/
function rules_web_hook_hash_token($token, $timestamp) {
// A single md5() is vulnerable to length-extension attacks, so use it twice.
return md5($timestamp . md5($token));
}
/**
* Implements hook_default_services_endpoint().
*/
function rules_web_hook_default_services_endpoint() {
$endpoint = new stdClass;
$endpoint->disabled = FALSE;
$endpoint->name = 'rules_web_hook';
$endpoint->title = 'Rules Web Hooks Endpoint';
$endpoint->server = 'rest_server';
$endpoint->path = 'rules_web';
$endpoint->authentication = array();
$endpoint->authentication_settings = FALSE;
$endpoint->resources = array(
'node' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 0,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 0,
),
'delete' => array(
'enabled' => 0,
),
'index' => array(
'enabled' => 0,
),
),
),
'comment' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 0,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 0,
),
'delete' => array(
'enabled' => 0,
),
'index' => array(
'enabled' => 0,
),
),
),
'user' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 0,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 0,
),
'delete' => array(
'enabled' => 0,
),
'index' => array(
'enabled' => 0,
),
),
),
'taxonomy_term' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 0,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 0,
),
'delete' => array(
'enabled' => 0,
),
'index' => array(
'enabled' => 0,
),
),
),
'taxonomy_vocabulary' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 0,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 0,
),
'delete' => array(
'enabled' => 0,
),
'index' => array(
'enabled' => 0,
),
),
),
'rules_web_hook' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 0,
),
'retrieve' => array(
'enabled' => 0,
),
'update' => array(
'enabled' => 0,
),
'delete' => array(
'enabled' => 0,
),
'index' => array(
'enabled' => 1,
),
),
),
'entity_metadata' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 0,
),
'retrieve' => array(
'enabled' => 0,
),
'update' => array(
'enabled' => 0,
),
'delete' => array(
'enabled' => 0,
),
'index' => array(
'enabled' => 1,
),
),
),
);
return array('rules_web_hook' => $endpoint);
}