Header.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\HTTP;
  7. /**
  8. * Library for working with HTTP headers
  9. */
  10. class Header
  11. {
  12. /**
  13. * Request object
  14. *
  15. * @var \Magento\Framework\App\RequestInterface
  16. */
  17. protected $_request;
  18. /**
  19. * @var \Magento\Framework\Stdlib\StringUtils
  20. */
  21. protected $_converter;
  22. /**
  23. * @param \Magento\Framework\App\RequestInterface $httpRequest
  24. * @param \Magento\Framework\Stdlib\StringUtils $converter
  25. */
  26. public function __construct(
  27. \Magento\Framework\App\RequestInterface $httpRequest,
  28. \Magento\Framework\Stdlib\StringUtils $converter
  29. ) {
  30. $this->_request = $httpRequest;
  31. $this->_converter = $converter;
  32. }
  33. /**
  34. * Retrieve HTTP HOST
  35. *
  36. * @param boolean $clean clean non UTF-8 characters
  37. * @return string
  38. */
  39. public function getHttpHost($clean = true)
  40. {
  41. return $this->_getHttpCleanValue('HTTP_HOST', $clean);
  42. }
  43. /**
  44. * Retrieve HTTP USER AGENT
  45. *
  46. * @param boolean $clean clean non UTF-8 characters
  47. * @return string
  48. */
  49. public function getHttpUserAgent($clean = true)
  50. {
  51. return $this->_getHttpCleanValue('HTTP_USER_AGENT', $clean);
  52. }
  53. /**
  54. * Retrieve HTTP ACCEPT LANGUAGE
  55. *
  56. * @param boolean $clean clean non UTF-8 characters
  57. * @return string
  58. */
  59. public function getHttpAcceptLanguage($clean = true)
  60. {
  61. return $this->_getHttpCleanValue('HTTP_ACCEPT_LANGUAGE', $clean);
  62. }
  63. /**
  64. * Retrieve HTTP ACCEPT CHARSET
  65. *
  66. * @param boolean $clean clean non UTF-8 characters
  67. * @return string
  68. */
  69. public function getHttpAcceptCharset($clean = true)
  70. {
  71. return $this->_getHttpCleanValue('HTTP_ACCEPT_CHARSET', $clean);
  72. }
  73. /**
  74. * Retrieve HTTP REFERER
  75. *
  76. * @param boolean $clean clean non UTF-8 characters
  77. * @return string
  78. */
  79. public function getHttpReferer($clean = true)
  80. {
  81. return $this->_getHttpCleanValue('HTTP_REFERER', $clean);
  82. }
  83. /**
  84. * Returns the REQUEST_URI taking into account
  85. * platform differences between Apache and IIS
  86. *
  87. * @param boolean $clean clean non UTF-8 characters
  88. * @return string
  89. */
  90. public function getRequestUri($clean = false)
  91. {
  92. $uri = $this->_request->getRequestUri();
  93. if ($clean) {
  94. $uri = $this->_converter->cleanString($uri);
  95. }
  96. return $uri;
  97. }
  98. /**
  99. * Retrieve HTTP "clean" value
  100. *
  101. * @param string $var
  102. * @param boolean $clean clean non UTF-8 characters
  103. * @return string
  104. */
  105. protected function _getHttpCleanValue($var, $clean = true)
  106. {
  107. $value = $this->_request->getServer($var, '');
  108. if ($clean) {
  109. $value = $this->_converter->cleanString($value);
  110. }
  111. return $value;
  112. }
  113. }