X-Git-Url: https://yaffs.net/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fmedia%2Fsrc%2FOEmbed%2FResourceFetcher.php;h=e58e7e26adae0494d5925b09d20f83dee4c4d29d;hb=1c1cb0980bfa6caf0c24cce671b6bb541dc87583;hp=0c210878feaa823f24a1c90fc1cc9f266ffa05fc;hpb=9424afc6c1f518c301bf87a23c047d1873435d05;p=yaffs-website diff --git a/web/core/modules/media/src/OEmbed/ResourceFetcher.php b/web/core/modules/media/src/OEmbed/ResourceFetcher.php index 0c210878f..e58e7e26a 100644 --- a/web/core/modules/media/src/OEmbed/ResourceFetcher.php +++ b/web/core/modules/media/src/OEmbed/ResourceFetcher.php @@ -7,7 +7,6 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\UseCacheBackendTrait; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; -use Symfony\Component\Serializer\Encoder\XmlEncoder; /** * Fetches and caches oEmbed resources. @@ -69,8 +68,7 @@ class ResourceFetcher implements ResourceFetcherInterface { $content = (string) $response->getBody(); if (strstr($format, 'text/xml') || strstr($format, 'application/xml')) { - $encoder = new XmlEncoder(); - $data = $encoder->decode($content, 'xml'); + $data = $this->parseResourceXml($content, $url); } elseif (strstr($format, 'text/javascript') || strstr($format, 'application/json')) { $data = Json::decode($content); @@ -194,4 +192,42 @@ class ResourceFetcher implements ResourceFetcherInterface { } } + /** + * Parses XML resource data. + * + * @param string $data + * The raw XML for the resource. + * @param string $url + * The resource URL. + * + * @return array + * The parsed resource data. + * + * @throws \Drupal\media\OEmbed\ResourceException + * If the resource data could not be parsed. + */ + protected function parseResourceXml($data, $url) { + // Enable userspace error handling. + $was_using_internal_errors = libxml_use_internal_errors(TRUE); + libxml_clear_errors(); + + $content = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); + // Restore the previous error handling behavior. + libxml_use_internal_errors($was_using_internal_errors); + + $error = libxml_get_last_error(); + if ($error) { + libxml_clear_errors(); + throw new ResourceException($error->message, $url); + } + elseif ($content === FALSE) { + throw new ResourceException('The fetched resource could not be parsed.', $url); + } + + // Convert XML to JSON so that the parsed resource has a consistent array + // structure, regardless of any XML attributes or quirks of the XML parser. + $data = Json::encode($content); + return Json::decode($data); + } + }