fluent-community
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Http
/
Request
/
InteractsWithHeadersTrait.php
InteractsWithHeadersTrait.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.6.01, at vendor/wpfluent/framework/src/WPFluent/Http/Request/InteractsWithHeadersTrait.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCommunity\Framework\Http\Request; |
| 4 | |
| 5 | use FluentCommunity\Framework\Support\Arr; |
| 6 | |
| 7 | trait InteractsWithHeadersTrait |
| 8 | { |
| 9 | /** |
| 10 | * Retrieve an item from the PHP headers |
| 11 | * @param string $key |
| 12 | * @param string $default |
| 13 | * @return mixed |
| 14 | */ |
| 15 | public function header($key = null, $default = null) |
| 16 | { |
| 17 | !$this->headers && $this->setHeaders(); |
| 18 | |
| 19 | $key = str_replace('-', '_', strtoupper($key ?? '')); |
| 20 | |
| 21 | return $key ? Arr::get( |
| 22 | $this->headers, $key, $default |
| 23 | ) : $this->headers; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Sets the headers for the request. |
| 28 | * |
| 29 | * Note: Taken from Symfony and modified. |
| 30 | */ |
| 31 | public function setHeaders() |
| 32 | { |
| 33 | $headers = []; |
| 34 | |
| 35 | $parameters = $this->server; |
| 36 | |
| 37 | $contentHeaders = [ |
| 38 | 'CONTENT_LENGTH' => true, |
| 39 | 'CONTENT_MD5' => true, |
| 40 | 'CONTENT_TYPE' => true |
| 41 | ]; |
| 42 | |
| 43 | foreach ($parameters as $key => $value) { |
| 44 | if (0 === strpos($key, 'HTTP_')) { |
| 45 | $headers[substr($key, 5)] = $value; |
| 46 | } elseif (isset($contentHeaders[$key])) { |
| 47 | $headers[$key] = $value; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (isset($parameters['PHP_AUTH_USER'])) { |
| 52 | $headers['PHP_AUTH_USER'] = $parameters['PHP_AUTH_USER']; |
| 53 | $headers['PHP_AUTH_PW'] = isset($parameters['PHP_AUTH_PW']) ? $parameters['PHP_AUTH_PW'] : ''; |
| 54 | } else { |
| 55 | /* |
| 56 | * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default |
| 57 | * For this workaround to work, add these lines to your .htaccess file: |
| 58 | * RewriteCond %{HTTP:Authorization} ^(.+)$ |
| 59 | * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
| 60 | * |
| 61 | * A sample .htaccess file: |
| 62 | * RewriteEngine On |
| 63 | * RewriteCond %{HTTP:Authorization} ^(.+)$ |
| 64 | * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
| 65 | * RewriteCond %{REQUEST_FILENAME} !-f |
| 66 | * RewriteRule ^(.*)$ app.php [QSA,L] |
| 67 | */ |
| 68 | |
| 69 | $authorizationHeader = null; |
| 70 | if (isset($parameters['HTTP_AUTHORIZATION'])) { |
| 71 | $authorizationHeader = $parameters['HTTP_AUTHORIZATION']; |
| 72 | } elseif (isset($parameters['REDIRECT_HTTP_AUTHORIZATION'])) { |
| 73 | $authorizationHeader = $parameters['REDIRECT_HTTP_AUTHORIZATION']; |
| 74 | } |
| 75 | |
| 76 | if (null !== $authorizationHeader) { |
| 77 | if (0 === stripos($authorizationHeader, 'basic ')) { |
| 78 | // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic |
| 79 | $exploded = explode( |
| 80 | ':', base64_decode( |
| 81 | substr($authorizationHeader, 6) |
| 82 | ), 2 |
| 83 | ); |
| 84 | |
| 85 | if (count($exploded) == 2) { |
| 86 | list( |
| 87 | $headers['PHP_AUTH_USER'], |
| 88 | $headers['PHP_AUTH_PW'] |
| 89 | ) = $exploded; |
| 90 | } |
| 91 | } elseif ( |
| 92 | empty($parameters['PHP_AUTH_DIGEST']) && |
| 93 | (0 === stripos($authorizationHeader, 'digest ')) |
| 94 | ) { |
| 95 | // In some circumstances PHP_AUTH_DIGEST needs to be set |
| 96 | $headers['PHP_AUTH_DIGEST'] = $authorizationHeader; |
| 97 | $parameters['PHP_AUTH_DIGEST'] = $authorizationHeader; |
| 98 | } elseif (0 === stripos($authorizationHeader, 'bearer ')) { |
| 99 | /* |
| 100 | * XXX: Since there is no PHP_AUTH_BEARER in PHP predefined variables, |
| 101 | * I'll just set $headers['AUTHORIZATION'] here. |
| 102 | * http://php.net/manual/en/reserved.variables.server.php |
| 103 | */ |
| 104 | $headers['AUTHORIZATION'] = $authorizationHeader; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (isset($headers['AUTHORIZATION'])) { |
| 110 | return $headers; |
| 111 | } |
| 112 | |
| 113 | // PHP_AUTH_USER/PHP_AUTH_PW |
| 114 | if (isset($headers['PHP_AUTH_USER'])) { |
| 115 | $headers['AUTHORIZATION'] = 'Basic '.base64_encode( |
| 116 | $headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW'] |
| 117 | ); |
| 118 | } elseif (isset($headers['PHP_AUTH_DIGEST'])) { |
| 119 | $headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST']; |
| 120 | } |
| 121 | |
| 122 | return $this->headers = $headers; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set a header on the request. |
| 127 | * |
| 128 | * @param string $key |
| 129 | * @param string $value |
| 130 | */ |
| 131 | public function setHeader($key, $value) |
| 132 | { |
| 133 | if (empty($this->headers)) { |
| 134 | $this->setHeaders(); |
| 135 | } |
| 136 | |
| 137 | $this->headers[$key] = $value; |
| 138 | } |
| 139 | } |
| 140 |