[TASK] self:: is not used for local static member reference
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / tools / class.tx_em_tools_unzip.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) Vincent Blavet <vincent@phpconcept.net>
6 * (c) 2005-2010 Karsten Dambekalns <karsten@typo3.org>
7 * All rights reserved
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301 USA
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Module: Extension manager
28 *
29 * @author Vincent Blavet <vincent@phpconcept.net>
30 * @author Karsten Dambekalns <karsten@typo3.org>
31 */
32
33
34 // Constants
35 define('ARCHIVE_ZIP_READ_BLOCK_SIZE', 2048);
36
37 // File list separator
38 define('ARCHIVE_ZIP_SEPARATOR', ',');
39
40 define('ARCHIVE_ZIP_TEMPORARY_DIR', '');
41
42 // Error codes
43 define('ARCHIVE_ZIP_ERR_NO_ERROR', 0);
44 define('ARCHIVE_ZIP_ERR_WRITE_OPEN_FAIL', -1);
45 define('ARCHIVE_ZIP_ERR_READ_OPEN_FAIL', -2);
46 define('ARCHIVE_ZIP_ERR_INVALID_PARAMETER', -3);
47 define('ARCHIVE_ZIP_ERR_MISSING_FILE', -4);
48 define('ARCHIVE_ZIP_ERR_FILENAME_TOO_LONG', -5);
49 define('ARCHIVE_ZIP_ERR_INVALID_ZIP', -6);
50 define('ARCHIVE_ZIP_ERR_BAD_EXTRACTED_FILE', -7);
51 define('ARCHIVE_ZIP_ERR_DIR_CREATE_FAIL', -8);
52 define('ARCHIVE_ZIP_ERR_BAD_EXTENSION', -9);
53 define('ARCHIVE_ZIP_ERR_BAD_FORMAT', -10);
54 define('ARCHIVE_ZIP_ERR_DELETE_FILE_FAIL', -11);
55 define('ARCHIVE_ZIP_ERR_RENAME_FILE_FAIL', -12);
56 define('ARCHIVE_ZIP_ERR_BAD_CHECKSUM', -13);
57 define('ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP', -14);
58 define('ARCHIVE_ZIP_ERR_MISSING_OPTION_VALUE', -15);
59 define('ARCHIVE_ZIP_ERR_INVALID_PARAM_VALUE', -16);
60
61 // Warning codes
62 define('ARCHIVE_ZIP_WARN_NO_WARNING', 0);
63 define('ARCHIVE_ZIP_WARN_FILE_EXIST', 1);
64
65 // Methods parameters
66 define('ARCHIVE_ZIP_PARAM_PATH', 'path');
67 define('ARCHIVE_ZIP_PARAM_ADD_PATH', 'add_path');
68 define('ARCHIVE_ZIP_PARAM_REMOVE_PATH', 'remove_path');
69 define('ARCHIVE_ZIP_PARAM_REMOVE_ALL_PATH', 'remove_all_path');
70 define('ARCHIVE_ZIP_PARAM_SET_CHMOD', 'set_chmod');
71 define('ARCHIVE_ZIP_PARAM_EXTRACT_AS_STRING', 'extract_as_string');
72 define('ARCHIVE_ZIP_PARAM_NO_COMPRESSION', 'no_compression');
73
74 define('ARCHIVE_ZIP_PARAM_PRE_EXTRACT', 'callback_pre_extract');
75 define('ARCHIVE_ZIP_PARAM_POST_EXTRACT', 'callback_post_extract');
76 define('ARCHIVE_ZIP_PARAM_PRE_ADD', 'callback_pre_add');
77 define('ARCHIVE_ZIP_PARAM_POST_ADD', 'callback_post_add');
78
79
80 /**
81 * Class for unpacking zip archive files
82 *
83 * @author Vincent Blavet <vincent@blavet.net>
84 * @author Karsten Dambekalns <karsten@typo3.org>
85 */
86 class tx_em_Tools_Unzip {
87 /**
88 * The filename of the zip archive.
89 *
90 * @var string Name of the Zip file
91 */
92 var $_zipname = '';
93
94 /**
95 * File descriptor of the opened Zip file.
96 *
97 * @var int Internal zip file descriptor
98 */
99 var $_zip_fd = 0;
100
101 /**
102 * @var int last error code
103 */
104 var $_error_code = 1;
105
106 /**
107 * @var string Last error description
108 */
109 var $_error_string = '';
110
111 /**
112 * tx_em_Tools_Unzip Class constructor. This flavour of the constructor only
113 * declare a new tx_em_Tools_Unzip object, identifying it by the name of the
114 * zip file.
115 *
116 * @param string $p_zipname The name of the zip archive to create
117 * @access public
118 */
119 public function __construct($p_zipname) {
120
121 // Check the zlib
122 if (!extension_loaded('zlib')) {
123 throw new RuntimeException(
124 'TYPO3 Fatal Error: ' . "The extension 'zlib' couldn't be found.\n" .
125 "Please make sure your version of PHP was built " .
126 "with 'zlib' support.\n",
127 1270853984
128 );
129 }
130
131 // Set the attributes
132 $this->_zipname = $p_zipname;
133 $this->_zip_fd = 0;
134
135 return;
136 }
137
138
139 /**
140 * This method extract the files and folders which are in the zip archive.
141 * It can extract all the archive or a part of the archive by using filter
142 * feature (extract by name, by index, by ereg, by preg). The extraction
143 * can occur in the current path or an other path.
144 * All the advanced features are activated by the use of variable
145 * parameters.
146 * The return value is an array of entry descriptions which gives
147 * information on extracted files (See listContent()).
148 * The method may return a success value (an array) even if some files
149 * are not correctly extracted (see the file status in listContent()).
150 * The supported variable parameters for this method are :
151 * 'add_path' : Path where the files and directories are to be extracted
152 *
153 * @access public
154 * @param mixed $p_params An array of variable parameters and values.
155 * @return mixed An array of file description on success,
156 * 0 on an unrecoverable failure, an error code is logged.
157 */
158 function extract($p_params = 0) {
159 $this->_errorReset();
160
161 // Check archive
162 if (!$this->_checkFormat()) {
163 return (0);
164 }
165
166 // Set default values
167 if ($p_params === 0) {
168 $p_params = array();
169 }
170 if ($this->_check_parameters($p_params,
171 array('extract_as_string' => FALSE,
172 'add_path' => '',
173 'remove_path' => '',
174 'remove_all_path' => FALSE,
175 'callback_pre_extract' => '',
176 'callback_post_extract' => '',
177 'set_chmod' => 0)) != 1) {
178 return 0;
179 }
180
181 // Call the extracting fct
182 $v_list = array();
183 if ($this->_extractByRule($v_list, $p_params) != 1) {
184 unset($v_list);
185 return (0);
186 }
187
188 return $v_list;
189 }
190
191 /**
192 * Method that gives the lastest error code.
193 *
194 * @access public
195 * @return integer The error code value.
196 */
197 function errorCode() {
198 return ($this->_error_code);
199 }
200
201 /**
202 * This method gives the latest error code name.
203 *
204 * @access public
205 * @param boolean $p_with_code If TRUE, gives the name and the int value.
206 * @return string The error name.
207 */
208 function errorName($p_with_code = FALSE) {
209 $v_const_list = get_defined_constants();
210
211 // Extract error constants from all const.
212 for (reset($v_const_list);
213 list($v_key, $v_value) = each($v_const_list);) {
214 if (substr($v_key, 0, strlen('ARCHIVE_ZIP_ERR_')) == 'ARCHIVE_ZIP_ERR_') {
215 $v_error_list[$v_key] = $v_value;
216 }
217 }
218
219 // Search the name form the code value
220 $v_key = array_search($this->_error_code, $v_error_list, TRUE);
221 if ($v_key != FALSE) {
222 $v_value = $v_key;
223 } else {
224 $v_value = 'NoName';
225 }
226
227 if ($p_with_code) {
228 return ($v_value . ' (' . $this->_error_code . ')');
229 } else {
230 return ($v_value);
231 }
232 }
233
234 /**
235 * This method returns the description associated with the latest error.
236 *
237 * @access public
238 * @param boolean $p_full If set to TRUE gives the description with the
239 * error code, the name and the description.
240 * If set to FALSE gives only the description
241 * and the error code.
242 * @return string The error description.
243 */
244 function errorInfo($p_full = FALSE) {
245 if ($p_full) {
246 return ($this->errorName(TRUE) . " : " . $this->_error_string);
247 } else {
248 return ($this->_error_string . " [code " . $this->_error_code . "]");
249 }
250 }
251
252
253 /**
254 * tx_em_Tools_Unzip::_checkFormat()
255 *
256 * { Description }
257 *
258 * @param integer $p_level
259 */
260 function _checkFormat($p_level = 0) {
261 $v_result = TRUE;
262
263 // Reset the error handler
264 $this->_errorReset();
265
266 // Look if the file exits
267 if (!is_file($this->_zipname)) {
268 // Error log
269 $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
270 "Missing archive file '" . $this->_zipname . "'");
271 return (FALSE);
272 }
273
274 // Check that the file is readeable
275 if (!is_readable($this->_zipname)) {
276 // Error log
277 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
278 "Unable to read archive '" . $this->_zipname . "'");
279 return (FALSE);
280 }
281
282 // Check the magic code
283 // TBC
284
285 // Check the central header
286 // TBC
287
288 // Check each file header
289 // TBC
290
291 // Return
292 return $v_result;
293 }
294
295
296 /**
297 * self::_openFd()
298 *
299 * { Description }
300 *
301 */
302 function _openFd($p_mode) {
303 $v_result = 1;
304
305 // Look if already open
306 if ($this->_zip_fd != 0) {
307 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
308 'Zip file \'' . $this->_zipname . '\' already open');
309 return self::errorCode();
310 }
311
312 // Open the zip file
313 if (($this->_zip_fd = @fopen($this->_zipname, $p_mode)) == 0) {
314 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
315 'Unable to open archive \'' . $this->_zipname
316 . '\' in ' . $p_mode . ' mode');
317 return self::errorCode();
318 }
319
320 // Return
321 return $v_result;
322 }
323
324 /**
325 * tx_em_Tools_Unzip::_closeFd()
326 *
327 * { Description }
328 *
329 */
330 function _closeFd() {
331 $v_result = 1;
332
333 if ($this->_zip_fd != 0) {
334 @fclose($this->_zip_fd);
335 }
336 $this->_zip_fd = 0;
337
338 // Return
339 return $v_result;
340 }
341
342
343 /**
344 * tx_em_Tools_Unzip::_convertHeader2FileInfo()
345 *
346 * { Description }
347 *
348 */
349 function _convertHeader2FileInfo($p_header, &$p_info) {
350 $v_result = 1;
351
352 // Get the interesting attributes
353 $p_info['filename'] = $p_header['filename'];
354 $p_info['stored_filename'] = $p_header['stored_filename'];
355 $p_info['size'] = $p_header['size'];
356 $p_info['compressed_size'] = $p_header['compressed_size'];
357 $p_info['mtime'] = $p_header['mtime'];
358 $p_info['comment'] = $p_header['comment'];
359 $p_info['folder'] = (($p_header['external'] & 0x00000010) == 0x00000010);
360 $p_info['index'] = $p_header['index'];
361 $p_info['status'] = $p_header['status'];
362
363 // Return
364 return $v_result;
365 }
366
367
368 // Function : _extractByRule()
369 // Description :
370 // Extract a file or directory depending of rules (by index, by name, ...)
371 // Parameters :
372 // $p_file_list : An array where will be placed the properties of each
373 // extracted file
374 // $p_path : Path to add while writing the extracted files
375 // $p_remove_path : Path to remove (from the file memorized path) while writing the
376 // extracted files. If the path does not match the file path,
377 // the file is extracted with its memorized path.
378 // $p_remove_path does not apply to 'list' mode.
379 // $p_path and $p_remove_path are commulative.
380 // Return Values :
381 // 1 on success,0 or less on error (see error code list)
382
383 /**
384 * tx_em_Tools_Unzip::_extractByRule()
385 *
386 * { Description }
387 *
388 */
389 function _extractByRule(&$p_file_list, &$p_params) {
390 $v_result = 1;
391
392 $p_path = $p_params['add_path'];
393 $p_remove_path = $p_params['remove_path'];
394 $p_remove_all_path = $p_params['remove_all_path'];
395
396 // Check the path
397 if (($p_path == "")
398 || ((substr($p_path, 0, 1) != "/")
399 && (substr($p_path, 0, 3) != "../") && (substr($p_path, 1, 2) != ":/"))) {
400 $p_path = "./" . $p_path;
401 }
402
403 // Reduce the path last (and duplicated) '/'
404 if (($p_path != "./") && ($p_path != "/")) {
405 // Look for the path end '/'
406 while (substr($p_path, -1) == "/") {
407 $p_path = substr($p_path, 0, strlen($p_path) - 1);
408 }
409 }
410
411 // Open the zip file
412 if (($v_result = $this->_openFd('rb')) != 1) {
413 return $v_result;
414 }
415
416 // Read the central directory informations
417 $v_central_dir = array();
418 if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
419 // Close the zip file
420 $this->_closeFd();
421
422 return $v_result;
423 }
424
425 // Start at beginning of Central Dir
426 $v_pos_entry = $v_central_dir['offset'];
427
428 // Read each entry
429 $j_start = 0;
430 for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; $i++) {
431 // Read next Central dir entry
432 @rewind($this->_zip_fd);
433 if (@fseek($this->_zip_fd, $v_pos_entry)) {
434 $this->_closeFd();
435
436 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
437 'Invalid archive size');
438
439 return self::errorCode();
440 }
441
442 // Read the file header
443 $v_header = array();
444 if (($v_result = $this->_readCentralFileHeader($v_header)) != 1) {
445 $this->_closeFd();
446
447 return $v_result;
448 }
449
450 // Store the index
451 $v_header['index'] = $i;
452
453 // Store the file position
454 $v_pos_entry = ftell($this->_zip_fd);
455
456
457 // Go to the file position
458 @rewind($this->_zip_fd);
459 if (@fseek($this->_zip_fd, $v_header['offset'])) {
460 // Close the zip file
461 $this->_closeFd();
462
463 // Error log
464 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
465
466 // Return
467 return self::errorCode();
468 }
469
470 // Extracting the file
471 if (($v_result = $this->_extractFile($v_header, $p_path, $p_remove_path, $p_remove_all_path, $p_params)) != 1) {
472 // Close the zip file
473 $this->_closeFd();
474
475 return $v_result;
476 }
477
478 // Get the only interesting attributes
479 if (($v_result = $this->_convertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
480 // Close the zip file
481 $this->_closeFd();
482
483 return $v_result;
484 }
485 }
486
487 // Close the zip file
488 $this->_closeFd();
489
490 // Return
491 return $v_result;
492 }
493
494 /**
495 * tx_em_Tools_Unzip::_extractFile()
496 *
497 * { Description }
498 *
499 */
500 function _extractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_params) {
501 $v_result = 1;
502
503 // Read the file header
504 $v_header = '';
505 if (($v_result = $this->_readFileHeader($v_header)) != 1) {
506 // Return
507 return $v_result;
508 }
509
510
511 // Check that the file header is coherent with $p_entry info
512 // TBC
513
514 // Look for all path to remove
515 if ($p_remove_all_path == TRUE) {
516 // Get the basename of the path
517 $p_entry['filename'] = basename($p_entry['filename']);
518 }
519
520 // Look for path to remove
521 else {
522 if ($p_remove_path != "") {
523 if ($this->_tool_PathInclusion($p_remove_path, $p_entry['filename']) == 2) {
524
525 // Change the file status
526 $p_entry['status'] = "filtered";
527
528 // Return
529 return $v_result;
530 }
531
532 $p_remove_path_size = strlen($p_remove_path);
533 if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
534
535 // Remove the path
536 $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
537
538 }
539 }
540 }
541
542 // added by TYPO3 secteam to check for invalid paths
543 if (!t3lib_div::validPathStr($p_entry['filename'])) {
544 return $v_result;
545 }
546
547 // Add the path
548 if ($p_path != '') {
549 $p_entry['filename'] = $p_path . "/" . $p_entry['filename'];
550 }
551
552 // Look for pre-extract callback
553 if ((isset($p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT]))
554 && ($p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT] != '')) {
555
556 // Generate a local information
557 $v_local_header = array();
558 $this->_convertHeader2FileInfo($p_entry, $v_local_header);
559
560 // Call the callback
561 // Here I do not use call_user_func() because I need to send a reference to the
562 // header.
563 eval('$v_result = ' . $p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT] . '(ARCHIVE_ZIP_PARAM_PRE_EXTRACT, $v_local_header);');
564 if ($v_result == 0) {
565 // Change the file status
566 $p_entry['status'] = "skipped";
567 $v_result = 1;
568 }
569
570 // Update the informations
571 // Only some fields can be modified
572 $p_entry['filename'] = $v_local_header['filename'];
573 }
574
575 // Trace
576
577 // Look if extraction should be done
578 if ($p_entry['status'] == 'ok') {
579
580 // Look for specific actions while the file exist
581 if (file_exists($p_entry['filename'])) {
582 // Look if file is a directory
583 if (is_dir($p_entry['filename'])) {
584 // Change the file status
585 $p_entry['status'] = "already_a_directory";
586 }
587 // Look if file is write protected
588 else {
589 if (!is_writeable($p_entry['filename'])) {
590 // Change the file status
591 $p_entry['status'] = "write_protected";
592 }
593
594 // Look if the extracted file is older
595 else {
596 if (filemtime($p_entry['filename']) > $p_entry['mtime']) {
597 // Change the file status
598 $p_entry['status'] = "newer_exist";
599 }
600 }
601 }
602 }
603
604 // Check the directory availability and create it if necessary
605 else {
606 if ((($p_entry['external'] & 0x00000010) == 0x00000010) || (substr($p_entry['filename'], -1) == '/')) {
607 $v_dir_to_check = $p_entry['filename'];
608 }
609 else {
610 if (!strstr($p_entry['filename'], "/")) {
611 $v_dir_to_check = "";
612 }
613 else
614 {
615 $v_dir_to_check = dirname($p_entry['filename']);
616 }
617 }
618
619 if (($v_result = $this->_dirCheck($v_dir_to_check, (($p_entry['external'] & 0x00000010) == 0x00000010))) != 1) {
620 // Change the file status
621 $p_entry['status'] = "path_creation_fail";
622
623 // Return
624 $v_result = 1;
625 }
626 }
627 }
628
629 // Look if extraction should be done
630 if ($p_entry['status'] == 'ok') {
631 // Do the extraction (if not a folder)
632 if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
633 // Look for not compressed file
634 if ($p_entry['compressed_size'] == $p_entry['size']) {
635 // Opening destination file
636 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
637 // Change the file status
638 $p_entry['status'] = "write_error";
639
640 // Return
641 return $v_result;
642 }
643
644
645 // Read the file by ARCHIVE_ZIP_READ_BLOCK_SIZE octets blocks
646 $v_size = $p_entry['compressed_size'];
647 while ($v_size != 0) {
648 $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
649 $v_buffer = fread($this->_zip_fd, $v_read_size);
650 $v_binary_data = pack('a' . $v_read_size, $v_buffer);
651 @fwrite($v_dest_file, $v_binary_data, $v_read_size);
652 $v_size -= $v_read_size;
653 }
654
655 // Closing the destination file
656 fclose($v_dest_file);
657
658 // Change the file mtime
659 touch($p_entry['filename'], $p_entry['mtime']);
660 } else {
661 // Trace
662
663 // Opening destination file
664 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
665
666 // Change the file status
667 $p_entry['status'] = "write_error";
668
669 return $v_result;
670 }
671
672
673 // Read the compressed file in a buffer (one shot)
674 $v_buffer = @fread($this->_zip_fd, $p_entry['compressed_size']);
675
676 // Decompress the file
677 $v_file_content = gzinflate($v_buffer);
678 unset($v_buffer);
679
680 // Write the uncompressed data
681 @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
682 unset($v_file_content);
683
684 // Closing the destination file
685 @fclose($v_dest_file);
686
687 // Change the file mtime
688 @touch($p_entry['filename'], $p_entry['mtime']);
689 }
690
691 // Look for chmod option
692 if ((isset($p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD]))
693 && ($p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD] != 0)) {
694
695 // Change the mode of the file
696 chmod($p_entry['filename'], $p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD]);
697 }
698
699 }
700 }
701
702 // Look for post-extract callback
703 if ((isset($p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT]))
704 && ($p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT] != '')) {
705
706 // Generate a local information
707 $v_local_header = array();
708 $this->_convertHeader2FileInfo($p_entry, $v_local_header);
709
710 // Call the callback
711 // Here I do not use call_user_func() because I need to send a reference to the
712 // header.
713 eval('$v_result = ' . $p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT] . '(ARCHIVE_ZIP_PARAM_POST_EXTRACT, $v_local_header);');
714 }
715
716 // Return
717 return $v_result;
718 }
719
720 /**
721 * tx_em_Tools_Unzip::_readFileHeader()
722 *
723 * { Description }
724 *
725 */
726 function _readFileHeader(&$p_header) {
727 $v_result = 1;
728
729 // Read the 4 bytes signature
730 $v_binary_data = @fread($this->_zip_fd, 4);
731 $v_data = unpack('Vid', $v_binary_data);
732
733 // Check signature
734 if ($v_data['id'] != 0x04034b50) {
735
736 // Error log
737 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
738
739 // Return
740 return self::errorCode();
741 }
742
743 // Read the first 42 bytes of the header
744 $v_binary_data = fread($this->_zip_fd, 26);
745
746 // Look for invalid block size
747 if (strlen($v_binary_data) != 26) {
748 $p_header['filename'] = "";
749 $p_header['status'] = "invalid_header";
750
751 // Error log
752 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, "Invalid block size : " . strlen($v_binary_data));
753
754 // Return
755 return self::errorCode();
756 }
757
758 // Extract the values
759 $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
760
761 // Get filename
762 $p_header['filename'] = fread($this->_zip_fd, $v_data['filename_len']);
763
764 // Get extra_fields
765 if ($v_data['extra_len'] != 0) {
766 $p_header['extra'] = fread($this->_zip_fd, $v_data['extra_len']);
767 }
768 else {
769 $p_header['extra'] = '';
770 }
771
772 // Extract properties
773 $p_header['compression'] = $v_data['compression'];
774 $p_header['size'] = $v_data['size'];
775 $p_header['compressed_size'] = $v_data['compressed_size'];
776 $p_header['crc'] = $v_data['crc'];
777 $p_header['flag'] = $v_data['flag'];
778
779 // Recuperate date in UNIX format
780 $p_header['mdate'] = $v_data['mdate'];
781 $p_header['mtime'] = $v_data['mtime'];
782 if ($p_header['mdate'] && $p_header['mtime']) {
783 // Extract time
784 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
785 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
786 $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
787
788 // Extract date
789 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
790 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
791 $v_day = $p_header['mdate'] & 0x001F;
792
793 // Get UNIX date format
794 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
795
796 } else {
797 $p_header['mtime'] = $GLOBALS['EXEC_TIME'];
798 }
799
800 // Other informations
801
802 // Set the stored filename
803 $p_header['stored_filename'] = $p_header['filename'];
804
805 // Set the status field
806 $p_header['status'] = "ok";
807
808 // Return
809 return $v_result;
810 }
811
812 /**
813 * tx_em_Tools_Unzip::_readCentralFileHeader()
814 *
815 * { Description }
816 *
817 */
818 function _readCentralFileHeader(&$p_header) {
819 $v_result = 1;
820
821 // Read the 4 bytes signature
822 $v_binary_data = @fread($this->_zip_fd, 4);
823 $v_data = unpack('Vid', $v_binary_data);
824
825 // Check signature
826 if ($v_data['id'] != 0x02014b50) {
827
828 // Error log
829 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
830
831 // Return
832 return self::errorCode();
833 }
834
835 // Read the first 42 bytes of the header
836 $v_binary_data = fread($this->_zip_fd, 42);
837
838 // Look for invalid block size
839 if (strlen($v_binary_data) != 42) {
840 $p_header['filename'] = "";
841 $p_header['status'] = "invalid_header";
842
843 // Error log
844 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, "Invalid block size : " . strlen($v_binary_data));
845
846 // Return
847 return self::errorCode();
848 }
849
850 // Extract the values
851 $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
852
853 // Get filename
854 if ($p_header['filename_len'] != 0) {
855 $p_header['filename'] = fread($this->_zip_fd, $p_header['filename_len']);
856 }
857 else
858 {
859 $p_header['filename'] = '';
860 }
861
862 // Get extra
863 if ($p_header['extra_len'] != 0) {
864 $p_header['extra'] = fread($this->_zip_fd, $p_header['extra_len']);
865 }
866 else
867 {
868 $p_header['extra'] = '';
869 }
870
871 // Get comment
872 if ($p_header['comment_len'] != 0) {
873 $p_header['comment'] = fread($this->_zip_fd, $p_header['comment_len']);
874 }
875 else
876 {
877 $p_header['comment'] = '';
878 }
879
880 // Extract properties
881
882 // Recuperate date in UNIX format
883 if ($p_header['mdate'] && $p_header['mtime']) {
884 // Extract time
885 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
886 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
887 $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
888
889 // Extract date
890 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
891 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
892 $v_day = $p_header['mdate'] & 0x001F;
893
894 // Get UNIX date format
895 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
896
897 } else {
898 $p_header['mtime'] = $GLOBALS['EXEC_TIME'];
899 }
900
901 // Set the stored filename
902 $p_header['stored_filename'] = $p_header['filename'];
903
904 // Set default status to ok
905 $p_header['status'] = 'ok';
906
907 // Look if it is a directory
908 if (substr($p_header['filename'], -1) == '/') {
909 $p_header['external'] = 0x41FF0010;
910 }
911
912
913 // Return
914 return $v_result;
915 }
916
917 /**
918 * tx_em_Tools_Unzip::_readEndCentralDir()
919 *
920 * { Description }
921 *
922 */
923 function _readEndCentralDir(&$p_central_dir) {
924 $v_result = 1;
925
926 // Go to the end of the zip file
927 $v_size = filesize($this->_zipname);
928 @fseek($this->_zip_fd, $v_size);
929 if (@ftell($this->_zip_fd) != $v_size) {
930 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
931 'Unable to go to the end of the archive \''
932 . $this->_zipname . '\'');
933 return self::errorCode();
934 }
935
936 // First try : look if this is an archive with no commentaries
937 // (most of the time)
938 // in this case the end of central dir is at 22 bytes of the file end
939 $v_found = 0;
940 if ($v_size > 26) {
941 @fseek($this->_zip_fd, $v_size - 22);
942 if (($v_pos = @ftell($this->_zip_fd)) != ($v_size - 22)) {
943 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
944 'Unable to seek back to the middle of the archive \''
945 . $this->_zipname . '\'');
946 return self::errorCode();
947 }
948
949 // Read for bytes
950 $v_binary_data = @fread($this->_zip_fd, 4);
951 $v_data = unpack('Vid', $v_binary_data);
952
953 // Check signature
954 if ($v_data['id'] == 0x06054b50) {
955 $v_found = 1;
956 }
957
958 $v_pos = ftell($this->_zip_fd);
959 }
960
961 // Go back to the maximum possible size of the Central Dir End Record
962 if (!$v_found) {
963 $v_maximum_size = 65557; // 0xFFFF + 22;
964 if ($v_maximum_size > $v_size) {
965 $v_maximum_size = $v_size;
966 }
967 @fseek($this->_zip_fd, $v_size - $v_maximum_size);
968 if (@ftell($this->_zip_fd) != ($v_size - $v_maximum_size)) {
969 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
970 'Unable to seek back to the middle of the archive \''
971 . $this->_zipname . '\'');
972 return self::errorCode();
973 }
974
975 // Read byte per byte in order to find the signature
976 $v_pos = ftell($this->_zip_fd);
977 $v_bytes = 0x00000000;
978 while ($v_pos < $v_size) {
979 // Read a byte
980 $v_byte = @fread($this->_zip_fd, 1);
981
982 // Add the byte
983 $v_bytes = ($v_bytes << 8) | Ord($v_byte);
984
985 // Compare the bytes
986 if ($v_bytes == 0x504b0506) {
987 $v_pos++;
988 break;
989 }
990
991 $v_pos++;
992 }
993
994 // Look if not found end of central dir
995 if ($v_pos == $v_size) {
996 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
997 "Unable to find End of Central Dir Record signature");
998 return self::errorCode();
999 }
1000 }
1001
1002 // Read the first 18 bytes of the header
1003 $v_binary_data = fread($this->_zip_fd, 18);
1004
1005 // Look for invalid block size
1006 if (strlen($v_binary_data) != 18) {
1007 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
1008 "Invalid End of Central Dir Record size : "
1009 . strlen($v_binary_data));
1010 return self::errorCode();
1011 }
1012
1013 // Extract the values
1014 $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
1015
1016 // Check the global size
1017 if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
1018 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
1019 "Fail to find the right signature");
1020 return self::errorCode();
1021 }
1022
1023 // Get comment
1024 if ($v_data['comment_size'] != 0) {
1025 $p_central_dir['comment'] = fread($this->_zip_fd, $v_data['comment_size']);
1026 }
1027 else
1028 {
1029 $p_central_dir['comment'] = '';
1030 }
1031
1032 $p_central_dir['entries'] = $v_data['entries'];
1033 $p_central_dir['disk_entries'] = $v_data['disk_entries'];
1034 $p_central_dir['offset'] = $v_data['offset'];
1035 $p_central_dir['size'] = $v_data['size'];
1036 $p_central_dir['disk'] = $v_data['disk'];
1037 $p_central_dir['disk_start'] = $v_data['disk_start'];
1038
1039 // Return
1040 return $v_result;
1041 }
1042
1043 /**
1044 * tx_em_Tools_Unzip::_dirCheck()
1045 *
1046 * { Description }
1047 *
1048 * @param [type] $p_is_dir
1049 */
1050 function _dirCheck($p_dir, $p_is_dir = FALSE) {
1051 $v_result = 1;
1052
1053 // Remove the final '/'
1054 if (($p_is_dir) && (substr($p_dir, -1) == '/')) {
1055 $p_dir = substr($p_dir, 0, strlen($p_dir) - 1);
1056 }
1057
1058 // Check the directory availability
1059 if ((is_dir($p_dir)) || ($p_dir == "")) {
1060 return 1;
1061 }
1062
1063 // Extract parent directory
1064 $p_parent_dir = dirname($p_dir);
1065
1066 // Just a check
1067 if ($p_parent_dir != $p_dir) {
1068 // Look for parent directory
1069 if ($p_parent_dir != "") {
1070 if (($v_result = $this->_dirCheck($p_parent_dir)) != 1) {
1071 return $v_result;
1072 }
1073 }
1074 }
1075
1076 // Create the directory
1077 if (!@mkdir($p_dir, 0777)) {
1078 $this->_errorLog(ARCHIVE_ZIP_ERR_DIR_CREATE_FAIL,
1079 'Unable to create directory "' . $p_dir . '"');
1080 return self::errorCode();
1081 }
1082
1083 // Return
1084 return $v_result;
1085 }
1086
1087 /**
1088 * tx_em_Tools_Unzip::_check_parameters()
1089 *
1090 * { Description }
1091 *
1092 * @param integer $p_error_code
1093 * @param string $p_error_string
1094 */
1095 function _check_parameters(&$p_params, $p_default) {
1096
1097 // Check that param is an array
1098 if (!is_array($p_params)) {
1099 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
1100 'Unsupported parameter, waiting for an array');
1101 return self::errorCode();
1102 }
1103
1104 // Check that all the params are valid
1105 for (reset($p_params); list($v_key, $v_value) = each($p_params);) {
1106 if (!isset($p_default[$v_key])) {
1107 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
1108 'Unsupported parameter with key \'' . $v_key . '\'');
1109
1110 return self::errorCode();
1111 }
1112 }
1113
1114 // Set the default values
1115 for (reset($p_default); list($v_key, $v_value) = each($p_default);) {
1116 if (!isset($p_params[$v_key])) {
1117 $p_params[$v_key] = $p_default[$v_key];
1118 }
1119 }
1120
1121 // Check specific parameters
1122 $v_callback_list = array('callback_pre_add', 'callback_post_add',
1123 'callback_pre_extract', 'callback_post_extract');
1124 for ($i = 0; $i < sizeof($v_callback_list); $i++) {
1125 $v_key = $v_callback_list[$i];
1126 if ((isset($p_params[$v_key])) && ($p_params[$v_key] != '')) {
1127 if (!function_exists($p_params[$v_key])) {
1128 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAM_VALUE,
1129 "Callback '" . $p_params[$v_key]
1130 . "()' is not an existing function for "
1131 . "parameter '" . $v_key . "'");
1132 return self::errorCode();
1133 }
1134 }
1135 }
1136
1137 return (1);
1138 }
1139
1140 /**
1141 * tx_em_Tools_Unzip::_errorLog()
1142 *
1143 * { Description }
1144 *
1145 * @param integer $p_error_code
1146 * @param string $p_error_string
1147 */
1148 function _errorLog($p_error_code = 0, $p_error_string = '') {
1149 $this->_error_code = $p_error_code;
1150 $this->_error_string = $p_error_string;
1151 }
1152
1153 /**
1154 * tx_em_Tools_Unzip::_errorReset()
1155 *
1156 * { Description }
1157 *
1158 */
1159 function _errorReset() {
1160 $this->_error_code = 1;
1161 $this->_error_string = '';
1162 }
1163
1164 /**
1165 * _tool_PathReduction()
1166 *
1167 * { Description }
1168 *
1169 */
1170 function _tool_PathReduction($p_dir) {
1171 $v_result = "";
1172
1173 // Look for not empty path
1174 if ($p_dir != "") {
1175 // Explode path by directory names
1176 $v_list = explode("/", $p_dir);
1177
1178 // Study directories from last to first
1179 for ($i = sizeof($v_list) - 1; $i >= 0; $i--) {
1180 // Look for current path
1181 if ($v_list[$i] == ".") {
1182 // Ignore this directory
1183 // Should be the first $i=0, but no check is done
1184 } else {
1185 if ($v_list[$i] == "..") {
1186 // Ignore it and ignore the $i-1
1187 $i--;
1188 } else {
1189 if (($v_list[$i] == "") && ($i != (sizeof($v_list) - 1)) && ($i != 0)) {
1190 // Ignore only the double '//' in path,
1191 // but not the first and last '/'
1192 } else {
1193 $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ? "/" . $v_result : "");
1194 }
1195 }
1196 }
1197 }
1198 }
1199
1200 // Return
1201 return $v_result;
1202 }
1203
1204 /**
1205 * _tool_PathInclusion()
1206 *
1207 * { Description }
1208 *
1209 */
1210 function _tool_PathInclusion($p_dir, $p_path) {
1211 $v_result = 1;
1212
1213 // Explode dir and path by directory separator
1214 $v_list_dir = explode("/", $p_dir);
1215 $v_list_dir_size = sizeof($v_list_dir);
1216 $v_list_path = explode("/", $p_path);
1217 $v_list_path_size = sizeof($v_list_path);
1218
1219 // Study directories paths
1220 $i = 0;
1221 $j = 0;
1222 while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
1223 // Look for empty dir (path reduction)
1224 if ($v_list_dir[$i] == '') {
1225 $i++;
1226 continue;
1227 }
1228 if ($v_list_path[$j] == '') {
1229 $j++;
1230 continue;
1231 }
1232
1233 // Compare the items
1234 if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ($v_list_path[$j] != '')) {
1235 $v_result = 0;
1236 }
1237
1238 // Next items
1239 $i++;
1240 $j++;
1241 }
1242
1243 // Look if everything seems to be the same
1244 if ($v_result) {
1245 // Skip all the empty items
1246 while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) {
1247 $j++;
1248 }
1249 while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) {
1250 $i++;
1251 }
1252
1253 if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
1254 // There are exactly the same
1255 $v_result = 2;
1256 } else {
1257 if ($i < $v_list_dir_size) {
1258 // The path is shorter than the dir
1259 $v_result = 0;
1260 }
1261 }
1262 }
1263
1264 // Return
1265 return $v_result;
1266 }
1267
1268 /**
1269 * _tool_CopyBlock()
1270 *
1271 * { Description }
1272 *
1273 * @param integer $p_mode
1274 */
1275 function _tool_CopyBlock($p_src, $p_dest, $p_size, $p_mode = 0) {
1276 $v_result = 1;
1277
1278 if ($p_mode == 0) {
1279 while ($p_size != 0) {
1280 $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
1281 ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
1282 $v_buffer = @fread($p_src, $v_read_size);
1283 @fwrite($p_dest, $v_buffer, $v_read_size);
1284 $p_size -= $v_read_size;
1285 }
1286 } else {
1287 if ($p_mode == 1) {
1288 while ($p_size != 0) {
1289 $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
1290 ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
1291 $v_buffer = @gzread($p_src, $v_read_size);
1292 @fwrite($p_dest, $v_buffer, $v_read_size);
1293 $p_size -= $v_read_size;
1294 }
1295 } else {
1296 if ($p_mode == 2) {
1297 while ($p_size != 0) {
1298 $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
1299 ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
1300 $v_buffer = @fread($p_src, $v_read_size);
1301 @gzwrite($p_dest, $v_buffer, $v_read_size);
1302 $p_size -= $v_read_size;
1303 }
1304 }
1305 else {
1306 if ($p_mode == 3) {
1307 while ($p_size != 0) {
1308 $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
1309 ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
1310 $v_buffer = @gzread($p_src, $v_read_size);
1311 @gzwrite($p_dest, $v_buffer, $v_read_size);
1312 $p_size -= $v_read_size;
1313 }
1314 }
1315 }
1316 }
1317 }
1318
1319 // Return
1320 return $v_result;
1321 }
1322
1323 /**
1324 * _tool_Rename()
1325 *
1326 * { Description }
1327 *
1328 */
1329 function _tool_Rename($p_src, $p_dest) {
1330 $v_result = 1;
1331
1332 // Try to rename the files
1333 if (!@rename($p_src, $p_dest)) {
1334
1335 // Try to copy & unlink the src
1336 if (!@copy($p_src, $p_dest)) {
1337 $v_result = 0;
1338 } else {
1339 if (!@unlink($p_src)) {
1340 $v_result = 0;
1341 }
1342 }
1343 }
1344
1345 // Return
1346 return $v_result;
1347 }
1348
1349 /**
1350 * _tool_TranslateWinPath()
1351 *
1352 * { Description }
1353 *
1354 * @param [type] $p_remove_disk_letter
1355 */
1356 function _tool_TranslateWinPath($p_path, $p_remove_disk_letter = TRUE) {
1357 if (stristr(php_uname(), 'windows')) {
1358 // Look for potential disk letter
1359 if (($p_remove_disk_letter)
1360 && (($v_position = strpos($p_path, ':')) != FALSE)) {
1361 $p_path = substr($p_path, $v_position + 1);
1362 }
1363 // Change potential windows directory separator
1364 if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
1365 $p_path = strtr($p_path, '\\', '/');
1366 }
1367 }
1368 return $p_path;
1369 }
1370 }
1371 ?>