Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kilrizzy committed Mar 7, 2019
0 parents commit 9a74ab6
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.idea
/vendor
/build
.phpunit.result.cache
composer.lock
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: laravel
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php

php:
- 5.6
- 7.1
- 7.2
- 7.3

env:
matrix:
- COMPOSER_FLAGS="--prefer-lowest"
- COMPOSER_FLAGS=""

before_script:
- travis_retry composer update

script:
- vendor/bin/phpunit
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2019-03-07
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Binary Cabin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Recaptcha

A laravel package for easy ReCAPTCHA integration

## Installation

```
composer require binarycabin/recaptcha
```

Publish your configuration file

```
php artisan vendor:publish --provider="BinaryCabin\Recaptcha\Providers\RecaptchaServiceProvider" --tag="config"
```

Update your environment variables:

```
RECAPTCHA_VERSION=3
RECAPTCHA_SITE_KEY=""
RECAPTCHA_SECRET_KEY=""
```

For local sites or test environments you can also disable recaptcha verification using:

```
RECAPTCHA_ENABLE=false
```

## Usage

At the end of your page, add the scripts needed for Google Recaptcha:

```
{!! Recaptcha::scripts() !!}
```

And within your form, add the hidden input that will contain your recaptcha token:

```
{!! Recaptcha::hiddenInput() !!}
```

Finally, add the validation in your controller:

```php
$this->validate($request, [
'recaptcha-token' => 'recaptcha',
]);
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License
[MIT](https://choosealicense.com/licenses/mit/)
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "binarycabin/recaptcha",
"description": "A laravel package for easy ReCAPTCHA integration",
"keywords": ["laravel"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Jeff Kilroy",
"email": "[email protected]"
}
],
"require": {},
"autoload":{
"psr-4": {
"BinaryCabin\\Recaptcha\\": "src"
}
},
"autoload-dev":{
"psr-4": {
"BinaryCabin\\Recaptcha\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"BinaryCabin\\Recaptcha\\Providers\\RecaptchaServiceProvider"
],
"aliases": {
"Recaptcha": "BinaryCabin\\Recaptcha\\Facades\\RecaptchaFacade"
}
}
},
"require-dev": {
"phpunit/phpunit": ">5.0"
}
}
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-text" target="build/coverage.txt" />
</logging>
</phpunit>
47 changes: 47 additions & 0 deletions src/Configuration/RecaptchaConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace BinaryCabin\Recaptcha\Configuration;

class RecaptchaConfig
{
private $recaptchaEnabled = true;
private $recaptchaVersion = '3';
private $recaptchaSiteKey = null;
private $recaptchaSecretKey = null;

public function __construct() {
$configArray = app('config')->get('recaptcha');
if(isset($configArray['enable'])){
$this->recaptchaEnabled = $configArray['enable'];
}
if(isset($configArray['version'])){
$this->recaptchaVersion = $configArray['version'];
}
if(!empty($configArray['site_key'])){
$this->recaptchaSiteKey = $configArray['site_key'];
}
if(!empty($configArray['secret_key'])){
$this->recaptchaSecretKey = $configArray['secret_key'];
}
}
public function getRecaptchaEnabled()
{
return $this->recaptchaEnabled;
}

public function getRecaptchaVersion()
{
return $this->recaptchaVersion;
}

public function getRecaptchaSiteKey()
{
return $this->recaptchaSiteKey;
}

public function getRecaptchaSecretKey()
{
return $this->recaptchaSecretKey;
}

}
13 changes: 13 additions & 0 deletions src/Configuration/Templates/recaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [

'enable' => env('RECAPTCHA_ENABLE', true),

'version' => env('RECAPTCHA_VERSION', 3),

'site_key' => env('RECAPTCHA_SITE_KEY', null),

'secret_key' => env('RECAPTCHA_SECRET_KEY', null),

];
15 changes: 15 additions & 0 deletions src/Facades/RecaptchaFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace BinaryCabin\Recaptcha\Facades;

use Illuminate\Support\Facades\Facade;

class RecaptchaFacade extends Facade
{

protected static function getFacadeAccessor()
{
return 'recaptcha';
}

}
54 changes: 54 additions & 0 deletions src/Providers/RecaptchaServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace BinaryCabin\Recaptcha\Providers;

use BinaryCabin\Recaptcha\Configuration\RecaptchaConfig;
use BinaryCabin\Recaptcha\Recaptcha;
use Illuminate\Support\ServiceProvider;

class RecaptchaServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->addValidator();
$this->publishes([
__DIR__.'/../Configuration/Templates/recaptcha.php' => config_path('recaptcha.php'),
],'config');
$this->loadViewsFrom(__DIR__.'/../views', 'recaptcha');
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->bindRecaptcha();
}

protected function bindRecaptcha()
{
$this->app->bind('recaptcha', function () {
return new Recaptcha(new RecaptchaConfig());
});
}

/**
* Extends Validator to include a recaptcha type
*/
public function addValidator()
{
$this->app->validator->extendImplicit('recaptcha', function ($attribute, $value, $parameters) {
$recaptcha = app('recaptcha');
$challenge = app('request')->input($recaptcha->getInputName());
return $recaptcha->check($challenge, $value);
}, 'Please ensure that you are a human!');
}

}
Loading

0 comments on commit 9a74ab6

Please sign in to comment.