Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProxyHeaders.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. /**
  11. * Proxy headers
  12. *
  13. */
  14. class ProxyHeaders
  15. {
  16. /**
  17. * Get protocol information, with the exception of HTTPS
  18. *
  19. * @return string protocol information
  20. */
  21. public static function getProtocolInformation()
  22. {
  23. if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) {
  24. return 'SERVER_PORT=443';
  25. }
  26. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https') {
  27. return 'X-Forwarded-Proto';
  28. }
  29. if (isset($_SERVER['HTTP_X_FORWARDED_SCHEME']) && strtolower($_SERVER['HTTP_X_FORWARDED_SCHEME']) == 'https') {
  30. return 'X-Forwarded-Scheme';
  31. }
  32. if (isset($_SERVER['HTTP_X_URL_SCHEME']) && strtolower($_SERVER['HTTP_X_URL_SCHEME']) == 'https') {
  33. return 'X-Url-Scheme';
  34. }
  35. return null;
  36. }
  37. /**
  38. * Get headers present in the HTTP request
  39. *
  40. * @param array $recognizedHeaders
  41. * @return array HTTP headers
  42. */
  43. private static function getHeaders($recognizedHeaders)
  44. {
  45. $headers = array();
  46. foreach ($recognizedHeaders as $header) {
  47. if (isset($_SERVER[$header])) {
  48. $headers[] = $header;
  49. }
  50. }
  51. return $headers;
  52. }
  53. /**
  54. * Detect proxy client headers
  55. *
  56. * @return array Proxy client HTTP headers
  57. */
  58. public static function getProxyClientHeaders()
  59. {
  60. return self::getHeaders(array(
  61. 'HTTP_CF_CONNECTING_IP',
  62. 'HTTP_CLIENT_IP',
  63. 'HTTP_X_FORWARDED_FOR',
  64. ));
  65. }
  66. /**
  67. * Detect proxy host headers
  68. *
  69. * @return array Proxy host HTTP headers
  70. */
  71. public static function getProxyHostHeaders()
  72. {
  73. return self::getHeaders(array(
  74. 'HTTP_X_FORWARDED_HOST',
  75. ));
  76. }
  77. }