mediawiki-extensions-Confir.../QuestyCaptcha/QuestyCaptcha.class.php

90 lines
2.5 KiB
PHP
Raw Normal View History

2009-07-19 04:15:55 +00:00
<?php
/**
* QuestyCaptcha class
*
* @file
2009-07-19 04:15:55 +00:00
* @author Benjamin Lees <emufarmers@gmail.com>
* @ingroup Extensions
2009-07-19 04:15:55 +00:00
*/
class QuestyCaptcha extends SimpleCaptcha {
// 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
/** 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'] );
}
2009-07-19 04:15:55 +00:00
}
function addCaptchaAPI( &$resultArr ) {
2009-07-19 04:15:55 +00:00
$captcha = $this->getCaptcha();
$index = $this->storeCaptcha( $captcha );
$resultArr['captcha'] = $this->describeCaptchaType();
2009-07-19 04:15:55 +00:00
$resultArr['captcha']['id'] = $index;
$resultArr['captcha']['question'] = $captcha['question'];
}
public function describeCaptchaType() {
return [
'type' => 'question',
'mime' => 'text/plain',
];
}
2009-07-19 04:15:55 +00:00
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 ];
2009-07-19 04:15:55 +00:00
}
function getForm( OutputPage $out, $tabIndex = 1 ) {
2009-07-19 04:15:55 +00:00
$captcha = $this->getCaptcha();
if ( !$captcha ) {
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 );
return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
Html::element( 'input', [
2009-07-19 04:15:55 +00:00
'name' => 'wpCaptchaWord',
'id' => 'wpCaptchaWord',
'class' => 'mw-ui-input',
'required',
'autocomplete' => 'off',
'tabindex' => $tabIndex ] ) . // tab in before the edit textarea
2009-07-19 04:15:55 +00:00
"</p>\n" .
Xml::element( 'input', [
2009-07-19 04:15:55 +00:00
'type' => 'hidden',
'name' => 'wpCaptchaId',
'id' => 'wpCaptchaId',
'value' => $index ] );
2009-07-19 04:15:55 +00:00
}
function showHelp() {
global $wgOut;
$wgOut->setPageTitle( wfMessage( 'captchahelp-title' )->text() );
$wgOut->addWikiMsg( 'questycaptchahelp-text' );
if ( CaptchaStore::get()->cookiesNeeded() ) {
$wgOut->addWikiMsg( 'captchahelp-cookies-needed' );
2009-07-19 04:15:55 +00:00
}
}
public function getCaptchaInfo( $captchaData, $id ) {
return $captchaData['question'];
}
}