Skip to content

Commit caeb549

Browse files
kenjisMGatner
authored andcommitted
refactor: extract method
1 parent cb6d801 commit caeb549

1 file changed

Lines changed: 37 additions & 33 deletions

File tree

system/HTTP/RequestTrait.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,11 @@ public function getIPAddress(): string
8080
// An IP address (and not a subnet) is specified.
8181
// We can compare right away.
8282
if ($proxyIP === $this->ipAddress) {
83-
$spoof = null;
84-
$headerObj = $this->header($header);
85-
86-
if ($headerObj !== null) {
87-
$spoof = $headerObj->getValue();
88-
89-
// Some proxies typically list the whole chain of IP
90-
// addresses through which the client has reached us.
91-
// e.g. client_ip, proxy_ip1, proxy_ip2, etc.
92-
sscanf($spoof, '%[^,]', $spoof);
93-
94-
if (! $ipValidator($spoof)) {
95-
$spoof = null;
96-
} else {
97-
$this->ipAddress = $spoof;
98-
break;
99-
}
83+
$spoof = $this->getClientIP($header);
84+
85+
if ($spoof !== null) {
86+
$this->ipAddress = $spoof;
87+
break;
10088
}
10189
}
10290

@@ -148,23 +136,11 @@ public function getIPAddress(): string
148136

149137
// Convert to binary and finally compare
150138
if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0) {
151-
$spoof = null;
152-
$headerObj = $this->header($header);
139+
$spoof = $this->getClientIP($header);
153140

154-
if ($headerObj !== null) {
155-
$spoof = $headerObj->getValue();
156-
157-
// Some proxies typically list the whole chain of IP
158-
// addresses through which the client has reached us.
159-
// e.g. client_ip, proxy_ip1, proxy_ip2, etc.
160-
sscanf($spoof, '%[^,]', $spoof);
161-
162-
if (! $ipValidator($spoof)) {
163-
$spoof = null;
164-
} else {
165-
$this->ipAddress = $spoof;
166-
break;
167-
}
141+
if ($spoof !== null) {
142+
$this->ipAddress = $spoof;
143+
break;
168144
}
169145
}
170146
}
@@ -177,6 +153,34 @@ public function getIPAddress(): string
177153
return empty($this->ipAddress) ? '' : $this->ipAddress;
178154
}
179155

156+
/**
157+
* Gets the client IP address from the HTTP header.
158+
*/
159+
private function getClientIP(string $header): ?string
160+
{
161+
$ipValidator = [
162+
new FormatRules(),
163+
'valid_ip',
164+
];
165+
$spoof = null;
166+
$headerObj = $this->header($header);
167+
168+
if ($headerObj !== null) {
169+
$spoof = $headerObj->getValue();
170+
171+
// Some proxies typically list the whole chain of IP
172+
// addresses through which the client has reached us.
173+
// e.g. client_ip, proxy_ip1, proxy_ip2, etc.
174+
sscanf($spoof, '%[^,]', $spoof);
175+
176+
if (! $ipValidator($spoof)) {
177+
$spoof = null;
178+
}
179+
}
180+
181+
return $spoof;
182+
}
183+
180184
/**
181185
* Fetch an item from the $_SERVER array.
182186
*

0 commit comments

Comments
 (0)