1
1
<?php
2
2
3
- namespace Potelo \LaravelBlockBots ;
3
+ namespace Potelo \LaravelBlockBots \ Middleware ;
4
4
5
5
use Closure ;
6
6
use Illuminate \Support \Facades \Redirect ;
10
10
11
11
use Carbon \Carbon ;
12
12
use Potelo \LaravelBlockBots \CheckIfBotIsReal ;
13
+ use Symfony \Component \HttpKernel \Exception \HttpException ;
13
14
14
15
class BlockBots
15
16
{
16
17
protected $ config ;
17
18
18
19
public function __construct ()
19
20
{
20
- $ this ->config = config ('block ' );
21
- }
22
-
23
- private function enabled ()
24
- {
25
- return $ this ->config ['enabled ' ];
21
+ $ this ->config = config ('block-bots ' );
26
22
}
27
23
28
24
/**
@@ -33,22 +29,21 @@ private function enabled()
33
29
*
34
30
* @return mixed
35
31
*/
36
- public function handle ($ request , Closure $ next , $ dailyLimit, $ redirectUrl )
32
+ public function handle ($ request , Closure $ next , $ dailyLimit )
37
33
{
34
+ try {
35
+ $ blocked = $ this ->blocked ($ request , $ dailyLimit );
38
36
39
- if ($ this ->enabled () && ($ request ->getRequestUri () != $ redirectUrl )) {
40
-
41
- try {
42
- $ blocked = $ this ->blocked ($ request , $ dailyLimit );
43
-
44
- } catch (Exception $ e ) {
45
- Log::error ("[Block-Bots] Error at handling request: {$ e ->getMessage ()}" );
46
- $ blocked = false ;
47
- }
37
+ } catch (Exception $ e ) {
38
+ Log::error ("[Block-Bots] Error at handling request: {$ e ->getMessage ()}" );
39
+ $ blocked = false ;
40
+ }
48
41
49
- if ($ blocked ) {
50
- return Redirect::to ($ redirectUrl );
42
+ if ($ blocked ) {
43
+ if ($ request ->expectsJson ()) {
44
+ return response ()->json (['message ' => 'You are over the specified limit. ' ], 403 );
51
45
}
46
+ return response (view ('block-bots::error ' ), 403 );
52
47
}
53
48
54
49
return $ next ($ request );
@@ -160,6 +155,9 @@ public function isWhitelisted($ip, $user_agent)
160
155
161
156
// If we got here, it is an unverified bot. Lets create a job to test it
162
157
\Potelo \LaravelBlockBots \Jobs \CheckIfBotIsReal::dispatch ($ ip , $ user_agent );
158
+ Redis::sadd ($ key_pending_bot , $ ip );
159
+ return true ;
160
+
163
161
164
162
}
165
163
0 commit comments