├── .editorconfig ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── botscout.php └── src ├── BotScout.php ├── BotScoutFacade.php ├── BotScoutServiceProvider.php └── BotScoutValidator.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | linting: true 4 | 5 | disabled: 6 | - single_class_element_per_statement 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-botscout` will be documented in this file 4 | 5 | ## 1.1.0 - 2019-06-07 6 | 7 | - Laravel 5.8 compatibility 8 | 9 | ## 1.1.0 - 2017-09-26 10 | 11 | - Laravel 5.5 compatibility 12 | 13 | ## 1.0.1 - 2017-02-13 14 | 15 | - Security fix 16 | 17 | 18 | ## 1.0.0 - 2017-02-13 19 | 20 | - Initial release 21 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Nicolas Beauvais 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel BotScout 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/nicolasbeauvais/laravel-botscout.svg?style=flat-square)](https://packagist.org/packages/nicolasbeauvais/laravel-botscout) 4 | [![Build Status](https://img.shields.io/travis/nicolasbeauvais/laravel-botscout/master.svg?style=flat-square)](https://travis-ci.org/nicolasbeauvais/laravel-botscout) 5 | [![SensioLabsInsight](https://insight.sensiolabs.com/projects/005620f8-d154-41f1-bc9b-4c27a1cf36ab/mini.png)](https://insight.sensiolabs.com/projects/005620f8-d154-41f1-bc9b-4c27a1cf36ab) 6 | [![Quality Score](https://img.shields.io/scrutinizer/g/nicolasbeauvais/laravel-botscout.svg?style=flat-square)](https://scrutinizer-ci.com/g/nicolasbeauvais/laravel-botscout) 7 | [![Total Downloads](https://img.shields.io/packagist/dt/nicolasbeauvais/laravel-botscout.svg?style=flat-square)](https://packagist.org/packages/nicolasbeauvais/laravel-botscout) 8 | 9 | ![bs_logo_full](https://cloud.githubusercontent.com/assets/2951704/22866541/8c6ddd80-f178-11e6-8a94-ded54a0b109a.gif) 10 | 11 | Protect your website against automated scripts using the [botscout.com](http://botscout.com/) API. 12 | 13 | ## Installation 14 | 15 | You can install the package via composer: 16 | 17 | ``` bash 18 | composer require nicolasbeauvais/laravel-botscout 19 | ``` 20 | 21 | Next, you must install the service provider: 22 | 23 | ```php 24 | // config/app.php 25 | 'providers' => [ 26 | ... 27 | NicolasBeauvais\LaravelBotScout\BotScoutServiceProvider::class, 28 | ]; 29 | ``` 30 | 31 | Add your [botscout.com](http://botscout.com/getkey.htm) api key to the `.env` file: 32 | ```bash 33 | BOTSCOUT_SECRET=your-api-key 34 | ``` 35 | 36 | If needed you can also publish the config file: 37 | ```bash 38 | php artisan vendor:publish --provider="NicolasBeauvais\LaravelBotScout\BotScoutServiceProvider" --tag="config" 39 | ``` 40 | 41 | If you want to make use of the facade you must install it as well: 42 | 43 | ```php 44 | // config/app.php 45 | 'aliases' => [ 46 | ... 47 | 'BotScout' => NicolasBeauvais\LaravelBotScout\BotScoutFacade::class, 48 | ]; 49 | ``` 50 | 51 | ## Usage 52 | 53 | You are highly advised to read the [BotScout.com API guide](http://botscout.com/api.htm) to understand the meaning of 54 | each method. 55 | 56 | ### Validator 57 | 58 | You can easily use botscout in your existing validators: 59 | 60 | ``` php 61 | // Validate name 62 | $validator = Validator::make(['name' => 'John Doe'], [ 63 | 'name' => 'required|botscout_name' 64 | ]); 65 | 66 | // Validate email 67 | $validator = Validator::make(['email' => 'toto@gmail.com'], [ 68 | 'email' => 'required|botscout_mail' 69 | ]); 70 | 71 | // Validate ip 72 | $validator = Validator::make(['ip' => '127.0.0.1'], [ 73 | 'ip' => 'required|botscout_ip' 74 | ]); 75 | ``` 76 | 77 | Note that you will need to create the validation message by yourself, as described in the [Laravel documentation](https://laravel.com/docs/5.5/validation#custom-error-messages). 78 | 79 | ### Facade 80 | 81 | You can use the BotScout facade anywhere in your app: 82 | 83 | ```php 84 | BotScout::multi('John Doe', 'email@test.com', '127.0.0.1')->isValid(); 85 | 86 | BotScout::all('John Doe')->isValid(); 87 | 88 | BotScout::name('John Doe')->isValid(); 89 | 90 | BotScout::mail('email@test.com')->isValid(); 91 | 92 | BotScout::ip('127.0.0.1')->isValid(); 93 | 94 | // We also include a quick way of testing a user with integrated exception catch 95 | BotScout::check('John Doe', 'email@test.com', '127.0.0.1'); // true or false 96 | ``` 97 | ### Real life example using the check method 98 | 99 | The `check` method is the recommended way to validate a register form: 100 | 101 | >The `check` method is a wrapper to the `multi`method that catch any http error / timeout. If the botscout api is not responding, the method will return false. 102 | 103 | ```php 104 | // Create a classic validation 105 | $validator = Validator::make($request->all(), [ 106 | 'email' => 'required|email|unique:users', 107 | 'name' => 'required|max:20', 108 | ]); 109 | 110 | $validator->after(function ($validator) { 111 | if (!BotScout::check($request->get('name'), $request->get('email'), $request->ip())) { 112 | $validator->errors()->add('email', 'Sorry, it looks like your a bot!'); 113 | } 114 | }); 115 | ``` 116 | 117 | ## Changelog 118 | 119 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 120 | 121 | ## Testing 122 | 123 | ``` bash 124 | $ composer test 125 | ``` 126 | 127 | ## Contributing 128 | 129 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 130 | 131 | ## Security 132 | 133 | If you discover any security related issues, please email nicolasbeauvais1@gmail.com instead of using the issue tracker. 134 | 135 | ## Credits 136 | 137 | - [Nicolas Beauvais](https://github.com/nicolasbeauvais) 138 | - [All Contributors](../../contributors) 139 | 140 | ## License 141 | 142 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 143 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nicolasbeauvais/laravel-botscout", 3 | "description": "botscout.com protection for laravel", 4 | "keywords": [ 5 | "botscout", 6 | "laravel", 7 | "bot", 8 | "protection" 9 | ], 10 | "homepage": "https://github.com/nicolasbeauvais/laravel-botscout", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Nicolas Beauvais", 15 | "email": "nicolasbeauvais1@gmail.com", 16 | "homepage": "http://nicolas-beauvais.com", 17 | "role": "Developer" 18 | } 19 | ], 20 | "require": { 21 | "php": "^7.0", 22 | "illuminate/contracts": "~5.1.0|~5.2.0|~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0", 23 | "illuminate/support": "~5.1.0|~5.2.0|~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0", 24 | "nicolasbeauvais/botscout-client": "^1.0" 25 | }, 26 | "require-dev": { 27 | "orchestra/testbench": "~3.5.0", 28 | "phpunit/phpunit": "^6.0", 29 | "mockery/mockery": "^0.9.8" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "NicolasBeauvais\\LaravelBotScout\\": "src" 34 | } 35 | }, 36 | "autoload-dev": { 37 | "psr-4": { 38 | "NicolasBeauvais\\LaravelBotScout\\Test\\": "tests" 39 | } 40 | }, 41 | "scripts": { 42 | "test": "vendor/bin/phpunit" 43 | }, 44 | "config": { 45 | "sort-packages": true 46 | }, 47 | "extra": { 48 | "laravel": { 49 | "providers": [ 50 | "NicolasBeauvais\\LaravelBotScout\\BotScoutServiceProvider" 51 | ], 52 | "aliases": { 53 | "BotScout": "NicolasBeauvais\\LaravelBotScout\\BotScoutFacade" 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /config/botscout.php: -------------------------------------------------------------------------------- 1 | env('BOTSCOUT_SECRET'), 9 | ]; 10 | -------------------------------------------------------------------------------- /src/BotScout.php: -------------------------------------------------------------------------------- 1 | botScoutClient = $client; 20 | } 21 | 22 | /** 23 | * Check based on the "multi" method with a failsafe. 24 | * 25 | * @param string $name 26 | * @param string $mail 27 | * @param string $ip 28 | * 29 | * @return bool 30 | */ 31 | public function check(string $name = null, string $mail = null, string $ip = null) : bool 32 | { 33 | try { 34 | return $this->botScoutClient->multi($name, $mail, $ip)->isValid(); 35 | } catch (\Exception $exception) { 36 | return false; 37 | } 38 | } 39 | 40 | /** 41 | * Test matches all parameters at once. 42 | * 43 | * @param string $name 44 | * @param string $mail 45 | * @param string $ip 46 | * 47 | * @return \NicolasBeauvais\BotScout\BotScoutResponse 48 | */ 49 | public function multi(string $name = null, string $mail = null, string $ip = null) 50 | { 51 | return $this->botScoutClient->multi($name, $mail, $ip); 52 | } 53 | 54 | /** 55 | * Test matches a single item against all fields in the botscout database. 56 | * 57 | * @param string $all 58 | * 59 | * @return \NicolasBeauvais\BotScout\BotScoutResponse 60 | */ 61 | public function all(string $all) 62 | { 63 | return $this->botScoutClient->all($all); 64 | } 65 | 66 | /** 67 | * Test matches a name. 68 | * 69 | * @param string $name 70 | * 71 | * @return \NicolasBeauvais\BotScout\BotScoutResponse 72 | */ 73 | public function name(string $name = null) 74 | { 75 | return $this->botScoutClient->name($name); 76 | } 77 | 78 | /** 79 | * Test matches an email. 80 | * 81 | * @param string $mail 82 | * 83 | * @return \NicolasBeauvais\BotScout\BotScoutResponse 84 | */ 85 | public function mail(string $mail = null) 86 | { 87 | return $this->botScoutClient->mail($mail); 88 | } 89 | 90 | /** 91 | * Test matches an ip. 92 | * 93 | * @param string $ip 94 | * 95 | * @return \NicolasBeauvais\BotScout\BotScoutResponse 96 | */ 97 | public function ip(string $ip = null) 98 | { 99 | return $this->botScoutClient->ip($ip); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/BotScoutFacade.php: -------------------------------------------------------------------------------- 1 | runningInConsole()) { 17 | $this->publishes([ 18 | __DIR__.'/../config/botscout.php' => config_path('botscout.php'), 19 | ], 'config'); 20 | } 21 | 22 | app('validator')->extend('botscout_name', 'NicolasBeauvais\LaravelBotScout\BotScoutValidator@validateName'); 23 | app('validator')->extend('botscout_mail', 'NicolasBeauvais\LaravelBotScout\BotScoutValidator@validateMail'); 24 | app('validator')->extend('botscout_ip', 'NicolasBeauvais\LaravelBotScout\BotScoutValidator@validateIp'); 25 | } 26 | 27 | /** 28 | * Register the application services. 29 | */ 30 | public function register() 31 | { 32 | $this->mergeConfigFrom(__DIR__.'/../config/botscout.php', 'botscout'); 33 | 34 | $this->app->singleton('laravel-botscout', function (...$arguments) { 35 | return new BotScout(new BotScoutClient(new Client(), config('botscout.api_key'))); 36 | }); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/BotScoutValidator.php: -------------------------------------------------------------------------------- 1 | botScout = app('laravel-botscout'); 16 | } 17 | 18 | public function validateName($message, $attribute, $rule, $parameters) : bool 19 | { 20 | return $this->botScout->name($attribute)->isValid(); 21 | } 22 | 23 | public function validateMail($message, $attribute, $rule, $parameters) : bool 24 | { 25 | return $this->botScout->mail($attribute)->isValid(); 26 | } 27 | 28 | public function validateIp($message, $attribute, $rule, $parameters) : bool 29 | { 30 | return $this->botScout->ip($attribute)->isValid(); 31 | } 32 | } 33 | --------------------------------------------------------------------------------