|
1
|
|
|
<?php namespace Arcanedev\SpamBlocker\Http\Middleware; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\SpamBlocker\Contracts\SpamBlocker; |
|
4
|
|
|
use Closure; |
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class BlockReferralSpam |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\SpamBlocker\Http\Middleware |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class BlockReferralSpam |
|
14
|
|
|
{ |
|
15
|
|
|
/* ----------------------------------------------------------------- |
|
16
|
|
|
| Properties |
|
17
|
|
|
| ----------------------------------------------------------------- |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** @var \Arcanedev\SpamBlocker\Contracts\SpamBlocker */ |
|
21
|
|
|
protected $blocker; |
|
22
|
|
|
|
|
23
|
|
|
/* ----------------------------------------------------------------- |
|
24
|
|
|
| Constructor |
|
25
|
|
|
| ----------------------------------------------------------------- |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* BlockReferralSpam constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Arcanedev\SpamBlocker\Contracts\SpamBlocker $blocker |
|
32
|
|
|
*/ |
|
33
|
8 |
|
public function __construct(SpamBlocker $blocker) |
|
34
|
|
|
{ |
|
35
|
8 |
|
$this->blocker = $blocker; |
|
36
|
8 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/* ----------------------------------------------------------------- |
|
39
|
|
|
| Main Methods |
|
40
|
|
|
| ----------------------------------------------------------------- |
|
41
|
|
|
*/ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Handle an incoming request. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Illuminate\Http\Request $request |
|
47
|
|
|
* @param \Closure $next |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
8 |
|
public function handle(Request $request, Closure $next) |
|
52
|
|
|
{ |
|
53
|
8 |
|
$referer = $request->headers->get('referer'); |
|
54
|
|
|
|
|
55
|
8 |
|
return $this->blocker->isBlocked($referer) |
|
|
|
|
|
|
56
|
6 |
|
? $this->getBlockedResponse() |
|
57
|
8 |
|
: $next($request); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/* ----------------------------------------------------------------- |
|
61
|
|
|
| Other Methods |
|
62
|
|
|
| ----------------------------------------------------------------- |
|
63
|
|
|
*/ |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the blocked referer's response. |
|
67
|
|
|
* |
|
68
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
|
69
|
|
|
*/ |
|
70
|
6 |
|
protected function getBlockedResponse() |
|
71
|
|
|
{ |
|
72
|
6 |
|
return response('Unauthorized.', 401); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.