Fix deprecated libxml_disable_entity_loader

This commit is contained in:
Jérémy Derussé 2020-08-07 11:40:57 +02:00
parent 995d784a0d
commit 1f19da3936
No known key found for this signature in database
GPG Key ID: 2083FA5758C473D2
8 changed files with 63 additions and 28 deletions

View File

@ -199,7 +199,9 @@ class XmlUtilsTest extends TestCase
// test for issue https://github.com/symfony/symfony/issues/9731
public function testLoadWrongEmptyXMLWithErrorHandler()
{
$originalDisableEntities = libxml_disable_entity_loader(false);
if (LIBXML_VERSION < 20900) {
$originalDisableEntities = libxml_disable_entity_loader(false);
}
$errorReporting = error_reporting(-1);
set_error_handler(function ($errno, $errstr) {
@ -219,12 +221,13 @@ class XmlUtilsTest extends TestCase
error_reporting($errorReporting);
}
$disableEntities = libxml_disable_entity_loader(true);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
libxml_disable_entity_loader($disableEntities);
libxml_disable_entity_loader($originalDisableEntities);
$this->assertFalse($disableEntities);
libxml_disable_entity_loader($originalDisableEntities);
$this->assertFalse($disableEntities);
}
// should not throw an exception
XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd');

View File

@ -51,13 +51,17 @@ class XmlUtils
}
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}
libxml_clear_errors();
$dom = new \DOMDocument();
$dom->validateOnParse = true;
if (!$dom->loadXML($content, LIBXML_NONET | (\defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
throw new XmlParsingException(implode("\n", static::getXmlErrors($internalErrors)));
}
@ -65,7 +69,9 @@ class XmlUtils
$dom->normalizeDocument();
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
foreach ($dom->childNodes as $child) {
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {

View File

@ -620,9 +620,13 @@ $imports
EOF
;
$disableEntities = libxml_disable_entity_loader(false);
$valid = @$dom->schemaValidateSource($source);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(false);
$valid = @$dom->schemaValidateSource($source);
libxml_disable_entity_loader($disableEntities);
} else {
$valid = @$dom->schemaValidateSource($source);
}
foreach ($tmpfiles as $tmpfile) {
@unlink($tmpfile);

View File

@ -95,13 +95,17 @@ class XmlFileLoaderTest extends TestCase
public function testLoadWithExternalEntitiesDisabled()
{
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}
$containerBuilder = new ContainerBuilder();
$loader = new XmlFileLoader($containerBuilder, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services2.xml');
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
$this->assertGreaterThan(0, $containerBuilder->getParameterBag()->all(), 'Parameters can be read from the config file.');
}

View File

@ -182,7 +182,9 @@ class Crawler implements \Countable, \IteratorAggregate
public function addHtmlContent($content, $charset = 'UTF-8')
{
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}
$dom = new \DOMDocument('1.0', $charset);
$dom->validateOnParse = true;
@ -203,7 +205,9 @@ class Crawler implements \Countable, \IteratorAggregate
}
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
$this->addDocument($dom);
@ -246,7 +250,9 @@ class Crawler implements \Countable, \IteratorAggregate
}
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}
$dom = new \DOMDocument('1.0', $charset);
$dom->validateOnParse = true;
@ -256,7 +262,9 @@ class Crawler implements \Countable, \IteratorAggregate
}
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
$this->addDocument($dom);

View File

@ -83,14 +83,18 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
}
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}
libxml_clear_errors();
$dom = new \DOMDocument();
$dom->loadXML($data, $this->loadOptions);
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
if ($error = libxml_get_last_error()) {
libxml_clear_errors();

View File

@ -189,15 +189,17 @@ class XliffFileLoader implements LoaderInterface
{
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(false);
if (!@$dom->schemaValidateSource($schema)) {
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(false);
$isValid = @$dom->schemaValidateSource($schema);
libxml_disable_entity_loader($disableEntities);
throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: ', $file).implode("\n", $this->getXmlErrors($internalErrors)));
} else {
$isValid = @$dom->schemaValidateSource($schema);
}
libxml_disable_entity_loader($disableEntities);
if (!$isValid) {
throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: ', $file).implode("\n", $this->getXmlErrors($internalErrors)));
}
$dom->normalizeDocument();

View File

@ -49,13 +49,17 @@ class XliffFileLoaderTest extends TestCase
public function testLoadWithExternalEntitiesDisabled()
{
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals([new FileResource($resource)], $catalogue->getResources());