source: extensions/netinstall/trunk/class.unzip.txt @ 3291

Last change on this file since 3291 was 2869, checked in by patdenice, 15 years ago
  • add multi-language support to auto-install script.
  • Add roma theme to piwigo-loader.
File size: 16.5 KB
Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3# This file is part of Clearbricks.
4# Copyright (c) 2008 Olivier Meunier and contributors.
5# All rights reserved.
6#
7# Clearbricks is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# Clearbricks is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with Clearbricks; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20#
21# ***** END LICENSE BLOCK *****
22
23class fileUnzip
24{
25        protected $file_name;
26        protected $compressed_list = array();
27        protected $eo_central = array();
28       
29        protected $zip_sig   = "\x50\x4b\x03\x04"; # local file header signature
30        protected $dir_sig   = "\x50\x4b\x01\x02"; # central dir header signature
31        protected $dir_sig_e = "\x50\x4b\x05\x06"; # end of central dir signature
32        protected $fp = null;
33       
34        protected $memory_limit = null;
35       
36        public function __construct($file_name)
37        {
38                $this->file_name = $file_name;
39        }
40       
41        public function __destroy()
42        {
43                $this->close();
44        }
45       
46        public function close()
47        {
48                if ($this->fp) {
49                        fclose($this->fp);
50                }
51               
52                if ($this->memory_limit) {
53                        ini_set('memory_limit',$this->memory_limit);
54                }
55        }
56       
57        public function getList($stop_on_file=false,$exclude=false)
58        {
59                if (!empty($this->compressed_list)) {
60                        return $this->compressed_list;
61                }
62               
63                if (!$this->loadFileListByEOF($stop_on_file,$exclude)) {
64                        if(!$this->loadFileListBySignatures($stop_on_file,$exclude)) {
65                                return false;
66                        }
67                }
68               
69                return $this->compressed_list;
70        }
71       
72        public function unzipAll($target)
73        {
74                if (empty($this->compressed_list)) {
75                        $this->getList();
76                }
77               
78                foreach ($this->compressed_list as $k => $v)
79                {
80                        if ($v['is_dir']) {
81                                continue;
82                        }
83                       
84                        $this->unzip($k,$target.'/'.$k);
85                }
86        }
87       
88        public function unzip($file_name,$target=false)
89        {
90                if (empty($this->compressed_list)) {
91                        $this->getList($file_name);
92                }
93               
94                if (!isset($this->compressed_list[$file_name])) {
95                        throw new Exception(sprintf(__('File %s is not compressed in the zip.'),$file_name));
96                }
97                $details =& $this->compressed_list[$file_name];
98               
99                if ($details['is_dir']) {
100                        throw new Exception(sprintf(__('Trying to unzip a folder name %s'),$file_name));
101                }
102               
103                if (!$details['uncompressed_size']) {
104                        return $this->putContent('',$target);
105                }
106               
107                if ($target) {
108                        $this->testTargetDir(dirname($target));
109                }
110               
111                fseek($this->fp(),$details['contents_start_offset']);
112               
113                $this->memoryAllocate($details['compressed_size']);
114                return $this->uncompress(
115                        fread($this->fp(), $details['compressed_size']),
116                        $details['compression_method'],
117                        $details['uncompressed_size'],
118                        $target
119                );
120        }
121       
122        public function getFilesList()
123        {
124                if (empty($this->compressed_list)) {
125                        $this->getList();
126                }
127               
128                $res = array();
129                foreach ($this->compressed_list as $k => $v) {
130                        if (!$v['is_dir']) {
131                                $res[] = $k;
132                        }
133                }
134                return $res;
135        }
136       
137        public function getDirsList()
138        {
139                if (empty($this->compressed_list)) {
140                        $this->getList();
141                }
142               
143                $res = array();
144                foreach ($this->compressed_list as $k => $v) {
145                        if ($v['is_dir']) {
146                                $res[] = substr($k,0,-1);
147                        }
148                }
149                return $res;
150        }
151       
152        public function getRootDir()
153        {
154                if (empty($this->compressed_list)) {
155                        $this->getList();
156                }
157               
158                $files = $this->getFilesList();
159                $dirs = $this->getDirsList();
160               
161                $root_files = 0;
162                $root_dirs = 0;
163                foreach ($files as $v) { if (strpos($v,'/') === false) { $root_files++; }}
164                foreach ($dirs as $v)  { if (strpos($v,'/') === false) { $root_dirs++;  }}
165               
166                if ($root_files == 0 && $root_dirs == 1) {
167                        return $dirs[0];
168                } else {
169                        return false;
170                }
171        }
172       
173        public function isEmpty()
174        {
175                if (empty($this->compressed_list)) {
176                        $this->getList();
177                }
178               
179                return count($this->compressed_list) == 0;
180        }
181       
182        public function hasFile($f)
183        {
184                if (empty($this->compressed_list)) {
185                        $this->getList();
186                }
187               
188                return isset($this->compressed_list[$f]);
189        }
190       
191        protected function fp()
192        {
193                if ($this->fp === null) {
194                        $this->fp = @fopen($this->file_name,'rb');
195                }
196               
197                if ($this->fp === false) {
198                        throw new Exception('Unable to open file.');
199                }
200               
201                return $this->fp;
202        }
203       
204        protected function putContent($content,$target=false)
205        {
206                if ($target) {
207                        $r = @file_put_contents($target,$content);
208                        if ($r === false) {
209                                throw new Exception(__('Unable to write destination file.'));
210                        }
211                        files::inheritChmod($target);
212                        return true;
213                }
214                return $content;
215        }
216       
217        protected function testTargetDir($dir)
218        {
219                if (is_dir($dir) && !is_writable($dir)) {
220                        throw new Exception(__('Unable to write in target directory, permission denied.'));
221                }
222               
223                if (!is_dir($dir)) {
224                        files::makeDir($dir,true);
225                }
226        }
227       
228        protected function uncompress($content,$mode,$size,$target=false)
229        {
230                switch ($mode)
231                {
232                        case 0:
233                                # Not compressed
234                                $this->memoryAllocate($size*2);
235                                return $this->putContent($content,$target);
236                        case 1:
237                                throw new Exception('Shrunk mode is not supported.');
238                        case 2:
239                        case 3:
240                        case 4:
241                        case 5:
242                                throw new Exception('Compression factor '.($mode-1).' is not supported.');
243                        case 6:
244                                throw new Exception('Implode is not supported.');
245                        case 7:
246                                throw new Exception('Tokenizing compression algorithm is not supported.');
247                        case 8:
248                                # Deflate
249                                if (!function_exists('gzinflate')) {
250                                        throw new Exception('Gzip functions are not available.');
251                                }
252                                $this->memoryAllocate($size*2);
253                                return $this->putContent(gzinflate($content,$size),$target);
254                        case 9:
255                                throw new Exception('Enhanced Deflating is not supported.');
256                        case 10:
257                                throw new Exception('PKWARE Date Compression Library Impoloding is not supported.');
258                        case 12:
259                                # Bzip2
260                                if (!function_exists('bzdecompress')) {
261                                        throw new Exception('Bzip2 functions are not available.');
262                                }
263                                $this->memoryAllocate($size*2);
264                                return $this->putContent(bzdecompress($content),$target);
265                        case 18:
266                                throw new Exception('IBM TERSE is not supported.');
267                        default:
268                                throw new Exception('Unknown uncompress method');
269                }
270        }
271       
272        protected function loadFileListByEOF($stop_on_file=false,$exclude=false)
273        {
274                $fp = $this->fp();
275               
276                for ($x=0; $x<1024; $x++)
277                {
278                        fseek($fp,-22-$x,SEEK_END);
279                        $signature = fread($fp,4);
280                       
281                        if ($signature == $this->dir_sig_e)
282                        {
283                                $dir_list = array();
284                               
285                                $eodir = array(
286                                        'disk_number_this'   => unpack('v', fread($fp,2)),
287                                        'disk_number'        => unpack('v', fread($fp,2)),
288                                        'total_entries_this' => unpack('v', fread($fp,2)),
289                                        'total_entries'      => unpack('v', fread($fp,2)),
290                                        'size_of_cd'         => unpack('V', fread($fp,4)),
291                                        'offset_start_cd'    => unpack('V', fread($fp,4))
292                                );
293                               
294                                $zip_comment_len = unpack('v', fread($fp,2));
295                                $eodir['zipfile_comment'] = $zip_comment_len[1] ? fread($fp,$zip_comment_len) : '';
296                               
297                                $this->eo_central = array(
298                                        'disk_number_this'   => $eodir['disk_number_this'][1],
299                                        'disk_number'        => $eodir['disk_number'][1],
300                                        'total_entries_this' => $eodir['total_entries_this'][1],
301                                        'total_entries'      => $eodir['total_entries'][1],
302                                        'size_of_cd'         => $eodir['size_of_cd'][1],
303                                        'offset_start_cd'    => $eodir['offset_start_cd'][1],
304                                        'zipfile_comment'    => $eodir['zipfile_comment']
305                                );
306                               
307                                fseek($fp, $this->eo_central['offset_start_cd']);
308                                $signature = fread($fp,4);
309                               
310                                while ($signature == $this->dir_sig)
311                                {
312                                        $dir = array();
313                                        $dir['version_madeby']       = unpack("v",fread($fp, 2)); # version made by
314                                        $dir['version_needed']       = unpack("v",fread($fp, 2)); # version needed to extract
315                                        $dir['general_bit_flag']     = unpack("v",fread($fp, 2)); # general purpose bit flag
316                                        $dir['compression_method']   = unpack("v",fread($fp, 2)); # compression method
317                                        $dir['lastmod_time']         = unpack("v",fread($fp, 2)); # last mod file time
318                                        $dir['lastmod_date']         = unpack("v",fread($fp, 2)); # last mod file date
319                                        $dir['crc-32']               = fread($fp,4);              # crc-32
320                                        $dir['compressed_size']      = unpack("V",fread($fp, 4)); # compressed size
321                                        $dir['uncompressed_size']    = unpack("V",fread($fp, 4)); # uncompressed size
322                                       
323                                        $file_name_len               = unpack("v",fread($fp, 2)); # filename length
324                                        $extra_field_len             = unpack("v",fread($fp, 2)); # extra field length
325                                        $file_comment_len            = unpack("v",fread($fp, 2)); # file comment length
326                                       
327                                        $dir['disk_number_start']    = unpack("v",fread($fp, 2)); # disk number start
328                                        $dir['internal_attributes']  = unpack("v",fread($fp, 2)); # internal file attributes-byte1
329                                        $dir['external_attributes1'] = unpack("v",fread($fp, 2)); # external file attributes-byte2
330                                        $dir['external_attributes2'] = unpack("v",fread($fp, 2)); # external file attributes
331                                        $dir['relative_offset']      = unpack("V",fread($fp, 4)); # relative offset of local header
332                                        $dir['file_name']            = $this->cleanFileName(fread($fp, $file_name_len[1]));          # filename
333                                        $dir['extra_field']          = $extra_field_len[1] ? fread($fp, $extra_field_len[1]) : '';   # extra field
334                                        $dir['file_comment']         = $file_comment_len[1] ? fread($fp, $file_comment_len[1]) : ''; # file comment                     
335                                       
336                                        $dir_list[$dir['file_name']] = array(
337                                                'version_madeby'       => $dir['version_madeby'][1],
338                                                'version_needed'       => $dir['version_needed'][1],
339                                                'general_bit_flag'     => str_pad(decbin($dir['general_bit_flag'][1]), 8, '0', STR_PAD_LEFT),
340                                                'compression_method'   => $dir['compression_method'][1],
341                                                'lastmod_datetime'     => $this->getTimeStamp($dir['lastmod_date'][1],$dir['lastmod_time'][1]),
342                                                'crc-32'               => str_pad(dechex(ord($dir['crc-32'][3])), 2, '0', STR_PAD_LEFT).
343                                                                                         str_pad(dechex(ord($dir['crc-32'][2])), 2, '0', STR_PAD_LEFT).
344                                                                                         str_pad(dechex(ord($dir['crc-32'][1])), 2, '0', STR_PAD_LEFT).
345                                                                                         str_pad(dechex(ord($dir['crc-32'][0])), 2, '0', STR_PAD_LEFT),
346                                                'compressed_size'      => $dir['compressed_size'][1],
347                                                'uncompressed_size'    => $dir['uncompressed_size'][1],
348                                                'disk_number_start'    => $dir['disk_number_start'][1],
349                                                'internal_attributes'  => $dir['internal_attributes'][1],
350                                                'external_attributes1' => $dir['external_attributes1'][1],
351                                                'external_attributes2' => $dir['external_attributes2'][1],
352                                                'relative_offset'      => $dir['relative_offset'][1],
353                                                'file_name'            => $dir['file_name'],
354                                                'extra_field'          => $dir['extra_field'],
355                                                'file_comment'         => $dir['file_comment']
356                                        );
357                                        $signature = fread($fp, 4);
358                                }
359                               
360                                foreach ($dir_list as $k => $v)
361                                {
362                                        if ($exclude && preg_match($exclude,$k)) {
363                                                continue;
364                                        }
365                                       
366                                        $i = $this->getFileHeaderInformation($v['relative_offset']);
367                                       
368                                        $this->compressed_list[$k]['file_name']            = $k;
369                                        $this->compressed_list[$k]['is_dir']               = $v['external_attributes1'] == 16 || substr($k,-1,1) == '/';
370                                        $this->compressed_list[$k]['compression_method']   = $v['compression_method'];
371                                        $this->compressed_list[$k]['version_needed']       = $v['version_needed'];
372                                        $this->compressed_list[$k]['lastmod_datetime']     = $v['lastmod_datetime'];
373                                        $this->compressed_list[$k]['crc-32']               = $v['crc-32'];
374                                        $this->compressed_list[$k]['compressed_size']      = $v['compressed_size'];
375                                        $this->compressed_list[$k]['uncompressed_size']    = $v['uncompressed_size'];
376                                        $this->compressed_list[$k]['lastmod_datetime']     = $v['lastmod_datetime'];
377                                        $this->compressed_list[$k]['extra_field']          = $i['extra_field'];
378                                        $this->compressed_list[$k]['contents_start_offset'] = $i['contents_start_offset'];
379                                       
380                                        if(strtolower($stop_on_file) == strtolower($k)) {
381                                                break;
382                                        }
383                                }
384                                return true;
385                        }
386                }
387                return false;
388        }
389       
390        protected function loadFileListBySignatures($stop_on_file=false,$exclude=false)
391        {
392                $fp = $this->fp();
393                fseek($fp,0);
394               
395                $return = false;
396                while(true)
397                {
398                        $details = $this->getFileHeaderInformation();
399                        if (!$details) {
400                                fseek($fp,12-4,SEEK_CUR); # 12: Data descriptor - 4: Signature (that will be read again)
401                                $details = $this->getFileHeaderInformation();
402                        }
403                        if (!$details) {
404                                break;
405                        }
406                        $filename = $details['file_name'];
407                       
408                        if ($exclude && preg_match($exclude,$filename)) {
409                                continue;
410                        }
411                       
412                        $this->compressed_list[$filename] = $details;
413                        $return = true;
414                       
415                        if (strtolower($stop_on_file) == strtolower($filename)) {
416                                break;
417                        }
418                }
419               
420                return $return;
421        }
422       
423        protected function getFileHeaderInformation($start_offset=false)
424        {
425                $fp = $this->fp();
426               
427                if ($start_offset !== false) {
428                        fseek($fp,$start_offset);
429                }
430               
431                $signature = fread($fp, 4);
432                if ($signature == $this->zip_sig)
433                {
434                        # Get information about the zipped file
435                        $file = array();
436                        $file['version_needed']        = unpack("v",fread($fp, 2)); # version needed to extract
437                        $file['general_bit_flag']      = unpack("v",fread($fp, 2)); # general purpose bit flag
438                        $file['compression_method']    = unpack("v",fread($fp, 2)); # compression method
439                        $file['lastmod_time']          = unpack("v",fread($fp, 2)); # last mod file time
440                        $file['lastmod_date']          = unpack("v",fread($fp, 2)); # last mod file date
441                        $file['crc-32']                = fread($fp,4);              # crc-32
442                        $file['compressed_size']       = unpack("V",fread($fp, 4)); # compressed size
443                        $file['uncompressed_size']     = unpack("V",fread($fp, 4)); # uncompressed size
444                       
445                        $file_name_len                 = unpack("v",fread($fp, 2)); # filename length
446                        $extra_field_len               = unpack("v",fread($fp, 2)); # extra field length
447                       
448                        $file['file_name']             = $this->cleanFileName(fread($fp,$file_name_len[1])); # filename
449                        $file['extra_field']           = $extra_field_len[1] ? fread($fp, $extra_field_len[1]) : ''; # extra field
450                        $file['contents_start_offset'] = ftell($fp);
451                       
452                        # Look for the next file
453                        fseek($fp, $file['compressed_size'][1], SEEK_CUR);
454                       
455                        # Mount file table
456                        $i = array(
457                                'file_name'            => $file['file_name'],
458                                'is_dir'               => substr($file['file_name'],-1,1) == '/',
459                                'compression_method'   => $file['compression_method'][1],
460                                'version_needed'       => $file['version_needed'][1],
461                                'lastmod_datetime'     => $this->getTimeStamp($file['lastmod_date'][1],$file['lastmod_time'][1]),
462                                'crc-32'               => str_pad(dechex(ord($file['crc-32'][3])), 2, '0', STR_PAD_LEFT).
463                                                                         str_pad(dechex(ord($file['crc-32'][2])), 2, '0', STR_PAD_LEFT).
464                                                                         str_pad(dechex(ord($file['crc-32'][1])), 2, '0', STR_PAD_LEFT).
465                                                                         str_pad(dechex(ord($file['crc-32'][0])), 2, '0', STR_PAD_LEFT),
466                                'compressed_size'      => $file['compressed_size'][1],
467                                'uncompressed_size'    => $file['uncompressed_size'][1],
468                                'extra_field'          => $file['extra_field'],
469                                'general_bit_flag'     => str_pad(decbin($file['general_bit_flag'][1]), 8, '0', STR_PAD_LEFT),
470                                'contents_start_offset'=>$file['contents_start_offset']
471                        );
472                        return $i;
473                }
474                return false;
475        }
476       
477        protected function getTimeStamp($date,$time)
478        {
479                $BINlastmod_date = str_pad(decbin($date), 16, '0', STR_PAD_LEFT);
480                $BINlastmod_time = str_pad(decbin($time), 16, '0', STR_PAD_LEFT);
481                $lastmod_dateY   = bindec(substr($BINlastmod_date,  0, 7))+1980;
482                $lastmod_dateM   = bindec(substr($BINlastmod_date,  7, 4));
483                $lastmod_dateD   = bindec(substr($BINlastmod_date, 11, 5));
484                $lastmod_timeH   = bindec(substr($BINlastmod_time,   0, 5));
485                $lastmod_timeM   = bindec(substr($BINlastmod_time,   5, 6));
486                $lastmod_timeS   = bindec(substr($BINlastmod_time,  11, 5)) * 2;
487               
488                return mktime($lastmod_timeH, $lastmod_timeM, $lastmod_timeS, $lastmod_dateM, $lastmod_dateD, $lastmod_dateY);
489        }
490       
491        protected function cleanFileName($n)
492        {
493                $n = str_replace('../','',$n);
494                $n = preg_replace('#^/+#','',$n);
495                return $n;
496        }
497       
498        protected function memoryAllocate($size)
499        {
500                $mem_used = function_exists('memory_get_usage') ? @memory_get_usage() : 4000000;
501                $mem_limit = @ini_get('memory_limit');
502                if ($mem_used && $mem_limit)
503                {
504                        $mem_limit = files::str2bytes($mem_limit);
505                        $mem_avail = $mem_limit-$mem_used-(512*1024);
506                        $mem_needed = $size;
507                       
508                        if ($mem_needed > $mem_avail)
509                        {
510                                if (@ini_set('memory_limit',$mem_limit+$mem_needed+$mem_used) === false) {
511                                        throw new Exception(__('Not enough memory to open file.'));
512                                }
513                               
514                                if (!$this->memory_limit) {
515                                        $this->memory_limit = $mem_limit;
516                                }
517                        }
518                }
519        }
520}
521?>
Note: See TracBrowser for help on using the repository browser.