Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to last version of original package #1

Merged
merged 46 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
32290c8
Update README.md
albertcht Oct 22, 2017
34f2a9e
Update README.md
albertcht Nov 5, 2017
7657761
Update README.md
albertcht Dec 3, 2017
f3e02c2
fix captcha blade directive
albertcht Dec 24, 2017
8881e6e
Merge pull request #58 from albertcht/develop
albertcht Dec 24, 2017
5fe18ce
Update README.md
albertcht Dec 25, 2017
6ed594c
Update README.md
albertcht Jan 30, 2018
e0b4c3a
Add support for Laravel 5.6
neamtua Feb 8, 2018
bce2ceb
Merge pull request #69 from neamtua/master
albertcht Feb 12, 2018
ad226a6
Update README.md
albertcht Feb 25, 2018
0e3f4ae
Update README.md
QuentinBontemps Feb 28, 2018
d8fe62d
Merge pull request #73 from QuentinBontemps/patch-1
albertcht Feb 28, 2018
c85f7b7
remove author email info
albertcht Mar 21, 2018
507c378
Pass form submit event to _beforeSubmit callback.
Marko-M Mar 30, 2018
17718f6
Update docs to reflect availability of e inside _beforeSubmit callback.
Marko-M Mar 30, 2018
feef498
Adds partial render methods and corresponding blade directives for fr…
danmatthews Apr 4, 2018
87eca4d
readme updated
danmatthews Apr 4, 2018
8afb79b
updated heading
danmatthews Apr 4, 2018
96ab1ab
Merge pull request #78 from Marko-M/feature/propagate-form-submit
albertcht Apr 7, 2018
b8f15ea
Add Beerpay's badge
albertcht Apr 29, 2018
c659e18
try to avoid conflict with window.onload event
albertcht Apr 29, 2018
c8481a2
Fix typos in phpdoc
emanuelmutschlechner May 11, 2018
7daa583
Merge pull request #82 from emanuelmutschlechner/patch-1
albertcht May 15, 2018
6a34a1c
Merge pull request #83 from albertcht/develop
albertcht May 15, 2018
fabbd62
Update README.md
albertcht Jun 20, 2018
b388e45
Update composer.json
danmatthews Jun 28, 2018
401a6b9
Update composer.json
BSN4 Aug 18, 2018
8aa950b
Merge pull request #90 from if4lcon/master
albertcht Aug 19, 2018
8b20d25
Fix syntax in README.
BrandonSurowiec Nov 9, 2018
5273a8e
Merge pull request #101 from BrandonSurowiec/patch-1
albertcht Dec 19, 2018
daa1f1e
Reverted composer.json
danmatthews Jan 11, 2019
9aeff52
Removed unnecessary semicolons.
danmatthews Jan 11, 2019
f7dcfc5
Merge branch 'master' into master
danmatthews Jan 11, 2019
37d45f0
Merge pull request #79 from danmatthews/master
albertcht Jan 26, 2019
d61b56f
Adding support for Laravel 5.8
sumityadav Feb 27, 2019
9bdb39c
Added php versions 7.2 and 7.3
sumityadav Feb 27, 2019
3b845c7
Merge pull request #105 from sumityadav/laravel-5.8-upgrade
albertcht Mar 1, 2019
5e5f064
remove constraints for laravel versions in composer.json
albertcht Mar 1, 2019
7aae21d
Fix unused $lang param for captchaPolyfill
albertcht Jun 13, 2019
5bfe170
add support laravel v6
garbinmarcelo Sep 4, 2019
8dfdf33
fix phpunit v8
garbinmarcelo Sep 4, 2019
e4c74eb
update composer phpunit and travis php
garbinmarcelo Sep 4, 2019
fbb6963
change 6.0.* to ^6.0
garbinmarcelo Sep 5, 2019
0e03f61
Merge pull request #115 from marcelogarbin/support-laravel6
albertcht Sep 5, 2019
8f95cc2
Add support for Laravel 7
hedii Mar 3, 2020
dd74d80
Merge pull request #123 from hedii/laravel7
albertcht Mar 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
composer.lock
.DS_Store
.idea
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: php

