mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/ConfirmEdit
synced 2024-11-12 09:32:55 +00:00
c112c24f54
* Hooking into the APIEditBeforeSave hook (which will appear in the ApiEdit_Vodafone branch in the next revision) * Moved general CAPTCHA generation in SimpleCaptcha to getCaptcha() * Moved general confirmEdit() functionality to doConfirmEdit() to facilitate differtiating between UI and API handling * Adding SimpleCaptcha::addCaptchaAPI() method that adds CAPTCHA information to an API result array. Other CAPTCHA implementations should override this method with a function that does the same (did this for FancyCaptcha and MathCaptcha)
49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
|
|
class MathCaptcha extends SimpleCaptcha {
|
|
|
|
/** Validate a captcha response */
|
|
function keyMatch( $answer, $info ) {
|
|
return (int)$answer == (int)$info['answer'];
|
|
}
|
|
|
|
function addCaptchaAPI(&$resultArr) {
|
|
list( $sum, $answer ) = $this->pickSum();
|
|
$index = $this->storeCaptcha( array('answer' => $answer ) );
|
|
$resultArr['captcha']['type'] = 'math';
|
|
$resultArr['captcha']['id'] = $index;
|
|
$resultArr['captcha']['sum'] = $sum;
|
|
}
|
|
|
|
/** Produce a nice little form */
|
|
function getForm() {
|
|
list( $sum, $answer ) = $this->pickSum();
|
|
$index = $this->storeCaptcha( array( 'answer' => $answer ) );
|
|
|
|
$form = '<table><tr><td>' . $this->fetchMath( $sum ) . '</td>';
|
|
$form .= '<td>' . wfInput( 'wpCaptchaAnswer', false, false, array( 'tabindex' => '1' ) ) . '</td></tr></table>';
|
|
$form .= wfHidden( 'wpCaptchaId', $index );
|
|
return $form;
|
|
}
|
|
|
|
/** Pick a random sum */
|
|
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 array( $sum, $ans );
|
|
}
|
|
|
|
/** Fetch the math */
|
|
function fetchMath( $sum ) {
|
|
$math = new MathRenderer( $sum );
|
|
$math->setOutputMode( MW_MATH_PNG );
|
|
$html = $math->render();
|
|
return preg_replace( '/alt=".*"/', '', $html );
|
|
}
|
|
|
|
}
|
|
?>
|