forked from imbushuo/MmixCaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMmixCaptcha.class.php
More file actions
219 lines (187 loc) · 7.74 KB
/
Copy pathMmixCaptcha.class.php
File metadata and controls
219 lines (187 loc) · 7.74 KB
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
<?php
/**
* MmixCaptcha class
*
* @file
* @author The Little Moe New LLC
* @author Bingxing Wang <ben@lmn.cat>
* @ingroup Extensions
*/
use MediaWiki\Auth\AuthenticationRequest;
use \MmixCaptcha\Connector;
include_once "MmixCaptcha.DataObjects.php";
include_once "MmixCaptcha.Connector.php";
class MmixCaptcha extends SimpleCaptcha {
// used for mmixcaptcha-edit, mmixcaptcha-addurl, mmixcaptcha-badlogin,
// mmixcaptcha-createaccount, mmixcaptcha-create, mmixcaptcha-sendemail via getMessage()
protected static $messagePrefix = 'mmixcaptcha-';
/**
* Given a required captcha run, test form input for correct
* input on the open session.
* @param string $index Captcha idenitifier
* @param string $word Captcha solution
* @return bool if passed, false if failed or new session
*/
protected function passCaptcha( $index, $word ) {
if ( !$index || $index == "") {
return false;
}
if ( !$word || $word == "") {
return false;
}
// API and Mobile will fire twice. Each key can be used twice.
global $wgRequest;
$pCounterData = $wgRequest->getSessionData("$index.$word");
if ( $pCounterData == 1 ) {
$wgRequest->setSessionData("$index.$word", null);
return true;
}
// Validate with backend.
$result = Connector::validateChallenge( $index, $word );
if ( $result ) {
// Save + counter
$wgRequest->setSessionData("$index.$word", 1);
}
return $result;
}
function addCaptchaAPI( &$resultArr ) {
$captcha = Connector::getChallenge();
global $wgMmixBackendEndpoint;
if ($captcha) {
$resultArr['captcha'] = $this->describeCaptchaType();
$resultArr['captcha']['id'] = $captcha->id;
$resultArr['captcha']['captchaId'] = $captcha->id;
$resultArr['captcha']['questionId'] = $captcha->id;
$imageIdEncoded = urlencode($captcha->id);
$description = wfMessage( 'mmixcaptcha-edit-mobile' )->parse();
$imageUrl = "https://{$wgMmixBackendEndpoint}/image/Retrieval?id={$imageIdEncoded}";
$resultArr['captcha']['question'] = "<p>{$description}</p><img style=\"display: block;\" src=\"{$imageUrl}\" alt=\"MMIX Captcha\" />";
}
}
public function describeCaptchaType() {
return [
'type' => 'question',
'mime' => 'text/html',
];
}
function generateForm() {
global $wgMmixBackendEndpoint, $wgUseResourceManager, $wgClientResourceIdentifier;
global $wgOut;
$out = $wgOut;
if (!isset($wgClientResourceIdentifier))
{
$wgClientResourceIdentifier = "";
}
if ($wgUseResourceManager)
{
$out->addModules("ext.mmixCaptcha");
$out->addModuleStyles("ext.mmixCaptcha");
}
else
{
$out->addHeadItem(
'mmix-coremodule',
"<script type=\"text/javascript\" src=\"//mmixstaticassets.azureedge.net/{$wgClientResourceIdentifier}/mmix.min.js\" async defer></script>"
);
$out->addHeadItem(
'mmix-corestylesheet',
"<link rel=\"stylesheet\" href=\"//mmixstaticassets.azureedge.net/{$wgClientResourceIdentifier}/mmix.min.css\" />"
);
}
$errorTitle = wfMessage( 'mmixcaptcha-error' )->text();
$errorContent = wfMessage( 'mmixcaptcha-retry' )->text();
$placeHolder = wfMessage( 'mmixcaptcha-textplaceholder' )->text();
$description = wfMessage( 'mmixcaptcha-edit' )->parse();
$content = <<<HTML
<div class="mmix-host" data-endpoint="{$wgMmixBackendEndpoint}">
<img id="mmix-global-captcha-progress-ring" src="//mmixstaticassets.azureedge.net/ProgressRing.gif" width="50" />
<div class="mmix-captcha-container" id="mmix-captcha-container-1">
<input id="wpCaptchaId" name="wpCaptchaId" type="hidden" value="" >
<input id="telemetryCorrelationId" name="telemetryCorrelationId" type="hidden" value="" >
<input id="telemetryInstanceId" name="telemetryInstanceId" type="hidden" value="">
<div class="mmix-captcha-ui-container" id="mmix-captcha-ui-container-1">
<div id="mmix-captcha-ui-image-container" class="mmix-captcha-ui-image-container">
<img id="mmix-captcha-progress-ring" src="//mmixstaticassets.azureedge.net/ProgressRing.gif" width="50" />
<img id="mmix-captcha-ui-image-control" />
<div id="mmix-captcha-ui-error-control" class="mmix-captcha-ui-fail-error-container" style="display: none">
<img class="mmix-captcha-ui-fail-error-image" src="//mmixstaticassets.azureedge.net/ErrorIcon.png">
<div class="mmix-captcha-ui-fail-error-text-container">
<h2 class="error-title">{$errorTitle}</h2>
<p id="mmixErrorDetailed" class="error-message">{$errorContent}</p>
<p id="mmixErrorRefA" class="error-message no-margin"></p>
<p id="mmixErrorRefB" class="error-message no-margin"></p>
<p id="mmixErrorRefC" class="error-message no-margin"></p>
</div>
</div>
</div>
<div class="mmix-captcha-ui-control-container">
<div class="mmix-captcha-ui-control-information-text-container">
<div class="mmix-captcha-ui-control-information-text">
{$description}
</div>
<input class="mmix-captcha-ui-control-input" autocomplete="off" name="wpCaptchaWord" type="text" placeholder="{$placeHolder}">
</div>
</div>
</div>
</div>
</div>
HTML;
return $content;
}
function getForm( OutputPage $out, $tabIndex = 1 ) {
return $this->generateForm();
}
function getFormInformation( $tabIndex = 1 ) {
return [
'html' => $this->generateForm()
];
}
protected function getCaptchaParamsFromRequest( WebRequest $request ) {
$index = $request->getVal( 'wpCaptchaId', $request->getVal( 'captchaId' ) );
$response = $request->getVal( 'wpCaptchaWord', $request->getVal( 'captchaWord' ) );
return [ $index, $response ];
}
public function storeCaptcha( $info ) {
return 'not used';
}
function showHelp() {
global $wgOut;
$wgOut->setPageTitle( wfMessage( 'captchahelp-title' )->text() );
$wgOut->addWikiMsg( 'mmixcaptchahelp-text' );
if ( CaptchaStore::get()->cookiesNeeded() ) {
$wgOut->addWikiMsg( 'captchahelp-cookies-needed' );
}
}
public function retrieveCaptcha( $index ) {
return [ 'index' => $index ];
}
public function getCaptcha() {
return [];
}
public function getCaptchaInfo( $captchaData, $id ) {
return wfMessage( 'mmixcaptcha-info' );
}
public function onAuthChangeFormFields( array $requests, array $fieldInfo,
array &$formDescriptor, $action ) {
/** @var CaptchaAuthenticationRequest $req */
$req =
AuthenticationRequest::getRequestByClass( $requests,
CaptchaAuthenticationRequest::class, true );
if ( !$req ) {
return;
}
// ugly way to retrieve error information
$captcha = ConfirmEditHooks::getInstance();
// Inject widget and ID
$formDescriptor['captchaId'] = [
'class' => HTMLMmixCaptchaField::class,
'error' => $captcha->getError(),
'label-message' => null
] + $formDescriptor['captchaId'];
$formDescriptor['captchaInfo']['label-message'] = null;
$formDescriptor['captchaWord'] = [
'class' => HTMLMmixCaptchaWidgetField::class,
'label-message' => null
];
}
}