UriTemplate.php
267 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2013 Google Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | namespace WPMailSMTP\Vendor\Google\Utils; |
| 19 | |
| 20 | /** |
| 21 | * Implementation of levels 1-3 of the URI Template spec. |
| 22 | * @see http://tools.ietf.org/html/rfc6570 |
| 23 | */ |
| 24 | class UriTemplate |
| 25 | { |
| 26 | const TYPE_MAP = "1"; |
| 27 | const TYPE_LIST = "2"; |
| 28 | const TYPE_SCALAR = "4"; |
| 29 | /** |
| 30 | * @var $operators array |
| 31 | * These are valid at the start of a template block to |
| 32 | * modify the way in which the variables inside are |
| 33 | * processed. |
| 34 | */ |
| 35 | private $operators = array("+" => "reserved", "/" => "segments", "." => "dotprefix", "#" => "fragment", ";" => "semicolon", "?" => "form", "&" => "continuation"); |
| 36 | /** |
| 37 | * @var reserved array |
| 38 | * These are the characters which should not be URL encoded in reserved |
| 39 | * strings. |
| 40 | */ |
| 41 | private $reserved = array("=", ",", "!", "@", "|", ":", "/", "?", "#", "[", "]", '$', "&", "'", "(", ")", "*", "+", ";"); |
| 42 | private $reservedEncoded = array("%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F", "%23", "%5B", "%5D", "%24", "%26", "%27", "%28", "%29", "%2A", "%2B", "%3B"); |
| 43 | public function parse($string, array $parameters) |
| 44 | { |
| 45 | return $this->resolveNextSection($string, $parameters); |
| 46 | } |
| 47 | /** |
| 48 | * This function finds the first matching {...} block and |
| 49 | * executes the replacement. It then calls itself to find |
| 50 | * subsequent blocks, if any. |
| 51 | */ |
| 52 | private function resolveNextSection($string, $parameters) |
| 53 | { |
| 54 | $start = \strpos($string, "{"); |
| 55 | if ($start === \false) { |
| 56 | return $string; |
| 57 | } |
| 58 | $end = \strpos($string, "}"); |
| 59 | if ($end === \false) { |
| 60 | return $string; |
| 61 | } |
| 62 | $string = $this->replace($string, $start, $end, $parameters); |
| 63 | return $this->resolveNextSection($string, $parameters); |
| 64 | } |
| 65 | private function replace($string, $start, $end, $parameters) |
| 66 | { |
| 67 | // We know a data block will have {} round it, so we can strip that. |
| 68 | $data = \substr($string, $start + 1, $end - $start - 1); |
| 69 | // If the first character is one of the reserved operators, it effects |
| 70 | // the processing of the stream. |
| 71 | if (isset($this->operators[$data[0]])) { |
| 72 | $op = $this->operators[$data[0]]; |
| 73 | $data = \substr($data, 1); |
| 74 | $prefix = ""; |
| 75 | $prefix_on_missing = \false; |
| 76 | switch ($op) { |
| 77 | case "reserved": |
| 78 | // Reserved means certain characters should not be URL encoded |
| 79 | $data = $this->replaceVars($data, $parameters, ",", null, \true); |
| 80 | break; |
| 81 | case "fragment": |
| 82 | // Comma separated with fragment prefix. Bare values only. |
| 83 | $prefix = "#"; |
| 84 | $prefix_on_missing = \true; |
| 85 | $data = $this->replaceVars($data, $parameters, ",", null, \true); |
| 86 | break; |
| 87 | case "segments": |
| 88 | // Slash separated data. Bare values only. |
| 89 | $prefix = "/"; |
| 90 | $data = $this->replaceVars($data, $parameters, "/"); |
| 91 | break; |
| 92 | case "dotprefix": |
| 93 | // Dot separated data. Bare values only. |
| 94 | $prefix = "."; |
| 95 | $prefix_on_missing = \true; |
| 96 | $data = $this->replaceVars($data, $parameters, "."); |
| 97 | break; |
| 98 | case "semicolon": |
| 99 | // Semicolon prefixed and separated. Uses the key name |
| 100 | $prefix = ";"; |
| 101 | $data = $this->replaceVars($data, $parameters, ";", "=", \false, \true, \false); |
| 102 | break; |
| 103 | case "form": |
| 104 | // Standard URL format. Uses the key name |
| 105 | $prefix = "?"; |
| 106 | $data = $this->replaceVars($data, $parameters, "&", "="); |
| 107 | break; |
| 108 | case "continuation": |
| 109 | // Standard URL, but with leading ampersand. Uses key name. |
| 110 | $prefix = "&"; |
| 111 | $data = $this->replaceVars($data, $parameters, "&", "="); |
| 112 | break; |
| 113 | } |
| 114 | // Add the initial prefix character if data is valid. |
| 115 | if ($data || $data !== \false && $prefix_on_missing) { |
| 116 | $data = $prefix . $data; |
| 117 | } |
| 118 | } else { |
| 119 | // If no operator we replace with the defaults. |
| 120 | $data = $this->replaceVars($data, $parameters); |
| 121 | } |
| 122 | // This is chops out the {...} and replaces with the new section. |
| 123 | return \substr($string, 0, $start) . $data . \substr($string, $end + 1); |
| 124 | } |
| 125 | private function replaceVars($section, $parameters, $sep = ",", $combine = null, $reserved = \false, $tag_empty = \false, $combine_on_empty = \true) |
| 126 | { |
| 127 | if (\strpos($section, ",") === \false) { |
| 128 | // If we only have a single value, we can immediately process. |
| 129 | return $this->combine($section, $parameters, $sep, $combine, $reserved, $tag_empty, $combine_on_empty); |
| 130 | } else { |
| 131 | // If we have multiple values, we need to split and loop over them. |
| 132 | // Each is treated individually, then glued together with the |
| 133 | // separator character. |
| 134 | $vars = \explode(",", $section); |
| 135 | return $this->combineList( |
| 136 | $vars, |
| 137 | $sep, |
| 138 | $parameters, |
| 139 | $combine, |
| 140 | $reserved, |
| 141 | \false, |
| 142 | // Never emit empty strings in multi-param replacements |
| 143 | $combine_on_empty |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 | public function combine($key, $parameters, $sep, $combine, $reserved, $tag_empty, $combine_on_empty) |
| 148 | { |
| 149 | $length = \false; |
| 150 | $explode = \false; |
| 151 | $skip_final_combine = \false; |
| 152 | $value = \false; |
| 153 | // Check for length restriction. |
| 154 | if (\strpos($key, ":") !== \false) { |
| 155 | list($key, $length) = \explode(":", $key); |
| 156 | } |
| 157 | // Check for explode parameter. |
| 158 | if ($key[\strlen($key) - 1] == "*") { |
| 159 | $explode = \true; |
| 160 | $key = \substr($key, 0, -1); |
| 161 | $skip_final_combine = \true; |
| 162 | } |
| 163 | // Define the list separator. |
| 164 | $list_sep = $explode ? $sep : ","; |
| 165 | if (isset($parameters[$key])) { |
| 166 | $data_type = $this->getDataType($parameters[$key]); |
| 167 | switch ($data_type) { |
| 168 | case self::TYPE_SCALAR: |
| 169 | $value = $this->getValue($parameters[$key], $length); |
| 170 | break; |
| 171 | case self::TYPE_LIST: |
| 172 | $values = array(); |
| 173 | foreach ($parameters[$key] as $pkey => $pvalue) { |
| 174 | $pvalue = $this->getValue($pvalue, $length); |
| 175 | if ($combine && $explode) { |
| 176 | $values[$pkey] = $key . $combine . $pvalue; |
| 177 | } else { |
| 178 | $values[$pkey] = $pvalue; |
| 179 | } |
| 180 | } |
| 181 | $value = \implode($list_sep, $values); |
| 182 | if ($value == '') { |
| 183 | return ''; |
| 184 | } |
| 185 | break; |
| 186 | case self::TYPE_MAP: |
| 187 | $values = array(); |
| 188 | foreach ($parameters[$key] as $pkey => $pvalue) { |
| 189 | $pvalue = $this->getValue($pvalue, $length); |
| 190 | if ($explode) { |
| 191 | $pkey = $this->getValue($pkey, $length); |
| 192 | $values[] = $pkey . "=" . $pvalue; |
| 193 | // Explode triggers = combine. |
| 194 | } else { |
| 195 | $values[] = $pkey; |
| 196 | $values[] = $pvalue; |
| 197 | } |
| 198 | } |
| 199 | $value = \implode($list_sep, $values); |
| 200 | if ($value == '') { |
| 201 | return \false; |
| 202 | } |
| 203 | break; |
| 204 | } |
| 205 | } else { |
| 206 | if ($tag_empty) { |
| 207 | // If we are just indicating empty values with their key name, return that. |
| 208 | return $key; |
| 209 | } else { |
| 210 | // Otherwise we can skip this variable due to not being defined. |
| 211 | return \false; |
| 212 | } |
| 213 | } |
| 214 | if ($reserved) { |
| 215 | $value = \str_replace($this->reservedEncoded, $this->reserved, $value); |
| 216 | } |
| 217 | // If we do not need to include the key name, we just return the raw |
| 218 | // value. |
| 219 | if (!$combine || $skip_final_combine) { |
| 220 | return $value; |
| 221 | } |
| 222 | // Else we combine the key name: foo=bar, if value is not the empty string. |
| 223 | return $key . ($value != '' || $combine_on_empty ? $combine . $value : ''); |
| 224 | } |
| 225 | /** |
| 226 | * Return the type of a passed in value |
| 227 | */ |
| 228 | private function getDataType($data) |
| 229 | { |
| 230 | if (\is_array($data)) { |
| 231 | \reset($data); |
| 232 | if (\key($data) !== 0) { |
| 233 | return self::TYPE_MAP; |
| 234 | } |
| 235 | return self::TYPE_LIST; |
| 236 | } |
| 237 | return self::TYPE_SCALAR; |
| 238 | } |
| 239 | /** |
| 240 | * Utility function that merges multiple combine calls |
| 241 | * for multi-key templates. |
| 242 | */ |
| 243 | private function combineList($vars, $sep, $parameters, $combine, $reserved, $tag_empty, $combine_on_empty) |
| 244 | { |
| 245 | $ret = array(); |
| 246 | foreach ($vars as $var) { |
| 247 | $response = $this->combine($var, $parameters, $sep, $combine, $reserved, $tag_empty, $combine_on_empty); |
| 248 | if ($response === \false) { |
| 249 | continue; |
| 250 | } |
| 251 | $ret[] = $response; |
| 252 | } |
| 253 | return \implode($sep, $ret); |
| 254 | } |
| 255 | /** |
| 256 | * Utility function to encode and trim values |
| 257 | */ |
| 258 | private function getValue($value, $length) |
| 259 | { |
| 260 | if ($length) { |
| 261 | $value = \substr($value, 0, $length); |
| 262 | } |
| 263 | $value = \rawurlencode($value); |
| 264 | return $value; |
| 265 | } |
| 266 | } |
| 267 |