andrey-helldar/spammers

This package is abandoned and no longer maintained. The author suggests using the johannebert/laravel-spam-protector package instead.

Simple package to prevent accessing of spammers to Laravel application

v1.2.1 2020-12-30 19:52 UTC

This package is auto-updated.

Last update: 2020-12-30 20:01:34 UTC


README

Simple package to prevent accessing of spammers to Laravel application.

spammers

StyleCI Total Downloads Latest Stable Version Latest Unstable Version License

Installation

To get the latest version of this package, simply require the project using Composer:

$ composer require andrey-helldar/spammers

Instead, you may of course manually update your require block and run composer update if you so choose:

{
    "require": {
        "andrey-helldar/spammers": "^1.0"
    }
}

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php:

Helldar\Spammers\ServiceProvider::class,

Next, call php artisan vendor:publish and php artisan migrate commands after set up config/spammers.php file and install the migrations by running php artisan migrate command.

Now, use spammer() helper and Artisan commands.

Documentation

Helpers Store

Store IP-address in a spam-table:

spammer()
    ->ip('1.2.3.4')
    ->store();
    
// or

spammer('1.2.3.4')->store();

Delete IP-address from a spam-table:

spammer()
    ->ip('1.2.3.4')
    ->delete();
    
// or

spammer('1.2.3.4')->delete();

Restore IP-address from a spam-table:

spammer()
    ->ip('1.2.3.4')
    ->restore();
    
// or

spammer('1.2.3.4')->restore();

Check exists IP-address in a spam-table:

spammer()
    ->ip('1.2.3.4')
    ->exists();
    
// or

spammer('1.2.3.4')->exists();

Helpers Accessing

To save an IP address with the URL in the database, use the helper spammer()->access():

spammer()
    ->access()
    ->ip('1.2.3.4')
    ->url('/foo/bar')
    ->store();

// or

spammer('1.2.3.4')
    ->access()
    ->url('/foo/bar')
    ->store();

Ban when attempts to get pages with errors exceed a given number.

Example:

  • When the number of attempts reaches 100 - ban for 24 hours.
  • When the number of attempts reaches 300 - ban for 72 hours.
  • When the number of attempts reaches 500 - permanent ban.

Default, permanent ban.

Middleware

Next, add link to middleware in $routeMiddleware block in app/Http/Kernel.php file, and use him in $middlewareGroups blocks:

protected $middlewareGroups = [
    'web' => [
        // ...
        'spammers'
    ],

    'api' => [
        // ...
        'spammers'
    ],
];

protected $routeMiddleware = [
    // ...
    'spammers' => Helldar\Spammers\Middleware\Spammers::class
];

Or you can specify globally in the attribute $middleware of the Http/Kernel.php file:

protected $middleware = [
    // ...
    \Helldar\Spammers\Middleware\Spammers::class,    
];

Console Command

This package maybe called in a console:

spam:amnesty
spam:store 1.2.3.4
spam:delete 1.2.3.4
spam:restore 1.2.3.4
spam:exists 1.2.3.4

The spam:amnesty command allows you to delete IP-addresses that have expired.

Additional

You can use a Helldar\Spammers\Models\Spammer model. His extended Illuminate\Database\Eloquent\Model.

Simple Using

You can specify globally in the attribute $middleware of the Http/Kernel.php file:

protected $middleware = [
    // ...
    \Helldar\Spammers\Middleware\Spammers::class,    
];

Next, in the report() method of file app\Exceptions\Handler.php, add the call to the spammer()->access() helper:

public function report(Exception $exception)
{
    spammer(request()->ip())
        ->access()
        ->url(request()->fullUrl())
        ->store();

    parent::report($exception);
}

And add a rule to the schedule() method of the app/Console/Kernel.php file:

protected function schedule(Schedule $schedule)
{
    $schedule->command('spam:amnesty')
        ->everyThirtyMinutes();
}

Execute the commands:

php artisan vendor:publish
php artisan migrate

Customize the file config/spammers.php for your purposes.

Profit!

Copyright and License

Laravel Spammers was written by Andrey Helldar for the Laravel framework 5.4 or later, and is released under the MIT License. See the LICENSE file for details.

Translation

Translations of text and comment by Google Translate. Help with translation +1 in karma :)