php:
- '7.0'
- '7.1'
- '7.2'
- '7.3'

install:
- travis_retry composer install --no-interaction --prefer-dist --no-suggest
Expand All @@ -12,4 +12,4 @@ before_script:
- travis_retry composer install --prefer-source --no-interaction --dev

script:
- vendor/bin/phpunit
- vendor/bin/phpunit
68 changes: 55 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Invisible reCAPTCHA
==========
![php-badge](https://img.shields.io/badge/php-%3E%3D%205.6-8892BF.svg)
[![packagist-badge](https://img.shields.io/packagist/v/albertcht/invisible-recaptcha.svg)](https://packagist.org/packages/albertcht/invisible-recaptcha)
[![Total Downloads](https://poser.pugx.org/albertcht/invisible-recaptcha/downloads)](https://packagist.org/packages/albertcht/invisible-recaptcha)
[![travis-badge](https://api.travis-ci.org/albertcht/invisible-recaptcha.svg?branch=master)](https://travis-ci.org/albertcht/invisible-recaptcha)

![invisible_recaptcha_demo](http://i.imgur.com/1dZ9XKn.png)
Expand All @@ -14,7 +15,6 @@ In reCAPTCHA v2, users need to click the button: "I'm not a robot" to prove they
## Notice

* The master branch doesn't support multi captchas feature, please use `multi-forms` branch if you need it. (**Most of the time you are misusing recaptcha when you try to put multiple captchas in one page.**)
* Please modify your configs parameter if you are not using this package with Laravel after you upgrade to `version 1.8`.

## Installation

Expand Down Expand Up @@ -62,34 +62,66 @@ INVISIBLE_RECAPTCHA_DEBUG=false

Before you render the captcha, please keep those notices in mind:

* `render()` function needs to be called within a form element.
* `render()` or `renderHTML()` function needs to be called within a form element.
* You have to ensure the `type` attribute of your submit button has to be `submit`.
* There can only be one submit button in your form.

##### Display reCAPTCHA in Your View

```php
{!! app('captcha')->render(); !!}
{!! app('captcha')->render() !!}

// or you can use this in blade
@captcha()
@captcha
```

With custom language support:

```php
{!! app('captcha')->render($lang = null); !!}
{!! app('captcha')->render('en') !!}

// or you can use this in blade
@captcha($lang = null)
@captcha('en')
```

##### Usage with Javascript frameworks like VueJS:

The `render()` process includes three distinct sections that can be rendered separately incase you're using the package with a framework like VueJS which throws console errors when `<script>` tags are included in templates.

You can render the polyfill (do this somewhere like the head of your HTML:)

```php
{!! app('captcha')->renderPolyfill() !!}
// Or with blade directive:
@captchaPolyfill
```

You can render the HTML using this following, this needs to be INSIDE your `<form>` tag:

```php
{!! app('captcha')->renderCaptchaHTML() !!}
// Or with blade directive:
@captchaHTML
```

And you can render the neccessary `<script>` tags including the optional language support by using:

```php
// The argument is optional.
{!! app('captcha')->renderFooterJS('en') !!}

// Or with blade directive:
@captchaScripts
// blade directive, with language support:
@captchaScripts('en')

```

##### Validation

Add `'g-recaptcha-response' => 'required|captcha'` to rules array.

```php

$validate = Validator::make(Input::all(), [
'g-recaptcha-response' => 'required|captcha'
]);
Expand All @@ -108,7 +140,7 @@ add lines in application/config/config.php :
$config['recaptcha.sitekey'] = 'sitekey';
$config['recaptcha.secret'] = 'secretkey';
// optional
$config['recaptcha.options'] [
$config['recaptcha.options'] = [
'hideBadge' => false,
'dataBadge' => 'bottomright',
'timeout' => 5,
Expand All @@ -132,7 +164,7 @@ In view, in your form:

Then back in your controller you can verify it:
```php
$captcha->verifyResponse($_POST['g-recaptcha-response']);
$captcha->verifyResponse($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
```

## Without Laravel or CodeIgniter
Expand All @@ -147,7 +179,7 @@ require_once "vendor/autoload.php";
$siteKey = 'sitekey';
$secretKey = 'secretkey';
// optional
$options [
$options = [
'hideBadge' => false,
'dataBadge' => 'bottomright',
'timeout' => 5,
Expand All @@ -159,7 +191,7 @@ $captcha = new \AlbertCht\InvisibleReCaptcha\InvisibleReCaptcha($siteKey, $secre
$captcha->setOption('debug', true);

if (!empty($_POST)) {
var_dump($captcha->verifyResponse($_POST['g-recaptcha-response']));
var_dump($captcha->verifyResponse($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']));
exit();
}

Expand All @@ -175,9 +207,10 @@ if (!empty($_POST)) {
Use this function only when you need to take all control after clicking submit button. Recaptcha validation will not be triggered if you return false in this function.

```javascript
_beforeSubmit = function() {
_beforeSubmit = function(e) {
console.log('submit button clicked.');
// do other things before captcha validation
// e represents reference to original form submit event
// return true if you want to continue triggering captcha validation, otherwise return false
return false;
}
Expand All @@ -195,7 +228,7 @@ _submitEvent = function() {
_submitForm();
}
```
Here's am example to use an ajax submit (using jquery selector)
Here's an example to use an ajax submit (using jquery selector)
```javascript
_submitEvent = function() {
$.ajax({
Expand All @@ -218,6 +251,10 @@ _submitEvent = function() {
});
};
```
## Example Repository
Repo: https://github.com/albertcht/invisible-recaptcha-example

This repo demonstrates how to use this package with ajax way.

## Showcases

Expand All @@ -227,3 +264,8 @@ _submitEvent = function() {

* anhskohbo (the author of no-captcha package)
* [Contributors](https://github.com/albertcht/invisible-recaptcha/graphs/contributors)

## Support on Beerpay
Hey dude! Help me out for a couple of :beers:!

[![Beerpay](https://beerpay.io/albertcht/invisible-recaptcha/badge.svg?style=beer-square)](https://beerpay.io/albertcht/invisible-recaptcha) [![Beerpay](https://beerpay.io/albertcht/invisible-recaptcha/make-wish.svg?style=flat-square)](https://beerpay.io/albertcht/invisible-recaptcha?focus=wish)
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"authors": [
{
"name": "Albert Chen",
"email": "albert.cht@gmail.com"
"homepage": "https://www.albert-chen.com"
}
],
"require": {
"php": "^5.6.4 || ^7.0",
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*",
"illuminate/support": "^5.0|^6.0|^7.0",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"phpunit/phpunit": "^6.1",
"illuminate/view": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*"
"phpunit/phpunit": "^8.0",
"illuminate/view": "^5.0|^6.0|^7.0"
},
"autoload": {
"psr-4": {
Expand Down
56 changes: 46 additions & 10 deletions src/InvisibleReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,64 @@ public function getPolyfillJs()
*/
public function render($lang = null)
{
$html = '<script src="' . $this->getPolyfillJs() . '"></script>' . PHP_EOL;
$html .= '<div id="_g-recaptcha"></div>' . PHP_EOL;
$html = $this->renderPolyfill();
$html .= $this->renderCaptchaHTML();
$html .= $this->renderFooterJS($lang);
return $html;
}

/**
* Render the polyfill JS components only.
*
* @return string
*/
public function renderPolyfill()
{
return '<script src="' . $this->getPolyfillJs() . '"></script>' . PHP_EOL;
}

/**
* Render the captcha HTML.
*
* @return string
*/
public function renderCaptchaHTML()
{
$html = '<div id="_g-recaptcha"></div>' . PHP_EOL;
if ($this->getOption('hideBadge', false)) {
$html .= '<style>.grecaptcha-badge{display:none;!important}</style>' . PHP_EOL;
}

$html .= '<div class="g-recaptcha" data-sitekey="' . $this->siteKey .'" ';
$html .= 'data-size="invisible" data-callback="_submitForm" data-badge="' . $this->getOption('dataBadge', 'bottomright') . '"></div>';
$html .= '<script src="' . $this->getCaptchaJs($lang) . '" async defer></script>' . PHP_EOL;
return $html;
}

/**
* Render the footer JS neccessary for the recaptcha integration.
*
* @return string
*/
public function renderFooterJS($lang = null)
{
$html = '<script src="' . $this->getCaptchaJs($lang) . '" async defer></script>' . PHP_EOL;
$html .= '<script>var _submitForm,_captchaForm,_captchaSubmit,_execute=true;</script>';
$html .= '<script>window.onload=function(){';
$html .= "<script>window.addEventListener('load', _loadCaptcha);" . PHP_EOL;
$html .= "function _loadCaptcha(){";
if ($this->getOption('hideBadge', false)) {
$html .= "document.querySelector('.grecaptcha-badge').style = 'display:none;!important'" . PHP_EOL;
}
$html .= '_captchaForm=document.querySelector("#_g-recaptcha").closest("form");';
$html .= "_captchaSubmit=_captchaForm.querySelector('[type=submit]');";
$html .= '_submitForm=function(){if(typeof _submitEvent==="function"){_submitEvent();';
$html .= 'grecaptcha.reset();}else{_captchaForm.submit();}};';
$html .= "_captchaForm.addEventListener('submit',";
$html .= "function(e){e.preventDefault();if(typeof _beforeSubmit==='function'){";
$html .= "_execute=_beforeSubmit();}if(_execute){grecaptcha.execute();}});";
$html .= "_execute=_beforeSubmit(e);}if(_execute){grecaptcha.execute();}});";
if ($this->getOption('debug', false)) {
$html .= $this->renderDebug();
}
$html .= "}</script>" . PHP_EOL;

return $html;
}

Expand Down Expand Up @@ -198,7 +234,7 @@ protected function sendVerifyRequest(array $query = [])
/**
* Getter function of site key
*
* @return strnig
* @return string
*/
public function getSiteKey()
{
Expand All @@ -208,7 +244,7 @@ public function getSiteKey()
/**
* Getter function of secret key
*
* @return strnig
* @return string
*/
public function getSecretKey()
{
Expand Down Expand Up @@ -239,7 +275,7 @@ public function setOption($key, $value)
/**
* Getter function of options
*
* @return strnig
* @return string
*/
public function getOptions()
{
Expand Down Expand Up @@ -272,7 +308,7 @@ public function setClient(Client $client)
/**
* Getter function of guzzle client
*
* @return strnig
* @return string
*/
public function getClient()
{
Expand Down
11 changes: 10 additions & 1 deletion src/InvisibleReCaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,16 @@ public function provides()
public function addBladeDirective(BladeCompiler $blade)
{
$blade->directive('captcha', function ($lang) {
return "<?php echo app('captcha')->render(" . ($lang ? "'$lang'" : '') . '); ?>';
return "<?php echo app('captcha')->render({$lang}); ?>";
});
$blade->directive('captchaPolyfill', function () {
return "<?php echo app('captcha')->renderPolyfill(); ?>";
});
$blade->directive('captchaHTML', function () {
return "<?php echo app('captcha')->renderCaptchaHTML(); ?>";
});
$blade->directive('captchaScripts', function ($lang) {
return "<?php echo app('captcha')->renderFooterJS({$lang}); ?>";
});
}
}
2 changes: 1 addition & 1 deletion tests/CaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CaptchaTest extends TestCase

protected $captcha;

protected function setUp()
protected function setUp(): void
{
$this->captcha = new InvisibleReCaptcha(
static::SITE_KEY,
Expand Down