-
Notifications
You must be signed in to change notification settings - Fork 2
/
rules_web_hook.services.inc
177 lines (154 loc) · 5.79 KB
/
rules_web_hook.services.inc
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
<?php
// $Id$
/**
* @file Rules Web Hooks - Provides services.
*/
/**
* Implements hook_services_resources().
*/
function rules_web_hook_services_resources() {
$crud_object = EntityResourceServicesCRUD::get('rules_web_hook');
$crud_object->callbacks['subscribe'] = 'rules_web_hook_subscribe';
$crud_object->callbacks['unsubscribe'] = 'rules_web_hook_unsubscribe';
$resources['rules_web_hook']['index'] = array(
'file' => array('type' => 'inc', 'module' => 'rules_web_hook', 'name' => 'rules_web_hook.services'),
'callback' => 'rules_web_hook_list_hooks',
'args' => array(),
'models' => array(),
'access callback' => array($crud_object, 'access'),
'access arguments' => array('view'),
);
$resources['rules_web_hook']['targeted actions']['subscribe'] = array(
'file' => array('type' => 'inc', 'module' => 'rules_web_hook', 'name' => 'rules_web_hook.services'),
'callback' => array($crud_object, 'subscribe'),
'args' => array(
array(
'name' => 'id',
'optional' => FALSE,
'source' => array('path' => 0),
'type' => 'string',
),
array(
'name' => 'url',
'source' => array('data' => 'url'),
'description' => 'The URL where to post notifications to.',
'type' => 'string',
'optional' => FALSE,
),
array(
'name' => 'data',
'source' => 'data',
'description' => 'The whole passed data.',
'type' => 'array',
'optional' => FALSE,
),
),
'models' => array(),
'access callback' => array($crud_object, 'access'),
'access arguments' => array('view'),
);
$resources['rules_web_hook']['targeted actions']['unsubscribe'] = array(
'callback' => array($crud_object, 'unsubscribe'),
) + $resources['rules_web_hook']['targeted actions']['subscribe'];
$resources['entity_metadata']['index'] = array(
'file' => array('type' => 'inc', 'module' => 'rules_web_hook', 'name' => 'rules_web_hook.services'),
'callback' => 'rules_web_hook_list_entity_metadata',
'args' => array(),
'models' => array(),
'access callback' => array($crud_object, 'access'),
'access arguments' => array('view'),
);
return $resources;
}
/**
* Service resource callback: Index all web hooks.
*/
function rules_web_hook_list_hooks() {
return entity_load('rules_web_hook', FALSE, array('active' => TRUE));
}
/**
* Service resource callback: List metadata about entites, but only if the
* current user has access to use it - so the user isn't aware of unaccesible
* entities / properties.
*/
function rules_web_hook_list_entity_metadata($account = NULL) {
$info = entity_get_info();
$return = array();
foreach ($info as $type => $entity_info) {
if (entity_metadata_entity_access('view', $type, NULL, $account)) {
$return[$type] = array_intersect_key($entity_info, array_flip(array('label', 'entity keys')));
// Make sure property names in the entity keys don't contain underscores.
foreach ($return[$type]['entity keys'] as $key => $value) {
$return[$type]['entity keys'][$key] = str_replace('_', '-', $value);
}
$wrapper = entity_metadata_wrapper($type);
foreach ($wrapper as $name => $property) {
if ($property->access('view', $account)) {
$return[$type]['properties'][$name] = array_intersect_key($property->info(), array_flip(array('label', 'type', 'description', 'sanitize')));
}
}
// Add in bundle properties
foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
$return[$type]['bundles'][$bundle] = array_intersect_key($bundle_info, array_flip(array('label')));
if (!empty($bundle_info['properties'])) {
$wrapper = entity_metadata_wrapper($type, $data = NULL, array('bundle' => $bundle));
foreach ($bundle_info['properties'] as $name => $info) {
if ($wrapper->$name->access('view', $account)) {
$return[$type]['bundles'][$bundle]['properties'][$name] = array_intersect_key($info, array_flip(array('label', 'type', 'description', 'sanitize')));
}
}
}
}
}
}
return $return;
}
/**
* Service resource action callback invoked via the entity resource crud object.
*/
function rules_web_hook_subscribe($crud_object, $args) {
if ($hook = $crud_object->load($args[0])) {
if (valid_url($url = (string)$args[1])) {
$data = $args[2] + array('http_auth' => NULL);
try {
// Verify the URL is valid except if it's from our own site (tests!).
if (strpos($url, $GLOBALS['base_url'] !== 0)) {
$client = rules_web_hook_get_client($data['http_auth']);
$client->post($url, array(
'handshake' => TRUE,
'token' => rules_web_hook_hash_token((string)$data['token'], $time = time()),
'time' => $time,
));
}
db_merge('rules_web_hook_subscriber')
->key(array('hook' => $hook->name, 'url' => $url))
->fields(array(
'uid' => $GLOBALS['user']->uid,
'http_auth' => serialize($data['http_auth']),
'token' => (string)$data['token'],
))
->execute();
return TRUE;
}
catch (Exception $e) {
// Troubles connecting to $url.
}
}
services_error('Connecting to provided URL fails.', 406);
}
services_error('Hook not found.', 404);
}
/**
* Service resource action callback invoked via the entity resource crud object.
*/
function rules_web_hook_unsubscribe($crud_object, $args) {
if (isset($args[2]['token'])) {
db_delete('rules_web_hook_subscriber')
->condition('hook', (string)$args[0])
->condition('url', (string)$args[1])
->condition('uid', $GLOBALS['user']->uid)
->condition('token', (string)$args[2]['token'])
->execute();
return TRUE;
}
}