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
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/** Validate a captcha response */
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-07-19 15:13:01 +00:00
|
|
|
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
|
|
|
|
2016-04-25 20:58:18 +00:00
|
|
|
public function describeCaptchaType() {
|
|
|
|
return [
|
|
|
|
'type' => 'question',
|
|
|
|
'mime' => 'text/plain',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2009-07-19 04:15:55 +00:00
|
|
|
function getCaptcha() {
|
|
|
|
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
|
|
|
|
2015-09-23 06:05:28 +00:00
|
|
|
function getForm( OutputPage $out, $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 );
|
|
|
|
return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> " .
|
2016-05-09 23:41:17 +00:00
|
|
|
Html::element( 'input', [
|
2009-07-19 04:15:55 +00:00
|
|
|
'name' => 'wpCaptchaWord',
|
|
|
|
'id' => 'wpCaptchaWord',
|
2014-07-30 01:39:22 +00:00
|
|
|
'class' => 'mw-ui-input',
|
2011-10-21 14:59:16 +00:00
|
|
|
'required',
|
2013-05-03 01:09:51 +00:00
|
|
|
'autocomplete' => 'off',
|
2016-05-09 23:41:17 +00:00
|
|
|
'tabindex' => $tabIndex ] ) . // tab in before the edit textarea
|
2009-07-19 04:15:55 +00:00
|
|
|
"</p>\n" .
|
2016-05-09 23:41:17 +00:00
|
|
|
Xml::element( 'input', [
|
2009-07-19 04:15:55 +00:00
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'wpCaptchaId',
|
|
|
|
'id' => 'wpCaptchaId',
|
2016-05-09 23:41:17 +00:00
|
|
|
'value' => $index ] );
|
2009-07-19 04:15:55 +00:00
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
|
2015-04-02 22:56:48 +00:00
|
|
|
function showHelp() {
|
|
|
|
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
|
|
|
|
|
|
|
public function getCaptchaInfo( $captchaData, $id ) {
|
|
|
|
return $captchaData['question'];
|
|
|
|
}
|
2009-07-19 15:13:01 +00:00
|
|
|
}
|