1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
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 <fabien@symfony.com>
18: * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
19: * @author Robert Kiss <kepten@gmail.com>
20: */
21: class ServerBag extends ParameterBag
22: {
23: /**
24: * Gets the HTTP headers.
25: *
26: * @return array
27: */
28: public function getHeaders()
29: {
30: $headers = array();
31: foreach ($this->parameters as $key => $value) {
32: if (0 === strpos($key, 'HTTP_')) {
33: $headers[substr($key, 5)] = $value;
34: }
35: // CONTENT_* are not prefixed with HTTP_
36: elseif (in_array($key, array('CONTENT_LENGTH', 'CONTENT_MD5', 'CONTENT_TYPE'))) {
37: $headers[$key] = $value;
38: }
39: }
40:
41: if (isset($this->parameters['PHP_AUTH_USER'])) {
42: $headers['PHP_AUTH_USER'] = $this->parameters['PHP_AUTH_USER'];
43: $headers['PHP_AUTH_PW'] = isset($this->parameters['PHP_AUTH_PW']) ? $this->parameters['PHP_AUTH_PW'] : '';
44: } else {
45: /*
46: * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default
47: * For this workaround to work, add these lines to your .htaccess file:
48: * RewriteCond %{HTTP:Authorization} ^(.+)$
49: * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
50: *
51: * A sample .htaccess file:
52: * RewriteEngine On
53: * RewriteCond %{HTTP:Authorization} ^(.+)$
54: * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
55: * RewriteCond %{REQUEST_FILENAME} !-f
56: * RewriteRule ^(.*)$ app.php [QSA,L]
57: */
58:
59: $authorizationHeader = null;
60: if (isset($this->parameters['HTTP_AUTHORIZATION'])) {
61: $authorizationHeader = $this->parameters['HTTP_AUTHORIZATION'];
62: } elseif (isset($this->parameters['REDIRECT_HTTP_AUTHORIZATION'])) {
63: $authorizationHeader = $this->parameters['REDIRECT_HTTP_AUTHORIZATION'];
64: }
65:
66: // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic
67: if ((null !== $authorizationHeader) && (0 === stripos($authorizationHeader, 'basic'))) {
68: $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)));
69: if (count($exploded) == 2) {
70: list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
71: }
72: }
73: }
74:
75: // PHP_AUTH_USER/PHP_AUTH_PW
76: if (isset($headers['PHP_AUTH_USER'])) {
77: $headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']);
78: }
79:
80: return $headers;
81: }
82: }
83: