Skip to content

Commit b45e3ed

Browse files
committed
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 php/php-src#5867 in PHP8 Commits ------- 1f19da3 Fix deprecated libxml_disable_entity_loader
2 parents 4ace472 + 1f19da3 commit b45e3ed

File tree

8 files changed

+62
-27
lines changed

8 files changed

+62
-27
lines changed

src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ public function testLoadEmptyXmlFile()
199199
// test for issue https://github.com/symfony/symfony/issues/9731
200200
public function testLoadWrongEmptyXMLWithErrorHandler()
201201
{
202-
$originalDisableEntities = libxml_disable_entity_loader(false);
202+
if (LIBXML_VERSION < 20900) {
203+
$originalDisableEntities = libxml_disable_entity_loader(false);
204+
}
203205
$errorReporting = error_reporting(-1);
204206

205207
set_error_handler(function ($errno, $errstr) {
@@ -219,12 +221,13 @@ public function testLoadWrongEmptyXMLWithErrorHandler()
219221
error_reporting($errorReporting);
220222
}
221223

222-
$disableEntities = libxml_disable_entity_loader(true);
223-
libxml_disable_entity_loader($disableEntities);
224-
225-
libxml_disable_entity_loader($originalDisableEntities);
224+
if (LIBXML_VERSION < 20900) {
225+
$disableEntities = libxml_disable_entity_loader(true);
226+
libxml_disable_entity_loader($disableEntities);
226227

227-
$this->assertFalse($disableEntities);
228+
libxml_disable_entity_loader($originalDisableEntities);
229+
$this->assertFalse($disableEntities);
230+
}
228231

229232
// should not throw an exception
230233
XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd');

src/Symfony/Component/Config/Util/XmlUtils.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,27 @@ public static function parse($content, $schemaOrCallable = null)
5151
}
5252

5353
$internalErrors = libxml_use_internal_errors(true);
54-
$disableEntities = libxml_disable_entity_loader(true);
54+
if (LIBXML_VERSION < 20900) {
55+
$disableEntities = libxml_disable_entity_loader(true);
56+
}
5557
libxml_clear_errors();
5658

5759
$dom = new \DOMDocument();
5860
$dom->validateOnParse = true;
5961
if (!$dom->loadXML($content, LIBXML_NONET | (\defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
60-
libxml_disable_entity_loader($disableEntities);
62+
if (LIBXML_VERSION < 20900) {
63+
libxml_disable_entity_loader($disableEntities);
64+
}
6165

6266
throw new XmlParsingException(implode("\n", static::getXmlErrors($internalErrors)));
6367
}
6468

6569
$dom->normalizeDocument();
6670

6771
libxml_use_internal_errors($internalErrors);
68-
libxml_disable_entity_loader($disableEntities);
72+
if (LIBXML_VERSION < 20900) {
73+
libxml_disable_entity_loader($disableEntities);
74+
}
6975

7076
foreach ($dom->childNodes as $child) {
7177
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,13 @@ public function validateSchema(\DOMDocument $dom)
620620
EOF
621621
;
622622

623-
$disableEntities = libxml_disable_entity_loader(false);
624-
$valid = @$dom->schemaValidateSource($source);
625-
libxml_disable_entity_loader($disableEntities);
623+
if (LIBXML_VERSION < 20900) {
624+
$disableEntities = libxml_disable_entity_loader(false);
625+
$valid = @$dom->schemaValidateSource($source);
626+
libxml_disable_entity_loader($disableEntities);
627+
} else {
628+
$valid = @$dom->schemaValidateSource($source);
629+
}
626630

627631
foreach ($tmpfiles as $tmpfile) {
628632
@unlink($tmpfile);

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,17 @@ public function testParseFile()
9595

9696
public function testLoadWithExternalEntitiesDisabled()
9797
{
98-
$disableEntities = libxml_disable_entity_loader(true);
98+
if (LIBXML_VERSION < 20900) {
99+
$disableEntities = libxml_disable_entity_loader(true);
100+
}
99101

100102
$containerBuilder = new ContainerBuilder();
101103
$loader = new XmlFileLoader($containerBuilder, new FileLocator(self::$fixturesPath.'/xml'));
102104
$loader->load('services2.xml');
103105

104-
libxml_disable_entity_loader($disableEntities);
106+
if (LIBXML_VERSION < 20900) {
107+
libxml_disable_entity_loader($disableEntities);
108+
}
105109

106110
$this->assertGreaterThan(0, $containerBuilder->getParameterBag()->all(), 'Parameters can be read from the config file.');
107111
}

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ public function addContent($content, $type = null)
182182
public function addHtmlContent($content, $charset = 'UTF-8')
183183
{
184184
$internalErrors = libxml_use_internal_errors(true);
185-
$disableEntities = libxml_disable_entity_loader(true);
185+
if (LIBXML_VERSION < 20900) {
186+
$disableEntities = libxml_disable_entity_loader(true);
187+
}
186188

187189
$dom = new \DOMDocument('1.0', $charset);
188190
$dom->validateOnParse = true;
@@ -203,7 +205,9 @@ public function addHtmlContent($content, $charset = 'UTF-8')
203205
}
204206

205207
libxml_use_internal_errors($internalErrors);
206-
libxml_disable_entity_loader($disableEntities);
208+
if (LIBXML_VERSION < 20900) {
209+
libxml_disable_entity_loader($disableEntities);
210+
}
207211

208212
$this->addDocument($dom);
209213

@@ -246,7 +250,9 @@ public function addXmlContent($content, $charset = 'UTF-8', $options = LIBXML_NO
246250
}
247251

248252
$internalErrors = libxml_use_internal_errors(true);
249-
$disableEntities = libxml_disable_entity_loader(true);
253+
if (LIBXML_VERSION < 20900) {
254+
$disableEntities = libxml_disable_entity_loader(true);
255+
}
250256

251257
$dom = new \DOMDocument('1.0', $charset);
252258
$dom->validateOnParse = true;
@@ -256,7 +262,9 @@ public function addXmlContent($content, $charset = 'UTF-8', $options = LIBXML_NO
256262
}
257263

258264
libxml_use_internal_errors($internalErrors);
259-
libxml_disable_entity_loader($disableEntities);
265+
if (LIBXML_VERSION < 20900) {
266+
libxml_disable_entity_loader($disableEntities);
267+
}
260268

261269
$this->addDocument($dom);
262270

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,18 @@ public function decode($data, $format, array $context = [])
8383
}
8484

8585
$internalErrors = libxml_use_internal_errors(true);
86-
$disableEntities = libxml_disable_entity_loader(true);
86+
if (LIBXML_VERSION < 20900) {
87+
$disableEntities = libxml_disable_entity_loader(true);
88+
}
8789
libxml_clear_errors();
8890

8991
$dom = new \DOMDocument();
9092
$dom->loadXML($data, $this->loadOptions);
9193

9294
libxml_use_internal_errors($internalErrors);
93-
libxml_disable_entity_loader($disableEntities);
95+
if (LIBXML_VERSION < 20900) {
96+
libxml_disable_entity_loader($disableEntities);
97+
}
9498

9599
if ($error = libxml_get_last_error()) {
96100
libxml_clear_errors();

src/Symfony/Component/Translation/Loader/XliffFileLoader.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,18 @@ private function validateSchema($file, \DOMDocument $dom, $schema)
189189
{
190190
$internalErrors = libxml_use_internal_errors(true);
191191

192-
$disableEntities = libxml_disable_entity_loader(false);
193-
194-
if (!@$dom->schemaValidateSource($schema)) {
192+
if (LIBXML_VERSION < 20900) {
193+
$disableEntities = libxml_disable_entity_loader(false);
194+
$isValid = @$dom->schemaValidateSource($schema);
195195
libxml_disable_entity_loader($disableEntities);
196+
} else {
197+
$isValid = @$dom->schemaValidateSource($schema);
198+
}
196199

200+
if (!$isValid) {
197201
throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: ', $file).implode("\n", $this->getXmlErrors($internalErrors)));
198202
}
199203

200-
libxml_disable_entity_loader($disableEntities);
201-
202204
$dom->normalizeDocument();
203205

204206
libxml_clear_errors();

src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ public function testLoadWithInternalErrorsEnabled()
4949

5050
public function testLoadWithExternalEntitiesDisabled()
5151
{
52-
$disableEntities = libxml_disable_entity_loader(true);
52+
if (LIBXML_VERSION < 20900) {
53+
$disableEntities = libxml_disable_entity_loader(true);
54+
}
5355

5456
$loader = new XliffFileLoader();
5557
$resource = __DIR__.'/../fixtures/resources.xlf';
5658
$catalogue = $loader->load($resource, 'en', 'domain1');
5759

58-
libxml_disable_entity_loader($disableEntities);
60+
if (LIBXML_VERSION < 20900) {
61+
libxml_disable_entity_loader($disableEntities);
62+
}
5963

6064
$this->assertEquals('en', $catalogue->getLocale());
6165
$this->assertEquals([new FileResource($resource)], $catalogue->getResources());

0 commit comments

Comments
 (0)