Florianschmidtwelzow has uploaded a new change for review.

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

Change subject: [WIP] Make ReCaptcha usable about the api
......................................................................

[WIP] Make ReCaptcha usable about the api

ToDo:
 * For any reason I get only _TOTALLY_ unreadable CAPTCHA images
   (damn Google! provide a way to get human readable the image
   from the server), and it seems to be always wrong (at least
   I had 0 solved CAPTCHAs for nearly 100 tries I think).

Change-Id: Ifaf0722eb73212416d187144b1cdfd2834e8f461
---
M ReCaptcha/ReCaptcha.class.php
1 file changed, 37 insertions(+), 5 deletions(-)


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

diff --git a/ReCaptcha/ReCaptcha.class.php b/ReCaptcha/ReCaptcha.class.php
index add9242..69b131d 100644
--- a/ReCaptcha/ReCaptcha.class.php
+++ b/ReCaptcha/ReCaptcha.class.php
@@ -34,10 +34,12 @@
                // API is hardwired to return wpCaptchaId and wpCaptchaWord,
                // so use that if the standard two are empty
                $challenge = $wgRequest->getVal(
-                       'recaptcha_challenge_field', $wgRequest->getVal( 
'wpCaptchaId' )
+                       'recaptcha_challenge_field',
+                       $wgRequest->getVal( 'captchaid', $wgRequest->getVal( 
'wpCaptchaId' ) )
                );
                $response = $wgRequest->getVal(
-                       'recaptcha_response_field', $wgRequest->getVal( 
'wpCaptchaWord' )
+                       'recaptcha_response_field',
+                       $wgRequest->getVal( 'captchaword' ), 
$wgRequest->getVal( 'wpCaptchaWord' )
                );
 
                if ( $response === null ) {
@@ -68,10 +70,28 @@
        function addCaptchaAPI( &$resultArr ) {
                global $wgReCaptchaPublicKey;
 
+               $challenge = $this->getReCaptchaChallenge( 
$wgReCaptchaPublicKey );
+
                $resultArr['captcha']['type'] = 'recaptcha';
                $resultArr['captcha']['mime'] = 'image/png';
-               $resultArr['captcha']['key'] = $wgReCaptchaPublicKey;
+               $resultArr['captcha']['id'] = $challenge['challenge'];
+               $resultArr['captcha']['url'] = $challenge['url'];
                $resultArr['captcha']['error'] = $this->recaptcha_error;
+               // kept for bc
+               $resultArr['captcha']['key'] = $wgReCaptchaPublicKey;
+       }
+
+       private function getReCaptchaChallenge( $publicKey ) {
+               $code = Http::get( 
'http://www.google.com/recaptcha/api/challenge?k=' . $publicKey );
+               preg_match( '/challenge : \'(.*)\',/', $code, $matches );
+               if ( is_array( $matches ) && isset( $matches[1] ) ) {
+                       return [
+                               'challenge' => $matches[1],
+                               'url' => 
'http://www.google.com/recaptcha/api/image?c=' . $matches[1],
+                       ];
+               } else {
+                       throw new UnexpectedValueError( 'Expected at least one 
challenge code, got none.' );
+               }
        }
 
        /**
@@ -93,15 +113,27 @@
        public function APIGetAllowedParams( &$module, &$params, $flags ) {
                if ( $flags && $this->isAPICaptchaModule( $module ) ) {
                        if ( defined( 'ApiBase::PARAM_HELP_MSG' ) ) {
+                               // kept for bc
                                $params['recaptcha_challenge_field'] = array(
                                        ApiBase::PARAM_HELP_MSG => 
'recaptcha-apihelp-param-recaptcha_challenge_field',
                                );
+                               $params['captchaid'] = array(
+                                       ApiBase::PARAM_HELP_MSG => 
'recaptcha-apihelp-param-recaptcha_challenge_field',
+                               );
+                               $params['captchaword'] = array(
+                                       ApiBase::PARAM_HELP_MSG => 
'recaptcha-apihelp-param-recaptcha_response_field',
+                               );
+                               // kept for bc
                                $params['recaptcha_response_field'] = array(
                                        ApiBase::PARAM_HELP_MSG => 
'recaptcha-apihelp-param-recaptcha_response_field',
                                );
                        } else {
                                // @todo: Remove this branch when support for 
MediaWiki < 1.25 is dropped
+                               $params['captchaid'] = null;
+                               // kept for bc
                                $params['recaptcha_challenge_field'] = null;
+                               $params['captchaword'] = null;
+                               // kept for bc
                                $params['recaptcha_response_field'] = null;
                        }
                }
@@ -114,8 +146,8 @@
         */
        public function APIGetParamDescription( &$module, &$desc ) {
                if ( $this->isAPICaptchaModule( $module ) ) {
-                       $desc['recaptcha_challenge_field'] = 'Field from the 
ReCaptcha widget';
-                       $desc['recaptcha_response_field'] = 'Field from the 
ReCaptcha widget';
+                       $desc['captchaid'] = 'Challenge ID from the ReCaptcha 
challenge to solve.';
+                       $desc['captchaword'] = 'The user\'s solution for this 
ReCaptcha challenge.';
                }
 
                return true;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifaf0722eb73212416d187144b1cdfd2834e8f461
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ConfirmEdit
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to