Skip to content

Commit b4aab0c

Browse files
authored
Merge pull request #1 from AlexStack/dev
Support Google Recaptcha v3
2 parents 17bf139 + 4759efe commit b4aab0c

File tree

5 files changed

+121
-17
lines changed

5 files changed

+121
-17
lines changed

README.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
# Google Recaptcha to any form
1+
# Google Recaptcha v2 or v3 to any form
22
[![Latest Stable Version](https://poser.pugx.org/alexstack/google-recaptcha-to-any-form/v/stable)](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)
33
[![License](https://poser.pugx.org/alexstack/google-recaptcha-to-any-form/license)](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)
44
[![Total Downloads](https://poser.pugx.org/alexstack/google-recaptcha-to-any-form/downloads)](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)
55

6-
- It can display a Google Recaptcha v2 in any custom form with flexible settings and no affection to your existing code. Also works well for SilverStripe 4.x/3.x/2.x & Larave & Wordpress & other CMS.
6+
- It can display a Google Recaptcha v2 or v3 in any custom form with flexible settings and no affection to your existing code. Also works well for SilverStripe 4.x/3.x/2.x & Larave & Wordpress & other CMS.
77

88
# Basic example codes
99

10-
- Display Google Recaptcha after a Form_Field_ID:
10+
- Display Google Recaptcha v2 or v3 after a Form_Field_ID:
1111

1212
```php
13+
// For Google recaptcha v2
1314
GoogleRecaptcha::show('SiteKey', 'Form_Field_ID');
15+
16+
// For Google recaptcha v3
17+
GoogleRecaptcha::showV3('SiteKey', 'Form_Field_ID');
1418
```
1519

1620
- Verify it in the backend php:
@@ -37,31 +41,46 @@ composer require alexstack/google-recaptcha-to-any-form
3741

3842
![How to display it on frontend page](https://developers.google.com/recaptcha/images/newCaptchaAnchor.gif "Google Recaptcha")
3943

40-
- Set up your Google Recaptcha account for you website and get the site key and secret key
44+
- Set up your Google Recaptcha v2 or v3 account for you website and get the site key and secret key
4145
- Import the GoogleRecaptcha class:
4246

4347
```php
4448
use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
4549
```
4650

47-
- Put below php code in your frontend template/page.
51+
- Put below php code in your frontend template/page for **Google Recaptcha v2**.
4852

4953
```php
50-
GoogleRecaptcha::show($GoogleRecaptchaSiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');
54+
GoogleRecaptcha::show($SiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');
5155
```
56+
![google-recaptcha-v2-default](docs/images/google-recaptcha-v2-default.png "google-recaptcha-v2-default")
5257

53-
- Description for above code:
54-
- '\$GoogleRecaptchaSiteKey': The Google Recaptcha Site Key of your website. You can directly put site key here or a variable or from database.
55-
- 'Form_ContactForm_Message': Form field id, via html code: ... `<input type="text" id="Form_ContactForm_Message" />` ... Your Google Recaptcha will display after this form field.
58+
### Explain for above code:
59+
- '\$SiteKey': The Google Recaptcha Site Key of your website. You can directly put site key here or a variable or from database.
60+
- 'Form_ContactForm_Message': Form_Field_ID, via html code. eg. ... `<input type="text" id="Form_ContactForm_Message" />` ... Your Google Recaptcha will display after this form field.
5661
- 'no_debug': Change to debug and not tick the I'm not a robot checkbox will continue submit the form, then you will see the failed message.
5762
- 'mt-4 mb-1': Extra css class name for the Google Recaptcha area.
58-
- 'Please tick the reCAPTCHA checkbox first': Frontend alert message if the end user does not tick the checkbox.
63+
- 'Please tick the reCAPTCHA checkbox first': Frontend alert message if the end user does not tick the checkbox. **Tips:** You can change this value to "v3", it will automatically switch to use Google Recaptcha v3
5964
- Default value of the parameters of the show() method
6065

6166
```php
6267
show($site_key,$after_field_id='Form_ContactForm_Comment', $debug='no_debug', $extra_class="mt-4 mb-4", $please_tick_msg="Please tick the I'm not robot checkbox");
6368
```
6469

70+
- Put below php code in your frontend template/page for **Google Recaptcha v3**
71+
```php
72+
GoogleRecaptcha::show($SiteKey, 'Form_Field_ID', 'no_debug','show-inline-badge mt-4 mb-3','v3');
73+
// or
74+
GoogleRecaptcha::showV3($SiteKey, 'Form_Field_ID', 'no_debug');
75+
```
76+
![google-recaptcha-v3-default](docs/images/google-recaptcha-v3-default.png "google-recaptcha-v3-default")
77+
78+
- Our Google Recaptcha v3 will automatically get g-recaptcha-response value after the page load 10 seconds or the Form_Field_ID(eg. Form_ContactForm_Message) was clicked.
79+
- 'no_debug': Change to "debug" will always submit an empty g-recaptcha-response to the backend.
80+
- 'show-inline-badge mt-4 mb-3': Extra css class name for the Google Recaptcha inline-badge. Remove show-inline-badge will show a right bottom float Recaptcha badge instead inline-badge.
81+
![google-recaptcha-v3-inline](docs/images/google-recaptcha-v3-inline.png "google-recaptcha-v3-inline")
82+
83+
6584
- If you do not want to use the show() method, You can also use your own code to display the recaptcha for a custom style. Just make sure the form action method is POST, then you can still use below verify() method in your backend script.
6685

6786
# <a name="backend"></a>How to verify it in the backend script
@@ -84,7 +103,13 @@ GoogleRecaptcha::verify($GoogleRecaptchaSecretKey, 'Google Recaptcha Validation
84103
- Default value of the parameters of the verify() method
85104

86105
```php
87-
verify($secret_key, $break_msg = null)
106+
verify($secret_key, $break_msg = null, $recaptcha_score=0.5)
107+
```
108+
109+
- If you are using Google Recaptcha v3, it verify score < 0.5 as a failed result by default. You can set the score if you want a different value. eg.
110+
111+
```php
112+
GoogleRecaptcha::verify($SecretKey, 'Google Recaptcha Validation Failed!!', 0.36);
88113
```
89114

90115
# <a name="silverstripe"></a>Usage example for SilverStripe 4.x/3.x
@@ -99,7 +124,7 @@ use GoogleRecaptchaToAnyForm\GoogleRecaptcha;
99124

100125
```php
101126
public function showGoogleRecaptcha() {
102-
return GoogleRecaptcha::show($GoogleRecaptchaSiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');
127+
return GoogleRecaptcha::show($SiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');
103128
}
104129
```
105130

20.7 KB
Loading
26.9 KB
Loading
35.2 KB
Loading

src/AlexStack/GoogleRecaptchaToAnyForm/GoogleRecaptcha.php

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class GoogleRecaptcha
1616
* @param [string] $break_msg, if set, pop up as an javascript alert and exit
1717
* @return true or false
1818
*/
19-
public static function verify($secret_key, $break_msg = null)
19+
public static function verify($secret_key, $break_msg = null, $recaptcha_score = 0.5)
2020
{
2121
$valid = false;
2222
if (isset($_POST['g-recaptcha-response']) && strlen($_POST['g-recaptcha-response']) > 20) {
23-
$valid = Self::result($secret_key, $_POST['g-recaptcha-response']);
23+
$valid = Self::result($secret_key, $_POST['g-recaptcha-response'], $recaptcha_score);
2424
}
2525

2626
if (!$valid && $break_msg) {
@@ -40,7 +40,7 @@ public static function verify($secret_key, $break_msg = null)
4040
* @param [type] $g_recaptcha_response
4141
* @return void
4242
*/
43-
public static function result($secret_key, $g_recaptcha_response)
43+
public static function result($secret_key, $g_recaptcha_response, $recaptcha_score=0.5)
4444
{
4545
$google_recaptcha_uri = 'https://www.google.com/recaptcha/api/siteverify';
4646

@@ -69,8 +69,11 @@ public static function result($secret_key, $g_recaptcha_response)
6969

7070
$response = json_decode($rs);
7171

72+
//var_dump($response); exit();// un-comment for debug
73+
7274
if (!$response || $response->success == false) {
73-
//var_dump($response); // un-comment for debug
75+
return false;
76+
} else if ( isset($response->score) && $response->score < $recaptcha_score ) {
7477
return false;
7578
} else {
7679
return true;
@@ -89,9 +92,12 @@ public static function result($secret_key, $g_recaptcha_response)
8992
*/
9093
public static function show($site_key, $after_field_id = 'Form_ContactForm_Comment', $debug = 'no_debug', $extra_class = "mt-4 mb-4", $please_tick_msg = "Please tick the I'm not robot checkbox")
9194
{
95+
if ( $please_tick_msg == 'v3'){
96+
return Self::showV3($site_key, $after_field_id, $debug, $extra_class);
97+
}
9298
$debug_alert = ($debug == 'no_debug') ? 'false' : 'true';
9399
$str = <<<EOF
94-
<!-- Start of the Google Recaptcha code -->
100+
<!-- Start of the Google Recaptcha v2 code -->
95101
96102
<script src='https://www.google.com/recaptcha/api.js'></script>
97103
<script>
@@ -135,6 +141,79 @@ function alexFindClosestNode(el, selector) {
135141
}
136142

137143

144+
/**
145+
* show recaptcha v3 function without jQuery
146+
*
147+
* @param string $site_key
148+
* @param string $after_field_id
149+
* @param string $debug
150+
* @param string $extra_class
151+
* @param string $please_tick_msg
152+
* @return void
153+
*/
154+
public static function showV3($site_key, $after_field_id = 'Form_ContactForm_Comment', $debug = 'no_debug', $extra_class = "mt-4 mb-4")
155+
{
156+
$debug_mode = ($debug == 'no_debug') ? '' : 'return false; // debug mode is on ';
157+
158+
if ( strpos($extra_class, 'show-inline-badge') !== false ){
159+
$api_js = "https://www.google.com/recaptcha/api.js?render=explicit&onload=alexGetRecaptchaValue";
160+
$recaptcha_client = "var recaptchaClient = grecaptcha.render('CustomContactUsForm-inline-badge', {
161+
'sitekey': '$site_key',
162+
'badge': 'inline',
163+
'size': 'invisible'
164+
});";
165+
} else {
166+
$api_js = "https://www.google.com/recaptcha/api.js?render=$site_key&onload=alexRecaptchaReadyCallback";
167+
$recaptcha_client = "var recaptchaClient = '$site_key';";
168+
}
169+
$str = <<<EOF
170+
<!-- Start of the Google Recaptcha v3 code -->
171+
172+
<script src="$api_js"></script>
173+
174+
175+
<script>
176+
177+
// Display google recaptcha v3
178+
var alexEL = document.getElementById('$after_field_id');
179+
alexEL.parentNode.insertAdjacentHTML('afterend', '<div id="CustomContactUsForm-inline-badge" class="inline-badge-div $extra_class"></div><input type="hidden" id="CustomContactUsForm-recaptcha" name="g-recaptcha-response">');
180+
181+
function alexGetRecaptchaValue(id) {
182+
$debug_mode
183+
if ( typeof(id)=='undefined' ) {
184+
id = 'CustomContactUsForm-recaptcha';
185+
}
186+
if ( document.getElementById(id).value == '' ) {
187+
188+
$recaptcha_client
189+
190+
grecaptcha.ready(function() {
191+
grecaptcha.execute(recaptchaClient, {
192+
action: 'CustomContactUsForm'
193+
}).then(function (token) {
194+
document.getElementById(id).value = token;
195+
});
196+
});
197+
}
198+
199+
}
200+
201+
setTimeout('alexGetRecaptchaValue("CustomContactUsForm-recaptcha")', 10000);
202+
203+
function alexRecaptchaReadyCallback() {
204+
alexEL.addEventListener('click',function(e){
205+
alexGetRecaptchaValue("CustomContactUsForm-recaptcha");
206+
});
207+
}
208+
209+
</script>
210+
211+
<!-- End of the Google Recaptcha code -->
212+
EOF;
213+
return $str;
214+
}
215+
216+
138217
/**
139218
* jqueryShow function
140219
*

0 commit comments

Comments
 (0)