Debian Codegraph @

extensions/ConfirmEdit/QuestyCaptcha/includes/QuestyCaptcha.php

raw symbols SCIP
1 <?php
2
3 /**
4 * QuestyCaptcha class
5 *
6 * @file
7 * @author Benjamin Lees <[email protected]>
8 * @ingroup Extensions
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 * @var string used for questycaptcha-edit, questycaptcha-addurl, questycaptcha-badlogin,
24 * questycaptcha-createaccount, questycaptcha-create, questycaptcha-sendemail via getMessage()
25 */
26 protected static $messagePrefix = 'questycaptcha-';
27
28 /**
29 * Validate a CAPTCHA response
30 *
31 * @note Trimming done as per T368112
32 *
33 * @param string $answer
34 * @param array $info
35 * @return bool
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 * @param array &$resultArr
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 * @return array
58 */
59 public function describeCaptchaType() {
60 return [
61 'type' => 'question',
62 'mime' => 'text/html',
63 ];
64 }
65
66 /**
67 * @return array
68 */
69 public function getCaptcha() {
70 global $wgCaptchaQuestions;
71
72 // Backwards compatibility
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 * @param int $tabIndex
84 * @return array
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 // tab in before the edit textarea
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 * @param array $captchaData
125 * @param string $id
126 * @return mixed
127 */
128 public function getCaptchaInfo( $captchaData, $id ) {
129 return $captchaData['question'];
130 }
131
132 /**
133 * @param array $requests
134 * @param array $fieldInfo
135 * @param array &$formDescriptor
136 * @param string $action
137 */
138 public function onAuthChangeFormFields( array $requests, array $fieldInfo,
139 array &$formDescriptor, $action ) {
140 /** @var CaptchaAuthenticationRequest $req */
141 $req =
142 AuthenticationRequest::getRequestByClass( $requests,
143 CaptchaAuthenticationRequest::class, true );
144 if ( !$req ) {
145 return;
146 }
147
148 // declare RAW HTML output.
149 $formDescriptor['captchaInfo']['raw'] = true;
150 $formDescriptor['captchaWord']['label-message'] = null;
151 }
152 }
153
154 class_alias( QuestyCaptcha::class, 'QuestyCaptcha' );