2009-07-19 04:15:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* QuestyCaptcha class
|
|
|
|
*
|
2010-06-06 15:12:22 +00:00
|
|
|
* @file
|
2009-07-19 04:15:55 +00:00
|
|
|
* @author Benjamin Lees <emufarmers@gmail.com>
|
2010-06-06 15:12:22 +00:00
|
|
|
* @ingroup Extensions
|
2009-07-19 04:15:55 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-05 08:07:54 +00:00
|
|
|
use MediaWiki\Auth\AuthenticationRequest;
|
|
|
|
|
2009-07-19 04:15:55 +00:00
|
|
|
class QuestyCaptcha extends SimpleCaptcha {
|
2016-04-25 20:58:18 +00:00
|
|
|
// used for questycaptcha-edit, questycaptcha-addurl, questycaptcha-badlogin,
|
|
|
|
// questycaptcha-createaccount, questycaptcha-create, questycaptcha-sendemail via getMessage()
|
|
|
|
protected static $messagePrefix = 'questycaptcha-';
|
2009-07-19 04:15:55 +00:00
|
|
|
|
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 ) {
|
2011-02-08 23:39:17 +00:00
|
|
|
if ( is_array( $info['answer'] ) ) {
|
2012-11-13 00:21:54 +00:00
|
|
|
return in_array( strtolower( $answer ), array_map( 'strtolower', $info['answer'] ) );
|
2011-02-08 23:39:17 +00:00
|
|
|
} else {
|
|
|
|
return strtolower( $answer ) == strtolower( $info['answer'] );
|
|
|
|
}
|
2009-07-19 04:15:55 +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 ) {
|
2009-07-19 04:15:55 +00:00
|
|
|
$captcha = $this->getCaptcha();
|
|
|
|
$index = $this->storeCaptcha( $captcha );
|
2016-04-25 20:58:18 +00:00
|
|
|
$resultArr['captcha'] = $this->describeCaptchaType();
|
2009-07-19 04:15:55 +00:00
|
|
|
$resultArr['captcha']['id'] = $index;
|
|
|
|
$resultArr['captcha']['question'] = $captcha['question'];
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2017-02-17 13:24:49 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-25 20:58:18 +00:00
|
|
|
public function describeCaptchaType() {
|
|
|
|
return [
|
|
|
|
'type' => 'question',
|
2016-10-05 08:07:54 +00:00
|
|
|
'mime' => 'text/html',
|
2016-04-25 20:58:18 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-02-17 13:24:49 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-06-15 21:08:14 +00:00
|
|
|
public function getCaptcha() {
|
2009-07-19 04:15:55 +00:00
|
|
|
global $wgCaptchaQuestions;
|
2014-03-27 00:33:55 +00:00
|
|
|
|
2015-10-28 15:52:04 +00:00
|
|
|
// Backwards compatibility
|
2014-03-27 00:33:55 +00:00
|
|
|
if ( $wgCaptchaQuestions === array_values( $wgCaptchaQuestions ) ) {
|
|
|
|
return $wgCaptchaQuestions[ mt_rand( 0, count( $wgCaptchaQuestions ) - 1 ) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
$question = array_rand( $wgCaptchaQuestions, 1 );
|
|
|
|
$answer = $wgCaptchaQuestions[ $question ];
|
2016-05-09 23:41:17 +00:00
|
|
|
return [ 'question' => $question, 'answer' => $answer ];
|
2009-07-19 04:15:55 +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 ) {
|
2009-07-19 04:15:55 +00:00
|
|
|
$captcha = $this->getCaptcha();
|
2009-07-19 15:13:01 +00:00
|
|
|
if ( !$captcha ) {
|
2015-10-28 15:52:04 +00:00
|
|
|
die(
|
|
|
|
"No questions found; set some in LocalSettings.php using the format from QuestyCaptcha.php."
|
|
|
|
);
|
2009-07-19 04:15:55 +00:00
|
|
|
}
|
|
|
|
$index = $this->storeCaptcha( $captcha );
|
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' => "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
|
|
|
|
Html::element( 'input', [
|
|
|
|
'name' => 'wpCaptchaWord',
|
|
|
|
'id' => 'wpCaptchaWord',
|
|
|
|
'class' => 'mw-ui-input',
|
|
|
|
'required',
|
|
|
|
'autocomplete' => 'off',
|
2018-06-15 21:08:14 +00:00
|
|
|
// tab in before the edit textarea
|
|
|
|
'tabindex' => $tabIndex ] ) .
|
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
|
|
|
"</p>\n" .
|
|
|
|
Xml::element( 'input', [
|
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'wpCaptchaId',
|
|
|
|
'id' => 'wpCaptchaId',
|
|
|
|
'value' => $index ] )
|
|
|
|
];
|
2009-07-19 04:15:55 +00:00
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2018-06-15 21:08:14 +00:00
|
|
|
public function showHelp() {
|
2015-04-02 22:56:48 +00:00
|
|
|
global $wgOut;
|
|
|
|
$wgOut->setPageTitle( wfMessage( 'captchahelp-title' )->text() );
|
|
|
|
$wgOut->addWikiMsg( 'questycaptchahelp-text' );
|
2011-12-01 16:24:49 +00:00
|
|
|
if ( CaptchaStore::get()->cookiesNeeded() ) {
|
2015-04-02 22:56:48 +00:00
|
|
|
$wgOut->addWikiMsg( 'captchahelp-cookies-needed' );
|
2009-07-19 04:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-25 20:58:18 +00:00
|
|
|
|
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 ) {
|
|
|
|
return $captchaData['question'];
|
|
|
|
}
|
2016-10-05 08:07:54 +00:00
|
|
|
|
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-10-05 08:07:54 +00:00
|
|
|
public function onAuthChangeFormFields( array $requests, array $fieldInfo,
|
|
|
|
array &$formDescriptor, $action ) {
|
|
|
|
/** @var CaptchaAuthenticationRequest $req */
|
|
|
|
$req =
|
|
|
|
AuthenticationRequest::getRequestByClass( $requests,
|
|
|
|
CaptchaAuthenticationRequest::class, true );
|
|
|
|
if ( !$req ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// declare RAW HTML output.
|
|
|
|
$formDescriptor['captchaInfo']['raw'] = true;
|
|
|
|
$formDescriptor['captchaWord']['label-message'] = null;
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
}
|