-
Notifications
You must be signed in to change notification settings - Fork 1
/
rules_api.rules.inc
159 lines (139 loc) · 3.83 KB
/
rules_api.rules.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
<?php
/**
* implements hook_rules_action_info
* creates custom action for rules
*/
function rules_api_rules_action_info() {
return array(
'rules_api_rules_make_request' => array(
'label' => t('Make an external HTTP request'),
'group' => t('API Hook'),
'parameter' => array(
'url' => array(
'label' => t('URL'),
'type' => 'text',
),
'method' => array(
'label' => t('HTTP method to use'),
'type' => 'text',
'options list' => '_rules_api_rules_http_methods',
'default value' => 'POST',
),
'headers' => array(
'label' => t('Headers to send with request'),
'type' => 'text',
'optional' => true,
),
'data' => array(
'label' => t('Data to send with request'),
'type' => 'text',
'description' => t('Supports tokens'),
),
'log_call' => array(
'label' => t('Log API calls to Drupal Watchdog?'),
'type' => 'boolean',
'default value' => false,
),
),
'provides' => array(
'status_code' => array(
'type' => 'integer',
'label' => t('Status Code'),
),
'status_message' => array(
'type' => 'text',
'label' => t('Status Message'),
),
'error' => array(
'type' => 'text',
'label' => t('Error Message'),
),
'data' => array(
'type' => 'text',
'label' => t('Data'),
),
),
'access callback' => '_rules_api_access_callback',
),
);
}
/**
* callback function for making an http request
*/
function rules_api_rules_make_request($url, $method, $headers, $data, $log_call) {
$return = array('status_code' => null, 'status_message' => null, 'error' => null, 'data' => null);
if (!valid_url($url, true)) {
$return['error'] = 'Invalid URL';
return $return;
}
$headers_options = _rules_api_parse_headers($headers);
$result = drupal_http_request($url, array('headers' => $headers_options, 'method' => $method, 'data' => $data));
$return['status_code'] = isset($result->code) ? $result->code : null;
$return['status_message'] = isset($result->status_message) ? $result->status_message : null;
$return['error'] = isset($result->error) ? $result->error : null;
$return['data'] = isset($result->data) ? $result->data : null;
if ($log_call) {
$log = $return;
$log['data_returned'] = $log['data'];
$log = array_merge($log, array(
'hook' => 'hook_rules_action',
'url' => $url,
'method' => $method,
'headers' => $headers,
'data' => $data,
));
_rules_api_log_request($log);
}
return $return;
}
/**
* helper function to provide list of HTTP methods
*/
function _rules_api_rules_http_methods() {
$methods = array(
'POST' => 'POST',
'GET' => 'GET',
'PUT' => 'PUT',
'DELETE' => 'DELETE',
'HEAD' => 'HEAD',
'OPTIONS' => 'OPTIONS',
'TRACE' => 'TRACE',
'CONNECT' => 'CONNECT'
);
return $methods;
}
/**
* helper function to provide access control
*/
function _rules_api_access_callback() {
return user_access('administer rules api');
}
/**
* helper function to parse headers from text to array
*/
function _rules_api_parse_headers($headers_text) {
$headers_arr = explode("\n", $headers_text);
$headers = array();
foreach ($headers_arr as $header_line) {
$header_components = explode(":", $header_line);
if (count($header_components) < 2) { continue; }
$header_header = trim(array_shift($header_components));
if ($header_header) {
$headers[$header_header] = trim(implode(":",$header_components));
}
}
return $headers;
}
/**
* helper function to log requests
*/
function _rules_api_log_request($log) {
$object = new stdClass();
$object->timestamp = time();
$fields = array('hook','url','method','headers','data','callback','status_code',
'status_method','error','data_returned');
foreach ($fields as $field) {
$object->$field = isset($log[$field]) ? $log[$field] : null;
}
watchdog('rules_api', '<pre>'.print_r($object, 1).'</pre>');
}