Skip to content

Commit b819f9d

Browse files
committed
refactor: by RemoveUnusedNonEmptyArrayBeforeForeachRector
1 parent ec5cb2f commit b819f9d

File tree

1 file changed

+52
-54
lines changed

1 file changed

+52
-54
lines changed

system/HTTP/RequestTrait.php

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -84,76 +84,74 @@ public function getIPAddress(): string
8484
return $this->ipAddress = '0.0.0.0';
8585
}
8686

87-
if ($proxyIPs) {
88-
// @TODO Extract all this IP address logic to another class.
89-
foreach ($proxyIPs as $proxyIP => $header) {
90-
// Check if we have an IP address or a subnet
91-
if (strpos($proxyIP, '/') === false) {
92-
// An IP address (and not a subnet) is specified.
93-
// We can compare right away.
94-
if ($proxyIP === $this->ipAddress) {
95-
$spoof = $this->getClientIP($header);
96-
97-
if ($spoof !== null) {
98-
$this->ipAddress = $spoof;
99-
break;
100-
}
101-
}
87+
// @TODO Extract all this IP address logic to another class.
88+
foreach ($proxyIPs as $proxyIP => $header) {
89+
// Check if we have an IP address or a subnet
90+
if (strpos($proxyIP, '/') === false) {
91+
// An IP address (and not a subnet) is specified.
92+
// We can compare right away.
93+
if ($proxyIP === $this->ipAddress) {
94+
$spoof = $this->getClientIP($header);
10295

103-
continue;
96+
if ($spoof !== null) {
97+
$this->ipAddress = $spoof;
98+
break;
99+
}
104100
}
105101

106-
// We have a subnet ... now the heavy lifting begins
107-
if (! isset($separator)) {
108-
$separator = $ipValidator($this->ipAddress, 'ipv6') ? ':' : '.';
109-
}
102+
continue;
103+
}
110104

111-
// If the proxy entry doesn't match the IP protocol - skip it
112-
if (strpos($proxyIP, $separator) === false) {
113-
continue;
114-
}
105+
// We have a subnet ... now the heavy lifting begins
106+
if (! isset($separator)) {
107+
$separator = $ipValidator($this->ipAddress, 'ipv6') ? ':' : '.';
108+
}
115109

116-
// Convert the REMOTE_ADDR IP address to binary, if needed
117-
if (! isset($ip, $sprintf)) {
118-
if ($separator === ':') {
119-
// Make sure we're having the "full" IPv6 format
120-
$ip = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($this->ipAddress, ':')), $this->ipAddress));
110+
// If the proxy entry doesn't match the IP protocol - skip it
111+
if (strpos($proxyIP, $separator) === false) {
112+
continue;
113+
}
121114

122-
for ($j = 0; $j < 8; $j++) {
123-
$ip[$j] = intval($ip[$j], 16);
124-
}
115+
// Convert the REMOTE_ADDR IP address to binary, if needed
116+
if (! isset($ip, $sprintf)) {
117+
if ($separator === ':') {
118+
// Make sure we're having the "full" IPv6 format
119+
$ip = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($this->ipAddress, ':')), $this->ipAddress));
125120

126-
$sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
127-
} else {
128-
$ip = explode('.', $this->ipAddress);
129-
$sprintf = '%08b%08b%08b%08b';
121+
for ($j = 0; $j < 8; $j++) {
122+
$ip[$j] = intval($ip[$j], 16);
130123
}
131124

132-
$ip = vsprintf($sprintf, $ip);
125+
$sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
126+
} else {
127+
$ip = explode('.', $this->ipAddress);
128+
$sprintf = '%08b%08b%08b%08b';
133129
}
134130

135-
// Split the netmask length off the network address
136-
sscanf($proxyIP, '%[^/]/%d', $netaddr, $masklen);
131+
$ip = vsprintf($sprintf, $ip);
132+
}
137133

138-
// Again, an IPv6 address is most likely in a compressed form
139-
if ($separator === ':') {
140-
$netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
134+
// Split the netmask length off the network address
135+
sscanf($proxyIP, '%[^/]/%d', $netaddr, $masklen);
141136

142-
for ($i = 0; $i < 8; $i++) {
143-
$netaddr[$i] = intval($netaddr[$i], 16);
144-
}
145-
} else {
146-
$netaddr = explode('.', $netaddr);
137+
// Again, an IPv6 address is most likely in a compressed form
138+
if ($separator === ':') {
139+
$netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
140+
141+
for ($i = 0; $i < 8; $i++) {
142+
$netaddr[$i] = intval($netaddr[$i], 16);
147143
}
144+
} else {
145+
$netaddr = explode('.', $netaddr);
146+
}
148147

149-
// Convert to binary and finally compare
150-
if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0) {
151-
$spoof = $this->getClientIP($header);
148+
// Convert to binary and finally compare
149+
if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0) {
150+
$spoof = $this->getClientIP($header);
152151

153-
if ($spoof !== null) {
154-
$this->ipAddress = $spoof;
155-
break;
156-
}
152+
if ($spoof !== null) {
153+
$this->ipAddress = $spoof;
154+
break;
157155
}
158156
}
159157
}

0 commit comments

Comments
 (0)