XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
Archive_Zip.php
1<?php
2/* vim: set ts=4 sw=4: */
3// +----------------------------------------------------------------------+
4// | PHP Version 4 |
5// +----------------------------------------------------------------------+
6// | Copyright (c) 1997-2003 The PHP Group |
7// +----------------------------------------------------------------------+
8// | This library is free software; you can redistribute it and/or |
9// | modify it under the terms of the GNU Lesser General Public |
10// | License as published by the Free Software Foundation; either |
11// | version 2.1 of the License, or (at your option) any later version. |
12// | |
13// | This library is distributed in the hope that it will be useful, |
14// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16// | Lesser General Public License for more details. |
17// | |
18// | You should have received a copy of the GNU Lesser General Public |
19// | License along with this library; if not, write to the Free Software |
20// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
21// | MA 02110-1301 USA |
22// +----------------------------------------------------------------------+
23// | Author: Vincent Blavet <vincent@phpconcept.net> |
24// +----------------------------------------------------------------------+
25//
26// $Id: Zip.php,v 1.2 2005/11/21 06:51:57 vblavet Exp $
27
28// require_once 'PEAR.php'; // GIJ
29
30 // ----- Constants
31 define('ARCHIVE_ZIP_READ_BLOCK_SIZE', 2048);
32
33 // ----- File list separator
34 define('ARCHIVE_ZIP_SEPARATOR', ',');
35
36 // ----- Optional static temporary directory
37 // By default temporary files are generated in the script current
38 // path.
39 // If defined :
40 // - MUST BE terminated by a '/'.
41 // - MUST be a valid, already created directory
42 // Samples :
43 // define( 'ARCHIVE_ZIP_TEMPORARY_DIR', '/temp/' );
44 // define( 'ARCHIVE_ZIP_TEMPORARY_DIR', 'C:/Temp/' );
45 define('ARCHIVE_ZIP_TEMPORARY_DIR', '');
46
47 // ----- Error codes
48 define('ARCHIVE_ZIP_ERR_NO_ERROR', 0);
49 define('ARCHIVE_ZIP_ERR_WRITE_OPEN_FAIL', -1);
50 define('ARCHIVE_ZIP_ERR_READ_OPEN_FAIL', -2);
51 define('ARCHIVE_ZIP_ERR_INVALID_PARAMETER', -3);
52 define('ARCHIVE_ZIP_ERR_MISSING_FILE', -4);
53 define('ARCHIVE_ZIP_ERR_FILENAME_TOO_LONG', -5);
54 define('ARCHIVE_ZIP_ERR_INVALID_ZIP', -6);
55 define('ARCHIVE_ZIP_ERR_BAD_EXTRACTED_FILE', -7);
56 define('ARCHIVE_ZIP_ERR_DIR_CREATE_FAIL', -8);
57 define('ARCHIVE_ZIP_ERR_BAD_EXTENSION', -9);
58 define('ARCHIVE_ZIP_ERR_BAD_FORMAT', -10);
59 define('ARCHIVE_ZIP_ERR_DELETE_FILE_FAIL', -11);
60 define('ARCHIVE_ZIP_ERR_RENAME_FILE_FAIL', -12);
61 define('ARCHIVE_ZIP_ERR_BAD_CHECKSUM', -13);
62 define('ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP', -14);
63 define('ARCHIVE_ZIP_ERR_MISSING_OPTION_VALUE', -15);
64 define('ARCHIVE_ZIP_ERR_INVALID_PARAM_VALUE', -16);
65
66 // ----- Warning codes
67 define('ARCHIVE_ZIP_WARN_NO_WARNING', 0);
68 define('ARCHIVE_ZIP_WARN_FILE_EXIST', 1);
69
70 // ----- Methods parameters
71 define('ARCHIVE_ZIP_PARAM_PATH', 'path');
72 define('ARCHIVE_ZIP_PARAM_ADD_PATH', 'add_path');
73 define('ARCHIVE_ZIP_PARAM_REMOVE_PATH', 'remove_path');
74 define('ARCHIVE_ZIP_PARAM_REMOVE_ALL_PATH', 'remove_all_path');
75 define('ARCHIVE_ZIP_PARAM_SET_CHMOD', 'set_chmod');
76 define('ARCHIVE_ZIP_PARAM_EXTRACT_AS_STRING', 'extract_as_string');
77 define('ARCHIVE_ZIP_PARAM_NO_COMPRESSION', 'no_compression');
78 define('ARCHIVE_ZIP_PARAM_BY_NAME', 'by_name');
79 define('ARCHIVE_ZIP_PARAM_BY_INDEX', 'by_index');
80 define('ARCHIVE_ZIP_PARAM_BY_EREG', 'by_ereg');
81 define('ARCHIVE_ZIP_PARAM_BY_PREG', 'by_preg');
82
83 define('ARCHIVE_ZIP_PARAM_PRE_EXTRACT', 'callback_pre_extract');
84 define('ARCHIVE_ZIP_PARAM_POST_EXTRACT', 'callback_post_extract');
85 define('ARCHIVE_ZIP_PARAM_PRE_ADD', 'callback_pre_add');
86 define('ARCHIVE_ZIP_PARAM_POST_ADD', 'callback_post_add');
87
88
89
103{
109 public $_zipname='';
110
116 public $_zip_fd=0;
117
121 public $_error_code=1;
122
126 public $_error_string='';
127
128 // {{{ constructor
137 public function __construct($p_zipname)
138 {
139
140 // ----- Check the zlib
141/* if (!extension_loaded('zlib')) {
142 PEAR::loadExtension('zlib');
143 }*/ // GIJ
144 if (!extension_loaded('zlib')) {
145 die("The extension 'zlib' couldn't be found.\n". 'Please make sure your version of PHP was built ' .
146 "with 'zlib' support.\n");
147 return false;
148 }
149
150 // ----- Set the attributes
151 $this->_zipname = $p_zipname;
152 $this->_zip_fd = 0;
153
154 return;
155 }
156 // }}}
157
158 // {{{ create()
181 public function create($p_filelist, $p_params=0)
182 {
183 $this->_errorReset();
184
185 // ----- Set default values
186 if (0 === $p_params) {
187 $p_params = [];
188 }
189 if (1 != $this->_check_parameters($p_params,
190 [
191 'no_compression' => false,
192 'add_path' => '',
193 'remove_path' => '',
194 'remove_all_path' => false
195 ]
196 )) {
197 return 0;
198 }
199
200 // ----- Look if the $p_filelist is really an array
201 $p_result_list = [];
202 if (is_array($p_filelist)) {
203 $v_result = $this->_create($p_filelist, $p_result_list, $p_params);
204 }
205
206 // ----- Look if the $p_filelist is a string
207 elseif (is_string($p_filelist)) {
208 // ----- Create a list with the elements from the string
209 $v_list = explode(ARCHIVE_ZIP_SEPARATOR, $p_filelist);
210
211 $v_result = $this->_create($v_list, $p_result_list, $p_params);
212 }
213
214 // ----- Invalid variable
215 else {
216 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
217 'Invalid variable type p_filelist');
218 $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
219 }
220
221 if (1 != $v_result) {
222 return 0;
223 }
224
225 return $p_result_list;
226 }
227 // }}}
228
229 // {{{ add()
255 public function add($p_filelist, $p_params=0)
256 {
257 $this->_errorReset();
258
259 // ----- Set default values
260 if (0 === $p_params) {
261 $p_params = [];
262 }
263 if (1 != $this->_check_parameters($p_params,
264 [
265 'no_compression' => false,
266 'add_path' => '',
267 'remove_path' => '',
268 'remove_all_path' => false,
269 'callback_pre_add' => '',
270 'callback_post_add' => ''
271 ]
272 )) {
273 return 0;
274 }
275
276 // ----- Look if the $p_filelist is really an array
277 $p_result_list = [];
278 if (is_array($p_filelist)) {
279 // ----- Call the create fct
280 $v_result = $this->_add($p_filelist, $p_result_list, $p_params);
281 }
282
283 // ----- Look if the $p_filelist is a string
284 elseif (is_string($p_filelist)) {
285 // ----- Create a list with the elements from the string
286 $v_list = explode(ARCHIVE_ZIP_SEPARATOR, $p_filelist);
287
288 // ----- Call the create fct
289 $v_result = $this->_add($v_list, $p_result_list, $p_params);
290 }
291
292 // ----- Invalid variable
293 else {
294 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
295 'add() : Invalid variable type p_filelist'
296 );
297 $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
298 }
299
300 if (1 != $v_result) {
301 return 0;
302 }
303
304 // ----- Return the result list
305 return $p_result_list;
306 }
307 // }}}
308
309 // {{{ listContent()
353 public function listContent()
354 {
355 $this->_errorReset();
356
357 // ----- Check archive
358 if (!$this->_checkFormat()) {
359 return(0);
360 }
361
362 $v_list = [];
363 if (1 != $this->_list($v_list)) {
364 unset($v_list);
365 return(0);
366 }
367
368 return $v_list;
369 }
370 // }}}
371
372 // {{{ extract()
410 public function extract($p_params=0)
411 {
412 $this->_errorReset();
413
414 // ----- Check archive
415 if (!$this->_checkFormat()) {
416 return(0);
417 }
418
419 // ----- Set default values
420 if (0 === $p_params) {
421 $p_params = [];
422 }
423 if (1 != $this->_check_parameters($p_params,
424 [
425 'extract_as_string' => false,
426 'add_path' => '',
427 'remove_path' => '',
428 'remove_all_path' => false,
429 'callback_pre_extract' => '',
430 'callback_post_extract' => '',
431 'set_chmod' => 0,
432 'by_name' => '',
433 'by_index' => '',
434 'by_ereg' => '',
435 'by_preg' => ''
436 ]
437 )) {
438 return 0;
439 }
440
441 // ----- Call the extracting fct
442 $v_list = [];
443 if (1 != $this->_extractByRule($v_list, $p_params)) {
444 unset($v_list);
445 return(0);
446 }
447
448 return $v_list;
449 }
450 // }}}
451
452
453 // {{{ delete()
475 public function delete($p_params)
476 {
477 $this->_errorReset();
478
479 // ----- Check archive
480 if (!$this->_checkFormat()) {
481 return(0);
482 }
483
484 // ----- Set default values
485 if (1 != $this->_check_parameters($p_params,
486 [
487 'by_name' => '',
488 'by_index' => '',
489 'by_ereg' => '',
490 'by_preg' => ''
491 ]
492 )) {
493 return 0;
494 }
495
496 // ----- Check that at least one rule is set
497 if (('' == $p_params['by_name'])
498 && ('' == $p_params['by_index'])
499 && ('' == $p_params['by_ereg'])
500 && ('' == $p_params['by_preg'])) {
501 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
502 'At least one filtering rule must'
503 .' be set as parameter');
504 return 0;
505 }
506
507 // ----- Call the delete fct
508 $v_list = [];
509 if (1 != $this->_deleteByRule($v_list, $p_params)) {
510 unset($v_list);
511 return(0);
512 }
513
514 return $v_list;
515 }
516 // }}}
517
518 // {{{ properties()
529 public function properties()
530 {
531 $this->_errorReset();
532
533 // ----- Check archive
534 if (!$this->_checkFormat()) {
535 return(0);
536 }
537
538 // ----- Default properties
539 $v_prop = [];
540 $v_prop['comment'] = '';
541 $v_prop['nb'] = 0;
542 $v_prop['status'] = 'not_exist';
543
544 // ----- Look if file exists
545 if (@is_file($this->_zipname)) {
546 // ----- Open the zip file
547 if (0 == ($this->_zip_fd = @fopen($this->_zipname, 'rb'))) {
548 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
549 'Unable to open archive \''.$this->_zipname
550 .'\' in binary read mode');
551 return 0;
552 }
553
554 // ----- Read the central directory informations
555 $v_central_dir = [];
556 if (1 != ($v_result = $this->_readEndCentralDir($v_central_dir))) {
557 return 0;
558 }
559
560 $this->_closeFd();
561
562 // ----- Set the user attributes
563 $v_prop['comment'] = $v_central_dir['comment'];
564 $v_prop['nb'] = $v_central_dir['entries'];
565 $v_prop['status'] = 'ok';
566 }
567
568 return $v_prop;
569 }
570 // }}}
571
572
573 // {{{ duplicate()
584 public function duplicate($p_archive)
585 {
586 $this->_errorReset();
587
588 // ----- Look if the $p_archive is a Archive_Zip object
589 if ((is_object($p_archive))
590 && ('archive_zip' == strtolower(get_class($p_archive)))) {
591 $v_result = $this->_duplicate($p_archive->_zipname);
592 }
593
594 // ----- Look if the $p_archive is a string (so a filename)
595 elseif (is_string($p_archive)) {
596 // ----- Check that $p_archive is a valid zip file
597 // TBC : Should also check the archive format
598 if (!is_file($p_archive)) {
599 $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
600 "No file with filename '".$p_archive."'");
601 $v_result = ARCHIVE_ZIP_ERR_MISSING_FILE;
602 } else {
603 $v_result = $this->_duplicate($p_archive);
604 }
605 }
606
607 // ----- Invalid variable
608 else {
609 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
610 'Invalid variable type p_archive_to_add'
611 );
612 $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
613 }
614
615 return $v_result;
616 }
617 // }}}
618
619 // {{{ merge()
631 public function merge($p_archive_to_add)
632 {
633 $v_result = 1;
634 $this->_errorReset();
635
636 // ----- Check archive
637 if (!$this->_checkFormat()) {
638 return(0);
639 }
640
641 // ----- Look if the $p_archive_to_add is a Archive_Zip object
642 if ((is_object($p_archive_to_add))
643 && ('archive_zip' == strtolower(get_class($p_archive_to_add)))) {
644 $v_result = $this->_merge($p_archive_to_add);
645 }
646
647 // ----- Look if the $p_archive_to_add is a string (so a filename)
648 elseif (is_string($p_archive_to_add)) {
649 // ----- Create a temporary archive
650 $v_object_archive = new Archive_Zip($p_archive_to_add);
651
652 // ----- Merge the archive
653 $v_result = $this->_merge($v_object_archive);
654 }
655
656 // ----- Invalid variable
657 else {
658 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
659 'Invalid variable type p_archive_to_add'
660 );
661 $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
662 }
663
664 return $v_result;
665 }
666 // }}}
667
668 // {{{ errorCode()
675 public function errorCode()
676 {
677 return($this->_error_code);
678 }
679 // }}}
680
681 // {{{ errorName()
689 public function errorName($p_with_code = false)
690 {
691 $v_const_list = get_defined_constants();
692
693 // ----- Extract error constants from all const.
694 $v_error_list = [];
695 foreach ($v_const_list as $v_key => $v_value) {
696 if (substr($v_key, 0, strlen('ARCHIVE_ZIP_ERR_')) === 'ARCHIVE_ZIP_ERR_') {
697 $v_error_list[$v_key] = $v_value;
698 }
699 }
700
701 // ----- Search the name from the code value
702 $v_key = array_search($this->_error_code, $v_error_list, true);
703 $v_value = $v_key !== false ? $v_key : 'NoName';
704
705 // ----- Return the error name with or without the error code
706 if ($p_with_code) {
707 return $v_value . ' (' . $this->_error_code . ')';
708 } else {
709 return $v_value;
710 }
711 }
712 // }}}
713
714 // {{{ errorInfo()
725 public function errorInfo($p_full=false)
726 {
727 if ($p_full) {
728 return($this->errorName(true) . ' : ' . $this->_error_string);
729 } else {
730 return($this->_error_string . ' [code ' . $this->_error_code . ']');
731 }
732 }
733 // }}}
734
735
736// -----------------------------------------------------------------------------
737// ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
738// ***** *****
739// ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
740// -----------------------------------------------------------------------------
741
742 // ---------------------------------------------------------------------------
743 // Function : _checkFormat()
744 // Description :
745 // This method check that the archive exists and is a valid zip archive.
746 // Several level of check exists. (futur)
747 // Parameters :
748 // $p_level : Level of check. Default 0.
749 // 0 : Check the first bytes (magic codes) (default value))
750 // 1 : 0 + Check the central directory (futur)
751 // 2 : 1 + Check each file header (futur)
752 // Return Values :
753 // true on success,
754 // false on error, the error code is set.
755 // ---------------------------------------------------------------------------
764 public function _checkFormat($p_level=0)
765 {
766 $v_result = true;
767
768 // ----- Reset the error handler
769 $this->_errorReset();
770
771 // ----- Look if the file exits
772 if (!is_file($this->_zipname)) {
773 // ----- Error log
774 $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
775 "Missing archive file '".$this->_zipname."'");
776 return(false);
777 }
778
779 // ----- Check that the file is readeable
780 if (!is_readable($this->_zipname)) {
781 // ----- Error log
782 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
783 "Unable to read archive '".$this->_zipname."'");
784 return(false);
785 }
786
787 // ----- Check the magic code
788 // TBC
789
790 // ----- Check the central header
791 // TBC
792
793 // ----- Check each file header
794 // TBC
795
796 // ----- Return
797 return $v_result;
798 }
799 // ---------------------------------------------------------------------------
800
801 // ---------------------------------------------------------------------------
802 // Function : _create()
803 // Description :
804 // Parameters :
805 // Return Values :
806 // ---------------------------------------------------------------------------
816 public function _create($p_list, &$p_result_list, &$p_params)
817 {
818 $v_result=1;
819 $v_list_detail = [];
820
821 $p_add_dir = $p_params['add_path'];
822 $p_remove_dir = $p_params['remove_path'];
823 $p_remove_all_dir = $p_params['remove_all_path'];
824
825 // ----- Open the file in write mode
826 if (1 != ($v_result = $this->_openFd('wb'))) {
827 // ----- Return
828 return $v_result;
829 }
830
831 // ----- Add the list of files
832 $v_result = $this->_addList($p_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params);
833
834 // ----- Close
835 $this->_closeFd();
836
837 // ----- Return
838 return $v_result;
839 }
840 // ---------------------------------------------------------------------------
841
842 // ---------------------------------------------------------------------------
843 // Function : _add()
844 // Description :
845 // Parameters :
846 // Return Values :
847 // ---------------------------------------------------------------------------
857 public function _add($p_list, &$p_result_list, &$p_params)
858 {
859 $v_result=1;
860 $v_list_detail = [];
861
862 $p_add_dir = $p_params['add_path'];
863 $p_remove_dir = $p_params['remove_path'];
864 $p_remove_all_dir = $p_params['remove_all_path'];
865
866 // ----- Look if the archive exists or is empty and need to be created
867 if ((!is_file($this->_zipname)) || (0 == filesize($this->_zipname))) {
868 $v_result = $this->_create($p_list, $p_result_list, $p_params);
869 return $v_result;
870 }
871
872 // ----- Open the zip file
873 if (1 != ($v_result=$this->_openFd('rb'))) {
874 return $v_result;
875 }
876
877 // ----- Read the central directory informations
878 $v_central_dir = [];
879 if (1 != ($v_result = $this->_readEndCentralDir($v_central_dir))) {
880 $this->_closeFd();
881 return $v_result;
882 }
883
884 // ----- Go to beginning of File
885 @rewind($this->_zip_fd);
886
887 // ----- Creates a temporay file
888 $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR.uniqid('archive_zip-').'.tmp';
889
890 // ----- Open the temporary file in write mode
891 if (0 == ($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb'))) {
892 $this->_closeFd();
893
894 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
895 'Unable to open temporary file \''
896 .$v_zip_temp_name.'\' in binary write mode');
897 return Archive_Zip::errorCode();
898 }
899
900 // ----- Copy the files from the archive to the temporary file
901 // TBC : Here I should better append the file and go back to erase the
902 // central dir
903 $v_size = $v_central_dir['offset'];
904 while (0 != $v_size) {
905 $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
906 ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
907 $v_buffer = fread($this->_zip_fd, $v_read_size);
908 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
909 $v_size -= $v_read_size;
910 }
911
912 // ----- Swap the file descriptor
913 // Here is a trick : I swap the temporary fd with the zip fd, in order to
914 // use the following methods on the temporary fil and not the real archive
915 $v_swap = $this->_zip_fd;
916 $this->_zip_fd = $v_zip_temp_fd;
917 $v_zip_temp_fd = $v_swap;
918
919 // ----- Add the files
920 $v_header_list = [];
921 if (1 != ($v_result = $this->_addFileList($p_list, $v_header_list,
922 $p_add_dir, $p_remove_dir,
923 $p_remove_all_dir, $p_params))) {
924 fclose($v_zip_temp_fd);
925 $this->_closeFd();
926 @unlink($v_zip_temp_name);
927
928 // ----- Return
929 return $v_result;
930 }
931
932 // ----- Store the offset of the central dir
933 $v_offset = @ftell($this->_zip_fd);
934
935 // ----- Copy the block of file headers from the old archive
936 $v_size = $v_central_dir['size'];
937 while (0 != $v_size) {
938 $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
939 ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
940 $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
941 @fwrite($this->_zip_fd, $v_buffer, $v_read_size);
942 $v_size -= $v_read_size;
943 }
944
945 // ----- Create the Central Dir files header
946 for ($i=0, $v_count=0; $i < count($v_header_list); $i++) {
947 // ----- Create the file header
948 if ('ok' == $v_header_list[$i]['status']) {
949 if (1 != ($v_result=$this->_writeCentralFileHeader($v_header_list[$i]))) {
950 fclose($v_zip_temp_fd);
951 $this->_closeFd();
952 @unlink($v_zip_temp_name);
953
954 // ----- Return
955 return $v_result;
956 }
957 $v_count++;
958 }
959
960 // ----- Transform the header to a 'usable' info
961 $this->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
962 }
963
964 // ----- Zip file comment
965 $v_comment = '';
966
967 // ----- Calculate the size of the central header
968 $v_size = @ftell($this->_zip_fd)-$v_offset;
969
970 // ----- Create the central dir footer
971 if (1 != ($v_result = $this->_writeCentralHeader($v_count
972 +$v_central_dir['entries'],
973 $v_size, $v_offset,
974 $v_comment))) {
975 // ----- Reset the file list
976 unset($v_header_list);
977
978 // ----- Return
979 return $v_result;
980 }
981
982 // ----- Swap back the file descriptor
983 $v_swap = $this->_zip_fd;
984 $this->_zip_fd = $v_zip_temp_fd;
985 $v_zip_temp_fd = $v_swap;
986
987 // ----- Close
988 $this->_closeFd();
989
990 // ----- Close the temporary file
991 @fclose($v_zip_temp_fd);
992
993 // ----- Delete the zip file
994 // TBC : I should test the result ...
995 @unlink($this->_zipname);
996
997 // ----- Rename the temporary file
998 // TBC : I should test the result ...
999 //@rename($v_zip_temp_name, $this->_zipname);
1000 $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
1001
1002 // ----- Return
1003 return $v_result;
1004 }
1005 // ---------------------------------------------------------------------------
1006
1007 // ---------------------------------------------------------------------------
1008 // Function : _openFd()
1009 // Description :
1010 // Parameters :
1011 // ---------------------------------------------------------------------------
1019 public function _openFd($p_mode)
1020 {
1021 $v_result=1;
1022
1023 // ----- Look if already open
1024 if (0 != $this->_zip_fd) {
1025 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
1026 'Zip file \''.$this->_zipname.'\' already open');
1027 return Archive_Zip::errorCode();
1028 }
1029
1030 // ----- Open the zip file
1031 if (0 == ($this->_zip_fd = @fopen($this->_zipname, $p_mode))) {
1032 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
1033 'Unable to open archive \''.$this->_zipname
1034 .'\' in '.$p_mode.' mode');
1035 return Archive_Zip::errorCode();
1036 }
1037
1038 // ----- Return
1039 return $v_result;
1040 }
1041 // ---------------------------------------------------------------------------
1042
1043 // ---------------------------------------------------------------------------
1044 // Function : _closeFd()
1045 // Description :
1046 // Parameters :
1047 // ---------------------------------------------------------------------------
1054 public function _closeFd()
1055 {
1056 $v_result=1;
1057
1058 if (0 != $this->_zip_fd) {
1059 @fclose($this->_zip_fd);
1060 }
1061 $this->_zip_fd = 0;
1062
1063 // ----- Return
1064 return $v_result;
1065 }
1066 // ---------------------------------------------------------------------------
1067
1068 // ---------------------------------------------------------------------------
1069 // Function : _addList()
1070 // Description :
1071 // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
1072 // different from the real path of the file. This is usefull if you want to have PclTar
1073 // running in any directory, and memorize relative path from an other directory.
1074 // Parameters :
1075 // $p_list : An array containing the file or directory names to add in the tar
1076 // $p_result_list : list of added files with their properties (specially the status field)
1077 // $p_add_dir : Path to add in the filename path archived
1078 // $p_remove_dir : Path to remove in the filename path archived
1079 // Return Values :
1080 // ---------------------------------------------------------------------------
1093 public function _addList($p_list, &$p_result_list,
1094 $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_params)
1095 {
1096 $v_result=1;
1097
1098 // ----- Add the files
1099 $v_header_list = [];
1100 if (1 != ($v_result = $this->_addFileList($p_list, $v_header_list,
1101 $p_add_dir, $p_remove_dir,
1102 $p_remove_all_dir, $p_params))) {
1103 return $v_result;
1104 }
1105
1106 // ----- Store the offset of the central dir
1107 $v_offset = @ftell($this->_zip_fd);
1108
1109 // ----- Create the Central Dir files header
1110 for ($i=0, $v_count=0; $i < count($v_header_list); $i++) {
1111 // ----- Create the file header
1112 if ('ok' == $v_header_list[$i]['status']) {
1113 if (1 != ($v_result = $this->_writeCentralFileHeader($v_header_list[$i]))) {
1114 return $v_result;
1115 }
1116 $v_count++;
1117 }
1118
1119 // ----- Transform the header to a 'usable' info
1120 $this->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
1121 }
1122
1123 // ----- Zip file comment
1124 $v_comment = '';
1125
1126 // ----- Calculate the size of the central header
1127 $v_size = @ftell($this->_zip_fd)-$v_offset;
1128
1129 // ----- Create the central dir footer
1130 if (1 != ($v_result = $this->_writeCentralHeader($v_count, $v_size, $v_offset,
1131 $v_comment))) {
1132 // ----- Reset the file list
1133 unset($v_header_list);
1134
1135 // ----- Return
1136 return $v_result;
1137 }
1138
1139 // ----- Return
1140 return $v_result;
1141 }
1142 // ---------------------------------------------------------------------------
1143
1144 // ---------------------------------------------------------------------------
1145 // Function : _addFileList()
1146 // Description :
1147 // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
1148 // different from the real path of the file. This is usefull if you want to
1149 // run the lib in any directory, and memorize relative path from an other directory.
1150 // Parameters :
1151 // $p_list : An array containing the file or directory names to add in the tar
1152 // $p_result_list : list of added files with their properties (specially the status field)
1153 // $p_add_dir : Path to add in the filename path archived
1154 // $p_remove_dir : Path to remove in the filename path archived
1155 // Return Values :
1156 // ---------------------------------------------------------------------------
1169 public function _addFileList($p_list, &$p_result_list,
1170 $p_add_dir, $p_remove_dir, $p_remove_all_dir,
1171 &$p_params)
1172 {
1173 $v_result=1;
1174 $v_header = [];
1175
1176 // ----- Recuperate the current number of elt in list
1177 $v_nb = count($p_result_list);
1178
1179 // ----- Loop on the files
1180 for ($j=0; ($j<count($p_list)) && (1 == $v_result); $j++) {
1181 // ----- Recuperate the filename
1182 $p_filename = $this->_tool_TranslateWinPath($p_list[$j], false);
1183
1184 // ----- Skip empty file names
1185 if ('' == $p_filename) {
1186 continue;
1187 }
1188
1189 // ----- Check the filename
1190 if (!file_exists($p_filename)) {
1191 $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
1192 "File '$p_filename' does not exists");
1193 return Archive_Zip::errorCode();
1194 }
1195
1196 // ----- Look if it is a file or a dir with no all pathnre move
1197 if ((is_file($p_filename)) || ((is_dir($p_filename)) && !$p_remove_all_dir)) {
1198 // ----- Add the file
1199 if (1 != ($v_result = $this->_addFile($p_filename, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params))) {
1200 // ----- Return status
1201 return $v_result;
1202 }
1203
1204 // ----- Store the file infos
1205 $p_result_list[$v_nb++] = $v_header;
1206 }
1207
1208 // ----- Look for directory
1209 if (is_dir($p_filename)) {
1210
1211 // ----- Look for path
1212 if ('.' != $p_filename) {
1213 $v_path = $p_filename . '/';
1214 } else {
1215 $v_path = '';
1216 }
1217
1218 // ----- Read the directory for files and sub-directories
1219 $p_hdir = opendir($p_filename);
1220 $p_hitem = readdir($p_hdir); // '.' directory
1221 $p_hitem = readdir($p_hdir); // '..' directory
1222 while ($p_hitem = readdir($p_hdir)) {
1223
1224 // ----- Look for a file
1225 if (is_file($v_path.$p_hitem)) {
1226
1227 // ----- Add the file
1228 if (1 != ($v_result = $this->_addFile($v_path . $p_hitem, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params))) {
1229 // ----- Return status
1230 return $v_result;
1231 }
1232
1233 // ----- Store the file infos
1234 $p_result_list[$v_nb++] = $v_header;
1235 }
1236
1237 // ----- Recursive call to _addFileList()
1238 else {
1239
1240 // ----- Need an array as parameter
1241 $p_temp_list[0] = $v_path.$p_hitem;
1242 $v_result = $this->_addFileList($p_temp_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params);
1243
1244 // ----- Update the number of elements of the list
1245 $v_nb = count($p_result_list);
1246 }
1247 }
1248
1249 // ----- Free memory for the recursive loop
1250 unset($p_temp_list);
1251 unset($p_hdir);
1252 unset($p_hitem);
1253 }
1254 }
1255
1256 return $v_result;
1257 }
1258 // ---------------------------------------------------------------------------
1259
1260 // ---------------------------------------------------------------------------
1261 // Function : _addFile()
1262 // Description :
1263 // Parameters :
1264 // Return Values :
1265 // ---------------------------------------------------------------------------
1278 public function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_params)
1279 {
1280 $v_result=1;
1281
1282 if ('' == $p_filename) {
1283 // ----- Error log
1284 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER, 'Invalid file list parameter (invalid or empty list)');
1285
1286 // ----- Return
1287 return Archive_Zip::errorCode();
1288 }
1289
1290 // ----- Calculate the stored filename
1291 $v_stored_filename = $p_filename;
1292
1293 // ----- Look for all path to remove
1294 if ($p_remove_all_dir) {
1295 $v_stored_filename = basename($p_filename);
1296 }
1297 // ----- Look for partial path remove
1298 elseif ('' != $p_remove_dir) {
1299 if ('/' != substr($p_remove_dir, -1)) {
1300 $p_remove_dir .= '/';
1301 }
1302
1303 if (('./' == substr($p_filename, 0, 2)) || ('./' == substr($p_remove_dir, 0, 2))) {
1304 if (('./' == substr($p_filename, 0, 2)) && ('./' != substr($p_remove_dir, 0, 2))) {
1305 $p_remove_dir = './' . $p_remove_dir;
1306 }
1307 if (('./' != substr($p_filename, 0, 2)) && ('./' == substr($p_remove_dir, 0, 2))) {
1308 $p_remove_dir = substr($p_remove_dir, 2);
1309 }
1310 }
1311
1312 $v_compare = $this->_tool_PathInclusion($p_remove_dir, $p_filename);
1313 if ($v_compare > 0) {
1314 // if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
1315
1316
1317 if (2 == $v_compare) {
1318 $v_stored_filename = '';
1319 } else {
1320 $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
1321 }
1322 }
1323 }
1324 // ----- Look for path to add
1325 if ('' != $p_add_dir) {
1326 if ('/' == substr($p_add_dir, -1)) {
1327 $v_stored_filename = $p_add_dir.$v_stored_filename;
1328 } else {
1329 $v_stored_filename = $p_add_dir . '/' . $v_stored_filename;
1330 }
1331 }
1332
1333 // ----- Filename (reduce the path of stored name)
1334 $v_stored_filename = $this->_tool_PathReduction($v_stored_filename);
1335
1336
1337 /* filename length moved after call-back in release 1.3
1338 // ----- Check the path length
1339 if (strlen($v_stored_filename) > 0xFF)
1340 {
1341 // ----- Error log
1342 $this->_errorLog(-5, "Stored file name is too long (max. 255) : '$v_stored_filename'");
1343
1344 // ----- Return
1345 return Archive_Zip::errorCode();
1346 }
1347 */
1348
1349 // ----- Set the file properties
1350 clearstatcache();
1351 $p_header['version'] = 20;
1352 $p_header['version_extracted'] = 10;
1353 $p_header['flag'] = 0;
1354 $p_header['compression'] = 0;
1355 $p_header['mtime'] = filemtime($p_filename);
1356 $p_header['crc'] = 0;
1357 $p_header['compressed_size'] = 0;
1358 $p_header['size'] = filesize($p_filename);
1359 $p_header['filename_len'] = strlen($p_filename);
1360 $p_header['extra_len'] = 0;
1361 $p_header['comment_len'] = 0;
1362 $p_header['disk'] = 0;
1363 $p_header['internal'] = 0;
1364 $p_header['external'] = (is_file($p_filename)?0xFE49FFE0:0x41FF0010);
1365 $p_header['offset'] = 0;
1366 $p_header['filename'] = $p_filename;
1367 $p_header['stored_filename'] = $v_stored_filename;
1368 $p_header['extra'] = '';
1369 $p_header['comment'] = '';
1370 $p_header['status'] = 'ok';
1371 $p_header['index'] = -1;
1372
1373 // ----- Look for pre-add callback
1374 if ((isset($p_params[ARCHIVE_ZIP_PARAM_PRE_ADD]))
1375 && ('' != $p_params[ARCHIVE_ZIP_PARAM_PRE_ADD])) {
1376
1377 // ----- Generate a local information
1378 $v_local_header = [];
1379 $this->_convertHeader2FileInfo($p_header, $v_local_header);
1380
1381 // ----- Call the callback
1382 // Here I do not use call_user_func() because I need to send a reference to the
1383 // header.
1384 eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_PRE_ADD].'(ARCHIVE_ZIP_PARAM_PRE_ADD, $v_local_header);');
1385 if (0 == $v_result) {
1386 // ----- Change the file status
1387 $p_header['status'] = 'skipped';
1388 $v_result = 1;
1389 }
1390
1391 // ----- Update the informations
1392 // Only some fields can be modified
1393 if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
1394 $p_header['stored_filename'] = $this->_tool_PathReduction($v_local_header['stored_filename']);
1395 }
1396 }
1397
1398 // ----- Look for empty stored filename
1399 if ('' == $p_header['stored_filename']) {
1400 $p_header['status'] = 'filtered';
1401 }
1402
1403 // ----- Check the path length
1404 if (strlen($p_header['stored_filename']) > 0xFF) {
1405 $p_header['status'] = 'filename_too_long';
1406 }
1407
1408 // ----- Look if no error, or file not skipped
1409 if ('ok' == $p_header['status']) {
1410
1411 // ----- Look for a file
1412 if (is_file($p_filename)) {
1413 // ----- Open the source file
1414 if (0 == ($v_file = @fopen($p_filename, 'rb'))) {
1415 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
1416 return Archive_Zip::errorCode();
1417 }
1418
1419 if ($p_params['no_compression']) {
1420 // ----- Read the file content
1421 $v_content_compressed = @fread($v_file, $p_header['size']);
1422
1423 // ----- Calculate the CRC
1424 $p_header['crc'] = crc32($v_content_compressed);
1425 } else {
1426 // ----- Read the file content
1427 $v_content = @fread($v_file, $p_header['size']);
1428
1429 // ----- Calculate the CRC
1430 $p_header['crc'] = crc32($v_content);
1431
1432 // ----- Compress the file
1433 $v_content_compressed = gzdeflate($v_content);
1434 }
1435
1436 // ----- Set header parameters
1437 $p_header['compressed_size'] = strlen($v_content_compressed);
1438 $p_header['compression'] = 8;
1439
1440 // ----- Call the header generation
1441 if (1 != ($v_result = $this->_writeFileHeader($p_header))) {
1442 @fclose($v_file);
1443 return $v_result;
1444 }
1445
1446 // ----- Write the compressed content
1447 $v_binary_data = pack('a'.$p_header['compressed_size'], $v_content_compressed);
1448 @fwrite($this->_zip_fd, $v_binary_data, $p_header['compressed_size']);
1449
1450 // ----- Close the file
1451 @fclose($v_file);
1452 }
1453
1454 // ----- Look for a directory
1455 else {
1456 // ----- Set the file properties
1457 $p_header['filename'] .= '/';
1458 $p_header['filename_len']++;
1459 $p_header['size'] = 0;
1460 $p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
1461
1462 // ----- Call the header generation
1463 if (1 != ($v_result = $this->_writeFileHeader($p_header))) {
1464 return $v_result;
1465 }
1466 }
1467 }
1468
1469 // ----- Look for pre-add callback
1470 if ((isset($p_params[ARCHIVE_ZIP_PARAM_POST_ADD]))
1471 && ('' != $p_params[ARCHIVE_ZIP_PARAM_POST_ADD])) {
1472
1473 // ----- Generate a local information
1474 $v_local_header = [];
1475 $this->_convertHeader2FileInfo($p_header, $v_local_header);
1476
1477 // ----- Call the callback
1478 // Here I do not use call_user_func() because I need to send a reference to the
1479 // header.
1480 eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_POST_ADD].'(ARCHIVE_ZIP_PARAM_POST_ADD, $v_local_header);');
1481 if (0 == $v_result) {
1482 // ----- Ignored
1483 $v_result = 1;
1484 }
1485
1486 // ----- Update the informations
1487 // Nothing can be modified
1488 }
1489
1490 // ----- Return
1491 return $v_result;
1492 }
1493 // ---------------------------------------------------------------------------
1494
1495 // ---------------------------------------------------------------------------
1496 // Function : _writeFileHeader()
1497 // Description :
1498 // Parameters :
1499 // Return Values :
1500 // ---------------------------------------------------------------------------
1508 public function _writeFileHeader(&$p_header)
1509 {
1510 $v_result=1;
1511
1512 // TBC
1513 //for(reset($p_header); $key = key($p_header); next($p_header)) {
1514 //}
1515
1516 // ----- Store the offset position of the file
1517 $p_header['offset'] = ftell($this->_zip_fd);
1518
1519 // ----- Transform UNIX mtime to DOS format mdate/mtime
1520 $v_date = getdate($p_header['mtime']);
1521 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
1522 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
1523
1524 // ----- Packed data
1525 $v_binary_data = pack(
1526 'VvvvvvVVVvv', 0x04034b50, $p_header['version'], $p_header['flag'],
1527 $p_header['compression'], $v_mtime, $v_mdate,
1528 $p_header['crc'], $p_header['compressed_size'], $p_header['size'],
1529 strlen($p_header['stored_filename']), $p_header['extra_len']);
1530
1531 // ----- Write the first 148 bytes of the header in the archive
1532 fwrite($this->_zip_fd, $v_binary_data, 30);
1533
1534 // ----- Write the variable fields
1535 if (0 != strlen($p_header['stored_filename'])) {
1536 fwrite($this->_zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
1537 }
1538 if (0 != $p_header['extra_len']) {
1539 fwrite($this->_zip_fd, $p_header['extra'], $p_header['extra_len']);
1540 }
1541
1542 // ----- Return
1543 return $v_result;
1544 }
1545 // ---------------------------------------------------------------------------
1546
1547 // ---------------------------------------------------------------------------
1548 // Function : _writeCentralFileHeader()
1549 // Description :
1550 // Parameters :
1551 // Return Values :
1552 // ---------------------------------------------------------------------------
1560 public function _writeCentralFileHeader(&$p_header)
1561 {
1562 $v_result=1;
1563
1564 // TBC
1565 //for(reset($p_header); $key = key($p_header); next($p_header)) {
1566 //}
1567
1568 // ----- Transform UNIX mtime to DOS format mdate/mtime
1569 $v_date = getdate($p_header['mtime']);
1570 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
1571 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
1572
1573 // ----- Packed data
1574 $v_binary_data = pack(
1575 'VvvvvvvVVVvvvvvVV', 0x02014b50, $p_header['version'], $p_header['version_extracted'],
1576 $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'],
1577 $p_header['compressed_size'], $p_header['size'],
1578 strlen($p_header['stored_filename']), $p_header['extra_len'], $p_header['comment_len'],
1579 $p_header['disk'], $p_header['internal'], $p_header['external'], $p_header['offset']);
1580
1581 // ----- Write the 42 bytes of the header in the zip file
1582 fwrite($this->_zip_fd, $v_binary_data, 46);
1583
1584 // ----- Write the variable fields
1585 if (0 != strlen($p_header['stored_filename'])) {
1586 fwrite($this->_zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
1587 }
1588 if (0 != $p_header['extra_len']) {
1589 fwrite($this->_zip_fd, $p_header['extra'], $p_header['extra_len']);
1590 }
1591 if (0 != $p_header['comment_len']) {
1592 fwrite($this->_zip_fd, $p_header['comment'], $p_header['comment_len']);
1593 }
1594
1595 // ----- Return
1596 return $v_result;
1597 }
1598 // ---------------------------------------------------------------------------
1599
1600 // ---------------------------------------------------------------------------
1601 // Function : _writeCentralHeader()
1602 // Description :
1603 // Parameters :
1604 // Return Values :
1605 // ---------------------------------------------------------------------------
1616 public function _writeCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
1617 {
1618 $v_result=1;
1619
1620 // ----- Packed data
1621 $v_binary_data = pack('VvvvvVVv', 0x06054b50, 0, 0, $p_nb_entries, $p_nb_entries, $p_size, $p_offset, strlen($p_comment));
1622
1623 // ----- Write the 22 bytes of the header in the zip file
1624 fwrite($this->_zip_fd, $v_binary_data, 22);
1625
1626 // ----- Write the variable fields
1627 if (0 != strlen($p_comment)) {
1628 fwrite($this->_zip_fd, $p_comment, strlen($p_comment));
1629 }
1630
1631 // ----- Return
1632 return $v_result;
1633 }
1634 // ---------------------------------------------------------------------------
1635
1636 // ---------------------------------------------------------------------------
1637 // Function : _list()
1638 // Description :
1639 // Parameters :
1640 // Return Values :
1641 // ---------------------------------------------------------------------------
1649 public function _list(&$p_list)
1650 {
1651 $v_result=1;
1652
1653 // ----- Open the zip file
1654 if (0 == ($this->_zip_fd = @fopen($this->_zipname, 'rb'))) {
1655 // ----- Error log
1656 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->_zipname.'\' in binary read mode');
1657
1658 // ----- Return
1659 return Archive_Zip::errorCode();
1660 }
1661
1662 // ----- Read the central directory informations
1663 $v_central_dir = [];
1664 if (1 != ($v_result = $this->_readEndCentralDir($v_central_dir))) {
1665 return $v_result;
1666 }
1667
1668 // ----- Go to beginning of Central Dir
1669 @rewind($this->_zip_fd);
1670 if (@fseek($this->_zip_fd, $v_central_dir['offset'])) {
1671 // ----- Error log
1672 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
1673
1674 // ----- Return
1675 return Archive_Zip::errorCode();
1676 }
1677
1678 // ----- Read each entry
1679 for ($i=0; $i<$v_central_dir['entries']; $i++) {
1680 // ----- Read the file header
1681 if (1 != ($v_result = $this->_readCentralFileHeader($v_header))) {
1682 return $v_result;
1683 }
1684 $v_header['index'] = $i;
1685
1686 // ----- Get the only interesting attributes
1687 $this->_convertHeader2FileInfo($v_header, $p_list[$i]);
1688 unset($v_header);
1689 }
1690
1691 // ----- Close the zip file
1692 $this->_closeFd();
1693
1694 // ----- Return
1695 return $v_result;
1696 }
1697 // ---------------------------------------------------------------------------
1698
1699 // ---------------------------------------------------------------------------
1700 // Function : _convertHeader2FileInfo()
1701 // Description :
1702 // This function takes the file informations from the central directory
1703 // entries and extract the interesting parameters that will be given back.
1704 // The resulting file infos are set in the array $p_info
1705 // $p_info['filename'] : Filename with full path. Given by user (add),
1706 // extracted in the filesystem (extract).
1707 // $p_info['stored_filename'] : Stored filename in the archive.
1708 // $p_info['size'] = Size of the file.
1709 // $p_info['compressed_size'] = Compressed size of the file.
1710 // $p_info['mtime'] = Last modification date of the file.
1711 // $p_info['comment'] = Comment associated with the file.
1712 // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
1713 // $p_info['status'] = status of the action on the file.
1714 // Parameters :
1715 // Return Values :
1716 // ---------------------------------------------------------------------------
1725 public function _convertHeader2FileInfo($p_header, &$p_info)
1726 {
1727 $v_result=1;
1728
1729 // ----- Get the interesting attributes
1730 $p_info['filename'] = $p_header['filename'];
1731 $p_info['stored_filename'] = $p_header['stored_filename'];
1732 $p_info['size'] = $p_header['size'];
1733 $p_info['compressed_size'] = $p_header['compressed_size'];
1734 $p_info['mtime'] = $p_header['mtime'];
1735 $p_info['comment'] = $p_header['comment'];
1736 $p_info['folder'] = (0x00000010 == ($p_header['external'] & 0x00000010));
1737 $p_info['index'] = $p_header['index'];
1738 $p_info['status'] = $p_header['status'];
1739
1740 // ----- Return
1741 return $v_result;
1742 }
1743 // ---------------------------------------------------------------------------
1744
1745 // ---------------------------------------------------------------------------
1746 // Function : _extractByRule()
1747 // Description :
1748 // Extract a file or directory depending of rules (by index, by name, ...)
1749 // Parameters :
1750 // $p_file_list : An array where will be placed the properties of each
1751 // extracted file
1752 // $p_path : Path to add while writing the extracted files
1753 // $p_remove_path : Path to remove (from the file memorized path) while writing the
1754 // extracted files. If the path does not match the file path,
1755 // the file is extracted with its memorized path.
1756 // $p_remove_path does not apply to 'list' mode.
1757 // $p_path and $p_remove_path are commulative.
1758 // Return Values :
1759 // 1 on success,0 or less on error (see error code list)
1760 // ---------------------------------------------------------------------------
1769 public function _extractByRule(&$p_file_list, &$p_params)
1770 {
1771 $v_result=1;
1772
1773 $p_path = $p_params['add_path'];
1774 $p_remove_path = $p_params['remove_path'];
1775 $p_remove_all_path = $p_params['remove_all_path'];
1776
1777 // ----- Check the path
1778 if (('' == $p_path)
1779 || (('/' != substr($p_path, 0, 1))
1780 && ('../' != substr($p_path, 0, 3)) && (':/' != substr($p_path, 1, 2)))) {
1781 $p_path = './' . $p_path;
1782 }
1783
1784 // ----- Reduce the path last (and duplicated) '/'
1785 if (('./' != $p_path) && ('/' != $p_path)) {
1786 // ----- Look for the path end '/'
1787 while ('/' == substr($p_path, -1)) {
1788 $p_path = substr($p_path, 0, strlen($p_path)-1);
1789 }
1790 }
1791
1792 // ----- Look for path to remove format (should end by /)
1793 if (('' != $p_remove_path) && ('/' != substr($p_remove_path, -1))) {
1794 $p_remove_path .= '/';
1795 }
1796 $p_remove_path_size = strlen($p_remove_path);
1797
1798 // ----- Open the zip file
1799 if (1 != ($v_result = $this->_openFd('rb'))) {
1800 return $v_result;
1801 }
1802
1803 // ----- Read the central directory informations
1804 $v_central_dir = [];
1805 if (1 != ($v_result = $this->_readEndCentralDir($v_central_dir))) {
1806 // ----- Close the zip file
1807 $this->_closeFd();
1808
1809 return $v_result;
1810 }
1811
1812 // ----- Start at beginning of Central Dir
1813 $v_pos_entry = $v_central_dir['offset'];
1814
1815 // ----- Read each entry
1816 $j_start = 0;
1817 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++) {
1818 // ----- Read next Central dir entry
1819 @rewind($this->_zip_fd);
1820 if (@fseek($this->_zip_fd, $v_pos_entry)) {
1821 $this->_closeFd();
1822
1823 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
1824 'Invalid archive size');
1825
1826 return Archive_Zip::errorCode();
1827 }
1828
1829 // ----- Read the file header
1830 $v_header = [];
1831 if (1 != ($v_result = $this->_readCentralFileHeader($v_header))) {
1832 $this->_closeFd();
1833
1834 return $v_result;
1835 }
1836
1837 // ----- Store the index
1838 $v_header['index'] = $i;
1839
1840 // ----- Store the file position
1841 $v_pos_entry = ftell($this->_zip_fd);
1842
1843 // ----- Look for the specific extract rules
1844 $v_extract = false;
1845
1846 // ----- Look for extract by name rule
1847 if ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
1848 && (0 != $p_params[ARCHIVE_ZIP_PARAM_BY_NAME])) {
1849
1850 // ----- Look if the filename is in the list
1851 for ($j=0;
1852 ($j < count($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
1853 && (!$v_extract);
1854 $j++) {
1855
1856 // ----- Look for a directory
1857 if ('/' == substr($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j], -1)) {
1858
1859 // ----- Look if the directory is in the filename path
1860 if ((strlen($v_header['stored_filename']) > strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]))
1861 && (substr($v_header['stored_filename'], 0, strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) {
1862 $v_extract = true;
1863 }
1864 }
1865 // ----- Look for a filename
1866 elseif ($v_header['stored_filename'] == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) {
1867 $v_extract = true;
1868 }
1869 }
1870 }
1871
1872 // ----- Look for extract by ereg rule
1873 elseif ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_EREG]))
1874 && ('' != $p_params[ARCHIVE_ZIP_PARAM_BY_EREG])) {
1876 if (preg_match('/\.([^\.]*$)/', $p_params[ARCHIVE_ZIP_PARAM_BY_EREG], $v_header['stored_filename'])) {
1877 //if (ereg($p_params[ARCHIVE_ZIP_PARAM_BY_EREG], $v_header['stored_filename'])) {
1878 $v_extract = true;
1879 }
1880 }
1881
1882 // ----- Look for extract by preg rule
1883 elseif ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_PREG]))
1884 && ('' != $p_params[ARCHIVE_ZIP_PARAM_BY_PREG])) {
1885 if (preg_match($p_params[ARCHIVE_ZIP_PARAM_BY_PREG], $v_header['stored_filename'])) {
1886 $v_extract = true;
1887 }
1888 }
1889
1890 // ----- Look for extract by index rule
1891 elseif ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]))
1892 && (0 != $p_params[ARCHIVE_ZIP_PARAM_BY_INDEX])) {
1893
1894 // ----- Look if the index is in the list
1895 for ($j=$j_start; ($j < count($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX])) && (!$v_extract); $j++) {
1896 if (($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start']) && ($i<=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end'])) {
1897 $v_extract = true;
1898 }
1899 if ($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end']) {
1900 $j_start = $j+1;
1901 }
1902
1903 if ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start']>$i) {
1904 break;
1905 }
1906 }
1907 }
1908
1909 // ----- Look for no rule, which means extract all the archive
1910 else {
1911 $v_extract = true;
1912 }
1913
1914
1915 // ----- Look for real extraction
1916 if ($v_extract) {
1917
1918 // ----- Go to the file position
1919 @rewind($this->_zip_fd);
1920 if (@fseek($this->_zip_fd, $v_header['offset'])) {
1921 // ----- Close the zip file
1922 $this->_closeFd();
1923
1924 // ----- Error log
1925 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
1926
1927 // ----- Return
1928 return Archive_Zip::errorCode();
1929 }
1930
1931 // ----- Look for extraction as string
1932 if ($p_params[ARCHIVE_ZIP_PARAM_EXTRACT_AS_STRING]) {
1933
1934 // ----- Extracting the file
1935 if (1 != ($v_result = $this->_extractFileAsString($v_header, $v_string))) {
1936 // ----- Close the zip file
1937 $this->_closeFd();
1938
1939 return $v_result;
1940 }
1941
1942 // ----- Get the only interesting attributes
1943 if (1 != ($v_result = $this->_convertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted]))) {
1944 // ----- Close the zip file
1945 $this->_closeFd();
1946
1947 return $v_result;
1948 }
1949
1950 // ----- Set the file content
1951 $p_file_list[$v_nb_extracted]['content'] = $v_string;
1952
1953 // ----- Next extracted file
1954 $v_nb_extracted++;
1955 } else {
1956 // ----- Extracting the file
1957 if (1 != ($v_result = $this->_extractFile($v_header, $p_path, $p_remove_path, $p_remove_all_path, $p_params))) {
1958 // ----- Close the zip file
1959 $this->_closeFd();
1960
1961 return $v_result;
1962 }
1963
1964 // ----- Get the only interesting attributes
1965 if (1 != ($v_result = $this->_convertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++]))) {
1966 // ----- Close the zip file
1967 $this->_closeFd();
1968
1969 return $v_result;
1970 }
1971 }
1972 }
1973 }
1974
1975 // ----- Close the zip file
1976 $this->_closeFd();
1977
1978 // ----- Return
1979 return $v_result;
1980 }
1981 // ---------------------------------------------------------------------------
1982
1983 // ---------------------------------------------------------------------------
1984 // Function : _extractFile()
1985 // Description :
1986 // Parameters :
1987 // Return Values :
1988 // ---------------------------------------------------------------------------
2000 public function _extractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_params)
2001 {
2002 $v_result=1;
2003
2004 // ----- Read the file header
2005 if (1 != ($v_result = $this->_readFileHeader($v_header))) {
2006 // ----- Return
2007 return $v_result;
2008 }
2009
2010
2011 // ----- Check that the file header is coherent with $p_entry info
2012 // TBC
2013
2014 // ----- Look for all path to remove
2015 if (true == $p_remove_all_path) {
2016 // ----- Get the basename of the path
2017 $p_entry['filename'] = basename($p_entry['filename']);
2018 }
2019
2020 // ----- Look for path to remove
2021 elseif ('' != $p_remove_path) {
2022 //if (strcmp($p_remove_path, $p_entry['filename'])==0)
2023 if (2 == $this->_tool_PathInclusion($p_remove_path, $p_entry['filename'])) {
2024
2025 // ----- Change the file status
2026 $p_entry['status'] = 'filtered';
2027
2028 // ----- Return
2029 return $v_result;
2030 }
2031
2032 $p_remove_path_size = strlen($p_remove_path);
2033 if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
2034
2035 // ----- Remove the path
2036 $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
2037 }
2038 }
2039
2040 // ----- Add the path
2041 if ('' != $p_path) {
2042 $p_entry['filename'] = $p_path . '/' . $p_entry['filename'];
2043 }
2044
2045 // ----- Look for pre-extract callback
2046 if ((isset($p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT]))
2047 && ('' != $p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT])) {
2048
2049 // ----- Generate a local information
2050 $v_local_header = [];
2051 $this->_convertHeader2FileInfo($p_entry, $v_local_header);
2052
2053 // ----- Call the callback
2054 // Here I do not use call_user_func() because I need to send a reference to the
2055 // header.
2056 eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT].'(ARCHIVE_ZIP_PARAM_PRE_EXTRACT, $v_local_header);');
2057 if (0 == $v_result) {
2058 // ----- Change the file status
2059 $p_entry['status'] = 'skipped';
2060 $v_result = 1;
2061 }
2062
2063 // ----- Update the informations
2064 // Only some fields can be modified
2065 $p_entry['filename'] = $v_local_header['filename'];
2066 }
2067
2068 // ----- Trace
2069
2070 // ----- Look if extraction should be done
2071 if ('ok' == $p_entry['status']) {
2072
2073 // ----- Look for specific actions while the file exist
2074 if (file_exists($p_entry['filename'])) {
2075
2076 // ----- Look if file is a directory
2077 if (is_dir($p_entry['filename'])) {
2078
2079 // ----- Change the file status
2080 $p_entry['status'] = 'already_a_directory';
2081
2082 // ----- Return
2083 //return $v_result;
2084 }
2085 // ----- Look if file is write protected
2086 elseif (!is_writable($p_entry['filename'])) {
2087
2088 // ----- Change the file status
2089 $p_entry['status'] = 'write_protected';
2090
2091 // ----- Return
2092 //return $v_result;
2093 }
2094
2095 // ----- Look if the extracted file is older
2096 elseif (filemtime($p_entry['filename']) > $p_entry['mtime']) {
2097
2098 // ----- Change the file status
2099 $p_entry['status'] = 'newer_exist';
2100
2101 // ----- Return
2102 //return $v_result;
2103 }
2104 }
2105
2106 // ----- Check the directory availability and create it if necessary
2107 else {
2108 if ((0x00000010 == ($p_entry['external'] & 0x00000010)) || ('/' == substr($p_entry['filename'], -1))) {
2109 $v_dir_to_check = $p_entry['filename'];
2110 } elseif (!strstr($p_entry['filename'], '/')) {
2111 $v_dir_to_check = '';
2112 } else {
2113 $v_dir_to_check = dirname($p_entry['filename']);
2114 }
2115
2116 if (1 != ($v_result = $this->_dirCheck($v_dir_to_check, (0x00000010 == ($p_entry['external'] & 0x00000010))))) {
2117
2118 // ----- Change the file status
2119 $p_entry['status'] = 'path_creation_fail';
2120
2121 // ----- Return
2122 //return $v_result;
2123 $v_result = 1;
2124 }
2125 }
2126 }
2127
2128 // ----- Look if extraction should be done
2129 if ('ok' == $p_entry['status']) {
2130
2131 // ----- Do the extraction (if not a folder)
2132 if (!(0x00000010 == ($p_entry['external'] & 0x00000010))) {
2133
2134 // ----- Look for not compressed file
2135 if ($p_entry['compressed_size'] == $p_entry['size']) {
2136
2137 // ----- Opening destination file
2138 if (0 == ($v_dest_file = @fopen($p_entry['filename'], 'wb'))) {
2139
2140 // ----- Change the file status
2141 $p_entry['status'] = 'write_error';
2142
2143 // ----- Return
2144 return $v_result;
2145 }
2146
2147
2148 // ----- Read the file by ARCHIVE_ZIP_READ_BLOCK_SIZE octets blocks
2149 $v_size = $p_entry['compressed_size'];
2150 while (0 != $v_size) {
2151 $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
2152 $v_buffer = fread($this->_zip_fd, $v_read_size);
2153 $v_binary_data = pack('a'.$v_read_size, $v_buffer);
2154 @fwrite($v_dest_file, $v_binary_data, $v_read_size);
2155 $v_size -= $v_read_size;
2156 }
2157
2158 // ----- Closing the destination file
2159 fclose($v_dest_file);
2160
2161 // ----- Change the file mtime
2162 touch($p_entry['filename'], $p_entry['mtime']);
2163 } else {
2164 // ----- Trace
2165
2166 // ----- Opening destination file
2167 if (0 == ($v_dest_file = @fopen($p_entry['filename'], 'wb'))) {
2168
2169 // ----- Change the file status
2170 $p_entry['status'] = 'write_error';
2171
2172 return $v_result;
2173 }
2174
2175
2176 // ----- Read the compressed file in a buffer (one shot)
2177 $v_buffer = @fread($this->_zip_fd, $p_entry['compressed_size']);
2178
2179 // ----- Decompress the file
2180 $v_file_content = gzinflate($v_buffer);
2181 unset($v_buffer);
2182
2183 // ----- Write the uncompressed data
2184 @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
2185 unset($v_file_content);
2186
2187 // ----- Closing the destination file
2188 @fclose($v_dest_file);
2189
2190 // ----- Change the file mtime
2191 touch($p_entry['filename'], $p_entry['mtime']);
2192 }
2193
2194 // ----- Look for chmod option
2195 if ((isset($p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD]))
2196 && (0 != $p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD])) {
2197
2198 // ----- Change the mode of the file
2199 chmod($p_entry['filename'], $p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD]);
2200 }
2201 }
2202 }
2203
2204 // ----- Look for post-extract callback
2205 if ((isset($p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT]))
2206 && ('' != $p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT])) {
2207
2208 // ----- Generate a local information
2209 $v_local_header = [];
2210 $this->_convertHeader2FileInfo($p_entry, $v_local_header);
2211
2212 // ----- Call the callback
2213 // Here I do not use call_user_func() because I need to send a reference to the
2214 // header.
2215 eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT].'(ARCHIVE_ZIP_PARAM_POST_EXTRACT, $v_local_header);');
2216 }
2217
2218 // ----- Return
2219 return $v_result;
2220 }
2221 // ---------------------------------------------------------------------------
2222
2223 // ---------------------------------------------------------------------------
2224 // Function : _extractFileAsString()
2225 // Description :
2226 // Parameters :
2227 // Return Values :
2228 // ---------------------------------------------------------------------------
2237 public function _extractFileAsString(&$p_entry, &$p_string)
2238 {
2239 $v_result=1;
2240
2241 // ----- Read the file header
2242 $v_header = [];
2243 if (1 != ($v_result = $this->_readFileHeader($v_header))) {
2244 // ----- Return
2245 return $v_result;
2246 }
2247
2248
2249 // ----- Check that the file header is coherent with $p_entry info
2250 // TBC
2251
2252 // ----- Trace
2253
2254 // ----- Do the extraction (if not a folder)
2255 if (!(0x00000010 == ($p_entry['external'] & 0x00000010))) {
2256 // ----- Look for not compressed file
2257 if ($p_entry['compressed_size'] == $p_entry['size']) {
2258 // ----- Trace
2259
2260 // ----- Reading the file
2261 $p_string = fread($this->_zip_fd, $p_entry['compressed_size']);
2262 } else {
2263 // ----- Trace
2264
2265 // ----- Reading the file
2266 $v_data = fread($this->_zip_fd, $p_entry['compressed_size']);
2267
2268 // ----- Decompress the file
2269 $p_string = gzinflate($v_data);
2270 }
2271
2272 // ----- Trace
2273 } else {
2274 // TBC : error : can not extract a folder in a string
2275 }
2276
2277 // ----- Return
2278 return $v_result;
2279 }
2280 // ---------------------------------------------------------------------------
2281
2282 // ---------------------------------------------------------------------------
2283 // Function : _readFileHeader()
2284 // Description :
2285 // Parameters :
2286 // Return Values :
2287 // ---------------------------------------------------------------------------
2295 public function _readFileHeader(&$p_header)
2296 {
2297 $v_result=1;
2298
2299 // ----- Read the 4 bytes signature
2300 $v_binary_data = @fread($this->_zip_fd, 4);
2301 $v_data = unpack('Vid', $v_binary_data);
2302
2303 // ----- Check signature
2304 if (0x04034b50 != $v_data['id']) {
2305
2306 // ----- Error log
2307 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
2308
2309 // ----- Return
2310 return Archive_Zip::errorCode();
2311 }
2312
2313 // ----- Read the first 42 bytes of the header
2314 $v_binary_data = fread($this->_zip_fd, 26);
2315
2316 // ----- Look for invalid block size
2317 if (26 != strlen($v_binary_data)) {
2318 $p_header['filename'] = '';
2319 $p_header['status'] = 'invalid_header';
2320
2321 // ----- Error log
2322 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid block size : ' . strlen($v_binary_data));
2323
2324 // ----- Return
2325 return Archive_Zip::errorCode();
2326 }
2327
2328 // ----- Extract the values
2329 $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
2330
2331 // ----- Get filename
2332 $p_header['filename'] = fread($this->_zip_fd, $v_data['filename_len']);
2333
2334 // ----- Get extra_fields
2335 if (0 != $v_data['extra_len']) {
2336 $p_header['extra'] = fread($this->_zip_fd, $v_data['extra_len']);
2337 } else {
2338 $p_header['extra'] = '';
2339 }
2340
2341 // ----- Extract properties
2342 $p_header['compression'] = $v_data['compression'];
2343 $p_header['size'] = $v_data['size'];
2344 $p_header['compressed_size'] = $v_data['compressed_size'];
2345 $p_header['crc'] = $v_data['crc'];
2346 $p_header['flag'] = $v_data['flag'];
2347
2348 // ----- Recuperate date in UNIX format
2349 $p_header['mdate'] = $v_data['mdate'];
2350 $p_header['mtime'] = $v_data['mtime'];
2351 if ($p_header['mdate'] && $p_header['mtime']) {
2352 // ----- Extract time
2353 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
2354 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
2355 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
2356
2357 // ----- Extract date
2358 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
2359 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
2360 $v_day = $p_header['mdate'] & 0x001F;
2361
2362 // ----- Get UNIX date format
2363 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
2364 } else {
2365 $p_header['mtime'] = time();
2366 }
2367
2368 // ----- Other informations
2369
2370 // TBC
2371 //for(reset($v_data); $key = key($v_data); next($v_data)) {
2372 //}
2373
2374 // ----- Set the stored filename
2375 $p_header['stored_filename'] = $p_header['filename'];
2376
2377 // ----- Set the status field
2378 $p_header['status'] = 'ok';
2379
2380 // ----- Return
2381 return $v_result;
2382 }
2383 // ---------------------------------------------------------------------------
2384
2385 // ---------------------------------------------------------------------------
2386 // Function : _readCentralFileHeader()
2387 // Description :
2388 // Parameters :
2389 // Return Values :
2390 // ---------------------------------------------------------------------------
2398 public function _readCentralFileHeader(&$p_header)
2399 {
2400 $v_result=1;
2401
2402 // ----- Read the 4 bytes signature
2403 $v_binary_data = @fread($this->_zip_fd, 4);
2404 $v_data = unpack('Vid', $v_binary_data);
2405
2406 // ----- Check signature
2407 if (0x02014b50 != $v_data['id']) {
2408
2409 // ----- Error log
2410 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
2411
2412 // ----- Return
2413 return Archive_Zip::errorCode();
2414 }
2415
2416 // ----- Read the first 42 bytes of the header
2417 $v_binary_data = fread($this->_zip_fd, 42);
2418
2419 // ----- Look for invalid block size
2420 if (42 != strlen($v_binary_data)) {
2421 $p_header['filename'] = '';
2422 $p_header['status'] = 'invalid_header';
2423
2424 // ----- Error log
2425 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid block size : ' . strlen($v_binary_data));
2426
2427 // ----- Return
2428 return Archive_Zip::errorCode();
2429 }
2430
2431 // ----- Extract the values
2432 $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);
2433
2434 // ----- Get filename
2435 if (0 != $p_header['filename_len']) {
2436 $p_header['filename'] = fread($this->_zip_fd, $p_header['filename_len']);
2437 } else {
2438 $p_header['filename'] = '';
2439 }
2440
2441 // ----- Get extra
2442 if (0 != $p_header['extra_len']) {
2443 $p_header['extra'] = fread($this->_zip_fd, $p_header['extra_len']);
2444 } else {
2445 $p_header['extra'] = '';
2446 }
2447
2448 // ----- Get comment
2449 if (0 != $p_header['comment_len']) {
2450 $p_header['comment'] = fread($this->_zip_fd, $p_header['comment_len']);
2451 } else {
2452 $p_header['comment'] = '';
2453 }
2454
2455 // ----- Extract properties
2456
2457 // ----- Recuperate date in UNIX format
2458 if ($p_header['mdate'] && $p_header['mtime']) {
2459 // ----- Extract time
2460 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
2461 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
2462 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
2463
2464 // ----- Extract date
2465 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
2466 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
2467 $v_day = $p_header['mdate'] & 0x001F;
2468
2469 // ----- Get UNIX date format
2470 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
2471 } else {
2472 $p_header['mtime'] = time();
2473 }
2474
2475 // ----- Set the stored filename
2476 $p_header['stored_filename'] = $p_header['filename'];
2477
2478 // ----- Set default status to ok
2479 $p_header['status'] = 'ok';
2480
2481 // ----- Look if it is a directory
2482 if ('/' == substr($p_header['filename'], -1)) {
2483 $p_header['external'] = 0x41FF0010;
2484 }
2485
2486
2487 // ----- Return
2488 return $v_result;
2489 }
2490 // ---------------------------------------------------------------------------
2491
2492 // ---------------------------------------------------------------------------
2493 // Function : _readEndCentralDir()
2494 // Description :
2495 // Parameters :
2496 // Return Values :
2497 // ---------------------------------------------------------------------------
2505 public function _readEndCentralDir(&$p_central_dir)
2506 {
2507 $v_result=1;
2508
2509 // ----- Go to the end of the zip file
2510 $v_size = filesize($this->_zipname);
2511 @fseek($this->_zip_fd, $v_size);
2512 if (@ftell($this->_zip_fd) != $v_size) {
2513 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
2514 'Unable to go to the end of the archive \''
2515 .$this->_zipname.'\'');
2516 return Archive_Zip::errorCode();
2517 }
2518
2519 // ----- First try : look if this is an archive with no commentaries
2520 // (most of the time)
2521 // in this case the end of central dir is at 22 bytes of the file end
2522 $v_found = 0;
2523 if ($v_size > 26) {
2524 @fseek($this->_zip_fd, $v_size-22);
2525 if (($v_pos = @ftell($this->_zip_fd)) != ($v_size-22)) {
2526 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
2527 'Unable to seek back to the middle of the archive \''
2528 .$this->_zipname.'\'');
2529 return Archive_Zip::errorCode();
2530 }
2531
2532 // ----- Read for bytes
2533 $v_binary_data = @fread($this->_zip_fd, 4);
2534 $v_data = unpack('Vid', $v_binary_data);
2535
2536 // ----- Check signature
2537 if (0x06054b50 == $v_data['id']) {
2538 $v_found = 1;
2539 }
2540
2541 $v_pos = ftell($this->_zip_fd);
2542 }
2543
2544 // ----- Go back to the maximum possible size of the Central Dir End Record
2545 if (!$v_found) {
2546 $v_maximum_size = 65557; // 0xFFFF + 22;
2547 if ($v_maximum_size > $v_size) {
2548 $v_maximum_size = $v_size;
2549 }
2550 @fseek($this->_zip_fd, $v_size-$v_maximum_size);
2551 if (@ftell($this->_zip_fd) != ($v_size-$v_maximum_size)) {
2552 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
2553 'Unable to seek back to the middle of the archive \''
2554 .$this->_zipname.'\'');
2555 return Archive_Zip::errorCode();
2556 }
2557
2558 // ----- Read byte per byte in order to find the signature
2559 $v_pos = ftell($this->_zip_fd);
2560 $v_bytes = 0x00000000;
2561 while ($v_pos < $v_size) {
2562 // ----- Read a byte
2563 $v_byte = @fread($this->_zip_fd, 1);
2564
2565 // ----- Add the byte
2566 $v_bytes = ($v_bytes << 8) | Ord($v_byte);
2567
2568 // ----- Compare the bytes
2569 if (0x504b0506 == $v_bytes) {
2570 $v_pos++;
2571 break;
2572 }
2573
2574 $v_pos++;
2575 }
2576
2577 // ----- Look if not found end of central dir
2578 if ($v_pos == $v_size) {
2579 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
2580 'Unable to find End of Central Dir Record signature'
2581 );
2582 return Archive_Zip::errorCode();
2583 }
2584 }
2585
2586 // ----- Read the first 18 bytes of the header
2587 $v_binary_data = fread($this->_zip_fd, 18);
2588
2589 // ----- Look for invalid block size
2590 if (18 != strlen($v_binary_data)) {
2591 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
2592 'Invalid End of Central Dir Record size : '
2593 . strlen($v_binary_data));
2594 return Archive_Zip::errorCode();
2595 }
2596
2597 // ----- Extract the values
2598 $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
2599
2600 // ----- Check the global size
2601 if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
2602 $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
2603 'Fail to find the right signature'
2604 );
2605 return Archive_Zip::errorCode();
2606 }
2607
2608 // ----- Get comment
2609 if (0 != $v_data['comment_size']) {
2610 $p_central_dir['comment'] = fread($this->_zip_fd, $v_data['comment_size']);
2611 } else {
2612 $p_central_dir['comment'] = '';
2613 }
2614
2615 $p_central_dir['entries'] = $v_data['entries'];
2616 $p_central_dir['disk_entries'] = $v_data['disk_entries'];
2617 $p_central_dir['offset'] = $v_data['offset'];
2618 $p_central_dir['size'] = $v_data['size'];
2619 $p_central_dir['disk'] = $v_data['disk'];
2620 $p_central_dir['disk_start'] = $v_data['disk_start'];
2621
2622 // ----- Return
2623 return $v_result;
2624 }
2625 // ---------------------------------------------------------------------------
2626
2627 // ---------------------------------------------------------------------------
2628 // Function : _deleteByRule()
2629 // Description :
2630 // Parameters :
2631 // Return Values :
2632 // ---------------------------------------------------------------------------
2641 public function _deleteByRule(&$p_result_list, &$p_params)
2642 {
2643 $v_result=1;
2644 $v_list_detail = [];
2645
2646 // ----- Open the zip file
2647 if (1 != ($v_result=$this->_openFd('rb'))) {
2648 // ----- Return
2649 return $v_result;
2650 }
2651
2652 // ----- Read the central directory informations
2653 $v_central_dir = [];
2654 if (1 != ($v_result = $this->_readEndCentralDir($v_central_dir))) {
2655 $this->_closeFd();
2656 return $v_result;
2657 }
2658
2659 // ----- Go to beginning of File
2660 @rewind($this->_zip_fd);
2661
2662 // ----- Scan all the files
2663 // ----- Start at beginning of Central Dir
2664 $v_pos_entry = $v_central_dir['offset'];
2665 @rewind($this->_zip_fd);
2666 if (@fseek($this->_zip_fd, $v_pos_entry)) {
2667 // ----- Clean
2668 $this->_closeFd();
2669
2670 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
2671 'Invalid archive size');
2672 return Archive_Zip::errorCode();
2673 }
2674
2675 // ----- Read each entry
2676 $v_header_list = [];
2677 $j_start = 0;
2678 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++) {
2679
2680 // ----- Read the file header
2681 $v_header_list[$v_nb_extracted] = [];
2682 $v_result
2683 = $this->_readCentralFileHeader($v_header_list[$v_nb_extracted]);
2684 if (1 != $v_result) {
2685 // ----- Clean
2686 $this->_closeFd();
2687
2688 return $v_result;
2689 }
2690
2691 // ----- Store the index
2692 $v_header_list[$v_nb_extracted]['index'] = $i;
2693
2694 // ----- Look for the specific extract rules
2695 $v_found = false;
2696
2697 // ----- Look for extract by name rule
2698 if ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
2699 && (0 != $p_params[ARCHIVE_ZIP_PARAM_BY_NAME])) {
2700
2701 // ----- Look if the filename is in the list
2702 for ($j=0;
2703 ($j < count($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
2704 && (!$v_found);
2705 $j++) {
2706
2707 // ----- Look for a directory
2708 if ('/' == substr($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j], -1)) {
2709
2710 // ----- Look if the directory is in the filename path
2711 if ((strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]))
2712 && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) {
2713 $v_found = true;
2714 } elseif ((0x00000010 == ($v_header_list[$v_nb_extracted]['external'] & 0x00000010)) /* Indicates a folder */
2715 && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) {
2716 $v_found = true;
2717 }
2718 }
2719 // ----- Look for a filename
2720 elseif ($v_header_list[$v_nb_extracted]['stored_filename']
2721 == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) {
2722 $v_found = true;
2723 }
2724 }
2725 }
2726
2727 // ----- Look for extract by ereg rule
2728 elseif ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_EREG]))
2729 && ('' != $p_params[ARCHIVE_ZIP_PARAM_BY_EREG])) {
2730 if (ereg($p_params[ARCHIVE_ZIP_PARAM_BY_EREG],
2731 $v_header_list[$v_nb_extracted]['stored_filename'])) {
2732 $v_found = true;
2733 }
2734 }
2735
2736 // ----- Look for extract by preg rule
2737 elseif ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_PREG]))
2738 && ('' != $p_params[ARCHIVE_ZIP_PARAM_BY_PREG])) {
2739 if (preg_match($p_params[ARCHIVE_ZIP_PARAM_BY_PREG],
2740 $v_header_list[$v_nb_extracted]['stored_filename'])) {
2741 $v_found = true;
2742 }
2743 }
2744
2745 // ----- Look for extract by index rule
2746 elseif ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]))
2747 && (0 != $p_params[ARCHIVE_ZIP_PARAM_BY_INDEX])) {
2748
2749 // ----- Look if the index is in the list
2750 for ($j=$j_start;
2751 ($j < count($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]))
2752 && (!$v_found);
2753 $j++) {
2754 if (($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start'])
2755 && ($i<=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end'])) {
2756 $v_found = true;
2757 }
2758 if ($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end']) {
2759 $j_start = $j+1;
2760 }
2761
2762 if ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start']>$i) {
2763 break;
2764 }
2765 }
2766 }
2767
2768 // ----- Look for deletion
2769 if ($v_found) {
2770 unset($v_header_list[$v_nb_extracted]);
2771 } else {
2772 $v_nb_extracted++;
2773 }
2774 }
2775
2776 // ----- Look if something need to be deleted
2777 if ($v_nb_extracted > 0) {
2778
2779 // ----- Creates a temporay file
2780 $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR.uniqid('archive_zip-')
2781 .'.tmp';
2782
2783 // ----- Creates a temporary zip archive
2784 $v_temp_zip = new Archive_Zip($v_zip_temp_name);
2785
2786 // ----- Open the temporary zip file in write mode
2787 if (1 != ($v_result = $v_temp_zip->_openFd('wb'))) {
2788 $this->_closeFd();
2789
2790 // ----- Return
2791 return $v_result;
2792 }
2793
2794 // ----- Look which file need to be kept
2795 for ($i=0; $i < count($v_header_list); $i++) {
2796
2797 // ----- Calculate the position of the header
2798 @rewind($this->_zip_fd);
2799 if (@fseek($this->_zip_fd, $v_header_list[$i]['offset'])) {
2800 // ----- Clean
2801 $this->_closeFd();
2802 $v_temp_zip->_closeFd();
2803 @unlink($v_zip_temp_name);
2804
2805 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
2806 'Invalid archive size');
2807 return Archive_Zip::errorCode();
2808 }
2809
2810 // ----- Read the file header
2811 if (1 != ($v_result = $this->_readFileHeader($v_header_list[$i]))) {
2812 // ----- Clean
2813 $this->_closeFd();
2814 $v_temp_zip->_closeFd();
2815 @unlink($v_zip_temp_name);
2816
2817 return $v_result;
2818 }
2819
2820 // ----- Write the file header
2821 $v_result = $v_temp_zip->_writeFileHeader($v_header_list[$i]);
2822 if (1 != $v_result) {
2823 // ----- Clean
2824 $this->_closeFd();
2825 $v_temp_zip->_closeFd();
2826 @unlink($v_zip_temp_name);
2827
2828 return $v_result;
2829 }
2830
2831 // ----- Read/write the data block
2832 $v_result = $this->_tool_CopyBlock($this->_zip_fd,
2833 $v_temp_zip->_zip_fd,
2834 $v_header_list[$i]['compressed_size']);
2835 if (1 != $v_result) {
2836 // ----- Clean
2837 $this->_closeFd();
2838 $v_temp_zip->_closeFd();
2839 @unlink($v_zip_temp_name);
2840
2841 return $v_result;
2842 }
2843 }
2844
2845 // ----- Store the offset of the central dir
2846 $v_offset = @ftell($v_temp_zip->_zip_fd);
2847
2848 // ----- Re-Create the Central Dir files header
2849 for ($i=0; $i < count($v_header_list); $i++) {
2850 // ----- Create the file header
2851 $v_result=$v_temp_zip->_writeCentralFileHeader($v_header_list[$i]);
2852 if (1 != $v_result) {
2853 // ----- Clean
2854 $v_temp_zip->_closeFd();
2855 $this->_closeFd();
2856 @unlink($v_zip_temp_name);
2857
2858 return $v_result;
2859 }
2860
2861 // ----- Transform the header to a 'usable' info
2862 $v_temp_zip->_convertHeader2FileInfo($v_header_list[$i],
2863 $p_result_list[$i]);
2864 }
2865
2866
2867 // ----- Zip file comment
2868 $v_comment = '';
2869
2870 // ----- Calculate the size of the central header
2871 $v_size = @ftell($v_temp_zip->_zip_fd)-$v_offset;
2872
2873 // ----- Create the central dir footer
2874 $v_result = $v_temp_zip->_writeCentralHeader(count($v_header_list),
2875 $v_size, $v_offset,
2876 $v_comment);
2877 if (1 != $v_result) {
2878 // ----- Clean
2879 unset($v_header_list);
2880 $v_temp_zip->_closeFd();
2881 $this->_closeFd();
2882 @unlink($v_zip_temp_name);
2883
2884 return $v_result;
2885 }
2886
2887 // ----- Close
2888 $v_temp_zip->_closeFd();
2889 $this->_closeFd();
2890
2891 // ----- Delete the zip file
2892 // TBC : I should test the result ...
2893 @unlink($this->_zipname);
2894
2895 // ----- Rename the temporary file
2896 // TBC : I should test the result ...
2897 //@rename($v_zip_temp_name, $this->_zipname);
2898 $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
2899
2900 // ----- Destroy the temporary archive
2901 unset($v_temp_zip);
2902 }
2903
2904 // ----- Return
2905 return $v_result;
2906 }
2907 // ---------------------------------------------------------------------------
2908
2909 // ---------------------------------------------------------------------------
2910 // Function : _dirCheck()
2911 // Description :
2912 // Check if a directory exists, if not it creates it and all the parents directory
2913 // which may be useful.
2914 // Parameters :
2915 // $p_dir : Directory path to check.
2916 // Return Values :
2917 // 1 : OK
2918 // -1 : Unable to create directory
2919 // ---------------------------------------------------------------------------
2929 public function _dirCheck($p_dir, $p_is_dir=false)
2930 {
2931 $v_result = 1;
2932
2933 // ----- Remove the final '/'
2934 if (($p_is_dir) && ('/' == substr($p_dir, -1))) {
2935 $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
2936 }
2937
2938 // ----- Check the directory availability
2939 if ((is_dir($p_dir)) || ('' == $p_dir)) {
2940 return 1;
2941 }
2942
2943 // ----- Extract parent directory
2944 $p_parent_dir = dirname($p_dir);
2945
2946 // ----- Just a check
2947 if ($p_parent_dir != $p_dir) {
2948 // ----- Look for parent directory
2949 if ('' != $p_parent_dir) {
2950 if (1 != ($v_result = $this->_dirCheck($p_parent_dir))) {
2951 return $v_result;
2952 }
2953 }
2954 }
2955
2956 // ----- Create the directory
2957 if (!@mkdir($p_dir, 0777)) {
2958 $this->_errorLog(ARCHIVE_ZIP_ERR_DIR_CREATE_FAIL,
2959 "Unable to create directory '$p_dir'");
2960 return Archive_Zip::errorCode();
2961 }
2962
2963 // ----- Return
2964 return $v_result;
2965 }
2966 // ---------------------------------------------------------------------------
2967
2968 // ---------------------------------------------------------------------------
2969 // Function : _merge()
2970 // Description :
2971 // If $p_archive_to_add does not exist, the function exit with a success result.
2972 // Parameters :
2973 // Return Values :
2974 // ---------------------------------------------------------------------------
2982 public function _merge(&$p_archive_to_add)
2983 {
2984 $v_result=1;
2985
2986 // ----- Look if the archive_to_add exists
2987 if (!is_file($p_archive_to_add->_zipname)) {
2988 // ----- Nothing to merge, so merge is a success
2989 return 1;
2990 }
2991
2992 // ----- Look if the archive exists
2993 if (!is_file($this->_zipname)) {
2994 // ----- Do a duplicate
2995 $v_result = $this->_duplicate($p_archive_to_add->_zipname);
2996
2997 return $v_result;
2998 }
2999
3000 // ----- Open the zip file
3001 if (1 != ($v_result=$this->_openFd('rb'))) {
3002 return $v_result;
3003 }
3004
3005 // ----- Read the central directory informations
3006 $v_central_dir = [];
3007 if (1 != ($v_result = $this->_readEndCentralDir($v_central_dir))) {
3008 $this->_closeFd();
3009 return $v_result;
3010 }
3011
3012 // ----- Go to beginning of File
3013 @rewind($this->_zip_fd);
3014
3015 // ----- Open the archive_to_add file
3016 if (1 != ($v_result=$p_archive_to_add->_openFd('rb'))) {
3017 $this->_closeFd();
3018 return $v_result;
3019 }
3020
3021 // ----- Read the central directory informations
3022 $v_central_dir_to_add = [];
3023 $v_result = $p_archive_to_add->_readEndCentralDir($v_central_dir_to_add);
3024 if (1 != $v_result) {
3025 $this->_closeFd();
3026 $p_archive_to_add->_closeFd();
3027 return $v_result;
3028 }
3029
3030 // ----- Go to beginning of File
3031 @rewind($p_archive_to_add->_zip_fd);
3032
3033 // ----- Creates a temporay file
3034 $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR.uniqid('archive_zip-').'.tmp';
3035
3036 // ----- Open the temporary file in write mode
3037 if (0 == ($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb'))) {
3038 $this->_closeFd();
3039 $p_archive_to_add->_closeFd();
3040 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
3041 'Unable to open temporary file \''
3042 .$v_zip_temp_name.'\' in binary write mode');
3043 return Archive_Zip::errorCode();
3044 }
3045
3046 // ----- Copy the files from the archive to the temporary file
3047 // TBC : Here I should better append the file and go back to erase the
3048 // central dir
3049 $v_size = $v_central_dir['offset'];
3050 while (0 != $v_size) {
3051 $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
3052 ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
3053 $v_buffer = fread($this->_zip_fd, $v_read_size);
3054 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
3055 $v_size -= $v_read_size;
3056 }
3057
3058 // ----- Copy the files from the archive_to_add into the temporary file
3059 $v_size = $v_central_dir_to_add['offset'];
3060 while (0 != $v_size) {
3061 $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
3062 ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
3063 $v_buffer = fread($p_archive_to_add->_zip_fd, $v_read_size);
3064 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
3065 $v_size -= $v_read_size;
3066 }
3067
3068 // ----- Store the offset of the central dir
3069 $v_offset = @ftell($v_zip_temp_fd);
3070
3071 // ----- Copy the block of file headers from the old archive
3072 $v_size = $v_central_dir['size'];
3073 while (0 != $v_size) {
3074 $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
3075 ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
3076 $v_buffer = @fread($this->_zip_fd, $v_read_size);
3077 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
3078 $v_size -= $v_read_size;
3079 }
3080
3081 // ----- Copy the block of file headers from the archive_to_add
3082 $v_size = $v_central_dir_to_add['size'];
3083 while (0 != $v_size) {
3084 $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
3085 ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
3086 $v_buffer = @fread($p_archive_to_add->_zip_fd, $v_read_size);
3087 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
3088 $v_size -= $v_read_size;
3089 }
3090
3091 // ----- Zip file comment
3092 // TBC : I should merge the two comments
3093 $v_comment = '';
3094
3095 // ----- Calculate the size of the (new) central header
3096 $v_size = @ftell($v_zip_temp_fd)-$v_offset;
3097
3098 // ----- Swap the file descriptor
3099 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
3100 // the following methods on the temporary fil and not the real archive fd
3101 $v_swap = $this->_zip_fd;
3102 $this->_zip_fd = $v_zip_temp_fd;
3103 $v_zip_temp_fd = $v_swap;
3104
3105 // ----- Create the central dir footer
3106 if (1 != ($v_result = $this->_writeCentralHeader($v_central_dir['entries']
3107 +$v_central_dir_to_add['entries'],
3108 $v_size, $v_offset,
3109 $v_comment))) {
3110 $this->_closeFd();
3111 $p_archive_to_add->_closeFd();
3112 @fclose($v_zip_temp_fd);
3113 $this->_zip_fd = null;
3114
3115 // ----- Reset the file list
3116 unset($v_header_list);
3117
3118 // ----- Return
3119 return $v_result;
3120 }
3121
3122 // ----- Swap back the file descriptor
3123 $v_swap = $this->_zip_fd;
3124 $this->_zip_fd = $v_zip_temp_fd;
3125 $v_zip_temp_fd = $v_swap;
3126
3127 // ----- Close
3128 $this->_closeFd();
3129 $p_archive_to_add->_closeFd();
3130
3131 // ----- Close the temporary file
3132 @fclose($v_zip_temp_fd);
3133
3134 // ----- Delete the zip file
3135 // TBC : I should test the result ...
3136 @unlink($this->_zipname);
3137
3138 // ----- Rename the temporary file
3139 // TBC : I should test the result ...
3140 //@rename($v_zip_temp_name, $this->_zipname);
3141 $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
3142
3143 // ----- Return
3144 return $v_result;
3145 }
3146 // ---------------------------------------------------------------------------
3147
3148 // ---------------------------------------------------------------------------
3149 // Function : _duplicate()
3150 // Description :
3151 // Parameters :
3152 // Return Values :
3153 // ---------------------------------------------------------------------------
3161 public function _duplicate($p_archive_filename)
3162 {
3163 $v_result=1;
3164
3165 // ----- Look if the $p_archive_filename exists
3166 if (!is_file($p_archive_filename)) {
3167
3168 // ----- Nothing to duplicate, so duplicate is a success.
3169 $v_result = 1;
3170
3171 // ----- Return
3172 return $v_result;
3173 }
3174
3175 // ----- Open the zip file
3176 if (1 != ($v_result=$this->_openFd('wb'))) {
3177 // ----- Return
3178 return $v_result;
3179 }
3180
3181 // ----- Open the temporary file in write mode
3182 if (0 == ($v_zip_temp_fd = @fopen($p_archive_filename, 'rb'))) {
3183 $this->_closeFd();
3184 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
3185 'Unable to open archive file \''
3186 .$p_archive_filename.'\' in binary write mode');
3187 return Archive_Zip::errorCode();
3188 }
3189
3190 // ----- Copy the files from the archive to the temporary file
3191 // TBC : Here I should better append the file and go back to erase the
3192 // central dir
3193 $v_size = filesize($p_archive_filename);
3194 while (0 != $v_size) {
3195 $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
3196 ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
3197 $v_buffer = fread($v_zip_temp_fd, $v_read_size);
3198 @fwrite($this->_zip_fd, $v_buffer, $v_read_size);
3199 $v_size -= $v_read_size;
3200 }
3201
3202 // ----- Close
3203 $this->_closeFd();
3204
3205 // ----- Close the temporary file
3206 @fclose($v_zip_temp_fd);
3207
3208 return $v_result;
3209 }
3210 // ---------------------------------------------------------------------------
3211
3221 public function _check_parameters(&$p_params, $p_default)
3222 {
3223
3224 // ----- Check that param is an array
3225 if (!is_array($p_params)) {
3226 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
3227 'Unsupported parameter, waiting for an array');
3228 return Archive_Zip::errorCode();
3229 }
3230
3231 // ----- Check that all the params are valid
3232 for (reset($p_params); list($v_key, $v_value) = each($p_params);) {
3233 if (!isset($p_default[$v_key])) {
3234 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
3235 'Unsupported parameter with key \''.$v_key.'\'');
3236
3237 return Archive_Zip::errorCode();
3238 }
3239 }
3240
3241 // ----- Set the default values
3242 for (reset($p_default); list($v_key, $v_value) = each($p_default);) {
3243 if (!isset($p_params[$v_key])) {
3244 $p_params[$v_key] = $p_default[$v_key];
3245 }
3246 }
3247
3248 // ----- Check specific parameters
3249 $v_callback_list = [
3250 'callback_pre_add', 'callback_post_add',
3251 'callback_pre_extract', 'callback_post_extract'
3252 ];
3253 for ($i=0; $i < count($v_callback_list); $i++) {
3254 $v_key=$v_callback_list[$i];
3255 if ((isset($p_params[$v_key])) && ('' != $p_params[$v_key])) {
3256 if (!function_exists($p_params[$v_key])) {
3257 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAM_VALUE,
3258 "Callback '".$p_params[$v_key]
3259 ."()' is not an existing function for "
3260 ."parameter '".$v_key."'");
3261 return Archive_Zip::errorCode();
3262 }
3263 }
3264 }
3265
3266 return(1);
3267 }
3268 // ---------------------------------------------------------------------------
3269
3270 // ---------------------------------------------------------------------------
3271 // Function : _errorLog()
3272 // Description :
3273 // Parameters :
3274 // ---------------------------------------------------------------------------
3283 public function _errorLog($p_error_code=0, $p_error_string='')
3284 {
3285 $this->_error_code = $p_error_code;
3286 $this->_error_string = $p_error_string;
3287 }
3288 // ---------------------------------------------------------------------------
3289
3290 // ---------------------------------------------------------------------------
3291 // Function : _errorReset()
3292 // Description :
3293 // Parameters :
3294 // ---------------------------------------------------------------------------
3301 public function _errorReset()
3302 {
3303 $this->_error_code = 1;
3304 $this->_error_string = '';
3305 }
3306 // ---------------------------------------------------------------------------
3307
3308 // ---------------------------------------------------------------------------
3309 // Function : $this->_tool_PathReduction()
3310 // Description :
3311 // Parameters :
3312 // Return Values :
3313 // ---------------------------------------------------------------------------
3321 public function _tool_PathReduction($p_dir)
3322 {
3323 $v_result = '';
3324
3325 // ----- Look for not empty path
3326 if ('' != $p_dir) {
3327 // ----- Explode path by directory names
3328 $v_list = explode('/', $p_dir);
3329
3330 // ----- Study directories from last to first
3331 for ($i= count($v_list) - 1; $i >= 0; $i--) {
3332 // ----- Look for current path
3333 if ('.' == $v_list[$i]) {
3334 // ----- Ignore this directory
3335 // Should be the first $i=0, but no check is done
3336 } elseif ('..' == $v_list[$i]) {
3337 // ----- Ignore it and ignore the $i-1
3338 $i--;
3339 } elseif (('' == $v_list[$i]) && ($i != (count($v_list) - 1)) && (0 != $i)) {
3340 // ----- Ignore only the double '//' in path,
3341 // but not the first and last '/'
3342 } else {
3343 $v_result = $v_list[$i].($i!=(count($v_list) - 1)? '/' . $v_result: '');
3344 }
3345 }
3346 }
3347
3348 // ----- Return
3349 return $v_result;
3350 }
3351 // ---------------------------------------------------------------------------
3352
3353 // ---------------------------------------------------------------------------
3354 // Function : $this->_tool_PathInclusion()
3355 // Description :
3356 // This function indicates if the path $p_path is under the $p_dir tree. Or,
3357 // said in an other way, if the file or sub-dir $p_path is inside the dir
3358 // $p_dir.
3359 // The function indicates also if the path is exactly the same as the dir.
3360 // This function supports path with duplicated '/' like '//', but does not
3361 // support '.' or '..' statements.
3362 // Parameters :
3363 // Return Values :
3364 // 0 if $p_path is not inside directory $p_dir
3365 // 1 if $p_path is inside directory $p_dir
3366 // 2 if $p_path is exactly the same as $p_dir
3367 // ---------------------------------------------------------------------------
3376 public function _tool_PathInclusion($p_dir, $p_path)
3377 {
3378 $v_result = 1;
3379
3380 // ----- Explode dir and path by directory separator
3381 $v_list_dir = explode('/', $p_dir);
3382 $v_list_dir_size = count($v_list_dir);
3383 $v_list_path = explode('/', $p_path);
3384 $v_list_path_size = count($v_list_path);
3385
3386 // ----- Study directories paths
3387 $i = 0;
3388 $j = 0;
3389 while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
3390
3391 // ----- Look for empty dir (path reduction)
3392 if ('' == $v_list_dir[$i]) {
3393 $i++;
3394 continue;
3395 }
3396 if ('' == $v_list_path[$j]) {
3397 $j++;
3398 continue;
3399 }
3400
3401 // ----- Compare the items
3402 if (($v_list_dir[$i] != $v_list_path[$j])
3403 && ('' != $v_list_dir[$i])
3404 && ('' != $v_list_path[$j])) {
3405 $v_result = 0;
3406 }
3407
3408 // ----- Next items
3409 $i++;
3410 $j++;
3411 }
3412
3413 // ----- Look if everything seems to be the same
3414 if ($v_result) {
3415 // ----- Skip all the empty items
3416 while (($j < $v_list_path_size) && ('' == $v_list_path[$j])) {
3417 $j++;
3418 }
3419 while (($i < $v_list_dir_size) && ('' == $v_list_dir[$i])) {
3420 $i++;
3421 }
3422
3423 if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
3424 // ----- There are exactly the same
3425 $v_result = 2;
3426 } elseif ($i < $v_list_dir_size) {
3427 // ----- The path is shorter than the dir
3428 $v_result = 0;
3429 }
3430 }
3431
3432 // ----- Return
3433 return $v_result;
3434 }
3435 // ---------------------------------------------------------------------------
3436
3437 // ---------------------------------------------------------------------------
3438 // Function : $this->_tool_CopyBlock()
3439 // Description :
3440 // Parameters :
3441 // $p_mode : read/write compression mode
3442 // 0 : src & dest normal
3443 // 1 : src gzip, dest normal
3444 // 2 : src normal, dest gzip
3445 // 3 : src & dest gzip
3446 // Return Values :
3447 // ---------------------------------------------------------------------------
3459 public function _tool_CopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
3460 {
3461 $v_result = 1;
3462
3463 if (0 == $p_mode) {
3464 while (0 != $p_size) {
3465 $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
3466 ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
3467 $v_buffer = @fread($p_src, $v_read_size);
3468 @fwrite($p_dest, $v_buffer, $v_read_size);
3469 $p_size -= $v_read_size;
3470 }
3471 } elseif (1 == $p_mode) {
3472 while (0 != $p_size) {
3473 $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
3474 ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
3475 $v_buffer = @gzread($p_src, $v_read_size);
3476 @fwrite($p_dest, $v_buffer, $v_read_size);
3477 $p_size -= $v_read_size;
3478 }
3479 } elseif (2 == $p_mode) {
3480 while (0 != $p_size) {
3481 $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
3482 ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
3483 $v_buffer = @fread($p_src, $v_read_size);
3484 @gzwrite($p_dest, $v_buffer, $v_read_size);
3485 $p_size -= $v_read_size;
3486 }
3487 } elseif (3 == $p_mode) {
3488 while (0 != $p_size) {
3489 $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
3490 ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
3491 $v_buffer = @gzread($p_src, $v_read_size);
3492 @gzwrite($p_dest, $v_buffer, $v_read_size);
3493 $p_size -= $v_read_size;
3494 }
3495 }
3496
3497 // ----- Return
3498 return $v_result;
3499 }
3500 // ---------------------------------------------------------------------------
3501
3502 // ---------------------------------------------------------------------------
3503 // Function : $this->_tool_Rename()
3504 // Description :
3505 // This function tries to do a simple rename() function. If it fails, it
3506 // tries to copy the $p_src file in a new $p_dest file and then unlink the
3507 // first one.
3508 // Parameters :
3509 // $p_src : Old filename
3510 // $p_dest : New filename
3511 // Return Values :
3512 // 1 on success, 0 on failure.
3513 // ---------------------------------------------------------------------------
3522 public function _tool_Rename($p_src, $p_dest)
3523 {
3524 $v_result = 1;
3525
3526 // ----- Try to rename the files
3527 if (!@rename($p_src, $p_dest)) {
3528
3529 // ----- Try to copy & unlink the src
3530 if (!@copy($p_src, $p_dest)) {
3531 $v_result = 0;
3532 } elseif (!@unlink($p_src)) {
3533 $v_result = 0;
3534 }
3535 }
3536
3537 // ----- Return
3538 return $v_result;
3539 }
3540 // ---------------------------------------------------------------------------
3541
3542 // ---------------------------------------------------------------------------
3543 // Function : $this->_tool_TranslateWinPath()
3544 // Description :
3545 // Translate windows path by replacing '\' by '/' and optionally removing
3546 // drive letter.
3547 // Parameters :
3548 // $p_path : path to translate.
3549 // $p_remove_disk_letter : true | false
3550 // Return Values :
3551 // The path translated.
3552 // ---------------------------------------------------------------------------
3562 public function _tool_TranslateWinPath($p_path, $p_remove_disk_letter=true)
3563 {
3564 if (stristr(php_uname(), 'windows')) {
3565 // ----- Look for potential disk letter
3566 if (($p_remove_disk_letter)
3567 && (false != ($v_position = strpos($p_path, ':')))) {
3568 $p_path = substr($p_path, $v_position+1);
3569 }
3570 // ----- Change potential windows directory separator
3571 if ((strpos($p_path, '\\') > 0) || ('\\' == substr($p_path, 0, 1))) {
3572 $p_path = strtr($p_path, '\\', '/');
3573 }
3574 }
3575 return $p_path;
3576 }
3577 // ---------------------------------------------------------------------------
3578}
3579 // End of class
3580;
_extractByRule(&$p_file_list, &$p_params)
_dirCheck($p_dir, $p_is_dir=false)
_readFileHeader(&$p_header)
_merge(&$p_archive_to_add)
_deleteByRule(&$p_result_list, &$p_params)
_tool_CopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
_readEndCentralDir(&$p_central_dir)
_tool_TranslateWinPath($p_path, $p_remove_disk_letter=true)
_check_parameters(&$p_params, $p_default)
_list(&$p_list)
extract($p_params=0)
_add($p_list, &$p_result_list, &$p_params)
_tool_Rename($p_src, $p_dest)
create($p_filelist, $p_params=0)
_checkFormat($p_level=0)
__construct($p_zipname)
_tool_PathInclusion($p_dir, $p_path)
_tool_PathReduction($p_dir)
_openFd($p_mode)
_duplicate($p_archive_filename)
_readCentralFileHeader(&$p_header)
_errorLog($p_error_code=0, $p_error_string='')
_create($p_list, &$p_result_list, &$p_params)
add($p_filelist, $p_params=0)