abb75afedeefd86a9d124b3d2c0b49590611639d
[yaffs-website] / src / functions / marshal_headers_from_sapi.php
1 <?php
2 /**
3  * @see       https://github.com/zendframework/zend-diactoros for the canonical source repository
4  * @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5  * @license   https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6  */
7
8 namespace Zend\Diactoros;
9
10 use function array_key_exists;
11 use function strpos;
12 use function strtolower;
13 use function strtr;
14 use function substr;
15
16 /**
17  * @param array $server Values obtained from the SAPI (generally `$_SERVER`).
18  * @return array Header/value pairs
19  */
20 function marshalHeadersFromSapi(array $server)
21 {
22     $headers = [];
23     foreach ($server as $key => $value) {
24         // Apache prefixes environment variables with REDIRECT_
25         // if they are added by rewrite rules
26         if (strpos($key, 'REDIRECT_') === 0) {
27             $key = substr($key, 9);
28
29             // We will not overwrite existing variables with the
30             // prefixed versions, though
31             if (array_key_exists($key, $server)) {
32                 continue;
33             }
34         }
35
36         if ($value && strpos($key, 'HTTP_') === 0) {
37             $name = strtr(strtolower(substr($key, 5)), '_', '-');
38             $headers[$name] = $value;
39             continue;
40         }
41
42         if ($value && strpos($key, 'CONTENT_') === 0) {
43             $name = 'content-' . strtolower(substr($key, 8));
44             $headers[$name] = $value;
45             continue;
46         }
47     }
48
49     return $headers;
50 }