Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/ServerBag.php

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