summaryrefslogtreecommitdiff
path: root/extensions/ConfirmEdit/MathCaptcha
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2015-06-04 07:31:04 +0200
committerPierre Schmitz <pierre@archlinux.de>2015-06-04 07:58:39 +0200
commitf6d65e533c62f6deb21342d4901ece24497b433e (patch)
treef28adf0362d14bcd448f7b65a7aaf38650f923aa /extensions/ConfirmEdit/MathCaptcha
parentc27b2e832fe25651ef2410fae85b41072aae7519 (diff)
Update to MediaWiki 1.25.1
Diffstat (limited to 'extensions/ConfirmEdit/MathCaptcha')
-rw-r--r--extensions/ConfirmEdit/MathCaptcha/MathCaptcha.class.php50
-rw-r--r--extensions/ConfirmEdit/MathCaptcha/MathCaptcha.php13
-rw-r--r--extensions/ConfirmEdit/MathCaptcha/extension.json9
3 files changed, 72 insertions, 0 deletions
diff --git a/extensions/ConfirmEdit/MathCaptcha/MathCaptcha.class.php b/extensions/ConfirmEdit/MathCaptcha/MathCaptcha.class.php
new file mode 100644
index 00000000..d3e4af69
--- /dev/null
+++ b/extensions/ConfirmEdit/MathCaptcha/MathCaptcha.class.php
@@ -0,0 +1,50 @@
+<?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']['mime'] = 'text/tex';
+ $resultArr['captcha']['id'] = $index;
+ $resultArr['captcha']['question'] = $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>' . Html::input( 'wpCaptchaWord', false, false, array( 'tabindex' => '1', 'autocomplete' => 'off', 'required' ) ) . '</td></tr></table>';
+ $form .= Html::hidden( '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 ) {
+ if ( class_exists( 'MathRenderer' ) ) {
+ $math = MathRenderer::getRenderer( $sum, array(), MW_MATH_PNG );
+ } else {
+ throw new Exception( 'MathCaptcha requires the Math extension for MediaWiki versions 1.18 and above.' );
+ }
+ $html = $math->render();
+ return preg_replace( '/alt=".*?"/', '', $html );
+ }
+}
diff --git a/extensions/ConfirmEdit/MathCaptcha/MathCaptcha.php b/extensions/ConfirmEdit/MathCaptcha/MathCaptcha.php
new file mode 100644
index 00000000..192b49a9
--- /dev/null
+++ b/extensions/ConfirmEdit/MathCaptcha/MathCaptcha.php
@@ -0,0 +1,13 @@
+<?php
+if ( function_exists( 'wfLoadExtension' ) ) {
+ wfLoadExtension( 'ConfirmEdit/MathCaptcha' );
+ // Keep i18n globals so mergeMessageFileList.php doesn't break
+ $wgMessagesDirs['MathCaptcha'] = __DIR__ . '/i18n';
+ /* wfWarn(
+ 'Deprecated PHP entry point used for MathCaptcha extension. Please use wfLoadExtension instead, ' .
+ 'see https://www.mediawiki.org/wiki/Extension_registration for more details.'
+ ); */
+ return;
+} else {
+ die( 'This version of the MathCaptcha extension requires MediaWiki 1.25+' );
+}
diff --git a/extensions/ConfirmEdit/MathCaptcha/extension.json b/extensions/ConfirmEdit/MathCaptcha/extension.json
new file mode 100644
index 00000000..2e3cc4bf
--- /dev/null
+++ b/extensions/ConfirmEdit/MathCaptcha/extension.json
@@ -0,0 +1,9 @@
+{
+ "name": "MathCaptcha",
+ "AutoloadClasses": {
+ "MathCaptcha": "MathCaptcha.class.php"
+ },
+ "config": {
+ "CaptchaClass": "MathCaptcha"
+ }
+}