Files
toolbox/app/Support/IpAccess.php

51 lines
1.1 KiB
PHP

<?php
namespace App\Support;
class IpAccess
{
/**
* @return array<int, string>
*/
public static function adminIps(): array
{
return config('toolbox.admin_ips', []);
}
public static function isAdmin(?string $ip): bool
{
if (!is_string($ip) || $ip === '') {
return false;
}
$adminIps = self::adminIps();
if (empty($adminIps)) {
return false;
}
foreach ($adminIps as $pattern) {
if ($pattern === $ip) {
return true;
}
if (str_contains($pattern, '*') && self::matchWildcard($pattern, $ip)) {
return true;
}
}
return false;
}
private static function matchWildcard(string $pattern, string $ip): bool
{
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
return false;
}
$escaped = preg_quote($pattern, '#');
$regex = str_replace('\*', '[0-9.]*', $escaped);
return (bool) preg_match('#^' . $regex . '$#', $ip);
}
}