source: trunk/forum/includes/functions_compress.php

Last change on this file was 702, checked in by george, 15 years ago
  • Upraveno: Aktualizace fóra.
File size: 18.5 KB
Line 
1<?php
2/**
3*
4* @package phpBB3
5* @version $Id$
6* @copyright (c) 2005 phpBB Group
7* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8*
9*/
10
11/**
12* @ignore
13*/
14if (!defined('IN_PHPBB'))
15{
16 exit;
17}
18
19/**
20* Class for handling archives (compression/decompression)
21* @package phpBB3
22*/
23class compress
24{
25 var $fp = 0;
26
27 /**
28 * Add file to archive
29 */
30 function add_file($src, $src_rm_prefix = '', $src_add_prefix = '', $skip_files = '')
31 {
32 global $phpbb_root_path;
33
34 $skip_files = explode(',', $skip_files);
35
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;
42
43 if (is_file($phpbb_root_path . $src))
44 {
45 $this->data($src_path, file_get_contents("$phpbb_root_path$src"), false, stat("$phpbb_root_path$src"));
46 }
47 else if (is_dir($phpbb_root_path . $src))
48 {
49 // Clean up path, add closing / if not present
50 $src_path = ($src_path && substr($src_path, -1) != '/') ? $src_path . '/' : $src_path;
51
52 $filelist = array();
53 $filelist = filelist("$phpbb_root_path$src", '', '*');
54 krsort($filelist);
55
56 if ($src_path)
57 {
58 $this->data($src_path, '', true, stat("$phpbb_root_path$src"));
59 }
60
61 foreach ($filelist as $path => $file_ary)
62 {
63 if ($path)
64 {
65 // Same as for src_path
66 $path = (substr($path, 0, 1) == '/') ? substr($path, 1) : $path;
67 $path = ($path && substr($path, -1) != '/') ? $path . '/' : $path;
68
69 $this->data("$src_path$path", '', true, stat("$phpbb_root_path$src$path"));
70 }
71
72 foreach ($file_ary as $file)
73 {
74 if (in_array($path . $file, $skip_files))
75 {
76 continue;
77 }
78
79 $this->data("$src_path$path$file", file_get_contents("$phpbb_root_path$src$path$file"), false, stat("$phpbb_root_path$src$path$file"));
80 }
81 }
82 }
83 else
84 {
85 // $src does not exist
86 return false;
87 }
88
89 return true;
90 }
91
92 /**
93 * Add custom file (the filepath will not be adjusted)
94 */
95 function add_custom_file($src, $filename)
96 {
97 if (!file_exists($src))
98 {
99 return false;
100 }
101
102 $this->data($filename, file_get_contents($src), false, stat($src));
103 return true;
104 }
105
106 /**
107 * Add file data
108 */
109 function add_data($src, $name)
110 {
111 $stat = array();
112 $stat[2] = 436; //384
113 $stat[4] = $stat[5] = 0;
114 $stat[7] = strlen($src);
115 $stat[9] = time();
116 $this->data($name, $src, false, $stat);
117 return true;
118 }
119
120 /**
121 * Return available methods
122 */
123 function methods()
124 {
125 $methods = array('.tar');
126 $available_methods = array('.tar.gz' => 'zlib', '.tar.bz2' => 'bz2', '.zip' => 'zlib');
127
128 foreach ($available_methods as $type => $module)
129 {
130 if (!@extension_loaded($module))
131 {
132 continue;
133 }
134 $methods[] = $type;
135 }
136
137 return $methods;
138 }
139}
140
141/**
142* Zip creation class from phpMyAdmin 2.3.0 (c) Tobias Ratschiller, Olivier Müller, Loïc Chapeaux,
143* Marc Delisle, http://www.phpmyadmin.net/
144*
145* Zip extraction function by Alexandre Tedeschi, alexandrebr at gmail dot com
146*
147* Modified extensively by psoTFX and DavidMJ, (c) phpBB Group, 2003
148*
149* Based on work by Eric Mueller and Denis125
150* Official ZIP file format: http://www.pkware.com/appnote.txt
151*
152* @package phpBB3
153*/
154class compress_zip extends compress
155{
156 var $datasec = array();
157 var $ctrl_dir = array();
158 var $eof_cdh = "\x50\x4b\x05\x06\x00\x00\x00\x00";
159
160 var $old_offset = 0;
161 var $datasec_len = 0;
162
163 /**
164 * Constructor
165 */
166 function compress_zip($mode, $file)
167 {
168 $this->fp = @fopen($file, $mode . 'b');
169
170 if (!$this->fp)
171 {
172 trigger_error('Unable to open file ' . $file . ' [' . $mode . 'b]');
173 }
174 }
175
176 /**
177 * Convert unix to dos time
178 */
179 function unix_to_dos_time($time)
180 {
181 $timearray = (!$time) ? getdate() : getdate($time);
182
183 if ($timearray['year'] < 1980)
184 {
185 $timearray['year'] = 1980;
186 $timearray['mon'] = $timearray['mday'] = 1;
187 $timearray['hours'] = $timearray['minutes'] = $timearray['seconds'] = 0;
188 }
189
190 return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
191 }
192
193 /**
194 * Extract archive
195 */
196 function extract($dst)
197 {
198 // Loop the file, looking for files and folders
199 $dd_try = false;
200 rewind($this->fp);
201
202 while (!feof($this->fp))
203 {
204 // Check if the signature is valid...
205 $signature = fread($this->fp, 4);
206
207 switch ($signature)
208 {
209 // 'Local File Header'
210 case "\x50\x4b\x03\x04":
211 // Lets get everything we need.
212 // We don't store the version needed to extract, the general purpose bit flag or the date and time fields
213 $data = unpack("@4/vc_method/@10/Vcrc/Vc_size/Vuc_size/vname_len/vextra_field", fread($this->fp, 26));
214 $file_name = fread($this->fp, $data['name_len']); // filename
215
216 if ($data['extra_field'])
217 {
218 fread($this->fp, $data['extra_field']); // extra field
219 }
220
221 $target_filename = "$dst$file_name";
222
223 if (!$data['uc_size'] && !$data['crc'] && substr($file_name, -1, 1) == '/')
224 {
225 if (!is_dir($target_filename))
226 {
227 $str = '';
228 $folders = explode('/', $target_filename);
229
230 // Create and folders and subfolders if they do not exist
231 foreach ($folders as $folder)
232 {
233 $folder = trim($folder);
234 if (!$folder)
235 {
236 continue;
237 }
238
239 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
240 if (!is_dir($str))
241 {
242 if (!@mkdir($str, 0777))
243 {
244 trigger_error("Could not create directory $folder");
245 }
246 phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
247 }
248 }
249 }
250 // This is a directory, we are not writting files
251 continue;
252 }
253 else
254 {
255 // Some archivers are punks, they don't include folders in their archives!
256 $str = '';
257 $folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
258
259 // Create and folders and subfolders if they do not exist
260 foreach ($folders as $folder)
261 {
262 $folder = trim($folder);
263 if (!$folder)
264 {
265 continue;
266 }
267
268 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
269 if (!is_dir($str))
270 {
271 if (!@mkdir($str, 0777))
272 {
273 trigger_error("Could not create directory $folder");
274 }
275 phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
276 }
277 }
278 }
279
280 if (!$data['uc_size'])
281 {
282 $content = '';
283 }
284 else
285 {
286 $content = fread($this->fp, $data['c_size']);
287 }
288
289 $fp = fopen($target_filename, "w");
290
291 switch ($data['c_method'])
292 {
293 case 0:
294 // Not compressed
295 fwrite($fp, $content);
296 break;
297
298 case 8:
299 // Deflate
300 fwrite($fp, gzinflate($content, $data['uc_size']));
301 break;
302
303 case 12:
304 // Bzip2
305 fwrite($fp, bzdecompress($content));
306 break;
307 }
308
309 fclose($fp);
310 break;
311
312 // We hit the 'Central Directory Header', we can stop because nothing else in here requires our attention
313 // 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
314 case "\x50\x4b\x01\x02":
315 // This case should simply never happen.. but it does exist..
316 case "\x50\x4b\x05\x06":
317 break 2;
318
319 // 'Packed to Removable Disk', ignore it and look for the next signature...
320 case 'PK00':
321 continue 2;
322
323 // We have encountered a header that is weird. Lets look for better data...
324 default:
325 if (!$dd_try)
326 {
327 // Unexpected header. Trying to detect wrong placed 'Data Descriptor';
328 $dd_try = true;
329 fseek($this->fp, 8, SEEK_CUR); // Jump over 'crc-32'(4) 'compressed-size'(4), 'uncompressed-size'(4)
330 continue 2;
331 }
332 trigger_error("Unexpected header, ending loop");
333 break 2;
334 }
335
336 $dd_try = false;
337 }
338 }
339
340 /**
341 * Close archive
342 */
343 function close()
344 {
345 // Write out central file directory and footer ... if it exists
346 if (sizeof($this->ctrl_dir))
347 {
348 fwrite($this->fp, $this->file());
349 }
350 fclose($this->fp);
351 }
352
353 /**
354 * Create the structures ... note we assume version made by is MSDOS
355 */
356 function data($name, $data, $is_dir = false, $stat)
357 {
358 $name = str_replace('\\', '/', $name);
359
360 $hexdtime = pack('V', $this->unix_to_dos_time($stat[9]));
361
362 if ($is_dir)
363 {
364 $unc_len = $c_len = $crc = 0;
365 $zdata = '';
366 $var_ext = 10;
367 }
368 else
369 {
370 $unc_len = strlen($data);
371 $crc = crc32($data);
372 $zdata = gzdeflate($data);
373 $c_len = strlen($zdata);
374 $var_ext = 20;
375
376 // Did we compress? No, then use data as is
377 if ($c_len >= $unc_len)
378 {
379 $zdata = $data;
380 $c_len = $unc_len;
381 $var_ext = 10;
382 }
383 }
384 unset($data);
385
386 // If we didn't compress set method to store, else deflate
387 $c_method = ($c_len == $unc_len) ? "\x00\x00" : "\x08\x00";
388
389 // Are we a file or a directory? Set archive for file
390 $attrib = ($is_dir) ? 16 : 32;
391
392 // File Record Header
393 $fr = "\x50\x4b\x03\x04"; // Local file header 4bytes
394 $fr .= pack('v', $var_ext); // ver needed to extract 2bytes
395 $fr .= "\x00\x00"; // gen purpose bit flag 2bytes
396 $fr .= $c_method; // compression method 2bytes
397 $fr .= $hexdtime; // last mod time and date 2+2bytes
398 $fr .= pack('V', $crc); // crc32 4bytes
399 $fr .= pack('V', $c_len); // compressed filesize 4bytes
400 $fr .= pack('V', $unc_len); // uncompressed filesize 4bytes
401 $fr .= pack('v', strlen($name));// length of filename 2bytes
402
403 $fr .= pack('v', 0); // extra field length 2bytes
404 $fr .= $name;
405 $fr .= $zdata;
406 unset($zdata);
407
408 $this->datasec_len += strlen($fr);
409
410 // Add data to file ... by writing data out incrementally we save some memory
411 fwrite($this->fp, $fr);
412 unset($fr);
413
414 // Central Directory Header
415 $cdrec = "\x50\x4b\x01\x02"; // header 4bytes
416 $cdrec .= "\x00\x00"; // version made by
417 $cdrec .= pack('v', $var_ext); // version needed to extract
418 $cdrec .= "\x00\x00"; // gen purpose bit flag
419 $cdrec .= $c_method; // compression method
420 $cdrec .= $hexdtime; // last mod time & date
421 $cdrec .= pack('V', $crc); // crc32
422 $cdrec .= pack('V', $c_len); // compressed filesize
423 $cdrec .= pack('V', $unc_len); // uncompressed filesize
424 $cdrec .= pack('v', strlen($name)); // length of filename
425 $cdrec .= pack('v', 0); // extra field length
426 $cdrec .= pack('v', 0); // file comment length
427 $cdrec .= pack('v', 0); // disk number start
428 $cdrec .= pack('v', 0); // internal file attributes
429 $cdrec .= pack('V', $attrib); // external file attributes
430 $cdrec .= pack('V', $this->old_offset); // relative offset of local header
431 $cdrec .= $name;
432
433 // Save to central directory
434 $this->ctrl_dir[] = $cdrec;
435
436 $this->old_offset = $this->datasec_len;
437 }
438
439 /**
440 * file
441 */
442 function file()
443 {
444 $ctrldir = implode('', $this->ctrl_dir);
445
446 return $ctrldir . $this->eof_cdh .
447 pack('v', sizeof($this->ctrl_dir)) . // total # of entries "on this disk"
448 pack('v', sizeof($this->ctrl_dir)) . // total # of entries overall
449 pack('V', strlen($ctrldir)) . // size of central dir
450 pack('V', $this->datasec_len) . // offset to start of central dir
451 "\x00\x00"; // .zip file comment length
452 }
453
454 /**
455 * Download archive
456 */
457 function download($filename, $download_name = false)
458 {
459 global $phpbb_root_path;
460
461 if ($download_name === false)
462 {
463 $download_name = $filename;
464 }
465
466 $mimetype = 'application/zip';
467
468 header('Pragma: no-cache');
469 header("Content-Type: $mimetype; name=\"$download_name.zip\"");
470 header("Content-disposition: attachment; filename=$download_name.zip");
471
472 $fp = @fopen("{$phpbb_root_path}store/$filename.zip", 'rb');
473 if ($fp)
474 {
475 while ($buffer = fread($fp, 1024))
476 {
477 echo $buffer;
478 }
479 fclose($fp);
480 }
481 }
482}
483
484/**
485* Tar/tar.gz compression routine
486* Header/checksum creation derived from tarfile.pl, (c) Tom Horsley, 1994
487*
488* @package phpBB3
489*/
490class compress_tar extends compress
491{
492 var $isgz = false;
493 var $isbz = false;
494 var $filename = '';
495 var $mode = '';
496 var $type = '';
497 var $wrote = false;
498
499 /**
500 * Constructor
501 */
502 function compress_tar($mode, $file, $type = '')
503 {
504 $type = (!$type) ? $file : $type;
505 $this->isgz = (strpos($type, '.tar.gz') !== false || strpos($type, '.tgz') !== false) ? true : false;
506 $this->isbz = (strpos($type, '.tar.bz2') !== false) ? true : false;
507
508 $this->mode = &$mode;
509 $this->file = &$file;
510 $this->type = &$type;
511 $this->open();
512 }
513
514 /**
515 * Extract archive
516 */
517 function extract($dst)
518 {
519 $fzread = ($this->isbz && function_exists('bzread')) ? 'bzread' : (($this->isgz && @extension_loaded('zlib')) ? 'gzread' : 'fread');
520
521 // Run through the file and grab directory entries
522 while ($buffer = $fzread($this->fp, 512))
523 {
524 $tmp = unpack('A6magic', substr($buffer, 257, 6));
525
526 if (trim($tmp['magic']) == 'ustar')
527 {
528 $tmp = unpack('A100name', $buffer);
529 $filename = trim($tmp['name']);
530
531 $tmp = unpack('Atype', substr($buffer, 156, 1));
532 $filetype = (int) trim($tmp['type']);
533
534 $tmp = unpack('A12size', substr($buffer, 124, 12));
535 $filesize = octdec((int) trim($tmp['size']));
536
537 $target_filename = "$dst$filename";
538
539 if ($filetype == 5)
540 {
541 if (!is_dir($target_filename))
542 {
543 $str = '';
544 $folders = explode('/', $target_filename);
545
546 // Create and folders and subfolders if they do not exist
547 foreach ($folders as $folder)
548 {
549 $folder = trim($folder);
550 if (!$folder)
551 {
552 continue;
553 }
554
555 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
556 if (!is_dir($str))
557 {
558 if (!@mkdir($str, 0777))
559 {
560 trigger_error("Could not create directory $folder");
561 }
562 phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
563 }
564 }
565 }
566 }
567 else if ($filesize >= 0 && ($filetype == 0 || $filetype == "\0"))
568 {
569 // Some archivers are punks, they don't properly order the folders in their archives!
570 $str = '';
571 $folders = explode('/', pathinfo($target_filename, PATHINFO_DIRNAME));
572
573 // Create and folders and subfolders if they do not exist
574 foreach ($folders as $folder)
575 {
576 $folder = trim($folder);
577 if (!$folder)
578 {
579 continue;
580 }
581
582 $str = (!empty($str)) ? $str . '/' . $folder : $folder;
583 if (!is_dir($str))
584 {
585 if (!@mkdir($str, 0777))
586 {
587 trigger_error("Could not create directory $folder");
588 }
589 phpbb_chmod($str, CHMOD_READ | CHMOD_WRITE);
590 }
591 }
592
593 // Write out the files
594 if (!($fp = fopen($target_filename, 'wb')))
595 {
596 trigger_error("Couldn't create file $filename");
597 }
598 phpbb_chmod($target_filename, CHMOD_READ);
599
600 // Grab the file contents
601 fwrite($fp, ($filesize) ? $fzread($this->fp, ($filesize + 511) &~ 511) : '', $filesize);
602 fclose($fp);
603 }
604 }
605 }
606 }
607
608 /**
609 * Close archive
610 */
611 function close()
612 {
613 $fzclose = ($this->isbz && function_exists('bzclose')) ? 'bzclose' : (($this->isgz && @extension_loaded('zlib')) ? 'gzclose' : 'fclose');
614
615 if ($this->wrote)
616 {
617 $fzwrite = ($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
618
619 // The end of a tar archive ends in two records of all NULLs (1024 bytes of \0)
620 $fzwrite($this->fp, str_repeat("\0", 1024));
621 }
622
623 $fzclose($this->fp);
624 }
625
626 /**
627 * Create the structures
628 */
629 function data($name, $data, $is_dir = false, $stat)
630 {
631 $this->wrote = true;
632 $fzwrite = ($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite');
633
634 $typeflag = ($is_dir) ? '5' : '';
635
636 // This is the header data, it contains all the info we know about the file or folder that we are about to archive
637 $header = '';
638 $header .= pack('a100', $name); // file name
639 $header .= pack('a8', sprintf("%07o", $stat[2])); // file mode
640 $header .= pack('a8', sprintf("%07o", $stat[4])); // owner id
641 $header .= pack('a8', sprintf("%07o", $stat[5])); // group id
642 $header .= pack('a12', sprintf("%011o", $stat[7])); // file size
643 $header .= pack('a12', sprintf("%011o", $stat[9])); // last mod time
644
645 // Checksum
646 $checksum = 0;
647 for ($i = 0; $i < 148; $i++)
648 {
649 $checksum += ord($header[$i]);
650 }
651
652 // 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
653 $checksum += 2415 + (($is_dir) ? 53 : 0);
654
655 $header .= pack('a8', sprintf("%07o", $checksum)); // checksum
656 $header .= pack('a1', $typeflag); // link indicator
657 $header .= pack('a100', ''); // name of linked file
658 $header .= pack('a6', 'ustar'); // ustar indicator
659 $header .= pack('a2', '00'); // ustar version
660 $header .= pack('a32', 'Unknown'); // owner name
661 $header .= pack('a32', 'Unknown'); // group name
662 $header .= pack('a8', ''); // device major number
663 $header .= pack('a8', ''); // device minor number
664 $header .= pack('a155', ''); // filename prefix
665 $header .= pack('a12', ''); // end
666
667 // This writes the entire file in one shot. Header, followed by data and then null padded to a multiple of 512
668 $fzwrite($this->fp, $header . (($stat[7] !== 0 && !$is_dir) ? $data . str_repeat("\0", (($stat[7] + 511) &~ 511) - $stat[7]) : ''));
669 unset($data);
670 }
671
672 /**
673 * Open archive
674 */
675 function open()
676 {
677 $fzopen = ($this->isbz && function_exists('bzopen')) ? 'bzopen' : (($this->isgz && @extension_loaded('zlib')) ? 'gzopen' : 'fopen');
678 $this->fp = @$fzopen($this->file, $this->mode . (($fzopen == 'bzopen') ? '' : 'b') . (($fzopen == 'gzopen') ? '9' : ''));
679
680 if (!$this->fp)
681 {
682 trigger_error('Unable to open file ' . $this->file . ' [' . $fzopen . ' - ' . $this->mode . 'b]');
683 }
684 }
685
686 /**
687 * Download archive
688 */
689 function download($filename, $download_name = false)
690 {
691 global $phpbb_root_path;
692
693 if ($download_name === false)
694 {
695 $download_name = $filename;
696 }
697
698 switch ($this->type)
699 {
700 case '.tar':
701 $mimetype = 'application/x-tar';
702 break;
703
704 case '.tar.gz':
705 $mimetype = 'application/x-gzip';
706 break;
707
708 case '.tar.bz2':
709 $mimetype = 'application/x-bzip2';
710 break;
711
712 default:
713 $mimetype = 'application/octet-stream';
714 break;
715 }
716
717 header('Pragma: no-cache');
718 header("Content-Type: $mimetype; name=\"$download_name$this->type\"");
719 header("Content-disposition: attachment; filename=$download_name$this->type");
720
721 $fp = @fopen("{$phpbb_root_path}store/$filename$this->type", 'rb');
722 if ($fp)
723 {
724 while ($buffer = fread($fp, 1024))
725 {
726 echo $buffer;
727 }
728 fclose($fp);
729 }
730 }
731}
732
733?>
Note: See TracBrowser for help on using the repository browser.