1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ipaddress.php
См. документацию.
1<?php
2
9
10namespace Bitrix\Main\Web;
11
12use Psr\Http\Message\UriInterface;
13
15{
16 protected $ip;
17
21 public function __construct($ip)
22 {
23 $this->ip = $ip;
24 }
25
32 public static function createByName($name)
33 {
34 $ip = gethostbyname($name);
35 return new static($ip);
36 }
37
44 public static function createByUri(UriInterface $uri)
45 {
46 return static::createByName($uri->getHost());
47 }
48
54 public function get(): string
55 {
56 return $this->ip;
57 }
58
64 public function __toString()
65 {
66 return $this->get();
67 }
68
74 public function isPrivate(): bool
75 {
76 return (filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false);
77 }
78
84 public function isIPv4(): bool
85 {
86 return (filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false);
87 }
88
95 public function matchRange(string $cidr): bool
96 {
97 if (str_contains($cidr, '/'))
98 {
99 [$subnet, $mask] = explode('/', $cidr);
100 }
101 else
102 {
103 $subnet = $cidr;
104 $mask = 32;
105 }
106
107 return (ip2long($this->ip) & ~((1 << (32 - $mask)) - 1)) === ip2long($subnet);
108 }
109
114 public function toUnsigned(): string
115 {
116 return sprintf('%u', ip2long($this->ip));
117 }
118
124 public function toRange(int $prefixLen): string
125 {
126 return long2ip(ip2long($this->ip) & ~((1 << (32 - $prefixLen)) - 1)) . '/' . $prefixLen;
127 }
128}
static createByName($name)
Определения ipaddress.php:32
isPrivate()
Определения ipaddress.php:74
__toString()
Определения ipaddress.php:64
toRange(int $prefixLen)
Определения ipaddress.php:124
__construct($ip)
Определения ipaddress.php:21
static createByUri(UriInterface $uri)
Определения ipaddress.php:44
matchRange(string $cidr)
Определения ipaddress.php:95
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения urlrewrite.php:61
$name
Определения menu_edit.php:35