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

75 lines
2.1 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 {
/** Validate a captcha response */
function keyMatch( $answer, $info ) {
if ( is_array( $info['answer'] ) ) {
return in_array( strtolower( $answer ), $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']['type'] = 'question';
$resultArr['captcha']['mime'] = 'text/plain';
$resultArr['captcha']['id'] = $index;
$resultArr['captcha']['question'] = $captcha['question'];
}
2009-07-19 04:15:55 +00:00
function getCaptcha() {
global $wgCaptchaQuestions;
return $wgCaptchaQuestions[mt_rand( 0, count( $wgCaptchaQuestions ) - 1 )]; // pick a question, any question
2009-07-19 04:15:55 +00:00
}
2009-07-19 04:15:55 +00:00
function getForm() {
$captcha = $this->getCaptcha();
if ( !$captcha ) {
2009-07-19 04:15:55 +00:00
die( "No questions found; set some in LocalSettings.php using the format from QuestyCaptcha.php." );
}
$index = $this->storeCaptcha( $captcha );
return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
Html::element( 'input', array(
2009-07-19 04:15:55 +00:00
'name' => 'wpCaptchaWord',
'id' => 'wpCaptchaWord',
'class' => 'mw-ui-input',
'required',
'autocomplete' => 'off',
2009-07-19 04:15:55 +00:00
'tabindex' => 1 ) ) . // tab in before the edit textarea
"</p>\n" .
Xml::element( 'input', array(
'type' => 'hidden',
'name' => 'wpCaptchaId',
'id' => 'wpCaptchaId',
'value' => $index ) );
}
2009-07-19 04:15:55 +00:00
function getMessage( $action ) {
$name = 'questycaptcha-' . $action;
$text = wfMessage( $name )->text();
2009-07-19 04:15:55 +00:00
# Obtain a more tailored message, if possible, otherwise, fall back to
# the default for edits
return wfMessage( $name, $text )->isDisabled() ? wfMessage( 'questycaptcha-edit' )->text() : $text;
2009-07-19 04:15:55 +00:00
}
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
}
}
}