#feature: add ip operation log & sql generator
This commit is contained in:
50
app/Support/IpAccess.php
Normal file
50
app/Support/IpAccess.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user