Unverified Commit faa28e8e authored by alexpott's avatar alexpott

Issue #2982372 by phenaproxima, catch, alexpott: oEmbed system can't parse XML attributes

(cherry picked from commit 6ca0cd27)
parent e4b3ec34
......@@ -7,7 +7,6 @@
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 @@ public function fetchResource($url) {
$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 @@ protected function createResource(array $data, $url) {
}
}
/**
* 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);
}
}
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
<type>video</type>
<version>1.0</version>
<version type="float">1.0</version>
<title>Let's Not Get a Drink Sometime</title>
<https/>
<author_name>CollegeHumor</author_name>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment