Edward Chernenko has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/198248

Change subject: Add support for ReCaptcha 2.0
......................................................................

Add support for ReCaptcha 2.0

Change-Id: Ie04fffc6fe353ff5ca64a0a6beb3057163e173b6
---
A ReCaptcha2.class.php
A ReCaptcha2.php
A i18n/re2/en.json
3 files changed, 116 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ConfirmEdit 
refs/changes/48/198248/1

diff --git a/ReCaptcha2.class.php b/ReCaptcha2.class.php
new file mode 100644
index 0000000..315392b
--- /dev/null
+++ b/ReCaptcha2.class.php
@@ -0,0 +1,80 @@
+<?php
+
+/**
+       @file
+       @author Edward Chernenko <edwards...@gmail.com>
+*/
+
+/**
+       @class
+       @brief Google reCaptcha 2.0
+*/
+class ReCaptcha2 extends SimpleCaptcha {
+
+       /**
+               @brief Print the captcha HTML.
+       */
+       function getForm() {
+               global $wgOut, $wgReCaptchaPublicKey;
+
+               
$wgOut->addScriptFile('https://www.google.com/recaptcha/api.js');
+
+               return Xml::tags('div', array('class' => 'recaptcha'),
+                       Xml::tags('div', array(
+                               'class' => 'g-recaptcha',
+                               'data-sitekey' => $wgReCaptchaPublicKey
+                       ), '')
+               );
+       }
+
+       /**
+               @brief Check if the user entered the captcha correctly or not.
+       */
+       function passCaptcha() {
+               global $wgReCaptchaPrivateKey, $wgRequest;
+
+               $response = $wgRequest->getVal('g-recaptcha-response');
+               if(!$response) {
+                       return false;
+               }
+
+               $url = wfAppendQuery(
+                       'https://www.google.com/recaptcha/api/siteverify',
+                       array(
+                               'secret' => $wgReCaptchaPrivateKey,
+                               'response' => $response,
+                               'remoteip' => $wgRequest->getIP()
+                       )
+               );
+               $res = Http::get($url);
+               if(!$res) {
+                       return false;
+               }
+
+               $ret = FormatJson::decode($res, true);
+               if(!$ret || !array_key_exists('success', $ret)) {
+                       return false;
+               }
+               return $ret['success'];
+       }
+
+       /**
+               @brief Kindly ask the user to pass the captcha.
+       */
+       function getMessage( $action ) {
+               $msg = wfMessage('recaptcha2-' . $action);
+               if($msg->isDisabled()) {
+                       $msg = wfMessage('recaptcha2-edit');
+               }
+               return $msg->text();
+       }
+
+       /**
+               @brief Override SimpleCaptcha::addCaptchaAPI() and do nothing.
+               @note reCaptcha 2.0 works by analyzing how mouse moves, etc.,
+                       so there is no way to "show" it via API.
+       */
+       function addCaptchaAPI( &$resultArr ) {
+               /* Nothing to do here. */
+       }
+}
diff --git a/ReCaptcha2.php b/ReCaptcha2.php
new file mode 100644
index 0000000..88574b5
--- /dev/null
+++ b/ReCaptcha2.php
@@ -0,0 +1,27 @@
+<?php
+
+/**
+       @file
+       @brief Google reCaptcha 2.0 for Extension:ConfirmEdit.
+       @author Edward Chernenko <edwards...@gmail.com>
+
+       See https://developers.google.com/recaptcha/ for details.
+*/
+
+if (!defined('MEDIAWIKI')) {
+        echo <<<EOT
+To install this extension, put the following line in LocalSettings.php:
+require_once( "\$IP/extensions/Moderation/Moderation.php" );
+EOT;
+        exit( 1 );
+}
+
+require_once(__DIR__ . '/ConfirmEdit.php');
+$wgCaptchaClass = 'ReCaptcha2';
+
+$wgMessagesDirs['ReCaptcha2'] = __DIR__ . '/i18n/re2';
+$wgAutoloadClasses['ReCaptcha2'] = __DIR__ . '/ReCaptcha2.class.php';
+
+/* These parameters must be set in LocalSettings.php */
+$wgReCaptchaPublicKey = '';
+$wgReCaptchaPrivateKey = '';
diff --git a/i18n/re2/en.json b/i18n/re2/en.json
new file mode 100644
index 0000000..274f559
--- /dev/null
+++ b/i18n/re2/en.json
@@ -0,0 +1,9 @@
+{
+       "@metadata": {
+               "authors": [
+                       "Edward Chernenko"
+               ]
+       },
+       "recaptcha2-desc": "reCaptcha 2.0 for ConfirmEdit",
+       "recaptcha2-edit": "Please confirm that you're not a robot:"
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/198248
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie04fffc6fe353ff5ca64a0a6beb3057163e173b6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ConfirmEdit
Gerrit-Branch: master
Gerrit-Owner: Edward Chernenko <edwards...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to