-
Notifications
You must be signed in to change notification settings - Fork 1
/
ElggApiCall.php
187 lines (158 loc) · 5.75 KB
/
ElggApiCall.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
<?php
/**
* This class sends Elgg API calls with optional HMAC authentication.
*
* The methods in the class have been reverse-engineered
* from the Elgg 1.8 API engine.
*
* @see http://elgg.org/ (source file: elgg-1.8.x/engine/lib/web_services.php)
*/
class ElggApiCall {
/**
* Send api call.
*
* @param string $url URL of the endpoint.
* @param array $call Associated array of "variable" => "value"
* @param string $method GET or POST
* @param string $post_data The post data
* @param string $content_type The content type
*/
public function sendApiCall(array $keys, $url, array $call, $method = 'GET', $post_data = '', $content_type = 'application/octet-stream') {
$headers = array();
$encoded_params = array();
$method = strtoupper($method);
switch (strtoupper($method)) {
case 'GET' :
case 'POST' :
break;
default:
throw new Exception("Call method $method is not implemented");
}
// Time
$time = time();
// Nonce
$nonce = uniqid('');
// URL encode all the parameters
foreach ($call as $k => $v) {
$encoded_params[] = urlencode($k) . '=' . urlencode($v);
}
$params = implode('&', $encoded_params);
// Put together the query string
$url = $url . "?" . $params;
// Construct headers
$posthash = "";
if ($method == 'POST') {
$posthash = $this->calculate_posthash($post_data, 'md5');
}
if ((isset($keys['public'])) && (isset($keys['private']))) {
$headers['X-Elgg-apikey'] = $keys['public'];
$headers['X-Elgg-time'] = $time;
$headers['X-Elgg-nonce'] = $nonce;
$headers['X-Elgg-hmac-algo'] = 'sha1';
$headers['X-Elgg-hmac'] = $this->calculate_hmac(
'sha1',
$time,
$nonce,
$keys['public'],
$keys['private'],
$params,
$posthash
);
}
if ($method == 'POST') {
$headers['X-Elgg-posthash'] = $posthash;
$headers['X-Elgg-posthash-algo'] = 'md5';
$headers['Content-type'] = $content_type;
$headers['Content-Length'] = strlen($post_data);
}
// Opt array
$http_opts = array(
'method' => $method,
'header' => $this->serialise_api_headers($headers)
);
if ($method == 'POST') {
$http_opts['content'] = $post_data;
}
$opts = array('http' => $http_opts);
// Send context
$context = stream_context_create($opts);
// Send the query and get the result. Suppress possible warning.
$results = @file_get_contents($url, false, $context);
if (!$results) {
$message = get_string("bad_url", 'block_elgg_community');
throw new Exception($message);
}
return $results;
}
/**
* Calculate the HMAC for the http request.
* This function signs an api request using the information provided. The signature returned
* has been base64 encoded and then url encoded.
*
* @param string $algo The HMAC algorithm used
* @param string $time String representation of unix time
* @param string $api_key Your api key
* @param string $secret Your private key
* @param string $get_variables URLEncoded string representation of the get variable parameters, eg "method=user&guid=2"
* @param string $post_hash Optional sha1 hash of the post data.
* @return string The HMAC signature
*/
private function calculate_hmac($algo, $time, $nonce, $api_key, $secret_key, $get_variables, $post_hash = "") {
$ctx = hash_init($this->map_api_hash($algo), HASH_HMAC, $secret_key);
hash_update($ctx, trim($time));
hash_update($ctx, trim($nonce));
hash_update($ctx, trim($api_key));
hash_update($ctx, trim($get_variables));
if (trim($post_hash)!="") {
hash_update($ctx, trim($post_hash));
}
return urlencode(base64_encode(hash_final($ctx, true)));
}
/**
* Map various algorithms to their PHP equivs.
* This also gives us an easy way to disable algorithms.
*
* @param string $algo The algorithm
* @return string The php algorithm
*/
private function map_api_hash($algo) {
$algo = strtolower($algo);
$supported_algos = array(
"md5" => "md5",
"sha" => "sha1", // alias for sha1
"sha1" => "sha1",
"sha256" => "sha256"
);
if (array_key_exists($algo, $supported_algos)) {
return $supported_algos[$algo];
}
throw new Exception("Algorithm $algo is not supported");
}
/**
* Calculate a hash for some post data.
*
* @todo Work out how to handle really large bits of data.
*
* @param string $postdata string The post data.
* @param string $algo The algorithm used.
* @return string The hash.
*/
private function calculate_posthash($postdata, $algo) {
$ctx = hash_init($this->map_api_hash($algo));
hash_update($ctx, $postdata);
return hash_final($ctx);
}
/**
* Utility function to serialise a header array into its text representation.
*
* @param array $headers The array of headers "key" => "value"
* @return string
*/
private function serialise_api_headers(array $headers) {
$headers_str = "";
foreach ($headers as $k => $v) {
$headers_str .= trim($k) . ": " . trim($v) . "\r\n";
}
return trim($headers_str);
}
}