forked from imbushuo/MmixCaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMmixCaptcha.Connector.php
73 lines (59 loc) · 2.33 KB
/
MmixCaptcha.Connector.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
<?php
/**
* MmixCaptcha connector class
*
* @file
* @author The Little Moe New LLC
* @author Bingxing Wang <[email protected]>
* @ingroup Extensions
*/
namespace MmixCaptcha;
include_once "MmixCaptcha.DataObjects.php";
class Connector {
public static function getChallenge() {
global $wgMmixBackendEndpoint;
$reqUrl = "https://{$wgMmixBackendEndpoint}/questionEntry/BeginChallenge";
$curl = curl_init($reqUrl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT_MS, 1500);
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
$result = curl_exec($curl);
$resultCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $resultCode != 200 ) {
$err = curl_error($curl);
wfDebug ( "[MMIX] Unexpected Error caught: $resultCode $err. ");
curl_close($curl);
return false;
} else {
// Decode entity
$e = json_decode ( $result );
curl_close($curl);
return new \MmixCaptcha\DataObjects\ChallengeMetadata(
$e->{"backendId"},
$e->{"id"},
$e->{"path"});
}
}
public static function validateChallenge( $id, $content ) {
global $wgMmixBackendEndpoint;
$reqUrl = "https://{$wgMmixBackendEndpoint}/questionEntry/PostResponse";
$rep = new \MmixCaptcha\DataObjects\ChallengeResponse( $id, $content );
$serializedRep = json_encode( $rep );
$curl = curl_init($reqUrl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $serializedRep);
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($curl, CURLOPT_TIMEOUT_MS, 1500);
$result = curl_exec($curl);
$resultCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($resultCode != 200) {
$err = curl_error($curl);
wfDebug ( "[MMIX] Unexpected Error caught: $err ($resultCode). ");
}
curl_close($curl);
return $resultCode == 200;
}
}