File "zip.class.php"
Full path: /home/kosmetik/public_html/wp-content/plugins/unlimited-elements-for-elementor/inc_php/framework/zip.class.php
File
size: 14.07 B
MIME-type: text/x-php
Charset: utf-8
Download Open Edit Advanced Editor Back
<?php
defined('UNLIMITED_ELEMENTS_INC') or die('Restricted access');
class UniteZipUC
{
private $_methods = array(0x0 => 'None', 0x1 => 'Shrunk', 0x2 => 'Super Fast', 0x3 => 'Fast', 0x4 => 'Normal', 0x5 => 'Maximum', 0x6 => 'Imploded', 0x8 => 'Deflated');
private $_ctrlDirHeader = "PK\1\2";
private $_ctrlDirEnd = "PK\5\6\0\0\0\0";
private $_fileHeader = "PK\3\4";
private $_data = null;
private $_metadata = null;
private $contents = array();
private $ctrldir = array();
private $isZipArchiveExists = false;
private $zip;
protected function isNativeSupportExists()
{
return function_exists('zip_open') && function_exists('zip_read');
}
protected function isZipArchiveExists()
{
$exists = class_exists("ZipArchive");
return $exists;
}
private function addItem($basePath, $path)
{
$rel_path = str_replace($basePath . "/", "", $path);
if (is_dir($path)) {
if ($basePath != $path) {
if ($this->isZipArchiveExists) {
$this->zip->addEmptyDir($rel_path);
}
}
$files = scandir($path);
foreach ($files as $file) {
if ($file == "." || $file == ".." || $file == ".svn") {
continue;
}
$filepath = $path . "/" . $file;
$this->addItem($basePath, $filepath);
}
} else {
if (!file_exists($path)) {
UniteFunctionsUC::throwError("filepath: '{$path}' don't exists, can't zip");
}
if ($this->isZipArchiveExists) {
$path = str_replace("//", "/", $path);
$this->zip->addFile($path, $rel_path);
} else {
$this->addFileToCustomZip($path, $rel_path);
}
}
}
public function makeZip($srcPath, $zipFilepath, $additionPaths = array())
{
if (!is_dir($srcPath)) {
UniteFunctionsUC::throwError("The path: '{$srcPath}' don't exists, can't zip");
}
$this->isZipArchiveExists = $this->isZipArchiveExists();
if ($this->isZipArchiveExists == true) {
$this->zip = new ZipArchive();
$success = $this->zip->open($zipFilepath, ZipArchive::CREATE);
if ($success == false) {
UniteFunctionsUC::throwError("Can't create zip file: {$zipFilepath}");
}
} else {
$this->contents = array();
$this->ctrldir = array();
}
$this->addItem($srcPath, $srcPath);
if (gettype($additionPaths) != "array") {
UniteFunctionsUC::throwError("Wrong additional paths variable.");
}
if (!empty($additionPaths)) {
foreach ($additionPaths as $path) {
if (!is_dir($path)) {
UniteFunctionsUC::throwError("Path: {$path} not found, can't zip");
}
$this->addItem($path, $path);
}
}
if ($this->isZipArchiveExists == true) {
$this->zip->close();
} else {
$this->_createZIPFile($this->contents, $this->ctrldir, $zipFilepath);
}
}
protected function checkCreateDir($filepath)
{
$dir = dirname($filepath);
if (is_dir($dir) == false) {
$success = $this->checkCreateDir($dir);
} else {
return true;
}
@mkdir($dir);
if (is_dir($dir) == false) {
UniteFunctionsUC::throwError("Can't create directory: {$dir}");
}
}
protected function writeFile($str, $filepath)
{
$this->checkCreateDir($filepath);
$fp = fopen($filepath, "w+");
fwrite($fp, $str);
fclose($fp);
if (file_exists($filepath) == false) {
UniteFunctionsUC::throwError("can't write file: {$filepath}");
}
}
protected function extract_native($src, $dest)
{
if (function_exists("zip_open") == false) {
UniteFunctionsUC::throwError("Please enable zip_open php function in php.ini");
}
$zip = zip_open($src);
if (is_resource($zip) == false) {
UniteFunctionsUC::throwError("Unable to open zip file: {$src}");
}
if (!is_dir($dest)) {
@mkdir($dest);
}
if (!is_dir($dest)) {
UniteFunctionsUC::throwError("Could not create folder: {$dest}");
}
$dest = UniteFunctionsUC::addPathEndingSlash($dest);
while ($file = @zip_read($zip)) {
$entryOpened = zip_entry_open($zip, $file, "r");
if ($entryOpened == false) {
UniteFunctionsUC::throwError("unable to read entry");
}
$filenameCorrent = substr(zip_entry_name($file), strlen(zip_entry_name($file)) - 1) != "/";
if ($filenameCorrent == false) {
zip_entry_close($file);
continue;
}
$buffer = zip_entry_read($file, zip_entry_filesize($file));
$destFilepath = $dest . zip_entry_name($file);
$this->writeFile($buffer, $destFilepath);
}
@zip_close($zip);
return true;
}
protected function extract_zipArchive($src, $dest)
{
$zip = new ZipArchive();
if ($zip->open($src) === true) {
$extracted = @$zip->extractTo($dest);
$zip->close();
if ($extracted == false) {
return false;
}
return true;
}
return false;
}
private function a_MAKEZIP_CUSTOM()
{
}
protected function _unix2DOSTime($unixtime = null)
{
$timearray = is_null($unixtime) ? getdate() : getdate($unixtime);
if ($timearray['year'] < 1980) {
$timearray['year'] = 1980;
$timearray['mon'] = 1;
$timearray['mday'] = 1;
$timearray['hours'] = 0;
$timearray['minutes'] = 0;
$timearray['seconds'] = 0;
}
return $timearray['year'] - 1980 << 25 | $timearray['mon'] << 21 | $timearray['mday'] << 16 | $timearray['hours'] << 11 | $timearray['minutes'] << 5 | $timearray['seconds'] >> 1;
}
private function addFileToCustomZip($path, $rel_path)
{
if (is_file($path) == false) {
UniteFunctionsUC::throwError("can't add to zip file: {$path}");
}
$content = file_get_contents($path);
$time = filemtime($path);
$file = array();
$file["data"] = $content;
$file["name"] = $rel_path;
$file["time"] = $time;
$this->_addToZIPFile($file, $this->contents, $this->ctrldir);
}
private function _addToZIPFile(array &$file, array &$contents, array &$ctrldir)
{
$data =& $file['data'];
$name = str_replace('\\', '/', $file['name']);
$ftime = null;
if (isset($file['time'])) {
$ftime = $file['time'];
}
$dtime = dechex($this->_unix2DosTime($ftime));
$hexdtime = chr(hexdec($dtime[6] . $dtime[7])) . chr(hexdec($dtime[4] . $dtime[5])) . chr(hexdec($dtime[2] . $dtime[3])) . chr(hexdec($dtime[0] . $dtime[1]));
$fr = $this->_fileHeader;
$fr .= "\24\0";
$fr .= "\0\0";
$fr .= "\10\0";
$fr .= $hexdtime;
$unc_len = strlen($data);
$crc = crc32($data);
$zdata = gzcompress($data);
$zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
$c_len = strlen($zdata);
$fr .= pack('V', $crc);
$fr .= pack('V', $c_len);
$fr .= pack('V', $unc_len);
$fr .= pack('v', strlen($name));
$fr .= pack('v', 0);
$fr .= $name;
$fr .= $zdata;
$old_offset = strlen(implode('', $contents));
$contents[] =& $fr;
$cdrec = $this->_ctrlDirHeader;
$cdrec .= "\0\0";
$cdrec .= "\24\0";
$cdrec .= "\0\0";
$cdrec .= "\10\0";
$cdrec .= $hexdtime;
$cdrec .= pack('V', $crc);
$cdrec .= pack('V', $c_len);
$cdrec .= pack('V', $unc_len);
$cdrec .= pack('v', strlen($name));
$cdrec .= pack('v', 0);
$cdrec .= pack('v', 0);
$cdrec .= pack('v', 0);
$cdrec .= pack('v', 0);
$cdrec .= pack('V', 32);
$cdrec .= pack('V', $old_offset);
$cdrec .= $name;
$ctrldir[] =& $cdrec;
}
private function _createZIPFile(array &$contents, array &$ctrlDir, $path)
{
$data = implode('', $contents);
$dir = implode('', $ctrlDir);
$buffer = $data . $dir . $this->_ctrlDirEnd . pack('v', count($ctrlDir)) . pack('v', count($ctrlDir)) . pack('V', strlen($dir)) . pack('V', strlen($data)) . "\0\0";
UniteFunctionsUC::writeFile($buffer, $path);
return true;
}
private function a_EXTRACT_CUSTOM()
{
}
private function extract_custom_readZipInfo(&$data)
{
$entries = array();
$fhLast = strpos($data, $this->_ctrlDirEnd);
do {
$last = $fhLast;
} while (($fhLast = strpos($data, $this->_ctrlDirEnd, $fhLast + 1)) !== false);
$offset = 0;
if ($last) {
$endOfCentralDirectory = unpack('vNumberOfDisk/vNoOfDiskWithStartOfCentralDirectory/vNoOfCentralDirectoryEntriesOnDisk/' . 'vTotalCentralDirectoryEntries/VSizeOfCentralDirectory/VCentralDirectoryOffset/vCommentLength', substr($data, $last + 4));
$offset = $endOfCentralDirectory['CentralDirectoryOffset'];
}
$fhStart = strpos($data, $this->_ctrlDirHeader, $offset);
$dataLength = strlen($data);
do {
if ($dataLength < $fhStart + 31) {
UniteFunctionsUC::throwError('Invalid Zip Data');
}
$info = unpack('vMethod/VTime/VCRC32/VCompressed/VUncompressed/vLength', substr($data, $fhStart + 10, 20));
$name = substr($data, $fhStart + 46, $info['Length']);
$entries[$name] = array('attr' => null, 'crc' => sprintf("%08s", dechex($info['CRC32'])), 'csize' => $info['Compressed'], 'date' => null, '_dataStart' => null, 'name' => $name, 'method' => $this->_methods[$info['Method']], '_method' => $info['Method'], 'size' => $info['Uncompressed'], 'type' => null);
$entries[$name]['date'] = mktime($info['Time'] >> 11 & 0x1f, $info['Time'] >> 5 & 0x3f, $info['Time'] << 1 & 0x3e, $info['Time'] >> 21 & 0x7, $info['Time'] >> 16 & 0x1f, ($info['Time'] >> 25 & 0x7f) + 1980);
if ($dataLength < $fhStart + 43) {
UniteFunctionsUC::throwError('Invalid Zip Data');
}
$info = unpack('vInternal/VExternal/VOffset', substr($data, $fhStart + 36, 10));
$entries[$name]['type'] = $info['Internal'] & 0x1 ? 'text' : 'binary';
$entries[$name]['attr'] = ($info['External'] & 0x10 ? 'D' : '-') . ($info['External'] & 0x20 ? 'A' : '-') . ($info['External'] & 0x3 ? 'S' : '-') . ($info['External'] & 0x2 ? 'H' : '-') . ($info['External'] & 0x1 ? 'R' : '-');
$entries[$name]['offset'] = $info['Offset'];
$lfhStart = strpos($data, $this->_fileHeader, $entries[$name]['offset']);
if ($dataLength < $lfhStart + 34) {
UniteFunctionsUC::throwError('Invalid Zip Data');
}
$info = unpack('vMethod/VTime/VCRC32/VCompressed/VUncompressed/vLength/vExtraLength', substr($data, $lfhStart + 8, 25));
$name = substr($data, $lfhStart + 30, $info['Length']);
$entries[$name]['_dataStart'] = $lfhStart + 30 + $info['Length'] + $info['ExtraLength'];
@set_time_limit(ini_get('max_execution_time'));
} while (($fhStart = strpos($data, $this->_ctrlDirHeader, $fhStart + 46)) !== false);
$this->_metadata = array_values($entries);
return true;
}
private function extract_custom_getFileData($key)
{
if ($this->_metadata[$key]['_method'] == 0x8) {
return gzinflate(substr($this->_data, $this->_metadata[$key]['_dataStart'], $this->_metadata[$key]['csize']));
} elseif ($this->_metadata[$key]['_method'] == 0x0) {
return substr($this->_data, $this->_metadata[$key]['_dataStart'], $this->_metadata[$key]['csize']);
} elseif ($this->_metadata[$key]['_method'] == 0x12) {
if (extension_loaded('bz2')) {
return bzdecompress(substr($this->_data, $this->_metadata[$key]['_dataStart'], $this->_metadata[$key]['csize']));
}
}
return '';
}
protected function extract_custom($src, $dest)
{
$this->_data = null;
$this->_metadata = null;
if (!extension_loaded('zlib')) {
UniteFunctionsUC::throwError('Zlib not supported, please enable in php.ini');
}
$this->_data = file_get_contents($src);
if (!$this->_data) {
UniteFunctionsUC::throwError('Get ZIP Data failed');
}
$success = $this->extract_custom_readZipInfo($this->_data);
if (!$success) {
UniteFunctionsUC::throwError('Get ZIP Information failed');
}
for ($i = 0, $n = count($this->_metadata); $i < $n; $i++) {
$lastPathCharacter = substr($this->_metadata[$i]['name'], -1, 1);
if ($lastPathCharacter !== '/' && $lastPathCharacter !== '\\') {
$buffer = $this->extract_custom_getFileData($i);
$destFilepath = UniteFunctionsUC::cleanPath($dest . '/' . $this->_metadata[$i]['name']);
$this->writeFile($buffer, $destFilepath);
}
}
return true;
}
public function extract($src, $dest)
{
$content = file_get_contents($src);
if ($this->isZipArchiveExists() == true) {
$success = $this->extract_zipArchive($src, $dest);
if ($success == true) {
return true;
}
}
if ($this->isNativeSupportExists() == true) {
try {
$success = $this->extract_native($src, $dest);
} catch (Exception $e) {
$success = false;
}
if ($success == true) {
return true;
}
}
$success = $this->extract_custom($src, $dest);
return $success;
}
}