2 /***************************************************************
5 * (c) Vincent Blavet <vincent@phpconcept.net>
6 * (c) 2005-2010 Karsten Dambekalns <karsten@typo3.org>
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.
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.
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,
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
27 * Module: Extension manager
29 * @author Vincent Blavet <vincent@phpconcept.net>
30 * @author Karsten Dambekalns <karsten@typo3.org>
35 define('ARCHIVE_ZIP_READ_BLOCK_SIZE', 2048);
37 // File list separator
38 define('ARCHIVE_ZIP_SEPARATOR', ',');
40 define('ARCHIVE_ZIP_TEMPORARY_DIR', '');
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);
62 define('ARCHIVE_ZIP_WARN_NO_WARNING', 0);
63 define('ARCHIVE_ZIP_WARN_FILE_EXIST', 1);
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');
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');
81 * Class for unpacking zip archive files
83 * @author Vincent Blavet <vincent@blavet.net>
84 * @author Karsten Dambekalns <karsten@typo3.org>
86 class tx_em_Tools_Unzip
{
88 * The filename of the zip archive.
90 * @var string Name of the Zip file
95 * File descriptor of the opened Zip file.
97 * @var int Internal zip file descriptor
102 * @var int last error code
104 var $_error_code = 1;
107 * @var string Last error description
109 var $_error_string = '';
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
116 * @param string $p_zipname The name of the zip archive to create
119 public function __construct($p_zipname) {
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",
131 // Set the attributes
132 $this->_zipname
= $p_zipname;
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
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
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.
158 function extract($p_params = 0) {
159 $this->_errorReset();
162 if (!$this->_checkFormat()) {
166 // Set default values
167 if ($p_params === 0) {
170 if ($this->_check_parameters($p_params,
171 array('extract_as_string' => FALSE,
174 'remove_all_path' => FALSE,
175 'callback_pre_extract' => '',
176 'callback_post_extract' => '',
177 'set_chmod' => 0)) != 1) {
181 // Call the extracting fct
183 if ($this->_extractByRule($v_list, $p_params) != 1) {
192 * Method that gives the lastest error code.
195 * @return integer The error code value.
197 function errorCode() {
198 return ($this->_error_code
);
202 * This method gives the latest error code name.
205 * @param boolean $p_with_code If TRUE, gives the name and the int value.
206 * @return string The error name.
208 function errorName($p_with_code = FALSE) {
209 $v_const_list = get_defined_constants();
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;
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) {
228 return ($v_value . ' (' . $this->_error_code
. ')');
235 * This method returns the description associated with the latest error.
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.
244 function errorInfo($p_full = FALSE) {
246 return ($this->errorName(TRUE) . " : " . $this->_error_string
);
248 return ($this->_error_string
. " [code " . $this->_error_code
. "]");
254 * tx_em_Tools_Unzip::_checkFormat()
258 * @param integer $p_level
260 function _checkFormat($p_level = 0) {
263 // Reset the error handler
264 $this->_errorReset();
266 // Look if the file exits
267 if (!is_file($this->_zipname
)) {
269 $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE
,
270 "Missing archive file '" . $this->_zipname
. "'");
274 // Check that the file is readeable
275 if (!is_readable($this->_zipname
)) {
277 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL
,
278 "Unable to read archive '" . $this->_zipname
. "'");
282 // Check the magic code
285 // Check the central header
288 // Check each file header
302 function _openFd($p_mode) {
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();
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();
325 * tx_em_Tools_Unzip::_closeFd()
330 function _closeFd() {
333 if ($this->_zip_fd
!= 0) {
334 @fclose
($this->_zip_fd
);
344 * tx_em_Tools_Unzip::_convertHeader2FileInfo()
349 function _convertHeader2FileInfo($p_header, &$p_info) {
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'];
368 // Function : _extractByRule()
370 // Extract a file or directory depending of rules (by index, by name, ...)
372 // $p_file_list : An array where will be placed the properties of each
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.
381 // 1 on success,0 or less on error (see error code list)
384 * tx_em_Tools_Unzip::_extractByRule()
389 function _extractByRule(&$p_file_list, &$p_params) {
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'];
398 ||
((substr($p_path, 0, 1) != "/")
399 && (substr($p_path, 0, 3) != "../") && (substr($p_path, 1, 2) != ":/"))) {
400 $p_path = "./" . $p_path;
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);
412 if (($v_result = $this->_openFd('rb')) != 1) {
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
425 // Start at beginning of Central Dir
426 $v_pos_entry = $v_central_dir['offset'];
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)) {
436 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP
,
437 'Invalid archive size');
439 return self
::errorCode();
442 // Read the file header
444 if (($v_result = $this->_readCentralFileHeader($v_header)) != 1) {
451 $v_header['index'] = $i;
453 // Store the file position
454 $v_pos_entry = ftell($this->_zip_fd
);
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
464 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP
, 'Invalid archive size');
467 return self
::errorCode();
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
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
487 // Close the zip file
495 * tx_em_Tools_Unzip::_extractFile()
500 function _extractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_params) {
503 // Read the file header
505 if (($v_result = $this->_readFileHeader($v_header)) != 1) {
511 // Check that the file header is coherent with $p_entry info
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']);
520 // Look for path to remove
522 if ($p_remove_path != "") {
523 if ($this->_tool_PathInclusion($p_remove_path, $p_entry['filename']) == 2) {
525 // Change the file status
526 $p_entry['status'] = "filtered";
532 $p_remove_path_size = strlen($p_remove_path);
533 if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
536 $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
542 // added by TYPO3 secteam to check for invalid paths
543 if (!t3lib_div
::validPathStr($p_entry['filename'])) {
549 $p_entry['filename'] = $p_path . "/" . $p_entry['filename'];
552 // Look for pre-extract callback
553 if ((isset($p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT
]))
554 && ($p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT
] != '')) {
556 // Generate a local information
557 $v_local_header = array();
558 $this->_convertHeader2FileInfo($p_entry, $v_local_header);
561 // Here I do not use call_user_func() because I need to send a reference to the
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";
570 // Update the informations
571 // Only some fields can be modified
572 $p_entry['filename'] = $v_local_header['filename'];
577 // Look if extraction should be done
578 if ($p_entry['status'] == 'ok') {
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";
587 // Look if file is write protected
589 if (!is_writeable($p_entry['filename'])) {
590 // Change the file status
591 $p_entry['status'] = "write_protected";
594 // Look if the extracted file is older
596 if (filemtime($p_entry['filename']) > $p_entry['mtime']) {
597 // Change the file status
598 $p_entry['status'] = "newer_exist";
604 // Check the directory availability and create it if necessary
606 if ((($p_entry['external'] & 0x00000010) == 0x00000010) ||
(substr($p_entry['filename'], -1) == '/')) {
607 $v_dir_to_check = $p_entry['filename'];
610 if (!strstr($p_entry['filename'], "/")) {
611 $v_dir_to_check = "";
615 $v_dir_to_check = dirname($p_entry['filename']);
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";
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";
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;
655 // Closing the destination file
656 fclose($v_dest_file);
658 // Change the file mtime
659 touch($p_entry['filename'], $p_entry['mtime']);
663 // Opening destination file
664 if (($v_dest_file = @fopen
($p_entry['filename'], 'wb')) == 0) {
666 // Change the file status
667 $p_entry['status'] = "write_error";
673 // Read the compressed file in a buffer (one shot)
674 $v_buffer = @fread
($this->_zip_fd
, $p_entry['compressed_size']);
676 // Decompress the file
677 $v_file_content = gzinflate($v_buffer);
680 // Write the uncompressed data
681 @fwrite
($v_dest_file, $v_file_content, $p_entry['size']);
682 unset($v_file_content);
684 // Closing the destination file
685 @fclose
($v_dest_file);
687 // Change the file mtime
688 @touch
($p_entry['filename'], $p_entry['mtime']);
691 // Look for chmod option
692 if ((isset($p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD
]))
693 && ($p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD
] != 0)) {
695 // Change the mode of the file
696 chmod($p_entry['filename'], $p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD
]);
702 // Look for post-extract callback
703 if ((isset($p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT
]))
704 && ($p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT
] != '')) {
706 // Generate a local information
707 $v_local_header = array();
708 $this->_convertHeader2FileInfo($p_entry, $v_local_header);
711 // Here I do not use call_user_func() because I need to send a reference to the
713 eval('$v_result = ' . $p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT
] . '(ARCHIVE_ZIP_PARAM_POST_EXTRACT, $v_local_header);');
721 * tx_em_Tools_Unzip::_readFileHeader()
726 function _readFileHeader(&$p_header) {
729 // Read the 4 bytes signature
730 $v_binary_data = @fread
($this->_zip_fd
, 4);
731 $v_data = unpack('Vid', $v_binary_data);
734 if ($v_data['id'] != 0x04034b50) {
737 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT
, 'Invalid archive structure');
740 return self
::errorCode();
743 // Read the first 42 bytes of the header
744 $v_binary_data = fread($this->_zip_fd
, 26);
746 // Look for invalid block size
747 if (strlen($v_binary_data) != 26) {
748 $p_header['filename'] = "";
749 $p_header['status'] = "invalid_header";
752 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT
, "Invalid block size : " . strlen($v_binary_data));
755 return self
::errorCode();
758 // Extract the values
759 $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
762 $p_header['filename'] = fread($this->_zip_fd
, $v_data['filename_len']);
765 if ($v_data['extra_len'] != 0) {
766 $p_header['extra'] = fread($this->_zip_fd
, $v_data['extra_len']);
769 $p_header['extra'] = '';
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'];
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']) {
784 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
785 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
786 $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
789 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) +
1980;
790 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
791 $v_day = $p_header['mdate'] & 0x001F;
793 // Get UNIX date format
794 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
797 $p_header['mtime'] = $GLOBALS['EXEC_TIME'];
800 // Other informations
802 // Set the stored filename
803 $p_header['stored_filename'] = $p_header['filename'];
805 // Set the status field
806 $p_header['status'] = "ok";
813 * tx_em_Tools_Unzip::_readCentralFileHeader()
818 function _readCentralFileHeader(&$p_header) {
821 // Read the 4 bytes signature
822 $v_binary_data = @fread
($this->_zip_fd
, 4);
823 $v_data = unpack('Vid', $v_binary_data);
826 if ($v_data['id'] != 0x02014b50) {
829 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT
, 'Invalid archive structure');
832 return self
::errorCode();
835 // Read the first 42 bytes of the header
836 $v_binary_data = fread($this->_zip_fd
, 42);
838 // Look for invalid block size
839 if (strlen($v_binary_data) != 42) {
840 $p_header['filename'] = "";
841 $p_header['status'] = "invalid_header";
844 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT
, "Invalid block size : " . strlen($v_binary_data));
847 return self
::errorCode();
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);
854 if ($p_header['filename_len'] != 0) {
855 $p_header['filename'] = fread($this->_zip_fd
, $p_header['filename_len']);
859 $p_header['filename'] = '';
863 if ($p_header['extra_len'] != 0) {
864 $p_header['extra'] = fread($this->_zip_fd
, $p_header['extra_len']);
868 $p_header['extra'] = '';
872 if ($p_header['comment_len'] != 0) {
873 $p_header['comment'] = fread($this->_zip_fd
, $p_header['comment_len']);
877 $p_header['comment'] = '';
880 // Extract properties
882 // Recuperate date in UNIX format
883 if ($p_header['mdate'] && $p_header['mtime']) {
885 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
886 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
887 $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
890 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) +
1980;
891 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
892 $v_day = $p_header['mdate'] & 0x001F;
894 // Get UNIX date format
895 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
898 $p_header['mtime'] = $GLOBALS['EXEC_TIME'];
901 // Set the stored filename
902 $p_header['stored_filename'] = $p_header['filename'];
904 // Set default status to ok
905 $p_header['status'] = 'ok';
907 // Look if it is a directory
908 if (substr($p_header['filename'], -1) == '/') {
909 $p_header['external'] = 0x41FF0010;
918 * tx_em_Tools_Unzip::_readEndCentralDir()
923 function _readEndCentralDir(&$p_central_dir) {
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();
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
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();
950 $v_binary_data = @fread
($this->_zip_fd
, 4);
951 $v_data = unpack('Vid', $v_binary_data);
954 if ($v_data['id'] == 0x06054b50) {
958 $v_pos = ftell($this->_zip_fd
);
961 // Go back to the maximum possible size of the Central Dir End Record
963 $v_maximum_size = 65557; // 0xFFFF + 22;
964 if ($v_maximum_size > $v_size) {
965 $v_maximum_size = $v_size;
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();
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) {
980 $v_byte = @fread
($this->_zip_fd
, 1);
983 $v_bytes = ($v_bytes << 8) |
Ord($v_byte);
986 if ($v_bytes == 0x504b0506) {
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();
1002 // Read the first 18 bytes of the header
1003 $v_binary_data = fread($this->_zip_fd
, 18);
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();
1013 // Extract the values
1014 $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
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();
1024 if ($v_data['comment_size'] != 0) {
1025 $p_central_dir['comment'] = fread($this->_zip_fd
, $v_data['comment_size']);
1029 $p_central_dir['comment'] = '';
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'];
1044 * tx_em_Tools_Unzip::_dirCheck()
1048 * @param [type] $p_is_dir
1050 function _dirCheck($p_dir, $p_is_dir = FALSE) {
1053 // Remove the final '/'
1054 if (($p_is_dir) && (substr($p_dir, -1) == '/')) {
1055 $p_dir = substr($p_dir, 0, strlen($p_dir) - 1);
1058 // Check the directory availability
1059 if ((is_dir($p_dir)) ||
($p_dir == "")) {
1063 // Extract parent directory
1064 $p_parent_dir = dirname($p_dir);
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) {
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();
1088 * tx_em_Tools_Unzip::_check_parameters()
1092 * @param integer $p_error_code
1093 * @param string $p_error_string
1095 function _check_parameters(&$p_params, $p_default) {
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();
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 . '\'');
1110 return self
::errorCode();
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];
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();
1141 * tx_em_Tools_Unzip::_errorLog()
1145 * @param integer $p_error_code
1146 * @param string $p_error_string
1148 function _errorLog($p_error_code = 0, $p_error_string = '') {
1149 $this->_error_code
= $p_error_code;
1150 $this->_error_string
= $p_error_string;
1154 * tx_em_Tools_Unzip::_errorReset()
1159 function _errorReset() {
1160 $this->_error_code
= 1;
1161 $this->_error_string
= '';
1165 * _tool_PathReduction()
1170 function _tool_PathReduction($p_dir) {
1173 // Look for not empty path
1175 // Explode path by directory names
1176 $v_list = explode("/", $p_dir);
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
1185 if ($v_list[$i] == "..") {
1186 // Ignore it and ignore the $i-1
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 '/'
1193 $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ?
"/" . $v_result : "");
1205 * _tool_PathInclusion()
1210 function _tool_PathInclusion($p_dir, $p_path) {
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);
1219 // Study directories paths
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] == '') {
1228 if ($v_list_path[$j] == '') {
1233 // Compare the items
1234 if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ($v_list_path[$j] != '')) {
1243 // Look if everything seems to be the same
1245 // Skip all the empty items
1246 while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) {
1249 while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) {
1253 if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
1254 // There are exactly the same
1257 if ($i < $v_list_dir_size) {
1258 // The path is shorter than the dir
1273 * @param integer $p_mode
1275 function _tool_CopyBlock($p_src, $p_dest, $p_size, $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;
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;
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;
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;
1329 function _tool_Rename($p_src, $p_dest) {
1332 // Try to rename the files
1333 if (!@rename
($p_src, $p_dest)) {
1335 // Try to copy & unlink the src
1336 if (!@copy
($p_src, $p_dest)) {
1339 if (!@unlink
($p_src)) {
1350 * _tool_TranslateWinPath()
1354 * @param [type] $p_remove_disk_letter
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);
1363 // Change potential windows directory separator
1364 if ((strpos($p_path, '\\') > 0) ||
(substr($p_path, 0, 1) == '\\')) {
1365 $p_path = strtr($p_path, '\\', '/');