<?php
/*
Flocke's "Archive on the Fly"
Set of PHP classes to create archives on the fly plus a class to extract
the contents of ZIP files. See the included file README.txt for information
on how to use it.
This file: vs.arc.php (abstract base class)
Version 1.2c - always find the latest version at
http://flocke.vssd.de/prog/code/php/aotf/
Copyright (C) 2005, 2006 Volker Siebert <flocke@vssd.de>
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
define('VSARC_FTYPE_FILE', 0);
define('VSARC_FTYPE_DIR', 1);
define('VSARC_FTYPE_SYMLINK', 2);
define('VSARC_SYMLINK_AS_FILE', 0);
define('VSARC_SYMLINK_OMIT', 1);
define('VSARC_SYMLINK_AS_LINK', 2);
$__vsArc_error_string__ = '';
function __vsArc_ignore_errors__($errno, $errstr, $errfile, $errline)
{
global $__vsArc_error_string__;
$__vsArc_error_string__ .= $errfile . ":" . $errline . ": " . $errstr;
}
class vsArcFile {
var $data; // Accumulated archive data
var $offset; // Total size of $this->data
var $count; // Number of files
var $files; // Array of all files
var $level; // Compression level (if supported)
var $mime; // Internet mime type
var $ext; // Default file extension
var $symlinks; // How to handle symbolic links
var $errors;
// Constructor
function vsArcFile($level = 6)
{
$this->clear();
$this->level = $level;
}
// Reset to create a new archive
function _clear($m, $x)
{
$this->data = '';
$this->offset = 0;
$this->count = 0;
$this->files = array();
$this->mime = $m;
$this->ext = $x;
$this->symlinks = VSARC_SYMLINK_AS_FILE;
$this->errors = '';
}
// Reset to create a new archive
function clear()
{
$this->_clear('text/plain', '.txt');
}
// Fixes a given filename
// 1. Backslashes are replaced by slashes
// 2. Multiple consecutive slashes are replaced by one
// 3. Prefixes "/", "./" and "../" are removed
// 4. Trailing slashes are removed
function _fix_name($name)
{
$name = str_replace('\\', '/', $name);
$name = ereg_replace('//+', '/', $name);
while (eregi("^\.*/(.*)$", $name, $regs))
$name = $regs[1];
while (substr($name, -1, 1) == '/')
$name = substr($name, 0, -1);
return $name;
}
// Fix date & time
function _fix_date($date)
{
if (empty($date))
$date = time();
elseif (is_string($date))
$date = @strtotime($date);
return $date;
}
// Fix compression level
function _fix_level()
{
$level = intval($this->level);
if ($level < 0)
$level = 0;
elseif ($level > 9)
$level = 9;
return $level;
}
// This function may be overridden in your own class to really do
// something, e.g. to flush the data with "echo $arc->preflush()"
function added_one_entry($ftype)
{
return;
}
// Add new data and adjust the offset
function _add_data(&$data)
{
$this->data .= $data;
$this->offset += strlen($data);
}
// Adds a new entry to the file
// NAME is the stored file name
// DATE is the modification date/time
// FTYPE is one of the VSARC_FTYPE_ constants
// DATA is the uncompressed data for files and empty otherwise
// LINKTARGET is the target of the symbolic link
// This function will be overridden in a real archiver class.
function _add_entry($name, $date, $ftype, &$data, $linktarget = '')
{
if ($ftype == VSARC_FTYPE_DIR)
$str = date("r", $date) . " <DIR> " . $name;
elseif ($ftype == VSARC_FTYPE_SYMLINK)
$str = date("r", $date) . " <LINK> " . $name . " -> " .
$linktarget;
else
$str = date("r", $date) . sprintf(" %10d ", strlen($data)) . $name;
$this->_add_data($str . "\n");
}
// Adds a directory to the archive.
// Note: calling this function should not be necessary
function add_dir($name, $date = 0)
{
$name = $this->_fix_name($name);
$date = $this->_fix_date($date);
if ($name == '' || $name == '.' || isset($this->files[$name . '/']))
return;
$this->files[$name . '/'] = 2;
if (eregi("^(.+)/[^/]+$", $name, $regs))
$this->add_dir($regs[1], $date);
$data = '';
$this->_add_entry($name . '/', $date, VSARC_FTYPE_DIR, $data);
$this->count += 1;
$this->added_one_entry(VSARC_FTYPE_DIR);
}
// Adds a file with given data to the archive
function add_link($name, $target, $date = 0)
{
$name = $this->_fix_name($name);
$date = $this->_fix_date($date);
if ($name == '' || isset($this->files[$name]))
return;
$this->files[$name] = 1;
if (eregi("^(.*)/[^/]+$", $name, $regs))
$this->add_dir($regs[1]);
$this->_add_entry($name, $date, VSARC_FTYPE_SYMLINK, '', $target);
$this->count += 1;
$this->added_one_entry(VSARC_FTYPE_SYMLINK);
}
// Adds a file with given data to the archive
function add_data($name, &$data, $date = 0)
{
$name = $this->_fix_name($name);
$date = $this->_fix_date($date);
if ($name == '' || isset($this->files[$name]))
return;
$this->files[$name] = 1;
if (eregi("^(.*)/[^/]+$", $name, $regs))
$this->add_dir($regs[1]);
$this->_add_entry($name, $date, VSARC_FTYPE_FILE, $data);
$this->count += 1;
$this->added_one_entry(VSARC_FTYPE_FILE);
}
// Sets our internal error handler
function __seh__()
{
set_error_handler("__vsArc_ignore_errors__");
}
// Restore the old error handler and remembers any
// error text in $this->errors
function __reh__()
{
global $__vsArc_error_string__;
restore_error_handler();
$this->errors .= $__vsArc_error_string__;
$__vsArc_error_string__ = '';
}
// Adds a file from disk to the archive
// 1. If FILENAME is an array, all its entries are processed via
// this function, where the archive filename is constructed
// by prefixing the basename with NAME.
// 2. If FILENAME is a directory, all the files and subdirectories
// are added to the archive, prefixing their names by NAME.
// 3. Otherwise the file FILENAME is added to the archive, using
// NAME as its name.
function __add_file($name, $filename)
{
$name = $this->_fix_name($name);
if (is_array($filename))
{
if ($name != '')
$name .= '/';
reset($filename);
while (list(, $file) = each($filename))
$this->__add_file($name . basename($file), $file);
}
elseif (@is_dir($filename))
{
if ($name != '')
$name .= '/';
$files = array();
if (($handle = opendir($filename)) != 0)
{
while (($file = readdir($handle)) !== false)
if ($file != '.' && $file != '..')
$files[] = $file;
closedir($handle);
$filename = str_replace('\\', '/', $filename);
if (substr($filename, -1, 1) != '/')
$filename .= '/';
reset($files);
while (list(, $file) = each($files))
$this->__add_file($name . $file, $filename . $file);
}
}
elseif ($this->symlinks && @is_link($name))
{
if ($this->symlinks == VSARC_SYMLINK_OMIT)
return;
if (isset($this->files[$name]))
return;
$this->add_link($name, readlink($filename), filemtime($filename));
}
else
{
if ($name == '')
$name = basename($filename);
if (isset($this->files[$name]))
return;
if (eregi("^(ftp|http)s?:", $filename))
$mtime = 0;
else
$mtime = @filemtime($filename);
if (function_exists('file_get_contents'))
$this->add_data($name, file_get_contents($filename), $mtime);
else
{
$handle = fopen($filename, "rb");
if ($handle !== false)
{
$data = fread($handle, 1024 * 1024 * 1024);
fclose($handle);
$this->add_data($name, $data, $mtime);
}
}
}
}
function add_file($name, $filename)
{
$this->__seh__();
$this->__add_file($name, $filename);
$this->__reh__();
}
// Returns the archive data that has been collected so far
// and can already be written
function preflush($minsize = 65536, $maxsize = 0)
{
$dlen = strlen($this->data);
if ($dlen < $minsize)
$res = '';
elseif ($maxsize <= 0 || $dlen <= $maxsize)
{
$res = $this->data;
$this->data = '';
}
else
{
$res = substr($this->data, 0, $maxsize);
$this->data = substr($this->data, $maxsize);
}
return $res;
}
// Returns the final archive data that still needs to be written
function finish()
{
$res = $this->data;
$this->clear();
return $res;
}
// MIME type for transfer
function mimetype()
{
return $this->mime;
}
// extension for suggested filename
function fileext()
{
return $this->ext;
}
// Returns the default internet headers
function get_headers($filename = '', $disposition = '')
{
$result = array(
'X-Compressed-By' => 'flocke.vssd.de archive-on-the-fly/php 1.2c',
'Content-Type' => $this->mimetype()
);
if (!empty($filename))
{
if ($disposition != 'attachment' && $disposition != 'inline')
$disposition = 'attachment';
$result['Content-Disposition'] = $disposition .
'; filename="' . $filename . $this->fileext() . '"';
}
elseif ($disposition != '')
$result['Content-Disposition'] = $disposition;
return $result;
}
// Send the default internet headers
function headers($filename = '', $disposition = '')
{
foreach ($this->get_headers($filename, $disposition) as $key => $value)
header("$key: $value");
}
}
?> |