5
* @version $Id: functions_compress.php,v 1.47 2007/10/05 14:30:10 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
20
* Class for handling archives (compression/decompression)
30
function add_file($src, $src_rm_prefix = '', $src_add_prefix = '', $skip_files = '')
32
global $phpbb_root_path;
34
$skip_files = explode(',', $skip_files);
36
// Remove rm prefix from src path
37
$src_path = ($src_rm_prefix) ? preg_replace('#^(' . preg_quote($src_rm_prefix, '#') . ')#', '', $src) : $src;
39
$src_path = ($src_add_prefix) ? ($src_add_prefix . ((substr($src_add_prefix, -1) != '/') ? '/' : '') . $src_path) : $src_path;
40
// Remove initial "/" if present
41
$src_path = (substr($src_path, 0, 1) == '/') ? substr($src_path, 1) : $src_path;
43
if (is_file($phpbb_root_path . $src))
45
$this->data($src_path, file_get_contents("$phpbb_root_path$src"), false, stat("$phpbb_root_path$src"));
47
else if (is_dir($phpbb_root_path . $src))
49
// Clean up path, add closing / if not present
50
$src_path = ($src_path && substr($src_path, -1) != '/') ? $src_path . '/' : $src_path;
53
$filelist = filelist("$phpbb_root_path$src", '', '*');
58
$this->data($src_path, '', true, stat("$phpbb_root_path$src"));
61
foreach ($filelist as $path => $file_ary)
65
// Same as for src_path
66
$path = (substr($path, 0, 1) == '/') ? substr($path, 1) : $path;
67
$path = ($path && substr($path, -1) != '/') ? $path . '/' : $path;
69
$this->data("$src_path$path", '', true, stat("$phpbb_root_path$src$path"));
72
foreach ($file_ary as $file)
74
if (in_array($path . $file, $skip_files))
79
$this->data("$src_path$path$file", file_get_contents("$phpbb_root_path$src$path$file"), false, stat("$phpbb_root_path$src$path$file"));
88
* Add custom file (the filepath will not be adjusted)
90
function add_custom_file($src, $filename)
92
$this->data($filename, file_get_contents($src), false, stat($src));
99
function add_data($src, $name)
102
$stat[2] = 436; //384
103
$stat[4] = $stat[5] = 0;
104
$stat[7] = strlen($src);
106
$this->data($name, $src, false, $stat);
111
* Return available methods
115
$methods = array('.tar');
116
$available_methods = array('.tar.gz' => 'zlib', '.tar.bz2' => 'bz2', '.zip' => 'zlib');
118
foreach ($available_methods as $type => $module)
120
if (!@extension_loaded($module))
132
* Zip creation class from phpMyAdmin 2.3.0 (c) Tobias Ratschiller, Olivier M�ller, Lo�c Chapeaux,
133
* Marc Delisle, http://www.phpmyadmin.net/
135
* Zip extraction function by Alexandre Tedeschi, alexandrebr at gmail dot com
137
* Modified extensively by psoTFX and DavidMJ, (c) phpBB Group, 2003
139
* Based on work by Eric Mueller and Denis125
140
* Official ZIP file format: http://www.pkware.com/appnote.txt
144
class compress_zip extends compress
146
var $datasec = array();
147
var $ctrl_dir = array();
148
var $eof_cdh = "\x50\x4b\x05\x06\x00\x00\x00\x00";
151
var $datasec_len = 0;
156
function compress_zip($mode, $file)
158
return $this->fp = @fopen($file, $mode . 'b');
162
* Convert unix to dos time
164
function unix_to_dos_time($time)
166
$timearray = (!$time) ? getdate() : getdate($time);
168
if ($timearray['year'] < 1980)
170
$timearray['year'] = 1980;
171
$timearray['mon'] = $timearray['mday'] = 1;
172
$timearray['hours'] = $timearray['minutes'] = $timearray['seconds'] = 0;
175
return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
181
function extract($dst)
183
// Loop the file, looking for files and folders
187
while (!feof($this->fp))
189
// Check if the signature is valid...
190
$signature = fread($this->fp, 4);
194
// 'Local File Header'
195
case "\x50\x4b\x03\x04":
196
// Lets get everything we need.
197
// We don't store the version needed to extract, the general purpose bit flag or the date and time fields
198
$data = unpack("@4/vc_method/@10/Vcrc/Vc_size/Vuc_size/vname_len/vextra_field", fread($this->fp, 26));
199
$file_name = fread($this->fp, $data['name_len']); // filename
201
if ($data['extra_field'])
203
fread($this->fp, $data['extra_field']); // extra field
206
$target_filename = "$dst$file_name";
208
if (!$data['uc_size'] && !$data['crc'] && substr($file_name, -1, 1) == '/')
210
if (!is_dir($target_filename))
213
$folders = explode('/', $target_filename);
215
// Create and folders and subfolders if they do not exist
216
foreach ($folders as $folder)
218
$str = (!empty($str)) ? $str . '/' . $folder : $folder;
221
if (!@mkdir($str, 0777))
223
trigger_error("Could not create directory $folder");
229
// This is a directory, we are not writting files
234
// Some archivers are punks, they don't don't include folders in their archives!
236
$folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
238
// Create and folders and subfolders if they do not exist
239
foreach ($folders as $folder)
241
$str = (!empty($str)) ? $str . '/' . $folder : $folder;
244
if (!@mkdir($str, 0777))
246
trigger_error("Could not create directory $folder");
253
if (!$data['uc_size'])
259
$content = fread($this->fp, $data['c_size']);
262
$fp = fopen($target_filename, "w");
264
switch ($data['c_method'])
268
fwrite($fp, $content);
273
fwrite($fp, gzinflate($content, $data['uc_size']));
278
fwrite($fp, bzdecompress($content));
285
// We hit the 'Central Directory Header', we can stop because nothing else in here requires our attention
286
// 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
287
case "\x50\x4b\x01\x02":
288
// This case should simply never happen.. but it does exist..
289
case "\x50\x4b\x05\x06":
292
// 'Packed to Removable Disk', ignore it and look for the next signature...
296
// We have encountered a header that is weird. Lets look for better data...
300
// Unexpected header. Trying to detect wrong placed 'Data Descriptor';
302
fseek($this->fp, 8, SEEK_CUR); // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
305
trigger_error("Unexpected header, ending loop");
318
// Write out central file directory and footer ... if it exists
319
if (sizeof($this->ctrl_dir))
321
fwrite($this->fp, $this->file());
327
* Create the structures ... note we assume version made by is MSDOS
329
function data($name, $data, $is_dir = false, $stat)
331
$name = str_replace('\\', '/', $name);
333
$hexdtime = pack('V', $this->unix_to_dos_time($stat[9]));
337
$unc_len = $c_len = $crc = 0;
343
$unc_len = strlen($data);
345
$zdata = gzdeflate($data);
346
$c_len = strlen($zdata);
349
// Did we compress? No, then use data as is
350
if ($c_len >= $unc_len)
359
// If we didn't compress set method to store, else deflate
360
$c_method = ($c_len == $unc_len) ? "\x00\x00" : "\x08\x00";
362
// Are we a file or a directory? Set archive for file
363
$attrib = ($is_dir) ? 16 : 32;
365
// File Record Header
366
$fr = "\x50\x4b\x03\x04"; // Local file header 4bytes
367
$fr .= pack('v', $var_ext); // ver needed to extract 2bytes
368
$fr .= "\x00\x00"; // gen purpose bit flag 2bytes
369
$fr .= $c_method; // compression method 2bytes
370
$fr .= $hexdtime; // last mod time and date 2+2bytes
371
$fr .= pack('V', $crc); // crc32 4bytes
372
$fr .= pack('V', $c_len); // compressed filesize 4bytes
373
$fr .= pack('V', $unc_len); // uncompressed filesize 4bytes
374
$fr .= pack('v', strlen($name));// length of filename 2bytes
376
$fr .= pack('v', 0); // extra field length 2bytes
381
$this->datasec_len += strlen($fr);
383
// Add data to file ... by writing data out incrementally we save some memory
384
fwrite($this->fp, $fr);
387
// Central Directory Header
388
$cdrec = "\x50\x4b\x01\x02"; // header 4bytes
389
$cdrec .= "\x00\x00"; // version made by
390
$cdrec .= pack('v', $var_ext); // version needed to extract
391
$cdrec .= "\x00\x00"; // gen purpose bit flag
392
$cdrec .= $c_method; // compression method
393
$cdrec .= $hexdtime; // last mod time & date
394
$cdrec .= pack('V', $crc); // crc32
395
$cdrec .= pack('V', $c_len); // compressed filesize
396
$cdrec .= pack('V', $unc_len); // uncompressed filesize
397
$cdrec .= pack('v', strlen($name)); // length of filename
398
$cdrec .= pack('v', 0); // extra field length
399
$cdrec .= pack('v', 0); // file comment length
400
$cdrec .= pack('v', 0); // disk number start
401
$cdrec .= pack('v', 0); // internal file attributes
402
$cdrec .= pack('V', $attrib); // external file attributes
403
$cdrec .= pack('V', $this->old_offset); // relative offset of local header
406
// Save to central directory
407
$this->ctrl_dir[] = $cdrec;
409
$this->old_offset = $this->datasec_len;
417
$ctrldir = implode('', $this->ctrl_dir);
419
return $ctrldir . $this->eof_cdh .
420
pack('v', sizeof($this->ctrl_dir)) . // total # of entries "on this disk"
421
pack('v', sizeof($this->ctrl_dir)) . // total # of entries overall
422
pack('V', strlen($ctrldir)) . // size of central dir
423
pack('V', $this->datasec_len) . // offset to start of central dir
424
"\x00\x00"; // .zip file comment length
430
function download($filename, $download_name = false)
432
global $phpbb_root_path;
434
if ($download_name === false)
436
$download_name = $filename;
439
$mimetype = 'application/zip';
441
header('Pragma: no-cache');
442
header("Content-Type: $mimetype; name=\"$download_name.zip\"");
443
header("Content-disposition: attachment; filename=$download_name.zip");
445
$fp = @fopen("{$phpbb_root_path}store/$filename.zip", 'rb');
448
while ($buffer = fread($fp, 1024))
458
* Tar/tar.gz compression routine
459
* Header/checksum creation derived from tarfile.pl, (c) Tom Horsley, 1994
463
class compress_tar extends compress
475
function compress_tar($mode, $file, $type = '')
477
$type = (!$type) ? $file : $type;
478
$this->isgz = (strpos($type, '.tar.gz') !== false || strpos($type, '.tgz') !== false) ? true : false;
479
$this->isbz = (strpos($type, '.tar.bz2') !== false) ? true : false;
481
$this->mode = &$mode;
482
$this->file = &$file;
483
$this->type = &$type;
490
function extract($dst)
492
$fzread = ($this->isbz && function_exists('bzread')) ? 'bzread' : (($this->isgz && @extension_loaded('zlib')) ? 'gzread' : 'fread');
494
// Run through the file and grab directory entries
495
while ($buffer = $fzread($this->fp, 512))
497
$tmp = unpack('A6magic', substr($buffer, 257, 6));
499
if (trim($tmp['magic']) == 'ustar')
501
$tmp = unpack('A100name', $buffer);
502
$filename = trim($tmp['name']);
504
$tmp = unpack('Atype', substr($buffer, 156, 1));
505
$filetype = (int) trim($tmp['type']);
507
$tmp = unpack('A12size', substr($buffer, 124, 12));
508
$filesize = octdec((int) trim($tmp['size']));
512
if (!is_dir("$dst$filename"))
515
$folders = explode('/', "$dst$filename");
517
// Create and folders and subfolders if they do not exist
518
foreach ($folders as $folder)
520
$str = (!empty($str)) ? $str . '/' . $folder : $folder;
523
if (!@mkdir($str, 0777))
525
trigger_error("Could not create directory $folder");
532
else if ($filesize != 0 && ($filetype == 0 || $filetype == "\0"))
534
// Write out the files
535
if (!($fp = fopen("$dst$filename", 'wb')))
537
trigger_error("Couldn't create file $filename");
539
@chmod("$dst$filename", 0777);
541
// Grab the file contents
542
fwrite($fp, $fzread($this->fp, ($filesize + 511) &~ 511), $filesize);
554
$fzclose = ($this->isbz && function_exists('bzclose')) ? 'bzclose' : (($this->isgz && @extension_loaded('zlib')) ? 'gzclose' : 'fclose');
558
$fzwrite = ($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
560
// The end of a tar archive ends in two records of all NULLs (1024 bytes of \0)
561
$fzwrite($this->fp, str_repeat("\0", 1024));
568
* Create the structures
570
function data($name, $data, $is_dir = false, $stat)
573
$fzwrite = ($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
575
$typeflag = ($is_dir) ? '5' : '';
577
// This is the header data, it contains all the info we know about the file or folder that we are about to archive
579
$header .= pack('a100', $name); // file name
580
$header .= pack('a8', sprintf("%07o", $stat[2])); // file mode
581
$header .= pack('a8', sprintf("%07o", $stat[4])); // owner id
582
$header .= pack('a8', sprintf("%07o", $stat[5])); // group id
583
$header .= pack('a12', sprintf("%011o", $stat[7])); // file size
584
$header .= pack('a12', sprintf("%011o", $stat[9])); // last mod time
588
for ($i = 0; $i < 148; $i++)
590
$checksum += ord($header[$i]);
593
// 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
594
$checksum += 2415 + (($is_dir) ? 53 : 0);
596
$header .= pack('a8', sprintf("%07o", $checksum)); // checksum
597
$header .= pack('a1', $typeflag); // link indicator
598
$header .= pack('a100', ''); // name of linked file
599
$header .= pack('a6', 'ustar'); // ustar indicator
600
$header .= pack('a2', '00'); // ustar version
601
$header .= pack('a32', 'Unknown'); // owner name
602
$header .= pack('a32', 'Unknown'); // group name
603
$header .= pack('a8', ''); // device major number
604
$header .= pack('a8', ''); // device minor number
605
$header .= pack('a155', ''); // filename prefix
606
$header .= pack('a12', ''); // end
608
// This writes the entire file in one shot. Header, followed by data and then null padded to a multiple of 512
609
$fzwrite($this->fp, $header . (($stat[7] !== 0 && !$is_dir) ? $data . str_repeat("\0", (($stat[7] + 511) &~ 511) - $stat[7]) : ''));
618
$fzopen = ($this->isbz && function_exists('bzopen')) ? 'bzopen' : (($this->isgz && @extension_loaded('zlib')) ? 'gzopen' : 'fopen');
619
$this->fp = @$fzopen($this->file, $this->mode . (($fzopen == 'bzopen') ? '' : 'b') . (($fzopen == 'gzopen') ? '9' : ''));
623
trigger_error('Unable to open file ' . $this->file . ' [' . $fzopen . ' - ' . $this->mode . 'b]');
630
function download($filename, $download_name = false)
632
global $phpbb_root_path;
634
if ($download_name === false)
636
$download_name = $filename;
642
$mimetype = 'application/x-tar';
646
$mimetype = 'application/x-gzip';
650
$mimetype = 'application/x-bzip2';
654
$mimetype = 'application/octet-stream';
658
header('Pragma: no-cache');
659
header("Content-Type: $mimetype; name=\"$download_name$this->type\"");
660
header("Content-disposition: attachment; filename=$download_name$this->type");
662
$fp = @fopen("{$phpbb_root_path}store/$filename$this->type", 'rb');
665
while ($buffer = fread($fp, 1024))
b'\\ No newline at end of file'