2020-04-10 22:21:08 +00:00
|
|
|
<?php
|
|
|
|
|
2022-03-05 15:05:22 +00:00
|
|
|
namespace MediaWiki\Extension\ConfirmEdit\hCaptcha;
|
2020-04-10 22:21:08 +00:00
|
|
|
|
2023-12-10 23:07:55 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2024-06-08 21:46:45 +00:00
|
|
|
use MediaWiki\HTMLForm\HTMLFormField;
|
2020-04-10 22:21:08 +00:00
|
|
|
|
|
|
|
class HTMLHCaptchaField extends HTMLFormField {
|
|
|
|
/** @var string Public key parameter to be passed to hCaptcha. */
|
|
|
|
protected $key;
|
|
|
|
|
|
|
|
/** @var string Error returned by hCaptcha in the previous round. */
|
|
|
|
protected $error;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameters:
|
|
|
|
* - key: (string, required) Public key
|
|
|
|
* - error: (string) Error from previous round
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function __construct( array $params ) {
|
|
|
|
$params += [ 'error' => null ];
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
|
|
|
$this->key = $params['key'];
|
|
|
|
$this->error = $params['error'];
|
|
|
|
|
|
|
|
$this->mName = 'h-captcha-response';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getInputHTML( $value ) {
|
|
|
|
$out = $this->mParent->getOutput();
|
|
|
|
|
|
|
|
$out->addHeadItem(
|
|
|
|
'h-captcha',
|
|
|
|
"<script src=\"https://hcaptcha.com/1/api.js\" async defer></script>"
|
|
|
|
);
|
2020-04-18 09:48:07 +00:00
|
|
|
HCaptcha::addCSPSources( $out->getCSP() );
|
2020-04-10 22:21:08 +00:00
|
|
|
return Html::element( 'div', [
|
|
|
|
'class' => [
|
|
|
|
'h-captcha',
|
|
|
|
'mw-confirmedit-captcha-fail' => (bool)$this->error,
|
|
|
|
],
|
|
|
|
'data-sitekey' => $this->key,
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
}
|