![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mautic.corals.io/vendor/knplabs/gaufrette/src/Gaufrette/Adapter/ |
<?php namespace Gaufrette\Adapter; use ZipArchive; use Gaufrette\Adapter; use Gaufrette\Util; /** * ZIP Archive adapter. * * @author Boris Guéry <[email protected]> * @author Antoine Hérault <[email protected]> */ class Zip implements Adapter { /** * @var string The zip archive full path */ protected $zipFile; /** * @var ZipArchive */ protected $zipArchive; public function __construct($zipFile) { if (!extension_loaded('zip')) { throw new \RuntimeException(sprintf( 'Unable to use %s as the ZIP extension is not available.', __CLASS__ )); } $this->zipFile = $zipFile; $this->reinitZipArchive(); } /** * {@inheritdoc} */ public function read($key) { if (false === ($content = $this->zipArchive->getFromName($key, 0))) { return false; } return $content; } /** * {@inheritdoc} */ public function write($key, $content) { if (!$this->zipArchive->addFromString($key, $content)) { return false; } if (!$this->save()) { return false; } return Util\Size::fromContent($content); } /** * {@inheritdoc} */ public function exists($key) { return (boolean) $this->getStat($key); } /** * {@inheritdoc} */ public function keys() { $keys = []; for ($i = 0; $i < $this->zipArchive->numFiles; ++$i) { $keys[$i] = $this->zipArchive->getNameIndex($i); } return $keys; } /** * @todo implement * * {@inheritdoc} */ public function isDirectory($key) { return false; } /** * {@inheritdoc} */ public function mtime($key) { $stat = $this->getStat($key); return $stat['mtime'] ?? false; } /** * {@inheritdoc} */ public function delete($key) { if (!$this->zipArchive->deleteName($key)) { return false; } return $this->save(); } /** * {@inheritdoc} */ public function rename($sourceKey, $targetKey) { if (!$this->zipArchive->renameName($sourceKey, $targetKey)) { return false; } return $this->save(); } /** * Returns the stat of a file in the zip archive * (name, index, crc, mtime, compression size, compression method, filesize). * * @param $key * * @return array|bool */ public function getStat($key) { $stat = $this->zipArchive->statName($key); if (false === $stat) { return []; } return $stat; } public function __destruct() { if ($this->zipArchive) { try { $this->zipArchive->close(); } catch (\Exception $e) { } unset($this->zipArchive); } } protected function reinitZipArchive() { $this->zipArchive = new ZipArchive(); if (true !== ($resultCode = $this->zipArchive->open($this->zipFile, ZipArchive::CREATE))) { switch ($resultCode) { case ZipArchive::ER_EXISTS: $errMsg = 'File already exists.'; break; case ZipArchive::ER_INCONS: $errMsg = 'Zip archive inconsistent.'; break; case ZipArchive::ER_INVAL: $errMsg = 'Invalid argument.'; break; case ZipArchive::ER_MEMORY: $errMsg = 'Malloc failure.'; break; case ZipArchive::ER_NOENT: $errMsg = 'Invalid argument.'; break; case ZipArchive::ER_NOZIP: $errMsg = 'Not a zip archive.'; break; case ZipArchive::ER_OPEN: $errMsg = 'Can\'t open file.'; break; case ZipArchive::ER_READ: $errMsg = 'Read error.'; break; case ZipArchive::ER_SEEK: $errMsg = 'Seek error.'; break; default: $errMsg = 'Unknown error.'; break; } throw new \RuntimeException(sprintf('%s', $errMsg)); } return $this; } /** * Saves archive modifications and updates current ZipArchive instance. * * @throws \RuntimeException If file could not be saved */ protected function save() { // Close to save modification if (!$this->zipArchive->close()) { return false; } // Re-initialize to get updated version $this->reinitZipArchive(); return true; } }