Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / functions_compress.php
blobeebc82b9d0dc2077077e99823a2281aef561a2e8
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 protected $fp = 0;
27 /**
28 * Add file to archive
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;
36 // Add src prefix
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;
50 $filelist = array();
51 $filelist = filelist(PHPBB_ROOT_PATH . $src, '', '*');
52 krsort($filelist);
54 if ($src_path)
56 $this->data($src_path, '', true, stat(PHPBB_ROOT_PATH . $src));
59 foreach ($filelist as $path => $file_ary)
61 if ($path)
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))
74 continue;
77 $this->data("$src_path$path$file", file_get_contents(PHPBB_ROOT_PATH . $src . $path . $file), false, stat(PHPBB_ROOT_PATH . $src . $path . $file));
82 return true;
85 /**
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));
91 return true;
94 /**
95 * Add file data
97 public function add_data($src, $name)
99 $stat = array();
100 $stat[2] = 436; //384
101 $stat[4] = $stat[5] = 0;
102 $stat[7] = strlen($src);
103 $stat[9] = time();
104 $this->data($name, $src, false, $stat);
105 return true;
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))
120 continue;
122 $methods[] = $type;
125 return $methods;
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
140 * @package phpBB3
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;
152 * Constructor
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);
177 * Extract archive
179 public function extract($dst)
181 // Loop the file, looking for files and folders
182 $dd_try = false;
183 rewind($this->fp);
185 while (!feof($this->fp))
187 // Check if the signature is valid...
188 $signature = fread($this->fp, 4);
190 switch ($signature)
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))
210 $str = '';
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);
217 if (!$folder)
219 continue;
222 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
223 if (!is_dir($str))
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
234 continue;
236 else
238 // Some archivers are punks, they don't include folders in their archives!
239 $str = '';
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);
246 if (!$folder)
248 continue;
251 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
252 if (!is_dir($str))
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'])
265 $content = '';
267 else
269 $content = fread($this->fp, $data['c_size']);
272 $fp = fopen($target_filename, "w");
274 switch ($data['c_method'])
276 case 0:
277 // Not compressed
278 fwrite($fp, $content);
279 break;
281 case 8:
282 // Deflate
283 fwrite($fp, gzinflate($content, $data['uc_size']));
284 break;
286 case 12:
287 // Bzip2
288 fwrite($fp, bzdecompress($content));
289 break;
292 fclose($fp);
293 break;
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":
300 break 2;
302 // 'Packed to Removable Disk', ignore it and look for the next signature...
303 case 'PK00':
304 continue 2;
306 // We have encountered a header that is weird. Lets look for better data...
307 default:
308 if (!$dd_try)
310 // Unexpected header. Trying to detect wrong placed 'Data Descriptor';
311 $dd_try = true;
312 fseek($this->fp, 8, SEEK_CUR); // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
313 continue 2;
315 trigger_error("Unexpected header, ending loop");
316 break 2;
319 $dd_try = false;
324 * Close archive
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());
333 fclose($this->fp);
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]));
345 if ($is_dir)
347 $unc_len = $c_len = $crc = 0;
348 $zdata = '';
349 $var_ext = 10;
351 else
353 $unc_len = strlen($data);
354 $crc = crc32($data);
355 $zdata = gzdeflate($data);
356 $c_len = strlen($zdata);
357 $var_ext = 20;
359 // Did we compress? No, then use data as is
360 if ($c_len >= $unc_len)
362 $zdata = $data;
363 $c_len = $unc_len;
364 $var_ext = 10;
367 unset($data);
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
387 $fr .= $name;
388 $fr .= $zdata;
389 unset($zdata);
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);
395 unset($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
414 $cdrec .= $name;
416 // Save to central directory
417 $this->ctrl_dir[] = $cdrec;
419 $this->old_offset = $this->datasec_len;
423 * file
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
438 * Download archive
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');
454 if ($fp)
456 while ($buffer = fread($fp, 1024))
458 echo $buffer;
460 fclose($fp);
466 * Tar/tar.gz compression routine
467 * Header/checksum creation derived from tarfile.pl, (c) Tom Horsley, 1994
469 * @package phpBB3
471 class compress_tar extends compress
473 private $isgz = false;
474 private $isbz = false;
475 private $filename = '';
476 private $mode = '';
477 private $type = '';
478 private $wrote = false;
481 * Constructor
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;
492 $this->open();
496 * Extract archive
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";
520 if ($filetype == 5)
522 if (!is_dir($target_filename))
524 $str = '';
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);
531 if (!$folder)
533 continue;
536 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
537 if (!is_dir($str))
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!
551 $str = '';
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);
558 if (!$folder)
560 continue;
563 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
564 if (!is_dir($str))
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);
583 fclose($fp);
590 * Close archive
592 public function close()
594 $fzclose = ($this->isbz && function_exists('bzclose')) ? 'bzclose' : (($this->isgz && @extension_loaded('zlib')) ? 'gzclose' : 'fclose');
596 if ($this->wrote)
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));
604 $fzclose($this->fp);
608 * Create the structures
610 protected function data($name, $data, $is_dir = false, $stat)
612 $this->wrote = true;
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
618 $header = '';
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
626 // Checksum
627 $checksum = 0;
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]) : ''));
650 unset($data);
654 * Open archive
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' : ''));
661 if (!$this->fp)
663 trigger_error('Unable to open file ' . $this->file . ' [' . $fzopen . ' - ' . $this->mode . 'b]');
668 * Download archive
670 public function download($filename, $download_name = false)
672 if ($download_name === false)
674 $download_name = $filename;
677 switch ($this->type)
679 case '.tar':
680 $mimetype = 'application/x-tar';
681 break;
683 case '.tar.gz':
684 $mimetype = 'application/x-gzip';
685 break;
687 case '.tar.bz2':
688 $mimetype = 'application/x-bzip2';
689 break;
691 default:
692 $mimetype = 'application/octet-stream';
693 break;
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');
701 if ($fp)
703 while ($buffer = fread($fp, 1024))
705 echo $buffer;
707 fclose($fp);