forked from slashme/parliamentdiagram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
494 lines (436 loc) · 15.4 KB
/
index.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title>OAuth Test</title>
<link rel="stylesheet" type="text/css" href="parliamentstyle.css">
</head>
<body>
<?php
/**
* Written in 2013 by Brad Jorsch
*
* To the extent possible under law, the author(s) have dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*
* See <http://creativecommons.org/publicdomain/zero/1.0/> for a copy of the
* CC0 Public Domain Dedication.
*/
// ******************** CONFIGURATION ********************
/**
* Set this to point to a file (outside the webserver root!) containing the
* following keys:
* - agent: The HTTP User-Agent to use
* - consumerKey: The "consumer token" given to you when registering your app
* - consumerSecret: The "secret token" given to you when registering your app
*/
$inifile = '/data/project/parliamentdiagram/oauth-enduser.ini';
/**
* Set this to the Special:OAuth/authorize URL.
* To work around MobileFrontend redirection, use /wiki/ rather than /w/index.php.
*/
$mwOAuthAuthorizeUrl = 'https://commons.wikimedia.org/wiki/Special:OAuth/authorize';
/**
* Set this to the Special:OAuth URL.
* Note that /wiki/Special:OAuth fails when checking the signature, while
* index.php?title=Special:OAuth works fine.
*/
$mwOAuthUrl = 'https://commons.wikimedia.org/w/index.php?title=Special:OAuth';
/**
* Set this to the API endpoint
*/
$apiUrl = 'https://commons.wikimedia.org/w/api.php';
/**
* This should normally be "500". But Tool Labs insists on overriding valid 500
* responses with a useless error page.
*/
$errorCode = 200;
// ****************** END CONFIGURATION ******************
// Setup the session cookie
session_name( 'ParliamentDiagramUpload' );
$params = session_get_cookie_params();
session_set_cookie_params(
$params['lifetime'],
dirname( $_SERVER['SCRIPT_NAME'] )
);
// Read the ini file
$ini = parse_ini_file( $inifile );
if ( $ini === false ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'The ini file could not be read';
exit(0);
}
if ( !isset( $ini['agent'] ) ||
!isset( $ini['consumerKey'] ) ||
!isset( $ini['consumerSecret'] )
) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'Required configuration directives not found in ini file';
exit(0);
}
$gUserAgent = $ini['agent'];
$gConsumerKey = $ini['consumerKey'];
$gConsumerSecret = $ini['consumerSecret'];
// Load the user token (request or access) from the session
$gTokenKey = '';
$gTokenSecret = '';
session_start();
if ( isset( $_SESSION['tokenKey'] ) ) {
$gTokenKey = $_SESSION['tokenKey'];
$gTokenSecret = $_SESSION['tokenSecret'];
}
session_write_close();
// Fetch the access token if this is the callback from requesting authorization
if ( isset( $_GET['oauth_verifier'] ) && $_GET['oauth_verifier'] ) {
fetchAccessToken();
}
if ( isset( $_GET['action'] ) && $_GET['action'] == 'authorize') { doAuthorizationRedirect(); } else { doIdentify(); }
// ******************** CODE ********************
/**
* Utility function to sign a request
*
* Note this doesn't properly handle the case where a parameter is set both in
* the query string in $url and in $params, or non-scalar values in $params.
*
* @param string $method Generally "GET" or "POST"
* @param string $url URL string
* @param array $params Extra parameters for the Authorization header or post
* data (if application/x-www-form-urlencoded).
* @return string Signature
*/
function sign_request( $method, $url, $params = array() ) {
global $gConsumerSecret, $gTokenSecret;
$parts = parse_url( $url );
// We need to normalize the endpoint URL
$scheme = isset( $parts['scheme'] ) ? $parts['scheme'] : 'http';
$host = isset( $parts['host'] ) ? $parts['host'] : '';
$port = isset( $parts['port'] ) ? $parts['port'] : ( $scheme == 'https' ? '443' : '80' );
$path = isset( $parts['path'] ) ? $parts['path'] : '';
if ( ( $scheme == 'https' && $port != '443' ) ||
( $scheme == 'http' && $port != '80' )
) {
// Only include the port if it's not the default
$host = "$host:$port";
}
// Also the parameters
$pairs = array();
parse_str( isset( $parts['query'] ) ? $parts['query'] : '', $query );
$query += $params;
unset( $query['oauth_signature'] );
if ( $query ) {
$query = array_combine(
// rawurlencode follows RFC 3986 since PHP 5.3
array_map( 'rawurlencode', array_keys( $query ) ),
array_map( 'rawurlencode', array_values( $query ) )
);
ksort( $query, SORT_STRING );
foreach ( $query as $k => $v ) {
$pairs[] = "$k=$v";
}
}
$toSign = rawurlencode( strtoupper( $method ) ) . '&' .
rawurlencode( "$scheme://$host$path" ) . '&' .
rawurlencode( join( '&', $pairs ) );
$key = rawurlencode( $gConsumerSecret ) . '&' . rawurlencode( $gTokenSecret );
return base64_encode( hash_hmac( 'sha1', $toSign, $key, true ) );
}
/**
* Request authorization
* @return void
*/
function doAuthorizationRedirect() {
global $mwOAuthUrl, $mwOAuthAuthorizeUrl, $gUserAgent, $gConsumerKey, $gTokenSecret;
// First, we need to fetch a request token.
// The request is signed with an empty token secret and no token key.
$gTokenSecret = '';
$url = $mwOAuthUrl . '/initiate';
$url .= strpos( $url, '?' ) ? '&' : '?';
$url .= http_build_query( array(
'format' => 'json',
// OAuth information
'oauth_callback' => 'oob', // Must be "oob" for MWOAuth
'oauth_consumer_key' => $gConsumerKey,
'oauth_version' => '1.0',
'oauth_nonce' => md5( microtime() . mt_rand() ),
'oauth_timestamp' => time(),
// We're using secret key signatures here.
'oauth_signature_method' => 'HMAC-SHA1',
) );
$signature = sign_request( 'GET', $url );
$url .= "&oauth_signature=" . urlencode( $signature );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $gUserAgent );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$data = curl_exec( $ch );
if ( !$data ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'Curl error: ' . htmlspecialchars( curl_error( $ch ) );
exit(0);
}
curl_close( $ch );
$token = json_decode( $data );
if ( is_object( $token ) && isset( $token->error ) ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo '<div class="error">';
echo 'Error retrieving token: ' . htmlspecialchars( $token->error ).'</br>';
echo 'Maybe everything is OK: Try <a href="index.php">this link.</a>';
echo '</div>';
exit(0);
}
if ( !is_object( $token ) || !isset( $token->key ) || !isset( $token->secret ) ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'Invalid response from token request';
exit(0);
}
// Now we have the request token, we need to save it for later.
session_start();
$_SESSION['tokenKey'] = $token->key;
$_SESSION['tokenSecret'] = $token->secret;
session_write_close();
// Then we send the user off to authorize
$url = $mwOAuthAuthorizeUrl;
$url .= strpos( $url, '?' ) ? '&' : '?';
$url .= http_build_query( array(
'oauth_token' => $token->key,
'oauth_consumer_key' => $gConsumerKey,
) );
header( "Location: $url" );
echo 'Please see <a href="' . htmlspecialchars( $url ) . '">' . htmlspecialchars( $url ) . '</a>';
}
/**
* Handle a callback to fetch the access token
* @return void
*/
function fetchAccessToken() {
global $mwOAuthUrl, $gUserAgent, $gConsumerKey, $gTokenKey, $gTokenSecret;
$url = $mwOAuthUrl . '/token';
$url .= strpos( $url, '?' ) ? '&' : '?';
$url .= http_build_query( array(
'format' => 'json',
'oauth_verifier' => $_GET['oauth_verifier'],
// OAuth information
'oauth_consumer_key' => $gConsumerKey,
'oauth_token' => $gTokenKey,
'oauth_version' => '1.0',
'oauth_nonce' => md5( microtime() . mt_rand() ),
'oauth_timestamp' => time(),
// We're using secret key signatures here.
'oauth_signature_method' => 'HMAC-SHA1',
) );
$signature = sign_request( 'GET', $url );
$url .= "&oauth_signature=" . urlencode( $signature );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $gUserAgent );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$data = curl_exec( $ch );
if ( !$data ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'Curl error: ' . htmlspecialchars( curl_error( $ch ) );
exit(0);
}
curl_close( $ch );
$token = json_decode( $data );
if ( is_object( $token ) && isset( $token->error ) ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo '<div class="error">';
echo 'Error retrieving token: ' . htmlspecialchars( $token->error ).'</br>';
echo 'Maybe everything is OK: Try <a href="index.php">this link.</a>';
echo '</div>';
exit(0);
}
if ( !is_object( $token ) || !isset( $token->key ) || !isset( $token->secret ) ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'Invalid response from token request';
exit(0);
}
// Save the access token
session_start();
$_SESSION['tokenKey'] = $gTokenKey = $token->key;
$_SESSION['tokenSecret'] = $gTokenSecret = $token->secret;
session_write_close();
}
/**
* Send an API query with OAuth authorization
*
* @param array $post Post data
* @param object $ch Curl handle
* @return array API results
*/
function doApiQuery( $post, &$ch = null , $mode = '' ) {
global $mwOAuthUrl, $gUserAgent, $gConsumerKey, $gTokenKey, $gConsumerSecret, $apiUrl ;
$headerArr = array(
// OAuth information
'oauth_consumer_key' => $gConsumerKey,
'oauth_token' => $gTokenKey,
'oauth_version' => '1.0',
'oauth_nonce' => md5( microtime() . mt_rand() ),
'oauth_timestamp' => time(),
// We're using secret key signatures here.
'oauth_signature_method' => 'HMAC-SHA1',
);
$to_sign = '' ;
if ( $mode == 'upload' ) {
$to_sign = $headerArr ;
} else {
$to_sign = $post + $headerArr ;
}
$url = $apiUrl ;
if ( $mode == 'identify' ) $url .= '/identify' ;
$signature = sign_request( 'POST', $url, $to_sign );
$headerArr['oauth_signature'] = $signature;
$header = array();
foreach ( $headerArr as $k => $v ) {
$header[] = rawurlencode( $k ) . '="' . rawurlencode( $v ) . '"';
}
$header = 'Authorization: OAuth ' . join( ', ', $header );
if ( !$ch ) {
$ch = curl_init();
}
$post_fields = '' ;
if ( $mode == 'upload' ) {
$post_fields = $post ;
} else {
$post_fields = http_build_query( $post ) ;
}
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( $header ) );
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $gUserAgent );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$data = curl_exec( $ch );
if ( isset ( $_REQUEST['test'] ) ) {
print "<hr/><h3>API query</h3>" ;
print "Header:<pre>" ; print_r ( $header ) ; print "</pre>" ;
print "Payload:<pre>" ; print_r ( $post ) ; print "</pre>" ;
print "Result:<pre>" ; print_r ( $data ) ; print "</pre>" ;
print "<hr/>" ;
}
if ( isset($_REQUEST['test']) ) {
print "RESULT:<hr/>" ;
print_r ( $data ) ;
print "<hr/>" ;
}
if ( !$data ) {
return ;
// if ( $mode != 'userinfo' ) header( "HTTP/1.1 500 Internal Server Error" );
$info = curl_getinfo($ch);
print "<pre>" ; print_r ( $info ) ; print "</pre>" ;
echo 'Curl error: ' . htmlspecialchars( curl_error( $ch ) );
exit(0);
}
$ret = json_decode( $data );
if ( $ret == null ) {
return ;
// if ( $mode != 'userinfo' ) header( "HTTP/1.1 500 Internal Server Error" );
print "<h1>API trouble!</h1>" ;
// print "<pre>" ; print_r ($header ) ; print "</pre>" ;
print "<pre>" ; print_r ($post ) ; print "</pre>" ;
print "<pre>" ; print_r ($data ) ; print "</pre>" ;
print "<pre>" ; print var_export ( $ch , 1 ) ; print "</pre>" ;
exit(0);
}
return $ret ;
}
/**
* Request a JWT and verify it
* @return void
*/
function doIdentify() {
global $mwOAuthUrl, $gUserAgent, $gConsumerKey, $gTokenKey, $gConsumerSecret;
$url = $mwOAuthUrl . '/identify';
$headerArr = array(
// OAuth information
'oauth_consumer_key' => $gConsumerKey,
'oauth_token' => $gTokenKey,
'oauth_version' => '1.0',
'oauth_nonce' => md5( microtime() . mt_rand() ),
'oauth_timestamp' => time(),
// We're using secret key signatures here.
'oauth_signature_method' => 'HMAC-SHA1',
);
$signature = sign_request( 'GET', $url, $headerArr );
$headerArr['oauth_signature'] = $signature;
$header = array();
foreach ( $headerArr as $k => $v ) {
$header[] = rawurlencode( $k ) . '="' . rawurlencode( $v ) . '"';
}
$header = 'Authorization: OAuth ' . join( ', ', $header );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( $header ) );
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, $gUserAgent );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$data = curl_exec( $ch );
if ( !$data ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'Curl error: ' . htmlspecialchars( curl_error( $ch ) );
exit(0);
}
$err = json_decode( $data );
if ( is_object( $err ) && isset( $err->error ) && $err->error === 'mwoauthdatastore-access-token-not-found' ) {
// We're not authorized!
echo 'To start using the parliament diagram tool, go <a href="archinputform.php">here</a>.';
echo '<hr>';
echo 'You haven\'t authorized this application yet! Go <a href="' . htmlspecialchars( $_SERVER['SCRIPT_NAME'] ) . '?action=authorize">here</a> to do that.';
echo '<hr>';
return;
}
// There are three fields in the response
$fields = explode( '.', $data );
if ( count( $fields ) !== 3 ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'Invalid identify response: ' . htmlspecialchars( $data );
exit(0);
}
// Validate the header. MWOAuth always returns alg "HS256".
$header = base64_decode( strtr( $fields[0], '-_', '+/' ), true );
if ( $header !== false ) {
$header = json_decode( $header );
}
if ( !is_object( $header ) || $header->typ !== 'JWT' || $header->alg !== 'HS256' ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'Invalid header in identify response: ' . htmlspecialchars( $data );
exit(0);
}
// Verify the signature
$sig = base64_decode( strtr( $fields[2], '-_', '+/' ), true );
$check = hash_hmac( 'sha256', $fields[0] . '.' . $fields[1], $gConsumerSecret, true );
if ( $sig !== $check ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'JWT signature validation failed: ' . htmlspecialchars( $data );
echo '<pre>'; var_dump( base64_encode($sig), base64_encode($check) ); echo '</pre>';
exit(0);
}
// Decode the payload
$payload = base64_decode( strtr( $fields[1], '-_', '+/' ), true );
if ( $payload !== false ) {
$payload = json_decode( $payload );
}
if ( !is_object( $payload ) ) {
header( "HTTP/1.1 $errorCode Internal Server Error" );
echo 'Invalid payload in identify response: ' . htmlspecialchars( $data );
exit(0);
}
echo "<div class='success'>";
echo 'You have authorized the parliament diagram tool to post on behalf of ' . htmlspecialchars( $payload->username ) . '. You can now close this window and click the upload link again.';
echo '<hr>';
echo 'To start using the parliament diagram tool, go <a href="archinputform.php">here</a>.';
echo "</div>\n";
}
// ******************** WEBPAGE ********************
?><p>
</p>
</body>
</html>