Cookie
3 years ago
Exception
3 years ago
Handler
3 years ago
Client.php
3 years ago
ClientInterface.php
3 years ago
HandlerStack.php
3 years ago
MessageFormatter.php
3 years ago
Middleware.php
3 years ago
Pool.php
3 years ago
PrepareBodyMiddleware.php
3 years ago
RedirectMiddleware.php
3 years ago
RequestOptions.php
3 years ago
RetryMiddleware.php
3 years ago
TransferStats.php
3 years ago
UriTemplate.php
3 years ago
Utils.php
3 years ago
functions.php
3 years ago
functions_include.php
3 years ago
UriTemplate.php
192 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Vendor\GuzzleHttp; |
| 4 | |
| 5 | /** |
| 6 | * Expands URI templates. Userland implementation of PECL uri_template. |
| 7 | * |
| 8 | * @link http://tools.ietf.org/html/rfc6570 |
| 9 | */ |
| 10 | class UriTemplate |
| 11 | { |
| 12 | /** @var string URI template */ |
| 13 | private $template; |
| 14 | /** @var array Variables to use in the template expansion */ |
| 15 | private $variables; |
| 16 | /** @var array Hash for quick operator lookups */ |
| 17 | private static $operatorHash = ['' => ['prefix' => '', 'joiner' => ',', 'query' => \false], '+' => ['prefix' => '', 'joiner' => ',', 'query' => \false], '#' => ['prefix' => '#', 'joiner' => ',', 'query' => \false], '.' => ['prefix' => '.', 'joiner' => '.', 'query' => \false], '/' => ['prefix' => '/', 'joiner' => '/', 'query' => \false], ';' => ['prefix' => ';', 'joiner' => ';', 'query' => \true], '?' => ['prefix' => '?', 'joiner' => '&', 'query' => \true], '&' => ['prefix' => '&', 'joiner' => '&', 'query' => \true]]; |
| 18 | /** @var array Delimiters */ |
| 19 | private static $delims = [':', '/', '?', '#', '[', ']', '@', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=']; |
| 20 | /** @var array Percent encoded delimiters */ |
| 21 | private static $delimsPct = ['%3A', '%2F', '%3F', '%23', '%5B', '%5D', '%40', '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D']; |
| 22 | public function expand($template, array $variables) |
| 23 | { |
| 24 | if (\false === \strpos($template, '{')) { |
| 25 | return $template; |
| 26 | } |
| 27 | $this->template = $template; |
| 28 | $this->variables = $variables; |
| 29 | return \preg_replace_callback('/\\{([^\\}]+)\\}/', [$this, 'expandMatch'], $this->template); |
| 30 | } |
| 31 | /** |
| 32 | * Parse an expression into parts |
| 33 | * |
| 34 | * @param string $expression Expression to parse |
| 35 | * |
| 36 | * @return array Returns an associative array of parts |
| 37 | */ |
| 38 | private function parseExpression($expression) |
| 39 | { |
| 40 | $result = []; |
| 41 | if (isset(self::$operatorHash[$expression[0]])) { |
| 42 | $result['operator'] = $expression[0]; |
| 43 | $expression = \substr($expression, 1); |
| 44 | } else { |
| 45 | $result['operator'] = ''; |
| 46 | } |
| 47 | foreach (\explode(',', $expression) as $value) { |
| 48 | $value = \trim($value); |
| 49 | $varspec = []; |
| 50 | if ($colonPos = \strpos($value, ':')) { |
| 51 | $varspec['value'] = \substr($value, 0, $colonPos); |
| 52 | $varspec['modifier'] = ':'; |
| 53 | $varspec['position'] = (int) \substr($value, $colonPos + 1); |
| 54 | } elseif (\substr($value, -1) === '*') { |
| 55 | $varspec['modifier'] = '*'; |
| 56 | $varspec['value'] = \substr($value, 0, -1); |
| 57 | } else { |
| 58 | $varspec['value'] = (string) $value; |
| 59 | $varspec['modifier'] = ''; |
| 60 | } |
| 61 | $result['values'][] = $varspec; |
| 62 | } |
| 63 | return $result; |
| 64 | } |
| 65 | /** |
| 66 | * Process an expansion |
| 67 | * |
| 68 | * @param array $matches Matches met in the preg_replace_callback |
| 69 | * |
| 70 | * @return string Returns the replacement string |
| 71 | */ |
| 72 | private function expandMatch(array $matches) |
| 73 | { |
| 74 | static $rfc1738to3986 = ['+' => '%20', '%7e' => '~']; |
| 75 | $replacements = []; |
| 76 | $parsed = self::parseExpression($matches[1]); |
| 77 | $prefix = self::$operatorHash[$parsed['operator']]['prefix']; |
| 78 | $joiner = self::$operatorHash[$parsed['operator']]['joiner']; |
| 79 | $useQuery = self::$operatorHash[$parsed['operator']]['query']; |
| 80 | foreach ($parsed['values'] as $value) { |
| 81 | if (!isset($this->variables[$value['value']])) { |
| 82 | continue; |
| 83 | } |
| 84 | $variable = $this->variables[$value['value']]; |
| 85 | $actuallyUseQuery = $useQuery; |
| 86 | $expanded = ''; |
| 87 | if (\is_array($variable)) { |
| 88 | $isAssoc = $this->isAssoc($variable); |
| 89 | $kvp = []; |
| 90 | foreach ($variable as $key => $var) { |
| 91 | if ($isAssoc) { |
| 92 | $key = \rawurlencode($key); |
| 93 | $isNestedArray = \is_array($var); |
| 94 | } else { |
| 95 | $isNestedArray = \false; |
| 96 | } |
| 97 | if (!$isNestedArray) { |
| 98 | $var = \rawurlencode($var); |
| 99 | if ($parsed['operator'] === '+' || $parsed['operator'] === '#') { |
| 100 | $var = $this->decodeReserved($var); |
| 101 | } |
| 102 | } |
| 103 | if ($value['modifier'] === '*') { |
| 104 | if ($isAssoc) { |
| 105 | if ($isNestedArray) { |
| 106 | // Nested arrays must allow for deeply nested |
| 107 | // structures. |
| 108 | $var = \strtr(\http_build_query([$key => $var]), $rfc1738to3986); |
| 109 | } else { |
| 110 | $var = $key . '=' . $var; |
| 111 | } |
| 112 | } elseif ($key > 0 && $actuallyUseQuery) { |
| 113 | $var = $value['value'] . '=' . $var; |
| 114 | } |
| 115 | } |
| 116 | $kvp[$key] = $var; |
| 117 | } |
| 118 | if (empty($variable)) { |
| 119 | $actuallyUseQuery = \false; |
| 120 | } elseif ($value['modifier'] === '*') { |
| 121 | $expanded = \implode($joiner, $kvp); |
| 122 | if ($isAssoc) { |
| 123 | // Don't prepend the value name when using the explode |
| 124 | // modifier with an associative array. |
| 125 | $actuallyUseQuery = \false; |
| 126 | } |
| 127 | } else { |
| 128 | if ($isAssoc) { |
| 129 | // When an associative array is encountered and the |
| 130 | // explode modifier is not set, then the result must be |
| 131 | // a comma separated list of keys followed by their |
| 132 | // respective values. |
| 133 | foreach ($kvp as $k => &$v) { |
| 134 | $v = $k . ',' . $v; |
| 135 | } |
| 136 | } |
| 137 | $expanded = \implode(',', $kvp); |
| 138 | } |
| 139 | } else { |
| 140 | if ($value['modifier'] === ':') { |
| 141 | $variable = \substr($variable, 0, $value['position']); |
| 142 | } |
| 143 | $expanded = \rawurlencode($variable); |
| 144 | if ($parsed['operator'] === '+' || $parsed['operator'] === '#') { |
| 145 | $expanded = $this->decodeReserved($expanded); |
| 146 | } |
| 147 | } |
| 148 | if ($actuallyUseQuery) { |
| 149 | if (!$expanded && $joiner !== '&') { |
| 150 | $expanded = $value['value']; |
| 151 | } else { |
| 152 | $expanded = $value['value'] . '=' . $expanded; |
| 153 | } |
| 154 | } |
| 155 | $replacements[] = $expanded; |
| 156 | } |
| 157 | $ret = \implode($joiner, $replacements); |
| 158 | if ($ret && $prefix) { |
| 159 | return $prefix . $ret; |
| 160 | } |
| 161 | return $ret; |
| 162 | } |
| 163 | /** |
| 164 | * Determines if an array is associative. |
| 165 | * |
| 166 | * This makes the assumption that input arrays are sequences or hashes. |
| 167 | * This assumption is a tradeoff for accuracy in favor of speed, but it |
| 168 | * should work in almost every case where input is supplied for a URI |
| 169 | * template. |
| 170 | * |
| 171 | * @param array $array Array to check |
| 172 | * |
| 173 | * @return bool |
| 174 | */ |
| 175 | private function isAssoc(array $array) |
| 176 | { |
| 177 | return $array && \array_keys($array)[0] !== 0; |
| 178 | } |
| 179 | /** |
| 180 | * Removes percent encoding on reserved characters (used with + and # |
| 181 | * modifiers). |
| 182 | * |
| 183 | * @param string $string String to fix |
| 184 | * |
| 185 | * @return string |
| 186 | */ |
| 187 | private function decodeReserved($string) |
| 188 | { |
| 189 | return \str_replace(self::$delimsPct, self::$delims, $string); |
| 190 | } |
| 191 | } |
| 192 |