| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\ConfirmEdit\ReCaptchaNoCaptcha; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Auth\AuthenticationRequest; |
| 7 |
use MediaWiki\Extension\ConfirmEdit\Auth\CaptchaAuthenticationRequest; |
| 8 |
use MediaWiki\Extension\ConfirmEdit\Hooks; |
| 9 |
use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha; |
| 10 |
use MediaWiki\Html\Html; |
| 11 |
use MediaWiki\Json\FormatJson; |
| 12 |
use MediaWiki\Language\RawMessage; |
| 13 |
use MediaWiki\MediaWikiServices; |
| 14 |
use MediaWiki\Message\Message; |
| 15 |
use MediaWiki\Request\WebRequest; |
| 16 |
use MediaWiki\Status\Status; |
| 17 |
|
| 18 |
class ReCaptchaNoCaptcha extends SimpleCaptcha { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
protected static $messagePrefix = 'renocaptcha-'; |
| 24 |
|
| 25 |
|
| 26 |
private $error = null; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function getFormInformation( $tabIndex = 1 ) { |
| 34 |
global $wgReCaptchaSiteKey, $wgLang; |
| 35 |
$lang = htmlspecialchars( urlencode( $wgLang->getCode() ) ); |
| 36 |
|
| 37 |
$output = Html::element( 'div', [ |
| 38 |
'class' => [ |
| 39 |
'g-recaptcha', |
| 40 |
'mw-confirmedit-captcha-fail' => (bool)$this->error, |
| 41 |
], |
| 42 |
'data-sitekey' => $wgReCaptchaSiteKey |
| 43 |
] ); |
| 44 |
$htmlUrlencoded = htmlspecialchars( urlencode( $wgReCaptchaSiteKey ) ); |
| 45 |
$output .= <<<HTML |
| 46 |
<noscript> |
| 47 |
<div> |
| 48 |
<div style="width: 302px; height: 422px; position: relative;"> |
| 49 |
<div style="width: 302px; height: 422px; position: absolute;"> |
| 50 |
<iframe |
| 51 |
src="https://www.recaptcha.net/recaptcha/api/fallback?k={$htmlUrlencoded}&hl={$lang}" |
| 52 |
frameborder="0" scrolling="no" |
| 53 |
style="width: 302px; height:422px; border-style: none;"> |
| 54 |
</iframe> |
| 55 |
</div> |
| 56 |
</div> |
| 57 |
<div style="width: 300px; height: 60px; border-style: none; |
| 58 |
bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; |
| 59 |
background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;"> |
| 60 |
<textarea id="g-recaptcha-response" name="g-recaptcha-response" |
| 61 |
class="g-recaptcha-response" |
| 62 |
style="width: 250px; height: 40px; border: 1px solid #c1c1c1; |
| 63 |
margin: 10px 25px; padding: 0px; resize: none;" > |
| 64 |
</textarea> |
| 65 |
</div> |
| 66 |
</div> |
| 67 |
</noscript> |
| 68 |
HTML; |
| 69 |
return [ |
| 70 |
'html' => $output, |
| 71 |
'headitems' => [ |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
"<script src=\"https://www.recaptcha.net/recaptcha/api.js?hl={$lang}\" async defer></script>" |
| 76 |
] |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public static function getCSPUrls() { |
| 84 |
return [ 'https://www.recaptcha.net/recaptcha/api.js' ]; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
protected function logCheckError( $info ) { |
| 91 |
if ( $info instanceof Status ) { |
| 92 |
$errors = $info->getErrorsArray(); |
| 93 |
$error = $errors[0][0]; |
| 94 |
} elseif ( is_array( $info ) ) { |
| 95 |
$error = implode( ',', $info ); |
| 96 |
} else { |
| 97 |
$error = $info; |
| 98 |
} |
| 99 |
|
| 100 |
wfDebugLog( 'captcha', 'Unable to validate response: ' . $error ); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
protected function getCaptchaParamsFromRequest( WebRequest $request ) { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
$index = 'not used'; |
| 112 |
$response = $request->getVal( |
| 113 |
'g-recaptcha-response', |
| 114 |
$request->getVal( 'captchaWord', $request->getVal( 'captchaword' ) ) |
| 115 |
); |
| 116 |
return [ $index, $response ]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
protected function passCaptcha( $_, $word ) { |
| 130 |
global $wgRequest, $wgReCaptchaSecretKey, $wgReCaptchaSendRemoteIP; |
| 131 |
|
| 132 |
$url = 'https://www.recaptcha.net/recaptcha/api/siteverify'; |
| 133 |
|
| 134 |
$data = [ |
| 135 |
'secret' => $wgReCaptchaSecretKey, |
| 136 |
'response' => $word, |
| 137 |
]; |
| 138 |
if ( $wgReCaptchaSendRemoteIP ) { |
| 139 |
$data['remoteip'] = $wgRequest->getIP(); |
| 140 |
} |
| 141 |
$url = wfAppendQuery( $url, $data ); |
| 142 |
$request = MediaWikiServices::getInstance()->getHttpRequestFactory() |
| 143 |
->create( $url, [ 'method' => 'POST' ], __METHOD__ ); |
| 144 |
$status = $request->execute(); |
| 145 |
if ( !$status->isOK() ) { |
| 146 |
$this->error = 'http'; |
| 147 |
$this->logCheckError( $status ); |
| 148 |
return false; |
| 149 |
} |
| 150 |
$response = FormatJson::decode( $request->getContent(), true ); |
| 151 |
if ( !$response ) { |
| 152 |
$this->error = 'json'; |
| 153 |
$this->logCheckError( $this->error ); |
| 154 |
return false; |
| 155 |
} |
| 156 |
if ( isset( $response['error-codes'] ) ) { |
| 157 |
$this->error = 'recaptcha-api'; |
| 158 |
$this->logCheckError( $response['error-codes'] ); |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
return $response['success']; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
protected function addCaptchaAPI( &$resultArr ) { |
| 169 |
$resultArr['captcha'] = $this->describeCaptchaType(); |
| 170 |
$resultArr['captcha']['error'] = $this->error; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function describeCaptchaType() { |
| 177 |
global $wgReCaptchaSiteKey; |
| 178 |
return [ |
| 179 |
'type' => 'recaptchanocaptcha', |
| 180 |
'mime' => 'application/javascript', |
| 181 |
'key' => $wgReCaptchaSiteKey, |
| 182 |
]; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public function getMessage( $action ) { |
| 193 |
$msg = parent::getMessage( $action ); |
| 194 |
if ( $this->error ) { |
| 195 |
$msg = new RawMessage( '<div class="error">$1</div>', [ $msg ] ); |
| 196 |
} |
| 197 |
return $msg; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function apiGetAllowedParams( ApiBase $module, &$params, $flags ) { |
| 207 |
if ( $flags && $this->isAPICaptchaModule( $module ) ) { |
| 208 |
$params['g-recaptcha-response'] = [ |
| 209 |
ApiBase::PARAM_HELP_MSG => 'renocaptcha-apihelp-param-g-recaptcha-response', |
| 210 |
]; |
| 211 |
} |
| 212 |
|
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function getError() { |
| 220 |
return $this->error; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function storeCaptcha( $info ) { |
| 227 |
|
| 228 |
|
| 229 |
return 'not used'; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public function retrieveCaptcha( $index ) { |
| 236 |
|
| 237 |
return [ 'index' => $index ]; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function getCaptcha() { |
| 244 |
|
| 245 |
return []; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public function createAuthenticationRequest() { |
| 252 |
return new ReCaptchaNoCaptchaAuthenticationRequest(); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function onAuthChangeFormFields( |
| 262 |
array $requests, array $fieldInfo, array &$formDescriptor, $action |
| 263 |
) { |
| 264 |
global $wgReCaptchaSiteKey; |
| 265 |
|
| 266 |
$req = AuthenticationRequest::getRequestByClass( $requests, |
| 267 |
CaptchaAuthenticationRequest::class, true ); |
| 268 |
if ( !$req ) { |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
$captcha = Hooks::getInstance(); |
| 274 |
|
| 275 |
$formDescriptor['captchaWord'] = [ |
| 276 |
'class' => HTMLReCaptchaNoCaptchaField::class, |
| 277 |
'key' => $wgReCaptchaSiteKey, |
| 278 |
'error' => $captcha->getError(), |
| 279 |
] + $formDescriptor['captchaWord']; |
| 280 |
} |
| 281 |
} |