MediaWiki  1.30.0
MathCaptcha.class.php
Go to the documentation of this file.
1 <?php
2 
3 use MediaWiki\Auth\AuthenticationRequest;
4 
5 class MathCaptcha extends SimpleCaptcha {
6 
13  function keyMatch( $answer, $info ) {
14  return (int)$answer == (int)$info['answer'];
15  }
16 
20  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  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 
58  function pickSum() {
59  $a = mt_rand( 0, 100 );
60  $b = mt_rand( 0, 10 );
61  $op = mt_rand( 0, 1 ) ? '+' : '-';
62  $sum = "{$a} {$op} {$b} = ";
63  $ans = $op == '+' ? ( $a + $b ) : ( $a - $b );
64  return [ $sum, $ans ];
65  }
66 
68  function fetchMath( $sum ) {
69  if ( class_exists( 'MathRenderer' ) ) {
70  $math = MathRenderer::getRenderer( $sum, [], 'png' );
71  } else {
72  throw new LogicException(
73  'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
74  }
75  $math->render();
76  $html = $math->getHtmlOutput();
77  return preg_replace( '/alt=".*?"/', '', $html );
78  }
79 
83  public function getCaptcha() {
84  list( $sum, $answer ) = $this->pickSum();
85  return [ 'question' => $sum, 'answer' => $answer ];
86  }
87 
93  public function getCaptchaInfo( $captchaData, $id ) {
94  $sum = $captchaData['question'];
95  return $this->fetchMath( $sum );
96  }
97 
104  public function onAuthChangeFormFields( array $requests, array $fieldInfo,
105  array &$formDescriptor, $action ) {
107  $req = AuthenticationRequest::getRequestByClass(
108  $requests,
110  true
111  );
112  if ( !$req ) {
113  return;
114  }
115 
116  $formDescriptor['captchaInfo']['raw'] = true;
117  $formDescriptor['captchaWord']['label-message'] = null;
118  }
119 }
MathCaptcha::getFormInformation
getFormInformation( $tabIndex=1)
Definition: MathCaptcha.class.php:43
MathCaptcha::keyMatch
keyMatch( $answer, $info)
Validate a captcha response.
Definition: MathCaptcha.class.php:13
MathCaptcha::fetchMath
fetchMath( $sum)
Fetch the math.
Definition: MathCaptcha.class.php:68
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$req
this hook is for auditing only $req
Definition: hooks.txt:988
SimpleCaptcha\$action
string $action
Used to select the right message.
Definition: Captcha.php:20
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Html\input
static input( $name, $value='', $type='text', array $attribs=[])
Convenience function to produce an "<input>" element.
Definition: Html.php:642
MathCaptcha::getCaptchaInfo
getCaptchaInfo( $captchaData, $id)
Definition: MathCaptcha.class.php:93
$html
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:1965
MathCaptcha
Definition: MathCaptcha.class.php:5
list
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
Html\hidden
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:725
MathCaptcha::getCaptcha
getCaptcha()
Definition: MathCaptcha.class.php:83
SimpleCaptcha
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
Definition: Captcha.php:9
SimpleCaptcha\storeCaptcha
storeCaptcha( $info)
Generate a captcha session ID and save the info in PHP's session storage.
Definition: Captcha.php:1030
MathCaptcha::addCaptchaAPI
addCaptchaAPI(&$resultArr)
Definition: MathCaptcha.class.php:20
$requests
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
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MathCaptcha::describeCaptchaType
describeCaptchaType()
Definition: MathCaptcha.class.php:32
MathCaptcha::onAuthChangeFormFields
onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
Definition: MathCaptcha.class.php:104
MathCaptcha::pickSum
pickSum()
Pick a random sum.
Definition: MathCaptcha.class.php:58
array
the array() calling protocol came about after MediaWiki 1.4rc1.