123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\HTTP\PhpEnvironment;
- use Magento\Framework\App\RequestInterface;
- /**
- * Library for working with client ip address.
- */
- class RemoteAddress
- {
- /**
- * Request object.
- *
- * @var RequestInterface
- */
- protected $request;
- /**
- * Remote address cache.
- *
- * @var string
- */
- protected $remoteAddress;
- /**
- * @var array
- */
- protected $alternativeHeaders;
- /**
- * @var string[]|null
- */
- private $trustedProxies;
- /**
- * @param RequestInterface $httpRequest
- * @param array $alternativeHeaders
- * @param string[]|null $trustedProxies
- */
- public function __construct(
- RequestInterface $httpRequest,
- array $alternativeHeaders = [],
- array $trustedProxies = null
- ) {
- $this->request = $httpRequest;
- $this->alternativeHeaders = $alternativeHeaders;
- $this->trustedProxies = $trustedProxies;
- }
- /**
- * Read address based on settings.
- *
- * @return string|null
- */
- private function readAddress()
- {
- $remoteAddress = null;
- foreach ($this->alternativeHeaders as $var) {
- if ($this->request->getServer($var, false)) {
- $remoteAddress = $this->request->getServer($var);
- break;
- }
- }
- if (!$remoteAddress) {
- $remoteAddress = $this->request->getServer('REMOTE_ADDR');
- }
- return $remoteAddress;
- }
- /**
- * Filter addresses by trusted proxies list.
- *
- * @param string $remoteAddress
- * @return string|null
- */
- private function filterAddress(string $remoteAddress)
- {
- if (strpos($remoteAddress, ',') !== false) {
- $ipList = explode(',', $remoteAddress);
- } else {
- $ipList = [$remoteAddress];
- }
- $ipList = array_filter(
- $ipList,
- function (string $ip) {
- return filter_var(trim($ip), FILTER_VALIDATE_IP);
- }
- );
- if ($this->trustedProxies !== null) {
- $ipList = array_filter(
- $ipList,
- function (string $ip) {
- return !in_array(trim($ip), $this->trustedProxies, true);
- }
- );
- $remoteAddress = trim(array_pop($ipList));
- } else {
- $remoteAddress = trim(reset($ipList));
- }
- return $remoteAddress ?: null;
- }
- /**
- * Retrieve Client Remote Address.
- * If alternative headers are used and said headers allow multiple IPs
- * it is suggested that trusted proxies is also used
- * for more accurate IP recognition.
- *
- * @param bool $ipToLong converting IP to long format
- *
- * @return string IPv4|long
- */
- public function getRemoteAddress(bool $ipToLong = false)
- {
- if ($this->remoteAddress !== null) {
- return $this->remoteAddress;
- }
- $remoteAddress = $this->readAddress();
- if (!$remoteAddress) {
- $this->remoteAddress = false;
- return false;
- }
- $remoteAddress = $this->filterAddress($remoteAddress);
- if (!$remoteAddress) {
- $this->remoteAddress = false;
- return false;
- } else {
- $this->remoteAddress = $remoteAddress;
- return $ipToLong ? ip2long($this->remoteAddress) : $this->remoteAddress;
- }
- }
- /**
- * Returns internet host name corresponding to remote server
- *
- * @return string|null
- */
- public function getRemoteHost()
- {
- return $this->getRemoteAddress()
- ? gethostbyaddr($this->getRemoteAddress())
- : null;
- }
- }
|