Blame Identity/Webenv/App/phpBB/3.0.4/includes/functions_compress.php

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