Florianschmidtwelzow has uploaded a new change for review. https://gerrit.wikimedia.org/r/179793
Change subject: Add new reCaptcha (no captcha) to ConfirmEdit ...................................................................... Add new reCaptcha (no captcha) to ConfirmEdit This change adds a new Captcha type (ReCaptchaNoCaptcha) to support the new version of Googles reCaptcha. See more: http://googleonlinesecurity.blogspot.de/2014/12/are-you-robot-introducing-no-captcha.html Change-Id: I5908fd2716786237adb01a403d5bd1e22d95c563 --- M Captcha.php A ReCaptchaNoCaptcha.class.php A ReCaptchaNoCaptcha.php A i18n/renocaptcha/en.json A i18n/renocaptcha/qqq.json 5 files changed, 121 insertions(+), 5 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ConfirmEdit refs/changes/93/179793/1 diff --git a/Captcha.php b/Captcha.php index 851936c..fa5491b 100644 --- a/Captcha.php +++ b/Captcha.php @@ -34,7 +34,7 @@ * * @return string HTML */ - function getForm() { + function getForm( OutputPage $out ) { $captcha = $this->getCaptcha(); $index = $this->storeCaptcha( $captcha ); @@ -66,7 +66,7 @@ } unset( $page->ConfirmEdit_ActivateCaptcha ); $out->addWikiText( $this->getMessage( $this->action ) ); - $out->addHTML( $this->getForm() ); + $out->addHTML( $this->getForm( $out ) ); } /** @@ -100,7 +100,7 @@ $form->addFooterText( "<div class='captcha'>" . $wgOut->parse( $this->getMessage( 'sendemail' ) ) . - $this->getForm() . + $this->getForm( $wgOut ) . "</div>\n" ); } return true; @@ -122,7 +122,7 @@ $template->set( 'header', "<div class='captcha'>" . $wgOut->parse( $this->getMessage( 'createaccount' ) ) . - $this->getForm() . + $this->getForm( $wgOut ) . "</div>\n" ); } return true; @@ -141,7 +141,7 @@ $template->set( 'header', "<div class='captcha'>" . $wgOut->parse( $this->getMessage( 'badlogin' ) ) . - $this->getForm() . + $this->getForm( $wgOut ) . "</div>\n" ); } return true; diff --git a/ReCaptchaNoCaptcha.class.php b/ReCaptchaNoCaptcha.class.php new file mode 100644 index 0000000..4272c06 --- /dev/null +++ b/ReCaptchaNoCaptcha.class.php @@ -0,0 +1,69 @@ +<?php + class ReCaptchaNoCaptcha extends SimpleCaptcha { + private $error = null; + /** + * Get the captcha form + * @return string + */ + function getForm( OutputPage $out ) { + global $wgReCaptchaSiteKey; + + // reCaptchas new magic comes from a script + $out->addHeadItem( + 'g-recaptchascript', + "<script src='https://www.google.com/recaptcha/api.js'></script>" + ); + + return Html::element( + 'div', + array( + 'class' => 'g-recaptcha', + 'data-sitekey' => $wgReCaptchaSiteKey + ) + ); + } + + /** + * Check, if the user solved the captcha + * + * @return boolean + */ + function passCaptcha() { + global $wgRequest, $wgReCaptchaSecretKey; + + $url = 'https://www.google.com/recaptcha/api/siteverify'; + // build data to append to request + $data = array( + 'secret' => $wgReCaptchaSecretKey, + 'response' => $wgRequest->getVal( 'g-recaptcha-response' ) + ); + $request = wfAppendQuery( $url, $data ); + $response = FormatJson::decode( Http::get( $request ), true ); + $this->error = $response['error-codes']; + + return $response['success']; + } + + function addCaptchaAPI( &$resultArr ) { + global $wgReCaptchaSiteKey; + + $resultArr['captcha']['type'] = 'recaptchanocaptcha'; + $resultArr['captcha']['mime'] = 'image/png'; + $resultArr['captcha']['key'] = $wgReCaptchaSiteKey; + $resultArr['captcha']['error'] = $this->error; + } + + /** + * Show a message asking the user to enter a captcha on edit + * The result will be treated as wiki text + * + * @param $action string Action being performed + * @return string + */ + function getMessage( $action ) { + $name = 'renocaptcha-' . $action; + $msg = wfMessage( $name ); + + return $msg->isDisabled() ? wfMessage( 'renocaptcha-edit' )->text() : $msg->text(); + } + } diff --git a/ReCaptchaNoCaptcha.php b/ReCaptchaNoCaptcha.php new file mode 100644 index 0000000..1423b9c --- /dev/null +++ b/ReCaptchaNoCaptcha.php @@ -0,0 +1,24 @@ +<?php + /** + * reCaptcha no captcha class + * + * @author Florian Schmidt <[email protected]> + * @licence MIT + */ + + if ( !defined( 'MEDIAWIKI' ) ) { + exit; + } + + $dir = __DIR__; + require_once $dir . '/ConfirmEdit.php'; + $wgCaptchaClass = 'ReCaptchaNoCaptcha'; + + $wgAutoloadClasses['ReCaptchaNoCaptcha'] = $dir . '/ReCaptchaNoCaptcha.class.php'; + + // reuse recaptcha messages dir + $wgMessagesDirs['ReCaptchaNoCaptcha'] = __DIR__ . '/i18n/renocaptcha'; + + // Site and secret keys + $wgReCaptchaSiteKey = ''; + $wgReCaptchaSecretKey = ''; diff --git a/i18n/renocaptcha/en.json b/i18n/renocaptcha/en.json new file mode 100644 index 0000000..2526723 --- /dev/null +++ b/i18n/renocaptcha/en.json @@ -0,0 +1,12 @@ +{ + "@metadata": { + "authors": [] + }, + "renocaptcha-edit": "To protect the wiki against automated edit spam, we kindly ask you to solve the following Captcha:", + "renocaptcha-addurl": "Your edit includes new external links. To protect the wiki against automated spam, we kindly ask you to solve the following Captcha:", + "renocaptcha-badlogin": "To protect the wiki against automated password cracking, we kindly ask you to solve the following Captcha:", + "renocaptcha-createaccount": "To protect the wiki against automated account creation, we kindly ask you to solve the following Captcha:", + "renocaptcha-createaccount-fail": "It seems you haven't solved the Captcha.", + "renocaptcha-create": "To protect the wiki against automated page creation, we kindly ask you to solve the following Captcha:", + "renocaptcha-noscript": "Unhappily you have disabled JavaScript, so we can't recognize automatically, if you're a human or not. Please solve the Captcha above and copy the resulting text into the following textarea:" +} \ No newline at end of file diff --git a/i18n/renocaptcha/qqq.json b/i18n/renocaptcha/qqq.json new file mode 100644 index 0000000..2f287df --- /dev/null +++ b/i18n/renocaptcha/qqq.json @@ -0,0 +1,11 @@ +{ + "@metadata": { + "authors": [] + }, + "renocaptcha-edit": "Message above the captcha for edit action.", + "renocaptcha-addurl": "Message above the captcha for addurl (user added new external links to the page) action.", + "renocaptcha-badlogin": "Message above the captcha for badlogin action.", + "renocaptcha-createaccount": "Message above the captcha for createaccount (user creates a new account) action.", + "renocaptcha-createaccount-fail": "Error message, when the captcha isn't solved correctly.", + "renocaptcha-create": "Message above the captcha for create (user creates a new page) action." +} \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/179793 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5908fd2716786237adb01a403d5bd1e22d95c563 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
