includes/functions_compress.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:47:37 +0400
changeset 16 d6012dd9347f
parent 0 ceef6d64c21d
permissions -rw-r--r--
Removing old cubometry-style style
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Class for handling archives (compression/decompression)
indvdum@0
    21
* @package phpBB3
indvdum@0
    22
*/
indvdum@0
    23
class compress
indvdum@0
    24
{
indvdum@0
    25
	var $fp = 0;
indvdum@0
    26
indvdum@0
    27
	/**
indvdum@0
    28
	* Add file to archive
indvdum@0
    29
	*/
indvdum@0
    30
	function add_file($src, $src_rm_prefix = '', $src_add_prefix = '', $skip_files = '')
indvdum@0
    31
	{
indvdum@0
    32
		global $phpbb_root_path;
indvdum@0
    33
indvdum@0
    34
		$skip_files = explode(',', $skip_files);
indvdum@0
    35
indvdum@0
    36
		// Remove rm prefix from src path
indvdum@0
    37
		$src_path = ($src_rm_prefix) ? preg_replace('#^(' . preg_quote($src_rm_prefix, '#') . ')#', '', $src) : $src;
indvdum@0
    38
		// Add src prefix
indvdum@0
    39
		$src_path = ($src_add_prefix) ? ($src_add_prefix . ((substr($src_add_prefix, -1) != '/') ? '/' : '') . $src_path) : $src_path;
indvdum@0
    40
		// Remove initial "/" if present
indvdum@0
    41
		$src_path = (substr($src_path, 0, 1) == '/') ? substr($src_path, 1) : $src_path;
indvdum@0
    42
indvdum@0
    43
		if (is_file($phpbb_root_path . $src))
indvdum@0
    44
		{
indvdum@0
    45
			$this->data($src_path, file_get_contents("$phpbb_root_path$src"), false, stat("$phpbb_root_path$src"));
indvdum@0
    46
		}
indvdum@0
    47
		else if (is_dir($phpbb_root_path . $src))
indvdum@0
    48
		{
indvdum@0
    49
			// Clean up path, add closing / if not present
indvdum@0
    50
			$src_path = ($src_path && substr($src_path, -1) != '/') ? $src_path . '/' : $src_path;
indvdum@0
    51
indvdum@0
    52
			$filelist = array();
indvdum@0
    53
			$filelist = filelist("$phpbb_root_path$src", '', '*');
indvdum@0
    54
			krsort($filelist);
indvdum@0
    55
indvdum@0
    56
			/**
indvdum@0
    57
			* Commented out, as adding the folders produces corrupted archives
indvdum@0
    58
			if ($src_path)
indvdum@0
    59
			{
indvdum@0
    60
				$this->data($src_path, '', true, stat("$phpbb_root_path$src"));
indvdum@0
    61
			}
indvdum@0
    62
			*/
indvdum@0
    63
indvdum@0
    64
			foreach ($filelist as $path => $file_ary)
indvdum@0
    65
			{
indvdum@0
    66
				/**
indvdum@0
    67
				* Commented out, as adding the folders produces corrupted archives
indvdum@0
    68
				if ($path)
indvdum@0
    69
				{
indvdum@0
    70
					// Same as for src_path
indvdum@0
    71
					$path = (substr($path, 0, 1) == '/') ? substr($path, 1) : $path;
indvdum@0
    72
					$path = ($path && substr($path, -1) != '/') ? $path . '/' : $path;
indvdum@0
    73
indvdum@0
    74
					$this->data("$src_path$path", '', true, stat("$phpbb_root_path$src$path"));
indvdum@0
    75
				}
indvdum@0
    76
				*/
indvdum@0
    77
indvdum@0
    78
				foreach ($file_ary as $file)
indvdum@0
    79
				{
indvdum@0
    80
					if (in_array($path . $file, $skip_files))
indvdum@0
    81
					{
indvdum@0
    82
						continue;
indvdum@0
    83
					}
indvdum@0
    84
indvdum@0
    85
					$this->data("$src_path$path$file", file_get_contents("$phpbb_root_path$src$path$file"), false, stat("$phpbb_root_path$src$path$file"));
indvdum@0
    86
				}
indvdum@0
    87
			}
indvdum@0
    88
		}
indvdum@0
    89
		else
indvdum@0
    90
		{
indvdum@0
    91
			// $src does not exist
indvdum@0
    92
			return false;
indvdum@0
    93
		}
indvdum@0
    94
indvdum@0
    95
		return true;
indvdum@0
    96
	}
indvdum@0
    97
indvdum@0
    98
	/**
indvdum@0
    99
	* Add custom file (the filepath will not be adjusted)
indvdum@0
   100
	*/
indvdum@0
   101
	function add_custom_file($src, $filename)
indvdum@0
   102
	{
indvdum@0
   103
		if (!file_exists($src))
indvdum@0
   104
		{
indvdum@0
   105
			return false;
indvdum@0
   106
		}
indvdum@0
   107
indvdum@0
   108
		$this->data($filename, file_get_contents($src), false, stat($src));
indvdum@0
   109
		return true;
indvdum@0
   110
	}
indvdum@0
   111
indvdum@0
   112
	/**
indvdum@0
   113
	* Add file data
indvdum@0
   114
	*/
indvdum@0
   115
	function add_data($src, $name)
indvdum@0
   116
	{
indvdum@0
   117
		$stat = array();
indvdum@0
   118
		$stat[2] = 436; //384
indvdum@0
   119
		$stat[4] = $stat[5] = 0;
indvdum@0
   120
		$stat[7] = strlen($src);
indvdum@0
   121
		$stat[9] = time();
indvdum@0
   122
		$this->data($name, $src, false, $stat);
indvdum@0
   123
		return true;
indvdum@0
   124
	}
indvdum@0
   125
indvdum@0
   126
	/**
indvdum@0
   127
	* Return available methods
indvdum@0
   128
	*/
indvdum@0
   129
	function methods()
indvdum@0
   130
	{
indvdum@0
   131
		$methods = array('.tar');
indvdum@0
   132
		$available_methods = array('.tar.gz' => 'zlib', '.tar.bz2' => 'bz2', '.zip' => 'zlib');
indvdum@0
   133
indvdum@0
   134
		foreach ($available_methods as $type => $module)
indvdum@0
   135
		{
indvdum@0
   136
			if (!@extension_loaded($module))
indvdum@0
   137
			{
indvdum@0
   138
				continue;
indvdum@0
   139
			}
indvdum@0
   140
			$methods[] = $type;
indvdum@0
   141
		}
indvdum@0
   142
indvdum@0
   143
		return $methods;
indvdum@0
   144
	}
indvdum@0
   145
}
indvdum@0
   146
indvdum@0
   147
/**
indvdum@0
   148
* Zip creation class from phpMyAdmin 2.3.0 (c) Tobias Ratschiller, Olivier Müller, Loïc Chapeaux,
indvdum@0
   149
* Marc Delisle, http://www.phpmyadmin.net/
indvdum@0
   150
*
indvdum@0
   151
* Zip extraction function by Alexandre Tedeschi, alexandrebr at gmail dot com
indvdum@0
   152
*
indvdum@0
   153
* Modified extensively by psoTFX and DavidMJ, (c) phpBB Group, 2003
indvdum@0
   154
*
indvdum@0
   155
* Based on work by Eric Mueller and Denis125
indvdum@0
   156
* Official ZIP file format: http://www.pkware.com/appnote.txt
indvdum@0
   157
*
indvdum@0
   158
* @package phpBB3
indvdum@0
   159
*/
indvdum@0
   160
class compress_zip extends compress
indvdum@0
   161
{
indvdum@0
   162
	var $datasec = array();
indvdum@0
   163
	var $ctrl_dir = array();
indvdum@0
   164
	var $eof_cdh = "\x50\x4b\x05\x06\x00\x00\x00\x00";
indvdum@0
   165
indvdum@0
   166
	var $old_offset = 0;
indvdum@0
   167
	var $datasec_len = 0;
indvdum@0
   168
indvdum@0
   169
	/**
indvdum@0
   170
	* Constructor
indvdum@0
   171
	*/
indvdum@0
   172
	function compress_zip($mode, $file)
indvdum@0
   173
	{
indvdum@0
   174
		$this->fp = @fopen($file, $mode . 'b');
indvdum@0
   175
indvdum@0
   176
		if (!$this->fp)
indvdum@0
   177
		{
indvdum@0
   178
			trigger_error('Unable to open file ' . $file . ' [' . $mode . 'b]');
indvdum@0
   179
		}
indvdum@0
   180
	}
indvdum@0
   181
indvdum@0
   182
	/**
indvdum@0
   183
	* Convert unix to dos time
indvdum@0
   184
	*/
indvdum@0
   185
	function unix_to_dos_time($time)
indvdum@0
   186
	{
indvdum@0
   187
		$timearray = (!$time) ? getdate() : getdate($time);
indvdum@0
   188
indvdum@0
   189
		if ($timearray['year'] < 1980)
indvdum@0
   190
		{
indvdum@0
   191
			$timearray['year'] = 1980;
indvdum@0
   192
			$timearray['mon'] = $timearray['mday'] = 1;
indvdum@0
   193
			$timearray['hours'] = $timearray['minutes'] = $timearray['seconds'] = 0;
indvdum@0
   194
		}
indvdum@0
   195
indvdum@0
   196
		return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
indvdum@0
   197
	}
indvdum@0
   198
indvdum@0
   199
	/**
indvdum@0
   200
	* Extract archive
indvdum@0
   201
	*/
indvdum@0
   202
	function extract($dst)
indvdum@0
   203
	{
indvdum@0
   204
		// Loop the file, looking for files and folders
indvdum@0
   205
		$dd_try = false;
indvdum@0
   206
		rewind($this->fp);
indvdum@0
   207
indvdum@0
   208
		while (!feof($this->fp))
indvdum@0
   209
		{
indvdum@0
   210
			// Check if the signature is valid...
indvdum@0
   211
			$signature = fread($this->fp, 4);
indvdum@0
   212
indvdum@0
   213
			switch ($signature)
indvdum@0
   214
			{
indvdum@0
   215
				// 'Local File Header'
indvdum@0
   216
				case "\x50\x4b\x03\x04":
indvdum@0
   217
					// Lets get everything we need.
indvdum@0
   218
					// We don't store the version needed to extract, the general purpose bit flag or the date and time fields
indvdum@0
   219
					$data = unpack("@4/vc_method/@10/Vcrc/Vc_size/Vuc_size/vname_len/vextra_field", fread($this->fp, 26));
indvdum@0
   220
					$file_name = fread($this->fp, $data['name_len']); // filename
indvdum@0
   221
indvdum@0
   222
					if ($data['extra_field'])
indvdum@0
   223
					{
indvdum@0
   224
						fread($this->fp, $data['extra_field']); // extra field
indvdum@0
   225
					}
indvdum@0
   226
indvdum@0
   227
					$target_filename = "$dst$file_name";
indvdum@0
   228
indvdum@0
   229
					if (!$data['uc_size'] && !$data['crc'] && substr($file_name, -1, 1) == '/')
indvdum@0
   230
					{
indvdum@0
   231
						if (!is_dir($target_filename))
indvdum@0
   232
						{
indvdum@0
   233
							$str = '';
indvdum@0
   234
							$folders = explode('/', $target_filename);
indvdum@0
   235
indvdum@0
   236
							// Create and folders and subfolders if they do not exist
indvdum@0
   237
							foreach ($folders as $folder)
indvdum@0
   238
							{
indvdum@0
   239
								$folder = trim($folder);
indvdum@0
   240
								if (!$folder)
indvdum@0
   241
								{
indvdum@0
   242
									continue;
indvdum@0
   243
								}
indvdum@0
   244
indvdum@0
   245
								$str = (!empty($str)) ? $str . '/' . $folder : $folder;
indvdum@0
   246
								if (!is_dir($str))
indvdum@0
   247
								{
indvdum@0
   248
									if (!@mkdir($str, 0777))
indvdum@0
   249
									{
indvdum@0
   250
										trigger_error("Could not create directory $folder");
indvdum@0
   251
									}
indvdum@0
   252
									phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
indvdum@0
   253
								}
indvdum@0
   254
							}
indvdum@0
   255
						}
indvdum@0
   256
						// This is a directory, we are not writting files
indvdum@0
   257
						continue;
indvdum@0
   258
					}
indvdum@0
   259
					else
indvdum@0
   260
					{
indvdum@0
   261
						// Some archivers are punks, they don't include folders in their archives!
indvdum@0
   262
						$str = '';
indvdum@0
   263
						$folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
indvdum@0
   264
indvdum@0
   265
						// Create and folders and subfolders if they do not exist
indvdum@0
   266
						foreach ($folders as $folder)
indvdum@0
   267
						{
indvdum@0
   268
							$folder = trim($folder);
indvdum@0
   269
							if (!$folder)
indvdum@0
   270
							{
indvdum@0
   271
								continue;
indvdum@0
   272
							}
indvdum@0
   273
indvdum@0
   274
							$str = (!empty($str)) ? $str . '/' . $folder : $folder;
indvdum@0
   275
							if (!is_dir($str))
indvdum@0
   276
							{
indvdum@0
   277
								if (!@mkdir($str, 0777))
indvdum@0
   278
								{
indvdum@0
   279
									trigger_error("Could not create directory $folder");
indvdum@0
   280
								}
indvdum@0
   281
								phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
indvdum@0
   282
							}
indvdum@0
   283
						}
indvdum@0
   284
					}
indvdum@0
   285
indvdum@0
   286
					if (!$data['uc_size'])
indvdum@0
   287
					{
indvdum@0
   288
						$content = '';
indvdum@0
   289
					}
indvdum@0
   290
					else
indvdum@0
   291
					{
indvdum@0
   292
						$content = fread($this->fp, $data['c_size']);
indvdum@0
   293
					}
indvdum@0
   294
indvdum@0
   295
					$fp = fopen($target_filename, "w");
indvdum@0
   296
indvdum@0
   297
					switch ($data['c_method'])
indvdum@0
   298
					{
indvdum@0
   299
						case 0:
indvdum@0
   300
							// Not compressed
indvdum@0
   301
							fwrite($fp, $content);
indvdum@0
   302
						break;
indvdum@0
   303
indvdum@0
   304
						case 8:
indvdum@0
   305
							// Deflate
indvdum@0
   306
							fwrite($fp, gzinflate($content, $data['uc_size']));
indvdum@0
   307
						break;
indvdum@0
   308
indvdum@0
   309
						case 12:
indvdum@0
   310
							// Bzip2
indvdum@0
   311
							fwrite($fp, bzdecompress($content));
indvdum@0
   312
						break;
indvdum@0
   313
					}
indvdum@0
   314
indvdum@0
   315
					fclose($fp);
indvdum@0
   316
				break;
indvdum@0
   317
indvdum@0
   318
				// We hit the 'Central Directory Header', we can stop because nothing else in here requires our attention
indvdum@0
   319
				// 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
indvdum@0
   320
				case "\x50\x4b\x01\x02":
indvdum@0
   321
				// This case should simply never happen.. but it does exist..
indvdum@0
   322
				case "\x50\x4b\x05\x06":
indvdum@0
   323
				break 2;
indvdum@0
   324
indvdum@0
   325
				// 'Packed to Removable Disk', ignore it and look for the next signature...
indvdum@0
   326
				case 'PK00':
indvdum@0
   327
				continue 2;
indvdum@0
   328
indvdum@0
   329
				// We have encountered a header that is weird. Lets look for better data...
indvdum@0
   330
				default:
indvdum@0
   331
					if (!$dd_try)
indvdum@0
   332
					{
indvdum@0
   333
						// Unexpected header. Trying to detect wrong placed 'Data Descriptor';
indvdum@0
   334
						$dd_try = true;
indvdum@0
   335
						fseek($this->fp, 8, SEEK_CUR); // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
indvdum@0
   336
						continue 2;
indvdum@0
   337
					}
indvdum@0
   338
					trigger_error("Unexpected header, ending loop");
indvdum@0
   339
				break 2;
indvdum@0
   340
			}
indvdum@0
   341
indvdum@0
   342
			$dd_try = false;
indvdum@0
   343
		}
indvdum@0
   344
	}
indvdum@0
   345
indvdum@0
   346
	/**
indvdum@0
   347
	* Close archive
indvdum@0
   348
	*/
indvdum@0
   349
	function close()
indvdum@0
   350
	{
indvdum@0
   351
		// Write out central file directory and footer ... if it exists
indvdum@0
   352
		if (sizeof($this->ctrl_dir))
indvdum@0
   353
		{
indvdum@0
   354
			fwrite($this->fp, $this->file());
indvdum@0
   355
		}
indvdum@0
   356
		fclose($this->fp);
indvdum@0
   357
	}
indvdum@0
   358
indvdum@0
   359
	/**
indvdum@0
   360
	* Create the structures ... note we assume version made by is MSDOS
indvdum@0
   361
	*/
indvdum@0
   362
	function data($name, $data, $is_dir = false, $stat)
indvdum@0
   363
	{
indvdum@0
   364
		$name = str_replace('\\', '/', $name);
indvdum@0
   365
indvdum@0
   366
		$hexdtime = pack('V', $this->unix_to_dos_time($stat[9]));
indvdum@0
   367
indvdum@0
   368
		if ($is_dir)
indvdum@0
   369
		{
indvdum@0
   370
			$unc_len = $c_len = $crc = 0;
indvdum@0
   371
			$zdata = '';
indvdum@0
   372
			$var_ext = 10;
indvdum@0
   373
		}
indvdum@0
   374
		else
indvdum@0
   375
		{
indvdum@0
   376
			$unc_len = strlen($data);
indvdum@0
   377
			$crc = crc32($data);
indvdum@0
   378
			$zdata = gzdeflate($data);
indvdum@0
   379
			$c_len = strlen($zdata);
indvdum@0
   380
			$var_ext = 20;
indvdum@0
   381
indvdum@0
   382
			// Did we compress? No, then use data as is
indvdum@0
   383
			if ($c_len >= $unc_len)
indvdum@0
   384
			{
indvdum@0
   385
				$zdata = $data;
indvdum@0
   386
				$c_len = $unc_len;
indvdum@0
   387
				$var_ext = 10;
indvdum@0
   388
			}
indvdum@0
   389
		}
indvdum@0
   390
		unset($data);
indvdum@0
   391
indvdum@0
   392
		// If we didn't compress set method to store, else deflate
indvdum@0
   393
		$c_method = ($c_len == $unc_len) ? "\x00\x00" : "\x08\x00";
indvdum@0
   394
indvdum@0
   395
		// Are we a file or a directory? Set archive for file
indvdum@0
   396
		$attrib = ($is_dir) ? 16 : 32;
indvdum@0
   397
indvdum@0
   398
		// File Record Header
indvdum@0
   399
		$fr = "\x50\x4b\x03\x04";		// Local file header 4bytes
indvdum@0
   400
		$fr .= pack('v', $var_ext);		// ver needed to extract 2bytes
indvdum@0
   401
		$fr .= "\x00\x00";				// gen purpose bit flag 2bytes
indvdum@0
   402
		$fr .= $c_method;				// compression method 2bytes
indvdum@0
   403
		$fr .= $hexdtime;				// last mod time and date 2+2bytes
indvdum@0
   404
		$fr .= pack('V', $crc);			// crc32 4bytes
indvdum@0
   405
		$fr .= pack('V', $c_len);		// compressed filesize 4bytes
indvdum@0
   406
		$fr .= pack('V', $unc_len);		// uncompressed filesize 4bytes
indvdum@0
   407
		$fr .= pack('v', strlen($name));// length of filename 2bytes
indvdum@0
   408
indvdum@0
   409
		$fr .= pack('v', 0);			// extra field length 2bytes
indvdum@0
   410
		$fr .= $name;
indvdum@0
   411
		$fr .= $zdata;
indvdum@0
   412
		unset($zdata);
indvdum@0
   413
indvdum@0
   414
		$this->datasec_len += strlen($fr);
indvdum@0
   415
indvdum@0
   416
		// Add data to file ... by writing data out incrementally we save some memory
indvdum@0
   417
		fwrite($this->fp, $fr);
indvdum@0
   418
		unset($fr);
indvdum@0
   419
indvdum@0
   420
		// Central Directory Header
indvdum@0
   421
		$cdrec = "\x50\x4b\x01\x02";		// header 4bytes
indvdum@0
   422
		$cdrec .= "\x00\x00";				// version made by
indvdum@0
   423
		$cdrec .= pack('v', $var_ext);		// version needed to extract
indvdum@0
   424
		$cdrec .= "\x00\x00";				// gen purpose bit flag
indvdum@0
   425
		$cdrec .= $c_method;				// compression method
indvdum@0
   426
		$cdrec .= $hexdtime;				// last mod time & date
indvdum@0
   427
		$cdrec .= pack('V', $crc);			// crc32
indvdum@0
   428
		$cdrec .= pack('V', $c_len);		// compressed filesize
indvdum@0
   429
		$cdrec .= pack('V', $unc_len);		// uncompressed filesize
indvdum@0
   430
		$cdrec .= pack('v', strlen($name));	// length of filename
indvdum@0
   431
		$cdrec .= pack('v', 0);				// extra field length
indvdum@0
   432
		$cdrec .= pack('v', 0);				// file comment length
indvdum@0
   433
		$cdrec .= pack('v', 0);				// disk number start
indvdum@0
   434
		$cdrec .= pack('v', 0);				// internal file attributes
indvdum@0
   435
		$cdrec .= pack('V', $attrib);		// external file attributes
indvdum@0
   436
		$cdrec .= pack('V', $this->old_offset);	// relative offset of local header
indvdum@0
   437
		$cdrec .= $name;
indvdum@0
   438
indvdum@0
   439
		// Save to central directory
indvdum@0
   440
		$this->ctrl_dir[] = $cdrec;
indvdum@0
   441
indvdum@0
   442
		$this->old_offset = $this->datasec_len;
indvdum@0
   443
	}
indvdum@0
   444
indvdum@0
   445
	/**
indvdum@0
   446
	* file
indvdum@0
   447
	*/
indvdum@0
   448
	function file()
indvdum@0
   449
	{
indvdum@0
   450
		$ctrldir = implode('', $this->ctrl_dir);
indvdum@0
   451
indvdum@0
   452
		return $ctrldir . $this->eof_cdh .
indvdum@0
   453
			pack('v', sizeof($this->ctrl_dir)) .	// total # of entries "on this disk"
indvdum@0
   454
			pack('v', sizeof($this->ctrl_dir)) .	// total # of entries overall
indvdum@0
   455
			pack('V', strlen($ctrldir)) .			// size of central dir
indvdum@0
   456
			pack('V', $this->datasec_len) .			// offset to start of central dir
indvdum@0
   457
			"\x00\x00";								// .zip file comment length
indvdum@0
   458
	}
indvdum@0
   459
indvdum@0
   460
	/**
indvdum@0
   461
	* Download archive
indvdum@0
   462
	*/
indvdum@0
   463
	function download($filename, $download_name = false)
indvdum@0
   464
	{
indvdum@0
   465
		global $phpbb_root_path;
indvdum@0
   466
indvdum@0
   467
		if ($download_name === false)
indvdum@0
   468
		{
indvdum@0
   469
			$download_name = $filename;
indvdum@0
   470
		}
indvdum@0
   471
indvdum@0
   472
		$mimetype = 'application/zip';
indvdum@0
   473
indvdum@0
   474
		header('Pragma: no-cache');
indvdum@0
   475
		header("Content-Type: $mimetype; name=\"$download_name.zip\"");
indvdum@0
   476
		header("Content-disposition: attachment; filename=$download_name.zip");
indvdum@0
   477
indvdum@0
   478
		$fp = @fopen("{$phpbb_root_path}store/$filename.zip", 'rb');
indvdum@0
   479
		if ($fp)
indvdum@0
   480
		{
indvdum@0
   481
			while ($buffer = fread($fp, 1024))
indvdum@0
   482
			{
indvdum@0
   483
				echo $buffer;
indvdum@0
   484
			}
indvdum@0
   485
			fclose($fp);
indvdum@0
   486
		}
indvdum@0
   487
	}
indvdum@0
   488
}
indvdum@0
   489
indvdum@0
   490
/**
indvdum@0
   491
* Tar/tar.gz compression routine
indvdum@0
   492
* Header/checksum creation derived from tarfile.pl, (c) Tom Horsley, 1994
indvdum@0
   493
*
indvdum@0
   494
* @package phpBB3
indvdum@0
   495
*/
indvdum@0
   496
class compress_tar extends compress
indvdum@0
   497
{
indvdum@0
   498
	var $isgz = false;
indvdum@0
   499
	var $isbz = false;
indvdum@0
   500
	var $filename = '';
indvdum@0
   501
	var $mode = '';
indvdum@0
   502
	var $type = '';
indvdum@0
   503
	var $wrote = false;
indvdum@0
   504
indvdum@0
   505
	/**
indvdum@0
   506
	* Constructor
indvdum@0
   507
	*/
indvdum@0
   508
	function compress_tar($mode, $file, $type = '')
indvdum@0
   509
	{
indvdum@0
   510
		$type = (!$type) ? $file : $type;
indvdum@0
   511
		$this->isgz = preg_match('#(\.tar\.gz|\.tgz)$#', $type);
indvdum@0
   512
		$this->isbz = preg_match('#\.tar\.bz2$#', $type);
indvdum@0
   513
indvdum@0
   514
		$this->mode = &$mode;
indvdum@0
   515
		$this->file = &$file;
indvdum@0
   516
		$this->type = &$type;
indvdum@0
   517
		$this->open();
indvdum@0
   518
	}
indvdum@0
   519
indvdum@0
   520
	/**
indvdum@0
   521
	* Extract archive
indvdum@0
   522
	*/
indvdum@0
   523
	function extract($dst)
indvdum@0
   524
	{
indvdum@0
   525
		$fzread = ($this->isbz && function_exists('bzread')) ? 'bzread' : (($this->isgz && @extension_loaded('zlib')) ? 'gzread' : 'fread');
indvdum@0
   526
indvdum@0
   527
		// Run through the file and grab directory entries
indvdum@0
   528
		while ($buffer = $fzread($this->fp, 512))
indvdum@0
   529
		{
indvdum@0
   530
			$tmp = unpack('A6magic', substr($buffer, 257, 6));
indvdum@0
   531
indvdum@0
   532
			if (trim($tmp['magic']) == 'ustar')
indvdum@0
   533
			{
indvdum@0
   534
				$tmp = unpack('A100name', $buffer);
indvdum@0
   535
				$filename = trim($tmp['name']);
indvdum@0
   536
indvdum@0
   537
				$tmp = unpack('Atype', substr($buffer, 156, 1));
indvdum@0
   538
				$filetype = (int) trim($tmp['type']);
indvdum@0
   539
indvdum@0
   540
				$tmp = unpack('A12size', substr($buffer, 124, 12));
indvdum@0
   541
				$filesize = octdec((int) trim($tmp['size']));
indvdum@0
   542
indvdum@0
   543
				$target_filename = "$dst$filename";
indvdum@0
   544
indvdum@0
   545
				if ($filetype == 5)
indvdum@0
   546
				{
indvdum@0
   547
					if (!is_dir($target_filename))
indvdum@0
   548
					{
indvdum@0
   549
						$str = '';
indvdum@0
   550
						$folders = explode('/', $target_filename);
indvdum@0
   551
indvdum@0
   552
						// Create and folders and subfolders if they do not exist
indvdum@0
   553
						foreach ($folders as $folder)
indvdum@0
   554
						{
indvdum@0
   555
							$folder = trim($folder);
indvdum@0
   556
							if (!$folder)
indvdum@0
   557
							{
indvdum@0
   558
								continue;
indvdum@0
   559
							}
indvdum@0
   560
indvdum@0
   561
							$str = (!empty($str)) ? $str . '/' . $folder : $folder;
indvdum@0
   562
							if (!is_dir($str))
indvdum@0
   563
							{
indvdum@0
   564
								if (!@mkdir($str, 0777))
indvdum@0
   565
								{
indvdum@0
   566
									trigger_error("Could not create directory $folder");
indvdum@0
   567
								}
indvdum@0
   568
								phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
indvdum@0
   569
							}
indvdum@0
   570
						}
indvdum@0
   571
					}
indvdum@0
   572
				}
indvdum@0
   573
				else if ($filesize >= 0 && ($filetype == 0 || $filetype == "\0"))
indvdum@0
   574
				{
indvdum@0
   575
					// Some archivers are punks, they don't properly order the folders in their archives!
indvdum@0
   576
					$str = '';
indvdum@0
   577
					$folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
indvdum@0
   578
indvdum@0
   579
					// Create and folders and subfolders if they do not exist
indvdum@0
   580
					foreach ($folders as $folder)
indvdum@0
   581
					{
indvdum@0
   582
						$folder = trim($folder);
indvdum@0
   583
						if (!$folder)
indvdum@0
   584
						{
indvdum@0
   585
							continue;
indvdum@0
   586
						}
indvdum@0
   587
indvdum@0
   588
						$str = (!empty($str)) ? $str . '/' . $folder : $folder;
indvdum@0
   589
						if (!is_dir($str))
indvdum@0
   590
						{
indvdum@0
   591
							if (!@mkdir($str, 0777))
indvdum@0
   592
							{
indvdum@0
   593
								trigger_error("Could not create directory $folder");
indvdum@0
   594
							}
indvdum@0
   595
							phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
indvdum@0
   596
						}
indvdum@0
   597
					}
indvdum@0
   598
indvdum@0
   599
					// Write out the files
indvdum@0
   600
					if (!($fp = fopen($target_filename, 'wb')))
indvdum@0
   601
					{
indvdum@0
   602
						trigger_error("Couldn't create file $filename");
indvdum@0
   603
					}
indvdum@0
   604
					phpbb_chmod($target_filename, CHMOD_READ);
indvdum@0
   605
indvdum@0
   606
					// Grab the file contents
indvdum@0
   607
					fwrite($fp, ($filesize) ? $fzread($this->fp, ($filesize + 511) &~ 511) : '', $filesize);
indvdum@0
   608
					fclose($fp);
indvdum@0
   609
				}
indvdum@0
   610
			}
indvdum@0
   611
		}
indvdum@0
   612
	}
indvdum@0
   613
indvdum@0
   614
	/**
indvdum@0
   615
	* Close archive
indvdum@0
   616
	*/
indvdum@0
   617
	function close()
indvdum@0
   618
	{
indvdum@0
   619
		$fzclose = ($this->isbz && function_exists('bzclose')) ? 'bzclose' : (($this->isgz && @extension_loaded('zlib')) ? 'gzclose' : 'fclose');
indvdum@0
   620
indvdum@0
   621
		if ($this->wrote)
indvdum@0
   622
		{
indvdum@0
   623
			$fzwrite = ($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
indvdum@0
   624
indvdum@0
   625
			// The end of a tar archive ends in two records of all NULLs (1024 bytes of \0)
indvdum@0
   626
			$fzwrite($this->fp, str_repeat("\0", 1024));
indvdum@0
   627
		}
indvdum@0
   628
indvdum@0
   629
		$fzclose($this->fp);
indvdum@0
   630
	}
indvdum@0
   631
indvdum@0
   632
	/**
indvdum@0
   633
	* Create the structures
indvdum@0
   634
	*/
indvdum@0
   635
	function data($name, $data, $is_dir = false, $stat)
indvdum@0
   636
	{
indvdum@0
   637
		$this->wrote = true;
indvdum@0
   638
		$fzwrite = 	($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
indvdum@0
   639
indvdum@0
   640
		$typeflag = ($is_dir) ? '5' : '';
indvdum@0
   641
indvdum@0
   642
		// This is the header data, it contains all the info we know about the file or folder that we are about to archive
indvdum@0
   643
		$header = '';
indvdum@0
   644
		$header .= pack('a100', $name);						// file name
indvdum@0
   645
		$header .= pack('a8', sprintf("%07o", $stat[2]));	// file mode
indvdum@0
   646
		$header .= pack('a8', sprintf("%07o", $stat[4]));	// owner id
indvdum@0
   647
		$header .= pack('a8', sprintf("%07o", $stat[5]));	// group id
indvdum@0
   648
		$header .= pack('a12', sprintf("%011o", $stat[7]));	// file size
indvdum@0
   649
		$header .= pack('a12', sprintf("%011o", $stat[9]));	// last mod time
indvdum@0
   650
indvdum@0
   651
		// Checksum
indvdum@0
   652
		$checksum = 0;
indvdum@0
   653
		for ($i = 0; $i < 148; $i++)
indvdum@0
   654
		{
indvdum@0
   655
			$checksum += ord($header[$i]);
indvdum@0
   656
		}
indvdum@0
   657
indvdum@0
   658
		// 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
indvdum@0
   659
		$checksum += 2415 + (($is_dir) ? 53 : 0);
indvdum@0
   660
indvdum@0
   661
		$header .= pack('a8', sprintf("%07o", $checksum));	// checksum
indvdum@0
   662
		$header .= pack('a1', $typeflag);					// link indicator
indvdum@0
   663
		$header .= pack('a100', '');						// name of linked file
indvdum@0
   664
		$header .= pack('a6', 'ustar');						// ustar indicator
indvdum@0
   665
		$header .= pack('a2', '00');						// ustar version
indvdum@0
   666
		$header .= pack('a32', 'Unknown');					// owner name
indvdum@0
   667
		$header .= pack('a32', 'Unknown');					// group name
indvdum@0
   668
		$header .= pack('a8', '');							// device major number
indvdum@0
   669
		$header .= pack('a8', '');							// device minor number
indvdum@0
   670
		$header .= pack('a155', '');						// filename prefix
indvdum@0
   671
		$header .= pack('a12', '');							// end
indvdum@0
   672
indvdum@0
   673
		// This writes the entire file in one shot. Header, followed by data and then null padded to a multiple of 512
indvdum@0
   674
		$fzwrite($this->fp, $header . (($stat[7] !== 0 && !$is_dir) ? $data . str_repeat("\0", (($stat[7] + 511) &~ 511) - $stat[7]) : ''));
indvdum@0
   675
		unset($data);
indvdum@0
   676
	}
indvdum@0
   677
indvdum@0
   678
	/**
indvdum@0
   679
	* Open archive
indvdum@0
   680
	*/
indvdum@0
   681
	function open()
indvdum@0
   682
	{
indvdum@0
   683
		$fzopen = ($this->isbz && function_exists('bzopen')) ? 'bzopen' : (($this->isgz && @extension_loaded('zlib')) ? 'gzopen' : 'fopen');
indvdum@0
   684
		$this->fp = @$fzopen($this->file, $this->mode . (($fzopen == 'bzopen') ? '' : 'b') . (($fzopen == 'gzopen') ? '9' : ''));
indvdum@0
   685
indvdum@0
   686
		if (!$this->fp)
indvdum@0
   687
		{
indvdum@0
   688
			trigger_error('Unable to open file ' . $this->file . ' [' . $fzopen . ' - ' . $this->mode . 'b]');
indvdum@0
   689
		}
indvdum@0
   690
	}
indvdum@0
   691
indvdum@0
   692
	/**
indvdum@0
   693
	* Download archive
indvdum@0
   694
	*/
indvdum@0
   695
	function download($filename, $download_name = false)
indvdum@0
   696
	{
indvdum@0
   697
		global $phpbb_root_path;
indvdum@0
   698
indvdum@0
   699
		if ($download_name === false)
indvdum@0
   700
		{
indvdum@0
   701
			$download_name = $filename;
indvdum@0
   702
		}
indvdum@0
   703
indvdum@0
   704
		switch ($this->type)
indvdum@0
   705
		{
indvdum@0
   706
			case '.tar':
indvdum@0
   707
				$mimetype = 'application/x-tar';
indvdum@0
   708
			break;
indvdum@0
   709
indvdum@0
   710
			case '.tar.gz':
indvdum@0
   711
				$mimetype = 'application/x-gzip';
indvdum@0
   712
			break;
indvdum@0
   713
indvdum@0
   714
			case '.tar.bz2':
indvdum@0
   715
				$mimetype = 'application/x-bzip2';
indvdum@0
   716
			break;
indvdum@0
   717
indvdum@0
   718
			default:
indvdum@0
   719
				$mimetype = 'application/octet-stream';
indvdum@0
   720
			break;
indvdum@0
   721
		}
indvdum@0
   722
indvdum@0
   723
		header('Pragma: no-cache');
indvdum@0
   724
		header("Content-Type: $mimetype; name=\"$download_name$this->type\"");
indvdum@0
   725
		header("Content-disposition: attachment; filename=$download_name$this->type");
indvdum@0
   726
indvdum@0
   727
		$fp = @fopen("{$phpbb_root_path}store/$filename$this->type", 'rb');
indvdum@0
   728
		if ($fp)
indvdum@0
   729
		{
indvdum@0
   730
			while ($buffer = fread($fp, 1024))
indvdum@0
   731
			{
indvdum@0
   732
				echo $buffer;
indvdum@0
   733
			}
indvdum@0
   734
			fclose($fp);
indvdum@0
   735
		}
indvdum@0
   736
	}
indvdum@0
   737
}
indvdum@0
   738
indvdum@0
   739
?>