|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
function wfDebugLog($section, $message) |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
// no op, needed by the mediawiki oauth library. |
|
6
|
|
|
} |
|
7
|
|
|
|
|
8
|
|
|
class OAuthUtility |
|
9
|
|
|
{ |
|
10
|
|
|
private $consumerToken; |
|
11
|
|
|
private $consumerSecret; |
|
12
|
|
|
private $baseUrl; |
|
13
|
|
|
private $baseUrlInternal; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($consumerToken, $consumerSecret, $baseUrl, $baseUrlInternal) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->consumerSecret = $consumerSecret; |
|
18
|
|
|
$this->consumerToken = $consumerToken; |
|
19
|
|
|
$this->baseUrl = $baseUrl; |
|
20
|
|
|
$this->baseUrlInternal = $baseUrlInternal; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function getRequestToken() |
|
24
|
|
|
{ |
|
25
|
|
|
global $toolUserAgent; |
|
26
|
|
|
|
|
27
|
|
|
$endpoint = $this->baseUrlInternal . '/initiate&format=json&oauth_callback=oob'; |
|
28
|
|
|
|
|
29
|
|
|
$c = new OAuthConsumer($this->consumerToken, $this->consumerSecret); |
|
|
|
|
|
|
30
|
|
|
$parsed = parse_url($endpoint); |
|
31
|
|
|
$params = array(); |
|
32
|
|
|
parse_str($parsed['query'], $params); |
|
33
|
|
|
$req_req = OAuthRequest::from_consumer_and_token($c, null, "GET", $endpoint, $params); |
|
|
|
|
|
|
34
|
|
|
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1(); |
|
|
|
|
|
|
35
|
|
|
$req_req->sign_request($hmac_method, $c, null); |
|
36
|
|
|
|
|
37
|
|
|
$ch = curl_init(); |
|
38
|
|
|
curl_setopt($ch, CURLOPT_URL, (string)$req_req); |
|
|
|
|
|
|
39
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
|
40
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
41
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
42
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $toolUserAgent); |
|
43
|
|
|
$data = curl_exec($ch); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
if (!$data) { |
|
46
|
|
|
throw new Exception('Curl error: ' . curl_error($ch)); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$token = json_decode($data); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if (!isset($token) || isset($token->error)) { |
|
52
|
|
|
throw new Exception("Error encountered while getting token."); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $token; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getAuthoriseUrl($requestToken) |
|
59
|
|
|
{ |
|
60
|
|
|
return "{$this->baseUrl}/authorize&oauth_token={$requestToken->key}&oauth_consumer_key={$this->consumerToken}"; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function callbackCompleted($requestToken, $requestSecret, $verifyToken) |
|
64
|
|
|
{ |
|
65
|
|
|
global $toolUserAgent; |
|
66
|
|
|
|
|
67
|
|
|
$endpoint = $this->baseUrlInternal . '/token&format=json'; |
|
68
|
|
|
|
|
69
|
|
|
$c = new OAuthConsumer($this->consumerToken, $this->consumerSecret); |
|
70
|
|
|
$rc = new OAuthConsumer($requestToken, $requestSecret); |
|
71
|
|
|
$parsed = parse_url($endpoint); |
|
72
|
|
|
parse_str($parsed['query'], $params); |
|
73
|
|
|
$params['oauth_verifier'] = trim($verifyToken); |
|
74
|
|
|
|
|
75
|
|
|
$acc_req = OAuthRequest::from_consumer_and_token($c, $rc, "GET", $endpoint, $params); |
|
76
|
|
|
|
|
77
|
|
|
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1(); |
|
78
|
|
|
$acc_req->sign_request($hmac_method, $c, $rc); |
|
79
|
|
|
|
|
80
|
|
|
unset($ch); |
|
|
|
|
|
|
81
|
|
|
$ch = curl_init(); |
|
82
|
|
|
curl_setopt($ch, CURLOPT_URL, (string)$acc_req); |
|
|
|
|
|
|
83
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
|
84
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
85
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
86
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $toolUserAgent); |
|
87
|
|
|
|
|
88
|
|
|
$data = curl_exec($ch); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
if (!$data) { |
|
91
|
|
|
throw new Exception('Curl error: ' . curl_error($ch)); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$token = json_decode($data); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
return $token; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function apiCall($apiParams, $accessToken, $accessSecret, $method = "GET") |
|
100
|
|
|
{ |
|
101
|
|
|
global $mediawikiWebServiceEndpoint, $toolUserAgent; |
|
102
|
|
|
|
|
103
|
|
|
$userToken = new OAuthToken($accessToken, $accessSecret); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
$apiParams['format'] = 'json'; |
|
106
|
|
|
|
|
107
|
|
|
$c = new OAuthConsumer($this->consumerToken, $this->consumerSecret); |
|
108
|
|
|
|
|
109
|
|
|
$api_req = OAuthRequest::from_consumer_and_token( |
|
110
|
|
|
$c, // Consumer |
|
111
|
|
|
$userToken, // User Access Token |
|
112
|
|
|
$method, // HTTP Method |
|
113
|
|
|
$mediawikiWebServiceEndpoint, // Endpoint url |
|
114
|
|
|
$apiParams // Extra signed parameters |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1(); |
|
118
|
|
|
|
|
119
|
|
|
$api_req->sign_request($hmac_method, $c, $userToken); |
|
120
|
|
|
|
|
121
|
|
|
$ch = curl_init(); |
|
122
|
|
|
if ($method == "GET") { |
|
123
|
|
|
curl_setopt($ch, CURLOPT_URL, $mediawikiWebServiceEndpoint . "?" . http_build_query($apiParams)); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ($method == "POST") { |
|
127
|
|
|
curl_setopt($ch, CURLOPT_URL, $mediawikiWebServiceEndpoint); |
|
128
|
|
|
curl_setopt($ch, CURLOPT_POST, count($apiParams)); |
|
129
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiParams)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
133
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
134
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
|
135
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array($api_req->to_header())); // Authorization header required for api |
|
136
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $toolUserAgent); |
|
137
|
|
|
|
|
138
|
|
|
$data = curl_exec($ch); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
if (!$data) { |
|
141
|
|
|
throw new Exception('Curl error: ' . curl_error($ch)); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return json_decode($data); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function getIdentity($accessToken, $accessSecret) |
|
148
|
|
|
{ |
|
149
|
|
|
|
|
150
|
|
|
global $toolUserAgent; |
|
151
|
|
|
|
|
152
|
|
|
$endpoint = $this->baseUrlInternal . '/identify&format=json'; |
|
153
|
|
|
|
|
154
|
|
|
$c = new OAuthConsumer($this->consumerToken, $this->consumerSecret); |
|
155
|
|
|
$rc = new OAuthToken($accessToken, $accessSecret); |
|
156
|
|
|
$parsed = parse_url($endpoint); |
|
157
|
|
|
parse_str($parsed['query'], $params); |
|
158
|
|
|
|
|
159
|
|
|
$acc_req = OAuthRequest::from_consumer_and_token($c, $rc, "GET", $endpoint, $params); |
|
160
|
|
|
|
|
161
|
|
|
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1(); |
|
162
|
|
|
$acc_req->sign_request($hmac_method, $c, $rc); |
|
163
|
|
|
|
|
164
|
|
|
unset($ch); |
|
|
|
|
|
|
165
|
|
|
$ch = curl_init(); |
|
166
|
|
|
curl_setopt($ch, CURLOPT_URL, (string)$acc_req); |
|
|
|
|
|
|
167
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
|
168
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
169
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
170
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $toolUserAgent); |
|
171
|
|
|
|
|
172
|
|
|
$data = curl_exec($ch); |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
if (!$data) { |
|
175
|
|
|
throw new Exception('Curl error: ' . curl_error($ch)); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$decodedData = json_decode($data); |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
if (isset($decodedData->error)) { |
|
181
|
|
|
throw new TransactionException($decodedData->error); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$identity = JWT::decode($data, $this->consumerSecret); |
|
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
return $identity; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.