1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Security\Protections; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | /** |
||
19 | * Class Firewall |
||
20 | * |
||
21 | * @package O2System\Security\Protections |
||
22 | */ |
||
23 | class Firewall |
||
24 | { |
||
25 | /** |
||
26 | * Firewall::$ipVersion |
||
27 | * |
||
28 | * Filter validation ip address version. |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $ipVersion = FILTER_FLAG_IPV4; |
||
33 | |||
34 | /** |
||
35 | * Firewall::$whitelist |
||
36 | * |
||
37 | * List of whitelist Ip Addresses. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $whitelistIpAddresses = []; |
||
42 | |||
43 | /** |
||
44 | * Firewall::$blacklist |
||
45 | * |
||
46 | * List of blacklist Ip Addresses. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $blacklistIpAddresses = []; |
||
51 | |||
52 | // ------------------------------------------------------------------------ |
||
53 | |||
54 | /** |
||
55 | * Firewall::setIpVersion |
||
56 | * |
||
57 | * Sets filter validation ip address version. |
||
58 | * |
||
59 | * @param $ipVersion |
||
60 | * |
||
61 | * @return static |
||
62 | */ |
||
63 | public function setIpVersion($ipVersion) |
||
64 | { |
||
65 | if (in_array($ipVersion, [FILTER_FLAG_IPV4, FILTER_FLAG_IPV6])) { |
||
66 | $this->ipVersion = $ipVersion; |
||
67 | } |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | // ------------------------------------------------------------------------ |
||
73 | |||
74 | /** |
||
75 | * Firewall::setWhitelistIpAddresses |
||
76 | * |
||
77 | * Sets whitelist ip addresses. |
||
78 | * |
||
79 | * @param array $ipAddresses List of whitelist ip addresses. |
||
80 | * |
||
81 | * @return static |
||
82 | */ |
||
83 | public function setWhitelistIpAddresses(array $ipAddresses) |
||
84 | { |
||
85 | foreach ($ipAddresses as $ipAddress) { |
||
86 | if ($this->isValid($ipAddress)) { |
||
87 | $this->whitelistIpAddresses[] = $ipAddress; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | // ------------------------------------------------------------------------ |
||
95 | |||
96 | /** |
||
97 | * Firewall::isValid |
||
98 | * |
||
99 | * Checks if the ip address is valid. |
||
100 | * |
||
101 | * @param string $ipAddress Ip Address. |
||
102 | * |
||
103 | * @return mixed |
||
104 | */ |
||
105 | protected function isValid($ipAddress) |
||
106 | { |
||
107 | return filter_var($ipAddress, FILTER_FLAG_IPV4); |
||
108 | } |
||
109 | |||
110 | // ------------------------------------------------------------------------ |
||
111 | |||
112 | /** |
||
113 | * Firewall::addWhitelistIpAddress |
||
114 | * |
||
115 | * Sets whitelist ip addresses. |
||
116 | * |
||
117 | * @param string $ipAddress Whitelist ip address. |
||
118 | * |
||
119 | * @return static |
||
120 | */ |
||
121 | public function addWhitelistIpAddress($ipAddress) |
||
122 | { |
||
123 | if ($this->isValid($ipAddress)) { |
||
124 | $this->whitelistIpAddresses[] = $ipAddress; |
||
125 | } |
||
126 | |||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | // ------------------------------------------------------------------------ |
||
131 | |||
132 | /** |
||
133 | * Firewall::setBlacklistIpAddresses |
||
134 | * |
||
135 | * Sets whitelist ip addresses. |
||
136 | * |
||
137 | * @param array $ipAddresses List of whitelist ip addresses. |
||
138 | * |
||
139 | * @return static |
||
140 | */ |
||
141 | public function setBlacklistIpAddresses(array $ipAddresses) |
||
142 | { |
||
143 | foreach ($ipAddresses as $ipAddress) { |
||
144 | if ($this->isValid($ipAddress)) { |
||
145 | $this->blacklistIpAddresses[] = $ipAddress; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | return $this; |
||
150 | } |
||
151 | |||
152 | // ------------------------------------------------------------------------ |
||
153 | |||
154 | /** |
||
155 | * Firewall::addBlacklistIpAddress |
||
156 | * |
||
157 | * Sets whitelist ip addresses. |
||
158 | * |
||
159 | * @param string $ipAddress Whitelist ip address. |
||
160 | * |
||
161 | * @return static |
||
162 | */ |
||
163 | public function addBlacklistIpAddress($ipAddress) |
||
164 | { |
||
165 | if ($this->isValid($ipAddress)) { |
||
166 | $this->whitelistIpAddresses[] = $ipAddress; |
||
167 | } |
||
168 | |||
169 | return $this; |
||
170 | } |
||
171 | |||
172 | // ------------------------------------------------------------------------ |
||
173 | |||
174 | /** |
||
175 | * Firewall::verify |
||
176 | * |
||
177 | * @param string $ipAddress |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function verify($ipAddress = null) |
||
182 | { |
||
183 | $ipAddress = isset($ipAddress) |
||
184 | ? $ipAddress |
||
185 | : input()->ipAddress(); |
||
0 ignored issues
–
show
|
|||
186 | |||
187 | if ($this->isWhitelisted($ipAddress) OR |
||
188 | $this->isBlacklisted($ipAddress) === false |
||
189 | ) { |
||
190 | return true; |
||
191 | } |
||
192 | |||
193 | return false; |
||
194 | } |
||
195 | |||
196 | // ------------------------------------------------------------------------ |
||
197 | |||
198 | /** |
||
199 | * Firewall::isWhitelisted |
||
200 | * |
||
201 | * Checks if the client ip address is whitelisted. |
||
202 | * |
||
203 | * @param string $ipAddress Client ip address. |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function isWhitelisted($ipAddress) |
||
208 | { |
||
209 | if ($this->isValid($ipAddress)) { |
||
210 | return (bool)in_array($ipAddress, $this->whitelistIpAddresses); |
||
211 | } |
||
212 | |||
213 | return false; |
||
214 | } |
||
215 | |||
216 | // ------------------------------------------------------------------------ |
||
217 | |||
218 | /** |
||
219 | * Firewall::isBlacklisted |
||
220 | * |
||
221 | * Checks if the client ip address is blacklisted. |
||
222 | * |
||
223 | * @param string $ipAddress Client ip address. |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function isBlacklisted($ipAddress) |
||
228 | { |
||
229 | if ($this->isValid($ipAddress)) { |
||
230 | return (bool)in_array($ipAddress, $this->whitelistIpAddresses); |
||
231 | } |
||
232 | |||
233 | return false; |
||
234 | } |
||
235 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.