芝麻web文件管理V1.00
编辑当前文件:/home/mgatv524/public_html/mctv/vendor/akrabat/ip-address-middleware/src/IpAddress.php
checkProxyHeaders = $checkProxyHeaders; $this->trustedProxies = $trustedProxies; if ($attributeName) { $this->attributeName = $attributeName; } if (!empty($headersToInspect)) { $this->headersToInspect = $headersToInspect; } } /** * {@inheritDoc} * * Set the "$attributeName" attribute to the client's IP address as determined from * the proxy header (X-Forwarded-For or from $_SERVER['REMOTE_ADDR'] */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $ipAddress = $this->determineClientIpAddress($request); $request = $request->withAttribute($this->attributeName, $ipAddress); return $handler->handle($request); } /** * Set the "$attributeName" attribute to the client's IP address as determined from * the proxy header (X-Forwarded-For or from $_SERVER['REMOTE_ADDR'] * * @param ServerRequestInterface $request PSR7 request * @param ResponseInterface $response PSR7 response * @param callable $next Next middleware * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) { if (!$next) { return $response; } $ipAddress = $this->determineClientIpAddress($request); $request = $request->withAttribute($this->attributeName, $ipAddress); return $response = $next($request, $response); } /** * Find out the client's IP address from the headers available to us * * @param ServerRequestInterface $request PSR-7 Request * @return string */ protected function determineClientIpAddress($request) { $ipAddress = null; $serverParams = $request->getServerParams(); if (isset($serverParams['REMOTE_ADDR'])) { $remoteAddr = $this->extractIpAddress($serverParams['REMOTE_ADDR']); if ($this->isValidIpAddress($remoteAddr)) { $ipAddress = $remoteAddr; } } $checkProxyHeaders = $this->checkProxyHeaders; if ($checkProxyHeaders && !empty($this->trustedProxies)) { if (!in_array($ipAddress, $this->trustedProxies)) { $checkProxyHeaders = false; } } if ($checkProxyHeaders) { foreach ($this->headersToInspect as $header) { if ($request->hasHeader($header)) { $ip = $this->getFirstIpAddressFromHeader($request, $header); if ($this->isValidIpAddress($ip)) { $ipAddress = $ip; break; } } } } return $ipAddress; } /** * Remove port from IPV4 address if it exists * * Note: leaves IPV6 addresses alone * * @param string $ipAddress * @return string */ protected function extractIpAddress($ipAddress) { $parts = explode(':', $ipAddress); if (count($parts) == 2) { if (filter_var($parts[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) { return $parts[0]; } } return $ipAddress; } /** * Check that a given string is a valid IP address * * @param string $ip * @return boolean */ protected function isValidIpAddress($ip) { $flags = FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6; if (filter_var($ip, FILTER_VALIDATE_IP, $flags) === false) { return false; } return true; } /** * Find out the client's IP address from the headers available to us * * @param ServerRequestInterface $request PSR-7 Request * @param string $header Header name * @return string */ private function getFirstIpAddressFromHeader($request, $header) { $items = explode(',', $request->getHeaderLine($header)); $headerValue = trim(reset($items)); if (ucfirst($header) == 'Forwarded') { foreach (explode(';', $headerValue) as $headerPart) { if (strtolower(substr($headerPart, 0, 4)) == 'for=') { $for = explode(']', $headerPart); $headerValue = trim(substr(reset($for), 4), " \t\n\r\0\x0B" . "\"[]"); break; } } } return $this->extractIpAddress($headerValue); } }