CODE STYLE class.archive.php

This commit is contained in:
Fernando Ontiveros
2012-10-09 12:27:04 -04:00
parent 0d0fd1c50c
commit 5b60cca280

View File

@@ -1,44 +1,34 @@
<?php
/*--------------------------------------------------
| TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES 2.1
| By Devin Doucette
| Copyright (c) 2005 Devin Doucette
| Email: darksnoopy@shaw.ca
+--------------------------------------------------
| Email bugs/suggestions to darksnoopy@shaw.ca
+--------------------------------------------------
| This script has been created and released under
| the GNU GPL and is free to use and redistribute
| only if this copyright statement is not removed
+--------------------------------------------------*/
* TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES 2.1
* By Devin Doucette
* Copyright (c) 2005 Devin Doucette
* Email: darksnoopy@shaw.ca
*--------------------------------------------------
* Email bugs/suggestions to darksnoopy@shaw.ca
*--------------------------------------------------
* This script has been created and released under
* the GNU GPL and is free to use and redistribute
* only if this copyright statement is not removed
*--------------------------------------------------*/
/**
*
* @package workflow.engine.classes
*/
class archive
{
/**
* This function is the constructor of the class archive
*
* @param string $name
* @return void
*
*/
function archive($name)
public function archive ($name)
{
$this->options = array (
'basedir' => ".",
'name' => $name,
'prepend' => "",
'inmemory' => 0,
'overwrite' => 0,
'recurse' => 1,
'storepaths' => 1,
'followlinks' => 0,
'level' => 3,
'method' => 1,
'sfx' => "",
'type' => "",
'comment' => ""
$this->options = array ('basedir' => ".",'name' => $name,'prepend' => "",'inmemory' => 0,'overwrite' => 0,'recurse' => 1,'storepaths' => 1,'followlinks' => 0,'level' => 3,'method' => 1,'sfx' => "",'type' => "",'comment' => ""
);
$this->files = array ();
$this->exclude = array ();
@@ -48,13 +38,15 @@ class archive
/**
* This function gives options to a archive
*
* @param array $options
* @return void
*/
function set_options($options)
public function set_options ($options)
{
foreach ($options as $key => $value)
foreach ($options as $key => $value) {
$this->options[$key] = $value;
}
if (! empty( $this->options['basedir'] )) {
$this->options['basedir'] = str_replace( "\\", "/", $this->options['basedir'] );
$this->options['basedir'] = preg_replace( "/\/+/", "/", $this->options['basedir'] );
@@ -74,9 +66,10 @@ class archive
/**
* This function is used to create a archive.
*
* @return boolean
*/
function create_archive()
public function create_archive ()
{
$this->make_list();
if ($this->options['inmemory'] == 0) {
@@ -86,16 +79,14 @@ class archive
$this->error[] = "File {$this->options['name']} already exist.";
chdir( $pwd );
return 0;
}
else if ($this->archive = @fopen($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : ""), "wb+"))
} else if ($this->archive = @fopen( $this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : ""), "wb+" )) {
chdir( $pwd );
else{
} else {
$this->error[] = "Could not open {$this->options['name']} for writing.";
chdir( $pwd );
return 0;
}
}
else
} else
$this->archive = "";
switch ($this->options['type']) {
case "zip":
@@ -132,29 +123,34 @@ class archive
}
if ($this->options['inmemory'] == 0) {
fclose( $this->archive );
if ($this->options['type'] == "gzip" || $this->options['type'] == "bzip")
if ($this->options['type'] == "gzip" || $this->options['type'] == "bzip") {
unlink( $this->options['basedir'] . "/" . $this->options['name'] . ".tmp" );
}
}
}
/**
* This function is used for add data to a archive
*
* @param string $data
* @return void
*/
function add_data($data)
public function add_data ($data)
{
if ($this->options['inmemory'] == 0)
if ($this->options['inmemory'] == 0) {
fwrite( $this->archive, $data );
else
}
else {
$this->archive .= $data;
}
}
/**
* This function make a list
*
* @return void
*/
function make_list()
public function make_list ()
{
if (! empty( $this->exclude ))
foreach ($this->files as $key => $value)
@@ -171,10 +167,11 @@ class archive
/**
* Add files a list
*
* @param array $list
* @return void
*/
function add_files($list)
public function add_files ($list)
{
$temp = $this->list_files( $list );
foreach ($temp as $current)
@@ -183,10 +180,11 @@ class archive
/**
* This function exclude files of a list
*
* @param array $list
* @return void
*/
function exclude_files($list)
public function exclude_files ($list)
{
$temp = $this->list_files( $list );
foreach ($temp as $current)
@@ -195,9 +193,10 @@ class archive
/**
* This function store files
*
* @param array $list
*/
function store_files($list)
public function store_files ($list)
{
$temp = $this->list_files( $list );
foreach ($temp as $current)
@@ -206,14 +205,16 @@ class archive
/**
* List files gives a List
*
* @param array $list
* @return array
*/
function list_files($list)
public function list_files ($list)
{
if (! is_array( $list )) {
$temp = $list;
$list = array ($temp);
$list = array ($temp
);
unset( $temp );
}
$files = array ();
@@ -232,37 +233,34 @@ class archive
if (preg_match( "/^{$regex}$/i", $current2['name'] ))
$files[] = $current2;
unset( $regex, $dir, $temp, $current );
}
else if (@is_dir($current)){
} else if (@is_dir( $current )) {
$temp = $this->parse_dir( $current );
foreach ($temp as $file)
$files[] = $file;
unset( $temp, $file );
}
else if (@file_exists($current))
$files[] = array ('name' => $current, 'name2' => $this->options['prepend'] .
preg_replace("/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr($current, "/")) ?
substr($current, strrpos($current, "/") + 1) : $current),
'type' => @is_link($current) && $this->options['followlinks'] == 0 ? 2 : 0,
'ext' => substr($current, strrpos($current, ".")), 'stat' => stat($current));
} else if (@file_exists( $current ))
$files[] = array ('name' => $current,'name2' => $this->options['prepend'] . preg_replace( "/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr( $current, "/" )) ? substr( $current, strrpos( $current, "/" ) + 1 ) : $current ),'type' => @is_link( $current ) && $this->options['followlinks'] == 0 ? 2 : 0,'ext' => substr( $current, strrpos( $current, "." ) ),'stat' => stat( $current )
);
}
chdir( $pwd );
unset( $current, $pwd );
usort($files, array ("archive", "sort_files"));
usort( $files, array ("archive","sort_files"
) );
return $files;
}
/**
* This function is for parse a directory name
*
* @param string $dirname
* @return array
*/
function parse_dir($dirname)
public function parse_dir ($dirname)
{
if ($this->options['storepaths'] == 1 && ! preg_match( "/^(\.+\/*)+$/", $dirname ))
$files = array (array ('name' => $dirname, 'name2' => $this->options['prepend'] .
preg_replace("/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr($dirname, "/")) ?
substr($dirname, strrpos($dirname, "/") + 1) : $dirname), 'type' => 5, 'stat' => stat($dirname)));
$files = array (array ('name' => $dirname,'name2' => $this->options['prepend'] . preg_replace( "/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr( $dirname, "/" )) ? substr( $dirname, strrpos( $dirname, "/" ) + 1 ) : $dirname ),'type' => 5,'stat' => stat( $dirname )
)
);
else
$files = array ();
$dir = @opendir( $dirname );
@@ -276,13 +274,9 @@ class archive
$temp = $this->parse_dir( $fullname );
foreach ($temp as $file2)
$files[] = $file2;
}
else if (@file_exists($fullname))
$files[] = array ('name' => $fullname, 'name2' => $this->options['prepend'] .
preg_replace("/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr($fullname, "/")) ?
substr($fullname, strrpos($fullname, "/") + 1) : $fullname),
'type' => @is_link($fullname) && $this->options['followlinks'] == 0 ? 2 : 0,
'ext' => substr($file, strrpos($file, ".")), 'stat' => stat($fullname));
} else if (@file_exists( $fullname ))
$files[] = array ('name' => $fullname,'name2' => $this->options['prepend'] . preg_replace( "/(\.+\/+)+/", "", ($this->options['storepaths'] == 0 && strstr( $fullname, "/" )) ? substr( $fullname, strrpos( $fullname, "/" ) + 1 ) : $fullname ),'type' => @is_link( $fullname ) && $this->options['followlinks'] == 0 ? 2 : 0,'ext' => substr( $file, strrpos( $file, "." ) ),'stat' => stat( $fullname )
);
}
@closedir( $dir );
return $files;
@@ -290,11 +284,12 @@ class archive
/**
* This function sort two files
*
* @param array $a
* @param array $b
* @return boolean
*/
function sort_files($a, $b)
public function sort_files ($a, $b)
{
if ($a['type'] != $b['type'])
if ($a['type'] == 5 || $b['type'] == 2)
@@ -314,9 +309,10 @@ class archive
/**
* This function download a file
*
* @return void
*/
function download_file()
public function download_file ()
{
if ($this->options['inmemory'] == 0) {
$this->error[] = "Can only use download_file() if archive is in memory. Redirect to file otherwise, it is faster.";
@@ -348,27 +344,33 @@ class archive
}
/**
* This class is derived from the class archive, is imployed to use files .tar
* This class is derived from the class archive, is imployed to use files .
* tar
*
* @package workflow.engine.classes
*
*/
class tar_file extends archive
{
/**
* This function is the constructor of the class tar_file
*
* @param string $name
*/
function tar_file($name)
public function tar_file ($name)
{
$this->archive( $name );
$this->options['type'] = "tar";
}
/**
* This function create a file .tar
* This function create a file .
* tar
*
* @return boolean
*/
function create_tar()
public function create_tar ()
{
$pwd = getcwd();
chdir( $this->options['basedir'] );
@@ -383,11 +385,7 @@ class tar_file extends archive
continue;
}
}
$block = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12", $current['name2'], sprintf("%07o",
$current['stat'][2]), sprintf("%07o", $current['stat'][4]), sprintf("%07o", $current['stat'][5]),
sprintf("%011o", $current['type'] == 2 ? 0 : $current['stat'][7]), sprintf("%011o", $current['stat'][9]),
" ", $current['type'], $current['type'] == 2 ? @readlink($current['name']) : "", "ustar ", " ",
"Unknown", "Unknown", "", "", !empty ($path) ? $path : "", "");
$block = pack( "a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12", $current['name2'], sprintf( "%07o", $current['stat'][2] ), sprintf( "%07o", $current['stat'][4] ), sprintf( "%07o", $current['stat'][5] ), sprintf( "%011o", $current['type'] == 2 ? 0 : $current['stat'][7] ), sprintf( "%011o", $current['stat'][9] ), " ", $current['type'], $current['type'] == 2 ? @readlink( $current['name'] ) : "", "ustar ", " ", "Unknown", "Unknown", "", "", ! empty( $path ) ? $path : "", "" );
$checksum = 0;
for ($i = 0; $i < 512; $i ++)
$checksum += ord( substr( $block, $i, 1 ) );
@@ -406,8 +404,7 @@ class tar_file extends archive
$this->add_data( $temp );
}
fclose( $fp );
}
else
} else
$this->error[] = "Could not open file {$current['name']} for reading. It was not added.";
}
$this->add_data( pack( "a1024", "" ) );
@@ -417,9 +414,10 @@ class tar_file extends archive
/**
* This function is used for extract files of the class tar_file
*
* @return void
*/
function extract_files()
public function extract_files ()
{
$pwd = getcwd();
chdir( $this->options['basedir'] );
@@ -428,18 +426,8 @@ class tar_file extends archive
$this->files = array ();
while ($block = fread( $fp, 512 )) {
$temp = unpack( "a100name/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100symlink/a6magic/a2temp/a32temp/a32temp/a8temp/a8temp/a155prefix/a12temp", $block );
$file = array (
'name' => $this->options['basedir'] . '/' . $temp['prefix'] . $temp['name'],
'stat' => array (
2 => $temp['mode'],
4 => octdec($temp['uid']),
5 => octdec($temp['gid']),
7 => octdec($temp['size']),
9 => octdec($temp['mtime']),
),
'checksum' => octdec($temp['checksum']),
'type' => $temp['type'],
'magic' => $temp['magic'],
$file = array ('name' => $this->options['basedir'] . '/' . $temp['prefix'] . $temp['name'],'stat' => array (2 => $temp['mode'],4 => octdec( $temp['uid'] ),5 => octdec( $temp['gid'] ),7 => octdec( $temp['size'] ),9 => octdec( $temp['mtime'] )
),'checksum' => octdec( $temp['checksum'] ),'type' => $temp['type'],'magic' => $temp['magic']
);
if ($file['checksum'] == 0x00000000)
break;
@@ -458,21 +446,17 @@ class tar_file extends archive
fread( $fp, (512 - $file['stat'][7] % 512) == 512 ? 0 : (512 - $file['stat'][7] % 512) );
unset( $file['checksum'], $file['magic'] );
$this->files[] = $file;
}
else if ($file['type'] == 5){
} else if ($file['type'] == 5) {
if (! is_dir( $file['name'] ))
//mkdir($file['name'], $file['stat'][2]);
mkdir( $file['name'], 0775 );
}
else if ($this->options['overwrite'] == 0 && file_exists($file['name'])){
} else if ($this->options['overwrite'] == 0 && file_exists( $file['name'] )) {
$this->error[] = "{$file['name']} already exist.";
continue;
}
else if ($file['type'] == 2){
} else if ($file['type'] == 2) {
symlink( $temp['symlink'], $file['name'] );
//chmod($file['name'], $file['stat'][2]);
}
else if ($new = @fopen($file['name'], "wb")){
} else if ($new = @fopen( $file['name'], "wb" )) {
fwrite( $new, fread( $fp, $file['stat'][7] ) );
if ((512 - $file['stat'][7] % 512) != 512) {
fread( $fp, (512 - $file['stat'][7] % 512) );
@@ -482,8 +466,7 @@ class tar_file extends archive
//chmod($file['name'], $file['stat'][2]);
chmod( $file['name'], 0777 );
$this->files[] = $file['name'];
}
else{
} else {
$this->error[] = "Could not open {$file['name']} for writing.";
continue;
}
@@ -492,45 +475,51 @@ class tar_file extends archive
@touch( $file['name'], $file['stat'][9] );
unset( $file );
}
}
else
} else
$this->error[] = "Could not open file {$this->options['name']}";
chdir( $pwd );
}
/**
* This function open a archive of the class tar_file
*
* @return void
*/
function open_archive()
public function open_archive ()
{
return @fopen( $this->options['name'], "rb" );
}
}
/**
* This class is derived of the class archive, is employed to use archives .gzip
* This class is derived of the class archive, is employed to use archives .
* gzip
*
* @package workflow.engine.classes
*
*/
class gzip_file extends tar_file
{
/**
* This function is the constructor of the class gzip_file
*
* @param string $name
* @return void
*/
function gzip_file($name)
public function gzip_file ($name)
{
$this->tar_file( $name );
$this->options['type'] = "gzip";
}
/**
* This function is employed to create files .gzip
* This function is employed to create files .
* gzip
*
* @return boolean
*/
function create_gzip()
public function create_gzip ()
{
if ($this->options['inmemory'] == 0) {
$pwd = getcwd();
@@ -541,52 +530,57 @@ class gzip_file extends tar_file
gzwrite( $fp, $temp );
gzclose( $fp );
chdir( $pwd );
}
else{
} else {
$this->error[] = "Could not open {$this->options['name']} for writing.";
chdir( $pwd );
return 0;
}
}
else
} else
$this->archive = gzencode( $this->archive, $this->options['level'] );
return 1;
}
/**
* This function open a archive of the class gzip_file
*
* @return void
*/
function open_archive()
public function open_archive ()
{
return @gzopen( $this->options['name'], "rb" );
}
}
/**
*
*
* This class is derived from the class archive, is employed to use files .bzip
*
* @package workflow.engine.classes
*
*/
class bzip_file extends tar_file
{
/**
* This function is the constructor of the class bzip_file
*
* @param string $name
* @return void
*/
function bzip_file($name)
public function bzip_file ($name)
{
$this->tar_file( $name );
$this->options['type'] = "bzip";
}
/**
* This function is employed to create files .bzip
* This function is employed to create files .
* bzip
*
* @return boolean
*/
function create_bzip()
public function create_bzip ()
{
if ($this->options['inmemory'] == 0) {
$pwd = getcwd();
@@ -597,14 +591,12 @@ class bzip_file extends tar_file
bzwrite( $fp, $temp );
bzclose( $fp );
chdir( $pwd );
}
else{
} else {
$this->error[] = "Could not open {$this->options['name']} for writing.";
chdir( $pwd );
return 0;
}
}
else
} else
$this->archive = bzcompress( $this->archive, $this->options['level'] );
return 1;
@@ -612,32 +604,38 @@ class bzip_file extends tar_file
/**
* This function open a archive of the class bzip_file
*
* @return void
*/
function open_archive()
public function open_archive ()
{
return @bzopen( $this->options['name'], "rb" );
}
}
/**
* This class is derived from the class archive, is imployed to use files .zip
* This class is derived from the class archive, is imployed to use files .
* zip
*
* @package workflow.engine.classes
*/
class zip_file extends archive
{
function zip_file($name)
public function zip_file ($name)
{
$this->archive( $name );
$this->options['type'] = "zip";
}
/**
* This function is used to create archives .zip
* This function is used to create archives .
* zip
*
* @return boolean
*/
function create_zip()
public function create_zip ()
{
$files = 0;
$offset = 0;
@@ -649,8 +647,7 @@ class zip_file extends archive
$this->add_data( $temp );
$offset += strlen( $temp );
unset( $temp );
}
else
} else
$this->error[] = "Could not open sfx module from {$this->options['sfx']}.";
$pwd = getcwd();
chdir( $this->options['basedir'] );
@@ -658,32 +655,25 @@ class zip_file extends archive
if ($current['name'] == $this->options['name'])
continue;
$timedate = explode( " ", date( "Y n j G i s", $current['stat'][9] ) );
$timedate = ($timedate[0] - 1980 << 25) | ($timedate[1] << 21) | ($timedate[2] << 16) |
($timedate[3] << 11) | ($timedate[4] << 5) | ($timedate[5]);
$timedate = ($timedate[0] - 1980 << 25) | ($timedate[1] << 21) | ($timedate[2] << 16) | ($timedate[3] << 11) | ($timedate[4] << 5) | ($timedate[5]);
$block = pack( "VvvvV", 0x04034b50, 0x000A, 0x0000, (isset( $current['method'] ) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate );
if ($current['stat'][7] == 0 && $current['type'] == 5) {
$block .= pack( "VVVvv", 0x00000000, 0x00000000, 0x00000000, strlen( $current['name2'] ) + 1, 0x0000 );
$block .= $current['name2'] . "/";
$this->add_data( $block );
$central .= pack("VvvvvVVVVvvvvvVV", 0x02014b50, 0x0014, $this->options['method'] == 0 ? 0x0000 : 0x000A, 0x0000,
(isset($current['method']) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate,
0x00000000, 0x00000000, 0x00000000, strlen($current['name2']) + 1, 0x0000, 0x0000, 0x0000, 0x0000, $current['type'] == 5 ? 0x00000010 : 0x00000000, $offset);
$central .= pack( "VvvvvVVVVvvvvvVV", 0x02014b50, 0x0014, $this->options['method'] == 0 ? 0x0000 : 0x000A, 0x0000, (isset( $current['method'] ) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate, 0x00000000, 0x00000000, 0x00000000, strlen( $current['name2'] ) + 1, 0x0000, 0x0000, 0x0000, 0x0000, $current['type'] == 5 ? 0x00000010 : 0x00000000, $offset );
$central .= $current['name2'] . "/";
$files ++;
$offset += (31 + strlen( $current['name2'] ));
}
else if ($current['stat'][7] == 0){
} else if ($current['stat'][7] == 0) {
$block .= pack( "VVVvv", 0x00000000, 0x00000000, 0x00000000, strlen( $current['name2'] ), 0x0000 );
$block .= $current['name2'];
$this->add_data( $block );
$central .= pack("VvvvvVVVVvvvvvVV", 0x02014b50, 0x0014, $this->options['method'] == 0 ? 0x0000 : 0x000A, 0x0000,
(isset($current['method']) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate,
0x00000000, 0x00000000, 0x00000000, strlen($current['name2']), 0x0000, 0x0000, 0x0000, 0x0000, $current['type'] == 5 ? 0x00000010 : 0x00000000, $offset);
$central .= pack( "VvvvvVVVVvvvvvVV", 0x02014b50, 0x0014, $this->options['method'] == 0 ? 0x0000 : 0x000A, 0x0000, (isset( $current['method'] ) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate, 0x00000000, 0x00000000, 0x00000000, strlen( $current['name2'] ), 0x0000, 0x0000, 0x0000, 0x0000, $current['type'] == 5 ? 0x00000010 : 0x00000000, $offset );
$central .= $current['name2'];
$files ++;
$offset += (30 + strlen( $current['name2'] ));
}
else if ($fp = @fopen($current['name'], "rb")){
} else if ($fp = @fopen( $current['name'], "rb" )) {
$temp = fread( $fp, $current['stat'][7] );
fclose( $fp );
$crc32 = crc32( $temp );
@@ -691,31 +681,26 @@ class zip_file extends archive
$temp = gzcompress( $temp, $this->options['level'] );
$size = strlen( $temp ) - 6;
$temp = substr( $temp, 2, $size );
}
else
} else
$size = strlen( $temp );
$block .= pack( "VVVvv", $crc32, $size, $current['stat'][7], strlen( $current['name2'] ), 0x0000 );
$block .= $current['name2'];
$this->add_data( $block );
$this->add_data( $temp );
unset( $temp );
$central .= pack("VvvvvVVVVvvvvvVV", 0x02014b50, 0x0014, $this->options['method'] == 0 ? 0x0000 : 0x000A, 0x0000,
(isset($current['method']) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate,
$crc32, $size, $current['stat'][7], strlen($current['name2']), 0x0000, 0x0000, 0x0000, 0x0000, 0x00000000, $offset);
$central .= pack( "VvvvvVVVVvvvvvVV", 0x02014b50, 0x0014, $this->options['method'] == 0 ? 0x0000 : 0x000A, 0x0000, (isset( $current['method'] ) || $this->options['method'] == 0) ? 0x0000 : 0x0008, $timedate, $crc32, $size, $current['stat'][7], strlen( $current['name2'] ), 0x0000, 0x0000, 0x0000, 0x0000, 0x00000000, $offset );
$central .= $current['name2'];
$files ++;
$offset += (30 + strlen( $current['name2'] ) + $size);
}
else
} else
$this->error[] = "Could not open file {$current['name']} for reading. It was not added.";
}
$this->add_data( $central );
$this->add_data(pack("VvvvvVVv", 0x06054b50, 0x0000, 0x0000, $files, $files, strlen($central), $offset,
!empty ($this->options['comment']) ? strlen($this->options['comment']) : 0x0000));
$this->add_data( pack( "VvvvvVVv", 0x06054b50, 0x0000, 0x0000, $files, $files, strlen( $central ), $offset, ! empty( $this->options['comment'] ) ? strlen( $this->options['comment'] ) : 0x0000 ) );
if (! empty( $this->options['comment'] ))
$this->add_data( $this->options['comment'] );
chdir( $pwd );
return 1;
}
}
?>