| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
namespace MediaWiki\Extension\ConfirmEdit\QuestyCaptcha; |
| 12 |
|
| 13 |
use MediaWiki\Auth\AuthenticationRequest; |
| 14 |
use MediaWiki\Context\RequestContext; |
| 15 |
use MediaWiki\Extension\ConfirmEdit\Auth\CaptchaAuthenticationRequest; |
| 16 |
use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha; |
| 17 |
use MediaWiki\Extension\ConfirmEdit\Store\CaptchaStore; |
| 18 |
use MediaWiki\Html\Html; |
| 19 |
use MediaWiki\Xml\Xml; |
| 20 |
|
| 21 |
class QuestyCaptcha extends SimpleCaptcha { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
protected static $messagePrefix = 'questycaptcha-'; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected function keyMatch( $answer, $info ) { |
| 38 |
if ( is_array( $info['answer'] ) ) { |
| 39 |
return in_array( strtolower( trim( $answer ) ), array_map( 'strtolower', $info['answer'] ) ); |
| 40 |
} else { |
| 41 |
return strtolower( trim( $answer ) ) == strtolower( $info['answer'] ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected function addCaptchaAPI( &$resultArr ) { |
| 49 |
$captcha = $this->getCaptcha(); |
| 50 |
$index = $this->storeCaptcha( $captcha ); |
| 51 |
$resultArr['captcha'] = $this->describeCaptchaType(); |
| 52 |
$resultArr['captcha']['id'] = $index; |
| 53 |
$resultArr['captcha']['question'] = $captcha['question']; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function describeCaptchaType() { |
| 60 |
return [ |
| 61 |
'type' => 'question', |
| 62 |
'mime' => 'text/html', |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function getCaptcha() { |
| 70 |
global $wgCaptchaQuestions; |
| 71 |
|
| 72 |
|
| 73 |
if ( $wgCaptchaQuestions === array_values( $wgCaptchaQuestions ) ) { |
| 74 |
return $wgCaptchaQuestions[ random_int( 0, count( $wgCaptchaQuestions ) - 1 ) ]; |
| 75 |
} |
| 76 |
|
| 77 |
$question = array_rand( $wgCaptchaQuestions, 1 ); |
| 78 |
$answer = $wgCaptchaQuestions[ $question ]; |
| 79 |
return [ 'question' => $question, 'answer' => $answer ]; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function getFormInformation( $tabIndex = 1 ) { |
| 87 |
$captcha = $this->getCaptcha(); |
| 88 |
if ( !$captcha ) { |
| 89 |
die( |
| 90 |
"No questions found; set some in LocalSettings.php using the format from QuestyCaptcha.php." |
| 91 |
); |
| 92 |
} |
| 93 |
$index = $this->storeCaptcha( $captcha ); |
| 94 |
return [ |
| 95 |
'html' => "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> " . |
| 96 |
Html::element( 'input', [ |
| 97 |
'name' => 'wpCaptchaWord', |
| 98 |
'id' => 'wpCaptchaWord', |
| 99 |
'required', |
| 100 |
'autocomplete' => 'off', |
| 101 |
|
| 102 |
'tabindex' => $tabIndex ] |
| 103 |
) . "</p>\n" . |
| 104 |
Xml::element( 'input', [ |
| 105 |
'type' => 'hidden', |
| 106 |
'name' => 'wpCaptchaId', |
| 107 |
'id' => 'wpCaptchaId', |
| 108 |
'value' => $index ] |
| 109 |
) |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
public function showHelp() { |
| 114 |
$context = RequestContext::getMain(); |
| 115 |
$out = $context->getOutput(); |
| 116 |
$out->setPageTitleMsg( $context->msg( 'captchahelp-title' ) ); |
| 117 |
$out->addWikiMsg( 'questycaptchahelp-text' ); |
| 118 |
if ( CaptchaStore::get()->cookiesNeeded() ) { |
| 119 |
$out->addWikiMsg( 'captchahelp-cookies-needed' ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function getCaptchaInfo( $captchaData, $id ) { |
| 129 |
return $captchaData['question']; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function onAuthChangeFormFields( array $requests, array $fieldInfo, |
| 139 |
array &$formDescriptor, $action ) { |
| 140 |
|
| 141 |
$req = |
| 142 |
AuthenticationRequest::getRequestByClass( $requests, |
| 143 |
CaptchaAuthenticationRequest::class, true ); |
| 144 |
if ( !$req ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
$formDescriptor['captchaInfo']['raw'] = true; |
| 150 |
$formDescriptor['captchaWord']['label-message'] = null; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
class_alias( QuestyCaptcha::class, 'QuestyCaptcha' ); |