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 $this->fp
= @fopen
($file, $mode . 'b');
162 trigger_error('Unable to open file ' . $file . ' [' . $mode . 'b]');
167 * Convert unix to dos time
169 function unix_to_dos_time($time)
171 $timearray = (!$time) ?
getdate() : getdate($time);
173 if ($timearray['year'] < 1980)
175 $timearray['year'] = 1980;
176 $timearray['mon'] = $timearray['mday'] = 1;
177 $timearray['hours'] = $timearray['minutes'] = $timearray['seconds'] = 0;
180 return (($timearray['year'] - 1980) << 25) |
($timearray['mon'] << 21) |
($timearray['mday'] << 16) |
($timearray['hours'] << 11) |
($timearray['minutes'] << 5) |
($timearray['seconds'] >> 1);
186 function extract($dst)
188 // Loop the file, looking for files and folders
192 while (!feof($this->fp
))
194 // Check if the signature is valid...
195 $signature = fread($this->fp
, 4);
199 // 'Local File Header'
200 case "\x50\x4b\x03\x04":
201 // Lets get everything we need.
202 // We don't store the version needed to extract, the general purpose bit flag or the date and time fields
203 $data = unpack("@4/vc_method/@10/Vcrc/Vc_size/Vuc_size/vname_len/vextra_field", fread($this->fp
, 26));
204 $file_name = fread($this->fp
, $data['name_len']); // filename
206 if ($data['extra_field'])
208 fread($this->fp
, $data['extra_field']); // extra field
211 $target_filename = "$dst$file_name";
213 if (!$data['uc_size'] && !$data['crc'] && substr($file_name, -1, 1) == '/')
215 if (!is_dir($target_filename))
218 $folders = explode('/', $target_filename);
220 // Create and folders and subfolders if they do not exist
221 foreach ($folders as $folder)
223 $folder = trim($folder);
229 $str = (!empty($str)) ?
$str . '/' . $folder : $folder;
232 if (!@mkdir
($str, 0777))
234 trigger_error("Could not create directory $folder");
236 phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE
);
240 // This is a directory, we are not writting files
245 // Some archivers are punks, they don't include folders in their archives!
247 $folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME
));
249 // Create and folders and subfolders if they do not exist
250 foreach ($folders as $folder)
252 $folder = trim($folder);
258 $str = (!empty($str)) ?
$str . '/' . $folder : $folder;
261 if (!@mkdir
($str, 0777))
263 trigger_error("Could not create directory $folder");
265 phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE
);
270 if (!$data['uc_size'])
276 $content = fread($this->fp
, $data['c_size']);
279 $fp = fopen($target_filename, "w");
281 switch ($data['c_method'])
285 fwrite($fp, $content);
290 fwrite($fp, gzinflate($content, $data['uc_size']));
295 fwrite($fp, bzdecompress($content));
302 // We hit the 'Central Directory Header', we can stop because nothing else in here requires our attention
303 // 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
304 case "\x50\x4b\x01\x02":
305 // This case should simply never happen.. but it does exist..
306 case "\x50\x4b\x05\x06":
309 // 'Packed to Removable Disk', ignore it and look for the next signature...
313 // We have encountered a header that is weird. Lets look for better data...
317 // Unexpected header. Trying to detect wrong placed 'Data Descriptor';
319 fseek($this->fp
, 8, SEEK_CUR
); // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
322 trigger_error("Unexpected header, ending loop");
335 // Write out central file directory and footer ... if it exists
336 if (sizeof($this->ctrl_dir
))
338 fwrite($this->fp
, $this->file());
344 * Create the structures ... note we assume version made by is MSDOS
346 function data($name, $data, $is_dir = false, $stat)
348 $name = str_replace('\\', '/', $name);
350 $hexdtime = pack('V', $this->unix_to_dos_time($stat[9]));
354 $unc_len = $c_len = $crc = 0;
360 $unc_len = strlen($data);
362 $zdata = gzdeflate($data);
363 $c_len = strlen($zdata);
366 // Did we compress? No, then use data as is
367 if ($c_len >= $unc_len)
376 // If we didn't compress set method to store, else deflate
377 $c_method = ($c_len == $unc_len) ?
"\x00\x00" : "\x08\x00";
379 // Are we a file or a directory? Set archive for file
380 $attrib = ($is_dir) ?
16 : 32;
382 // File Record Header
383 $fr = "\x50\x4b\x03\x04"; // Local file header 4bytes
384 $fr .= pack('v', $var_ext); // ver needed to extract 2bytes
385 $fr .= "\x00\x00"; // gen purpose bit flag 2bytes
386 $fr .= $c_method; // compression method 2bytes
387 $fr .= $hexdtime; // last mod time and date 2+2bytes
388 $fr .= pack('V', $crc); // crc32 4bytes
389 $fr .= pack('V', $c_len); // compressed filesize 4bytes
390 $fr .= pack('V', $unc_len); // uncompressed filesize 4bytes
391 $fr .= pack('v', strlen($name));// length of filename 2bytes
393 $fr .= pack('v', 0); // extra field length 2bytes
398 $this->datasec_len +
= strlen($fr);
400 // Add data to file ... by writing data out incrementally we save some memory
401 fwrite($this->fp
, $fr);
404 // Central Directory Header
405 $cdrec = "\x50\x4b\x01\x02"; // header 4bytes
406 $cdrec .= "\x00\x00"; // version made by
407 $cdrec .= pack('v', $var_ext); // version needed to extract
408 $cdrec .= "\x00\x00"; // gen purpose bit flag
409 $cdrec .= $c_method; // compression method
410 $cdrec .= $hexdtime; // last mod time & date
411 $cdrec .= pack('V', $crc); // crc32
412 $cdrec .= pack('V', $c_len); // compressed filesize
413 $cdrec .= pack('V', $unc_len); // uncompressed filesize
414 $cdrec .= pack('v', strlen($name)); // length of filename
415 $cdrec .= pack('v', 0); // extra field length
416 $cdrec .= pack('v', 0); // file comment length
417 $cdrec .= pack('v', 0); // disk number start
418 $cdrec .= pack('v', 0); // internal file attributes
419 $cdrec .= pack('V', $attrib); // external file attributes
420 $cdrec .= pack('V', $this->old_offset
); // relative offset of local header
423 // Save to central directory
424 $this->ctrl_dir
[] = $cdrec;
426 $this->old_offset
= $this->datasec_len
;
434 $ctrldir = implode('', $this->ctrl_dir
);
436 return $ctrldir . $this->eof_cdh
.
437 pack('v', sizeof($this->ctrl_dir
)) . // total # of entries "on this disk"
438 pack('v', sizeof($this->ctrl_dir
)) . // total # of entries overall
439 pack('V', strlen($ctrldir)) . // size of central dir
440 pack('V', $this->datasec_len
) . // offset to start of central dir
441 "\x00\x00"; // .zip file comment length
447 function download($filename, $download_name = false)
449 global $phpbb_root_path;
451 if ($download_name === false)
453 $download_name = $filename;
456 $mimetype = 'application/zip';
458 header('Pragma: no-cache');
459 header("Content-Type: $mimetype; name=\"$download_name.zip\"");
460 header("Content-disposition: attachment; filename=$download_name.zip");
462 $fp = @fopen
("{$phpbb_root_path}store/$filename.zip", 'rb');
465 while ($buffer = fread($fp, 1024))
475 * Tar/tar.gz compression routine
476 * Header/checksum creation derived from tarfile.pl, (c) Tom Horsley, 1994
480 class compress_tar
extends compress
492 function compress_tar($mode, $file, $type = '')
494 $type = (!$type) ?
$file : $type;
495 $this->isgz
= (strpos($type, '.tar.gz') !== false ||
strpos($type, '.tgz') !== false) ?
true : false;
496 $this->isbz
= (strpos($type, '.tar.bz2') !== false) ?
true : false;
498 $this->mode
= &$mode;
499 $this->file
= &$file;
500 $this->type
= &$type;
507 function extract($dst)
509 $fzread = ($this->isbz
&& function_exists('bzread')) ?
'bzread' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzread' : 'fread');
511 // Run through the file and grab directory entries
512 while ($buffer = $fzread($this->fp
, 512))
514 $tmp = unpack('A6magic', substr($buffer, 257, 6));
516 if (trim($tmp['magic']) == 'ustar')
518 $tmp = unpack('A100name', $buffer);
519 $filename = trim($tmp['name']);
521 $tmp = unpack('Atype', substr($buffer, 156, 1));
522 $filetype = (int) trim($tmp['type']);
524 $tmp = unpack('A12size', substr($buffer, 124, 12));
525 $filesize = octdec((int) trim($tmp['size']));
527 $target_filename = "$dst$filename";
531 if (!is_dir($target_filename))
534 $folders = explode('/', $target_filename);
536 // Create and folders and subfolders if they do not exist
537 foreach ($folders as $folder)
539 $folder = trim($folder);
545 $str = (!empty($str)) ?
$str . '/' . $folder : $folder;
548 if (!@mkdir
($str, 0777))
550 trigger_error("Could not create directory $folder");
552 phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE
);
557 else if ($filesize >= 0 && ($filetype == 0 ||
$filetype == "\0"))
559 // Some archivers are punks, they don't properly order the folders in their archives!
561 $folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME
));
563 // Create and folders and subfolders if they do not exist
564 foreach ($folders as $folder)
566 $folder = trim($folder);
572 $str = (!empty($str)) ?
$str . '/' . $folder : $folder;
575 if (!@mkdir
($str, 0777))
577 trigger_error("Could not create directory $folder");
579 phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE
);
583 // Write out the files
584 if (!($fp = fopen($target_filename, 'wb')))
586 trigger_error("Couldn't create file $filename");
588 phpbb_chmod($target_filename, CHMOD_READ
);
590 // Grab the file contents
591 fwrite($fp, ($filesize) ?
$fzread($this->fp
, ($filesize +
511) &~
511) : '', $filesize);
603 $fzclose = ($this->isbz
&& function_exists('bzclose')) ?
'bzclose' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzclose' : 'fclose');
607 $fzwrite = ($this->isbz
&& function_exists('bzwrite')) ?
'bzwrite' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzwrite' : 'fwrite');
609 // The end of a tar archive ends in two records of all NULLs (1024 bytes of \0)
610 $fzwrite($this->fp
, str_repeat("\0", 1024));
617 * Create the structures
619 function data($name, $data, $is_dir = false, $stat)
622 $fzwrite = ($this->isbz
&& function_exists('bzwrite')) ?
'bzwrite' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzwrite' : 'fwrite');
624 $typeflag = ($is_dir) ?
'5' : '';
626 // This is the header data, it contains all the info we know about the file or folder that we are about to archive
628 $header .= pack('a100', $name); // file name
629 $header .= pack('a8', sprintf("%07o", $stat[2])); // file mode
630 $header .= pack('a8', sprintf("%07o", $stat[4])); // owner id
631 $header .= pack('a8', sprintf("%07o", $stat[5])); // group id
632 $header .= pack('a12', sprintf("%011o", $stat[7])); // file size
633 $header .= pack('a12', sprintf("%011o", $stat[9])); // last mod time
637 for ($i = 0; $i < 148; $i++
)
639 $checksum +
= ord($header[$i]);
642 // 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
643 $checksum +
= 2415 +
(($is_dir) ?
53 : 0);
645 $header .= pack('a8', sprintf("%07o", $checksum)); // checksum
646 $header .= pack('a1', $typeflag); // link indicator
647 $header .= pack('a100', ''); // name of linked file
648 $header .= pack('a6', 'ustar'); // ustar indicator
649 $header .= pack('a2', '00'); // ustar version
650 $header .= pack('a32', 'Unknown'); // owner name
651 $header .= pack('a32', 'Unknown'); // group name
652 $header .= pack('a8', ''); // device major number
653 $header .= pack('a8', ''); // device minor number
654 $header .= pack('a155', ''); // filename prefix
655 $header .= pack('a12', ''); // end
657 // This writes the entire file in one shot. Header, followed by data and then null padded to a multiple of 512
658 $fzwrite($this->fp
, $header . (($stat[7] !== 0 && !$is_dir) ?
$data . str_repeat("\0", (($stat[7] +
511) &~
511) - $stat[7]) : ''));
667 $fzopen = ($this->isbz
&& function_exists('bzopen')) ?
'bzopen' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzopen' : 'fopen');
668 $this->fp
= @$fzopen($this->file
, $this->mode
. (($fzopen == 'bzopen') ?
'' : 'b') . (($fzopen == 'gzopen') ?
'9' : ''));
672 trigger_error('Unable to open file ' . $this->file
. ' [' . $fzopen . ' - ' . $this->mode
. 'b]');
679 function download($filename, $download_name = false)
681 global $phpbb_root_path;
683 if ($download_name === false)
685 $download_name = $filename;
691 $mimetype = 'application/x-tar';
695 $mimetype = 'application/x-gzip';
699 $mimetype = 'application/x-bzip2';
703 $mimetype = 'application/octet-stream';
707 header('Pragma: no-cache');
708 header("Content-Type: $mimetype; name=\"$download_name$this->type\"");
709 header("Content-disposition: attachment; filename=$download_name$this->type");
711 $fp = @fopen
("{$phpbb_root_path}store/$filename$this->type", 'rb');
714 while ($buffer = fread($fp, 1024))