2007-11-12 07:42:25 +00:00
|
|
|
<?php
|
|
|
|
|
2016-04-25 20:58:18 +00:00
|
|
|
use MediaWiki\Auth\AuthenticationRequest;
|
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
class MathCaptcha extends SimpleCaptcha {
|
|
|
|
|
2017-02-17 13:24:49 +00:00
|
|
|
/**
|
|
|
|
* Validate a captcha response
|
|
|
|
* @param string $answer
|
|
|
|
* @param array $info
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-06-15 21:08:14 +00:00
|
|
|
protected function keyMatch( $answer, $info ) {
|
2008-01-24 21:42:21 +00:00
|
|
|
return (int)$answer == (int)$info['answer'];
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2008-02-28 17:42:23 +00:00
|
|
|
|
2017-02-17 13:24:49 +00:00
|
|
|
/**
|
2017-09-09 18:10:12 +00:00
|
|
|
* @param array &$resultArr
|
2017-02-17 13:24:49 +00:00
|
|
|
*/
|
2018-06-15 21:08:14 +00:00
|
|
|
protected function addCaptchaAPI( &$resultArr ) {
|
2008-02-28 17:42:23 +00:00
|
|
|
list( $sum, $answer ) = $this->pickSum();
|
2016-04-25 20:58:18 +00:00
|
|
|
$html = $this->fetchMath( $sum );
|
2016-05-09 23:41:17 +00:00
|
|
|
$index = $this->storeCaptcha( [ 'answer' => $answer ] );
|
2016-04-25 20:58:18 +00:00
|
|
|
$resultArr['captcha'] = $this->describeCaptchaType();
|
2008-02-28 17:42:23 +00:00
|
|
|
$resultArr['captcha']['id'] = $index;
|
2016-04-25 20:58:18 +00:00
|
|
|
$resultArr['captcha']['question'] = $html;
|
|
|
|
}
|
|
|
|
|
2017-02-17 13:24:49 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-25 20:58:18 +00:00
|
|
|
public function describeCaptchaType() {
|
|
|
|
return [
|
|
|
|
'type' => 'math',
|
|
|
|
'mime' => 'text/html',
|
|
|
|
];
|
2008-02-28 17:42:23 +00:00
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2017-02-17 13:24:49 +00:00
|
|
|
/**
|
|
|
|
* @param int $tabIndex
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-06-15 21:08:14 +00:00
|
|
|
public function getFormInformation( $tabIndex = 1 ) {
|
2007-11-12 07:42:25 +00:00
|
|
|
list( $sum, $answer ) = $this->pickSum();
|
2016-05-09 23:41:17 +00:00
|
|
|
$index = $this->storeCaptcha( [ 'answer' => $answer ] );
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2007-11-12 07:42:25 +00:00
|
|
|
$form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
|
2019-12-21 02:46:59 +00:00
|
|
|
$form .= '<td>' . Html::input( 'wpCaptchaWord', '', '', [
|
2016-04-25 20:58:18 +00:00
|
|
|
'tabindex' => $tabIndex,
|
|
|
|
'autocomplete' => 'off',
|
|
|
|
'required'
|
|
|
|
] ) . '</td></tr></table>';
|
2010-10-29 15:14:44 +00:00
|
|
|
$form .= Html::hidden( 'wpCaptchaId', $index );
|
Remove getForm() and replace by getFormInformation()
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
2016-07-26 16:08:42 +00:00
|
|
|
return [ 'html' => $form ];
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2017-09-09 18:10:12 +00:00
|
|
|
/**
|
|
|
|
* Pick a random sum
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-06-15 21:08:14 +00:00
|
|
|
private function pickSum() {
|
2007-11-12 07:42:25 +00:00
|
|
|
$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 );
|
2016-05-09 23:41:17 +00:00
|
|
|
return [ $sum, $ans ];
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2017-09-09 18:10:12 +00:00
|
|
|
/**
|
|
|
|
* Fetch the math
|
2019-12-21 02:46:59 +00:00
|
|
|
* @param string $sum
|
2017-09-09 18:10:12 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-06-15 21:08:14 +00:00
|
|
|
private function fetchMath( $sum ) {
|
2019-03-02 19:21:48 +00:00
|
|
|
$math = MathRenderer::getRenderer( $sum, [], 'png' );
|
2016-04-25 20:58:18 +00:00
|
|
|
$math->render();
|
|
|
|
$html = $math->getHtmlOutput();
|
2008-12-20 02:12:36 +00:00
|
|
|
return preg_replace( '/alt=".*?"/', '', $html );
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|
2016-04-25 20:58:18 +00:00
|
|
|
|
2017-02-17 13:24:49 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-25 20:58:18 +00:00
|
|
|
public function getCaptcha() {
|
|
|
|
list( $sum, $answer ) = $this->pickSum();
|
|
|
|
return [ 'question' => $sum, 'answer' => $answer ];
|
|
|
|
}
|
|
|
|
|
2017-02-17 13:24:49 +00:00
|
|
|
/**
|
|
|
|
* @param array $captchaData
|
|
|
|
* @param string $id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-04-25 20:58:18 +00:00
|
|
|
public function getCaptchaInfo( $captchaData, $id ) {
|
|
|
|
$sum = $captchaData['question'];
|
|
|
|
return $this->fetchMath( $sum );
|
|
|
|
}
|
|
|
|
|
2017-02-17 13:24:49 +00:00
|
|
|
/**
|
|
|
|
* @param array $requests
|
|
|
|
* @param array $fieldInfo
|
2017-09-09 18:10:12 +00:00
|
|
|
* @param array &$formDescriptor
|
2017-02-17 13:24:49 +00:00
|
|
|
* @param string $action
|
|
|
|
*/
|
2016-04-25 20:58:18 +00:00
|
|
|
public function onAuthChangeFormFields( array $requests, array $fieldInfo,
|
|
|
|
array &$formDescriptor, $action ) {
|
|
|
|
/** @var CaptchaAuthenticationRequest $req */
|
2017-02-17 13:24:49 +00:00
|
|
|
$req = AuthenticationRequest::getRequestByClass(
|
|
|
|
$requests,
|
|
|
|
CaptchaAuthenticationRequest::class,
|
2020-04-11 01:27:12 +00:00
|
|
|
true
|
2017-02-17 13:24:49 +00:00
|
|
|
);
|
2016-04-25 20:58:18 +00:00
|
|
|
if ( !$req ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$formDescriptor['captchaInfo']['raw'] = true;
|
|
|
|
$formDescriptor['captchaWord']['label-message'] = null;
|
|
|
|
}
|
2007-11-12 07:42:25 +00:00
|
|
|
}
|