Skip to content

Commit 629d825

Browse files
Implement ReCaptcha protection for registration
1 parent 2d35957 commit 629d825

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

html-templates/register/register.tpl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
{block "title"}Register — {$dwoo.parent}{/block}
44

5+
{block js-top}
6+
{$dwoo.parent}
7+
8+
{if RemoteSystems\ReCaptcha::$siteKey}
9+
<script src='https://www.google.com/recaptcha/api.js'></script>
10+
{/if}
11+
{/block}
12+
513
{block "content"}
614
{$User = $data}
715
<form method="POST" id="register">
@@ -49,7 +57,11 @@
4957
<label for="PasswordConfirm">{_ "Password Confirmation"}</label>
5058
<input type="password" class="form-control" id="PasswordConfirm" name="PasswordConfirm" value="{refill field=PasswordConfirm}">
5159
</div>
52-
60+
61+
{if RemoteSystems\ReCaptcha::$siteKey}
62+
<div class="form-group g-recaptcha" data-sitekey="{RemoteSystems\ReCaptcha::$siteKey|escape}"></div>
63+
{/if}
64+
5365
<div class="form-group">
5466
<button type="submit" class="btn btn-primary">{_ "Create Account"}</button>
5567
<p class="help-block">{_ "Already have an account?"} <a href="/login{tif $.request.return ? cat('?return=', escape($.request.return, url))}">{_ "Log in"}</a></p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace RemoteSystems;
4+
5+
class ReCaptcha
6+
{
7+
public static $siteKey;
8+
public static $secretKey;
9+
10+
protected static $instance;
11+
12+
public static function setInstance(\ReCaptcha\ReCaptcha $instance)
13+
{
14+
static::$instance = $instance;
15+
}
16+
17+
public static function getInstance()
18+
{
19+
if (!static::$instance && static::$secretKey) {
20+
static::$instance = new \ReCaptcha\ReCaptcha(static::$secretKey);
21+
}
22+
23+
return static::$instance;
24+
}
25+
}

php-config/Emergence/People/RegistrationRequestHandler.config.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@
1919
# 'replace_interests' => false,
2020
# 'send_welcome' => false
2121
# ]);
22-
#};
22+
#};
23+
24+
RegistrationRequestHandler::$applyRegistrationData = function(User $User, array $requestData, array &$additionalErrors) {
25+
if ($recaptcha = \RemoteSystems\ReCaptcha::getInstance()) {
26+
$recaptchaResponse = $recaptcha->verify($requestData['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
27+
28+
if (!$recaptchaResponse->isSuccess()) {
29+
$additionalErrors['ReCaptcha'] = 'Please prove that you aren\'t a spam robot by completing the reCAPTCHA';
30+
}
31+
}
32+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
/**
4+
* Obtain site and secret keys from Google by registering
5+
* at https://www.google.com/recaptcha/admin
6+
*/
7+
8+
//RemoteSystems\ReCaptcha::$siteKey = 'YOUR_SITE_KEY';
9+
//RemoteSystems\ReCaptcha::$secretKey = 'YOUR_SECRET_KEY';

0 commit comments

Comments
 (0)