secure-custom-fields
/
vendor
/
justinrainbow
/
json-schema
/
src
/
JsonSchema
/
Uri
/
Retrievers
/
FileGetContents.php
secure-custom-fields
/
vendor
/
justinrainbow
/
json-schema
/
src
/
JsonSchema
/
Uri
/
Retrievers
Last commit date
AbstractRetriever.php
9 months ago
Curl.php
9 months ago
FileGetContents.php
9 months ago
PredefinedArray.php
9 months ago
UriRetrieverInterface.php
9 months ago
FileGetContents.php
94 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\Retrievers; |
| 11 | |
| 12 | use JsonSchema\Exception\ResourceNotFoundException; |
| 13 | |
| 14 | /** |
| 15 | * Tries to retrieve JSON schemas from a URI using file_get_contents() |
| 16 | * |
| 17 | * @author Sander Coolen <sander@jibber.nl> |
| 18 | */ |
| 19 | class FileGetContents extends AbstractRetriever |
| 20 | { |
| 21 | protected $messageBody; |
| 22 | |
| 23 | /** |
| 24 | * {@inheritdoc} |
| 25 | * |
| 26 | * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve() |
| 27 | */ |
| 28 | public function retrieve($uri) |
| 29 | { |
| 30 | $errorMessage = null; |
| 31 | set_error_handler(function ($errno, $errstr) use (&$errorMessage) { |
| 32 | $errorMessage = $errstr; |
| 33 | }); |
| 34 | $response = file_get_contents($uri); |
| 35 | restore_error_handler(); |
| 36 | |
| 37 | if ($errorMessage) { |
| 38 | throw new ResourceNotFoundException($errorMessage); |
| 39 | } |
| 40 | |
| 41 | if (false === $response) { |
| 42 | throw new ResourceNotFoundException('JSON schema not found at ' . $uri); |
| 43 | } |
| 44 | |
| 45 | if ($response == '' |
| 46 | && substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/' |
| 47 | ) { |
| 48 | throw new ResourceNotFoundException('JSON schema not found at ' . $uri); |
| 49 | } |
| 50 | |
| 51 | $this->messageBody = $response; |
| 52 | if (!empty($http_response_header)) { |
| 53 | // $http_response_header cannot be tested, because it's defined in the method's local scope |
| 54 | // See http://php.net/manual/en/reserved.variables.httpresponseheader.php for more info. |
| 55 | $this->fetchContentType($http_response_header); // @codeCoverageIgnore |
| 56 | } else { // @codeCoverageIgnore |
| 57 | // Could be a "file://" url or something else - fake up the response |
| 58 | $this->contentType = null; |
| 59 | } |
| 60 | |
| 61 | return $this->messageBody; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param array $headers HTTP Response Headers |
| 66 | * |
| 67 | * @return bool Whether the Content-Type header was found or not |
| 68 | */ |
| 69 | private function fetchContentType(array $headers) |
| 70 | { |
| 71 | foreach ($headers as $header) { |
| 72 | if ($this->contentType = self::getContentTypeMatchInHeader($header)) { |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param string $header |
| 82 | * |
| 83 | * @return string|null |
| 84 | */ |
| 85 | protected static function getContentTypeMatchInHeader($header) |
| 86 | { |
| 87 | if (0 < preg_match("/Content-Type:(\V*)/ims", $header, $match)) { |
| 88 | return trim($match[1]); |
| 89 | } |
| 90 | |
| 91 | return null; |
| 92 | } |
| 93 | } |
| 94 |