/usr/share/php/JsonSchema/Uri/UriRetriever.php is in php-json-schema 1.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | <?php
/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JsonSchema\Uri;
use JsonSchema\Uri\Retrievers\FileGetContents;
use JsonSchema\Uri\Retrievers\UriRetrieverInterface;
use JsonSchema\Validator;
use JsonSchema\Exception\InvalidSchemaMediaTypeException;
use JsonSchema\Exception\JsonDecodingException;
use JsonSchema\Exception\ResourceNotFoundException;
/**
* Retrieves JSON Schema URIs
*
* @author Tyler Akins <fidian@rumkin.com>
*/
class UriRetriever
{
/**
* @var null|UriRetrieverInterface
*/
protected $uriRetriever = null;
/**
* @var array|object[]
* @see loadSchema
*/
private $schemaCache = array();
/**
* Guarantee the correct media type was encountered
*
* @param UriRetrieverInterface $uriRetriever
* @param string $uri
* @return bool|void
*/
public function confirmMediaType($uriRetriever, $uri)
{
$contentType = $uriRetriever->getContentType();
if (is_null($contentType)) {
// Well, we didn't get an invalid one
return;
}
if (Validator::SCHEMA_MEDIA_TYPE === $contentType) {
return;
}
if (substr($uri, 0, 23) == 'http://json-schema.org/') {
//HACK; they deliver broken content types
return true;
}
throw new InvalidSchemaMediaTypeException(sprintf('Media type %s expected', Validator::SCHEMA_MEDIA_TYPE));
}
/**
* Get a URI Retriever
*
* If none is specified, sets a default FileGetContents retriever and
* returns that object.
*
* @return UriRetrieverInterface
*/
public function getUriRetriever()
{
if (is_null($this->uriRetriever)) {
$this->setUriRetriever(new FileGetContents);
}
return $this->uriRetriever;
}
/**
* Resolve a schema based on pointer
*
* URIs can have a fragment at the end in the format of
* #/path/to/object and we are to look up the 'path' property of
* the first object then the 'to' and 'object' properties.
*
* @param object $jsonSchema JSON Schema contents
* @param string $uri JSON Schema URI
* @return object JSON Schema after walking down the fragment pieces
*
* @throws ResourceNotFoundException
*/
public function resolvePointer($jsonSchema, $uri)
{
$resolver = new UriResolver();
$parsed = $resolver->parse($uri);
if (empty($parsed['fragment'])) {
return $jsonSchema;
}
$path = explode('/', $parsed['fragment']);
while ($path) {
$pathElement = array_shift($path);
if (! empty($pathElement)) {
$pathElement = str_replace('~1', '/', $pathElement);
$pathElement = str_replace('~0', '~', $pathElement);
if (! empty($jsonSchema->$pathElement)) {
$jsonSchema = $jsonSchema->$pathElement;
} else {
throw new ResourceNotFoundException(
'Fragment "' . $parsed['fragment'] . '" not found'
. ' in ' . $uri
);
}
if (! is_object($jsonSchema)) {
throw new ResourceNotFoundException(
'Fragment part "' . $pathElement . '" is no object '
. ' in ' . $uri
);
}
}
}
return $jsonSchema;
}
/**
* Retrieve a URI
*
* @param string $uri JSON Schema URI
* @param string|null $baseUri
* @return object JSON Schema contents
*/
public function retrieve($uri, $baseUri = null)
{
$resolver = new UriResolver();
$resolvedUri = $fetchUri = $resolver->resolve($uri, $baseUri);
//fetch URL without #fragment
$arParts = $resolver->parse($resolvedUri);
if (isset($arParts['fragment'])) {
unset($arParts['fragment']);
$fetchUri = $resolver->generate($arParts);
}
$jsonSchema = $this->loadSchema($fetchUri);
// Use the JSON pointer if specified
$jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);
if ($jsonSchema instanceof \stdClass) {
$jsonSchema->id = $resolvedUri;
}
return $jsonSchema;
}
/**
* Fetch a schema from the given URI, json-decode it and return it.
* Caches schema objects.
*
* @param string $fetchUri Absolute URI
*
* @return object JSON schema object
*/
protected function loadSchema($fetchUri)
{
if (isset($this->schemaCache[$fetchUri])) {
return $this->schemaCache[$fetchUri];
}
$uriRetriever = $this->getUriRetriever();
$contents = $this->uriRetriever->retrieve($fetchUri);
$this->confirmMediaType($uriRetriever, $fetchUri);
$jsonSchema = json_decode($contents);
if (JSON_ERROR_NONE < $error = json_last_error()) {
throw new JsonDecodingException($error);
}
$this->schemaCache[$fetchUri] = $jsonSchema;
return $jsonSchema;
}
/**
* Set the URI Retriever
*
* @param UriRetrieverInterface $uriRetriever
* @return $this for chaining
*/
public function setUriRetriever(UriRetrieverInterface $uriRetriever)
{
$this->uriRetriever = $uriRetriever;
return $this;
}
/**
* Parses a URI into five main components
*
* @param string $uri
* @return array
*/
public function parse($uri)
{
preg_match('|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?|', $uri, $match);
$components = array();
if (5 < count($match)) {
$components = array(
'scheme' => $match[2],
'authority' => $match[4],
'path' => $match[5]
);
}
if (7 < count($match)) {
$components['query'] = $match[7];
}
if (9 < count($match)) {
$components['fragment'] = $match[9];
}
return $components;
}
/**
* Builds a URI based on n array with the main components
*
* @param array $components
* @return string
*/
public function generate(array $components)
{
$uri = $components['scheme'] . '://'
. $components['authority']
. $components['path'];
if (array_key_exists('query', $components)) {
$uri .= $components['query'];
}
if (array_key_exists('fragment', $components)) {
$uri .= $components['fragment'];
}
return $uri;
}
/**
* Resolves a URI
*
* @param string $uri Absolute or relative
* @param string $baseUri Optional base URI
* @return string
*/
public function resolve($uri, $baseUri = null)
{
$components = $this->parse($uri);
$path = $components['path'];
if ((array_key_exists('scheme', $components)) && ('http' === $components['scheme'])) {
return $uri;
}
$baseComponents = $this->parse($baseUri);
$basePath = $baseComponents['path'];
$baseComponents['path'] = UriResolver::combineRelativePathWithBasePath($path, $basePath);
return $this->generate($baseComponents);
}
/**
* @param string $uri
* @return boolean
*/
public function isValid($uri)
{
$components = $this->parse($uri);
return !empty($components);
}
}
|