1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\LaravelIpMiddleware; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Contracts\Config\Repository; |
8
|
|
|
use Illuminate\Contracts\Foundation\Application; |
9
|
|
|
|
10
|
|
|
abstract class Middleware |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Application |
14
|
|
|
*/ |
15
|
|
|
protected $application; |
16
|
|
|
/** |
17
|
|
|
* @var Repository |
18
|
|
|
*/ |
19
|
|
|
protected $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Middleware constructor. |
23
|
|
|
* |
24
|
|
|
* @param Application $application |
25
|
|
|
* @param Repository $config |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Application $application, Repository $config) |
28
|
|
|
{ |
29
|
|
|
$this->application = $application; |
30
|
|
|
$this->config = $config; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns client IP address. |
35
|
|
|
* |
36
|
|
|
* @param Request $request |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
protected function clientIp($request): string |
40
|
|
|
{ |
41
|
|
|
return $this->config->get('ip-middleware.custom_server_parameter') |
42
|
|
|
? $request->server->get($this->config->get('ip-middleware.custom_server_parameter')) |
43
|
|
|
: $request->ip(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return result if middleware should IP check. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
protected function shouldCheck(): bool |
52
|
|
|
{ |
53
|
|
|
return ! $this->application->environment($this->config->get('ip-middleware.ignore_environments')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns IP address list parsed from original middleware parameter. |
58
|
|
|
* |
59
|
|
|
* @param array<string> $list |
60
|
|
|
* |
61
|
|
|
* @return array<string> |
62
|
|
|
*/ |
63
|
|
|
protected function ipList(array $list): array |
64
|
|
|
{ |
65
|
|
|
$predefinedLists = $this->config->get('ip-middleware.predefined_lists'); |
66
|
|
|
|
67
|
|
|
$finalList = []; |
68
|
|
|
foreach (Arr::flatten($list) as $item) { |
69
|
|
|
if (isset($predefinedLists[$item])) { |
70
|
|
|
$finalList[] = $this->parsePredefinedListItem($predefinedLists[$item]); |
71
|
|
|
} else { |
72
|
|
|
$finalList[] = $item; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return Arr::flatten($finalList); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array<string>|string $item |
81
|
|
|
* |
82
|
|
|
* @return array<string> |
83
|
|
|
*/ |
84
|
|
|
private function parsePredefinedListItem($item): array |
85
|
|
|
{ |
86
|
|
|
if (is_array($item)) { |
87
|
|
|
return $item; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return explode(',', $item); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Aborts application with configured error code. |
95
|
|
|
*/ |
96
|
|
|
protected function abort() |
97
|
|
|
{ |
98
|
|
|
return $this->application->abort( |
99
|
|
|
$this->config->get('ip-middleware.error_code') |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|