-
Notifications
You must be signed in to change notification settings - Fork 2
/
rules_web_remote.module
201 lines (186 loc) · 6.23 KB
/
rules_web_remote.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
<?php
// $Id$
/**
* @file Rules Remote Sites - Module file.
*/
/**
* Implements of hook_menu().
*/
function rules_web_remote_menu() {
$items['rules_web/rules_web_remote/%rules_web_remote/notify'] = array(
'page callback' => 'rules_web_remote_notify',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'rules_web_remote.inc',
);
return $items;
}
function rules_web_remote_access($remote) {
return $remote->access();
}
/**
* Implements of hook_entity_info().
*/
function rules_web_remote_entity_info() {
return array(
'rules_web_remote' => array(
'label' => t('Remote site'),
'entity class' => 'RulesWebRemote',
'controller class' => 'EntityAPIController',
'base table' => 'rules_web_remote',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'name',
),
'exportable' => TRUE,
),
);
}
/**
* Implements of hook_entity_info_alter().
*/
function rules_web_remote_entity_info_alter(&$info) {
$remotes = entity_load('rules_web_remote', FALSE);
// We register entities for all remote data types, however we skip the remote
// site pointing at ourself ('self') defined by the testing module, as this
// would result in issueing a page request during the entity info cache
// refresh on our site, which would result in a nice cache building endless
// loop.
unset($remotes['self']);
foreach ($remotes as $name => $remote) {
foreach ($remote->entities() as $type => $type_info) {
// Don't add in entity types, that stem from another site.
if (strpos($type, 'rules_web_') !== 0) {
$type_info += array('entity keys' => array(), 'label' => $type);
$info['rules_web_' . $name . '_' . $type] = array(
'label' => t('Remote %name %type', array('%name' => $remote->label, '%type' => $type_info['label'])),
'entity keys' => $type_info['entity keys'],
'controller class' => 'RulesWebRemoteEntityController',
'rules web remote' => array(
'remote' => $name,
'type' => $type,
),
'fieldable' => FALSE,
'revision table' => FALSE,
);
foreach ($type_info['bundles'] as $bundle => $bundle_info) {
$info['rules_web_' . $name . '_' . $type]['bundles'][$bundle] = array_intersect_key($bundle_info, array_flip(array('label')));
}
// Apply defaults as entity_get_info().
$info['rules_web_' . $name . '_' . $type] += array(
'static cache' => TRUE,
'load hook' => 'rules_web_' . $name . '_' . $type . '_load',
'view modes' => array(),
'cacheable' => TRUE,
'translation' => array(),
);
}
}
}
}
/**
* Implements of hook_entity_property_info().
*/
function rules_web_remote_entity_property_info() {
$info = array();
foreach (entity_load('rules_web_remote', FALSE) as $name => $remote) {
foreach ($remote->entities() as $type => $type_info) {
// Don't add in entity types, that stem from another site.
if (strpos($type, 'rules_web_') !== 0) {
$info['rules_web_' . $name . '_' . $type] = _rules_web_remote_get_property_info($type_info);
foreach ($type_info['bundles'] as $bundle => $bundle_info) {
$info['rules_web_' . $name . '_' . $type]['bundles'][$bundle] = _rules_web_remote_get_property_info($bundle_info);
}
}
}
}
return $info;
}
function _rules_web_remote_get_property_info($type_info) {
$info = array_intersect_key($type_info, array_flip(array('properties')));
if (!empty($info['properties'])) {
foreach ($info['properties'] as &$property_info) {
// Filter out only allowed keys.
$property_info = array_intersect_key($property_info, array_flip(array('label', 'type', 'description', 'sanitize')));
if (empty($property_info['type']) || $property_info['type'] == 'text') {
// Make sure to sanitize text - don't trust given sanitize functions.
$property_info += array('sanitize' => 'check_plain');
if (!in_array($property_info['sanitize'], array('check_plain', 'filter_xss'))) {
$property_info['sanitize'] = 'check_plain';
}
}
}
}
return $info;
}
/**
* Implements hook_permission().
*/
function rules_web_remote_permission() {
$permissions['administer remote sites'] = array(
'title' => t('Administer remote sites'),
'description' => t('Administer remote sites used with rules.'),
);
foreach (entity_load('rules_web_remote', FALSE) as $name => $remote) {
$permissions['interact with remote ' . $name] = array(
'title' => t('Interact with remote site %label', array('%label' => $remote->label)),
);
}
return $permissions;
}
/**
* Gets all defined remote endpoint types.
*/
function rules_web_remote_get_types() {
return rules_fetch_data('endpoint_types');
}
/**
* Implements hook_rules_endpoint_types().
*/
function rules_web_remote_rules_endpoint_types() {
return array(
'rules_web_hook' => array(
'label' => t('Rules Web Hooks'),
'class' => 'RulesWebRemoteEndpointWebHooks',
),
);
}
/**
* Load a single rules web remote.
*
* @return RulesWebRemote
* The rules web remote site definition or FALSE.
*/
function rules_web_remote_load($name) {
$return = entity_load('rules_web_remote', array($name));
return reset($return);
}
/**
* Gets configured http auth settings.
*/
function rules_web_remote_get_http_auth() {
if (($username = variable_get('rules_web_httpauth_username', NULL)) && $password = variable_get('rules_web_httpauth_password', NULL)) {
return array(
'method' => variable_get('rules_web_httpauth_method', CURLAUTH_BASIC),
'username' => $username,
'password' => $password,
);
}
}
/**
* Implements hook_rules_event_set_alter().
*/
function rules_web_remote_rules_event_set_alter($event_name, RulesEventSet $event_set) {
// If a remote event is used make sure we are subscribed to it.
if (strpos($event_name, 'rules_web_') === 0) {
$info = $event_set->info();
if ($remote = rules_web_remote_load($info['rules web remote']['remote'])) {
$event = $info['rules web remote']['event'];
if (!$remote->isSubscribedTo($event)) {
$remote->subscribe($event);
}
}
}
}