mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/ConfirmEdit
synced 2024-12-01 03:06:30 +00:00
e566dcb966
This commit removes SimpleCaptcha::getForm() and replaces it by its more informative counterpart getFormInformation(), which returns an array, which provides some more information about the form than only the html. The information included in the array is: * html: The HTML of the CAPTCHA form (this is the same as what you expected from getForm() previously) * modules: ResourceLoader modules, if any, that should be added to the output of the page * modulestyles: ResourceLoader style modules, if any, that should be added to th output of the page * headitems: Head items that should be added to the output (see OutputPage::addHeadItems) Mostly you shouldn't need to handle the response of getFormInformation() anymore, as there's a new function, addFormToOutput(), which takes an instance of OutputPage as a first parameter and handles the response of getFormInformation for you (adds all information to the given OutputPage instance, if they're provided). Bug: T141300 Depends-On: I433afd124b57526caa13a540cda48ba2b99a9bde Change-Id: I25f344538052fc18993c43185fbd97804a7cfc81
89 lines
2.4 KiB
PHP
89 lines
2.4 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Auth\AuthenticationRequest;
|
|
|
|
class MathCaptcha extends SimpleCaptcha {
|
|
|
|
/** Validate a captcha response */
|
|
function keyMatch( $answer, $info ) {
|
|
return (int)$answer == (int)$info['answer'];
|
|
}
|
|
|
|
function addCaptchaAPI( &$resultArr ) {
|
|
list( $sum, $answer ) = $this->pickSum();
|
|
$html = $this->fetchMath( $sum );
|
|
$index = $this->storeCaptcha( [ 'answer' => $answer ] );
|
|
$resultArr['captcha'] = $this->describeCaptchaType();
|
|
$resultArr['captcha']['id'] = $index;
|
|
$resultArr['captcha']['question'] = $html;
|
|
}
|
|
|
|
public function describeCaptchaType() {
|
|
return [
|
|
'type' => 'math',
|
|
'mime' => 'text/html',
|
|
];
|
|
}
|
|
|
|
function getFormInformation( $tabIndex = 1 ) {
|
|
list( $sum, $answer ) = $this->pickSum();
|
|
$index = $this->storeCaptcha( [ 'answer' => $answer ] );
|
|
|
|
$form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
|
|
$form .= '<td>' . Html::input( 'wpCaptchaWord', false, false, [
|
|
'tabindex' => $tabIndex,
|
|
'autocomplete' => 'off',
|
|
'required'
|
|
] ) . '</td></tr></table>';
|
|
$form .= Html::hidden( 'wpCaptchaId', $index );
|
|
return [ 'html' => $form ];
|
|
}
|
|
|
|
/** Pick a random sum */
|
|
function pickSum() {
|
|
$a = mt_rand( 0, 100 );
|
|
$b = mt_rand( 0, 10 );
|
|
$op = mt_rand( 0, 1 ) ? '+' : '-';
|
|
$sum = "{$a} {$op} {$b} = ";
|
|
$ans = $op == '+' ? ( $a + $b ) : ( $a - $b );
|
|
return [ $sum, $ans ];
|
|
}
|
|
|
|
/** Fetch the math */
|
|
function fetchMath( $sum ) {
|
|
if ( class_exists( 'MathRenderer' ) ) {
|
|
$math = MathRenderer::getRenderer( $sum, [], 'png' );
|
|
} else {
|
|
throw new LogicException(
|
|
'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
|
|
}
|
|
$math->render();
|
|
$html = $math->getHtmlOutput();
|
|
return preg_replace( '/alt=".*?"/', '', $html );
|
|
}
|
|
|
|
public function getCaptcha() {
|
|
list( $sum, $answer ) = $this->pickSum();
|
|
return [ 'question' => $sum, 'answer' => $answer ];
|
|
}
|
|
|
|
public function getCaptchaInfo( $captchaData, $id ) {
|
|
$sum = $captchaData['question'];
|
|
return $this->fetchMath( $sum );
|
|
}
|
|
|
|
public function onAuthChangeFormFields( array $requests, array $fieldInfo,
|
|
array &$formDescriptor, $action ) {
|
|
/** @var CaptchaAuthenticationRequest $req */
|
|
$req =
|
|
AuthenticationRequest::getRequestByClass( $requests,
|
|
CaptchaAuthenticationRequest::class, true );
|
|
if ( !$req ) {
|
|
return;
|
|
}
|
|
|
|
$formDescriptor['captchaInfo']['raw'] = true;
|
|
$formDescriptor['captchaWord']['label-message'] = null;
|
|
}
|
|
}
|