secure-custom-fields
/
vendor
/
justinrainbow
/
json-schema
/
src
/
JsonSchema
/
Uri
/
UriResolver.php
secure-custom-fields
/
vendor
/
justinrainbow
/
json-schema
/
src
/
JsonSchema
/
Uri
Last commit date
Retrievers
9 months ago
UriResolver.php
9 months ago
UriRetriever.php
9 months ago
UriResolver.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the JsonSchema package. |
| 5 | * |
| 6 | * For the full copyright and license information, please view the LICENSE |
| 7 | * file that was distributed with this source code. |
| 8 | */ |
| 9 | |
| 10 | namespace JsonSchema\Uri; |
| 11 | |
| 12 | use JsonSchema\Exception\UriResolverException; |
| 13 | use JsonSchema\UriResolverInterface; |
| 14 | |
| 15 | /** |
| 16 | * Resolves JSON Schema URIs |
| 17 | * |
| 18 | * @author Sander Coolen <sander@jibber.nl> |
| 19 | */ |
| 20 | class UriResolver implements UriResolverInterface |
| 21 | { |
| 22 | /** |
| 23 | * Parses a URI into five main components |
| 24 | * |
| 25 | * @param string $uri |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | public function parse($uri) |
| 30 | { |
| 31 | preg_match('|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?|', $uri, $match); |
| 32 | |
| 33 | $components = array(); |
| 34 | if (5 < count($match)) { |
| 35 | $components = array( |
| 36 | 'scheme' => $match[2], |
| 37 | 'authority' => $match[4], |
| 38 | 'path' => $match[5] |
| 39 | ); |
| 40 | } |
| 41 | if (7 < count($match)) { |
| 42 | $components['query'] = $match[7]; |
| 43 | } |
| 44 | if (9 < count($match)) { |
| 45 | $components['fragment'] = $match[9]; |
| 46 | } |
| 47 | |
| 48 | return $components; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Builds a URI based on n array with the main components |
| 53 | * |
| 54 | * @param array $components |
| 55 | * |
| 56 | * @return string |
| 57 | */ |
| 58 | public function generate(array $components) |
| 59 | { |
| 60 | $uri = $components['scheme'] . '://' |
| 61 | . $components['authority'] |
| 62 | . $components['path']; |
| 63 | |
| 64 | if (array_key_exists('query', $components) && strlen($components['query'])) { |
| 65 | $uri .= '?' . $components['query']; |
| 66 | } |
| 67 | if (array_key_exists('fragment', $components)) { |
| 68 | $uri .= '#' . $components['fragment']; |
| 69 | } |
| 70 | |
| 71 | return $uri; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * {@inheritdoc} |
| 76 | */ |
| 77 | public function resolve($uri, $baseUri = null) |
| 78 | { |
| 79 | // treat non-uri base as local file path |
| 80 | if ( |
| 81 | !is_null($baseUri) && |
| 82 | !filter_var($baseUri, \FILTER_VALIDATE_URL) && |
| 83 | !preg_match('|^[^/]+://|u', $baseUri) |
| 84 | ) { |
| 85 | if (is_file($baseUri)) { |
| 86 | $baseUri = 'file://' . realpath($baseUri); |
| 87 | } elseif (is_dir($baseUri)) { |
| 88 | $baseUri = 'file://' . realpath($baseUri) . '/'; |
| 89 | } else { |
| 90 | $baseUri = 'file://' . getcwd() . '/' . $baseUri; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if ($uri == '') { |
| 95 | return $baseUri; |
| 96 | } |
| 97 | |
| 98 | $components = $this->parse($uri); |
| 99 | $path = $components['path']; |
| 100 | |
| 101 | if (!empty($components['scheme'])) { |
| 102 | return $uri; |
| 103 | } |
| 104 | $baseComponents = $this->parse($baseUri); |
| 105 | $basePath = $baseComponents['path']; |
| 106 | |
| 107 | $baseComponents['path'] = self::combineRelativePathWithBasePath($path, $basePath); |
| 108 | if (isset($components['fragment'])) { |
| 109 | $baseComponents['fragment'] = $components['fragment']; |
| 110 | } |
| 111 | |
| 112 | return $this->generate($baseComponents); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Tries to glue a relative path onto an absolute one |
| 117 | * |
| 118 | * @param string $relativePath |
| 119 | * @param string $basePath |
| 120 | * |
| 121 | * @throws UriResolverException |
| 122 | * |
| 123 | * @return string Merged path |
| 124 | */ |
| 125 | public static function combineRelativePathWithBasePath($relativePath, $basePath) |
| 126 | { |
| 127 | $relativePath = self::normalizePath($relativePath); |
| 128 | if ($relativePath == '') { |
| 129 | return $basePath; |
| 130 | } |
| 131 | if ($relativePath[0] == '/') { |
| 132 | return $relativePath; |
| 133 | } |
| 134 | |
| 135 | $basePathSegments = explode('/', $basePath); |
| 136 | |
| 137 | preg_match('|^/?(\.\./(?:\./)*)*|', $relativePath, $match); |
| 138 | $numLevelUp = strlen($match[0]) /3 + 1; |
| 139 | if ($numLevelUp >= count($basePathSegments)) { |
| 140 | throw new UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath)); |
| 141 | } |
| 142 | |
| 143 | $basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp); |
| 144 | $path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath); |
| 145 | |
| 146 | return implode('/', $basePathSegments) . '/' . $path; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Normalizes a URI path component by removing dot-slash and double slashes |
| 151 | * |
| 152 | * @param string $path |
| 153 | * |
| 154 | * @return string |
| 155 | */ |
| 156 | private static function normalizePath($path) |
| 157 | { |
| 158 | $path = preg_replace('|((?<!\.)\./)*|', '', $path); |
| 159 | $path = preg_replace('|//|', '/', $path); |
| 160 | |
| 161 | return $path; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @param string $uri |
| 166 | * |
| 167 | * @return bool |
| 168 | */ |
| 169 | public function isValid($uri) |
| 170 | { |
| 171 | $components = $this->parse($uri); |
| 172 | |
| 173 | return !empty($components); |
| 174 | } |
| 175 | } |
| 176 |