-
Notifications
You must be signed in to change notification settings - Fork 6
/
RequestHelper.php
150 lines (134 loc) · 4.38 KB
/
RequestHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace mgcode\helpers;
class RequestHelper extends \yii\base\Component
{
/**
* Returns unsigned IP address from signed IP address
* @param $signed
* @return string
*/
public static function signedToUnsigned($signed)
{
return sprintf("%u", $signed);
}
/**
* Converts a string containing an (IPv4) Internet Protocol dotted address into unsigned integer
* @param string $ip A standard format address.
* @return string
*/
public static function ipToUnsignedInt($ip)
{
return static::signedToUnsigned(ip2long($ip));
}
/**
* Converts an (IPv4) Internet network address into a string in Internet standard dotted format
* Signed/Unsigned integers can be used
* @param string $ip A proper address representation.
* @return string
*/
public static function intToIp($ip)
{
return long2ip($ip);
}
/**
* Checks if IP address is in CIDR IP range
* @param string $ip Unsigned integer IP address
* @param $cidr
* @return bool
*/
public static function isIpInCidr($unsignedIp, $cidr)
{
$range = static::getIpRangeFromCidr($cidr);
return $unsignedIp >= $range[0] && $unsignedIp <= $range[1];
}
/**
* Returns IP range from CIDR
* @param string $cidr
* @return array
*/
public static function getIpRangeFromCidr($cidr)
{
list($ip, $mask) = explode('/', $cidr);
$maskBinStr = str_repeat("1", $mask).str_repeat("0", 32 - $mask); //net mask binary string
$inverseMaskBinStr = str_repeat("0", $mask).str_repeat("1", 32 - $mask); //inverse mask
$ipLong = ip2long($ip);
$ipMaskLong = bindec($maskBinStr);
$inverseIpMaskLong = bindec($inverseMaskBinStr);
$netWork = $ipLong & $ipMaskLong;
$start = $netWork + 1; //ignore network ID(eg: 192.168.1.0)
$end = ($netWork | $inverseIpMaskLong) - 1; //ignore broadcast IP(eg: 192.168.1.255)
return [
static::signedToUnsigned($start),
static::signedToUnsigned($end),
];
}
/**
* Checks is users ip public.
* @param string $ip A standard (dotted) format address
* @param bool $matchIpv6 Whether to match ipv6 addresses or not
* @return bool
*/
public static function isPublicIp($ip, $matchIpv6 = true)
{
$ip = trim($ip);
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return true;
} else if ($matchIpv6 && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return true;
}
return false;
}
/**
* Returns forwarded user IP address.
* @param bool $returnDefault Whether to return default address if not found
* @return string
*/
public static function getUserIpForwarded($returnDefault = false)
{
$varsToCheck = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED');
foreach ($varsToCheck as $key) {
if (!array_key_exists($key, $_SERVER)) {
continue;
}
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip); // just to be safe
if (self::isPublicIp($ip)) {
return $ip;
}
}
}
return $returnDefault ? $_SERVER["REMOTE_ADDR"] : null;
}
/**
* Converts ip address to binary IP
* @param string $ip A standard format address
* @return string
*/
public static function ipToBin($ip)
{
if (filter_var($ip, FILTER_VALIDATE_IP) === false) {
return false;
}
return inet_pton($ip);
}
/**
* Converts binary address to standard format address
* @param $bin
* @return string A standard format address
*/
public static function binToIp($bin)
{
return inet_ntop($bin);
}
/**
* Checks is code in white list.
* White list codes: 1xx, 2xx, 3xx
* @param integer $code
* @return bool
*/
public static function isCodeInWhiteList($code)
{
$regex = '/^(1|2|3)\d\d$/';
return preg_match($regex, $code) ? true : false;
}
}