Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
ipaddress.php
1
<?php
2
10
namespace
Bitrix\Main\Web
;
11
12
use Psr\Http\Message\UriInterface;
13
14
class
IpAddress
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
}
Bitrix\Main\Web\IpAddress
Definition
ipaddress.php:15
Bitrix\Main\Web\IpAddress\createByName
static createByName($name)
Definition
ipaddress.php:32
Bitrix\Main\Web\IpAddress\isPrivate
isPrivate()
Definition
ipaddress.php:74
Bitrix\Main\Web\IpAddress\__toString
__toString()
Definition
ipaddress.php:64
Bitrix\Main\Web\IpAddress\$ip
$ip
Definition
ipaddress.php:16
Bitrix\Main\Web\IpAddress\toRange
toRange(int $prefixLen)
Definition
ipaddress.php:113
Bitrix\Main\Web\IpAddress\__construct
__construct($ip)
Definition
ipaddress.php:21
Bitrix\Main\Web\IpAddress\toUnsigned
toUnsigned()
Definition
ipaddress.php:104
Bitrix\Main\Web\IpAddress\createByUri
static createByUri(UriInterface $uri)
Definition
ipaddress.php:44
Bitrix\Main\Web\IpAddress\matchRange
matchRange(string $cidr)
Definition
ipaddress.php:85
Bitrix\Main\Web
Definition
cookie.php:3
modules
main
lib
web
ipaddress.php
Создано системой
1.10.0