Files
luos/workflow/engine/classes/Archive.php

366 lines
13 KiB
PHP
Raw Normal View History

2017-08-11 11:10:27 -04:00
<?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
*--------------------------------------------------*/
/**
*
* @package workflow.engine.classes
*/
2017-08-11 12:33:30 -04:00
class Archive
2017-08-11 11:10:27 -04:00
{
/**
* This function is the constructor of the class archive
*
* @param string $name
* @return void
*
*/
2017-08-11 12:33:30 -04:00
public function archive($name)
2017-08-11 11:10:27 -04:00
{
2017-08-11 12:33:30 -04:00
$this->options = array('basedir' => ".", 'name' => $name, 'prepend' => "", 'inmemory' => 0, 'overwrite' => 0, 'recurse' => 1, 'storepaths' => 1, 'followlinks' => 0, 'level' => 3, 'method' => 1, 'sfx' => "", 'type' => "", 'comment' => ""
2017-08-11 11:10:27 -04:00
);
2017-08-11 12:33:30 -04:00
$this->files = array();
$this->exclude = array();
$this->storeonly = array();
$this->error = array();
2017-08-11 11:10:27 -04:00
}
/**
* This function gives options to a archive
*
* @param array $options
* @return void
*/
2017-08-11 12:33:30 -04:00
public function set_options($options)
2017-08-11 11:10:27 -04:00
{
foreach ($options as $key => $value) {
$this->options[$key] = $value;
}
2017-08-11 12:33:30 -04:00
if (!empty($this->options['basedir'])) {
$this->options['basedir'] = str_replace("\\", "/", $this->options['basedir']);
$this->options['basedir'] = preg_replace("/\/+/", "/", $this->options['basedir']);
$this->options['basedir'] = preg_replace("/\/$/", "", $this->options['basedir']);
2017-08-11 11:10:27 -04:00
}
2017-08-11 12:33:30 -04:00
if (!empty($this->options['name'])) {
$this->options['name'] = str_replace("\\", "/", $this->options['name']);
$this->options['name'] = preg_replace("/\/+/", "/", $this->options['name']);
2017-08-11 11:10:27 -04:00
}
2017-08-11 12:33:30 -04:00
if (!empty($this->options['prepend'])) {
$this->options['prepend'] = str_replace("\\", "/", $this->options['prepend']);
$this->options['prepend'] = preg_replace("/^(\.*\/+)+/", "", $this->options['prepend']);
$this->options['prepend'] = preg_replace("/\/+/", "/", $this->options['prepend']);
$this->options['prepend'] = preg_replace("/\/$/", "", $this->options['prepend']) . "/";
2017-08-11 11:10:27 -04:00
}
}
/**
* This function is used to create a archive.
*
* @return boolean
*/
2017-08-11 12:33:30 -04:00
public function create_archive()
2017-08-11 11:10:27 -04:00
{
$this->make_list();
if ($this->options['inmemory'] == 0) {
$pwd = getcwd();
2017-08-11 12:33:30 -04:00
chdir($this->options['basedir']);
if ($this->options['overwrite'] == 0 && file_exists($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : ""))) {
2017-08-11 11:10:27 -04:00
$this->error[] = "File {$this->options['name']} already exist.";
2017-08-11 12:33:30 -04:00
chdir($pwd);
2017-08-11 11:10:27 -04:00
return 0;
2017-08-11 12:33:30 -04:00
} elseif ($this->archive = @fopen($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip" ? ".tmp" : ""), "wb+")) {
chdir($pwd);
2017-08-11 11:10:27 -04:00
} else {
$this->error[] = "Could not open {$this->options['name']} for writing.";
2017-08-11 12:33:30 -04:00
chdir($pwd);
2017-08-11 11:10:27 -04:00
return 0;
}
} else {
$this->archive = "";
}
switch ($this->options['type']) {
case "zip":
2017-08-11 12:33:30 -04:00
if (!$this->create_zip()) {
2017-08-11 11:10:27 -04:00
$this->error[] = "Could not create zip file.";
return 0;
}
break;
case "bzip":
2017-08-11 12:33:30 -04:00
if (!$this->create_tar()) {
2017-08-11 11:10:27 -04:00
$this->error[] = "Could not create tar file.";
return 0;
}
2017-08-11 12:33:30 -04:00
if (!$this->create_bzip()) {
2017-08-11 11:10:27 -04:00
$this->error[] = "Could not create bzip2 file.";
return 0;
}
break;
case "gzip":
2017-08-11 12:33:30 -04:00
if (!$this->create_tar()) {
2017-08-11 11:10:27 -04:00
$this->error[] = "Could not create tar file.";
return 0;
}
2017-08-11 12:33:30 -04:00
if (!$this->create_gzip()) {
2017-08-11 11:10:27 -04:00
$this->error[] = "Could not create gzip file.";
return 0;
}
break;
case "tar":
2017-08-11 12:33:30 -04:00
if (!$this->create_tar()) {
2017-08-11 11:10:27 -04:00
$this->error[] = "Could not create tar file.";
return 0;
}
}
if ($this->options['inmemory'] == 0) {
2017-08-11 12:33:30 -04:00
fclose($this->archive);
2017-08-11 11:10:27 -04:00
if ($this->options['type'] == "gzip" || $this->options['type'] == "bzip") {
2017-08-11 12:33:30 -04:00
unlink($this->options['basedir'] . "/" . $this->options['name'] . ".tmp");
2017-08-11 11:10:27 -04:00
}
}
}
/**
* This function is used for add data to a archive
*
* @param string $data
* @return void
*/
2017-08-11 12:33:30 -04:00
public function add_data($data)
2017-08-11 11:10:27 -04:00
{
if ($this->options['inmemory'] == 0) {
2017-08-11 12:33:30 -04:00
fwrite($this->archive, $data);
2017-08-11 11:10:27 -04:00
} else {
$this->archive .= $data;
}
}
/**
* This function make a list
*
* @return void
*/
2017-08-11 12:33:30 -04:00
public function make_list()
2017-08-11 11:10:27 -04:00
{
2017-08-11 12:33:30 -04:00
if (!empty($this->exclude)) {
2017-08-11 11:10:27 -04:00
foreach ($this->files as $key => $value) {
foreach ($this->exclude as $current) {
if ($value['name'] == $current['name']) {
2017-08-11 12:33:30 -04:00
unset($this->files[$key]);
2017-08-11 11:10:27 -04:00
}
}
}
}
2017-08-11 12:33:30 -04:00
if (!empty($this->storeonly)) {
2017-08-11 11:10:27 -04:00
foreach ($this->files as $key => $value) {
foreach ($this->storeonly as $current) {
if ($value['name'] == $current['name']) {
$this->files[$key]['method'] = 0;
}
}
}
}
2017-08-11 12:33:30 -04:00
unset($this->exclude, $this->storeonly);
2017-08-11 11:10:27 -04:00
}
/**
* Add files a list
*
* @param array $list
* @return void
*/
2017-08-11 12:33:30 -04:00
public function add_files($list)
2017-08-11 11:10:27 -04:00
{
2017-08-11 12:33:30 -04:00
$temp = $this->list_files($list);
2017-08-11 11:10:27 -04:00
foreach ($temp as $current) {
$this->files[] = $current;
}
}
/**
* This function exclude files of a list
*
* @param array $list
* @return void
*/
2017-08-11 12:33:30 -04:00
public function exclude_files($list)
2017-08-11 11:10:27 -04:00
{
2017-08-11 12:33:30 -04:00
$temp = $this->list_files($list);
2017-08-11 11:10:27 -04:00
foreach ($temp as $current) {
$this->exclude[] = $current;
}
}
/**
* This function store files
*
* @param array $list
*/
2017-08-11 12:33:30 -04:00
public function store_files($list)
2017-08-11 11:10:27 -04:00
{
2017-08-11 12:33:30 -04:00
$temp = $this->list_files($list);
2017-08-11 11:10:27 -04:00
foreach ($temp as $current) {
$this->storeonly[] = $current;
}
}
/**
* List files gives a List
*
* @param array $list
* @return array
*/
2017-08-11 12:33:30 -04:00
public function list_files($list)
2017-08-11 11:10:27 -04:00
{
2017-08-11 12:33:30 -04:00
if (!is_array($list)) {
2017-08-11 11:10:27 -04:00
$temp = $list;
2017-08-11 12:33:30 -04:00
$list = array($temp
2017-08-11 11:10:27 -04:00
);
2017-08-11 12:33:30 -04:00
unset($temp);
2017-08-11 11:10:27 -04:00
}
2017-08-11 12:33:30 -04:00
$files = array();
2017-08-11 11:10:27 -04:00
$pwd = getcwd();
2017-08-11 12:33:30 -04:00
chdir($this->options['basedir']);
2017-08-11 11:10:27 -04:00
foreach ($list as $current) {
2017-08-11 12:33:30 -04:00
$current = str_replace("\\", "/", $current);
$current = preg_replace("/\/+/", "/", $current);
$current = preg_replace("/\/$/", "", $current);
if (strstr($current, "*")) {
$regex = preg_replace("/([\\\^\$\.\[\]\|\(\)\?\+\{\}\/])/", "\\\\\\1", $current);
$regex = str_replace("*", ".*", $regex);
$dir = strstr($current, "/") ? substr($current, 0, strrpos($current, "/")) : ".";
$temp = $this->parse_dir($dir);
2017-08-11 11:10:27 -04:00
foreach ($temp as $current2) {
2017-08-11 12:33:30 -04:00
if (preg_match("/^{$regex}$/i", $current2['name'])) {
2017-08-11 11:10:27 -04:00
$files[] = $current2;
}
}
2017-08-11 12:33:30 -04:00
unset($regex, $dir, $temp, $current);
} elseif (@is_dir($current)) {
$temp = $this->parse_dir($current);
2017-08-11 11:10:27 -04:00
foreach ($temp as $file) {
$files[] = $file;
}
2017-08-11 12:33:30 -04:00
unset($temp, $file);
} elseif (@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)
2017-08-11 11:10:27 -04:00
);
}
}
2017-08-11 12:33:30 -04:00
chdir($pwd);
unset($current, $pwd);
usort($files, array("archive", "sort_files"
));
2017-08-11 11:10:27 -04:00
return $files;
}
/**
* This function is for parse a directory name
*
* @param string $dirname
* @return array
*/
2017-08-11 12:33:30 -04:00
public function parse_dir($dirname)
2017-08-11 11:10:27 -04:00
{
2017-08-11 12:33:30 -04:00
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)
2017-08-11 11:10:27 -04:00
)
);
} else {
2017-08-11 12:33:30 -04:00
$files = array();
2017-08-11 11:10:27 -04:00
}
2017-08-11 12:33:30 -04:00
$dir = @opendir($dirname);
while ($file = @readdir($dir)) {
2017-08-11 11:10:27 -04:00
$fullname = $dirname . "/" . $file;
if ($file == "." || $file == "..") {
continue;
2017-08-11 12:33:30 -04:00
} elseif (@is_dir($fullname)) {
if (empty($this->options['recurse'])) {
2017-08-11 11:10:27 -04:00
continue;
}
2017-08-11 12:33:30 -04:00
$temp = $this->parse_dir($fullname);
2017-08-11 11:10:27 -04:00
foreach ($temp as $file2) {
$files[] = $file2;
}
2017-08-11 12:33:30 -04:00
} elseif (@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)
2017-08-11 11:10:27 -04:00
);
}
}
2017-08-11 12:33:30 -04:00
@closedir($dir);
2017-08-11 11:10:27 -04:00
return $files;
}
/**
* This function sort two files
*
* @param array $a
* @param array $b
* @return boolean
*/
2017-08-11 12:33:30 -04:00
public function sort_files($a, $b)
2017-08-11 11:10:27 -04:00
{
if ($a['type'] != $b['type']) {
if ($a['type'] == 5 || $b['type'] == 2) {
2017-08-11 12:33:30 -04:00
return -1;
2017-08-11 11:10:27 -04:00
} elseif ($a['type'] == 2 || $b['type'] == 5) {
return 1;
} elseif ($a['type'] == 5) {
2017-08-11 12:33:30 -04:00
return strcmp(strtolower($a['name']), strtolower($b['name']));
2017-08-11 11:10:27 -04:00
} elseif ($a['ext'] != $b['ext']) {
2017-08-11 12:33:30 -04:00
return strcmp($a['ext'], $b['ext']);
2017-08-11 11:10:27 -04:00
} elseif ($a['stat'][7] != $b['stat'][7]) {
2017-08-11 12:33:30 -04:00
return $a['stat'][7] > $b['stat'][7] ? -1 : 1;
2017-08-11 11:10:27 -04:00
} else {
2017-08-11 12:33:30 -04:00
return strcmp(strtolower($a['name']), strtolower($b['name']));
2017-08-11 11:10:27 -04:00
}
}
return 0;
}
/**
* This function download a file
*
* @return void
*/
2017-08-11 12:33:30 -04:00
public function download_file()
2017-08-11 11:10:27 -04:00
{
if ($this->options['inmemory'] == 0) {
$this->error[] = "Can only use download_file() if archive is in memory. Redirect to file otherwise, it is faster.";
return;
}
switch ($this->options['type']) {
case "zip":
2017-08-11 12:33:30 -04:00
header("Content-Type: application/zip");
2017-08-11 11:10:27 -04:00
break;
case "bzip":
2017-08-11 12:33:30 -04:00
header("Content-Type: application/x-bzip2");
2017-08-11 11:10:27 -04:00
break;
case "gzip":
2017-08-11 12:33:30 -04:00
header("Content-Type: application/x-gzip");
2017-08-11 11:10:27 -04:00
break;
case "tar":
2017-08-11 12:33:30 -04:00
header("Content-Type: application/x-tar");
2017-08-11 11:10:27 -04:00
}
$header = "Content-Disposition: attachment; filename=\"";
2017-08-11 12:33:30 -04:00
$header .= strstr($this->options['name'], "/") ? substr($this->options['name'], strrpos($this->options['name'], "/") + 1) : $this->options['name'];
2017-08-11 11:10:27 -04:00
$header .= "\"";
2017-08-11 12:33:30 -04:00
header($header);
header("Content-Length: " . strlen($this->archive));
header("Content-Transfer-Encoding: binary");
header("Cache-Control: no-cache, must-revalidate, max-age=60");
header("Expires: Sat, 01 Jan 2000 12:00:00 GMT");
print ($this->archive);
2017-08-11 11:10:27 -04:00
}
}