feat: xxbb
This commit is contained in:
@@ -54,8 +54,51 @@ class PageVisit extends Model
|
||||
public static function visitorUid(?string $domain, ?string $ip): string
|
||||
{
|
||||
$domainKey = self::normalizeDomain($domain) ?? strtolower(trim((string) $domain));
|
||||
$ipKey = trim((string) $ip);
|
||||
$ipKey = self::normalizeIp($ip);
|
||||
|
||||
return substr(hash('sha256', $domainKey."\0".$ipKey), 0, self::VISITOR_UID_LENGTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwrap IPv4-mapped IPv6 (`::ffff:1.2.3.4` → `1.2.3.4`) and canonicalize.
|
||||
*/
|
||||
public static function normalizeIp(?string $ip): string
|
||||
{
|
||||
$ip = strtolower(trim((string) $ip));
|
||||
if ($ip === '') {
|
||||
return '';
|
||||
}
|
||||
if (str_starts_with($ip, '::ffff:')) {
|
||||
$mapped = substr($ip, 7);
|
||||
if (filter_var($mapped, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
||||
return $mapped;
|
||||
}
|
||||
}
|
||||
$packed = filter_var($ip, FILTER_VALIDATE_IP) ? inet_pton($ip) : false;
|
||||
if ($packed !== false) {
|
||||
$canon = inet_ntop($packed);
|
||||
if (is_string($canon) && $canon !== '') {
|
||||
return strtolower($canon);
|
||||
}
|
||||
}
|
||||
|
||||
return $ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function ipLookupKeys(?string $ip): array
|
||||
{
|
||||
$norm = self::normalizeIp($ip);
|
||||
if ($norm === '') {
|
||||
return [];
|
||||
}
|
||||
$keys = [$norm];
|
||||
if (filter_var($norm, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
||||
$keys[] = '::ffff:'.$norm;
|
||||
}
|
||||
|
||||
return array_values(array_unique($keys));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user