Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed php8 deprecation warning for libxml_disable_entity_loader() #1625

Merged
merged 5 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
### Fixed

- PrintArea causes exception [#1544](https://github.com/phpoffice/phpspreadsheet/pull/1544)
- Fixed PHP8 deprecation warning for libxml_disable_entity_loader() [#1625](https://github.com/phpoffice/phpspreadsheet/pull/1625)

## 1.14.1 - 2020-07-19

Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Reader/Security/XmlScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function threadSafeLibxmlDisableEntityLoaderAvailability()

private function disableEntityLoaderCheck(): void
{
if (Settings::getLibXmlDisableEntityLoader()) {
if (Settings::getLibXmlDisableEntityLoader() && \PHP_VERSION_ID < 80000) {
$libxmlDisableEntityLoaderValue = libxml_disable_entity_loader(true);

if (self::$libxmlDisableEntityLoaderValue === null) {
Expand All @@ -74,7 +74,7 @@ private function disableEntityLoaderCheck(): void

public static function shutdown(): void
{
if (self::$libxmlDisableEntityLoaderValue !== null) {
if (self::$libxmlDisableEntityLoaderValue !== null && \PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader(self::$libxmlDisableEntityLoaderValue);
self::$libxmlDisableEntityLoaderValue = null;
}
Expand Down
25 changes: 20 additions & 5 deletions tests/PhpSpreadsheetTests/Reader/Security/XmlScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class XmlScannerTest extends TestCase
{
protected function setUp(): void
{
libxml_disable_entity_loader(false);
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader(false);
}
}

/**
Expand All @@ -24,13 +27,19 @@ protected function setUp(): void
*/
public function testValidXML($filename, $expectedResult, $libxmlDisableEntityLoader): void
{
$oldDisableEntityLoaderState = libxml_disable_entity_loader($libxmlDisableEntityLoader);
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
$oldDisableEntityLoaderState = libxml_disable_entity_loader($libxmlDisableEntityLoader);
}

$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
$result = $reader->scanFile($filename);
self::assertEquals($expectedResult, $result);

libxml_disable_entity_loader($oldDisableEntityLoaderState);
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($oldDisableEntityLoaderState);
}
}

public function providerValidXML()
Expand All @@ -56,13 +65,19 @@ public function testInvalidXML($filename, $libxmlDisableEntityLoader): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);

libxml_disable_entity_loader($libxmlDisableEntityLoader);
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($libxmlDisableEntityLoader);
}

$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
$expectedResult = 'FAILURE: Should throw an Exception rather than return a value';
$result = $reader->scanFile($filename);
self::assertEquals($expectedResult, $result);
self::assertEquals($libxmlDisableEntityLoader, libxml_disable_entity_loader());
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
self::assertEquals($libxmlDisableEntityLoader, libxml_disable_entity_loader());
}
}

public function providerInvalidXML()
Expand Down
26 changes: 19 additions & 7 deletions tests/PhpSpreadsheetTests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,39 @@ class SettingsTest extends TestCase

protected function setUp(): void
{
$this->prevValue = libxml_disable_entity_loader();
libxml_disable_entity_loader(false); // Enable entity loader
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
$this->prevValue = libxml_disable_entity_loader();
libxml_disable_entity_loader(false); // Enable entity loader
}
}

protected function tearDown(): void
{
libxml_disable_entity_loader($this->prevValue);
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($this->prevValue);
}
}

public function testGetXMLSettings(): void
{
$result = Settings::getLibXmlLoaderOptions();
self::assertTrue((bool) ((LIBXML_DTDLOAD | LIBXML_DTDATTR) & $result));
self::assertFalse(libxml_disable_entity_loader());
self::assertTrue((bool)((LIBXML_DTDLOAD | LIBXML_DTDATTR) & $result));
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
self::assertFalse(libxml_disable_entity_loader());
}
}

public function testSetXMLSettings(): void
{
Settings::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_DTDVALID);
$result = Settings::getLibXmlLoaderOptions();
self::assertTrue((bool) ((LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_DTDVALID) & $result));
self::assertFalse(libxml_disable_entity_loader());
self::assertTrue((bool)((LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_DTDVALID) & $result));
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
self::assertFalse(libxml_disable_entity_loader());
}
}
}