|
| 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 | + * HTTP header utility functions. |
| 16 | + * |
| 17 | + * @author Christian Schmidt <[email protected]> |
| 18 | + */ |
| 19 | +class HeaderUtils |
| 20 | +{ |
| 21 | + /** |
| 22 | + * This class should not be instantiated. |
| 23 | + */ |
| 24 | + private function __construct() |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Splits an HTTP header by one or more separators. |
| 30 | + * |
| 31 | + * Example: |
| 32 | + * |
| 33 | + * HeaderUtils::split("da, en-gb;q=0.8", ",;") |
| 34 | + * // => array(array("da"), array("en-gb"), array("q", "0.8")) |
| 35 | + * |
| 36 | + * @param string $header HTTP header value |
| 37 | + * @param string $separators List of characters to split on, ordered by |
| 38 | + * precedence, e.g. ",", ";=", or ",;=" |
| 39 | + * |
| 40 | + * @return array Nested array with as many levels as there are characters in |
| 41 | + * $separators |
| 42 | + */ |
| 43 | + public static function split(string $header, $separators) |
| 44 | + { |
| 45 | + $quotedSeparators = preg_quote($separators); |
| 46 | + |
| 47 | + preg_match_all(' |
| 48 | + / |
| 49 | + (?!\s) |
| 50 | + (?: |
| 51 | + # quoted-string |
| 52 | + "(?:[^"\\\\]|\\\\.)*(?:"|\\\\|$) |
| 53 | + | |
| 54 | + # token |
| 55 | + [^"'.$quotedSeparators.']+ |
| 56 | + )+ |
| 57 | + (?<!\s) |
| 58 | + | |
| 59 | + # separator |
| 60 | + \s* |
| 61 | + (?<separator>['.$quotedSeparators.']) |
| 62 | + \s* |
| 63 | + /x', trim($header), $matches, PREG_SET_ORDER); |
| 64 | + |
| 65 | + return self::groupParts($matches, $separators); |
| 66 | + } |
| 67 | + |
| 68 | + private static function groupParts(array $matches, $separators) |
| 69 | + { |
| 70 | + $separator = $separators[0]; |
| 71 | + $partSeparators = substr($separators, 1); |
| 72 | + |
| 73 | + $i = 0; |
| 74 | + $partMatches = array(); |
| 75 | + foreach ($matches as $match) { |
| 76 | + if (isset($match['separator']) && $match['separator'] === $separator) { |
| 77 | + ++$i; |
| 78 | + } else { |
| 79 | + $partMatches[$i][] = $match; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + $parts = array(); |
| 84 | + if ($partSeparators) { |
| 85 | + foreach ($partMatches as $matches) { |
| 86 | + $parts[] = self::groupParts($matches, $partSeparators); |
| 87 | + } |
| 88 | + } else { |
| 89 | + foreach ($partMatches as $matches) { |
| 90 | + $parts[] = self::unquote($matches[0][0]); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return $parts; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Combines an array of arrays into one associative array. |
| 99 | + * |
| 100 | + * Each of the nested arrays should have one or two elements. The first |
| 101 | + * value will be used as the keys in the associative array, and the second |
| 102 | + * will be used as the values, or true if the nested array only contains one |
| 103 | + * element. |
| 104 | + * |
| 105 | + * Example: |
| 106 | + * |
| 107 | + * HeaderUtils::combineParts(array(array("foo", "abc"), array("bar"))) |
| 108 | + * // => array("foo" => "abc", "bar" => true) |
| 109 | + * |
| 110 | + * @param array $parts Array of arrays |
| 111 | + * |
| 112 | + * @return array Associative array |
| 113 | + */ |
| 114 | + public static function combineParts(array $parts) |
| 115 | + { |
| 116 | + $assoc = array(); |
| 117 | + foreach ($parts as $part) { |
| 118 | + $name = strtolower($part[0]); |
| 119 | + $value = isset($part[1]) ? $part[1] : true; |
| 120 | + $assoc[$name] = $value; |
| 121 | + } |
| 122 | + |
| 123 | + return $assoc; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Joins an associative array into a string. |
| 128 | + * |
| 129 | + * The key and value of each entry are joined with "=", and all entries |
| 130 | + * is joined with the specified separator and an additional space (for |
| 131 | + * readability). Values are quoted if necessary. |
| 132 | + * |
| 133 | + * Example: |
| 134 | + * |
| 135 | + * HeaderUtils::joinAssoc(array("foo" => "abc", "bar" => true, "baz" => "a b c"), ",") |
| 136 | + * // => 'foo=bar, baz, baz="a b c"' |
| 137 | + * |
| 138 | + * @param array $assoc The associative array |
| 139 | + * @param string $separator Separator between each array entry |
| 140 | + * |
| 141 | + * @return string A string formatted for use in an HTTP header |
| 142 | + */ |
| 143 | + public static function joinAssoc(array $assoc, $separator) |
| 144 | + { |
| 145 | + $parts = array(); |
| 146 | + foreach ($assoc as $name => $value) { |
| 147 | + if (true === $value) { |
| 148 | + $parts[] = $name; |
| 149 | + } else { |
| 150 | + $parts[] = $name.'='.self::quote($value); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return implode($separator.' ', $parts); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Encodes a string as a quoted string, if necessary. |
| 159 | + * |
| 160 | + * If a string contains characters not allowed by the "token" construct in |
| 161 | + * the HTTP specification, it is backslash-escaped and enclosed in quotes |
| 162 | + * to match the "quoted-string" construct. |
| 163 | + * |
| 164 | + * @param string $s The raw string |
| 165 | + * |
| 166 | + * @return return The quoted string |
| 167 | + */ |
| 168 | + public static function quote($s) |
| 169 | + { |
| 170 | + if (preg_match('/^[a-z0-9!#$%&\'*.^_`|~-]+$/i', $s)) { |
| 171 | + return $s; |
| 172 | + } |
| 173 | + |
| 174 | + return '"'.addcslashes($s, '"\\"').'"'; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Decodes a quoted string. |
| 179 | + * |
| 180 | + * If passed an unquoted string that matches the "token" construct (as |
| 181 | + * defined in the HTTP specification), it is passed through verbatimly. |
| 182 | + * |
| 183 | + * @param string $s The quoted string |
| 184 | + * |
| 185 | + * @return string The raw string |
| 186 | + */ |
| 187 | + public static function unquote($s) |
| 188 | + { |
| 189 | + return preg_replace('/\\\\(.)|"/', '$1', $s); |
| 190 | + } |
| 191 | +} |
0 commit comments