1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
<?php
use MediaWiki\Auth\AuthenticationRequest;
class MathCaptcha extends SimpleCaptcha {
/**
* Validate a captcha response
* @param string $answer
* @param array $info
* @return bool
*/
function keyMatch( $answer, $info ) {
return (int)$answer == (int)$info['answer'];
}
/**
* @param array &$resultArr
*/
function addCaptchaAPI( &$resultArr ) {
list( $sum, $answer ) = $this->pickSum();
$html = $this->fetchMath( $sum );
$index = $this->storeCaptcha( [ 'answer' => $answer ] );
$resultArr['captcha'] = $this->describeCaptchaType();
$resultArr['captcha']['id'] = $index;
$resultArr['captcha']['question'] = $html;
}
/**
* @return array
*/
public function describeCaptchaType() {
return [
'type' => 'math',
'mime' => 'text/html',
];
}
/**
* @param int $tabIndex
* @return array
*/
function getFormInformation( $tabIndex = 1 ) {
list( $sum, $answer ) = $this->pickSum();
$index = $this->storeCaptcha( [ 'answer' => $answer ] );
$form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
$form .= '<td>' . Html::input( 'wpCaptchaWord', false, false, [
'tabindex' => $tabIndex,
'autocomplete' => 'off',
'required'
] ) . '</td></tr></table>';
$form .= Html::hidden( 'wpCaptchaId', $index );
return [ 'html' => $form ];
}
/**
* Pick a random sum
* @return array
*/
function pickSum() {
$a = mt_rand( 0, 100 );
$b = mt_rand( 0, 10 );
$op = mt_rand( 0, 1 ) ? '+' : '-';
$sum = "{$a} {$op} {$b} = ";
$ans = $op == '+' ? ( $a + $b ) : ( $a - $b );
return [ $sum, $ans ];
}
/**
* Fetch the math
* @param int $sum
* @return string
*/
function fetchMath( $sum ) {
if ( class_exists( 'MathRenderer' ) ) {
$math = MathRenderer::getRenderer( $sum, [], 'png' );
} else {
throw new LogicException(
'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
}
$math->render();
$html = $math->getHtmlOutput();
return preg_replace( '/alt=".*?"/', '', $html );
}
/**
* @return array
*/
public function getCaptcha() {
list( $sum, $answer ) = $this->pickSum();
return [ 'question' => $sum, 'answer' => $answer ];
}
/**
* @param array $captchaData
* @param string $id
* @return mixed
*/
public function getCaptchaInfo( $captchaData, $id ) {
$sum = $captchaData['question'];
return $this->fetchMath( $sum );
}
/**
* @param array $requests
* @param array $fieldInfo
* @param array &$formDescriptor
* @param string $action
*/
public function onAuthChangeFormFields( array $requests, array $fieldInfo,
array &$formDescriptor, $action ) {
/** @var CaptchaAuthenticationRequest $req */
$req = AuthenticationRequest::getRequestByClass(
$requests,
CaptchaAuthenticationRequest::class,
true
);
if ( !$req ) {
return;
}
$formDescriptor['captchaInfo']['raw'] = true;
$formDescriptor['captchaWord']['label-message'] = null;
}
}
|