ServerBag.php 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. /**
  12. * ServerBag is a container for HTTP headers from the $_SERVER variable.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  16. * @author Robert Kiss <kepten@gmail.com>
  17. */
  18. class ServerBag extends ParameterBag
  19. {
  20. /**
  21. * Gets the HTTP headers.
  22. */
  23. public function getHeaders(): array
  24. {
  25. $headers = [];
  26. foreach ($this->parameters as $key => $value) {
  27. if (str_starts_with($key, 'HTTP_')) {
  28. $headers[substr($key, 5)] = $value;
  29. } elseif (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true) && '' !== $value) {
  30. $headers[$key] = $value;
  31. }
  32. }
  33. if (isset($this->parameters['PHP_AUTH_USER'])) {
  34. $headers['PHP_AUTH_USER'] = $this->parameters['PHP_AUTH_USER'];
  35. $headers['PHP_AUTH_PW'] = $this->parameters['PHP_AUTH_PW'] ?? '';
  36. } else {
  37. /*
  38. * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default
  39. * For this workaround to work, add these lines to your .htaccess file:
  40. * RewriteCond %{HTTP:Authorization} .+
  41. * RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
  42. *
  43. * A sample .htaccess file:
  44. * RewriteEngine On
  45. * RewriteCond %{HTTP:Authorization} .+
  46. * RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
  47. * RewriteCond %{REQUEST_FILENAME} !-f
  48. * RewriteRule ^(.*)$ index.php [QSA,L]
  49. */
  50. $authorizationHeader = null;
  51. if (isset($this->parameters['HTTP_AUTHORIZATION'])) {
  52. $authorizationHeader = $this->parameters['HTTP_AUTHORIZATION'];
  53. } elseif (isset($this->parameters['REDIRECT_HTTP_AUTHORIZATION'])) {
  54. $authorizationHeader = $this->parameters['REDIRECT_HTTP_AUTHORIZATION'];
  55. }
  56. if (null !== $authorizationHeader) {
  57. if (0 === stripos($authorizationHeader, 'basic ')) {
  58. // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic
  59. $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)), 2);
  60. if (2 == \count($exploded)) {
  61. [$headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']] = $exploded;
  62. }
  63. } elseif (empty($this->parameters['PHP_AUTH_DIGEST']) && (0 === stripos($authorizationHeader, 'digest '))) {
  64. // In some circumstances PHP_AUTH_DIGEST needs to be set
  65. $headers['PHP_AUTH_DIGEST'] = $authorizationHeader;
  66. $this->parameters['PHP_AUTH_DIGEST'] = $authorizationHeader;
  67. } elseif (0 === stripos($authorizationHeader, 'bearer ')) {
  68. /*
  69. * XXX: Since there is no PHP_AUTH_BEARER in PHP predefined variables,
  70. * I'll just set $headers['AUTHORIZATION'] here.
  71. * https://php.net/reserved.variables.server
  72. */
  73. $headers['AUTHORIZATION'] = $authorizationHeader;
  74. }
  75. }
  76. }
  77. if (isset($headers['AUTHORIZATION'])) {
  78. return $headers;
  79. }
  80. // PHP_AUTH_USER/PHP_AUTH_PW
  81. if (isset($headers['PHP_AUTH_USER'])) {
  82. $headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.($headers['PHP_AUTH_PW'] ?? ''));
  83. } elseif (isset($headers['PHP_AUTH_DIGEST'])) {
  84. $headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST'];
  85. }
  86. return $headers;
  87. }
  88. }