MediaWiki REL1_32
MathCaptcha.php
Go to the documentation of this file.
1<?php
2
4
6
13 protected function keyMatch( $answer, $info ) {
14 return (int)$answer == (int)$info['answer'];
15 }
16
20 protected function addCaptchaAPI( &$resultArr ) {
21 list( $sum, $answer ) = $this->pickSum();
22 $html = $this->fetchMath( $sum );
23 $index = $this->storeCaptcha( [ 'answer' => $answer ] );
24 $resultArr['captcha'] = $this->describeCaptchaType();
25 $resultArr['captcha']['id'] = $index;
26 $resultArr['captcha']['question'] = $html;
27 }
28
32 public function describeCaptchaType() {
33 return [
34 'type' => 'math',
35 'mime' => 'text/html',
36 ];
37 }
38
43 public function getFormInformation( $tabIndex = 1 ) {
44 list( $sum, $answer ) = $this->pickSum();
45 $index = $this->storeCaptcha( [ 'answer' => $answer ] );
46
47 $form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
48 $form .= '<td>' . Html::input( 'wpCaptchaWord', false, false, [
49 'tabindex' => $tabIndex,
50 'autocomplete' => 'off',
51 'required'
52 ] ) . '</td></tr></table>';
53 $form .= Html::hidden( 'wpCaptchaId', $index );
54 return [ 'html' => $form ];
55 }
56
61 private function pickSum() {
62 $a = mt_rand( 0, 100 );
63 $b = mt_rand( 0, 10 );
64 $op = mt_rand( 0, 1 ) ? '+' : '-';
65 $sum = "{$a} {$op} {$b} = ";
66 $ans = $op == '+' ? ( $a + $b ) : ( $a - $b );
67 return [ $sum, $ans ];
68 }
69
75 private function fetchMath( $sum ) {
76 if ( class_exists( 'MathRenderer' ) ) {
77 $math = MathRenderer::getRenderer( $sum, [], 'png' );
78 } else {
79 throw new LogicException(
80 'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
81 }
82 $math->render();
83 $html = $math->getHtmlOutput();
84 return preg_replace( '/alt=".*?"/', '', $html );
85 }
86
90 public function getCaptcha() {
91 list( $sum, $answer ) = $this->pickSum();
92 return [ 'question' => $sum, 'answer' => $answer ];
93 }
94
100 public function getCaptchaInfo( $captchaData, $id ) {
101 $sum = $captchaData['question'];
102 return $this->fetchMath( $sum );
103 }
104
111 public function onAuthChangeFormFields( array $requests, array $fieldInfo,
114 $req = AuthenticationRequest::getRequestByClass(
115 $requests,
116 CaptchaAuthenticationRequest::class,
117 true
118 );
119 if ( !$req ) {
120 return;
121 }
122
123 $formDescriptor['captchaInfo']['raw'] = true;
124 $formDescriptor['captchaWord']['label-message'] = null;
125 }
126}
addCaptchaAPI(&$resultArr)
pickSum()
Pick a random sum.
getFormInformation( $tabIndex=1)
keyMatch( $answer, $info)
Validate a captcha response.
getCaptchaInfo( $captchaData, $id)
onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
fetchMath( $sum)
Fetch the math.
This is a value object for authentication requests.
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
string $action
Used to select the right message.
storeCaptcha( $info)
Generate a captcha session ID and save the info in PHP's session storage.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
this hook is for auditing only $req
Definition hooks.txt:1018
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead & $formDescriptor
Definition hooks.txt:2208
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2062
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead. & $feedLinks hooks can tweak the array to change how login etc forms should look $requests
Definition hooks.txt:304
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))