system: Linux mars.sprixweb.com 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
<?php
class Lib_Zip
{
/**
* Stores compressed Data
*
* @var array $compressedData
*/
private $compressedData = array();
/**
* Stores central directory
*
* @var array $centralDirectory
*/
private $centralDirectory = array();
/**
* Stores end of Central directory record
*
* @var string $endOfCentralDirectory
*/
private $endOfCentralDirectory = "\x50\x4b\x05\x06\x00\x00\x00\x00";
/**
* Stores old Offset value
*
* @var integer $oldOffset
*/
private $oldOffset = 0;
/**
* Stores function type's name
*
* @var string $type
*/
private $type;
/**
* Flag for error
*
* @var bool $error
*/
private $error;
/**
* Stores source path
*
* @var string $source
*/
private $source;
/**
* Stores destination path
*
* @var string $destination
*/
private $destination;
/**
* Stores error numbers and description
*
* @var array $debug
*/
public $debug = array();
/**
* Stores the output
*
* @var bool $result
*/
public $result;
/**
* Constructs a Lib_Zip object with given parameters
* also it will invoke zip and unzip process
*
* @param string $type
* @param string $source
* @param string $destination
* @return Lib_Zip
*/
public function Lib_Zip($type,$source,$destination)
{
$this->type = $type;
$this->source = $source;
$this->destination = $destination;
if($this->isValidCall())
{
if(strtolower($type)=='zip')
$this->zipFiles();
if(strtolower($type)=='unzip')
$this->unZip();
}
}
/**
* Check whether the function call is valid or not
*
* @return bool
*/
private function isValidCall()
{
if(strtolower($this->type)!='zip' && strtolower($this->type)!='unzip')
{
echo '<b>Component Error!<b> Invalid argument <i>type</i> - zip or unzip expected';
exit();
}
else if(empty($this->source))
{
echo '<b>Component Error!<b> Invalid argument data type <i>source</i> - source expected';
exit();
}
else if(is_numeric($this->source))
{
echo '<b>Component Error!<b> Invalid argument data type <i>source</i> - source path expected';
exit();
}
else if(!file_exists($this->source))
{
echo '<b>Component Error!<b> Path not found <i>'.$this->source.'</i> - source path not found';
exit();
}
else if(empty($this->destination))
{
echo '<b>Component Error!<b> Invalid argument data type <i>destination</i> - destination expected';
exit();
}
else if(is_numeric($this->destination))
{
echo '<b>Component Error!<b> Invalid argument data type <i>destination</i> - destination path expected';
exit();
}
/*else if(!file_exists(str_replace(substr(strrchr($this->destination, "\\"),1),"",$this->destination)))
{
echo '<b>Component Error!<b> Invalid argument data type <i>'.$this->destination.'</i> - destination path not found';
exit();
}
else if(!is_writable(str_replace(substr(strrchr($this->destination, "\\"),1),"",$this->destination)))
{
echo '<b>Component Error!<b> Access Denied <i>'.$this->destination.'</i> - destination path not writable';
exit();
}*/
return true;
}
/**
* This function is use to add all directory and subdirectory.
*
* @return array addDirectory
*/
private function addDirectory($directoryName)
{
$directoryName = str_replace("\\", "/", $directoryName);
$feedArrayRow = "\x50\x4b\x03\x04";
$feedArrayRow .= "\x0a\x00";
$feedArrayRow .= "\x00\x00";
$feedArrayRow .= "\x00\x00";
$feedArrayRow .= "\x00\x00\x00\x00";
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("v", strlen($directoryName) );
$feedArrayRow .= pack("v", 0 );
$feedArrayRow .= $directoryName;
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$feedArrayRow .= pack("V",0);
$this -> compressedData[] = $feedArrayRow;
$newOffset = strlen(implode("", $this->compressedData));
$addCentralRecord = "\x50\x4b\x01\x02";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x0a\x00";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x00\x00\x00\x00";
$addCentralRecord .= pack("V",0);
$addCentralRecord .= pack("V",0);
$addCentralRecord .= pack("V",0);
$addCentralRecord .= pack("v", strlen($directoryName) );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$ext = "\x00\x00\x10\x00";
$ext = "\xff\xff\xff\xff";
$addCentralRecord .= pack("V", 16 );
$addCentralRecord .= pack("V", $this -> oldOffset );
$this -> oldOffset = $newOffset;
$addCentralRecord .= $directoryName;
$this -> centralDirectory[] = $addCentralRecord;
}
/**
* This function is use to add all files from the directory.
*
* @return array addFile
*/
private function addFile($data, $directoryName)
{
$directoryName = str_replace("\\", "/", $directoryName);
$feedArrayRow = "\x50\x4b\x03\x04";
$feedArrayRow .= "\x14\x00";
$feedArrayRow .= "\x00\x00";
$feedArrayRow .= "\x08\x00";
$feedArrayRow .= "\x00\x00\x00\x00";
$uncompressedLength = strlen($data);
$compression = crc32($data);
$gzCompressedData = gzcompress($data);
$gzCompressedData = substr( substr($gzCompressedData, 0, strlen($gzCompressedData) - 4), 2);
$compressedLength = strlen($gzCompressedData);
$feedArrayRow .= pack("V",$compression);
$feedArrayRow .= pack("V",$compressedLength);
$feedArrayRow .= pack("V",$uncompressedLength);
$feedArrayRow .= pack("v", strlen($directoryName) );
$feedArrayRow .= pack("v", 0 );
$feedArrayRow .= $directoryName;
$feedArrayRow .= $gzCompressedData;
$feedArrayRow .= pack("V",$compression);
$feedArrayRow .= pack("V",$compressedLength);
$feedArrayRow .= pack("V",$uncompressedLength);
$this -> compressedData[] = $feedArrayRow;
$newOffset = strlen(implode("", $this->compressedData));
$addCentralRecord = "\x50\x4b\x01\x02";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x14\x00";
$addCentralRecord .="\x00\x00";
$addCentralRecord .="\x08\x00";
$addCentralRecord .="\x00\x00\x00\x00";
$addCentralRecord .= pack("V",$compression);
$addCentralRecord .= pack("V",$compressedLength);
$addCentralRecord .= pack("V",$uncompressedLength);
$addCentralRecord .= pack("v", strlen($directoryName) );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("v", 0 );
$addCentralRecord .= pack("V", 32 );
$addCentralRecord .= pack("V", $this -> oldOffset );
$this -> oldOffset = $newOffset;
$addCentralRecord .= $directoryName;
$this -> centralDirectory[] = $addCentralRecord;
}
/**
* This function is use to create zip file.
*
* @return array getZippedfile
*/
private function getZippedfile()
{
$data = implode("", $this -> compressedData);
$controlDirectory = implode("", $this -> centralDirectory);
return
$data.
$controlDirectory.
$this -> endOfCentralDirectory.
pack("v", sizeof($this -> centralDirectory)).
pack("v", sizeof($this -> centralDirectory)).
pack("V", strlen($controlDirectory)).
pack("V", strlen($data)).
"\x00\x00";
}
/**
* This function is use to download the zip file.
*
* @return array forceDownload
*/
private function forceDownload($archiveName)
{
$headerInfo = '';
if(ini_get('zlib.output_compression'))
{
ini_set('zlib.output_compression', 'Off');
}
// Security checks
if( $archiveName == "" )
{
echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> The download file was NOT SPECIFIED.</body></html>";
exit;
}
elseif ( ! file_exists( $archiveName ) )
{
echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> File not found.</body></html>";
exit;
}
}
/**
* This function is use get all the files from folder and sub folder.
*
* @return array getFilesfromfolder
*/
private function getFilesfromfolder($directory, $put_into)
{
if ($handle = opendir($directory))
{
while (false !== ($file = readdir($handle)))
{
if (is_file($directory.$file))
{
$fileContents = file_get_contents($directory.$file);
$this->addFile($fileContents, $put_into.$file);
}
elseif ($file != '.' and $file != '..' and is_dir($directory.$file))
{
$this->addDirectory($put_into.$file.'/');
$this->getFilesfromfolder($directory.$file.'/', $put_into.$file.'/');
}
}
}
closedir($handle);
}
/**
* This function is use to call all zip functions and zip the folder.
*
* @return bool
*/
private function zipFiles()
{
if(substr(strtolower($this->destination),-4)==".zip")
{
$this->addDirectory($this->source."/");
$this->getFilesfromfolder($this->source.'/',$this->source.'/');
$fd = fopen ($this->destination, 'wb');
$out = fwrite ($fd, $this->getZippedfile());
fclose ($fd);
$this->forceDownload($this->destination);
$result="true";
$this->result=$result;
return true;
}
else
{
$this->error = 1;
$this->debug['errinfo'] = array(1001=>'Please give the zip extension to destination Parameter');
return false;
}
}
/**
* This function is use to call all un zip functions and un zip the folder.
*
* @return bool
*/
private function unZip()
{
if(substr(strtolower($this->source),-4)==".zip")
{
$zip = new ZipArchive;
$res = $zip->open($this->source);
if ($res === TRUE)
{
$zip->extractTo($this->destination);
$zip->close();
$result="true";
$this->result=$result;
return true;
}
else
{
$this->error = 1;
$this->debug['errinfo'] = array(1003=>'source file does not open');
return false;
}
}
else
{
$this->error = 1;
$this->debug['errinfo'] = array(1002=>'Please give the zip extension to destination Parameter');
return false;
}
}
}
?>