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 public function add_file($src, $src_rm_prefix = '', $src_add_prefix = '', $skip_files = '')
32 $skip_files = explode(',', $skip_files);
34 // Remove rm prefix from src path
35 $src_path = ($src_rm_prefix) ?
preg_replace('#^(' . preg_quote($src_rm_prefix, '#') . ')#', '', $src) : $src;
37 $src_path = ($src_add_prefix) ?
($src_add_prefix . ((substr($src_add_prefix, -1) != '/') ?
'/' : '') . $src_path) : $src_path;
38 // Remove initial "/" if present
39 $src_path = (substr($src_path, 0, 1) == '/') ?
substr($src_path, 1) : $src_path;
41 if (is_file(PHPBB_ROOT_PATH
. $src))
43 $this->data($src_path, file_get_contents(PHPBB_ROOT_PATH
. $src), false, stat(PHPBB_ROOT_PATH
. $src));
45 else if (is_dir(PHPBB_ROOT_PATH
. $src))
47 // Clean up path, add closing / if not present
48 $src_path = ($src_path && substr($src_path, -1) != '/') ?
$src_path . '/' : $src_path;
51 $filelist = filelist(PHPBB_ROOT_PATH
. $src, '', '*');
56 $this->data($src_path, '', true, stat(PHPBB_ROOT_PATH
. $src));
59 foreach ($filelist as $path => $file_ary)
63 // Same as for src_path
64 $path = (substr($path, 0, 1) == '/') ?
substr($path, 1) : $path;
65 $path = ($path && substr($path, -1) != '/') ?
$path . '/' : $path;
67 $this->data("$src_path$path", '', true, stat(PHPBB_ROOT_PATH
. $src . $path));
70 foreach ($file_ary as $file)
72 if (in_array($path . $file, $skip_files))
77 $this->data("$src_path$path$file", file_get_contents(PHPBB_ROOT_PATH
. $src . $path . $file), false, stat(PHPBB_ROOT_PATH
. $src . $path . $file));
86 * Add custom file (the filepath will not be adjusted)
88 public function add_custom_file($src, $filename)
90 $this->data($filename, file_get_contents($src), false, stat($src));
97 public function add_data($src, $name)
100 $stat[2] = 436; //384
101 $stat[4] = $stat[5] = 0;
102 $stat[7] = strlen($src);
104 $this->data($name, $src, false, $stat);
109 * Return available methods
111 public static function methods()
113 $methods = array('.tar');
114 $available_methods = array('.tar.gz' => 'zlib', '.tar.bz2' => 'bz2', '.zip' => 'zlib');
116 foreach ($available_methods as $type => $module)
118 if (!@extension_loaded
($module))
130 * Zip creation class from phpMyAdmin 2.3.0 (c) Tobias Ratschiller, Olivier Müller, Loïc Chapeaux,
131 * Marc Delisle, http://www.phpmyadmin.net/
133 * Zip extraction function by Alexandre Tedeschi, alexandrebr at gmail dot com
135 * Modified extensively by psoTFX and DavidMJ, (c) phpBB Group, 2003
137 * Based on work by Eric Mueller and Denis125
138 * Official ZIP file format: http://www.pkware.com/appnote.txt
142 class compress_zip
extends compress
144 private $datasec = array();
145 private $ctrl_dir = array();
146 const eof_cdh
= "\x50\x4b\x05\x06\x00\x00\x00\x00";
148 private $old_offset = 0;
149 private $datasec_len = 0;
154 function __construct($mode, $file)
156 return $this->fp
= @fopen
($file, $mode . 'b');
160 * Convert unix to dos time
162 private static function unix_to_dos_time($time)
164 $timearray = (!$time) ?
getdate() : getdate($time);
166 if ($timearray['year'] < 1980)
168 $timearray['year'] = 1980;
169 $timearray['mon'] = $timearray['mday'] = 1;
170 $timearray['hours'] = $timearray['minutes'] = $timearray['seconds'] = 0;
173 return (($timearray['year'] - 1980) << 25) |
($timearray['mon'] << 21) |
($timearray['mday'] << 16) |
($timearray['hours'] << 11) |
($timearray['minutes'] << 5) |
($timearray['seconds'] >> 1);
179 public function extract($dst)
181 // Loop the file, looking for files and folders
185 while (!feof($this->fp
))
187 // Check if the signature is valid...
188 $signature = fread($this->fp
, 4);
192 // 'Local File Header'
193 case "\x50\x4b\x03\x04":
194 // Lets get everything we need.
195 // We don't store the version needed to extract, the general purpose bit flag or the date and time fields
196 $data = unpack("@4/vc_method/@10/Vcrc/Vc_size/Vuc_size/vname_len/vextra_field", fread($this->fp
, 26));
197 $file_name = fread($this->fp
, $data['name_len']); // filename
199 if ($data['extra_field'])
201 fread($this->fp
, $data['extra_field']); // extra field
204 $target_filename = "$dst$file_name";
206 if (!$data['uc_size'] && !$data['crc'] && substr($file_name, -1, 1) == '/')
208 if (!is_dir($target_filename))
211 $folders = explode('/', $target_filename);
213 // Create and folders and subfolders if they do not exist
214 foreach ($folders as $folder)
216 $folder = trim($folder);
222 $str = (!empty($str)) ?
$str . '/' . $folder : $folder;
225 if (!@mkdir
($str, 0777))
227 trigger_error("Could not create directory $folder");
229 phpbb
::$system->chmod($str, phpbb
::CHMOD_READ | phpbb
::CHMOD_WRITE
);
233 // This is a directory, we are not writting files
238 // Some archivers are punks, they don't include folders in their archives!
240 $folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME
));
242 // Create and folders and subfolders if they do not exist
243 foreach ($folders as $folder)
245 $folder = trim($folder);
251 $str = (!empty($str)) ?
$str . '/' . $folder : $folder;
254 if (!@mkdir
($str, 0777))
256 trigger_error("Could not create directory $folder");
258 phpbb
::$system->chmod($str, phpbb
::CHMOD_READ | phpbb
::CHMOD_WRITE
);
263 if (!$data['uc_size'])
269 $content = fread($this->fp
, $data['c_size']);
272 $fp = fopen($target_filename, "w");
274 switch ($data['c_method'])
278 fwrite($fp, $content);
283 fwrite($fp, gzinflate($content, $data['uc_size']));
288 fwrite($fp, bzdecompress($content));
295 // We hit the 'Central Directory Header', we can stop because nothing else in here requires our attention
296 // 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
297 case "\x50\x4b\x01\x02":
298 // This case should simply never happen.. but it does exist..
299 case "\x50\x4b\x05\x06":
302 // 'Packed to Removable Disk', ignore it and look for the next signature...
306 // We have encountered a header that is weird. Lets look for better data...
310 // Unexpected header. Trying to detect wrong placed 'Data Descriptor';
312 fseek($this->fp
, 8, SEEK_CUR
); // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
315 trigger_error("Unexpected header, ending loop");
326 public function close()
328 // Write out central file directory and footer ... if it exists
329 if (sizeof($this->ctrl_dir
))
331 fwrite($this->fp
, $this->file());
337 * Create the structures ... note we assume version made by is MSDOS
339 protected function data($name, $data, $is_dir = false, $stat)
341 $name = str_replace('\\', '/', $name);
343 $hexdtime = pack('V', self
::unix_to_dos_time($stat[9]));
347 $unc_len = $c_len = $crc = 0;
353 $unc_len = strlen($data);
355 $zdata = gzdeflate($data);
356 $c_len = strlen($zdata);
359 // Did we compress? No, then use data as is
360 if ($c_len >= $unc_len)
369 // If we didn't compress set method to store, else deflate
370 $c_method = ($c_len == $unc_len) ?
"\x00\x00" : "\x08\x00";
372 // Are we a file or a directory? Set archive for file
373 $attrib = ($is_dir) ?
16 : 32;
375 // File Record Header
376 $fr = "\x50\x4b\x03\x04"; // Local file header 4bytes
377 $fr .= pack('v', $var_ext); // ver needed to extract 2bytes
378 $fr .= "\x00\x00"; // gen purpose bit flag 2bytes
379 $fr .= $c_method; // compression method 2bytes
380 $fr .= $hexdtime; // last mod time and date 2+2bytes
381 $fr .= pack('V', $crc); // crc32 4bytes
382 $fr .= pack('V', $c_len); // compressed filesize 4bytes
383 $fr .= pack('V', $unc_len); // uncompressed filesize 4bytes
384 $fr .= pack('v', strlen($name));// length of filename 2bytes
386 $fr .= pack('v', 0); // extra field length 2bytes
391 $this->datasec_len +
= strlen($fr);
393 // Add data to file ... by writing data out incrementally we save some memory
394 fwrite($this->fp
, $fr);
397 // Central Directory Header
398 $cdrec = "\x50\x4b\x01\x02"; // header 4bytes
399 $cdrec .= "\x00\x00"; // version made by
400 $cdrec .= pack('v', $var_ext); // version needed to extract
401 $cdrec .= "\x00\x00"; // gen purpose bit flag
402 $cdrec .= $c_method; // compression method
403 $cdrec .= $hexdtime; // last mod time & date
404 $cdrec .= pack('V', $crc); // crc32
405 $cdrec .= pack('V', $c_len); // compressed filesize
406 $cdrec .= pack('V', $unc_len); // uncompressed filesize
407 $cdrec .= pack('v', strlen($name)); // length of filename
408 $cdrec .= pack('v', 0); // extra field length
409 $cdrec .= pack('v', 0); // file comment length
410 $cdrec .= pack('v', 0); // disk number start
411 $cdrec .= pack('v', 0); // internal file attributes
412 $cdrec .= pack('V', $attrib); // external file attributes
413 $cdrec .= pack('V', $this->old_offset
); // relative offset of local header
416 // Save to central directory
417 $this->ctrl_dir
[] = $cdrec;
419 $this->old_offset
= $this->datasec_len
;
425 private function file()
427 $ctrldir = implode('', $this->ctrl_dir
);
429 return $ctrldir . self
::eof_cdh
.
430 pack('v', sizeof($this->ctrl_dir
)) . // total # of entries "on this disk"
431 pack('v', sizeof($this->ctrl_dir
)) . // total # of entries overall
432 pack('V', strlen($ctrldir)) . // size of central dir
433 pack('V', $this->datasec_len
) . // offset to start of central dir
434 "\x00\x00"; // .zip file comment length
440 public function download($filename, $download_name = false)
442 if ($download_name === false)
444 $download_name = $filename;
447 $mimetype = 'application/zip';
449 header('Pragma: no-cache');
450 header("Content-Type: $mimetype; name=\"$download_name.zip\"");
451 header("Content-disposition: attachment; filename=$download_name.zip");
453 $fp = @fopen
(PHPBB_ROOT_PATH
. "store/$filename.zip", 'rb');
456 while ($buffer = fread($fp, 1024))
466 * Tar/tar.gz compression routine
467 * Header/checksum creation derived from tarfile.pl, (c) Tom Horsley, 1994
471 class compress_tar
extends compress
473 private $isgz = false;
474 private $isbz = false;
475 private $filename = '';
478 private $wrote = false;
483 function __construct($mode, $file, $type = '')
485 $type = (!$type) ?
$file : $type;
486 $this->isgz
= (strpos($type, '.tar.gz') !== false ||
strpos($type, '.tgz') !== false) ?
true : false;
487 $this->isbz
= (strpos($type, '.tar.bz2') !== false) ?
true : false;
489 $this->mode
= &$mode;
490 $this->file
= &$file;
491 $this->type
= &$type;
498 public function extract($dst)
500 $fzread = ($this->isbz
&& function_exists('bzread')) ?
'bzread' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzread' : 'fread');
502 // Run through the file and grab directory entries
503 while ($buffer = $fzread($this->fp
, 512))
505 $tmp = unpack('A6magic', substr($buffer, 257, 6));
507 if (trim($tmp['magic']) == 'ustar')
509 $tmp = unpack('A100name', $buffer);
510 $filename = trim($tmp['name']);
512 $tmp = unpack('Atype', substr($buffer, 156, 1));
513 $filetype = (int) trim($tmp['type']);
515 $tmp = unpack('A12size', substr($buffer, 124, 12));
516 $filesize = octdec((int) trim($tmp['size']));
518 $target_filename = "$dst$filename";
522 if (!is_dir($target_filename))
525 $folders = explode('/', $target_filename);
527 // Create and folders and subfolders if they do not exist
528 foreach ($folders as $folder)
530 $folder = trim($folder);
536 $str = (!empty($str)) ?
$str . '/' . $folder : $folder;
539 if (!@mkdir
($str, 0777))
541 trigger_error("Could not create directory $folder");
543 phpbb
::$system->chmod($str, phpbb
::CHMOD_READ | phpbb
::CHMOD_WRITE
);
548 else if ($filesize >= 0 && ($filetype == 0 ||
$filetype == "\0"))
550 // Some archivers are punks, they don't properly order the folders in their archives!
552 $folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME
));
554 // Create and folders and subfolders if they do not exist
555 foreach ($folders as $folder)
557 $folder = trim($folder);
563 $str = (!empty($str)) ?
$str . '/' . $folder : $folder;
566 if (!@mkdir
($str, 0777))
568 trigger_error("Could not create directory $folder");
570 phpbb
::$system->chmod($str, phpbb
::CHMOD_READ | phpbb
::CHMOD_WRITE
);
574 // Write out the files
575 if (!($fp = fopen($target_filename, 'wb')))
577 trigger_error("Couldn't create file $filename");
579 phpbb
::$system->chmod($target_filename, phpbb
::CHMOD_READ | phpbb
::CHMOD_WRITE
);
581 // Grab the file contents
582 fwrite($fp, ($filesize) ?
$fzread($this->fp
, ($filesize +
511) &~
511) : '', $filesize);
592 public function close()
594 $fzclose = ($this->isbz
&& function_exists('bzclose')) ?
'bzclose' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzclose' : 'fclose');
598 $fzwrite = ($this->isbz
&& function_exists('bzwrite')) ?
'bzwrite' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzwrite' : 'fwrite');
600 // The end of a tar archive ends in two records of all NULLs (1024 bytes of \0)
601 $fzwrite($this->fp
, str_repeat("\0", 1024));
608 * Create the structures
610 protected function data($name, $data, $is_dir = false, $stat)
613 $fzwrite = ($this->isbz
&& function_exists('bzwrite')) ?
'bzwrite' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzwrite' : 'fwrite');
615 $typeflag = ($is_dir) ?
'5' : '';
617 // This is the header data, it contains all the info we know about the file or folder that we are about to archive
619 $header .= pack('a100', $name); // file name
620 $header .= pack('a8', sprintf("%07o", $stat[2])); // file mode
621 $header .= pack('a8', sprintf("%07o", $stat[4])); // owner id
622 $header .= pack('a8', sprintf("%07o", $stat[5])); // group id
623 $header .= pack('a12', sprintf("%011o", $stat[7])); // file size
624 $header .= pack('a12', sprintf("%011o", $stat[9])); // last mod time
628 for ($i = 0; $i < 148; $i++
)
630 $checksum +
= ord($header[$i]);
633 // 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
634 $checksum +
= 2415 +
(($is_dir) ?
53 : 0);
636 $header .= pack('a8', sprintf("%07o", $checksum)); // checksum
637 $header .= pack('a1', $typeflag); // link indicator
638 $header .= pack('a100', ''); // name of linked file
639 $header .= pack('a6', 'ustar'); // ustar indicator
640 $header .= pack('a2', '00'); // ustar version
641 $header .= pack('a32', 'Unknown'); // owner name
642 $header .= pack('a32', 'Unknown'); // group name
643 $header .= pack('a8', ''); // device major number
644 $header .= pack('a8', ''); // device minor number
645 $header .= pack('a155', ''); // filename prefix
646 $header .= pack('a12', ''); // end
648 // This writes the entire file in one shot. Header, followed by data and then null padded to a multiple of 512
649 $fzwrite($this->fp
, $header . (($stat[7] !== 0 && !$is_dir) ?
$data . str_repeat("\0", (($stat[7] +
511) &~
511) - $stat[7]) : ''));
656 private function open()
658 $fzopen = ($this->isbz
&& function_exists('bzopen')) ?
'bzopen' : (($this->isgz
&& @extension_loaded
('zlib')) ?
'gzopen' : 'fopen');
659 $this->fp
= @$fzopen($this->file
, $this->mode
. (($fzopen == 'bzopen') ?
'' : 'b') . (($fzopen == 'gzopen') ?
'9' : ''));
663 trigger_error('Unable to open file ' . $this->file
. ' [' . $fzopen . ' - ' . $this->mode
. 'b]');
670 public function download($filename, $download_name = false)
672 if ($download_name === false)
674 $download_name = $filename;
680 $mimetype = 'application/x-tar';
684 $mimetype = 'application/x-gzip';
688 $mimetype = 'application/x-bzip2';
692 $mimetype = 'application/octet-stream';
696 header('Pragma: no-cache');
697 header("Content-Type: $mimetype; name=\"$download_name$this->type\"");
698 header("Content-disposition: attachment; filename=$download_name$this->type");
700 $fp = @fopen
(PHPBB_ROOT_PATH
. "store/$filename$this->type", 'rb');
703 while ($buffer = fread($fp, 1024))