This commit is contained in:
2024-05-20 15:37:46 +03:00
commit 00b7dbd0b7
10404 changed files with 3285853 additions and 0 deletions

View File

@ -0,0 +1,197 @@
<?php
namespace Nextend\Framework\Misc\Zip;
/**
* Zip file creation class.
* Makes zip files.
*
* @access public
* @package PhpMyAdmin
* @see Official ZIP file format: http://www.pkware.com/support/zip-app-note
*/
class Creator {
/**
* Whether to echo zip as it's built or return as string from -> file
*
* @var boolean $doWrite
*/
var $doWrite = false;
/**
* Array to store compressed data
*
* @var array $datasec
*/
var $datasec = array();
/**
* Central directory
*
* @var array $ctrl_dir
*/
var $ctrl_dir = array();
/**
* End of central directory record
*
* @var string $eof_ctrl_dir
*/
var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
/**
* Last offset position
*
* @var integer $old_offset
*/
var $old_offset = 0;
/**
* Sets member variable this -> doWrite to true
* - Should be called immediately after class instantiantion
* - If set to true, then ZIP archive are echo'ed to STDOUT as each
* file is added via this -> addfile(), and central directories are
* echoed to STDOUT on final call to this -> file(). Also,
* this -> file() returns an empty string so it is safe to issue a
* "echo $zipfile;" command
*
* @access public
*
* @return void
*/
function setDoWrite() {
$this->doWrite = true;
} // end of the 'setDoWrite()' method
/**
* Converts an Unix timestamp to a four byte DOS date and time format (date
* in high two bytes, time in low two bytes allowing magnitude comparison).
*
* @param integer $unixtime the current Unix timestamp
*
* @return integer the current date in a four byte DOS format
*
* @access private
*/
function unix2DosTime($unixtime = 0) {
$timearray = ($unixtime == 0) ? 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;
} // end if
return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
} // end of the 'unix2DosTime()' method
/**
* Adds "file" to archive
*
* @param string $data file contents
* @param string $name name of the file in the archive (may contains the path)
* @param integer $time the current timestamp
*
* @access public
*
* @return void
*/
function addFile($data, $name, $time = 0) {
$name = str_replace('\\', '/', $name);
$hexdtime = pack('V', $this->unix2DosTime($time));
$fr = "\x50\x4b\x03\x04";
$fr .= "\x14\x00"; // ver needed to extract
$fr .= "\x00\x00"; // gen purpose bit flag
$fr .= "\x08\x00"; // compression method
$fr .= $hexdtime; // last mod time and date
// "local file header" segment
$unc_len = strlen($data);
$crc = crc32($data);
$zdata = gzcompress($data);
$zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
$c_len = strlen($zdata);
$fr .= pack('V', $crc); // crc32
$fr .= pack('V', $c_len); // compressed filesize
$fr .= pack('V', $unc_len); // uncompressed filesize
$fr .= pack('v', strlen($name)); // length of filename
$fr .= pack('v', 0); // extra field length
$fr .= $name;
// "file data" segment
$fr .= $zdata;
// echo this entry on the fly, ...
if ($this->doWrite) {
// PHPCS - The value contains zip file binary data, so nothing to escape
echo $fr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else { // ... OR add this entry to array
$this->datasec[] = $fr;
}
// now add to central directory record
$cdrec = "\x50\x4b\x01\x02";
$cdrec .= "\x00\x00"; // version made by
$cdrec .= "\x14\x00"; // version needed to extract
$cdrec .= "\x00\x00"; // gen purpose bit flag
$cdrec .= "\x08\x00"; // compression method
$cdrec .= $hexdtime; // last mod time & date
$cdrec .= pack('V', $crc); // crc32
$cdrec .= pack('V', $c_len); // compressed filesize
$cdrec .= pack('V', $unc_len); // uncompressed filesize
$cdrec .= pack('v', strlen($name)); // length of filename
$cdrec .= pack('v', 0); // extra field length
$cdrec .= pack('v', 0); // file comment length
$cdrec .= pack('v', 0); // disk number start
$cdrec .= pack('v', 0); // internal file attributes
$cdrec .= pack('V', 32); // external file attributes
// - 'archive' bit set
$cdrec .= pack('V', $this->old_offset); // relative offset of local header
$this->old_offset += strlen($fr);
$cdrec .= $name;
// optional extra field, file comment goes here
// save to central directory
$this->ctrl_dir[] = $cdrec;
} // end of the 'addFile()' method
/**
* Echo central dir if ->doWrite==true, else build string to return
*
* @return string if ->doWrite {empty string} else the ZIP file contents
*
* @access public
*/
function file() {
$ctrldir = implode('', $this->ctrl_dir);
$header = $ctrldir . $this->eof_ctrl_dir . pack('v', sizeof($this->ctrl_dir)) . //total #of entries "on this disk"
pack('v', sizeof($this->ctrl_dir)) . //total #of entries overall
pack('V', strlen($ctrldir)) . //size of central dir
pack('V', $this->old_offset) . //offset to start of central dir
"\x00\x00"; //.zip file comment length
if ($this->doWrite) { // Send central directory & end ctrl dir to STDOUT
// PHPCS - The value is zip file binary header, so nothing to escape.
echo $header; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return ""; // Return empty string
} else { // Return entire ZIP archive as string
$data = implode('', $this->datasec);
return $data . $header;
}
} // end of the 'file()' method
}

