From 06394df0bbd5fc7d398f591f4602e16c8239aaa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Helfensd=C3=B6rfer?= Date: Thu, 21 Jan 2021 16:26:39 +0100 Subject: [PATCH] Use ZipArchive instead of deprecated zip_* functions closes #50 --- src/AutoUpdate.php | 89 +++++++++++----------------------------------- 1 file changed, 21 insertions(+), 68 deletions(-) diff --git a/src/AutoUpdate.php b/src/AutoUpdate.php index ff718c3..5d358b6 100755 --- a/src/AutoUpdate.php +++ b/src/AutoUpdate.php @@ -4,6 +4,7 @@ use Exception; use RuntimeException; +use ZipArchive; use Composer\Semver\Comparator; use Desarrolla2\Cache\CacheInterface; @@ -671,21 +672,20 @@ protected function simulateInstall(string $updateFile): bool clearstatcache(); // Check if zip file could be opened - $zip = zip_open($updateFile); - if (!is_resource($zip)) { - $this->log->error(sprintf('Could not open zip file "%s", error: %d', $updateFile, $zip)); + $zip = new ZipArchive(); + $resource = $zip->open($updateFile); + if ($resource !== true) { + $this->log->error(sprintf('Could not open zip file "%s", error: %d', $updateFile, $resource)); return false; } - $i = - 1; $files = []; $simulateSuccess = true; - while ($file = zip_read($zip)) { - $i ++; - - $filename = zip_entry_name($file); + for ($i = 0; $i < $zip->numFiles; $i++) { + $fileStats = $zip->statIndex($i); + $filename = $fileStats['name']; $foldername = $this->installDir . dirname($filename); $absoluteFilename = $this->installDir . $filename; @@ -721,16 +721,6 @@ protected function simulateInstall(string $updateFile): bool continue; } - // Read file contents from archive - $contents = zip_entry_read($file, zip_entry_filesize($file)); - if ($contents === false) { - $files[$i]['extractable'] = false; - - $simulateSuccess = false; - $this->log->warning(sprintf('[SIMULATE] Coud not read contents of file "%s" from zip file!', - $filename)); - } - // Write to file if (file_exists($absoluteFilename)) { $files[$i]['file_exists'] = true; @@ -768,6 +758,8 @@ protected function simulateInstall(string $updateFile): bool } } + $zip->close(); + $this->simulationResults = $files; return $simulateSuccess; @@ -803,16 +795,18 @@ protected function install(string $updateFile, bool $simulateInstall, string $ve // Install only if simulateInstall === false // Check if zip file could be opened - $zip = zip_open($updateFile); - if (!is_resource($zip)) { - $this->log->error(sprintf('Could not open zip file "%s", error: %d', $updateFile, $zip)); + $zip = new ZipArchive(); + $resource = $zip->open($updateFile); + if ($resource !== true) { + $this->log->error(sprintf('Could not open zip file "%s", error: %d', $updateFile, $resource)); return false; } // Read every file from archive - while ($file = zip_read($zip)) { - $filename = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, zip_entry_name($file)); + for ($i = 0; $i < $zip->numFiles; $i++) { + $fileStats = $zip->statIndex($i); + $filename = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $fileStats['filename']); $foldername = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->installDir . dirname($filename)); $absoluteFilename = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->installDir . $filename); @@ -829,53 +823,12 @@ protected function install(string $updateFile, bool $simulateInstall, string $ve continue; } - // Read file contents from archive - $contents = zip_entry_read($file, zip_entry_filesize($file)); - - if ($contents === false) { - $this->log->error(sprintf('Coud not read zip entry "%s"', $file)); + // Extract file + if ($zip->extractTo($absoluteFilename, $fileStats['filename']) === false) { + $this->log->error(sprintf('Coud not read zip entry "%s"', $fileStats['filename'])); continue; } - // Write to file - if (file_exists($absoluteFilename)) { - if (!is_writable($absoluteFilename)) { - $this->log->error(sprintf('Could not overwrite "%s"!', $absoluteFilename)); - - zip_close($zip); - - return false; - } - } else { - // touch will fail if PHP is not the owner of the file, and file_put_contents is faster than touch. - if (file_put_contents($absoluteFilename, '') === false) { - $this->log->error(sprintf('The file "%s" could not be created!', $absoluteFilename)); - zip_close($zip); - - return false; - } - - $this->log->debug(sprintf('File "%s" created', $absoluteFilename)); - } - - $updateHandle = fopen($absoluteFilename, 'wb'); - - if (!$updateHandle) { - $this->log->error(sprintf('Could not open file "%s"!', $absoluteFilename)); - zip_close($zip); - - return false; - } - - if (false === fwrite($updateHandle, $contents)) { - $this->log->error(sprintf('Could not write to file "%s"!', $absoluteFilename)); - zip_close($zip); - - return false; - } - - fclose($updateHandle); - //If file is a update script, include if ($filename === $this->updateScriptName) { $this->log->debug(sprintf('Try to include update script "%s"', $absoluteFilename)); @@ -888,7 +841,7 @@ protected function install(string $updateFile, bool $simulateInstall, string $ve } } - zip_close($zip); + $zip->close(); $this->log->notice(sprintf('Update "%s" successfully installed', $version));