File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed
src/Tqdev/PhpCrudApi/Middleware Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change 2
2
namespace Tqdev \PhpCrudApi \Middleware ;
3
3
4
4
use Tqdev \PhpCrudApi \Controller \Responder ;
5
+ use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
5
6
use Tqdev \PhpCrudApi \Record \ErrorCode ;
6
7
use Tqdev \PhpCrudApi \Request ;
7
8
use Tqdev \PhpCrudApi \Response ;
8
- use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
9
9
10
10
class FirewallMiddleware extends Middleware
11
11
{
12
+ private function ipMatch (String $ ip , String $ cidr ): bool
13
+ {
14
+ if (strpos ($ cidr , '/ ' ) !== false ) {
15
+ list ($ subnet , $ mask ) = explode ('/ ' , trim ($ cidr ));
16
+ if ((ip2long ($ ip ) & ~((1 << (32 - $ mask )) - 1 )) == ip2long ($ subnet )) {
17
+ return true ;
18
+ }
19
+ } else {
20
+ if (ip2long ($ ip ) == ip2long ($ cidr )) {
21
+ return true ;
22
+ }
23
+ }
24
+ return false ;
25
+ }
26
+
12
27
private function isIpAllowed (String $ ipAddress , String $ allowedIpAddresses ): bool
13
28
{
14
29
foreach (explode (', ' , $ allowedIpAddresses ) as $ allowedIp ) {
15
- if ($ ipAddress == trim ( $ allowedIp )) {
30
+ if ($ this -> ipMatch ( $ ipAddress, $ allowedIp )) {
16
31
return true ;
17
32
}
18
33
}
You can’t perform that action at this time.
0 commit comments