Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ipaddress.php
1<?php
2
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()
55 {
56 return $this->ip;
57 }
58
64 public function __toString()
65 {
66 return $this->get();
67 }
68
74 public function isPrivate()
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
85 public function matchRange(string $cidr): bool
86 {
87 if (strpos($cidr,'/') !== false)
88 {
89 [$subnet, $mask] = explode('/', $cidr);
90 }
91 else
92 {
93 $subnet = $cidr;
94 $mask = 32;
95 }
96
97 return (ip2long($this->ip) & ~((1 << (32 - $mask)) - 1)) === ip2long($subnet);
98 }
99
104 public function toUnsigned()
105 {
106 return sprintf('%u', ip2long($this->ip));
107 }
108
113 public function toRange(int $prefixLen)
114 {
115 return long2ip(ip2long($this->ip) & ~((1 << (32 - $prefixLen)) - 1)) . '/' . $prefixLen;
116 }
117}
static createByName($name)
Definition ipaddress.php:32
toRange(int $prefixLen)
static createByUri(UriInterface $uri)
Definition ipaddress.php:44
matchRange(string $cidr)
Definition ipaddress.php:85