Blame Extras/phpBB/3.0.4/includes/functions_compress.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package phpBB3
4c79b5
* @version $Id: functions_compress.php 8780 2008-08-22 12:52:48Z acydburn $
4c79b5
* @copyright (c) 2005 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Class for handling archives (compression/decompression)
4c79b5
* @package phpBB3
4c79b5
*/
4c79b5
class compress
4c79b5
{
4c79b5
	var $fp = 0;
4c79b5
4c79b5
	/**
4c79b5
	* Add file to archive
4c79b5
	*/
4c79b5
	function add_file($src, $src_rm_prefix = '', $src_add_prefix = '', $skip_files = '')
4c79b5
	{
4c79b5
		global $phpbb_root_path;
4c79b5
4c79b5
		$skip_files = explode(',', $skip_files);
4c79b5
4c79b5
		// Remove rm prefix from src path
4c79b5
		$src_path = ($src_rm_prefix) ? preg_replace('#^(' . preg_quote($src_rm_prefix, '#') . ')#', '', $src) : $src;
4c79b5
		// Add src prefix
4c79b5
		$src_path = ($src_add_prefix) ? ($src_add_prefix . ((substr($src_add_prefix, -1) != '/') ? '/' : '') . $src_path) : $src_path;
4c79b5
		// Remove initial "/" if present
4c79b5
		$src_path = (substr($src_path, 0, 1) == '/') ? substr($src_path, 1) : $src_path;
4c79b5
4c79b5
		if (is_file($phpbb_root_path . $src))
4c79b5
		{
4c79b5
			$this->data($src_path, file_get_contents("$phpbb_root_path$src"), false, stat("$phpbb_root_path$src"));
4c79b5
		}
4c79b5
		else if (is_dir($phpbb_root_path . $src))
4c79b5
		{
4c79b5
			// Clean up path, add closing / if not present
4c79b5
			$src_path = ($src_path && substr($src_path, -1) != '/') ? $src_path . '/' : $src_path;
4c79b5
4c79b5
			$filelist = array();
4c79b5
			$filelist = filelist("$phpbb_root_path$src", '', '*');
4c79b5
			krsort($filelist);
4c79b5
4c79b5
			if ($src_path)
4c79b5
			{
4c79b5
				$this->data($src_path, '', true, stat("$phpbb_root_path$src"));
4c79b5
			}
4c79b5
4c79b5
			foreach ($filelist as $path => $file_ary)
4c79b5
			{
4c79b5
				if ($path)
4c79b5
				{
4c79b5
					// Same as for src_path
4c79b5
					$path = (substr($path, 0, 1) == '/') ? substr($path, 1) : $path;
4c79b5
					$path = ($path && substr($path, -1) != '/') ? $path . '/' : $path;
4c79b5
4c79b5
					$this->data("$src_path$path", '', true, stat("$phpbb_root_path$src$path"));
4c79b5
				}
4c79b5
4c79b5
				foreach ($file_ary as $file)
4c79b5
				{
4c79b5
					if (in_array($path . $file, $skip_files))
4c79b5
					{
4c79b5
						continue;
4c79b5
					}
4c79b5
4c79b5
					$this->data("$src_path$path$file", file_get_contents("$phpbb_root_path$src$path$file"), false, stat("$phpbb_root_path$src$path$file"));
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Add custom file (the filepath will not be adjusted)
4c79b5
	*/
4c79b5
	function add_custom_file($src, $filename)
4c79b5
	{
4c79b5
		$this->data($filename, file_get_contents($src), false, stat($src));
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Add file data
4c79b5
	*/
4c79b5
	function add_data($src, $name)
4c79b5
	{
4c79b5
		$stat = array();
4c79b5
		$stat[2] = 436; //384
4c79b5
		$stat[4] = $stat[5] = 0;
4c79b5
		$stat[7] = strlen($src);
4c79b5
		$stat[9] = time();
4c79b5
		$this->data($name, $src, false, $stat);
4c79b5
		return true;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Return available methods
4c79b5
	*/
4c79b5
	function methods()
4c79b5
	{
4c79b5
		$methods = array('.tar');
4c79b5
		$available_methods = array('.tar.gz' => 'zlib', '.tar.bz2' => 'bz2', '.zip' => 'zlib');
4c79b5
4c79b5
		foreach ($available_methods as $type => $module)
4c79b5
		{
4c79b5
			if (!@extension_loaded($module))
4c79b5
			{
4c79b5
				continue;
4c79b5
			}
4c79b5
			$methods[] = $type;
4c79b5
		}
4c79b5
4c79b5
		return $methods;
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Zip creation class from phpMyAdmin 2.3.0 (c) Tobias Ratschiller, Olivier Müller, Loïc Chapeaux,
4c79b5
* Marc Delisle, http://www.phpmyadmin.net/
4c79b5
*
4c79b5
* Zip extraction function by Alexandre Tedeschi, alexandrebr at gmail dot com
4c79b5
*
4c79b5
* Modified extensively by psoTFX and DavidMJ, (c) phpBB Group, 2003
4c79b5
*
4c79b5
* Based on work by Eric Mueller and Denis125
4c79b5
* Official ZIP file format: http://www.pkware.com/appnote.txt
4c79b5
*
4c79b5
* @package phpBB3
4c79b5
*/
4c79b5
class compress_zip extends compress
4c79b5
{
4c79b5
	var $datasec = array();
4c79b5
	var $ctrl_dir = array();
4c79b5
	var $eof_cdh = "\x50\x4b\x05\x06\x00\x00\x00\x00";
4c79b5
4c79b5
	var $old_offset = 0;
4c79b5
	var $datasec_len = 0;
4c79b5
4c79b5
	/**
4c79b5
	* Constructor
4c79b5
	*/
4c79b5
	function compress_zip($mode, $file)
4c79b5
	{
4c79b5
		return $this->fp = @fopen($file, $mode . 'b');
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Convert unix to dos time
4c79b5
	*/
4c79b5
	function unix_to_dos_time($time)
4c79b5
	{
4c79b5
		$timearray = (!$time) ? getdate() : getdate($time);
4c79b5
4c79b5
		if ($timearray['year'] < 1980)
4c79b5
		{
4c79b5
			$timearray['year'] = 1980;
4c79b5
			$timearray['mon'] = $timearray['mday'] = 1;
4c79b5
			$timearray['hours'] = $timearray['minutes'] = $timearray['seconds'] = 0;
4c79b5
		}
4c79b5
4c79b5
		return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Extract archive
4c79b5
	*/
4c79b5
	function extract($dst)
4c79b5
	{
4c79b5
		// Loop the file, looking for files and folders
4c79b5
		$dd_try = false;
4c79b5
		rewind($this->fp);
4c79b5
4c79b5
		while (!feof($this->fp))
4c79b5
		{
4c79b5
			// Check if the signature is valid...
4c79b5
			$signature = fread($this->fp, 4);
4c79b5
4c79b5
			switch ($signature)
4c79b5
			{
4c79b5
				// 'Local File Header'
4c79b5
				case "\x50\x4b\x03\x04":
4c79b5
					// Lets get everything we need.
4c79b5
					// We don't store the version needed to extract, the general purpose bit flag or the date and time fields
4c79b5
					$data = unpack("@4/vc_method/@10/Vcrc/Vc_size/Vuc_size/vname_len/vextra_field", fread($this->fp, 26));
4c79b5
					$file_name = fread($this->fp, $data['name_len']); // filename
4c79b5
4c79b5
					if ($data['extra_field'])
4c79b5
					{
4c79b5
						fread($this->fp, $data['extra_field']); // extra field
4c79b5
					}
4c79b5
4c79b5
					$target_filename = "$dst$file_name";
4c79b5
4c79b5
					if (!$data['uc_size'] && !$data['crc'] && substr($file_name, -1, 1) == '/')
4c79b5
					{
4c79b5
						if (!is_dir($target_filename))
4c79b5
						{
4c79b5
							$str = '';
4c79b5
							$folders = explode('/', $target_filename);
4c79b5
4c79b5
							// Create and folders and subfolders if they do not exist
4c79b5
							foreach ($folders as $folder)
4c79b5
							{
4c79b5
								$folder = trim($folder);
4c79b5
								if (!$folder)
4c79b5
								{
4c79b5
									continue;
4c79b5
								}
4c79b5
4c79b5
								$str = (!empty($str)) ? $str . '/' . $folder : $folder;
4c79b5
								if (!is_dir($str))
4c79b5
								{
4c79b5
									if (!@mkdir($str, 0777))
4c79b5
									{
4c79b5
										trigger_error("Could not create directory $folder");
4c79b5
									}
4c79b5
									phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
4c79b5
								}
4c79b5
							}
4c79b5
						}
4c79b5
						// This is a directory, we are not writting files
4c79b5
						continue;
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						// Some archivers are punks, they don't include folders in their archives!
4c79b5
						$str = '';
4c79b5
						$folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
4c79b5
4c79b5
						// Create and folders and subfolders if they do not exist
4c79b5
						foreach ($folders as $folder)
4c79b5
						{
4c79b5
							$folder = trim($folder);
4c79b5
							if (!$folder)
4c79b5
							{
4c79b5
								continue;
4c79b5
							}
4c79b5
4c79b5
							$str = (!empty($str)) ? $str . '/' . $folder : $folder;
4c79b5
							if (!is_dir($str))
4c79b5
							{
4c79b5
								if (!@mkdir($str, 0777))
4c79b5
								{
4c79b5
									trigger_error("Could not create directory $folder");
4c79b5
								}
4c79b5
								phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
4c79b5
							}
4c79b5
						}
4c79b5
					}
4c79b5
4c79b5
					if (!$data['uc_size'])
4c79b5
					{
4c79b5
						$content = '';
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						$content = fread($this->fp, $data['c_size']);
4c79b5
					}
4c79b5
4c79b5
					$fp = fopen($target_filename, "w");
4c79b5
4c79b5
					switch ($data['c_method'])
4c79b5
					{
4c79b5
						case 0:
4c79b5
							// Not compressed
4c79b5
							fwrite($fp, $content);
4c79b5
						break;
4c79b5
4c79b5
						case 8:
4c79b5
							// Deflate
4c79b5
							fwrite($fp, gzinflate($content, $data['uc_size']));
4c79b5
						break;
4c79b5
4c79b5
						case 12:
4c79b5
							// Bzip2
4c79b5
							fwrite($fp, bzdecompress($content));
4c79b5
						break;
4c79b5
					}
4c79b5
4c79b5
					fclose($fp);
4c79b5
				break;
4c79b5
4c79b5
				// We hit the 'Central Directory Header', we can stop because nothing else in here requires our attention
4c79b5
				// or we hit the end of the central directory record, we can safely end the loop as we are totally finished with looking for files and folders
4c79b5
				case "\x50\x4b\x01\x02":
4c79b5
				// This case should simply never happen.. but it does exist..
4c79b5
				case "\x50\x4b\x05\x06":
4c79b5
				break 2;
4c79b5
4c79b5
				// 'Packed to Removable Disk', ignore it and look for the next signature...
4c79b5
				case 'PK00':
4c79b5
				continue 2;
4c79b5
4c79b5
				// We have encountered a header that is weird. Lets look for better data...
4c79b5
				default:
4c79b5
					if (!$dd_try)
4c79b5
					{
4c79b5
						// Unexpected header. Trying to detect wrong placed 'Data Descriptor';
4c79b5
						$dd_try = true;
4c79b5
						fseek($this->fp, 8, SEEK_CUR); // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
4c79b5
						continue 2;
4c79b5
					}
4c79b5
					trigger_error("Unexpected header, ending loop");
4c79b5
				break 2;
4c79b5
			}
4c79b5
4c79b5
			$dd_try = false;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Close archive
4c79b5
	*/
4c79b5
	function close()
4c79b5
	{
4c79b5
		// Write out central file directory and footer ... if it exists
4c79b5
		if (sizeof($this->ctrl_dir))
4c79b5
		{
4c79b5
			fwrite($this->fp, $this->file());
4c79b5
		}
4c79b5
		fclose($this->fp);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Create the structures ... note we assume version made by is MSDOS
4c79b5
	*/
4c79b5
	function data($name, $data, $is_dir = false, $stat)
4c79b5
	{
4c79b5
		$name = str_replace('\\', '/', $name);
4c79b5
4c79b5
		$hexdtime = pack('V', $this->unix_to_dos_time($stat[9]));
4c79b5
4c79b5
		if ($is_dir)
4c79b5
		{
4c79b5
			$unc_len = $c_len = $crc = 0;
4c79b5
			$zdata = '';
4c79b5
			$var_ext = 10;
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$unc_len = strlen($data);
4c79b5
			$crc = crc32($data);
4c79b5
			$zdata = gzdeflate($data);
4c79b5
			$c_len = strlen($zdata);
4c79b5
			$var_ext = 20;
4c79b5
4c79b5
			// Did we compress? No, then use data as is
4c79b5
			if ($c_len >= $unc_len)
4c79b5
			{
4c79b5
				$zdata = $data;
4c79b5
				$c_len = $unc_len;
4c79b5
				$var_ext = 10;
4c79b5
			}
4c79b5
		}
4c79b5
		unset($data);
4c79b5
4c79b5
		// If we didn't compress set method to store, else deflate
4c79b5
		$c_method = ($c_len == $unc_len) ? "\x00\x00" : "\x08\x00";
4c79b5
4c79b5
		// Are we a file or a directory? Set archive for file
4c79b5
		$attrib = ($is_dir) ? 16 : 32;
4c79b5
4c79b5
		// File Record Header
4c79b5
		$fr = "\x50\x4b\x03\x04";		// Local file header 4bytes
4c79b5
		$fr .= pack('v', $var_ext);		// ver needed to extract 2bytes
4c79b5
		$fr .= "\x00\x00";				// gen purpose bit flag 2bytes
4c79b5
		$fr .= $c_method;				// compression method 2bytes
4c79b5
		$fr .= $hexdtime;				// last mod time and date 2+2bytes
4c79b5
		$fr .= pack('V', $crc);			// crc32 4bytes
4c79b5
		$fr .= pack('V', $c_len);		// compressed filesize 4bytes
4c79b5
		$fr .= pack('V', $unc_len);		// uncompressed filesize 4bytes
4c79b5
		$fr .= pack('v', strlen($name));// length of filename 2bytes
4c79b5
4c79b5
		$fr .= pack('v', 0);			// extra field length 2bytes
4c79b5
		$fr .= $name;
4c79b5
		$fr .= $zdata;
4c79b5
		unset($zdata);
4c79b5
4c79b5
		$this->datasec_len += strlen($fr);
4c79b5
4c79b5
		// Add data to file ... by writing data out incrementally we save some memory
4c79b5
		fwrite($this->fp, $fr);
4c79b5
		unset($fr);
4c79b5
4c79b5
		// Central Directory Header
4c79b5
		$cdrec = "\x50\x4b\x01\x02";		// header 4bytes
4c79b5
		$cdrec .= "\x00\x00";				// version made by
4c79b5
		$cdrec .= pack('v', $var_ext);		// version needed to extract
4c79b5
		$cdrec .= "\x00\x00";				// gen purpose bit flag
4c79b5
		$cdrec .= $c_method;				// compression method
4c79b5
		$cdrec .= $hexdtime;				// last mod time & date
4c79b5
		$cdrec .= pack('V', $crc);			// crc32
4c79b5
		$cdrec .= pack('V', $c_len);		// compressed filesize
4c79b5
		$cdrec .= pack('V', $unc_len);		// uncompressed filesize
4c79b5
		$cdrec .= pack('v', strlen($name));	// length of filename
4c79b5
		$cdrec .= pack('v', 0);				// extra field length
4c79b5
		$cdrec .= pack('v', 0);				// file comment length
4c79b5
		$cdrec .= pack('v', 0);				// disk number start
4c79b5
		$cdrec .= pack('v', 0);				// internal file attributes
4c79b5
		$cdrec .= pack('V', $attrib);		// external file attributes
4c79b5
		$cdrec .= pack('V', $this->old_offset);	// relative offset of local header
4c79b5
		$cdrec .= $name;
4c79b5
4c79b5
		// Save to central directory
4c79b5
		$this->ctrl_dir[] = $cdrec;
4c79b5
4c79b5
		$this->old_offset = $this->datasec_len;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* file
4c79b5
	*/
4c79b5
	function file()
4c79b5
	{
4c79b5
		$ctrldir = implode('', $this->ctrl_dir);
4c79b5
4c79b5
		return $ctrldir . $this->eof_cdh .
4c79b5
			pack('v', sizeof($this->ctrl_dir)) .	// total # of entries "on this disk"
4c79b5
			pack('v', sizeof($this->ctrl_dir)) .	// total # of entries overall
4c79b5
			pack('V', strlen($ctrldir)) .			// size of central dir
4c79b5
			pack('V', $this->datasec_len) .			// offset to start of central dir
4c79b5
			"\x00\x00";								// .zip file comment length
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Download archive
4c79b5
	*/
4c79b5
	function download($filename, $download_name = false)
4c79b5
	{
4c79b5
		global $phpbb_root_path;
4c79b5
4c79b5
		if ($download_name === false)
4c79b5
		{
4c79b5
			$download_name = $filename;
4c79b5
		}
4c79b5
4c79b5
		$mimetype = 'application/zip';
4c79b5
4c79b5
		header('Pragma: no-cache');
4c79b5
		header("Content-Type: $mimetype; name=\"$download_name.zip\"");
4c79b5
		header("Content-disposition: attachment; filename=$download_name.zip");
4c79b5
4c79b5
		$fp = @fopen("{$phpbb_root_path}store/$filename.zip", 'rb');
4c79b5
		if ($fp)
4c79b5
		{
4c79b5
			while ($buffer = fread($fp, 1024))
4c79b5
			{
4c79b5
				echo $buffer;
4c79b5
			}
4c79b5
			fclose($fp);
4c79b5
		}
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Tar/tar.gz compression routine
4c79b5
* Header/checksum creation derived from tarfile.pl, (c) Tom Horsley, 1994
4c79b5
*
4c79b5
* @package phpBB3
4c79b5
*/
4c79b5
class compress_tar extends compress
4c79b5
{
4c79b5
	var $isgz = false;
4c79b5
	var $isbz = false;
4c79b5
	var $filename = '';
4c79b5
	var $mode = '';
4c79b5
	var $type = '';
4c79b5
	var $wrote = false;
4c79b5
4c79b5
	/**
4c79b5
	* Constructor
4c79b5
	*/
4c79b5
	function compress_tar($mode, $file, $type = '')
4c79b5
	{
4c79b5
		$type = (!$type) ? $file : $type;
4c79b5
		$this->isgz = (strpos($type, '.tar.gz') !== false || strpos($type, '.tgz') !== false) ? true : false;
4c79b5
		$this->isbz = (strpos($type, '.tar.bz2') !== false) ? true : false;
4c79b5
4c79b5
		$this->mode = &$mode;
4c79b5
		$this->file = &$file;
4c79b5
		$this->type = &$type;
4c79b5
		$this->open();
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Extract archive
4c79b5
	*/
4c79b5
	function extract($dst)
4c79b5
	{
4c79b5
		$fzread = ($this->isbz && function_exists('bzread')) ? 'bzread' : (($this->isgz && @extension_loaded('zlib')) ? 'gzread' : 'fread');
4c79b5
4c79b5
		// Run through the file and grab directory entries
4c79b5
		while ($buffer = $fzread($this->fp, 512))
4c79b5
		{
4c79b5
			$tmp = unpack('A6magic', substr($buffer, 257, 6));
4c79b5
4c79b5
			if (trim($tmp['magic']) == 'ustar')
4c79b5
			{
4c79b5
				$tmp = unpack('A100name', $buffer);
4c79b5
				$filename = trim($tmp['name']);
4c79b5
4c79b5
				$tmp = unpack('Atype', substr($buffer, 156, 1));
4c79b5
				$filetype = (int) trim($tmp['type']);
4c79b5
4c79b5
				$tmp = unpack('A12size', substr($buffer, 124, 12));
4c79b5
				$filesize = octdec((int) trim($tmp['size']));
4c79b5
4c79b5
				$target_filename = "$dst$filename";
4c79b5
4c79b5
				if ($filetype == 5)
4c79b5
				{
4c79b5
					if (!is_dir($target_filename))
4c79b5
					{
4c79b5
						$str = '';
4c79b5
						$folders = explode('/', $target_filename);
4c79b5
4c79b5
						// Create and folders and subfolders if they do not exist
4c79b5
						foreach ($folders as $folder)
4c79b5
						{
4c79b5
							$folder = trim($folder);
4c79b5
							if (!$folder)
4c79b5
							{
4c79b5
								continue;
4c79b5
							}
4c79b5
4c79b5
							$str = (!empty($str)) ? $str . '/' . $folder : $folder;
4c79b5
							if (!is_dir($str))
4c79b5
							{
4c79b5
								if (!@mkdir($str, 0777))
4c79b5
								{
4c79b5
									trigger_error("Could not create directory $folder");
4c79b5
								}
4c79b5
								phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
4c79b5
							}
4c79b5
						}
4c79b5
					}
4c79b5
				}
4c79b5
				else if ($filesize >= 0 && ($filetype == 0 || $filetype == "\0"))
4c79b5
				{
4c79b5
					// Some archivers are punks, they don't properly order the folders in their archives!
4c79b5
					$str = '';
4c79b5
					$folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
4c79b5
4c79b5
					// Create and folders and subfolders if they do not exist
4c79b5
					foreach ($folders as $folder)
4c79b5
					{
4c79b5
						$folder = trim($folder);
4c79b5
						if (!$folder)
4c79b5
						{
4c79b5
							continue;
4c79b5
						}
4c79b5
4c79b5
						$str = (!empty($str)) ? $str . '/' . $folder : $folder;
4c79b5
						if (!is_dir($str))
4c79b5
						{
4c79b5
							if (!@mkdir($str, 0777))
4c79b5
							{
4c79b5
								trigger_error("Could not create directory $folder");
4c79b5
							}
4c79b5
							phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
4c79b5
						}
4c79b5
					}
4c79b5
4c79b5
					// Write out the files
4c79b5
					if (!($fp = fopen($target_filename, 'wb')))
4c79b5
					{
4c79b5
						trigger_error("Couldn't create file $filename");
4c79b5
					}
4c79b5
					phpbb_chmod($target_filename, CHMOD_READ);
4c79b5
4c79b5
					// Grab the file contents
4c79b5
					fwrite($fp, ($filesize) ? $fzread($this->fp, ($filesize + 511) &~ 511) : '', $filesize);
4c79b5
					fclose($fp);
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Close archive
4c79b5
	*/
4c79b5
	function close()
4c79b5
	{
4c79b5
		$fzclose = ($this->isbz && function_exists('bzclose')) ? 'bzclose' : (($this->isgz && @extension_loaded('zlib')) ? 'gzclose' : 'fclose');
4c79b5
4c79b5
		if ($this->wrote)
4c79b5
		{
4c79b5
			$fzwrite = ($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
4c79b5
4c79b5
			// The end of a tar archive ends in two records of all NULLs (1024 bytes of \0)
4c79b5
			$fzwrite($this->fp, str_repeat("\0", 1024));
4c79b5
		}
4c79b5
4c79b5
		$fzclose($this->fp);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Create the structures
4c79b5
	*/
4c79b5
	function data($name, $data, $is_dir = false, $stat)
4c79b5
	{
4c79b5
		$this->wrote = true;
4c79b5
		$fzwrite = 	($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
4c79b5
4c79b5
		$typeflag = ($is_dir) ? '5' : '';
4c79b5
4c79b5
		// This is the header data, it contains all the info we know about the file or folder that we are about to archive
4c79b5
		$header = '';
4c79b5
		$header .= pack('a100', $name);						// file name
4c79b5
		$header .= pack('a8', sprintf("%07o", $stat[2]));	// file mode
4c79b5
		$header .= pack('a8', sprintf("%07o", $stat[4]));	// owner id
4c79b5
		$header .= pack('a8', sprintf("%07o", $stat[5]));	// group id
4c79b5
		$header .= pack('a12', sprintf("%011o", $stat[7]));	// file size
4c79b5
		$header .= pack('a12', sprintf("%011o", $stat[9]));	// last mod time
4c79b5
4c79b5
		// Checksum
4c79b5
		$checksum = 0;
4c79b5
		for ($i = 0; $i < 148; $i++)
4c79b5
		{
4c79b5
			$checksum += ord($header[$i]);
4c79b5
		}
4c79b5
4c79b5
		// We precompute the rest of the hash, this saves us time in the loop and allows us to insert our hash without resorting to string functions
4c79b5
		$checksum += 2415 + (($is_dir) ? 53 : 0);
4c79b5
4c79b5
		$header .= pack('a8', sprintf("%07o", $checksum));	// checksum
4c79b5
		$header .= pack('a1', $typeflag);					// link indicator
4c79b5
		$header .= pack('a100', '');						// name of linked file
4c79b5
		$header .= pack('a6', 'ustar');						// ustar indicator
4c79b5
		$header .= pack('a2', '00');						// ustar version
4c79b5
		$header .= pack('a32', 'Unknown');					// owner name
4c79b5
		$header .= pack('a32', 'Unknown');					// group name
4c79b5
		$header .= pack('a8', '');							// device major number
4c79b5
		$header .= pack('a8', '');							// device minor number
4c79b5
		$header .= pack('a155', '');						// filename prefix
4c79b5
		$header .= pack('a12', '');							// end
4c79b5
4c79b5
		// This writes the entire file in one shot. Header, followed by data and then null padded to a multiple of 512
4c79b5
		$fzwrite($this->fp, $header . (($stat[7] !== 0 && !$is_dir) ? $data . str_repeat("\0", (($stat[7] + 511) &~ 511) - $stat[7]) : ''));
4c79b5
		unset($data);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Open archive
4c79b5
	*/
4c79b5
	function open()
4c79b5
	{
4c79b5
		$fzopen = ($this->isbz && function_exists('bzopen')) ? 'bzopen' : (($this->isgz && @extension_loaded('zlib')) ? 'gzopen' : 'fopen');
4c79b5
		$this->fp = @$fzopen($this->file, $this->mode . (($fzopen == 'bzopen') ? '' : 'b') . (($fzopen == 'gzopen') ? '9' : ''));
4c79b5
4c79b5
		if (!$this->fp)
4c79b5
		{
4c79b5
			trigger_error('Unable to open file ' . $this->file . ' [' . $fzopen . ' - ' . $this->mode . 'b]');
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Download archive
4c79b5
	*/
4c79b5
	function download($filename, $download_name = false)
4c79b5
	{
4c79b5
		global $phpbb_root_path;
4c79b5
4c79b5
		if ($download_name === false)
4c79b5
		{
4c79b5
			$download_name = $filename;
4c79b5
		}
4c79b5
4c79b5
		switch ($this->type)
4c79b5
		{
4c79b5
			case '.tar':
4c79b5
				$mimetype = 'application/x-tar';
4c79b5
			break;
4c79b5
4c79b5
			case '.tar.gz':
4c79b5
				$mimetype = 'application/x-gzip';
4c79b5
			break;
4c79b5
4c79b5
			case '.tar.bz2':
4c79b5
				$mimetype = 'application/x-bzip2';
4c79b5
			break;
4c79b5
4c79b5
			default:
4c79b5
				$mimetype = 'application/octet-stream';
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		header('Pragma: no-cache');
4c79b5
		header("Content-Type: $mimetype; name=\"$download_name$this->type\"");
4c79b5
		header("Content-disposition: attachment; filename=$download_name$this->type");
4c79b5
4c79b5
		$fp = @fopen("{$phpbb_root_path}store/$filename$this->type", 'rb');
4c79b5
		if ($fp)
4c79b5
		{
4c79b5
			while ($buffer = fread($fp, 1024))
4c79b5
			{
4c79b5
				echo $buffer;
4c79b5
			}
4c79b5
			fclose($fp);
4c79b5
		}
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
?>