From b60b1d25c04180a3c8ce71dc84cf1c841f5f5b5c Mon Sep 17 00:00:00 2001 From: basbl Date: Mon, 27 Apr 2020 11:47:22 +0200 Subject: [PATCH] Test scrutinizer --- src/PhpSpreadsheet/Writer/Ods.php | 32 ++++++++++++++++++----------- src/PhpSpreadsheet/Writer/Xlsx.php | 33 ++++++++++++++++++------------ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Ods.php b/src/PhpSpreadsheet/Writer/Ods.php index 2a1dba02..08bd2629 100644 --- a/src/PhpSpreadsheet/Writer/Ods.php +++ b/src/PhpSpreadsheet/Writer/Ods.php @@ -93,18 +93,18 @@ class Ods extends BaseWriter // garbage collect $this->spreadSheet->garbageCollect(); - // If $pFilename is php://output or php://stdout, make it a temporary file... $originalFilename = $pFilename; if (is_resource($pFilename)) { $this->fileHandle = $pFilename; - } elseif (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - // If $pFilename is php://output or php://stdout, make it a temporary file... - $pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp'); - if ($pFilename == '') { - $pFilename = $originalFilename; - } - $this->fileHandle = fopen($pFilename, 'wb+'); } else { + // 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'); + if ($pFilename === '') { + $pFilename = $originalFilename; + } + } + $this->fileHandle = fopen($pFilename, 'wb+'); } @@ -128,11 +128,19 @@ class Ods extends BaseWriter rewind($this->fileHandle); // If a temporary file was used, copy it to the correct file stream - if ($originalFilename != $pFilename) { - if (stream_copy_to_stream($this->fileHandle, fopen($originalFilename, 'wb+')) === false) { + if ($originalFilename !== $pFilename) { + $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."); } - @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() { // Try opening the ZIP file - if ($this->fileHandle === false) { + if (!is_resource($this->fileHandle)) { throw new WriterException('Could not open resource for writing.'); } diff --git a/src/PhpSpreadsheet/Writer/Xlsx.php b/src/PhpSpreadsheet/Writer/Xlsx.php index 2297002b..9c947d0a 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx.php +++ b/src/PhpSpreadsheet/Writer/Xlsx.php @@ -185,22 +185,21 @@ class Xlsx extends BaseWriter $this->spreadSheet->garbageCollect(); $originalFilename = $pFilename; - if (is_resource($pFilename)) { $this->fileHandle = $pFilename; - } elseif (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { - // If $pFilename is php://output or php://stdout, make it a temporary file... - $pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp'); - if ($pFilename == '') { - $pFilename = $originalFilename; - } - $this->fileHandle = fopen($pFilename, 'wb+'); } else { + // 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'); + if ($pFilename === '') { + $pFilename = $originalFilename; + } + } + $this->fileHandle = fopen($pFilename, 'wb+'); } - // Try opening the ZIP file - if ($this->fileHandle === false) { + if (!is_resource($this->fileHandle)) { throw new WriterException('Could not open resource for writing.'); } @@ -428,11 +427,19 @@ class Xlsx extends BaseWriter rewind($this->fileHandle); // If a temporary file was used, copy it to the correct file stream - if ($originalFilename != $pFilename) { - if (stream_copy_to_stream($this->fileHandle, fopen($originalFilename, 'wb+')) === false) { + if ($originalFilename !== $pFilename) { + $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."); } - @unlink($pFilename); + + if (is_string($pFilename) && !unlink($pFilename)) { + throw new WriterException('Could not unlink temporary zip file.'); + } } } else { throw new WriterException('PhpSpreadsheet object unassigned.');