|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spinzar\Firewall\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Spinzar\Firewall\Models\Log; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\IpUtils; |
|
7
|
|
|
|
|
8
|
|
|
trait Helper |
|
9
|
|
|
{ |
|
10
|
|
|
public function isEnabled($middleware = null) |
|
11
|
|
|
{ |
|
12
|
|
|
$middleware = $middleware ?? $this->middleware; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
return config('firewall.middleware.' . $middleware . '.enabled', config('firewall.enabled')); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function isDisabled($middleware = null) |
|
18
|
|
|
{ |
|
19
|
|
|
return !$this->isEnabled($middleware); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function isWhitelist() |
|
23
|
|
|
{ |
|
24
|
|
|
return IpUtils::checkIp($this->ip(), config('firewall.whitelist')); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function isMethod($middleware = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$middleware = $middleware ?? $this->middleware; |
|
30
|
|
|
|
|
31
|
|
|
if (!$methods = config('firewall.middleware.' . $middleware . '.methods')) { |
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (in_array('all', $methods)) { |
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return in_array(strtolower($this->request->method()), $methods); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function isRoute($middleware = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$middleware = $middleware ?? $this->middleware; |
|
45
|
|
|
|
|
46
|
|
|
if (!$routes = config('firewall.middleware.' . $middleware . '.routes')) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
foreach ($routes['except'] as $ex) { |
|
51
|
|
|
if (!$this->request->is($ex)) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
foreach ($routes['only'] as $on) { |
|
59
|
|
|
if ($this->request->is($on)) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function isInput($name, $middleware = null) |
|
70
|
|
|
{ |
|
71
|
|
|
$middleware = $middleware ?? $this->middleware; |
|
72
|
|
|
|
|
73
|
|
|
if (!$inputs = config('firewall.middleware.' . $middleware . '.inputs')) { |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (!empty($inputs['only']) && !in_array((string) $name, (array) $inputs['only'])) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return !in_array((string) $name, (array) $inputs['except']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function log($middleware = null, $user_id = null, $level = 'medium') |
|
85
|
|
|
{ |
|
86
|
|
|
$middleware = $middleware ?? $this->middleware; |
|
87
|
|
|
$user_id = $user_id ?? $this->user_id; |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$log = config('firewall.models.log', Log::class); |
|
90
|
|
|
return $log::create([ |
|
91
|
|
|
'ip' => $this->ip(), |
|
92
|
|
|
'level' => $level, |
|
93
|
|
|
'middleware' => $middleware, |
|
94
|
|
|
'user_id' => $user_id, |
|
95
|
|
|
'url' => $this->request->fullUrl(), |
|
96
|
|
|
'referrer' => $this->request->server('HTTP_REFERER') ?: 'NULL', |
|
97
|
|
|
'request' => urldecode(http_build_query($this->request->input())), |
|
98
|
|
|
]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function ip() |
|
102
|
|
|
{ |
|
103
|
|
|
if ($cf_ip = $this->request->header('CF_CONNECTING_IP')) { |
|
104
|
|
|
$ip = $cf_ip; |
|
105
|
|
|
} else { |
|
106
|
|
|
$ip = $this->request->ip(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $ip; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: