includes/functions_compress.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_compress.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,739 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* Class for handling archives (compression/decompression)
    1.24 +* @package phpBB3
    1.25 +*/
    1.26 +class compress
    1.27 +{
    1.28 +	var $fp = 0;
    1.29 +
    1.30 +	/**
    1.31 +	* Add file to archive
    1.32 +	*/
    1.33 +	function add_file($src, $src_rm_prefix = '', $src_add_prefix = '', $skip_files = '')
    1.34 +	{
    1.35 +		global $phpbb_root_path;
    1.36 +
    1.37 +		$skip_files = explode(',', $skip_files);
    1.38 +
    1.39 +		// Remove rm prefix from src path
    1.40 +		$src_path = ($src_rm_prefix) ? preg_replace('#^(' . preg_quote($src_rm_prefix, '#') . ')#', '', $src) : $src;
    1.41 +		// Add src prefix
    1.42 +		$src_path = ($src_add_prefix) ? ($src_add_prefix . ((substr($src_add_prefix, -1) != '/') ? '/' : '') . $src_path) : $src_path;
    1.43 +		// Remove initial "/" if present
    1.44 +		$src_path = (substr($src_path, 0, 1) == '/') ? substr($src_path, 1) : $src_path;
    1.45 +
    1.46 +		if (is_file($phpbb_root_path . $src))
    1.47 +		{
    1.48 +			$this->data($src_path, file_get_contents("$phpbb_root_path$src"), false, stat("$phpbb_root_path$src"));
    1.49 +		}
    1.50 +		else if (is_dir($phpbb_root_path . $src))
    1.51 +		{
    1.52 +			// Clean up path, add closing / if not present
    1.53 +			$src_path = ($src_path && substr($src_path, -1) != '/') ? $src_path . '/' : $src_path;
    1.54 +
    1.55 +			$filelist = array();
    1.56 +			$filelist = filelist("$phpbb_root_path$src", '', '*');
    1.57 +			krsort($filelist);
    1.58 +
    1.59 +			/**
    1.60 +			* Commented out, as adding the folders produces corrupted archives
    1.61 +			if ($src_path)
    1.62 +			{
    1.63 +				$this->data($src_path, '', true, stat("$phpbb_root_path$src"));
    1.64 +			}
    1.65 +			*/
    1.66 +
    1.67 +			foreach ($filelist as $path => $file_ary)
    1.68 +			{
    1.69 +				/**
    1.70 +				* Commented out, as adding the folders produces corrupted archives
    1.71 +				if ($path)
    1.72 +				{
    1.73 +					// Same as for src_path
    1.74 +					$path = (substr($path, 0, 1) == '/') ? substr($path, 1) : $path;
    1.75 +					$path = ($path && substr($path, -1) != '/') ? $path . '/' : $path;
    1.76 +
    1.77 +					$this->data("$src_path$path", '', true, stat("$phpbb_root_path$src$path"));
    1.78 +				}
    1.79 +				*/
    1.80 +
    1.81 +				foreach ($file_ary as $file)
    1.82 +				{
    1.83 +					if (in_array($path . $file, $skip_files))
    1.84 +					{
    1.85 +						continue;
    1.86 +					}
    1.87 +
    1.88 +					$this->data("$src_path$path$file", file_get_contents("$phpbb_root_path$src$path$file"), false, stat("$phpbb_root_path$src$path$file"));
    1.89 +				}
    1.90 +			}
    1.91 +		}
    1.92 +		else
    1.93 +		{
    1.94 +			// $src does not exist
    1.95 +			return false;
    1.96 +		}
    1.97 +
    1.98 +		return true;
    1.99 +	}
   1.100 +
   1.101 +	/**
   1.102 +	* Add custom file (the filepath will not be adjusted)
   1.103 +	*/
   1.104 +	function add_custom_file($src, $filename)
   1.105 +	{
   1.106 +		if (!file_exists($src))
   1.107 +		{
   1.108 +			return false;
   1.109 +		}
   1.110 +
   1.111 +		$this->data($filename, file_get_contents($src), false, stat($src));
   1.112 +		return true;
   1.113 +	}
   1.114 +
   1.115 +	/**
   1.116 +	* Add file data
   1.117 +	*/
   1.118 +	function add_data($src, $name)
   1.119 +	{
   1.120 +		$stat = array();
   1.121 +		$stat[2] = 436; //384
   1.122 +		$stat[4] = $stat[5] = 0;
   1.123 +		$stat[7] = strlen($src);
   1.124 +		$stat[9] = time();
   1.125 +		$this->data($name, $src, false, $stat);
   1.126 +		return true;
   1.127 +	}
   1.128 +
   1.129 +	/**
   1.130 +	* Return available methods
   1.131 +	*/
   1.132 +	function methods()
   1.133 +	{
   1.134 +		$methods = array('.tar');
   1.135 +		$available_methods = array('.tar.gz' => 'zlib', '.tar.bz2' => 'bz2', '.zip' => 'zlib');
   1.136 +
   1.137 +		foreach ($available_methods as $type => $module)
   1.138 +		{
   1.139 +			if (!@extension_loaded($module))
   1.140 +			{
   1.141 +				continue;
   1.142 +			}
   1.143 +			$methods[] = $type;
   1.144 +		}
   1.145 +
   1.146 +		return $methods;
   1.147 +	}
   1.148 +}
   1.149 +
   1.150 +/**
   1.151 +* Zip creation class from phpMyAdmin 2.3.0 (c) Tobias Ratschiller, Olivier Müller, Loïc Chapeaux,
   1.152 +* Marc Delisle, http://www.phpmyadmin.net/
   1.153 +*
   1.154 +* Zip extraction function by Alexandre Tedeschi, alexandrebr at gmail dot com
   1.155 +*
   1.156 +* Modified extensively by psoTFX and DavidMJ, (c) phpBB Group, 2003
   1.157 +*
   1.158 +* Based on work by Eric Mueller and Denis125
   1.159 +* Official ZIP file format: http://www.pkware.com/appnote.txt
   1.160 +*
   1.161 +* @package phpBB3
   1.162 +*/
   1.163 +class compress_zip extends compress
   1.164 +{
   1.165 +	var $datasec = array();
   1.166 +	var $ctrl_dir = array();
   1.167 +	var $eof_cdh = "\x50\x4b\x05\x06\x00\x00\x00\x00";
   1.168 +
   1.169 +	var $old_offset = 0;
   1.170 +	var $datasec_len = 0;
   1.171 +
   1.172 +	/**
   1.173 +	* Constructor
   1.174 +	*/
   1.175 +	function compress_zip($mode, $file)
   1.176 +	{
   1.177 +		$this->fp = @fopen($file, $mode . 'b');
   1.178 +
   1.179 +		if (!$this->fp)
   1.180 +		{
   1.181 +			trigger_error('Unable to open file ' . $file . ' [' . $mode . 'b]');
   1.182 +		}
   1.183 +	}
   1.184 +
   1.185 +	/**
   1.186 +	* Convert unix to dos time
   1.187 +	*/
   1.188 +	function unix_to_dos_time($time)
   1.189 +	{
   1.190 +		$timearray = (!$time) ? getdate() : getdate($time);
   1.191 +
   1.192 +		if ($timearray['year'] < 1980)
   1.193 +		{
   1.194 +			$timearray['year'] = 1980;
   1.195 +			$timearray['mon'] = $timearray['mday'] = 1;
   1.196 +			$timearray['hours'] = $timearray['minutes'] = $timearray['seconds'] = 0;
   1.197 +		}
   1.198 +
   1.199 +		return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
   1.200 +	}
   1.201 +
   1.202 +	/**
   1.203 +	* Extract archive
   1.204 +	*/
   1.205 +	function extract($dst)
   1.206 +	{
   1.207 +		// Loop the file, looking for files and folders
   1.208 +		$dd_try = false;
   1.209 +		rewind($this->fp);
   1.210 +
   1.211 +		while (!feof($this->fp))
   1.212 +		{
   1.213 +			// Check if the signature is valid...
   1.214 +			$signature = fread($this->fp, 4);
   1.215 +
   1.216 +			switch ($signature)
   1.217 +			{
   1.218 +				// 'Local File Header'
   1.219 +				case "\x50\x4b\x03\x04":
   1.220 +					// Lets get everything we need.
   1.221 +					// We don't store the version needed to extract, the general purpose bit flag or the date and time fields
   1.222 +					$data = unpack("@4/vc_method/@10/Vcrc/Vc_size/Vuc_size/vname_len/vextra_field", fread($this->fp, 26));
   1.223 +					$file_name = fread($this->fp, $data['name_len']); // filename
   1.224 +
   1.225 +					if ($data['extra_field'])
   1.226 +					{
   1.227 +						fread($this->fp, $data['extra_field']); // extra field
   1.228 +					}
   1.229 +
   1.230 +					$target_filename = "$dst$file_name";
   1.231 +
   1.232 +					if (!$data['uc_size'] && !$data['crc'] && substr($file_name, -1, 1) == '/')
   1.233 +					{
   1.234 +						if (!is_dir($target_filename))
   1.235 +						{
   1.236 +							$str = '';
   1.237 +							$folders = explode('/', $target_filename);
   1.238 +
   1.239 +							// Create and folders and subfolders if they do not exist
   1.240 +							foreach ($folders as $folder)
   1.241 +							{
   1.242 +								$folder = trim($folder);
   1.243 +								if (!$folder)
   1.244 +								{
   1.245 +									continue;
   1.246 +								}
   1.247 +
   1.248 +								$str = (!empty($str)) ? $str . '/' . $folder : $folder;
   1.249 +								if (!is_dir($str))
   1.250 +								{
   1.251 +									if (!@mkdir($str, 0777))
   1.252 +									{
   1.253 +										trigger_error("Could not create directory $folder");
   1.254 +									}
   1.255 +									phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
   1.256 +								}
   1.257 +							}
   1.258 +						}
   1.259 +						// This is a directory, we are not writting files
   1.260 +						continue;
   1.261 +					}
   1.262 +					else
   1.263 +					{
   1.264 +						// Some archivers are punks, they don't include folders in their archives!
   1.265 +						$str = '';
   1.266 +						$folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
   1.267 +
   1.268 +						// Create and folders and subfolders if they do not exist
   1.269 +						foreach ($folders as $folder)
   1.270 +						{
   1.271 +							$folder = trim($folder);
   1.272 +							if (!$folder)
   1.273 +							{
   1.274 +								continue;
   1.275 +							}
   1.276 +
   1.277 +							$str = (!empty($str)) ? $str . '/' . $folder : $folder;
   1.278 +							if (!is_dir($str))
   1.279 +							{
   1.280 +								if (!@mkdir($str, 0777))
   1.281 +								{
   1.282 +									trigger_error("Could not create directory $folder");
   1.283 +								}
   1.284 +								phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
   1.285 +							}
   1.286 +						}
   1.287 +					}
   1.288 +
   1.289 +					if (!$data['uc_size'])
   1.290 +					{
   1.291 +						$content = '';
   1.292 +					}
   1.293 +					else
   1.294 +					{
   1.295 +						$content = fread($this->fp, $data['c_size']);
   1.296 +					}
   1.297 +
   1.298 +					$fp = fopen($target_filename, "w");
   1.299 +
   1.300 +					switch ($data['c_method'])
   1.301 +					{
   1.302 +						case 0:
   1.303 +							// Not compressed
   1.304 +							fwrite($fp, $content);
   1.305 +						break;
   1.306 +
   1.307 +						case 8:
   1.308 +							// Deflate
   1.309 +							fwrite($fp, gzinflate($content, $data['uc_size']));
   1.310 +						break;
   1.311 +
   1.312 +						case 12:
   1.313 +							// Bzip2
   1.314 +							fwrite($fp, bzdecompress($content));
   1.315 +						break;
   1.316 +					}
   1.317 +
   1.318 +					fclose($fp);
   1.319 +				break;
   1.320 +
   1.321 +				// We hit the 'Central Directory Header', we can stop because nothing else in here requires our attention
   1.322 +				// 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
   1.323 +				case "\x50\x4b\x01\x02":
   1.324 +				// This case should simply never happen.. but it does exist..
   1.325 +				case "\x50\x4b\x05\x06":
   1.326 +				break 2;
   1.327 +
   1.328 +				// 'Packed to Removable Disk', ignore it and look for the next signature...
   1.329 +				case 'PK00':
   1.330 +				continue 2;
   1.331 +
   1.332 +				// We have encountered a header that is weird. Lets look for better data...
   1.333 +				default:
   1.334 +					if (!$dd_try)
   1.335 +					{
   1.336 +						// Unexpected header. Trying to detect wrong placed 'Data Descriptor';
   1.337 +						$dd_try = true;
   1.338 +						fseek($this->fp, 8, SEEK_CUR); // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
   1.339 +						continue 2;
   1.340 +					}
   1.341 +					trigger_error("Unexpected header, ending loop");
   1.342 +				break 2;
   1.343 +			}
   1.344 +
   1.345 +			$dd_try = false;
   1.346 +		}
   1.347 +	}
   1.348 +
   1.349 +	/**
   1.350 +	* Close archive
   1.351 +	*/
   1.352 +	function close()
   1.353 +	{
   1.354 +		// Write out central file directory and footer ... if it exists
   1.355 +		if (sizeof($this->ctrl_dir))
   1.356 +		{
   1.357 +			fwrite($this->fp, $this->file());
   1.358 +		}
   1.359 +		fclose($this->fp);
   1.360 +	}
   1.361 +
   1.362 +	/**
   1.363 +	* Create the structures ... note we assume version made by is MSDOS
   1.364 +	*/
   1.365 +	function data($name, $data, $is_dir = false, $stat)
   1.366 +	{
   1.367 +		$name = str_replace('\\', '/', $name);
   1.368 +
   1.369 +		$hexdtime = pack('V', $this->unix_to_dos_time($stat[9]));
   1.370 +
   1.371 +		if ($is_dir)
   1.372 +		{
   1.373 +			$unc_len = $c_len = $crc = 0;
   1.374 +			$zdata = '';
   1.375 +			$var_ext = 10;
   1.376 +		}
   1.377 +		else
   1.378 +		{
   1.379 +			$unc_len = strlen($data);
   1.380 +			$crc = crc32($data);
   1.381 +			$zdata = gzdeflate($data);
   1.382 +			$c_len = strlen($zdata);
   1.383 +			$var_ext = 20;
   1.384 +
   1.385 +			// Did we compress? No, then use data as is
   1.386 +			if ($c_len >= $unc_len)
   1.387 +			{
   1.388 +				$zdata = $data;
   1.389 +				$c_len = $unc_len;
   1.390 +				$var_ext = 10;
   1.391 +			}
   1.392 +		}
   1.393 +		unset($data);
   1.394 +
   1.395 +		// If we didn't compress set method to store, else deflate
   1.396 +		$c_method = ($c_len == $unc_len) ? "\x00\x00" : "\x08\x00";
   1.397 +
   1.398 +		// Are we a file or a directory? Set archive for file
   1.399 +		$attrib = ($is_dir) ? 16 : 32;
   1.400 +
   1.401 +		// File Record Header
   1.402 +		$fr = "\x50\x4b\x03\x04";		// Local file header 4bytes
   1.403 +		$fr .= pack('v', $var_ext);		// ver needed to extract 2bytes
   1.404 +		$fr .= "\x00\x00";				// gen purpose bit flag 2bytes
   1.405 +		$fr .= $c_method;				// compression method 2bytes
   1.406 +		$fr .= $hexdtime;				// last mod time and date 2+2bytes
   1.407 +		$fr .= pack('V', $crc);			// crc32 4bytes
   1.408 +		$fr .= pack('V', $c_len);		// compressed filesize 4bytes
   1.409 +		$fr .= pack('V', $unc_len);		// uncompressed filesize 4bytes
   1.410 +		$fr .= pack('v', strlen($name));// length of filename 2bytes
   1.411 +
   1.412 +		$fr .= pack('v', 0);			// extra field length 2bytes
   1.413 +		$fr .= $name;
   1.414 +		$fr .= $zdata;
   1.415 +		unset($zdata);
   1.416 +
   1.417 +		$this->datasec_len += strlen($fr);
   1.418 +
   1.419 +		// Add data to file ... by writing data out incrementally we save some memory
   1.420 +		fwrite($this->fp, $fr);
   1.421 +		unset($fr);
   1.422 +
   1.423 +		// Central Directory Header
   1.424 +		$cdrec = "\x50\x4b\x01\x02";		// header 4bytes
   1.425 +		$cdrec .= "\x00\x00";				// version made by
   1.426 +		$cdrec .= pack('v', $var_ext);		// version needed to extract
   1.427 +		$cdrec .= "\x00\x00";				// gen purpose bit flag
   1.428 +		$cdrec .= $c_method;				// compression method
   1.429 +		$cdrec .= $hexdtime;				// last mod time & date
   1.430 +		$cdrec .= pack('V', $crc);			// crc32
   1.431 +		$cdrec .= pack('V', $c_len);		// compressed filesize
   1.432 +		$cdrec .= pack('V', $unc_len);		// uncompressed filesize
   1.433 +		$cdrec .= pack('v', strlen($name));	// length of filename
   1.434 +		$cdrec .= pack('v', 0);				// extra field length
   1.435 +		$cdrec .= pack('v', 0);				// file comment length
   1.436 +		$cdrec .= pack('v', 0);				// disk number start
   1.437 +		$cdrec .= pack('v', 0);				// internal file attributes
   1.438 +		$cdrec .= pack('V', $attrib);		// external file attributes
   1.439 +		$cdrec .= pack('V', $this->old_offset);	// relative offset of local header
   1.440 +		$cdrec .= $name;
   1.441 +
   1.442 +		// Save to central directory
   1.443 +		$this->ctrl_dir[] = $cdrec;
   1.444 +
   1.445 +		$this->old_offset = $this->datasec_len;
   1.446 +	}
   1.447 +
   1.448 +	/**
   1.449 +	* file
   1.450 +	*/
   1.451 +	function file()
   1.452 +	{
   1.453 +		$ctrldir = implode('', $this->ctrl_dir);
   1.454 +
   1.455 +		return $ctrldir . $this->eof_cdh .
   1.456 +			pack('v', sizeof($this->ctrl_dir)) .	// total # of entries "on this disk"
   1.457 +			pack('v', sizeof($this->ctrl_dir)) .	// total # of entries overall
   1.458 +			pack('V', strlen($ctrldir)) .			// size of central dir
   1.459 +			pack('V', $this->datasec_len) .			// offset to start of central dir
   1.460 +			"\x00\x00";								// .zip file comment length
   1.461 +	}
   1.462 +
   1.463 +	/**
   1.464 +	* Download archive
   1.465 +	*/
   1.466 +	function download($filename, $download_name = false)
   1.467 +	{
   1.468 +		global $phpbb_root_path;
   1.469 +
   1.470 +		if ($download_name === false)
   1.471 +		{
   1.472 +			$download_name = $filename;
   1.473 +		}
   1.474 +
   1.475 +		$mimetype = 'application/zip';
   1.476 +
   1.477 +		header('Pragma: no-cache');
   1.478 +		header("Content-Type: $mimetype; name=\"$download_name.zip\"");
   1.479 +		header("Content-disposition: attachment; filename=$download_name.zip");
   1.480 +
   1.481 +		$fp = @fopen("{$phpbb_root_path}store/$filename.zip", 'rb');
   1.482 +		if ($fp)
   1.483 +		{
   1.484 +			while ($buffer = fread($fp, 1024))
   1.485 +			{
   1.486 +				echo $buffer;
   1.487 +			}
   1.488 +			fclose($fp);
   1.489 +		}
   1.490 +	}
   1.491 +}
   1.492 +
   1.493 +/**
   1.494 +* Tar/tar.gz compression routine
   1.495 +* Header/checksum creation derived from tarfile.pl, (c) Tom Horsley, 1994
   1.496 +*
   1.497 +* @package phpBB3
   1.498 +*/
   1.499 +class compress_tar extends compress
   1.500 +{
   1.501 +	var $isgz = false;
   1.502 +	var $isbz = false;
   1.503 +	var $filename = '';
   1.504 +	var $mode = '';
   1.505 +	var $type = '';
   1.506 +	var $wrote = false;
   1.507 +
   1.508 +	/**
   1.509 +	* Constructor
   1.510 +	*/
   1.511 +	function compress_tar($mode, $file, $type = '')
   1.512 +	{
   1.513 +		$type = (!$type) ? $file : $type;
   1.514 +		$this->isgz = preg_match('#(\.tar\.gz|\.tgz)$#', $type);
   1.515 +		$this->isbz = preg_match('#\.tar\.bz2$#', $type);
   1.516 +
   1.517 +		$this->mode = &$mode;
   1.518 +		$this->file = &$file;
   1.519 +		$this->type = &$type;
   1.520 +		$this->open();
   1.521 +	}
   1.522 +
   1.523 +	/**
   1.524 +	* Extract archive
   1.525 +	*/
   1.526 +	function extract($dst)
   1.527 +	{
   1.528 +		$fzread = ($this->isbz && function_exists('bzread')) ? 'bzread' : (($this->isgz && @extension_loaded('zlib')) ? 'gzread' : 'fread');
   1.529 +
   1.530 +		// Run through the file and grab directory entries
   1.531 +		while ($buffer = $fzread($this->fp, 512))
   1.532 +		{
   1.533 +			$tmp = unpack('A6magic', substr($buffer, 257, 6));
   1.534 +
   1.535 +			if (trim($tmp['magic']) == 'ustar')
   1.536 +			{
   1.537 +				$tmp = unpack('A100name', $buffer);
   1.538 +				$filename = trim($tmp['name']);
   1.539 +
   1.540 +				$tmp = unpack('Atype', substr($buffer, 156, 1));
   1.541 +				$filetype = (int) trim($tmp['type']);
   1.542 +
   1.543 +				$tmp = unpack('A12size', substr($buffer, 124, 12));
   1.544 +				$filesize = octdec((int) trim($tmp['size']));
   1.545 +
   1.546 +				$target_filename = "$dst$filename";
   1.547 +
   1.548 +				if ($filetype == 5)
   1.549 +				{
   1.550 +					if (!is_dir($target_filename))
   1.551 +					{
   1.552 +						$str = '';
   1.553 +						$folders = explode('/', $target_filename);
   1.554 +
   1.555 +						// Create and folders and subfolders if they do not exist
   1.556 +						foreach ($folders as $folder)
   1.557 +						{
   1.558 +							$folder = trim($folder);
   1.559 +							if (!$folder)
   1.560 +							{
   1.561 +								continue;
   1.562 +							}
   1.563 +
   1.564 +							$str = (!empty($str)) ? $str . '/' . $folder : $folder;
   1.565 +							if (!is_dir($str))
   1.566 +							{
   1.567 +								if (!@mkdir($str, 0777))
   1.568 +								{
   1.569 +									trigger_error("Could not create directory $folder");
   1.570 +								}
   1.571 +								phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
   1.572 +							}
   1.573 +						}
   1.574 +					}
   1.575 +				}
   1.576 +				else if ($filesize >= 0 && ($filetype == 0 || $filetype == "\0"))
   1.577 +				{
   1.578 +					// Some archivers are punks, they don't properly order the folders in their archives!
   1.579 +					$str = '';
   1.580 +					$folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
   1.581 +
   1.582 +					// Create and folders and subfolders if they do not exist
   1.583 +					foreach ($folders as $folder)
   1.584 +					{
   1.585 +						$folder = trim($folder);
   1.586 +						if (!$folder)
   1.587 +						{
   1.588 +							continue;
   1.589 +						}
   1.590 +
   1.591 +						$str = (!empty($str)) ? $str . '/' . $folder : $folder;
   1.592 +						if (!is_dir($str))
   1.593 +						{
   1.594 +							if (!@mkdir($str, 0777))
   1.595 +							{
   1.596 +								trigger_error("Could not create directory $folder");
   1.597 +							}
   1.598 +							phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
   1.599 +						}
   1.600 +					}
   1.601 +
   1.602 +					// Write out the files
   1.603 +					if (!($fp = fopen($target_filename, 'wb')))
   1.604 +					{
   1.605 +						trigger_error("Couldn't create file $filename");
   1.606 +					}
   1.607 +					phpbb_chmod($target_filename, CHMOD_READ);
   1.608 +
   1.609 +					// Grab the file contents
   1.610 +					fwrite($fp, ($filesize) ? $fzread($this->fp, ($filesize + 511) &~ 511) : '', $filesize);
   1.611 +					fclose($fp);
   1.612 +				}
   1.613 +			}
   1.614 +		}
   1.615 +	}
   1.616 +
   1.617 +	/**
   1.618 +	* Close archive
   1.619 +	*/
   1.620 +	function close()
   1.621 +	{
   1.622 +		$fzclose = ($this->isbz && function_exists('bzclose')) ? 'bzclose' : (($this->isgz && @extension_loaded('zlib')) ? 'gzclose' : 'fclose');
   1.623 +
   1.624 +		if ($this->wrote)
   1.625 +		{
   1.626 +			$fzwrite = ($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
   1.627 +
   1.628 +			// The end of a tar archive ends in two records of all NULLs (1024 bytes of \0)
   1.629 +			$fzwrite($this->fp, str_repeat("\0", 1024));
   1.630 +		}
   1.631 +
   1.632 +		$fzclose($this->fp);
   1.633 +	}
   1.634 +
   1.635 +	/**
   1.636 +	* Create the structures
   1.637 +	*/
   1.638 +	function data($name, $data, $is_dir = false, $stat)
   1.639 +	{
   1.640 +		$this->wrote = true;
   1.641 +		$fzwrite = 	($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
   1.642 +
   1.643 +		$typeflag = ($is_dir) ? '5' : '';
   1.644 +
   1.645 +		// This is the header data, it contains all the info we know about the file or folder that we are about to archive
   1.646 +		$header = '';
   1.647 +		$header .= pack('a100', $name);						// file name
   1.648 +		$header .= pack('a8', sprintf("%07o", $stat[2]));	// file mode
   1.649 +		$header .= pack('a8', sprintf("%07o", $stat[4]));	// owner id
   1.650 +		$header .= pack('a8', sprintf("%07o", $stat[5]));	// group id
   1.651 +		$header .= pack('a12', sprintf("%011o", $stat[7]));	// file size
   1.652 +		$header .= pack('a12', sprintf("%011o", $stat[9]));	// last mod time
   1.653 +
   1.654 +		// Checksum
   1.655 +		$checksum = 0;
   1.656 +		for ($i = 0; $i < 148; $i++)
   1.657 +		{
   1.658 +			$checksum += ord($header[$i]);
   1.659 +		}
   1.660 +
   1.661 +		// 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
   1.662 +		$checksum += 2415 + (($is_dir) ? 53 : 0);
   1.663 +
   1.664 +		$header .= pack('a8', sprintf("%07o", $checksum));	// checksum
   1.665 +		$header .= pack('a1', $typeflag);					// link indicator
   1.666 +		$header .= pack('a100', '');						// name of linked file
   1.667 +		$header .= pack('a6', 'ustar');						// ustar indicator
   1.668 +		$header .= pack('a2', '00');						// ustar version
   1.669 +		$header .= pack('a32', 'Unknown');					// owner name
   1.670 +		$header .= pack('a32', 'Unknown');					// group name
   1.671 +		$header .= pack('a8', '');							// device major number
   1.672 +		$header .= pack('a8', '');							// device minor number
   1.673 +		$header .= pack('a155', '');						// filename prefix
   1.674 +		$header .= pack('a12', '');							// end
   1.675 +
   1.676 +		// This writes the entire file in one shot. Header, followed by data and then null padded to a multiple of 512
   1.677 +		$fzwrite($this->fp, $header . (($stat[7] !== 0 && !$is_dir) ? $data . str_repeat("\0", (($stat[7] + 511) &~ 511) - $stat[7]) : ''));
   1.678 +		unset($data);
   1.679 +	}
   1.680 +
   1.681 +	/**
   1.682 +	* Open archive
   1.683 +	*/
   1.684 +	function open()
   1.685 +	{
   1.686 +		$fzopen = ($this->isbz && function_exists('bzopen')) ? 'bzopen' : (($this->isgz && @extension_loaded('zlib')) ? 'gzopen' : 'fopen');
   1.687 +		$this->fp = @$fzopen($this->file, $this->mode . (($fzopen == 'bzopen') ? '' : 'b') . (($fzopen == 'gzopen') ? '9' : ''));
   1.688 +
   1.689 +		if (!$this->fp)
   1.690 +		{
   1.691 +			trigger_error('Unable to open file ' . $this->file . ' [' . $fzopen . ' - ' . $this->mode . 'b]');
   1.692 +		}
   1.693 +	}
   1.694 +
   1.695 +	/**
   1.696 +	* Download archive
   1.697 +	*/
   1.698 +	function download($filename, $download_name = false)
   1.699 +	{
   1.700 +		global $phpbb_root_path;
   1.701 +
   1.702 +		if ($download_name === false)
   1.703 +		{
   1.704 +			$download_name = $filename;
   1.705 +		}
   1.706 +
   1.707 +		switch ($this->type)
   1.708 +		{
   1.709 +			case '.tar':
   1.710 +				$mimetype = 'application/x-tar';
   1.711 +			break;
   1.712 +
   1.713 +			case '.tar.gz':
   1.714 +				$mimetype = 'application/x-gzip';
   1.715 +			break;
   1.716 +
   1.717 +			case '.tar.bz2':
   1.718 +				$mimetype = 'application/x-bzip2';
   1.719 +			break;
   1.720 +
   1.721 +			default:
   1.722 +				$mimetype = 'application/octet-stream';
   1.723 +			break;
   1.724 +		}
   1.725 +
   1.726 +		header('Pragma: no-cache');
   1.727 +		header("Content-Type: $mimetype; name=\"$download_name$this->type\"");
   1.728 +		header("Content-disposition: attachment; filename=$download_name$this->type");
   1.729 +
   1.730 +		$fp = @fopen("{$phpbb_root_path}store/$filename$this->type", 'rb');
   1.731 +		if ($fp)
   1.732 +		{
   1.733 +			while ($buffer = fread($fp, 1024))
   1.734 +			{
   1.735 +				echo $buffer;
   1.736 +			}
   1.737 +			fclose($fp);
   1.738 +		}
   1.739 +	}
   1.740 +}
   1.741 +
   1.742 +?>
   1.743 \ No newline at end of file