style updates

This commit is contained in:
Mark Gregory
2026-07-24 14:59:44 +01:00
parent 24ab771cd3
commit dd23205d0b
482 changed files with 7173 additions and 1435 deletions
+29 -14
View File
@@ -1,24 +1,39 @@
<?php
namespace Modules\Security\Services;
use Modules\Security\Models\IpRule;
final class IpRuleService
{
public function isBlocked(string $ip, string $scope='admin'): bool
public function isBlocked(string $ip, string $scope = 'admin'): bool
{
$column=$scope==='login'?'applies_to_login':'applies_to_admin';
$rules=IpRule::query()->where('is_active',true)->where($column,true)->get();
$allow=$rules->where('type','allow');
if ($allow->isNotEmpty() && !$allow->contains(fn(IpRule $rule)=>$this->matches($ip,$rule->address))) return true;
return $rules->where('type','deny')->contains(fn(IpRule $rule)=>$this->matches($ip,$rule->address));
}
private function matches(string $ip,string $rule): bool
{
if ($ip===$rule) return true;
if (!str_contains($rule,'/')) return false;
[$subnet,$bits]=explode('/',$rule,2); $bits=(int)$bits;
if (filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_IPV4) && filter_var($subnet,FILTER_VALIDATE_IP,FILTER_FLAG_IPV4)) {
$mask=-1 << (32-$bits); return (ip2long($ip)&$mask)===(ip2long($subnet)&$mask);
$column = $scope === 'login' ? 'applies_to_login' : 'applies_to_admin';
$rules = IpRule::query()->where('is_active', true)->where($column, true)->get();
$allow = $rules->where('type', 'allow');
if ($allow->isNotEmpty() && ! $allow->contains(fn (IpRule $rule) => $this->matches($ip, $rule->address))) {
return true;
}
return $rules->where('type', 'deny')->contains(fn (IpRule $rule) => $this->matches($ip, $rule->address));
}
private function matches(string $ip, string $rule): bool
{
if ($ip === $rule) {
return true;
}
if (! str_contains($rule, '/')) {
return false;
}
[$subnet,$bits] = explode('/', $rule, 2);
$bits = (int) $bits;
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && filter_var($subnet, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$mask = -1 << (32 - $bits);
return (ip2long($ip) & $mask) === (ip2long($subnet) & $mask);
}
return false;
}
}