bug #37763 Fix deprecated libxml_disable_entity_loader (jderusse)

This PR was merged into the 3.4 branch.

Discussion
----------

Fix deprecated libxml_disable_entity_loader

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | /
| License       | MIT
| Doc PR        | /

Fix deprecation `Function libxml_disable_entity_loader() is deprecated` triggered by https://github.com/php/php-src/pull/5867 in PHP8

Commits
-------

1f19da3936 Fix deprecated libxml_disable_entity_loader
This commit is contained in:
Fabien Potencier 2020-08-10 09:13:15 +02:00
commit b45e3ed0e7
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 // test for issue https://github.com/symfony/symfony/issues/9731
public function testLoadWrongEmptyXMLWithErrorHandler() public function testLoadWrongEmptyXMLWithErrorHandler()
{ {
$originalDisableEntities = libxml_disable_entity_loader(false); if (LIBXML_VERSION < 20900) {
$originalDisableEntities = libxml_disable_entity_loader(false);
}
$errorReporting = error_reporting(-1); $errorReporting = error_reporting(-1);
set_error_handler(function ($errno, $errstr) { set_error_handler(function ($errno, $errstr) {
@ -219,12 +221,13 @@ class XmlUtilsTest extends TestCase
error_reporting($errorReporting); error_reporting($errorReporting);
} }
$disableEntities = libxml_disable_entity_loader(true); if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities); $disableEntities = libxml_disable_entity_loader(true);
libxml_disable_entity_loader($disableEntities);
libxml_disable_entity_loader($originalDisableEntities); libxml_disable_entity_loader($originalDisableEntities);
$this->assertFalse($disableEntities);
$this->assertFalse($disableEntities); }
// should not throw an exception // should not throw an exception
XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd'); 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); $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(); libxml_clear_errors();
$dom = new \DOMDocument(); $dom = new \DOMDocument();
$dom->validateOnParse = true; $dom->validateOnParse = true;
if (!$dom->loadXML($content, LIBXML_NONET | (\defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) { 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))); throw new XmlParsingException(implode("\n", static::getXmlErrors($internalErrors)));
} }
@ -65,7 +69,9 @@ class XmlUtils
$dom->normalizeDocument(); $dom->normalizeDocument();
libxml_use_internal_errors($internalErrors); libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities); if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
foreach ($dom->childNodes as $child) { foreach ($dom->childNodes as $child) {
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) { if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {

View File

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

View File

@ -95,13 +95,17 @@ class XmlFileLoaderTest extends TestCase
public function testLoadWithExternalEntitiesDisabled() public function testLoadWithExternalEntitiesDisabled()
{ {
$disableEntities = libxml_disable_entity_loader(true); if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}
$containerBuilder = new ContainerBuilder(); $containerBuilder = new ContainerBuilder();
$loader = new XmlFileLoader($containerBuilder, new FileLocator(self::$fixturesPath.'/xml')); $loader = new XmlFileLoader($containerBuilder, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services2.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.'); $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') public function addHtmlContent($content, $charset = 'UTF-8')
{ {
$internalErrors = libxml_use_internal_errors(true); $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 = new \DOMDocument('1.0', $charset);
$dom->validateOnParse = true; $dom->validateOnParse = true;
@ -203,7 +205,9 @@ class Crawler implements \Countable, \IteratorAggregate
} }
libxml_use_internal_errors($internalErrors); libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities); if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
$this->addDocument($dom); $this->addDocument($dom);
@ -246,7 +250,9 @@ class Crawler implements \Countable, \IteratorAggregate
} }
$internalErrors = libxml_use_internal_errors(true); $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 = new \DOMDocument('1.0', $charset);
$dom->validateOnParse = true; $dom->validateOnParse = true;
@ -256,7 +262,9 @@ class Crawler implements \Countable, \IteratorAggregate
} }
libxml_use_internal_errors($internalErrors); libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities); if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}
$this->addDocument($dom); $this->addDocument($dom);

View File

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

View File

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

View File

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