PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/classes/Zend/Http/PhpEnvironment/RemoteAddress.php

https://gitlab.com/jalon/doadoronline
PHP | 172 lines | 67 code | 19 blank | 86 comment | 8 complexity | 31c5231d6d0b39b6eda6325e4df286e7 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Http\PhpEnvironment;
  10. /**
  11. * Functionality for determining client IP address.
  12. */
  13. class RemoteAddress
  14. {
  15. /**
  16. * Whether to use proxy addresses or not.
  17. *
  18. * As default this setting is disabled - IP address is mostly needed to increase
  19. * security. HTTP_* are not reliable since can easily be spoofed. It can be enabled
  20. * just for more flexibility, but if user uses proxy to connect to trusted services
  21. * it's his/her own risk, only reliable field for IP address is $_SERVER['REMOTE_ADDR'].
  22. *
  23. * @var bool
  24. */
  25. protected $useProxy = false;
  26. /**
  27. * List of trusted proxy IP addresses
  28. *
  29. * @var array
  30. */
  31. protected $trustedProxies = array();
  32. /**
  33. * HTTP header to introspect for proxies
  34. *
  35. * @var string
  36. */
  37. protected $proxyHeader = 'HTTP_X_FORWARDED_FOR';
  38. /**
  39. * Changes proxy handling setting.
  40. *
  41. * This must be static method, since validators are recovered automatically
  42. * at session read, so this is the only way to switch setting.
  43. *
  44. * @param bool $useProxy Whether to check also proxied IP addresses.
  45. * @return RemoteAddress
  46. */
  47. public function setUseProxy($useProxy = true)
  48. {
  49. $this->useProxy = $useProxy;
  50. return $this;
  51. }
  52. /**
  53. * Checks proxy handling setting.
  54. *
  55. * @return bool Current setting value.
  56. */
  57. public function getUseProxy()
  58. {
  59. return $this->useProxy;
  60. }
  61. /**
  62. * Set list of trusted proxy addresses
  63. *
  64. * @param array $trustedProxies
  65. * @return RemoteAddress
  66. */
  67. public function setTrustedProxies(array $trustedProxies)
  68. {
  69. $this->trustedProxies = $trustedProxies;
  70. return $this;
  71. }
  72. /**
  73. * Set the header to introspect for proxy IPs
  74. *
  75. * @param string $header
  76. * @return RemoteAddress
  77. */
  78. public function setProxyHeader($header = 'X-Forwarded-For')
  79. {
  80. $this->proxyHeader = $this->normalizeProxyHeader($header);
  81. return $this;
  82. }
  83. /**
  84. * Returns client IP address.
  85. *
  86. * @return string IP address.
  87. */
  88. public function getIpAddress()
  89. {
  90. $ip = $this->getIpAddressFromProxy();
  91. if ($ip) {
  92. return $ip;
  93. }
  94. // direct IP address
  95. if (isset($_SERVER['REMOTE_ADDR'])) {
  96. return $_SERVER['REMOTE_ADDR'];
  97. }
  98. return '';
  99. }
  100. /**
  101. * Attempt to get the IP address for a proxied client
  102. *
  103. * @see http://tools.ietf.org/html/draft-ietf-appsawg-http-forwarded-10#section-5.2
  104. * @return false|string
  105. */
  106. protected function getIpAddressFromProxy()
  107. {
  108. if (!$this->useProxy
  109. || !in_array($_SERVER['REMOTE_ADDR'], $this->trustedProxies)
  110. ) {
  111. return false;
  112. }
  113. $header = $this->proxyHeader;
  114. if (!isset($_SERVER[$header]) || empty($_SERVER[$header])) {
  115. return false;
  116. }
  117. // Extract IPs
  118. $ips = explode(',', $_SERVER[$header]);
  119. // trim, so we can compare against trusted proxies properly
  120. $ips = array_map('trim', $ips);
  121. // remove trusted proxy IPs
  122. $ips = array_diff($ips, $this->trustedProxies);
  123. // Any left?
  124. if (empty($ips)) {
  125. return false;
  126. }
  127. // Since we've removed any known, trusted proxy servers, the right-most
  128. // address represents the first IP we do not know about -- i.e., we do
  129. // not know if it is a proxy server, or a client. As such, we treat it
  130. // as the originating IP.
  131. // @see http://en.wikipedia.org/wiki/X-Forwarded-For
  132. $ip = array_pop($ips);
  133. return $ip;
  134. }
  135. /**
  136. * Normalize a header string
  137. *
  138. * Normalizes a header string to a format that is compatible with
  139. * $_SERVER
  140. *
  141. * @param string $header
  142. * @return string
  143. */
  144. protected function normalizeProxyHeader($header)
  145. {
  146. $header = strtoupper($header);
  147. $header = str_replace('-', '_', $header);
  148. if (0 !== strpos($header, 'HTTP_')) {
  149. $header = 'HTTP_' . $header;
  150. }
  151. return $header;
  152. }
  153. }