Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 35 additions & 0 deletions src/Events/UserBlockedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Potelo\LaravelBlockBots\Events;

use Carbon\Carbon;
use Illuminate\Queue\SerializesModels;

class UserBlockedEvent
{
use SerializesModels;

public $user;

/** @var integer */
public $number_of_hits;

/** @var Carbon */
public $block_date;

/**
* Create a new event instance.
*
* @param $user
* @param integer $number_of_hits
* @param Carbon $block_date
*
* @return void
*/
public function __construct($user, $number_of_hits, $block_date)
{
$this->user = $user;
$this->number_of_hits = $number_of_hits;
$this->block_date = $block_date;
}
}
23 changes: 12 additions & 11 deletions src/Middleware/BlockBots.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Carbon\Carbon;
use Potelo\LaravelBlockBots\CheckIfBotIsReal;
use Potelo\LaravelBlockBots\Events\UserBlockedEvent;


class BlockBots
Expand Down Expand Up @@ -71,7 +72,8 @@ public function blocked($request, $dailyLimit)

$full_url = substr($request->fullUrl(), strlen($request->getScheme(). "://")); # Get the URL without scheme

$key_access_count = "block_bot:{$ip}";
$key_identifier = Auth::check() ? Auth::id() : $ip;
$key_access_count = "block_bot:{$key_identifier}";

$number_of_hits = 1;

Expand All @@ -95,23 +97,22 @@ public function blocked($request, $dailyLimit)
return false;
}
else{
if ($log_blocked_requests){
if ($log_blocked_requests){
$key_notified = "block_bot:notified:{$ip}";
if(!Redis::exists($key_notified))
{
if (!Redis::exists($key_notified)) {
$end_of_day_unix_timestamp = Carbon::tomorrow()->startOfDay()->timestamp;
Redis::set($key_notified, 1);
Redis::expireat($key_notified, $end_of_day_unix_timestamp);
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url);

}
if (Auth::guest()) {
\Potelo\LaravelBlockBots\Jobs\ProcessLogWithIpInfo::dispatch($ip, $user_agent, 'BLOCKED', $full_url);
}
}
}

if($fake_mode)
{
if ($fake_mode) {
return false;
}
else{
} else {
event(new UserBlockedEvent(Auth::user(), $number_of_hits, Carbon::now()));
return true;
}
}
Expand Down