Remove non-obvious BaseReader's dependency
BaseReader called `isValidFormat` despite it doesn't require it to be implemented. Instead we replaced `isValidFormat` by a proper implementation of `canRead` in each classes. This also remove the semantic amibguity between those two methods. FIX #32
This commit is contained in:
parent
8c58385d6c
commit
f74fde155f
|
@ -244,28 +244,6 @@ abstract class BaseReader implements IReader
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Can the current IReader read the file?
|
|
||||||
*
|
|
||||||
* @param string $pFilename
|
|
||||||
* @throws Exception
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function canRead($pFilename)
|
|
||||||
{
|
|
||||||
// Check if file exists
|
|
||||||
try {
|
|
||||||
$this->openFile($pFilename);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$readable = $this->isValidFormat();
|
|
||||||
fclose($this->fileHandle);
|
|
||||||
|
|
||||||
return $readable;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
|
* Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
|
||||||
*
|
*
|
||||||
|
|
|
@ -78,16 +78,6 @@ class CSV extends BaseReader implements IReader
|
||||||
$this->readFilter = new DefaultReadFilter();
|
$this->readFilter = new DefaultReadFilter();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate that the current file is a CSV file
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
protected function isValidFormat()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set input encoding
|
* Set input encoding
|
||||||
*
|
*
|
||||||
|
@ -171,11 +161,10 @@ class CSV extends BaseReader implements IReader
|
||||||
public function listWorksheetInfo($pFilename)
|
public function listWorksheetInfo($pFilename)
|
||||||
{
|
{
|
||||||
// Open file
|
// Open file
|
||||||
$this->openFile($pFilename);
|
if (!$this->canRead($pFilename)) {
|
||||||
if (!$this->isValidFormat()) {
|
|
||||||
fclose($this->fileHandle);
|
|
||||||
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
||||||
}
|
}
|
||||||
|
$this->openFile($pFilename);
|
||||||
$fileHandle = $this->fileHandle;
|
$fileHandle = $this->fileHandle;
|
||||||
|
|
||||||
// Skip BOM, if any
|
// Skip BOM, if any
|
||||||
|
@ -236,11 +225,10 @@ class CSV extends BaseReader implements IReader
|
||||||
ini_set('auto_detect_line_endings', true);
|
ini_set('auto_detect_line_endings', true);
|
||||||
|
|
||||||
// Open file
|
// Open file
|
||||||
$this->openFile($pFilename);
|
if (!$this->canRead($pFilename)) {
|
||||||
if (!$this->isValidFormat()) {
|
|
||||||
fclose($this->fileHandle);
|
|
||||||
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
||||||
}
|
}
|
||||||
|
$this->openFile($pFilename);
|
||||||
$fileHandle = $this->fileHandle;
|
$fileHandle = $this->fileHandle;
|
||||||
|
|
||||||
// Skip BOM, if any
|
// Skip BOM, if any
|
||||||
|
@ -394,4 +382,25 @@ class CSV extends BaseReader implements IReader
|
||||||
{
|
{
|
||||||
return $this->contiguous;
|
return $this->contiguous;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Can the current IReader read the file?
|
||||||
|
*
|
||||||
|
* @param string $pFilename
|
||||||
|
* @throws Exception
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function canRead($pFilename)
|
||||||
|
{
|
||||||
|
// Check if file exists
|
||||||
|
try {
|
||||||
|
$this->openFile($pFilename);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($this->fileHandle);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,25 +127,27 @@ class HTML extends BaseReader implements IReader
|
||||||
/**
|
/**
|
||||||
* Validate that the current file is an HTML file
|
* Validate that the current file is an HTML file
|
||||||
*
|
*
|
||||||
|
* @param string $pFilename
|
||||||
|
* @throws Exception
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function isValidFormat()
|
public function canRead($pFilename)
|
||||||
{
|
{
|
||||||
|
// Check if file exists
|
||||||
|
try {
|
||||||
|
$this->openFile($pFilename);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$beginning = $this->readBeginning();
|
$beginning = $this->readBeginning();
|
||||||
|
$startWithTag = self::startsWithTag($beginning);
|
||||||
|
$containsTags = self::containsTags($beginning);
|
||||||
|
$endsWithTag = self::endsWithTag($this->readEnding());
|
||||||
|
|
||||||
if (!self::startsWithTag($beginning)) {
|
fclose($this->fileHandle);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!self::containsTags($beginning)) {
|
return $startWithTag && $containsTags && $endsWithTag;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!self::endsWithTag($this->readEnding())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function readBeginning()
|
private function readBeginning()
|
||||||
|
@ -492,14 +494,10 @@ class HTML extends BaseReader implements IReader
|
||||||
*/
|
*/
|
||||||
public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
|
public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
|
||||||
{
|
{
|
||||||
// Open file to validate
|
// Validate
|
||||||
$this->openFile($pFilename);
|
if (!$this->canRead($pFilename)) {
|
||||||
if (!$this->isValidFormat()) {
|
|
||||||
fclose($this->fileHandle);
|
|
||||||
throw new Exception($pFilename . ' is an Invalid HTML file.');
|
throw new Exception($pFilename . ' is an Invalid HTML file.');
|
||||||
}
|
}
|
||||||
// Close after validating
|
|
||||||
fclose($this->fileHandle);
|
|
||||||
|
|
||||||
// Create new sheet
|
// Create new sheet
|
||||||
while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
|
while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
|
||||||
|
|
|
@ -65,26 +65,33 @@ class SYLK extends BaseReader implements IReader
|
||||||
/**
|
/**
|
||||||
* Validate that the current file is a SYLK file
|
* Validate that the current file is a SYLK file
|
||||||
*
|
*
|
||||||
|
* @param string $pFilename
|
||||||
|
* @throws Exception
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function isValidFormat()
|
public function canRead($pFilename)
|
||||||
{
|
{
|
||||||
|
// Check if file exists
|
||||||
|
try {
|
||||||
|
$this->openFile($pFilename);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Read sample data (first 2 KB will do)
|
// Read sample data (first 2 KB will do)
|
||||||
$data = fread($this->fileHandle, 2048);
|
$data = fread($this->fileHandle, 2048);
|
||||||
|
|
||||||
// Count delimiters in file
|
// Count delimiters in file
|
||||||
$delimiterCount = substr_count($data, ';');
|
$delimiterCount = substr_count($data, ';');
|
||||||
if ($delimiterCount < 1) {
|
$hasDelimiter = $delimiterCount > 0;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Analyze first line looking for ID; signature
|
// Analyze first line looking for ID; signature
|
||||||
$lines = explode("\n", $data);
|
$lines = explode("\n", $data);
|
||||||
if (substr($lines[0], 0, 4) != 'ID;P') {
|
$hasId = substr($lines[0], 0, 4) === 'ID;P';
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
fclose($this->fileHandle);
|
||||||
|
|
||||||
|
return $hasDelimiter && $hasId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -118,11 +125,10 @@ class SYLK extends BaseReader implements IReader
|
||||||
public function listWorksheetInfo($pFilename)
|
public function listWorksheetInfo($pFilename)
|
||||||
{
|
{
|
||||||
// Open file
|
// Open file
|
||||||
$this->openFile($pFilename);
|
if (!$this->canRead($pFilename)) {
|
||||||
if (!$this->isValidFormat()) {
|
|
||||||
fclose($this->fileHandle);
|
|
||||||
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
||||||
}
|
}
|
||||||
|
$this->openFile($pFilename);
|
||||||
$fileHandle = $this->fileHandle;
|
$fileHandle = $this->fileHandle;
|
||||||
rewind($fileHandle);
|
rewind($fileHandle);
|
||||||
|
|
||||||
|
@ -205,11 +211,10 @@ class SYLK extends BaseReader implements IReader
|
||||||
public function loadIntoExisting($pFilename, \PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet)
|
public function loadIntoExisting($pFilename, \PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet)
|
||||||
{
|
{
|
||||||
// Open file
|
// Open file
|
||||||
$this->openFile($pFilename);
|
if (!$this->canRead($pFilename)) {
|
||||||
if (!$this->isValidFormat()) {
|
|
||||||
fclose($this->fileHandle);
|
|
||||||
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
||||||
}
|
}
|
||||||
|
$this->openFile($pFilename);
|
||||||
$fileHandle = $this->fileHandle;
|
$fileHandle = $this->fileHandle;
|
||||||
rewind($fileHandle);
|
rewind($fileHandle);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue