aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/functions_compress.php
blob: ac0ba106bb04b039921821b3561b95b7dc7cbe8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?
/***************************************************************************
 *                           functions_compress.php
 *                            -------------------
 *   begin                : Saturday, Jul 19, 2003
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 *
 *   $Id$
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

class compress 
{
	var $fp = 0;

	function add_file($src, $src_prefix = '', $skip_files = '')
	{
		global $phpbb_root_path;

		$skip_files = explode(',', $skip_files);

		if (is_file($phpbb_root_path . $src))
		{
			$src_prefix = ($src_prefix) ? preg_replace('#^(' . preg_quote($src_prefix) . ')#', '', $src) : $src;

			if (!($fp = @fopen($phpbb_root_path . $src, 'rb')))
			{
				return false;
			}

			$data = fread($fp, filesize($phpbb_root_path . $src));
			fclose($fp);

			$this->data($src_prefix, $data, filemtime($phpbb_root_path . $src), false);
		}
		else if (is_dir($phpbb_root_path . $src))
		{
			// Remove prefix from src path 
			$src_prefix = ($src_prefix) ? preg_replace('#^(' . preg_quote($src_prefix) . ')#', '', $src) : $src;

			// Clean up path, remove initial / if present, add ending / if not present
			$src_prefix = (strpos($src_prefix, '/') === 0) ? substr($src_prefix, 1) : $src_prefix;
			$src_prefix = (strrpos($src_prefix, '/') != strlen($src_prefix) - 1) ? (($src_prefix != '') ? $src_prefix . '/' : '') : $src_prefix;

			$filelist = filelist($phpbb_root_path . $src, '', '*');
			ksort($filelist);

			if ($src_prefix)
			{
				$this->data($src_prefix, '', filemtime($src_prefix), true);
			}

			foreach ($filelist as $path => $file_ary)
			{
				if ($path)
				{
					// Same as for src_prefix
					$path = (strpos($path, '/') === 0) ? substr($path, 1) : $path;
					$path = (strrpos($path, '/') != strlen($path) - 1) ? $path . '/' : $path;

					$this->data($src_prefix . $path, '', filemtime($src_prefix . $path), true);
				}

				foreach ($file_ary as $file)
				{
					if (in_array($path . $file, $skip_files))
					{
						continue;
					}

					$this->data($src_prefix . $path . $file, implode('', file($phpbb_root_path . $src . $path . $file)), filemtime($phpbb_root_path . $src . $path . $file), false);
				}
			}

		}
		return true;
	}

	function add_data($src, $name)
	{
		$this->data($name, $src);
		return true;
	}
}

// Zip creation class from phpMyAdmin 2.3.0 © Tobias Ratschiller, Olivier Müller, Loïc Chapeaux, Marc Delisle
// http://www.phpmyadmin.net/
//
// Modified extensively by psoTFX, © phpBB Group, 2003
//
// Based on work by Eric Mueller and Denis125
// Official ZIP file format: http://www.pkware.com/appnote.txt
class compress_zip extends compress
{
	var $datasec = array();
	var $ctrl_dir = array();
	var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";

	var $old_offset = 0;
	var $datasec_len = 0;

	function compress_zip($mode, $file)
	{
		return $this->fp = @fopen($phpbb_root_path . $file, $mode . 'b');
	}

	function unix_to_dos_time($time)
	{
		$timearray = (!$time) ? getdate() : getdate($time);

		if ($timearray['year'] < 1980)
		{
			$timearray['year'] = 1980;
			$timearray['mon'] = $timearray['mday'] = 1;
			$timearray['hours'] = $timearray['minutes'] = $timearray['seconds'] = 0;
		}

		return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
	}

	function extract($dst)
	{
	}

	function close()
	{
		// Write out central file directory and footer
		fwrite($this->fp, $this->file());
		fclose($this->fp);
	}

	function data($name, $data, $mtime = false, $is_dir = false)
	{
		$name = str_replace('\\', '/', $name);

		$dtime = dechex($this->unix_to_dos_time($mtime));
		$hexdtime = '\x' . $dtime[6] . $dtime[7] . '\x' . $dtime[4] . $dtime[5] . '\x' . $dtime[2] . $dtime[3] . '\x' . $dtime[0] . $dtime[1];
		eval('$hexdtime = "' . $hexdtime . '";');

		$unc_len = strlen($data);
		$crc = crc32($data);
		$zdata = gzcompress($data);
		$zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
		$c_len = strlen($zdata);
		unset($data);

		$fr = "\x50\x4b\x03\x04";
		$fr .= "\x14\x00";	// ver needed to extract
		$fr .= "\x00\x00";	// gen purpose bit flag
		$fr .= "\x08\x00";	// compression method
		$fr .= $hexdtime;				// last mod time and date
		$fr .= pack('V', $crc);			// crc32
		$fr .= pack('V', $c_len);		// compressed filesize
		$fr .= pack('V', $unc_len);		// uncompressed filesize
		$fr .= pack('v', strlen($name));// length of filename
		$fr .= pack('v', 0);			// extra field length
		$fr .= $name;
		$fr .= $zdata;
		unset($zdata);
		$fr .= pack('V', $crc);
		$fr .= pack('V', $c_len);	// compressed filesize
		$fr .= pack('V', $unc_len);	// uncompressed filesize

		$this->datasec_len += strlen($fr);

		// Add data to file ... by writing data out incrementally we
		// save some memory
		fwrite($this->fp, $fr);
		unset($fr);

		// Are we a file or a directory? Set archive for file
		$attrib = ($is_dir) ? 0x41FF0010 : 32;

		$cdrec = "\x50\x4b\x01\x02";
		$cdrec .= "\x00\x00";                // version made by
		$cdrec .= "\x14\x00";                // version needed to extract
		$cdrec .= "\x00\x00";                // gen purpose bit flag
		$cdrec .= "\x08\x00";                // compression method
		$cdrec .= $hexdtime;                 // last mod time & date
		$cdrec .= pack('V', $crc);           // crc32
		$cdrec .= pack('V', $c_len);         // compressed filesize
		$cdrec .= pack('V', $unc_len);       // uncompressed filesize
		$cdrec .= pack('v', strlen($name)); // length of filename
		$cdrec .= pack('v', 0);             // extra field length
		$cdrec .= pack('v', 0);             // file comment length
		$cdrec .= pack('v', 0);             // disk number start
		$cdrec .= pack('v', 0);             // internal file attributes
		$cdrec .= pack('V', $attrib);       // external file attributes
		$cdrec .= pack('V', $this->old_offset); // relative offset of local header
		$cdrec .= $name;

		// Save to central directory
		$this->ctrl_dir[] = $cdrec;

		$this->old_offset = $this->datasec_len;
	}

	function file()
	{
		$ctrldir = implode('', $this->ctrl_dir);

		return $ctrldir . $this->eof_ctrl_dir .
			pack('v', sizeof($this->ctrl_dir)) .	// total # of entries "on this disk"
			pack('v', sizeof($this->ctrl_dir)) .	// total # of entries overall
			pack('V', strlen($ctrldir)) .			// size of central dir
			pack('V', $this->datasec_len) .			// offset to start of central dir
			"\x00\x00";								// .zip file comment length
	}
}

// Tar/tar.gz compression routine
// Header/checksum creation derived from tarfile.pl, © Tom Horsley, 1994
class compress_tar extends compress 
{
	var $fzopen = '';
	var $fzclose = '';
	var $fzread = '';
	var $fzwrite = '';
	var $isgz = false;
	var $isbz = false;

	function compress_tar($mode, $file)
	{
		$this->isgz = (strpos($file, '.tar.gz') !== false || strpos($file, '.tgz') !== false) ? true : false;
		$this->isbz = (strpos($file, '.tar.bz2') !== false) ? true : false;

		$fzopen = ($this->isbz && function_exists('bzopen')) ? 'bzopen' : (($this->isgz && extension_loaded('zlib')) ? 'gzopen' : 'fopen');
		return $this->fp = @$fzopen($phpbb_root_path . $file, $mode . 'b');
	}

	function extract($dst)
	{
		$fzread = ($this->isbz && function_exists('bzread')) ? 'bzread' : (($this->isgz && extension_loaded('zlib')) ? 'gzread' : 'fread');

		$header = $data = '';
		$size = 0;
		while ($buffer = $fzread($this->fp, 512))
		{
			$tmp = unpack("A6magic", substr($buffer, 257, 6));

			if (trim($tmp['magic']) == 'ustar')
			{
				$tmp = unpack("A100name", $buffer);
				$filename = trim($tmp['name']);

				$tmp = unpack("Atype", substr($buffer, 156, 1));
				$filetype = (int) trim($tmp['type']);

				if ($filetype == 5)
				{
					mkdir($dst . $filename);
					continue;
				}
				else
				{
					$tmp = unpack("A12size", substr($buffer, 124, 12));
					$filesize = octdec((int) trim($tmp['size']));
	
					if (!($fp = fopen($dst . $filename, 'wb')))
					{
						trigger_error('Could not open file for output');
					}

					$size = 0;
					continue;
				}
			}

			$size += 512;
			$length = ($size > $filesize) ? 512 - ($size - $filesize) : 512;

			$tmp = unpack("A512data", $buffer);

			fwrite($fp, (string) $tmp['data'], $length);
			unset($buffer);
		}
	}

	function close()
	{
		$fzclose = ($this->isbz && function_exists('bzclose')) ? 'bzclose' : (($this->isgz && extension_loaded('zlib')) ? 'gzclose' : 'fclose');
		$fzclose($this->fp);
	}

	function data($name, $data, $mtime = false, $is_dir = false)
	{
		$fzwrite = 	($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');

		$mode = ($is_dir) ? '493' : '436';
		$mtime = (!$mtime) ? time() : $mtime;
		$filesize = ($is_dir) ? 0 : strlen($data);
		$typeflag = ($is_dir) ? '5' : '';

		$header = '';
		$header .= pack("a100", $name);
		$header .= pack("a8", sprintf("%07o", $mode));
		$header .= pack("a8", sprintf("%07o", 0));
		$header .= pack("a8", sprintf("%07o", 0));
		$header .= pack("a12", sprintf("%011o", $filesize));
		$header .= pack("a12", sprintf("%011o", $mtime));
		$header .= '        ';
		$header .= pack("a", $typeflag);
		$header .= pack("a100", '');
		$header .= 'ustar';
		$header .= pack("x");
		$header .= '00';
		$header .= pack("x247");

		// Checksum
		for ($i = 0; $i < 512; $i++)
		{
			$b = unpack("c1char", substr($header, $i, 1));
			$checksum += $b['char'];
		}
		$header = substr_replace($header, pack("a8",sprintf("%07o", $checksum)), 148, 8);

		$fzwrite($this->fp, $header);

		$i = 0;
		// Read the data 512 bytes at a time and write it out
		while ($buffer = substr($data, $i, 512))
		{
			$fzwrite($this->fp, pack("a512", $buffer));
			$i += 512;
		}
		unset($data);
	}
}

?>