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

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