View File

@ -0,0 +1,20 @@
<?php
namespace Nextend\Framework\Misc\Zip;
use Nextend\Framework\Misc\Zip\Reader\Custom;
use Nextend\Framework\Misc\Zip\Reader\ZipExtension;
class Reader {
public static function read($path) {
if (class_exists('ZipArchive') && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
$reader = new ZipExtension();
} else {
$reader = new Custom();
}
return $reader->read($path);
}
}

View File

@ -0,0 +1,129 @@
<?php
namespace Nextend\Framework\Misc\Zip\Reader;
use Nextend\Framework\Misc\Zip\ReaderInterface;
use Nextend\Framework\Notification\Notification;
class Custom implements ReaderInterface {
private $fileHandle;
private $file;
public function read($path) {
$this->file = $path;
return $this->extract();
}
private function extract() {
$extractedData = array();
if (!is_readable(dirname($this->file))) {
Notification::error(sprintf(n2_('%s is not readable'), dirname($this->file)));
return false;
}
if (!$this->file || !is_file($this->file)) return false;
$filesize = sprintf('%u', filesize($this->file));
$this->fileHandle = fopen($this->file, 'rb');
$EofCentralDirData = $this->_findEOFCentralDirectoryRecord($filesize);
if (!is_array($EofCentralDirData)) return false;
$centralDirectoryHeaderOffset = $EofCentralDirData['centraldiroffset'];
for ($i = 0; $i < $EofCentralDirData['totalentries']; $i++) {
rewind($this->fileHandle);
fseek($this->fileHandle, $centralDirectoryHeaderOffset);
$centralDirectoryData = $this->_readCentralDirectoryData();
$centralDirectoryHeaderOffset += 46 + $centralDirectoryData['filenamelength'] + $centralDirectoryData['extrafieldlength'] + $centralDirectoryData['commentlength'];
if (!is_array($centralDirectoryData) || substr($centralDirectoryData['filename'], -1) == '/') continue;
$data = $this->_readLocalFileHeaderAndData($centralDirectoryData);
if (!$data) continue;
$dir = dirname($centralDirectoryData['filename']);
$fileName = basename($centralDirectoryData['filename']);
if ($dir != '.' && $dir != '') {
if (!isset($extractedData[$dir])) {
$extractedData[$dir] = array();
}
$extractedData[$dir][$fileName] = $data;
} else {
$extractedData[$fileName] = $data;
}
}
fclose($this->fileHandle);
return $extractedData;
}
private function _findEOFCentralDirectoryRecord($filesize) {
fseek($this->fileHandle, $filesize - 22);
$EofCentralDirSignature = unpack('Vsignature', fread($this->fileHandle, 4));
if ($EofCentralDirSignature['signature'] != 0x06054b50) {
$maxLength = 65535 + 22;
$maxLength > $filesize && $maxLength = $filesize;
fseek($this->fileHandle, $filesize - $maxLength);
$searchPos = ftell($this->fileHandle);
while ($searchPos < $filesize) {
fseek($this->fileHandle, $searchPos);
$sigData = unpack('Vsignature', fread($this->fileHandle, 4));
if ($sigData['signature'] == 0x06054b50) {
break;
}
$searchPos++;
}
}
$EofCentralDirData = unpack('vdisknum/vdiskstart/vcentraldirnum/vtotalentries/Vcentraldirsize/Vcentraldiroffset/vcommentlength', fread($this->fileHandle, 18));
return $EofCentralDirData;
}
private function _readCentralDirectoryData() {
$centralDirectorySignature = unpack('Vsignature', fread($this->fileHandle, 4));
if ($centralDirectorySignature['signature'] != 0x02014b50) return false;
$centralDirectoryData = fread($this->fileHandle, 42);
$centralDirectoryData = unpack('vmadeversion/vextractversion/vflag/vcompressmethod/vmodtime/vmoddate/Vcrc/Vcompressedsize/Vuncompressedsize/vfilenamelength/vextrafieldlength/vcommentlength/vdiskstart/vinternal/Vexternal/Vlocalheaderoffset', $centralDirectoryData);
$centralDirectoryData['filenamelength'] && $centralDirectoryData['filename'] = fread($this->fileHandle, $centralDirectoryData['filenamelength']);
return $centralDirectoryData;
}
private function _readLocalFileHeaderAndData($centralDirectoryData) {
fseek($this->fileHandle, $centralDirectoryData['localheaderoffset']);
$localFileHeaderSignature = unpack('Vsignature', fread($this->fileHandle, 4));
if ($localFileHeaderSignature['signature'] != 0x04034b50) return false;
$localFileHeaderData = fread($this->fileHandle, 26);
$localFileHeaderData = unpack('vextractversion/vflag/vcompressmethod/vmodtime/vmoddate/Vcrc/Vcompressedsize/Vuncompressedsize/vfilenamelength/vextrafieldlength', $localFileHeaderData);
$localFileHeaderData['filenamelength'] && $localFileHeaderData['filename'] = fread($this->fileHandle, $localFileHeaderData['filenamelength']);
if (!$this->_checkLocalFileHeaderAndCentralDir($localFileHeaderData, $centralDirectoryData)) return false;
if ($localFileHeaderData['flag'] & 1) return false;
$compressedData = fread($this->fileHandle, $localFileHeaderData['compressedsize']);
$data = $this->_unCompressData($compressedData, $localFileHeaderData['compressmethod']);
if (crc32($data) != $localFileHeaderData['crc'] || strlen($data) != $localFileHeaderData['uncompressedsize']) return false;
return $data;
}
private function _unCompressData($data, $compressMethod) {
if (!$compressMethod) return $data;
switch ($compressMethod) {
case 8 :
$data = gzinflate($data);
break;
default :
return false;
break;
}
return $data;
}
private function _checkLocalFileHeaderAndCentralDir($localFileHeaderData, $centralDirectoryData) {
return true;
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace Nextend\Framework\Misc\Zip\Reader;
use Nextend\Framework\Misc\Zip\ReaderInterface;
use ZipArchive;
class ZipExtension implements ReaderInterface {
public function read($path) {
$zip = new ZipArchive();
if (!$zip->open($path)) {
return array();
}
$data = array();
for ($i = 0; $i < $zip->numFiles; $i++) {
$stat = $zip->statIndex($i);
$this->recursiveRead($data, explode('/', $stat['name']), $zip->getFromIndex($i));
}
$zip->close();
return $data;
}
private function recursiveRead(&$data, $parts, $content) {
if (count($parts) == 1) {
$data[$parts[0]] = $content;
} else {
if (!isset($data[$parts[0]])) {
$data[$parts[0]] = array();
}
$this->recursiveRead($data[array_shift($parts)], $parts, $content);
}
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace Nextend\Framework\Misc\Zip;
interface ReaderInterface {
public function read($path);
}