(a little test for later merges)
[phpbb.git] / phpBB / includes / functions_compress.php
blob590daabf1d9617482724b4ed085496244d2d96df
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Class for handling archives (compression/decompression)
21 * @package phpBB3
23 class compress
25 var $fp = 0;
27 /**
28 * Add file to archive
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;
38 // Add src prefix
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;
52 $filelist = array();
53 $filelist = filelist("$phpbb_root_path$src", '', '*');
54 krsort($filelist);
56 if ($src_path)
58 $this->data($src_path, '', true, stat("$phpbb_root_path$src"));
61 foreach ($filelist as $path => $file_ary)
63 if ($path)
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))
76 continue;
79 $this->data("$src_path$path$file", file_get_contents("$phpbb_root_path$src$path$file"), false, stat("$phpbb_root_path$src$path$file"));
84 return true;
87 /**
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));
93 return true;
96 /**
97 * Add file data
99 function add_data($src, $name)
101 $stat = array();
102 $stat[2] = 436; //384
103 $stat[4] = $stat[5] = 0;
104 $stat[7] = strlen($src);
105 $stat[9] = time();
106 $this->data($name, $src, false, $stat);
107 return true;
111 * Return available methods
113 function 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))
122 continue;
124 $methods[] = $type;
127 return $methods;
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
142 * @package phpBB3
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";
150 var $old_offset = 0;
151 var $datasec_len = 0;
154 * Constructor
156 function compress_zip($mode, $file)
158 $this->fp = @fopen($file, $mode . 'b');
160 if (!$this->fp)
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);
184 * Extract archive
186 function extract($dst)
188 // Loop the file, looking for files and folders
189 $dd_try = false;
190 rewind($this->fp);
192 while (!feof($this->fp))
194 // Check if the signature is valid...
195 $signature = fread($this->fp, 4);
197 switch ($signature)
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))
217 $str = '';
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);
224 if (!$folder)
226 continue;
229 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
230 if (!is_dir($str))
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
241 continue;
243 else
245 // Some archivers are punks, they don't include folders in their archives!
246 $str = '';
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);
253 if (!$folder)
255 continue;
258 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
259 if (!is_dir($str))
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'])
272 $content = '';
274 else
276 $content = fread($this->fp, $data['c_size']);
279 $fp = fopen($target_filename, "w");
281 switch ($data['c_method'])
283 case 0:
284 // Not compressed
285 fwrite($fp, $content);
286 break;
288 case 8:
289 // Deflate
290 fwrite($fp, gzinflate($content, $data['uc_size']));
291 break;
293 case 12:
294 // Bzip2
295 fwrite($fp, bzdecompress($content));
296 break;
299 fclose($fp);
300 break;
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":
307 break 2;
309 // 'Packed to Removable Disk', ignore it and look for the next signature...
310 case 'PK00':
311 continue 2;
313 // We have encountered a header that is weird. Lets look for better data...
314 default:
315 if (!$dd_try)
317 // Unexpected header. Trying to detect wrong placed 'Data Descriptor';
318 $dd_try = true;
319 fseek($this->fp, 8, SEEK_CUR); // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
320 continue 2;
322 trigger_error("Unexpected header, ending loop");
323 break 2;
326 $dd_try = false;
331 * Close archive
333 function close()
335 // Write out central file directory and footer ... if it exists
336 if (sizeof($this->ctrl_dir))
338 fwrite($this->fp, $this->file());
340 fclose($this->fp);
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]));
352 if ($is_dir)
354 $unc_len = $c_len = $crc = 0;
355 $zdata = '';
356 $var_ext = 10;
358 else
360 $unc_len = strlen($data);
361 $crc = crc32($data);
362 $zdata = gzdeflate($data);
363 $c_len = strlen($zdata);
364 $var_ext = 20;
366 // Did we compress? No, then use data as is
367 if ($c_len >= $unc_len)
369 $zdata = $data;
370 $c_len = $unc_len;
371 $var_ext = 10;
374 unset($data);
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
394 $fr .= $name;
395 $fr .= $zdata;
396 unset($zdata);
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);
402 unset($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
421 $cdrec .= $name;
423 // Save to central directory
424 $this->ctrl_dir[] = $cdrec;
426 $this->old_offset = $this->datasec_len;
430 * file
432 function file()
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
445 * Download archive
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');
463 if ($fp)
465 while ($buffer = fread($fp, 1024))
467 echo $buffer;
469 fclose($fp);
475 * Tar/tar.gz compression routine
476 * Header/checksum creation derived from tarfile.pl, (c) Tom Horsley, 1994
478 * @package phpBB3
480 class compress_tar extends compress
482 var $isgz = false;
483 var $isbz = false;
484 var $filename = '';
485 var $mode = '';
486 var $type = '';
487 var $wrote = false;
490 * Constructor
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;
501 $this->open();
505 * Extract archive
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";
529 if ($filetype == 5)
531 if (!is_dir($target_filename))
533 $str = '';
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);
540 if (!$folder)
542 continue;
545 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
546 if (!is_dir($str))
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!
560 $str = '';
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);
567 if (!$folder)
569 continue;
572 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
573 if (!is_dir($str))
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);
592 fclose($fp);
599 * Close archive
601 function close()
603 $fzclose = ($this->isbz && function_exists('bzclose')) ? 'bzclose' : (($this->isgz && @extension_loaded('zlib')) ? 'gzclose' : 'fclose');
605 if ($this->wrote)
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));
613 $fzclose($this->fp);
617 * Create the structures
619 function data($name, $data, $is_dir = false, $stat)
621 $this->wrote = true;
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
627 $header = '';
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
635 // Checksum
636 $checksum = 0;
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]) : ''));
659 unset($data);
663 * Open archive
665 function open()
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' : ''));
670 if (!$this->fp)
672 trigger_error('Unable to open file ' . $this->file . ' [' . $fzopen . ' - ' . $this->mode . 'b]');
677 * Download archive
679 function download($filename, $download_name = false)
681 global $phpbb_root_path;
683 if ($download_name === false)
685 $download_name = $filename;
688 switch ($this->type)
690 case '.tar':
691 $mimetype = 'application/x-tar';
692 break;
694 case '.tar.gz':
695 $mimetype = 'application/x-gzip';
696 break;
698 case '.tar.bz2':
699 $mimetype = 'application/x-bzip2';
700 break;
702 default:
703 $mimetype = 'application/octet-stream';
704 break;
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');
712 if ($fp)
714 while ($buffer = fread($fp, 1024))
716 echo $buffer;
718 fclose($fp);