Refactor scanner into base reader class

This commit is contained in:
MarkBaker 2018-11-25 12:14:54 +01:00
parent aba41f6495
commit c708411529
8 changed files with 45 additions and 26 deletions

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheet\Reader; namespace PhpOffice\PhpSpreadsheet\Reader;
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
use PhpOffice\PhpSpreadsheet\Shared\File; use PhpOffice\PhpSpreadsheet\Shared\File;
abstract class BaseReader implements IReader abstract class BaseReader implements IReader
@ -49,6 +50,11 @@ abstract class BaseReader implements IReader
protected $fileHandle; protected $fileHandle;
/**
* @var XmlScanner
*/
protected $securityScanner;
/** /**
* Read data only? * Read data only?
* If this is true, then the Reader will only read data values for cells, it will not read any formatting information. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
@ -204,6 +210,14 @@ abstract class BaseReader implements IReader
return $this; return $this;
} }
public function getSecuritySCanner()
{
if (property_exists($this, 'securityScanner')) {
return $this->securityScanner;
}
return null;
}
/** /**
* Open file for reading. * Open file for reading.
* *

View File

@ -31,11 +31,6 @@ class Gnumeric extends BaseReader
private $referenceHelper; private $referenceHelper;
/**
* @var XmlScanner
*/
private $securityScanner;
/** /**
* Create a new Gnumeric. * Create a new Gnumeric.
*/ */

View File

@ -7,7 +7,6 @@ use DOMElement;
use DOMNode; use DOMNode;
use DOMText; use DOMText;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color; use PhpOffice\PhpSpreadsheet\Style\Color;
@ -17,11 +16,6 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
/** PhpSpreadsheet root directory */ /** PhpSpreadsheet root directory */
class Html extends BaseReader class Html extends BaseReader
{ {
/**
* @var XmlScanner
*/
private $securityScanner;
/** /**
* Sample size to read to determine if it's HTML or not. * Sample size to read to determine if it's HTML or not.
*/ */

View File

@ -20,11 +20,6 @@ use ZipArchive;
class Ods extends BaseReader class Ods extends BaseReader
{ {
/**
* @var XmlScanner
*/
private $securityScanner;
/** /**
* Create a new Ods Reader instance. * Create a new Ods Reader instance.
*/ */

View File

@ -13,8 +13,18 @@ class XmlScanner
*/ */
private $libxmlDisableEntityLoader = false; private $libxmlDisableEntityLoader = false;
/**
* Store the initial setting of libxmlDisableEntityLoader so that we can resore t later.
*
* @var bool
*/
private $previousLibxmlDisableEntityLoaderValue; private $previousLibxmlDisableEntityLoaderValue;
/**
* String used to identify risky xml elements.
*
* @var string
*/
private $pattern; private $pattern;
private function __construct($pattern = '<!DOCTYPE') private function __construct($pattern = '<!DOCTYPE')

View File

@ -47,11 +47,6 @@ class Xlsx extends BaseReader
*/ */
private static $theme = null; private static $theme = null;
/**
* @var XmlScanner
*/
private $securityScanner;
/** /**
* Create a new Xlsx Reader instance. * Create a new Xlsx Reader instance.
*/ */

View File

@ -36,11 +36,6 @@ class Xml extends BaseReader
*/ */
protected $charSet = 'UTF-8'; protected $charSet = 'UTF-8';
/**
* @var XmlScanner
*/
private $securityScanner;
/** /**
* Create a new Excel2003XML Reader instance. * Create a new Excel2003XML Reader instance.
*/ */

View File

@ -3,6 +3,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Reader\Security; namespace PhpOffice\PhpSpreadsheetTests\Reader\Security;
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class XmlScannerTest extends TestCase class XmlScannerTest extends TestCase
@ -54,4 +56,23 @@ class XmlScannerTest extends TestCase
return $tests; return $tests;
} }
public function testGetSecurityScannerForXmlBasedReader()
{
$fileReader = new Xlsx();
$scanner = $fileReader->getSecuritySCanner();
// Must return an object...
$this->assertTrue(is_object($scanner));
// ... of the correct type
$this->assertInstanceOf(XmlScanner::class, $scanner);
}
public function testGetSecurityScannerForNonXmlBasedReader()
{
$fileReader = new Xls();
$scanner = $fileReader->getSecuritySCanner();
// Must return a null...
$this->assertNull($scanner);
}
} }