RemoteAddress.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\HTTP\PhpEnvironment;
  7. use Magento\Framework\App\RequestInterface;
  8. /**
  9. * Library for working with client ip address.
  10. */
  11. class RemoteAddress
  12. {
  13. /**
  14. * Request object.
  15. *
  16. * @var RequestInterface
  17. */
  18. protected $request;
  19. /**
  20. * Remote address cache.
  21. *
  22. * @var string
  23. */
  24. protected $remoteAddress;
  25. /**
  26. * @var array
  27. */
  28. protected $alternativeHeaders;
  29. /**
  30. * @var string[]|null
  31. */
  32. private $trustedProxies;
  33. /**
  34. * @param RequestInterface $httpRequest
  35. * @param array $alternativeHeaders
  36. * @param string[]|null $trustedProxies
  37. */
  38. public function __construct(
  39. RequestInterface $httpRequest,
  40. array $alternativeHeaders = [],
  41. array $trustedProxies = null
  42. ) {
  43. $this->request = $httpRequest;
  44. $this->alternativeHeaders = $alternativeHeaders;
  45. $this->trustedProxies = $trustedProxies;
  46. }
  47. /**
  48. * Read address based on settings.
  49. *
  50. * @return string|null
  51. */
  52. private function readAddress()
  53. {
  54. $remoteAddress = null;
  55. foreach ($this->alternativeHeaders as $var) {
  56. if ($this->request->getServer($var, false)) {
  57. $remoteAddress = $this->request->getServer($var);
  58. break;
  59. }
  60. }
  61. if (!$remoteAddress) {
  62. $remoteAddress = $this->request->getServer('REMOTE_ADDR');
  63. }
  64. return $remoteAddress;
  65. }
  66. /**
  67. * Filter addresses by trusted proxies list.
  68. *
  69. * @param string $remoteAddress
  70. * @return string|null
  71. */
  72. private function filterAddress(string $remoteAddress)
  73. {
  74. if (strpos($remoteAddress, ',') !== false) {
  75. $ipList = explode(',', $remoteAddress);
  76. } else {
  77. $ipList = [$remoteAddress];
  78. }
  79. $ipList = array_filter(
  80. $ipList,
  81. function (string $ip) {
  82. return filter_var(trim($ip), FILTER_VALIDATE_IP);
  83. }
  84. );
  85. if ($this->trustedProxies !== null) {
  86. $ipList = array_filter(
  87. $ipList,
  88. function (string $ip) {
  89. return !in_array(trim($ip), $this->trustedProxies, true);
  90. }
  91. );
  92. $remoteAddress = trim(array_pop($ipList));
  93. } else {
  94. $remoteAddress = trim(reset($ipList));
  95. }
  96. return $remoteAddress ?: null;
  97. }
  98. /**
  99. * Retrieve Client Remote Address.
  100. * If alternative headers are used and said headers allow multiple IPs
  101. * it is suggested that trusted proxies is also used
  102. * for more accurate IP recognition.
  103. *
  104. * @param bool $ipToLong converting IP to long format
  105. *
  106. * @return string IPv4|long
  107. */
  108. public function getRemoteAddress(bool $ipToLong = false)
  109. {
  110. if ($this->remoteAddress !== null) {
  111. return $this->remoteAddress;
  112. }
  113. $remoteAddress = $this->readAddress();
  114. if (!$remoteAddress) {
  115. $this->remoteAddress = false;
  116. return false;
  117. }
  118. $remoteAddress = $this->filterAddress($remoteAddress);
  119. if (!$remoteAddress) {
  120. $this->remoteAddress = false;
  121. return false;
  122. } else {
  123. $this->remoteAddress = $remoteAddress;
  124. return $ipToLong ? ip2long($this->remoteAddress) : $this->remoteAddress;
  125. }
  126. }
  127. /**
  128. * Returns internet host name corresponding to remote server
  129. *
  130. * @return string|null
  131. */
  132. public function getRemoteHost()
  133. {
  134. return $this->getRemoteAddress()
  135. ? gethostbyaddr($this->getRemoteAddress())
  136. : null;
  137. }
  138. }