| Current Path : /proc/1908984/root/proc/self/root/proc/self/root/proc/2404962/root/tmp/ |
| Current File : //proc/1908984/root/proc/self/root/proc/self/root/proc/2404962/root/tmp/phpne8l7v |
home/digilove/public_html/110/libraries/vendor/joomla/archive/src/Zip.php 0000644 00000041504 15243310217 0022320 0 ustar 00 <?php
/**
* Part of the Joomla Framework Archive Package
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Archive;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;
use Joomla\Filesystem\Path;
/**
* ZIP format adapter for the Archive package
*
* The ZIP compression code is partially based on code from:
* Eric Mueller <eric@themepark.com>
* http://www.zend.com/codex.php?id=535&single=1
*
* Deins125 <webmaster@atlant.ru>
* http://www.zend.com/codex.php?id=470&single=1
*
* The ZIP compression date code is partially based on code from
* Peter Listiak <mlady@users.sourceforge.net>
*
* This class is inspired from and draws heavily in code and concept from the Compress package of
* The Horde Project <http://www.horde.org>
*
* @contributor Chuck Hagenbuch <chuck@horde.org>
* @contributor Michael Slusarz <slusarz@horde.org>
* @contributor Michael Cochrane <mike@graftonhall.co.nz>
*
* @since 1.0
*/
class Zip implements ExtractableInterface
{
/**
* ZIP compression methods.
*
* @var array
* @since 1.0
*/
private $methods = array(
0x0 => 'None',
0x1 => 'Shrunk',
0x2 => 'Super Fast',
0x3 => 'Fast',
0x4 => 'Normal',
0x5 => 'Maximum',
0x6 => 'Imploded',
0x8 => 'Deflated',
);
/**
* Beginning of central directory record.
*
* @var string
* @since 1.0
*/
private $ctrlDirHeader = "\x50\x4b\x01\x02";
/**
* End of central directory record.
*
* @var string
* @since 1.0
*/
private $ctrlDirEnd = "\x50\x4b\x05\x06\x00\x00\x00\x00";
/**
* Beginning of file contents.
*
* @var string
* @since 1.0
*/
private $fileHeader = "\x50\x4b\x03\x04";
/**
* ZIP file data buffer
*
* @var string
* @since 1.0
*/
private $data;
/**
* ZIP file metadata array
*
* @var array
* @since 1.0
*/
private $metadata;
/**
* Holds the options array.
*
* @var array|\ArrayAccess
* @since 1.0
*/
protected $options = array();
/**
* Create a new Archive object.
*
* @param array|\ArrayAccess $options An array of options or an object that implements \ArrayAccess
*
* @since 1.0
* @throws \InvalidArgumentException
*/
public function __construct($options = array())
{
if (!\is_array($options) && !($options instanceof \ArrayAccess))
{
throw new \InvalidArgumentException(
'The options param must be an array or implement the ArrayAccess interface.'
);
}
$this->options = $options;
}
/**
* Create a ZIP compressed file from an array of file data.
*
* @param string $archive Path to save archive.
* @param array $files Array of files to add to archive.
*
* @return boolean True if successful.
*
* @since 1.0
* @todo Finish Implementation
*/
public function create($archive, $files)
{
$contents = array();
$ctrldir = array();
foreach ($files as $file)
{
$this->addToZipFile($file, $contents, $ctrldir);
}
return $this->createZipFile($contents, $ctrldir, $archive);
}
/**
* Extract a ZIP compressed file to a given path
*
* @param string $archive Path to ZIP archive to extract
* @param string $destination Path to extract archive into
*
* @return boolean True if successful
*
* @since 1.0
* @throws \RuntimeException
*/
public function extract($archive, $destination)
{
if (!is_file($archive))
{
throw new \RuntimeException('Archive does not exist at ' . $archive);
}
if (static::hasNativeSupport())
{
return $this->extractNative($archive, $destination);
}
return $this->extractCustom($archive, $destination);
}
/**
* Tests whether this adapter can unpack files on this computer.
*
* @return boolean True if supported
*
* @since 1.0
*/
public static function isSupported()
{
return self::hasNativeSupport() || \extension_loaded('zlib');
}
/**
* Method to determine if the server has native zip support for faster handling
*
* @return boolean True if php has native ZIP support
*
* @since 1.0
*/
public static function hasNativeSupport()
{
return \extension_loaded('zip');
}
/**
* Checks to see if the data is a valid ZIP file.
*
* @param string $data ZIP archive data buffer.
*
* @return boolean True if valid, false if invalid.
*
* @since 1.0
*/
public function checkZipData($data)
{
return strpos($data, $this->fileHeader) !== false;
}
/**
* Extract a ZIP compressed file to a given path using a php based algorithm that only requires zlib support
*
* @param string $archive Path to ZIP archive to extract.
* @param string $destination Path to extract archive into.
*
* @return boolean True if successful
*
* @since 1.0
* @throws \RuntimeException
*/
protected function extractCustom($archive, $destination)
{
$this->metadata = null;
$this->data = file_get_contents($archive);
if (!$this->data)
{
throw new \RuntimeException('Unable to read archive');
}
if (!$this->readZipInfo($this->data))
{
throw new \RuntimeException('Get ZIP Information failed');
}
foreach ($this->metadata as $i => $metadata)
{
$lastPathCharacter = substr($metadata['name'], -1, 1);
if ($lastPathCharacter !== '/' && $lastPathCharacter !== '\\')
{
$buffer = $this->getFileData($i);
$path = Path::clean($destination . '/' . $metadata['name']);
if (!$this->isBelow($destination, $path))
{
throw new \OutOfBoundsException('Unable to write outside of destination path', 100);
}
// Make sure the destination folder exists
if (!Folder::create(\dirname($path)))
{
throw new \RuntimeException('Unable to create destination folder');
}
if (!File::write($path, $buffer))
{
throw new \RuntimeException('Unable to write file');
}
}
}
return true;
}
/**
* Extract a ZIP compressed file to a given path using native php api calls for speed
*
* @param string $archive Path to ZIP archive to extract
* @param string $destination Path to extract archive into
*
* @return boolean True on success
*
* @throws \RuntimeException
* @since 1.0
*/
protected function extractNative($archive, $destination)
{
$zip = new \ZipArchive;
if ($zip->open($archive) !== true)
{
throw new \RuntimeException('Unable to open archive');
}
// Make sure the destination folder exists
if (!Folder::create($destination))
{
throw new \RuntimeException('Unable to create destination folder ' . \dirname($destination));
}
// Read files in the archive
for ($index = 0; $index < $zip->numFiles; $index++)
{
$file = $zip->getNameIndex($index);
if (substr($file, -1) === '/')
{
continue;
}
$buffer = $zip->getFromIndex($index);
if ($buffer === false)
{
throw new \RuntimeException('Unable to read ZIP entry');
}
if (!$this->isBelow($destination, $destination . '/' . $file))
{
throw new \RuntimeException('Unable to write outside of destination path', 100);
}
if (File::write($destination . '/' . $file, $buffer) === false)
{
throw new \RuntimeException('Unable to write ZIP entry to file ' . $destination . '/' . $file);
}
}
$zip->close();
return true;
}
/**
* Get the list of files/data from a ZIP archive buffer.
*
* <pre>
* KEY: Position in zipfile
* VALUES: 'attr' -- File attributes
* 'crc' -- CRC checksum
* 'csize' -- Compressed file size
* 'date' -- File modification time
* 'name' -- Filename
* 'method'-- Compression method
* 'size' -- Original file size
* 'type' -- File type
* </pre>
*
* @param string $data The ZIP archive buffer.
*
* @return boolean True on success
*
* @since 1.0
* @throws \RuntimeException
*/
private function readZipInfo($data)
{
$entries = array();
// Find the last central directory header entry
$fhLast = strpos($data, $this->ctrlDirEnd);
do
{
$last = $fhLast;