mediawiki-extensions-Confir.../QuestyCaptcha/QuestyCaptcha.class.php
Florian e566dcb966 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-08-04 01:45:13 +00:00

92 lines
2.5 KiB
PHP

<?php
/**
* QuestyCaptcha class
*
* @file
* @author Benjamin Lees <emufarmers@gmail.com>
* @ingroup Extensions
*/
class QuestyCaptcha extends SimpleCaptcha {
// used for questycaptcha-edit, questycaptcha-addurl, questycaptcha-badlogin,
// questycaptcha-createaccount, questycaptcha-create, questycaptcha-sendemail via getMessage()
protected static $messagePrefix = 'questycaptcha-';
/** Validate a captcha response */
function keyMatch( $answer, $info ) {
if ( is_array( $info['answer'] ) ) {
return in_array( strtolower( $answer ), array_map( 'strtolower', $info['answer'] ) );
} else {
return strtolower( $answer ) == strtolower( $info['answer'] );
}
}
function addCaptchaAPI( &$resultArr ) {
$captcha = $this->getCaptcha();
$index = $this->storeCaptcha( $captcha );
$resultArr['captcha'] = $this->describeCaptchaType();
$resultArr['captcha']['id'] = $index;
$resultArr['captcha']['question'] = $captcha['question'];
}
public function describeCaptchaType() {
return [
'type' => 'question',
'mime' => 'text/plain',
];
}
function getCaptcha() {
global $wgCaptchaQuestions;
// Backwards compatibility
if ( $wgCaptchaQuestions === array_values( $wgCaptchaQuestions ) ) {
return $wgCaptchaQuestions[ mt_rand( 0, count( $wgCaptchaQuestions ) - 1 ) ];
}
$question = array_rand( $wgCaptchaQuestions, 1 );
$answer = $wgCaptchaQuestions[ $question ];
return [ 'question' => $question, 'answer' => $answer ];
}
function getFormInformation( $tabIndex = 1 ) {
$captcha = $this->getCaptcha();
if ( !$captcha ) {
die(
"No questions found; set some in LocalSettings.php using the format from QuestyCaptcha.php."
);
}
$index = $this->storeCaptcha( $captcha );
return [
'html' => "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
Html::element( 'input', [
'name' => 'wpCaptchaWord',
'id' => 'wpCaptchaWord',
'class' => 'mw-ui-input',
'required',
'autocomplete' => 'off',
'tabindex' => $tabIndex ] ) . // tab in before the edit textarea
"</p>\n" .
Xml::element( 'input', [
'type' => 'hidden',
'name' => 'wpCaptchaId',
'id' => 'wpCaptchaId',
'value' => $index ] )
];
}
function showHelp() {
global $wgOut;
$wgOut->setPageTitle( wfMessage( 'captchahelp-title' )->text() );
$wgOut->addWikiMsg( 'questycaptchahelp-text' );
if ( CaptchaStore::get()->cookiesNeeded() ) {
$wgOut->addWikiMsg( 'captchahelp-cookies-needed' );
}
}
public function getCaptchaInfo( $captchaData, $id ) {
return $captchaData['question'];
}
}