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 22namespace Tqdev \PhpCrudApi \Middleware ;
33
44use Tqdev \PhpCrudApi \Controller \Responder ;
5+ use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
56use Tqdev \PhpCrudApi \Record \ErrorCode ;
67use Tqdev \PhpCrudApi \Request ;
78use Tqdev \PhpCrudApi \Response ;
8- use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
99
1010class FirewallMiddleware extends Middleware
1111{
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+
1227 private function isIpAllowed (String $ ipAddress , String $ allowedIpAddresses ): bool
1328 {
1429 foreach (explode (', ' , $ allowedIpAddresses ) as $ allowedIp ) {
15- if ($ ipAddress == trim ( $ allowedIp )) {
30+ if ($ this -> ipMatch ( $ ipAddress, $ allowedIp )) {
1631 return true ;
1732 }
1833 }
You can’t perform that action at this time.
0 commit comments