Test scrutinizer

This commit is contained in:
basbl 2020-04-27 11:47:22 +02:00
parent 11499aad9a
commit b60b1d25c0
No known key found for this signature in database
GPG Key ID: 95C7B7848BC5F1C5
2 changed files with 40 additions and 25 deletions

View File

@ -93,18 +93,18 @@ class Ods extends BaseWriter
// garbage collect // garbage collect
$this->spreadSheet->garbageCollect(); $this->spreadSheet->garbageCollect();
// If $pFilename is php://output or php://stdout, make it a temporary file...
$originalFilename = $pFilename; $originalFilename = $pFilename;
if (is_resource($pFilename)) { if (is_resource($pFilename)) {
$this->fileHandle = $pFilename; $this->fileHandle = $pFilename;
} elseif (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { } else {
// If $pFilename is php://output or php://stdout, make it a temporary file... // If $pFilename is php://output or php://stdout, make it a temporary file...
if (in_array(strtolower($pFilename), ['php://output', 'php://stdout'], true)) {
$pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp'); $pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
if ($pFilename == '') { if ($pFilename === '') {
$pFilename = $originalFilename; $pFilename = $originalFilename;
} }
$this->fileHandle = fopen($pFilename, 'wb+'); }
} else {
$this->fileHandle = fopen($pFilename, 'wb+'); $this->fileHandle = fopen($pFilename, 'wb+');
} }
@ -128,11 +128,19 @@ class Ods extends BaseWriter
rewind($this->fileHandle); rewind($this->fileHandle);
// If a temporary file was used, copy it to the correct file stream // If a temporary file was used, copy it to the correct file stream
if ($originalFilename != $pFilename) { if ($originalFilename !== $pFilename) {
if (stream_copy_to_stream($this->fileHandle, fopen($originalFilename, 'wb+')) === false) { $destinationFileHandle = fopen($originalFilename, 'wb+');
if (!is_resource($destinationFileHandle)) {
throw new WriterException("Could not open resource $originalFilename for writing.");
}
if (stream_copy_to_stream($this->fileHandle, $destinationFileHandle) === false) {
throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename."); throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename.");
} }
@unlink($pFilename);
if (is_string($pFilename) && !unlink($pFilename)) {
throw new WriterException('Could not unlink temporary zip file.');
}
} }
} }
@ -146,7 +154,7 @@ class Ods extends BaseWriter
private function createZip() private function createZip()
{ {
// Try opening the ZIP file // Try opening the ZIP file
if ($this->fileHandle === false) { if (!is_resource($this->fileHandle)) {
throw new WriterException('Could not open resource for writing.'); throw new WriterException('Could not open resource for writing.');
} }

View File

@ -185,22 +185,21 @@ class Xlsx extends BaseWriter
$this->spreadSheet->garbageCollect(); $this->spreadSheet->garbageCollect();
$originalFilename = $pFilename; $originalFilename = $pFilename;
if (is_resource($pFilename)) { if (is_resource($pFilename)) {
$this->fileHandle = $pFilename; $this->fileHandle = $pFilename;
} elseif (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { } else {
// If $pFilename is php://output or php://stdout, make it a temporary file... // If $pFilename is php://output or php://stdout, make it a temporary file...
if (in_array(strtolower($pFilename), ['php://output', 'php://stdout'], true)) {
$pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp'); $pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
if ($pFilename == '') { if ($pFilename === '') {
$pFilename = $originalFilename; $pFilename = $originalFilename;
} }
$this->fileHandle = fopen($pFilename, 'wb+'); }
} else {
$this->fileHandle = fopen($pFilename, 'wb+'); $this->fileHandle = fopen($pFilename, 'wb+');
} }
// Try opening the ZIP file if (!is_resource($this->fileHandle)) {
if ($this->fileHandle === false) {
throw new WriterException('Could not open resource for writing.'); throw new WriterException('Could not open resource for writing.');
} }
@ -428,11 +427,19 @@ class Xlsx extends BaseWriter
rewind($this->fileHandle); rewind($this->fileHandle);
// If a temporary file was used, copy it to the correct file stream // If a temporary file was used, copy it to the correct file stream
if ($originalFilename != $pFilename) { if ($originalFilename !== $pFilename) {
if (stream_copy_to_stream($this->fileHandle, fopen($originalFilename, 'wb+')) === false) { $destinationFileHandle = fopen($originalFilename, 'wb+');
if (!is_resource($destinationFileHandle)) {
throw new WriterException("Could not open resource $originalFilename for writing.");
}
if (stream_copy_to_stream($this->fileHandle, $destinationFileHandle) === false) {
throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename."); throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename.");
} }
@unlink($pFilename);
if (is_string($pFilename) && !unlink($pFilename)) {
throw new WriterException('Could not unlink temporary zip file.');
}
} }
} else { } else {
throw new WriterException('PhpSpreadsheet object unassigned.'); throw new WriterException('PhpSpreadsheet object unassigned.');